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
// Logging support
#[macro_use]
extern crate log;

// Config parser (YAML)
extern crate yaml_rust;

// Webhook listener
extern crate hyper;
extern crate rifling;

// Run shell commands
extern crate run_script;

use std::collections::HashMap;
use std::error::Error;
use std::fs::File;
use std::io::prelude::*;
use std::io::BufReader;
use std::net::SocketAddr;
use std::thread;

use hyper::rt::{run, Future};
use hyper::Server;
use run_script::ScriptOptions;
use yaml_rust::{Yaml, YamlLoader};

use rifling::hook::HookFunc;
use rifling::{Constructor, Delivery, DeliveryType, Hook};

/// Get values inside Option<T>
macro_rules! get_value {
    ($source:expr) => {
        match $source {
            Some(string) => string.as_str(),
            None => "unknown",
        }
    };
}

// Some constant
/// Name of the settings section in configuration file
const SETTINGS: &str = "settings";
/// Name of the field that let trigger to warn you about @kotomei2 or not
const KOTOMEI: &str = "kotomei";
/// Name of the events section in configuration file
const EVENTS: &str = "events";
/// Name of the common part inside events section in configuration file
const EVENTS_COMMON: &str = "common";
/// Name of the `else` part inside events section in configuration file
const EVENTS_ELSE: &str = "else";
/// Name of the `all` part inside events section in configuration file
const EVENTS_ALL: &str = "all";

#[derive(Clone)]
/// Handler of the deliveries
pub struct Handler {
    config: Yaml,
}

/// Handler of the deliveries
impl Handler {
    /// Create a new instance from given configuration
    fn new(config: Yaml) -> Handler {
        Handler { config }
    }

    /// Prepare command from information of the delivery
    fn process_commands(&self, event: &str, delivery: &Delivery) -> Option<String> {
        let common_command = self.config[EVENTS][EVENTS_COMMON].as_str().unwrap_or("");
        if let Some(command) = self.config[EVENTS][event].as_str() {
            let mut exec = String::from(command);
            exec = format!("{}\n{}", &common_command, &exec);
            // Replace placeholders in commands
            exec = exec.replace(
                "{source}",
                format!("{:?}", &delivery.delivery_type).as_str(),
            );
            exec = exec.replace("{id}", get_value!(&delivery.id));
            exec = exec.replace("{event}", get_value!(&delivery.event));
            exec = exec.replace("{signature}", get_value!(&delivery.signature));
            exec = exec.replace("{payload}", get_value!(&delivery.unparsed_payload));
            exec = exec.replace("{request_body}", get_value!(&delivery.request_body));
            Some(exec)
        } else {
            None
        }
    }
}

impl HookFunc for Handler {
    /// Handle the delivery
    fn run(&self, delivery: &Delivery) {
        let event = get_value!(&delivery.event);
        info!(
            "Received \"{}\" event from {:?}",
            &event, &delivery.delivery_type
        );
        match &delivery.delivery_type {
            DeliveryType::GitHub => {
                let id = get_value!(&delivery.id);
                info!("Delivery ID: \"{}\"", id);
            }
            _ => {
                info!(
                    "Delivery ID not available for requests from {:?}",
                    &delivery.delivery_type
                );
            }
        }
        // Prepare the commands
        let mut commands_all: HashMap<String, Option<String>> = HashMap::new();

        // Prepare commands in `all` section
        commands_all.insert(
            EVENTS_ALL.into(),
            self.process_commands(EVENTS_ALL, &delivery),
        );

        // Prepare commands matching the event
        if let Some(command) = self.process_commands(event, &delivery) {
            commands_all.insert(event.into(), Some(command));
        } else {
            commands_all.insert(
                EVENTS_ELSE.into(),
                self.process_commands(EVENTS_ELSE, &delivery),
            );
        }

        // Execute the commands
        for (section_name, command) in commands_all {
            if let Some(exec) = command {
                info!("Running commands in \"{}\" section", &section_name);
                debug!("Parsed command: {}", &exec);
                let mut options = ScriptOptions::new();
                options.capture_output = self.config[SETTINGS]["capture_output"]
                    .as_bool()
                    .unwrap_or(false);
                options.exit_on_error = self.config[SETTINGS]["exit_on_error"]
                    .as_bool()
                    .unwrap_or(false);
                options.print_commands = self.config[SETTINGS]["print_commands"]
                    .as_bool()
                    .unwrap_or(false);
                debug!("Executor option: {:#?}", &options);
                let args = vec![];
                thread::spawn(move || {
                    let (code, output, error) = run_script::run(&exec.as_str(), &args, &options)
                        .expect("Failed to execute command");
                    info!("Commands in \"{}\" section exited with code {}", &section_name, code);
                    if options.capture_output {
                        debug!("stdout:\n{}", output);
                        debug!("stderr:\n{}", error);
                    } else {
                        debug!("Output not captured.");
                    }
                });
            }
        }
        info!("Returning 200");
    }
}

