asterust_junos/system/services/
outbound_https.rs1use crate::{arg_of, named_children, secret_value, Secret};
9use asterust::config::ConfigLine;
10
11#[derive(Clone, Debug, PartialEq, Eq)]
13#[non_exhaustive]
14pub struct OutboundHttpsAddress {
15 pub address: String,
16 pub port: Option<String>,
17 pub trusted_cert: Option<String>,
18}
19
20#[derive(Clone, Debug, PartialEq, Eq)]
22#[non_exhaustive]
23pub struct OutboundHttpsClient {
24 pub id: String,
25 pub addresses: Vec<OutboundHttpsAddress>,
26 pub device_id: Option<String>,
27 pub reconnect_strategy: Option<String>,
29 pub secret: Option<Secret>,
31 pub waittime: Option<String>,
33}
34
35pub(super) fn parse_outbound_https(outbound: &ConfigLine) -> Vec<OutboundHttpsClient> {
36 named_children(outbound, "client")
37 .map(|(id, client)| OutboundHttpsClient {
38 id: id.to_string(),
39 addresses: named_children(client, "address")
40 .map(|(address, block)| OutboundHttpsAddress {
41 address: address.to_string(),
42 port: arg_of(block, "port").map(str::to_string),
43 trusted_cert: arg_of(block, "trusted-cert").map(str::to_string),
44 })
45 .collect(),
46 device_id: arg_of(client, "device-id").map(str::to_string),
47 reconnect_strategy: arg_of(client, "reconnect-strategy").map(str::to_string),
48 secret: secret_value(arg_of(client, "secret")),
49 waittime: arg_of(client, "waittime").map(str::to_string),
50 })
51 .collect()
52}
53
54#[cfg(test)]
55mod tests {
56 use crate::system::system;
57 use asterust::config::juniper;
58
59 #[test]
61 fn extracts_outbound_https_clients_without_the_secret() {
62 let clients = system(&juniper::parse_braces(
63 r#"
64system {
65 services {
66 outbound-https {
67 client mgr1 {
68 address 192.0.2.50 {
69 port 443;
70 trusted-cert mgr-ca;
71 }
72 device-id pe1;
73 reconnect-strategy sticky;
74 secret "$9$secret"; ## SECRET-DATA
75 waittime 15;
76 }
77 }
78 }
79}
80"#,
81 ))
82 .unwrap()
83 .services
84 .unwrap()
85 .outbound_https;
86 assert_eq!(clients.len(), 1);
87 let client = &clients[0];
88 assert_eq!(client.id, "mgr1");
89 assert_eq!(client.addresses[0].address, "192.0.2.50");
90 assert_eq!(client.addresses[0].port.as_deref(), Some("443"));
91 assert_eq!(client.addresses[0].trusted_cert.as_deref(), Some("mgr-ca"));
92 assert_eq!(client.device_id.as_deref(), Some("pe1"));
93 assert_eq!(client.reconnect_strategy.as_deref(), Some("sticky"));
94 assert_eq!(client.waittime.as_deref(), Some("15"));
95 assert_eq!(
96 client.secret.as_ref().map(crate::Secret::expose),
97 crate::secret::expected("$9$secret")
98 );
99 }
100
101 #[test]
102 fn no_outbound_https_is_empty() {
103 let services = system(&juniper::parse_braces(
104 "system {\n services {\n telnet;\n }\n}\n",
105 ))
106 .unwrap()
107 .services
108 .unwrap();
109 assert!(services.outbound_https.is_empty());
110 }
111}