1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
mod rules;

use crate::errors::*;
use crate::cmd::run_cmd::prepare_keyring;
use crate::cmd::run_cmd::Params;
use crate::engine::Module;
use crate::options;
use crate::shell::Shell;
use crate::term::SpinLogger;
use crate::worker;
use self::rules::Glob;
use serde::{Serialize, Deserialize};
use structopt::StructOpt;
use sn0int_common::metadata::Source;
use sn0int_std::blobs::Blob;
use sn0int_std::ratelimits::Ratelimiter;
use std::collections::HashMap;

#[derive(Debug, StructOpt, Serialize)]
pub struct Notification {
    pub subject: String,
    pub body: Option<String>,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct NotificationConfig {
    #[serde(default)]
    pub workspaces: Vec<String>,
    #[serde(default)]
    pub topics: Vec<Glob>,
    pub script: String,
    #[serde(default)]
    pub options: Vec<options::Opt>,
}

fn apply_rule<T>(name: &str, filters: &[T], value: &str, cmp: fn(&T, &str) -> Option<bool>) -> bool {
    if !filters.is_empty() {
        debug!("{} filter is active", name);
        for filter in filters {
            match cmp(filter, value) {
                Some(true) => {
                    debug!("{} was allow-listed", name);
                    return true;
                }
                Some(false) => {
                    debug!("{} was excluded", name);
                    return false;
                }
                _ => (),
            }
        }
        debug!("{} didn't match any rules, skipping", name);
        false
    } else {
        true
    }
}

impl NotificationConfig {
    fn matches(&self, name: &str, workspace: &str, topic: &str) -> bool {
        debug!("Testing notification with rules: {:?}", name);
        if !apply_rule("workspace", &self.workspaces, workspace, |filter, value| if filter == value { Some(true) } else { None }) {
            return false;
        }
        if !apply_rule("topic", &self.topics, topic, |filter, value| filter.matches(value)) {
            return false;
        }
        debug!("Notification matches this config");
        true
    }
}

pub fn trigger_notify_event<T: SpinLogger>(rl: &mut Shell, spinner: &mut T, ratelimit: &mut Ratelimiter, topic: &str, notification: &Notification) -> Result<()> {
    run_router(rl, spinner, ratelimit, false, topic, notification)
}

fn prepare_arg(notification: &Notification) -> Result<(serde_json::Value, Option<String>, Vec<Blob>)> {
    let arg = serde_json::to_value(notification)?;
    Ok((arg, None, vec![]))
}

pub fn exec(rl: &mut Shell, module: &Module, ratelimit: &mut Ratelimiter, options: HashMap<String, String>, verbose: u64, notification: &Notification) -> Result<usize> {
    let module_name = module.canonical();
    debug!("Setting up notification execution with {:?}", module_name);

    if *module.source() != Some(Source::Notifications) {
        bail!("Module doesn't take notifications as source");
    }

    let params = Params {
        threads: 1,
        verbose,
        stdin: false,
        grants: &[],
        grant_full_keyring: false,
        deny_keyring: false,
        exit_on_error: false,
    };

    prepare_keyring(rl.keyring_mut(), &module, &params)?;
    let args = vec![prepare_arg(&notification)?];

    debug!("Executing notification module {:?}", module_name);
    let errors = worker::spawn(rl, &module, ratelimit, args, &params, rl.config().network.proxy, options);
    debug!("Notification module {:?} exited with {:?} errors", module_name, errors);

    Ok(errors)
}

pub fn run_router<T: SpinLogger>(rl: &mut Shell, spinner: &mut T, ratelimit: &mut Ratelimiter, dry_run: bool, topic: &str, notification: &Notification) -> Result<()> {
    let configs = rl.config().notifications.clone();

    debug!("Running notification router");
    for (name, config) in configs {
        if rl.signal_register().ctrlc_received() {
            debug!("Exiting notification router due to ctrl-c");
            break;
        }

        if config.matches(&name, rl.workspace(), topic) {
            let module = rl.library().get(&config.script)?.clone();
            if dry_run {
                spinner.success(&format!("Executed {} for {:?} (dry-run)", module.canonical(), name));
            } else {
                let options = options::Opt::collect(&config.options);
                match exec(rl, &module, ratelimit, options, 0, notification) {
                    Ok(0) => {
                        let msg = format!("Executed {} for {:?}", module.canonical(), name);
                        spinner.success(&msg);
                    },
                    Ok(errors) => {
                        let msg = format!("Executed {} for {:?} ({} errors)", module.canonical(), name, errors);
                        spinner.error(&msg);
                    },
                    Err(err) => {
                        spinner.error(&format!("Fatal {} for {:?}: {}", module.canonical(), name, err));
                    },
                }
            }
        }
    }
    debug!("Notification router finished");

    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;

    fn mkconfig(topics: &[&str]) -> NotificationConfig {
        let topics = topics.iter()
            .map(|s| s.parse::<Glob>())
            .collect::<Result<Vec<_>>>().unwrap();

        NotificationConfig {
            workspaces: Vec::new(),
            topics,
            script: "some/script".to_string(),
            options: Vec::new(),
        }
    }

    #[test]
    fn test_empty_topic() {
        let config = mkconfig(&[]);
        assert!(config.matches("name", "workspace", "topic"));
    }

    #[test]
    fn test_match_topic() {
        let config = mkconfig(&[
            "db:subdomain:example.com:*",
        ]);
        assert!(config.matches("name", "workspace", "db:subdomain:example.com:update"));
    }

    #[test]
    fn test_not_match_topic() {
        let config = mkconfig(&[
            "db:subdomain:example.com:*",
        ]);
        assert!(!config.matches("name", "workspace", "db:subdomain:foobar.com:update"));
    }

    #[test]
    fn test_exclude_topic() {
        let config = mkconfig(&[
            "!db:subdomain:example.com:*",
        ]);
        assert!(!config.matches("name", "workspace", "db:subdomain:example.com:update"));
    }

    #[test]
    fn test_exclude_other_topic() {
        let config = mkconfig(&[
            "!db:subdomain:example.com:*",
            "db:subdomain:foobar.com:*",
        ]);
        assert!(config.matches("name", "workspace", "db:subdomain:foobar.com:update"));
    }

    #[test]
    fn test_no_inverse_does_not_imply_match() {
        let config = mkconfig(&[
            "!db:subdomain:example.com:*",
        ]);
        assert!(!config.matches("name", "workspace", "db:subdomain:foobar.com:update"));
    }

    #[test]
    fn test_everything_except() {
        let config = mkconfig(&[
            "!db:subdomain:example.com:*",
            "*:*:*:*",
        ]);
        assert!(config.matches("name", "workspace", "db:subdomain:foobar.com:update"));
    }

    #[test]
    fn test_exclude_everything() {
        let config = mkconfig(&[
            "!*:*:*:*",
        ]);
        assert!(!config.matches("name", "workspace", "db:subdomain:foobar.com:update"));
    }

    #[test]
    fn test_execute_in_order_1() {
        let config = mkconfig(&[
            "!*:*:*:*",
            "*:*:*:*",
        ]);
        assert!(!config.matches("name", "workspace", "db:subdomain:foobar.com:update"));
    }

    #[test]
    fn test_execute_in_order_2() {
        let config = mkconfig(&[
            "*:*:*:*",
            "!*:*:*:*",
        ]);
        assert!(config.matches("name", "workspace", "db:subdomain:foobar.com:update"));
    }
}