Skip to main content

asterust_junos/system/services/
mod.rs

1//! `services`
2//! `system { services { ... } }`.
3//!
4//! # See also
5//!
6//! - [`[edit system]` hierarchy](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/topic-map/hierarchy-edit-system.html)
7
8mod dhcp_local_server;
9mod flow_tap_dtcp;
10mod local_policy_decision_function;
11mod netconf;
12mod outbound_ssh;
13mod service_deployment;
14mod ssh;
15mod static_subscribers;
16mod subscriber_management;
17mod web_management;
18mod xnm_clear_text;
19mod xnm_ssl;
20
21pub use dhcp_local_server::{DhcpLocalServer, DhcpLocalServerGroup};
22pub use flow_tap_dtcp::{FlowTapDtcp, FlowTapDtcpSsh};
23pub use local_policy_decision_function::{AaclStatisticsProfile, LocalPolicyDecisionFunction};
24pub use netconf::{Netconf, NetconfSsh};
25pub use outbound_ssh::{OutboundSshClient, OutboundSshServer};
26pub use service_deployment::{ServiceDeployment, ServiceDeploymentServer};
27pub use ssh::SshService;
28pub use static_subscribers::{StaticSubscriberGroup, StaticSubscriberProfile, StaticSubscribers};
29pub use subscriber_management::{RedundancyInterface, SubscriberManagement, SubscriberRedundancy};
30pub use web_management::{WebManagement, WebManagementHttp, WebManagementHttps};
31pub use xnm_clear_text::XnmClearText;
32pub use xnm_ssl::XnmSsl;
33
34use crate::child_block;
35use asterust::config::ConfigLine;
36use dhcp_local_server::parse_dhcp_local_server;
37use flow_tap_dtcp::parse_flow_tap_dtcp;
38use local_policy_decision_function::parse_local_policy_decision_function;
39use netconf::parse_netconf;
40use outbound_ssh::parse_outbound_ssh;
41use service_deployment::parse_service_deployment;
42use ssh::parse_ssh;
43use static_subscribers::parse_static_subscribers;
44use subscriber_management::parse_subscriber_management;
45use web_management::parse_web_management;
46use xnm_clear_text::parse_xnm_clear_text;
47use xnm_ssl::parse_xnm_ssl;
48
49/// The `system services` configuration. Services other than those listed are not extracted.
50#[derive(Clone, Debug, PartialEq, Eq)]
51#[non_exhaustive]
52pub struct Services {
53    pub ssh: Option<SshService>,
54    pub telnet: bool,
55    pub ftp: bool,
56    /// `netconf { ssh; }`.
57    pub netconf_ssh: bool,
58    /// `web-management { http; }`.
59    pub http: bool,
60    /// `web-management { https; }`.
61    pub https: bool,
62    pub outbound_ssh: Vec<OutboundSshClient>,
63    /// The details behind `http` and `https`.
64    pub web_management: Option<WebManagement>,
65    /// The details behind `netconf_ssh`.
66    pub netconf: Option<Netconf>,
67    pub xnm_clear_text: Option<XnmClearText>,
68    pub xnm_ssl: Option<XnmSsl>,
69    pub subscriber_management: Option<SubscriberManagement>,
70    pub static_subscribers: Option<StaticSubscribers>,
71    pub dhcp_local_server: Option<DhcpLocalServer>,
72    pub local_policy_decision_function: Option<LocalPolicyDecisionFunction>,
73    pub flow_tap_dtcp: Option<FlowTapDtcp>,
74    pub service_deployment: Option<ServiceDeployment>,
75}
76
77pub(super) fn parse_services(services: &ConfigLine) -> Services {
78    let web = child_block(services, "web-management");
79    let netconf = child_block(services, "netconf");
80    Services {
81        ssh: child_block(services, "ssh").map(parse_ssh),
82        telnet: child_block(services, "telnet").is_some(),
83        ftp: child_block(services, "ftp").is_some(),
84        netconf_ssh: netconf.is_some_and(|n| child_block(n, "ssh").is_some()),
85        http: web.is_some_and(|w| child_block(w, "http").is_some()),
86        https: web.is_some_and(|w| child_block(w, "https").is_some()),
87        outbound_ssh: child_block(services, "outbound-ssh")
88            .map(parse_outbound_ssh)
89            .unwrap_or_default(),
90        web_management: web.map(parse_web_management),
91        netconf: netconf.map(parse_netconf),
92        xnm_clear_text: child_block(services, "xnm-clear-text").map(parse_xnm_clear_text),
93        xnm_ssl: child_block(services, "xnm-ssl").map(parse_xnm_ssl),
94        subscriber_management: child_block(services, "subscriber-management").map(parse_subscriber_management),
95        static_subscribers: child_block(services, "static-subscribers").map(parse_static_subscribers),
96        dhcp_local_server: child_block(services, "dhcp-local-server").map(parse_dhcp_local_server),
97        local_policy_decision_function: child_block(services, "local-policy-decision-function")
98            .map(parse_local_policy_decision_function),
99        flow_tap_dtcp: child_block(services, "flow-tap-dtcp").map(parse_flow_tap_dtcp),
100        service_deployment: child_block(services, "service-deployment").map(parse_service_deployment),
101    }
102}
103
104#[cfg(test)]
105mod tests {
106    use super::*;
107    use crate::system::system;
108    use asterust::config::juniper;
109
110    const SERVICES: &str = r#"
111system {
112    services {
113        ssh {
114            root-login deny;
115            protocol-version v2;
116            port 2222;
117            connection-limit 10;
118            rate-limit 5;
119            ciphers [ aes256-ctr aes128-ctr ];
120            sftp-server;
121        }
122        netconf {
123            ssh;
124        }
125        web-management {
126            https {
127                system-generated-certificate;
128            }
129        }
130        telnet;
131    }
132}
133"#;
134
135    #[test]
136    fn extracts_ssh_service_settings() {
137        let services = system(&juniper::parse_braces(SERVICES)).unwrap().services.unwrap();
138        let ssh = services.ssh.unwrap();
139        assert_eq!(ssh.root_login.as_deref(), Some("deny"));
140        assert_eq!(ssh.protocol_versions, ["v2"]);
141        assert_eq!(ssh.port.as_deref(), Some("2222"));
142        assert_eq!(ssh.connection_limit.as_deref(), Some("10"));
143        assert_eq!(ssh.rate_limit.as_deref(), Some("5"));
144        assert_eq!(ssh.ciphers, ["aes256-ctr", "aes128-ctr"]);
145        assert!(ssh.sftp_server);
146    }
147
148    #[test]
149    fn extracts_enabled_services() {
150        let services = system(&juniper::parse_braces(SERVICES)).unwrap().services.unwrap();
151        assert!(services.telnet);
152        assert!(services.netconf_ssh);
153        assert!(services.https);
154        assert!(!services.http);
155        assert!(!services.ftp);
156    }
157
158    #[test]
159    fn bare_service_statements_enable_with_defaults() {
160        let services = system(&juniper::parse_braces(
161            "system {\n    services {\n        ssh;\n        ftp;\n        web-management {\n            http;\n        }\n    }\n}\n",
162        ))
163        .unwrap()
164        .services
165        .unwrap();
166        assert_eq!(services.ssh, Some(SshService::default()));
167        assert!(services.ftp);
168        assert!(services.http);
169        assert!(!services.telnet);
170        assert!(!services.netconf_ssh);
171    }
172
173    #[test]
174    fn disabled_service_is_not_reported() {
175        let services = system(&juniper::parse_braces(
176            "system {\n    services {\n        inactive: telnet;\n        ssh;\n    }\n}\n",
177        ))
178        .unwrap()
179        .services
180        .unwrap();
181        assert!(!services.telnet);
182    }
183
184    #[test]
185    fn extracts_netconf_and_xnm_settings() {
186        let services = system(&juniper::parse_braces(
187            r#"
188system {
189    services {
190        netconf {
191            ssh {
192                port 830;
193                connection-limit 5;
194                rate-limit 10;
195            }
196            rfc-compliant;
197        }
198        xnm-clear-text {
199            connection-limit 3;
200        }
201        xnm-ssl {
202            port 3220;
203            local-certificate XNM;
204            rate-limit 7;
205        }
206    }
207}
208"#,
209        ))
210        .unwrap()
211        .services
212        .unwrap();
213        let netconf = services.netconf.unwrap();
214        assert!(netconf.rfc_compliant);
215        let ssh = netconf.ssh.unwrap();
216        assert_eq!(ssh.port.as_deref(), Some("830"));
217        assert_eq!(ssh.connection_limit.as_deref(), Some("5"));
218        assert_eq!(ssh.rate_limit.as_deref(), Some("10"));
219        assert!(services.netconf_ssh);
220
221        let clear_text = services.xnm_clear_text.unwrap();
222        assert_eq!(clear_text.connection_limit.as_deref(), Some("3"));
223        assert_eq!(clear_text.rate_limit, None);
224
225        let ssl = services.xnm_ssl.unwrap();
226        assert_eq!(ssl.port.as_deref(), Some("3220"));
227        assert_eq!(ssl.local_certificate.as_deref(), Some("XNM"));
228        assert_eq!(ssl.rate_limit.as_deref(), Some("7"));
229    }
230
231    #[test]
232    fn no_services_is_none() {
233        assert!(system(&juniper::parse_braces("system {\n    host-name pe1;\n}\n"))
234            .unwrap()
235            .services
236            .is_none());
237    }
238
239    // Not from documentation: written from the CLI Reference syntax pages. See the README "Sources".
240    const SUBSCRIBERS: &str = r#"
241system {
242    dynamic-profile-options {
243        versioning;
244    }
245    services {
246        subscriber-management {
247            enable;
248            enforce-strict-scale-limit-license;
249            gres-route-flush-delay 60;
250            cgnat {
251                enable;
252            }
253            redundancy {
254                interface ae0.100 {
255                    local-inet-address 192.0.2.1;
256                    local-inet6-address 2001:db8::1;
257                    shared-key "$9$secret"; ## SECRET-DATA
258                    virtual-inet-address 192.0.2.254;
259                    virtual-inet6-address 2001:db8::fe;
260                }
261                no-advertise-routes-on-backup;
262                protocol {
263                    vrrp;
264                }
265            }
266        }
267        static-subscribers {
268            access-profile AP;
269            service-profile SP;
270            authentication {
271                password "$9$secret"; ## SECRET-DATA
272            }
273            group STATIC1 {
274                access-profile AP1;
275                authentication {
276                    password "$9$other"; ## SECRET-DATA
277                }
278                dynamic-profile PROFILE1 {
279                    aggregate-clients merge;
280                }
281                interface ge-1/0/0.100 upto ge-1/0/0.200;
282                interface ge-1/0/0.150 exclude;
283            }
284        }
285        dhcp-local-server {
286            group G1 {
287                interface ge-1/0/0.0;
288                interface ge-1/0/1.0 upto ge-1/0/1.9;
289            }
290            dhcpv6 {
291                group V6 {
292                    interface ge-2/0/0.0;
293                }
294            }
295        }
296        local-policy-decision-function {
297            statistics {
298                aacl-statistics-profile AACL1 {
299                    aacl-fields {
300                        input-bytes;
301                        output-bytes;
302                    }
303                    file aacl.stats;
304                    record-mode interim-full;
305                    report-interval 30;
306                }
307            }
308        }
309    }
310}
311"#;
312
313    #[test]
314    fn extracts_subscriber_management() {
315        let system = system(&juniper::parse_braces(SUBSCRIBERS)).unwrap();
316        let management = system.services.unwrap().subscriber_management.unwrap();
317        assert!(management.enable);
318        assert!(management.cgnat);
319        assert!(management.enforce_strict_scale_limit_license);
320        assert_eq!(management.gres_route_flush_delay.as_deref(), Some("60"));
321
322        let redundancy = management.redundancy.unwrap();
323        assert!(redundancy.no_advertise_routes_on_backup);
324        assert_eq!(redundancy.protocols, ["vrrp"]);
325        let interface = &redundancy.interfaces[0];
326        assert_eq!(interface.name, "ae0.100");
327        assert_eq!(interface.local_inet_address.as_deref(), Some("192.0.2.1"));
328        assert_eq!(interface.local_inet6_address.as_deref(), Some("2001:db8::1"));
329        assert_eq!(interface.virtual_inet_address.as_deref(), Some("192.0.2.254"));
330        assert_eq!(interface.virtual_inet6_address.as_deref(), Some("2001:db8::fe"));
331    }
332
333    #[test]
334    fn extracts_static_subscribers() {
335        let system = system(&juniper::parse_braces(SUBSCRIBERS)).unwrap();
336        let subscribers = system.services.unwrap().static_subscribers.unwrap();
337        assert_eq!(subscribers.access_profile.as_deref(), Some("AP"));
338        assert_eq!(subscribers.service_profile.as_deref(), Some("SP"));
339
340        let group = &subscribers.groups[0];
341        assert_eq!(group.name, "STATIC1");
342        assert_eq!(group.access_profile.as_deref(), Some("AP1"));
343        assert_eq!(group.dynamic_profiles[0].name, "PROFILE1");
344        assert_eq!(group.dynamic_profiles[0].aggregate_clients.as_deref(), Some("merge"));
345        assert_eq!(
346            group.interfaces,
347            ["ge-1/0/0.100 upto ge-1/0/0.200", "ge-1/0/0.150 exclude"]
348        );
349    }
350
351    #[test]
352    fn extracts_dhcp_local_server_groups() {
353        let system = system(&juniper::parse_braces(SUBSCRIBERS)).unwrap();
354        let server = system.services.unwrap().dhcp_local_server.unwrap();
355        assert_eq!(server.groups.len(), 1);
356        assert_eq!(server.groups[0].name, "G1");
357        assert_eq!(
358            server.groups[0].interfaces,
359            ["ge-1/0/0.0", "ge-1/0/1.0 upto ge-1/0/1.9"]
360        );
361        assert_eq!(server.dhcpv6_groups[0].name, "V6");
362        assert_eq!(server.dhcpv6_groups[0].interfaces, ["ge-2/0/0.0"]);
363    }
364
365    #[test]
366    fn extracts_aacl_statistics_profiles() {
367        let system = system(&juniper::parse_braces(SUBSCRIBERS)).unwrap();
368        let function = system.services.unwrap().local_policy_decision_function.unwrap();
369        let profile = &function.statistics[0];
370        assert_eq!(profile.name, "AACL1");
371        assert_eq!(profile.aacl_fields, ["input-bytes", "output-bytes"]);
372        assert_eq!(profile.file.as_deref(), Some("aacl.stats"));
373        assert_eq!(profile.record_mode.as_deref(), Some("interim-full"));
374        assert_eq!(profile.report_interval.as_deref(), Some("30"));
375    }
376
377    #[test]
378    fn subscriber_settings_are_optional() {
379        let system = system(&juniper::parse_braces(
380            "system {\n    services {\n        ssh;\n    }\n}\n",
381        ))
382        .unwrap();
383        assert!(system.dynamic_profile_options.is_none());
384        let services = system.services.unwrap();
385        assert!(services.subscriber_management.is_none());
386        assert!(services.static_subscribers.is_none());
387        assert!(services.dhcp_local_server.is_none());
388        assert!(services.local_policy_decision_function.is_none());
389    }
390}