/// Start the server from given config file path
pub fn start(config_filename: &str) -> Result<(), Box<dyn Error>> {
    debug!("Setting up...");

    // Read config (from `trigger.yaml`)
    let mut config_content = String::new();
    let config_file = File::open(config_filename)?;
    let mut buf_reader = BufReader::new(config_file);
    buf_reader.read_to_string(&mut config_content)?;
    debug!(
        "Got config:\n\"\"\"\n{}\n\"\"\"\nfrom file {}",
        config_content, config_filename
    );

    let config = &YamlLoader::load_from_str(config_content.as_str())?[0];
    debug!("Config parsed: {:?}", config);

    // Prepare secret
    let secret = if let Some(secret) = config[SETTINGS]["secret"].as_str() {
        Some(String::from(secret))
    } else {
        None
    };

    if config[SETTINGS][KOTOMEI].as_bool().unwrap_or(true) {
        warn!("If you ever see @kotomei2, tell him to study for the exam!");
    }

    // Setup handler
    let handler = Handler::new(config.clone());
    let mut cons = Constructor::new();
    let hook = Hook::new("*", secret, handler);
    cons.register(hook);

    // Setup server
    let addr: SocketAddr = config[SETTINGS]["host"]
        .as_str()
        .expect("Unable to read host address")
        .parse()
        .expect("Unable to parse host address");
    let ip_type = if addr.is_ipv4() { "IPv4" } else { "IPv6" };
    info!(
        "Listening on {} address {}:{}",
        ip_type,
        &addr.ip(),
        &addr.port()
    );
    let server = Server::bind(&addr)
        .serve(cons)
        .map_err(|e| error!("Error: {:?}", e));
    info!("Started");

    // Link start!
    run(server);
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use rifling::handler::ContentType;
    use rifling::handler::DeliveryType;

    #[test]
    fn command_generation() {
        let test_config = r#"
settings:
  host: 0.0.0.0:4567
  secret: "secret"

events:
  common: common
  all: all
  push: push
  else: else
"#;
        let config = &YamlLoader::load_from_str(test_config).unwrap()[0];
        let handler = Handler::new(config.clone());
        let delivery = Delivery::new(
            DeliveryType::GitHub,
            Some(String::from("unknown")),
            Some(String::from("push")),
            None,
            ContentType::JSON,
            Some(String::from(r#"{ zen: "test" }"#)),
        );
        let result_all = handler.process_commands(EVENTS_ALL, &delivery);
        assert_eq!(
            "\
common
all\
            ",
            result_all.unwrap().as_str()
        );
        let result_push = handler.process_commands("push", &delivery);
        assert_eq!(
            "\
common
push\
            ",
            result_push.unwrap().as_str()
        );
        let result_else = handler.process_commands(EVENTS_ELSE, &delivery);
        assert_eq!(
            "\
common
else\
            ",
            result_else.unwrap().as_str()
        );
    }
}