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