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 dns;
10mod extension_service;
11mod flow_tap_dtcp;
12mod ftp;
13mod http_servers;
14mod inband_ztp;
15mod jeb;
16mod limits;
17mod local_policy_decision_function;
18mod netconf;
19mod outbound_https;
20mod outbound_ssh;
21mod resource_monitor;
22mod rest;
23mod reverse;
24mod service_deployment;
25mod ssh;
26mod static_subscribers;
27mod subscriber_management;
28mod telnet;
29mod web_management;
30mod xnm_clear_text;
31mod xnm_ssl;
32
33pub use dhcp_local_server::{DhcpLocalServer, DhcpLocalServerGroup};
34pub use dns::{Dns, DnsCacheEntry, DnsDomain, DnsProxy, DnsView, Dnssec};
35pub use extension_service::{ExtensionService, GrpcService, GrpcSsl, JetNotification};
36pub use flow_tap_dtcp::{FlowTapDtcp, FlowTapDtcpSsh};
37pub use ftp::FtpService;
38pub use http_servers::{HttpServer, HttpServerTls};
39pub use inband_ztp::InbandZtp;
40pub use jeb::{Jeb, JebTls};
41pub use limits::ConnectionLimits;
42pub use local_policy_decision_function::{AaclStatisticsProfile, LocalPolicyDecisionFunction};
43pub use netconf::{
44    Netconf, NetconfClientIdentity, NetconfDefaultClientIdentity, NetconfHelloMessage, NetconfMonitoring, NetconfSsh,
45    NetconfTls, NetconfYangModules,
46};
47pub use outbound_https::{OutboundHttpsAddress, OutboundHttpsClient};
48pub use outbound_ssh::{OutboundSshClient, OutboundSshServer};
49pub use resource_monitor::{FpcLimit, JtreeResourceType, PicLimit, PortLimit, ResourceMonitor, SubscriberLimit};
50pub use rest::{Rest, RestHttp, RestHttps};
51pub use reverse::Reverse;
52pub use service_deployment::{ServiceDeployment, ServiceDeploymentServer};
53pub use ssh::{SshModuli, SshService};
54pub use static_subscribers::{StaticSubscriberGroup, StaticSubscriberProfile, StaticSubscribers};
55pub use subscriber_management::{RedundancyInterface, SubscriberManagement, SubscriberRedundancy};
56pub use telnet::TelnetService;
57pub use web_management::{WebManagement, WebManagementHttp, WebManagementHttps, WebManagementVirtualDomain};
58pub use xnm_clear_text::XnmClearText;
59pub use xnm_ssl::XnmSsl;
60
61use crate::child_block;
62use crate::protocols::traceoptions::{parse_traceoptions, Traceoptions};
63use asterust::config::ConfigLine;
64use dhcp_local_server::parse_dhcp_local_server;
65use dns::parse_dns;
66use extension_service::parse_extension_service;
67use flow_tap_dtcp::parse_flow_tap_dtcp;
68use ftp::parse_ftp;
69use http_servers::parse_http_servers;
70use inband_ztp::parse_inband_ztp;
71use jeb::parse_jeb;
72use limits::parse_limits;
73use local_policy_decision_function::parse_local_policy_decision_function;
74use netconf::parse_netconf;
75use outbound_https::parse_outbound_https;
76use outbound_ssh::parse_outbound_ssh;
77use resource_monitor::parse_resource_monitor;
78use rest::parse_rest;
79use reverse::parse_reverse;
80use service_deployment::parse_service_deployment;
81use ssh::parse_ssh;
82use static_subscribers::parse_static_subscribers;
83use subscriber_management::parse_subscriber_management;
84use telnet::parse_telnet;
85use web_management::parse_web_management;
86use xnm_clear_text::parse_xnm_clear_text;
87use xnm_ssl::parse_xnm_ssl;
88
89/// The `system services` configuration. Services other than those listed are not extracted.
90#[derive(Clone, Debug, PartialEq, Eq)]
91#[non_exhaustive]
92pub struct Services {
93    pub ssh: Option<SshService>,
94    pub telnet: bool,
95    pub ftp: bool,
96    /// `netconf { ssh; }`.
97    pub netconf_ssh: bool,
98    /// `web-management { http; }`.
99    pub http: bool,
100    /// `web-management { https; }`.
101    pub https: bool,
102    pub outbound_ssh: Vec<OutboundSshClient>,
103    /// The details behind `http` and `https`.
104    pub web_management: Option<WebManagement>,
105    /// The details behind `netconf_ssh`.
106    pub netconf: Option<Netconf>,
107    pub xnm_clear_text: Option<XnmClearText>,
108    pub xnm_ssl: Option<XnmSsl>,
109    pub subscriber_management: Option<SubscriberManagement>,
110    pub static_subscribers: Option<StaticSubscribers>,
111    pub dhcp_local_server: Option<DhcpLocalServer>,
112    pub local_policy_decision_function: Option<LocalPolicyDecisionFunction>,
113    pub flow_tap_dtcp: Option<FlowTapDtcp>,
114    pub service_deployment: Option<ServiceDeployment>,
115    pub telnet_service: Option<TelnetService>,
116    pub ftp_service: Option<FtpService>,
117    pub finger: Option<ConnectionLimits>,
118    pub tftp_server: Option<ConnectionLimits>,
119    pub reverse: Option<Reverse>,
120    pub rest: Option<Rest>,
121    /// The servers of `http { servers { ... } }`.
122    pub http_servers: Vec<HttpServer>,
123    pub extension_service: Option<ExtensionService>,
124    pub jeb: Option<Jeb>,
125    pub inband_ztp: Option<InbandZtp>,
126    pub outbound_https: Vec<OutboundHttpsClient>,
127    /// The `traceoptions` of `outbound-ssh`.
128    pub outbound_ssh_traceoptions: Option<Traceoptions>,
129    pub resource_monitor: Option<ResourceMonitor>,
130    pub dns: Option<Dns>,
131}
132
133pub(super) fn parse_services(services: &ConfigLine) -> Services {
134    let web = child_block(services, "web-management");
135    let netconf = child_block(services, "netconf");
136    Services {
137        ssh: child_block(services, "ssh").map(parse_ssh),
138        telnet: child_block(services, "telnet").is_some(),
139        ftp: child_block(services, "ftp").is_some(),
140        netconf_ssh: netconf.is_some_and(|n| child_block(n, "ssh").is_some()),
141        http: web.is_some_and(|w| child_block(w, "http").is_some()),
142        https: web.is_some_and(|w| child_block(w, "https").is_some()),
143        outbound_ssh: child_block(services, "outbound-ssh")
144            .map(parse_outbound_ssh)
145            .unwrap_or_default(),
146        web_management: web.map(parse_web_management),
147        netconf: netconf.map(parse_netconf),
148        xnm_clear_text: child_block(services, "xnm-clear-text").map(parse_xnm_clear_text),
149        xnm_ssl: child_block(services, "xnm-ssl").map(parse_xnm_ssl),
150        subscriber_management: child_block(services, "subscriber-management").map(parse_subscriber_management),
151        static_subscribers: child_block(services, "static-subscribers").map(parse_static_subscribers),
152        dhcp_local_server: child_block(services, "dhcp-local-server").map(parse_dhcp_local_server),
153        local_policy_decision_function: child_block(services, "local-policy-decision-function")
154            .map(parse_local_policy_decision_function),
155        flow_tap_dtcp: child_block(services, "flow-tap-dtcp").map(parse_flow_tap_dtcp),
156        service_deployment: child_block(services, "service-deployment").map(parse_service_deployment),
157        telnet_service: child_block(services, "telnet").map(parse_telnet),
158        ftp_service: child_block(services, "ftp").map(parse_ftp),
159        finger: child_block(services, "finger").map(parse_limits),
160        tftp_server: child_block(services, "tftp-server").map(parse_limits),
161        reverse: child_block(services, "reverse").map(parse_reverse),
162        rest: child_block(services, "rest").map(parse_rest),
163        http_servers: child_block(services, "http")
164            .map(parse_http_servers)
165            .unwrap_or_default(),
166        extension_service: child_block(services, "extension-service").map(parse_extension_service),
167        jeb: child_block(services, "jeb").map(parse_jeb),
168        inband_ztp: child_block(services, "inband-ztp").map(parse_inband_ztp),
169        outbound_https: child_block(services, "outbound-https")
170            .map(parse_outbound_https)
171            .unwrap_or_default(),
172        outbound_ssh_traceoptions: child_block(services, "outbound-ssh").and_then(parse_traceoptions),
173        resource_monitor: child_block(services, "resource-monitor").map(parse_resource_monitor),
174        dns: child_block(services, "dns").map(parse_dns),
175    }
176}
177
178#[cfg(test)]
179mod tests {
180    use super::*;
181    use crate::system::system;
182    use asterust::config::juniper;
183
184    const SERVICES: &str = r#"
185system {
186    services {
187        ssh {
188            root-login deny;
189            protocol-version v2;
190            port 2222;
191            connection-limit 10;
192            rate-limit 5;
193            ciphers [ aes256-ctr aes128-ctr ];
194            sftp-server;
195        }
196        netconf {
197            ssh;
198        }
199        web-management {
200            https {
201                system-generated-certificate;
202            }
203        }
204        telnet;
205    }
206}
207"#;
208
209    #[test]
210    fn extracts_ssh_service_settings() {
211        let services = system(&juniper::parse_braces(SERVICES)).unwrap().services.unwrap();
212        let ssh = services.ssh.unwrap();
213        assert_eq!(ssh.root_login.as_deref(), Some("deny"));
214        assert_eq!(ssh.protocol_versions, ["v2"]);
215        assert_eq!(ssh.port.as_deref(), Some("2222"));
216        assert_eq!(ssh.connection_limit.as_deref(), Some("10"));
217        assert_eq!(ssh.rate_limit.as_deref(), Some("5"));
218        assert_eq!(ssh.ciphers, ["aes256-ctr", "aes128-ctr"]);
219        assert!(ssh.sftp_server);
220    }
221
222    #[test]
223    fn extracts_enabled_services() {
224        let services = system(&juniper::parse_braces(SERVICES)).unwrap().services.unwrap();
225        assert!(services.telnet);
226        assert!(services.netconf_ssh);
227        assert!(services.https);
228        assert!(!services.http);
229        assert!(!services.ftp);
230    }
231
232    #[test]
233    fn bare_service_statements_enable_with_defaults() {
234        let services = system(&juniper::parse_braces(
235            "system {\n    services {\n        ssh;\n        ftp;\n        web-management {\n            http;\n        }\n    }\n}\n",
236        ))
237        .unwrap()
238        .services
239        .unwrap();
240        assert_eq!(services.ssh, Some(SshService::default()));
241        assert!(services.ftp);
242        assert!(services.http);
243        assert!(!services.telnet);
244        assert!(!services.netconf_ssh);
245    }
246
247    #[test]
248    fn disabled_service_is_not_reported() {
249        let services = system(&juniper::parse_braces(
250            "system {\n    services {\n        inactive: telnet;\n        ssh;\n    }\n}\n",
251        ))
252        .unwrap()
253        .services
254        .unwrap();
255        assert!(!services.telnet);
256    }
257
258    #[test]
259    fn extracts_netconf_and_xnm_settings() {
260        let services = system(&juniper::parse_braces(
261            r#"
262system {
263    services {
264        netconf {
265            ssh {
266                port 830;
267                connection-limit 5;
268                rate-limit 10;
269            }
270            rfc-compliant;
271        }
272        xnm-clear-text {
273            connection-limit 3;
274        }
275        xnm-ssl {
276            port 3220;
277            local-certificate XNM;
278            rate-limit 7;
279        }
280    }
281}
282"#,
283        ))
284        .unwrap()
285        .services
286        .unwrap();
287        let netconf = services.netconf.unwrap();
288        assert!(netconf.rfc_compliant);
289        let ssh = netconf.ssh.unwrap();
290        assert_eq!(ssh.port.as_deref(), Some("830"));
291        assert_eq!(ssh.connection_limit.as_deref(), Some("5"));
292        assert_eq!(ssh.rate_limit.as_deref(), Some("10"));
293        assert!(services.netconf_ssh);
294
295        let clear_text = services.xnm_clear_text.unwrap();
296        assert_eq!(clear_text.connection_limit.as_deref(), Some("3"));
297        assert_eq!(clear_text.rate_limit, None);
298
299        let ssl = services.xnm_ssl.unwrap();
300        assert_eq!(ssl.port.as_deref(), Some("3220"));
301        assert_eq!(ssl.local_certificate.as_deref(), Some("XNM"));
302        assert_eq!(ssl.rate_limit.as_deref(), Some("7"));
303    }
304
305    #[test]
306    fn no_services_is_none() {
307        assert!(system(&juniper::parse_braces("system {\n    host-name pe1;\n}\n"))
308            .unwrap()
309            .services
310            .is_none());
311    }
312
313    // Not from documentation: written from the CLI Reference syntax pages. See the README "Sources".
314    const SUBSCRIBERS: &str = r#"
315system {
316    dynamic-profile-options {
317        versioning;
318    }
319    services {
320        subscriber-management {
321            enable;
322            enforce-strict-scale-limit-license;
323            gres-route-flush-delay 60;
324            cgnat {
325                enable;
326            }
327            redundancy {
328                interface ae0.100 {
329                    local-inet-address 192.0.2.1;
330                    local-inet6-address 2001:db8::1;
331                    shared-key "$9$secret"; ## SECRET-DATA
332                    virtual-inet-address 192.0.2.254;
333                    virtual-inet6-address 2001:db8::fe;
334                }
335                no-advertise-routes-on-backup;
336                protocol {
337                    vrrp;
338                }
339            }
340        }
341        static-subscribers {
342            access-profile AP;
343            service-profile SP;
344            authentication {
345                password "$9$secret"; ## SECRET-DATA
346            }
347            group STATIC1 {
348                access-profile AP1;
349                authentication {
350                    password "$9$other"; ## SECRET-DATA
351                }
352                dynamic-profile PROFILE1 {
353                    aggregate-clients merge;
354                }
355                interface ge-1/0/0.100 upto ge-1/0/0.200;
356                interface ge-1/0/0.150 exclude;
357            }
358        }
359        dhcp-local-server {
360            group G1 {
361                interface ge-1/0/0.0;
362                interface ge-1/0/1.0 upto ge-1/0/1.9;
363            }
364            dhcpv6 {
365                group V6 {
366                    interface ge-2/0/0.0;
367                }
368            }
369        }
370        local-policy-decision-function {
371            statistics {
372                aacl-statistics-profile AACL1 {
373                    aacl-fields {
374                        input-bytes;
375                        output-bytes;
376                    }
377                    file aacl.stats;
378                    record-mode interim-full;
379                    report-interval 30;
380                }
381            }
382        }
383    }
384}
385"#;
386
387    #[test]
388    fn extracts_subscriber_management() {
389        let system = system(&juniper::parse_braces(SUBSCRIBERS)).unwrap();
390        let management = system.services.unwrap().subscriber_management.unwrap();
391        assert!(management.enable);
392        assert!(management.cgnat);
393        assert!(management.enforce_strict_scale_limit_license);
394        assert_eq!(management.gres_route_flush_delay.as_deref(), Some("60"));
395
396        let redundancy = management.redundancy.unwrap();
397        assert!(redundancy.no_advertise_routes_on_backup);
398        assert_eq!(redundancy.protocols, ["vrrp"]);
399        let interface = &redundancy.interfaces[0];
400        assert_eq!(interface.name, "ae0.100");
401        assert_eq!(
402            interface.shared_key.as_ref().map(crate::Secret::expose),
403            crate::secret::expected("$9$secret")
404        );
405        assert_eq!(interface.local_inet_address.as_deref(), Some("192.0.2.1"));
406        assert_eq!(interface.local_inet6_address.as_deref(), Some("2001:db8::1"));
407        assert_eq!(interface.virtual_inet_address.as_deref(), Some("192.0.2.254"));
408        assert_eq!(interface.virtual_inet6_address.as_deref(), Some("2001:db8::fe"));
409    }
410
411    #[test]
412    fn extracts_static_subscribers() {
413        let system = system(&juniper::parse_braces(SUBSCRIBERS)).unwrap();
414        let subscribers = system.services.unwrap().static_subscribers.unwrap();
415        assert_eq!(subscribers.access_profile.as_deref(), Some("AP"));
416        assert_eq!(subscribers.service_profile.as_deref(), Some("SP"));
417        assert_eq!(
418            subscribers.password.as_ref().map(crate::Secret::expose),
419            crate::secret::expected("$9$secret")
420        );
421
422        let group = &subscribers.groups[0];
423        assert_eq!(group.name, "STATIC1");
424        assert_eq!(group.access_profile.as_deref(), Some("AP1"));
425        assert_eq!(
426            group.password.as_ref().map(crate::Secret::expose),
427            crate::secret::expected("$9$other")
428        );
429        assert_eq!(group.dynamic_profiles[0].name, "PROFILE1");
430        assert_eq!(group.dynamic_profiles[0].aggregate_clients.as_deref(), Some("merge"));
431        assert_eq!(
432            group.interfaces,
433            ["ge-1/0/0.100 upto ge-1/0/0.200", "ge-1/0/0.150 exclude"]
434        );
435    }
436
437    #[test]
438    fn extracts_dhcp_local_server_groups() {
439        let system = system(&juniper::parse_braces(SUBSCRIBERS)).unwrap();
440        let server = system.services.unwrap().dhcp_local_server.unwrap();
441        assert_eq!(server.groups.len(), 1);
442        assert_eq!(server.groups[0].name, "G1");
443        assert_eq!(
444            server.groups[0].interfaces,
445            ["ge-1/0/0.0", "ge-1/0/1.0 upto ge-1/0/1.9"]
446        );
447        assert_eq!(server.dhcpv6_groups[0].name, "V6");
448        assert_eq!(server.dhcpv6_groups[0].interfaces, ["ge-2/0/0.0"]);
449    }
450
451    #[test]
452    fn extracts_aacl_statistics_profiles() {
453        let system = system(&juniper::parse_braces(SUBSCRIBERS)).unwrap();
454        let function = system.services.unwrap().local_policy_decision_function.unwrap();
455        let profile = &function.statistics[0];
456        assert_eq!(profile.name, "AACL1");
457        assert_eq!(profile.aacl_fields, ["input-bytes", "output-bytes"]);
458        assert_eq!(profile.file.as_deref(), Some("aacl.stats"));
459        assert_eq!(profile.record_mode.as_deref(), Some("interim-full"));
460        assert_eq!(profile.report_interval.as_deref(), Some("30"));
461    }
462
463    #[test]
464    fn subscriber_settings_are_optional() {
465        let system = system(&juniper::parse_braces(
466            "system {\n    services {\n        ssh;\n    }\n}\n",
467        ))
468        .unwrap();
469        assert!(system.dynamic_profile_options.is_none());
470        let services = system.services.unwrap();
471        assert!(services.subscriber_management.is_none());
472        assert!(services.static_subscribers.is_none());
473        assert!(services.dhcp_local_server.is_none());
474        assert!(services.local_policy_decision_function.is_none());
475    }
476}