fisher 1.0.0

Webhooks catcher written in Rust
Documentation
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
// Copyright (C) 2016-2017 Pietro Albini
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

use std::fs::File;
use std::io::{BufRead, BufReader};
use std::sync::Arc;

use regex::Regex;
use serde_json;

use common::prelude::*;
use common::state::{IdKind, State, UniqueId};

use providers::Provider;
use requests::{Request, RequestType};


#[derive(Debug, Clone)]
pub struct ScriptProvider {
    pub script: Arc<Script>,
    pub provider: Arc<Provider>,
}


lazy_static! {
    static ref PREFERENCES_HEADER_RE: Regex = Regex::new(
        r"## Fisher: (.*)"
    ).unwrap();
    static ref PROVIDER_HEADER_RE: Regex = Regex::new(
        r"## Fisher-([a-zA-Z]+): (.*)"
    ).unwrap();
}


#[derive(Debug, Deserialize)]
struct Preferences {
    priority: Option<isize>,
    parallel: Option<bool>,
}

impl Preferences {
    fn empty() -> Self {
        Preferences {
            priority: None,
            parallel: None,
        }
    }

    #[inline]
    fn priority(&self) -> isize {
        self.priority.unwrap_or(0)
    }

    #[inline]
    fn parallel(&self) -> bool {
        self.parallel.unwrap_or(true)
    }
}


struct LoadHeadersOutput {
    preferences: Preferences,
    providers: Vec<Arc<Provider>>,
}


fn load_headers(file: &str) -> Result<LoadHeadersOutput> {
    let f = File::open(file).unwrap();
    let reader = BufReader::new(f);

    let mut content;
    let mut line_number: u32 = 0;
    let mut providers = vec![];
    let mut preferences = None;
    for line in reader.lines() {
        line_number += 1;
        content = line.unwrap();

        // Just ignore everything after an empty line
        if content == "" {
            break;
        }

        if preferences.is_none() {
            if let Some(cap) = PREFERENCES_HEADER_RE.captures(&content) {
                preferences = Some(serde_json::from_str(&cap[1])?);
                continue; // Don't capture anything else for this line
            }
        }

        if let Some(cap) = PROVIDER_HEADER_RE.captures(&content) {
            let name = &cap[1];
            let data = &cap[2];

            match Provider::new(name, data) {
                Ok(provider) => {
                    providers.push(Arc::new(provider));
                }
                Err(mut error) => {
                    Err(error.chain_err(|| ErrorKind::ScriptParsingError(
                        file.into(), line_number,
                    )))?;
                }
            }
        }
    }

    Ok(LoadHeadersOutput {
        preferences: if let Some(pref) = preferences {
            pref
        } else {
            Preferences::empty()
        },
        providers: providers,
    })
}


#[derive(Debug)]
pub struct Script {
    id: UniqueId,
    name: String,
    exec: String,
    priority: isize,
    parallel: bool,
    pub(crate) providers: Vec<Arc<Provider>>,
}

impl Script {
    pub fn load(
        name: String,
        exec: String,
        state: &Arc<State>,
    ) -> Result<Self> {
        let headers = load_headers(&exec)?;

        Ok(Script {
            id: state.next_id(IdKind::HookId),
            name: name,
            exec: exec,
            priority: headers.preferences.priority(),
            parallel: headers.preferences.parallel(),
            providers: headers.providers,
        })
    }

    pub fn validate(
        &self,
        req: &Request,
    ) -> (RequestType, Option<Arc<Provider>>) {
        if !self.providers.is_empty() {
            // Check every provider if they're present
            for provider in &self.providers {
                let result = provider.validate(req);

                if result != RequestType::Invalid {
                    return (result, Some(provider.clone()));
                }
            }
            (RequestType::Invalid, None)
        } else {
            (RequestType::ExecuteHook, None)
        }
    }

    pub fn name(&self) -> &str {
        &self.name
    }

    pub fn exec(&self) -> &str {
        &self.exec
    }

    pub fn priority(&self) -> isize {
        self.priority
    }
}

impl ScriptTrait for Script {
    type Id = UniqueId;

    fn id(&self) -> UniqueId {
        self.id
    }

    fn can_be_parallel(&self) -> bool {
        self.parallel
    }
}


#[cfg(test)]
mod tests {
    use common::prelude::*;
    use requests::{Request, RequestType};
    use scripts::test_utils::*;


