Skip to main content

asterust_junos/system/services/
outbound_ssh.rs

1//! `system { services { outbound-ssh { client <id> { ... } } } }`.
2
3use crate::{active_children, arg_of, args_of, list_values, named_children};
4use asterust::config::ConfigLine;
5
6/// An `address` of an outbound-ssh client.
7#[derive(Clone, Debug, PartialEq, Eq)]
8#[non_exhaustive]
9pub struct OutboundSshServer {
10    pub address: String,
11    pub port: Option<String>,
12    pub retry: Option<String>,
13    pub timeout: Option<String>,
14}
15
16/// An `outbound-ssh client <id>`. The `secret` is not extracted.
17#[derive(Clone, Debug, PartialEq, Eq)]
18#[non_exhaustive]
19pub struct OutboundSshClient {
20    pub id: String,
21    pub device_id: Option<String>,
22    pub servers: Vec<OutboundSshServer>,
23    /// e.g. `"netconf"`.
24    pub services: Vec<String>,
25    /// `in-order` or `sticky`.
26    pub reconnect_strategy: Option<String>,
27    pub routing_instance: Option<String>,
28}
29
30fn parse_outbound_client(id: &str, client: &ConfigLine) -> OutboundSshClient {
31    OutboundSshClient {
32        id: id.to_string(),
33        device_id: arg_of(client, "device-id").map(str::to_string),
34        servers: active_children(client)
35            // Servers are the children written as a bare address; the single-word blocks are other options.
36            .filter(|c| !c.text.contains(' ') && !matches!(c.text.as_str(), "keep-alive" | "proxy-server"))
37            .map(|server| OutboundSshServer {
38                address: server.text.clone(),
39                port: arg_of(server, "port").map(str::to_string),
40                retry: arg_of(server, "retry").map(str::to_string),
41                timeout: arg_of(server, "timeout").map(str::to_string),
42            })
43            .collect(),
44        services: args_of(client, "services").into_iter().flat_map(list_values).collect(),
45        reconnect_strategy: arg_of(client, "reconnect-strategy").map(str::to_string),
46        routing_instance: arg_of(client, "routing-instance").map(str::to_string),
47    }
48}
49
50pub(super) fn parse_outbound_ssh(outbound: &ConfigLine) -> Vec<OutboundSshClient> {
51    named_children(outbound, "client")
52        .map(|(id, client)| parse_outbound_client(id, client))
53        .collect()
54}
55
56#[cfg(test)]
57mod tests {
58    use crate::system::system;
59    use asterust::config::juniper;
60
61    #[test]
62    fn extracts_outbound_ssh_clients() {
63        let services = system(&juniper::parse_braces(
64            r#"
65system {
66    services {
67        outbound-ssh {
68            client mgmt {
69                device-id pe1;
70                secret "$9$secret"; ## SECRET-DATA
71                reconnect-strategy sticky;
72                routing-instance mgmt_junos;
73                services netconf;
74                192.0.2.50 {
75                    port 7804;
76                    retry 5;
77                    timeout 20;
78                }
79            }
80            client backup {
81                192.0.2.60;
82                keep-alive {
83                    retry 3;
84                    timeout 15;
85                }
86            }
87        }
88    }
89}
90"#,
91        ))
92        .unwrap()
93        .services
94        .unwrap();
95        assert_eq!(services.outbound_ssh.len(), 2);
96
97        let mgmt = &services.outbound_ssh[0];
98        assert_eq!(mgmt.id, "mgmt");
99        assert_eq!(mgmt.device_id.as_deref(), Some("pe1"));
100        assert_eq!(mgmt.reconnect_strategy.as_deref(), Some("sticky"));
101        assert_eq!(mgmt.routing_instance.as_deref(), Some("mgmt_junos"));
102        assert_eq!(mgmt.services, ["netconf"]);
103        assert_eq!(mgmt.servers.len(), 1);
104        assert_eq!(mgmt.servers[0].address, "192.0.2.50");
105        assert_eq!(mgmt.servers[0].port.as_deref(), Some("7804"));
106        assert_eq!(mgmt.servers[0].retry.as_deref(), Some("5"));
107        assert_eq!(mgmt.servers[0].timeout.as_deref(), Some("20"));
108
109        let backup = &services.outbound_ssh[1];
110        assert_eq!(backup.id, "backup");
111        // `keep-alive` is an option block, not a server.
112        assert_eq!(backup.servers.len(), 1);
113        assert_eq!(backup.servers[0].address, "192.0.2.60");
114        assert_eq!(backup.servers[0].port, None);
115    }
116
117    #[test]
118    fn no_outbound_ssh_is_empty() {
119        let services = system(&juniper::parse_braces(
120            "system {\n    services {\n        ssh;\n    }\n}\n",
121        ))
122        .unwrap()
123        .services
124        .unwrap();
125        assert!(services.outbound_ssh.is_empty());
126    }
127}