-
Notifications
You must be signed in to change notification settings - Fork 472
Expand file tree
/
Copy pathmod.rs
More file actions
380 lines (303 loc) · 12.4 KB
/
mod.rs
File metadata and controls
380 lines (303 loc) · 12.4 KB
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
// Copyright (c) Microsoft. All rights reserved.
mod init;
pub mod config;
pub mod network;
pub mod runtime;
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
pub struct Settings {
#[serde(flatten)]
pub base: crate::base::Settings<config::DockerConfig>,
pub moby_runtime: runtime::MobyRuntime,
}
pub const CONFIG_FILE_DEFAULT: &str = "/etc/aziot/edged/config.toml";
impl Settings {
/// Load the aziot-edged configuration.
///
/// Configuration is made up of /etc/aziot/edged/config.toml (overridden by the `AZIOT_EDGED_CONFIG` env var)
/// and any files in the /etc/aziot/edged/config.d directory (overridden by the `AZIOT_EDGED_CONFIG_DIR` env var).
pub fn new() -> Result<Self, Box<dyn std::error::Error>> {
let config_path = std::env::var("AZIOT_EDGED_CONFIG")
.unwrap_or_else(|_| "/etc/aziot/edged/config.toml".to_string());
let config_path = std::path::Path::new(&config_path);
let config_directory_path = std::env::var("AZIOT_EDGED_CONFIG_DIR")
.unwrap_or_else(|_| "/etc/aziot/edged/config.d".to_string());
let config_directory_path = std::path::Path::new(&config_directory_path);
let mut settings: Settings =
config_common::read_config(config_path, Some(config_directory_path))?;
init::agent_spec(&mut settings)?;
Ok(settings)
}
pub fn moby_runtime(&self) -> &runtime::MobyRuntime {
&self.moby_runtime
}
#[must_use]
pub fn agent_upstream_resolve(mut self, parent_hostname: &str) -> Self {
crate::RuntimeSettings::agent_mut(&mut self)
.config_mut()
.parent_hostname_resolve(parent_hostname);
self
}
}
impl crate::RuntimeSettings for Settings {
type ModuleConfig = config::DockerConfig;
fn hostname(&self) -> &str {
self.base.hostname()
}
fn edge_ca_cert(&self) -> Option<&str> {
self.base.edge_ca_cert()
}
fn edge_ca_key(&self) -> Option<&str> {
self.base.edge_ca_key()
}
fn edge_ca_auto_renew(&self) -> &Option<cert_renewal::AutoRenewConfig> {
self.base.edge_ca_auto_renew()
}
fn edge_ca_subject(&self) -> &Option<aziot_certd_config::CertSubject> {
self.base.edge_ca_subject()
}
fn trust_bundle_cert(&self) -> Option<&str> {
self.base.trust_bundle_cert()
}
fn manifest_trust_bundle_cert(&self) -> Option<&str> {
self.base.manifest_trust_bundle_cert()
}
fn auto_reprovisioning_mode(&self) -> crate::aziot::AutoReprovisioningMode {
self.base.auto_reprovisioning_mode()
}
fn iotedge_max_requests(&self) -> &crate::IotedgeMaxRequests {
self.base.iotedge_max_requests()
}
fn homedir(&self) -> &std::path::Path {
self.base.homedir()
}
fn allow_elevated_docker_permissions(&self) -> bool {
self.base.allow_elevated_docker_permissions()
}
fn agent(&self) -> &crate::module::Settings<Self::ModuleConfig> {
self.base.agent()
}
fn agent_mut(&mut self) -> &mut crate::module::Settings<Self::ModuleConfig> {
self.base.agent_mut()
}
fn connect(&self) -> &crate::uri::Connect {
self.base.connect()
}
fn listen(&self) -> &crate::uri::Listen {
self.base.listen()
}
fn watchdog(&self) -> &crate::watchdog::Settings {
self.base.watchdog()
}
fn endpoints(&self) -> &crate::aziot::Endpoints {
self.base.endpoints()
}
fn additional_info(&self) -> &std::collections::BTreeMap<String, String> {
self.base.additional_info()
}
fn image_garbage_collection(&self) -> &crate::base::image_gc_settings::Settings {
self.base.image_garbage_collection()
}
}
#[cfg(test)]
mod tests {
use std::time::Duration;
use super::Settings;
use crate::docker::network;
use crate::RuntimeSettings;
use crate::DEFAULT_NETWORKID;
// Prevents multiple tests from modifying environment variables concurrently.
lazy_static::lazy_static! {
static ref ENV_LOCK: std::sync::Mutex<()> = std::sync::Mutex::default();
}
static CONFIG_DIR: &str = "test-files/config.d";
// Test files.
static BAD_SETTINGS: &str = "test-files/bad_sample_settings.toml";
static BAD_SETTINGS_CONTENT_TRUST: &str = "test-files/bad_settings_content_trust.toml";
static GOOD_SETTINGS: &str = "test-files/sample_settings.toml";
static GOOD_SETTINGS_CASE_SENSITIVE: &str = "test-files/case_sensitive.toml";
static GOOD_SETTINGS_CONTENT_TRUST: &str = "test-files/sample_settings_content_trust.toml";
static GOOD_SETTINGS_NETWORK: &str = "test-files/sample_settings.network.toml";
static GOOD_SETTINGS_IMAGE_GC: &str = "test-files/sample_settings_image_gc.toml";
#[test]
fn err_no_file() {
let _env_lock = ENV_LOCK.lock().expect("env lock poisoned");
std::env::set_var("AZIOT_EDGED_CONFIG", "garbage");
let settings = Settings::new();
assert!(settings.is_err());
}
#[test]
fn err_bad_file() {
let _env_lock = ENV_LOCK.lock().expect("env lock poisoned");
std::env::set_var("AZIOT_EDGED_CONFIG", BAD_SETTINGS);
std::env::set_var("AZIOT_EDGED_CONFIG_DIR", CONFIG_DIR);
let settings = Settings::new();
assert!(settings.is_err());
}
#[test]
fn case_sensitive() {
let _env_lock = ENV_LOCK.lock().expect("env lock poisoned");
std::env::set_var("AZIOT_EDGED_CONFIG", GOOD_SETTINGS_CASE_SENSITIVE);
std::env::set_var("AZIOT_EDGED_CONFIG_DIR", CONFIG_DIR);
let settings = Settings::new().unwrap();
let env = settings.agent().env();
assert_eq!(env.get("AbC").map(AsRef::as_ref), Some("VAluE1"));
assert_eq!(env.get("DeF").map(AsRef::as_ref), Some("VAluE2"));
let create_options = settings.agent().config().create_options();
assert_eq!(create_options.hostname(), Some("VAluE3"));
}
#[test]
fn watchdog() {
let _env_lock = ENV_LOCK.lock().expect("env lock poisoned");
std::env::set_var("AZIOT_EDGED_CONFIG", GOOD_SETTINGS);
std::env::set_var("AZIOT_EDGED_CONFIG_DIR", CONFIG_DIR);
let settings = Settings::new().unwrap();
let watchdog_settings = settings.watchdog();
assert_eq!(watchdog_settings.max_retries(), 3);
}
#[test]
fn min_tls_version_default() {
let _env_lock = ENV_LOCK.lock().expect("env lock poisoned");
std::env::set_var("AZIOT_EDGED_CONFIG", GOOD_SETTINGS);
std::env::set_var("AZIOT_EDGED_CONFIG_DIR", CONFIG_DIR);
let settings = Settings::new().unwrap();
assert_eq!(
settings.listen().min_tls_version(),
crate::uri::MinTlsVersion::Tls10
);
}
#[test]
fn network_settings() {
let _env_lock = ENV_LOCK.lock().expect("env lock poisoned");
std::env::set_var("AZIOT_EDGED_CONFIG", GOOD_SETTINGS_NETWORK);
std::env::set_var("AZIOT_EDGED_CONFIG_DIR", CONFIG_DIR);
let settings = Settings::new().unwrap();
let moby_runtime = settings.moby_runtime();
assert_eq!(
moby_runtime.uri().to_string(),
"http://localhost:2375/".to_string()
);
let network = moby_runtime.network();
assert_eq!(network.name(), DEFAULT_NETWORKID);
match network {
network::MobyNetwork::Network(moby_network) => {
assert!(moby_network.ipv6().unwrap());
let ipam_spec = moby_network.ipam().expect("Expected IPAM specification.");
let ipam_config = ipam_spec.config().expect("Expected IPAM configuration.");
let ipam_1 = network::IpamConfig::default()
.with_gateway("172.18.0.1".to_string())
.with_ip_range("172.18.0.0/16".to_string())
.with_subnet("172.18.0.0/16".to_string());
let ipam_2 = network::IpamConfig::default()
.with_gateway("2001:4898:e0:3b1:1::1".to_string())
.with_ip_range("2001:4898:e0:3b1:1::/80".to_string())
.with_subnet("2001:4898:e0:3b1:1::/80".to_string());
let expected_ipam_config: Vec<network::IpamConfig> = vec![ipam_1, ipam_2];
for ipam_config in ipam_config.iter() {
assert!(expected_ipam_config.contains(ipam_config));
}
}
network::MobyNetwork::Name(_name) => panic!("Unexpected network configuration."),
};
}
#[test]
fn networking_create_options() {
let _env_lock = ENV_LOCK.lock().expect("env lock poisoned");
std::env::set_var("AZIOT_EDGED_CONFIG", GOOD_SETTINGS);
std::env::set_var("AZIOT_EDGED_CONFIG_DIR", CONFIG_DIR);
let settings = Settings::new().unwrap();
let create_options = settings.agent().config().create_options();
assert!(create_options
.networking_config()
.unwrap()
.endpoints_config()
.unwrap()
.contains_key(DEFAULT_NETWORKID));
}
#[test]
fn agent_labels() {
let _env_lock = ENV_LOCK.lock().expect("env lock poisoned");
std::env::set_var("AZIOT_EDGED_CONFIG", GOOD_SETTINGS);
std::env::set_var("AZIOT_EDGED_CONFIG_DIR", CONFIG_DIR);
let settings = Settings::new().unwrap();
let create_options = settings.agent().config().create_options();
let labels = create_options.labels().unwrap();
assert_eq!(
labels.get("net.azure-devices.edge.create-options"),
Some(&"{}".to_string())
);
assert_eq!(
labels.get("net.azure-devices.edge.env"),
Some(&"{}".to_string())
);
}
#[test]
fn image_garbage_collection() {
let _env_lock = ENV_LOCK.lock().expect("env lock poisoned");
std::env::set_var("AZIOT_EDGED_CONFIG", GOOD_SETTINGS_IMAGE_GC);
std::env::set_var("AZIOT_EDGED_CONFIG_DIR", CONFIG_DIR);
let settings = Settings::new().unwrap();
let image_prune_settings = settings.image_garbage_collection();
assert!(image_prune_settings.is_enabled());
assert_eq!(
image_prune_settings.image_age_cleanup_threshold(),
Duration::from_secs(1440 * 60 * 3 * 7)
);
assert_eq!(
image_prune_settings.cleanup_recurrence(),
Duration::from_secs(1440 * 60 * 3)
);
assert_eq!(image_prune_settings.cleanup_time(), 600);
}
#[test]
fn image_garbage_collection_defaults() {
let _env_lock = ENV_LOCK.lock().expect("env lock poisoned");
std::env::set_var("AZIOT_EDGED_CONFIG", GOOD_SETTINGS);
std::env::set_var("AZIOT_EDGED_CONFIG_DIR", CONFIG_DIR);
let image_gc_settings = Settings::new().unwrap().image_garbage_collection().clone();
assert!(image_gc_settings.is_enabled());
assert_eq!(
image_gc_settings.image_age_cleanup_threshold(),
Duration::from_secs(60 * 60 * 24 * 7)
);
assert_eq!(
image_gc_settings.cleanup_recurrence(),
Duration::from_secs(60 * 60 * 24)
);
assert_eq!(image_gc_settings.cleanup_time(), 0);
}
#[test]
fn content_trust_env() {
let _env_lock = ENV_LOCK.lock().expect("env lock poisoned");
std::env::set_var("AZIOT_EDGED_CONFIG", GOOD_SETTINGS_CONTENT_TRUST);
std::env::set_var("AZIOT_EDGED_CONFIG_DIR", CONFIG_DIR);
let settings = Settings::new().unwrap();
if let Some(content_trust_map) = settings
.moby_runtime()
.content_trust()
.and_then(crate::docker::runtime::ContentTrust::ca_certs)
{
assert_eq!(
content_trust_map
.get("contoso1.azurcr.io")
.map(AsRef::as_ref),
Some("content-trust-contoso1.azurecr.io")
);
assert_eq!(
content_trust_map
.get("contoso2.azurcr.io")
.map(AsRef::as_ref),
Some("content-trust-contoso2.azurecr.io")
);
} else {
panic!();
}
}
#[test]
fn content_trust_env_err() {
let _env_lock = ENV_LOCK.lock().expect("env lock poisoned");
std::env::set_var("AZIOT_EDGED_CONFIG", BAD_SETTINGS_CONTENT_TRUST);
std::env::set_var("AZIOT_EDGED_CONFIG_DIR", CONFIG_DIR);
let settings = Settings::new();
assert!(settings.is_err());
}
}