    #[test]
    fn test_scripts_are_loaded_properly() {
        // This little helper avoids repeating code all the time
        fn create_and_assert(
            env: &TestEnv,
            name: &str,
            content: &[&str],
            priority: isize,
            parallel: bool,
            providers: &[&str],
        ) -> Result<()> {
            // Create and load the script
            env.create_script(name, content)?;
            let script = env.load_script(name)?;

            // Check if the basic attributes are loaded properly
            assert_eq!(script.name(), name);
            assert_eq!(script.priority(), priority);
            assert_eq!(script.can_be_parallel(), parallel);

            // Check if the executable path is correct
            assert_eq!(
                script.exec(),
                env.scripts_dir().join(name).to_str().unwrap()
            );

            assert_eq!(script.providers.len(), providers.len());
            for provider in &script.providers {
                assert!(providers.contains(&provider.name()));
            }

            Ok(())
        }

        test_wrapper(|env| {
            // Check if naked scripts are loaded properly
            create_and_assert(
                &env,
                "naked.sh",
                &[r#"#!/bin/bash"#, r#"echo "This is a naked script""#],
                0,
                true,
                &[],
            )?;

            // Check if scripts with preferences are loaded properly
            create_and_assert(
                &env,
                "prefs.sh",
                &[
                    r#"#!/bin/bash"#,
                    r#"## Fisher: {"parallel": false, "priority": 5}"#,
                    r#"echo "This script has preferences""#,
                ],
                5,
                false,
                &[],
            )?;

            // Check if scripts with one provider are loaded properly
            create_and_assert(
                &env,
                "one-provider.sh",
                &[
                    r#"#!/bin/bash"#,
                    r#"## Fisher-Testing: {}"#,
                    r#"echo "This script has one provider""#,
                ],
                0,
                true,
                &["Testing"],
            )?;

            // Check if scripts with preferences and providers are loaded properly
            create_and_assert(
                &env,
                "provider-prefs.sh",
                &[
                    r#"#!/bin/bash"#,
                    r#"## Fisher: {"parallel": false, "priority": 5}"#,
                    r#"## Fisher-Testing: {}"#,
                    r#"echo "This script has one provider and some preferences""#,
                ],
                5,
                false,
                &["Testing"],
            )?;

            // Check if scripts with two providers are loaded properly
            create_and_assert(
                &env,
                "two-providers.sh",
                &[
                    r#"#!/bin/bash"#,
                    r#"## Fisher-Testing: {}"#,
                    r#"## Fisher-Standalone: {"secret": "abcde"}"#,
                    r#"echo "This script has one provider""#,
                ],
                0,
                true,
                &["Testing", "Standalone"],
            )?;

            Ok(())
        });
    }


    #[test]
    fn test_requests_can_be_validated_against_scripts() {
        test_wrapper(|env| {
            // Create all the needed scripts
            env.create_script(
                "single.sh",
                &[r#"#!/bin/bash"#, r#"## Fisher-Testing: {}"#, r#"echo "ok""#],
            )?;
            env.create_script(
                "failing.sh",
                &[
                    r#"#!/bin/bash"#,
                    r#"## Fisher-Standalone: {"secret": "abcde"}"#,
                    r#"echo "ok""#,
                ],
            )?;
            env.create_script(
                "multiple1.sh",
                &[
                    r#"#!/bin/bash"#,
                    r#"## Fisher-Testing: {}"#,
                    r#"## Fisher-Standalone: {"secret": "abcde"}"#,
                    r#"echo "ok""#,
                ],
            )?;
            env.create_script(
                "multiple2.sh",
                &[
                    r#"#!/bin/bash"#,
                    r#"## Fisher-Standalone: {"secret": "abcde"}"#,
                    r#"## Fisher-Testing: {}"#,
                    r#"echo "ok""#,
                ],
            )?;

            // Load all the needed scripts
            let single = env.load_script("single.sh")?;
            let failing = env.load_script("failing.sh")?;
            let multiple1 = env.load_script("multiple1.sh")?;
            let multiple2 = env.load_script("multiple2.sh")?;

            // Create a dummy web request
            let req = Request::Web(dummy_web_request());

            // Validate the request against the scripts
            assert!(single.validate(&req).0 == RequestType::ExecuteHook);
            assert!(failing.validate(&req).0 == RequestType::Invalid);
            assert!(multiple1.validate(&req).0 == RequestType::ExecuteHook);
            assert!(multiple2.validate(&req).0 == RequestType::ExecuteHook);

            Ok(())
        });
    }


    #[test]
    fn test_script_ids_are_unique() {
        test_wrapper(|env| {
            // Create two different scripts
            env.create_script(
                "script1.sh",
                &[r#"#!/bin/bash"#, r#"echo "Script 1""#],
            )?;
            env.create_script(
                "script2.sh",
                &[r#"#!/bin/bash"#, r#"echo "Script 2""#],
            )?;

            // Load the scripts three time
            let id1 = env.load_script("script1.sh")?.id();
            let id2 = env.load_script("script1.sh")?.id();
            let id3 = env.load_script("script2.sh")?.id();

            // Check all the IDs are different
            assert_ne!(id1, id2);
            assert_ne!(id1, id3);
            assert_ne!(id2, id1);
            assert_ne!(id2, id3);
            assert_ne!(id3, id1);
            assert_ne!(id3, id2);

            Ok(())
        });
    }
}