summaryrefslogtreecommitdiffstats
path: root/crates/core/tedge/src/cli/connect/common_mosquitto_config.rs
blob: 5de4f5cc47a8b32695faa4c3f9711af3a78f83b9 (plain)
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
const COMMON_MOSQUITTO_CONFIG_FILENAME: &str = "tedge-mosquitto.conf";

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ListenerConfig {
    pub port: Option<u16>,
    pub bind_address: Option<String>,
    pub bind_interface: Option<String>,
    pub allow_anonymous: bool,
    pub capath: Option<String>,
    pub certfile: Option<String>,
    pub keyfile: Option<String>,
    pub require_certificate: bool,
}

impl Default for ListenerConfig {
    fn default() -> Self {
        Self {
            port: None,
            bind_address: None,
            bind_interface: None,
            allow_anonymous: false,
            capath: None,
            certfile: None,
            keyfile: None,
            require_certificate: true,
        }
    }
}

impl ListenerConfig {
    fn maybe_writeln<W: std::io::Write + ?Sized, D: std::fmt::Display>(
        &self,
        writer: &mut W,
        key: &str,
        value: Option<D>,
    ) -> std::io::Result<()> {
        value
            .map(|v| self.writeln(writer, key, v))
            .unwrap_or(Ok(()))
    }
    fn writeln<W: std::io::Write + ?Sized, D: std::fmt::Display>(
        &self,
        writer: &mut W,
        key: &str,
        value: D,
    ) -> std::io::Result<()> {
        writeln!(writer, "{} {}", key, value)
    }
    pub fn write(&self, writer: &mut dyn std::io::Write) -> std::io::Result<()> {
        let bind_address = self.bind_address.clone().unwrap_or_else(|| "".to_string());
        let maybe_listener = self
            .port
            .as_ref()
            .map(|port| format!("{} {}", port, bind_address));
        match maybe_listener {
            None => Ok(()),
            Some(listener) => {
                self.writeln(writer, "listener", listener)?;
                self.writeln(writer, "allow_anonymous", self.allow_anonymous)?;
                self.writeln(writer, "require_certificate", self.require_certificate)?;
                self.maybe_writeln(writer, "bind_interface", self.bind_interface.as_ref())?;
                self.maybe_writeln(writer, "capath", self.capath.as_ref())?;
                self.maybe_writeln(writer, "certfile", self.certfile.as_ref())?;
                self.maybe_writeln(writer, "keyfile", self.keyfile.as_ref())
            }
        }
    }
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct CommonMosquittoConfig {
    pub config_file: String,
    pub internal_listener: ListenerConfig,
    pub external_listener: ListenerConfig,
    pub log_types: Vec<String>,
    pub message_size_limit: u32,
}

impl Default for CommonMosquittoConfig {
    fn default() -> Self {
        CommonMosquittoConfig {
            config_file: COMMON_MOSQUITTO_CONFIG_FILENAME.into(),
            internal_listener: ListenerConfig {
                port: Some(1883),
                bind_address: Some("localhost".into()),
                allow_anonymous: true,
                require_certificate: false,
                ..Default::default()
            },
            external_listener: Default::default(),
            log_types: vec![
                "error".into(),
                "warning".into(),
                "notice".into(),
                "information".into(),
                "subscribe".into(),
                "unsubscribe".into(),
            ],
            message_size_limit: 268435455,
        }
    }
}

impl CommonMosquittoConfig {
    pub fn serialize<W: std::io::Write>(&self, writer: &mut W) -> std::io::Result<()> {
        writeln!(writer, "per_listener_settings true")?;

        writeln!(writer, "connection_messages true")?;

        for log_type in &self.log_types {
            writeln!(writer, "log_type {}", log_type)?;
        }
        writeln!(writer, "message_size_limit {}", self.message_size_limit)?;

        self.internal_listener.write(writer)?;
        self.external_listener.write(writer)?;

        Ok(())
    }

    pub fn with_internal_opts(self, port: u16, bind_address: String) -> Self {
        let internal_listener = ListenerConfig {
            port: Some(port),
            bind_address: Some(bind_address),
            ..self.internal_listener
        };
        Self {
            internal_listener,
            ..self
        }
    }

    pub fn with_external_opts(
        self,
        port: Option<u16>,
        bind_address: Option<String>,
        bind_interface: Option<String>,
        capath: Option<String>,
        certfile: Option<String>,
        keyfile: Option<String>,
    ) -> Self {
        let mut external_listener = ListenerConfig {
            port,
            bind_address,
            bind_interface,
            capath,
            certfile,
            keyfile,
            allow_anonymous: true,
            require_certificate: false,
        };

        if external_listener.capath.is_some() {
            external_listener.allow_anonymous = false;
            external_listener.require_certificate = true;
        }

        Self {
            external_listener,
            ..self
        }
    }
}

#[test]
fn test_serialize() -> anyhow::Result<()> {
    let common_mosquitto_config = CommonMosquittoConfig::default();

    let mut buffer = Vec::new();
    common_mosquitto_config.serialize(&mut buffer)?;

    let contents = String::from_utf8(buffer).unwrap();
    let config_set: std::collections::HashSet<&str> = contents
        .lines()
        .filter(|str| !str.is_empty() && !str.starts_with('#'))
        .collect();
    let mut expected = std::collections::HashSet::new();

    expected.insert("listener 1883 localhost");
    expected.insert("allow_anonymous true");
    expected.insert("connection_messages true");

    expected.insert("log_type error");
    expected.insert("log_type warning");
    expected.insert("log_type notice");
    expected.insert("log_type information");
    expected.insert("log_type subscribe");
    expected.insert("log_type unsubscribe");
    expected.insert("message_size_limit 268435455");
    expected.insert("per_listener_settings true");
    expected.insert("require_certificate false");

    assert_eq!(config_set, expected);

    Ok(())
}

#[test]
fn test_serialize_with_opts() -> anyhow::Result<()> {
    let common_mosquitto_config = CommonMosquittoConfig::default();
    let mosquitto_config_with_opts = common_mosquitto_config
        .with_internal_opts(1234, "1.2.3.4".into())
        .with_external_opts(
            Some(2345),
            Some("0.0.0.0".to_string()),
            Some("wlan0".into()),
            Some("/etc/ssl/certs".into()),
            Some("cert.pem".into()),
            Some("key.pem".into()),
        );

    assert!(mosquitto_config_with_opts
        .internal_listener
        .port
        .eq(&Some(1234)));

    let mut buffer = Vec::new();
    mosquitto_config_with_opts.serialize(&mut buffer)?;

    let contents = String::from_utf8(buffer).unwrap();
    let expected = concat!(
        "per_listener_settings true\n",
        "connection_messages true\n",
        "log_type error\n",
        "log_type warning\n",
        "log_type notice\n",
        "log_type information\n",
        "log_type subscribe\n",
        "log_type unsubscribe\n",
        "message_size_limit 268435455\n",
        "listener 1234 1.2.3.4\n",
        "allow_anonymous true\n",
        "require_certificate false\n",
        "listener 2345 0.0.0.0\n",
        "allow_anonymous false\n",
        "require_certificate true\n",
        "bind_interface wlan0\n",
        "capath /etc/ssl/certs\n",
        "certfile cert.pem\n"