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
use openssl::{rsa::Rsa, pkey::Public};
use serde_json::Value;
use crate::strings::ERROR_SERDE_PARSE;

struct StaticJsonStrings {
    pub name: &'static str,
    pub update_path_prefix: &'static str,
    pub scope: &'static str,
    pub replaces_default_rulesets: &'static str,
    pub pem: &'static str,
}

const JSON_STRINGS: StaticJsonStrings = StaticJsonStrings {
    name: "name",
    update_path_prefix: "update_path_prefix",
    scope: "scope",
    replaces_default_rulesets: "replaces_default_rulesets",
    pem: "pem",
};


/// An UpdateChannel defines where to find ruleset updates, the key to verify them, the scope they
/// are applied to (which should be a regular expression), and whether they replace the default
/// rulesets included with the application.
#[derive(Debug)]
pub struct UpdateChannel {
    name: String,
    key: Rsa<Public>,
    update_path_prefix: String,
    scope: Option<String>,
    replaces_default_rulesets: bool,
}

impl From<&String> for UpdateChannel {
    /// Returns an update channel given a JSON string
    ///
    /// # Arguments
    ///
    /// * `json_string` - A json string specifying the update channel.  See
    /// [`tests/update_channels.json`](https://github.com/EFForg/https-everywhere-lib-core/blob/master/tests/update_channels.json) for the correct format
    ///
    /// # Panics
    ///
    /// Panics if a name, update path prefix, or pem is not specified, if the pem file does not
    /// parse correctly into an RSA key, or it is not an object
    fn from(json_string: &String) -> UpdateChannel {
        let update_channel: Value = serde_json::from_str(&json_string).expect(ERROR_SERDE_PARSE);
        UpdateChannel::from(&update_channel)
    }
}

impl From<&Value> for UpdateChannel {
    /// Returns an update channel given a serde_json::Value
    ///
    /// See the implementation of `From<&String>` for more detail
    fn from(json_value: &Value) -> UpdateChannel {
        if let Value::Object(update_channel) = json_value {
            let name = match update_channel.get(JSON_STRINGS.name) {
                Some(Value::String(name)) => name.to_string(),
                _ => panic!("Name can not be blank")
            };
            let update_path_prefix = match update_channel.get(JSON_STRINGS.update_path_prefix) {
                Some(Value::String(update_path_prefix)) => update_path_prefix.to_string(),
                _ => panic!("Update path prefix can not be blank")
            };
            let scope = match update_channel.get(JSON_STRINGS.scope) {
                Some(Value::String(scope)) if scope == "" => None,
                Some(Value::String(scope)) => Some(scope.to_string()),
                _ => None
            };
            let replaces_default_rulesets = match update_channel.get(JSON_STRINGS.replaces_default_rulesets) {
                Some(Value::Bool(replaces_default_rulesets)) => replaces_default_rulesets.clone(),
                _ => false
            };
            let key = match update_channel.get(JSON_STRINGS.pem) {
                Some(Value::String(pem)) => {
                    match Rsa::public_key_from_pem(&pem.clone().into_bytes()) {
                        Ok(key) => key,
                        _ => panic!("Could not parse public key")
                    }
                },
                _ => panic!("Pem can not be blank")
            };
            UpdateChannel {
                name,
                key,
                update_path_prefix,
                scope,
                replaces_default_rulesets,
            }
        } else {
            panic!("Unexpected: update channel is not an object");
        }
    }
}


/// RuleSets consists of a tuple vec of update channels
#[derive(Debug)]
pub struct UpdateChannels(Vec<UpdateChannel>);

/// Returns update channels given a JSON string
///
/// See the implementation of `From<&String> for UpdateChannel` for more detail
///
/// # Panics
///
/// Panics if the update channels JSON is not an array
impl From<&String> for UpdateChannels {
    fn from(json_string: &String) -> UpdateChannels {
        if let Value::Array(update_channels) = serde_json::from_str(&json_string).expect(ERROR_SERDE_PARSE) {
            UpdateChannels(update_channels.into_iter().map(|uc| {
                UpdateChannel::from(&uc)
            }).collect())
        } else {
            panic!("Unexpected: update channels is not an array")
        }
    }
}

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

    fn mock_update_channels_json() -> String {
        fs::read_to_string("tests/update_channels.json").unwrap()
    }

    fn create_mock_update_channels() -> UpdateChannels {
        UpdateChannels::from(&mock_update_channels_json())
    }

    #[test]
    fn creates_update_channels_correctly() {
        let ucs = create_mock_update_channels();

        let update_channels_representation = fs::read_to_string("tests/update_channels_representation.txt").unwrap();
        assert_eq!(format!("{:?}", ucs), update_channels_representation);
    }

    #[test]
    #[should_panic]
    fn panics_if_no_name_specified() {
        let mut update_channels: Value = serde_json::from_str(&mock_update_channels_json()).expect(ERROR_SERDE_PARSE);
        update_channels.get_mut(0).unwrap().get_mut(JSON_STRINGS.name).unwrap().take();
        UpdateChannel::from(update_channels.get(0).unwrap());
    }

    #[test]
    #[should_panic]
    fn panics_if_no_update_path_prefix_specified() {
        let mut update_channels: Value = serde_json::from_str(&mock_update_channels_json()).expect(ERROR_SERDE_PARSE);
        update_channels.get_mut(0).unwrap().get_mut(JSON_STRINGS.update_path_prefix).unwrap().take();
        UpdateChannel::from(update_channels.get(0).unwrap());
    }

    #[test]
    #[should_panic]
    fn panics_if_no_pem_specified() {
        let mut update_channels: Value = serde_json::from_str(&mock_update_channels_json()).expect(ERROR_SERDE_PARSE);
        update_channels.get_mut(0).unwrap().get_mut(JSON_STRINGS.pem).unwrap().take();
        UpdateChannel::from(update_channels.get(0).unwrap());
    }

    #[test]
    #[should_panic]
    fn panics_if_pem_specified_incorrectly() {
        let mut update_channels: Value = serde_json::from_str(&mock_update_channels_json()).expect(ERROR_SERDE_PARSE);
        let pem = update_channels.get_mut(0).unwrap().get_mut(JSON_STRINGS.pem).unwrap();
        *pem = Value::String(String::from("Not a pem value"));
        UpdateChannel::from(update_channels.get(0).unwrap());
    }
}