Skip to main content

asterust_junos/system/services/
outbound_ssh.rs

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