Skip to main content

asterust_junos/system/services/
mod.rs

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