Skip to main content

asterust_junos/system/
mod.rs

1//! `system { ... }`.
2
3mod accounting;
4mod addressing;
5mod archival;
6mod auth;
7mod autoinstallation;
8mod commit;
9mod extensions;
10mod flow_tap_dtcp;
11mod internet;
12mod license;
13mod location;
14mod login;
15mod ntp;
16mod pixel_tracing;
17mod ports;
18mod processes;
19mod schema;
20mod scripts;
21mod service_deployment;
22mod services;
23mod subscriber;
24mod syslog;
25
26pub use accounting::{Accounting, AccountingDestination};
27pub use addressing::{BackupRouter, NameServer, StaticHostMapping};
28pub use archival::{Archival, ArchivalConfiguration};
29pub use auth::{MasterPassword, RadiusServer, RootAuthentication, TacplusServer};
30pub use autoinstallation::{Autoinstallation, AutoinstallationInterface};
31pub use commit::Commit;
32pub use extensions::{ExtensionApplication, ExtensionProvider, Extensions, ProviderLicense};
33pub use flow_tap_dtcp::{FlowTapDtcp, FlowTapDtcpSsh};
34pub use internet::{IcmpRateLimit, InternetOptions};
35pub use license::{License, LicenseAutoupdate, LicenseRenew};
36pub use location::Location;
37pub use login::{Login, LoginClass, LoginPassword, LoginRetryOptions, LoginUser};
38pub use ntp::{Ntp, NtpAuthenticationKey, NtpHost};
39pub use pixel_tracing::PixelTracing;
40pub use ports::{Auxiliary, Console, Ports};
41pub use processes::Process;
42pub use schema::Schema;
43pub use scripts::Scripts;
44pub use service_deployment::{ServiceDeployment, ServiceDeploymentServer};
45pub use services::{
46    Netconf, NetconfSsh, OutboundSshClient, OutboundSshServer, Services, SshService, WebManagement, WebManagementHttp,
47    WebManagementHttps, XnmClearText, XnmSsl,
48};
49pub use subscriber::{
50    AaclStatisticsProfile, DhcpLocalServer, DhcpLocalServerGroup, DynamicProfileOptions, LocalPolicyDecisionFunction,
51    RedundancyInterface, StaticSubscriberGroup, StaticSubscriberProfile, StaticSubscribers, SubscriberManagement,
52    SubscriberRedundancy,
53};
54pub use syslog::{Syslog, SyslogArchive, SyslogHost, SyslogRule, SyslogStructuredData, SyslogTarget};
55
56use crate::{arg_of, args_of, child_block, has_leaf, list_values, top_level};
57use accounting::parse_accounting;
58use addressing::{name_servers, parse_backup_router, static_host_mappings};
59use archival::parse_archival;
60use asterust::config::Config;
61use auth::{parse_master_password, password_methods, radius_servers, root_authentication, tacplus_servers};
62use autoinstallation::parse_autoinstallation;
63use commit::parse_commit;
64use extensions::parse_extensions;
65use internet::parse_internet_options;
66use license::parse_license;
67use location::parse_location;
68use login::parse_login;
69use ntp::parse_ntp;
70use pixel_tracing::parse_pixel_tracing;
71use ports::parse_ports;
72use processes::parse_processes;
73use schema::parse_schema;
74use scripts::parse_scripts;
75use services::parse_services;
76use subscriber::parse_dynamic_profile_options;
77use syslog::parse_syslog;
78
79/// The top-level `system` configuration.
80#[derive(Clone, Debug, PartialEq, Eq)]
81#[non_exhaustive]
82pub struct System {
83    pub host_name: Option<String>,
84    pub domain_name: Option<String>,
85    /// e.g. `"UTC"` or `"America/New_York"`.
86    pub time_zone: Option<String>,
87    pub domain_search: Vec<String>,
88    pub static_host_mappings: Vec<StaticHostMapping>,
89    pub backup_router: Option<BackupRouter>,
90    pub inet6_backup_router: Option<BackupRouter>,
91    /// Methods in the order tried: `password`, `radius`, `tacplus`.
92    pub authentication_order: Vec<String>,
93    pub radius_servers: Vec<RadiusServer>,
94    pub tacplus_servers: Vec<TacplusServer>,
95    pub location: Option<Location>,
96    pub ports: Option<Ports>,
97    pub max_configurations_on_flash: Option<String>,
98    pub max_configuration_rollbacks: Option<String>,
99    pub no_redirects: bool,
100    /// `default-address-selection`: use the loopback as the source of locally generated packets.
101    pub default_address_selection: bool,
102    pub internet_options: Option<InternetOptions>,
103    pub commit: Option<Commit>,
104    pub processes: Vec<Process>,
105    pub dynamic_profile_options: Option<DynamicProfileOptions>,
106    pub extensions: Option<Extensions>,
107    pub autoinstallation: Option<Autoinstallation>,
108    pub schema: Option<Schema>,
109    pub pixel_tracing: Option<PixelTracing>,
110    pub scripts: Option<Scripts>,
111    pub no_multicast_echo: bool,
112    pub no_ping_record_route: bool,
113    pub no_ping_time_stamp: bool,
114    pub auto_snapshot: bool,
115    pub compress_configuration_files: bool,
116    pub mirror_flash_on_disk: bool,
117    pub switchover_on_routing_crash: bool,
118    pub use_imported_time_zones: bool,
119    pub archival: Option<Archival>,
120    pub accounting: Option<Accounting>,
121    pub root_authentication: Option<RootAuthentication>,
122    pub master_password: Option<MasterPassword>,
123    /// Method name of `diag-port-authentication`, e.g. `encrypted-password`; the password is not extracted.
124    pub diag_port_authentication: Vec<String>,
125    /// Method names of `pic-console-authentication`; the password is not extracted.
126    pub pic_console_authentication: Vec<String>,
127    pub license: Option<License>,
128    pub name_servers: Vec<NameServer>,
129    pub ntp: Option<Ntp>,
130    pub syslog: Option<Syslog>,
131    pub login: Option<Login>,
132    pub services: Option<Services>,
133}
134
135/// The top-level `system` block, or `None` if there is none.
136pub fn system(config: &Config) -> Option<System> {
137    let system = top_level(config, "system")?;
138    Some(System {
139        host_name: arg_of(system, "host-name").map(str::to_string),
140        domain_name: arg_of(system, "domain-name").map(str::to_string),
141        time_zone: arg_of(system, "time-zone").map(str::to_string),
142        domain_search: args_of(system, "domain-search")
143            .into_iter()
144            .flat_map(list_values)
145            .collect(),
146        static_host_mappings: static_host_mappings(system),
147        backup_router: arg_of(system, "backup-router").map(parse_backup_router),
148        inet6_backup_router: arg_of(system, "inet6-backup-router").map(parse_backup_router),
149        authentication_order: args_of(system, "authentication-order")
150            .into_iter()
151            .flat_map(list_values)
152            .collect(),
153        radius_servers: radius_servers(system, "radius-server"),
154        tacplus_servers: tacplus_servers(system, "tacplus-server"),
155        location: child_block(system, "location").map(parse_location),
156        ports: child_block(system, "ports").map(parse_ports),
157        max_configurations_on_flash: arg_of(system, "max-configurations-on-flash").map(str::to_string),
158        max_configuration_rollbacks: arg_of(system, "max-configuration-rollbacks").map(str::to_string),
159        no_redirects: has_leaf(system, "no-redirects"),
160        default_address_selection: has_leaf(system, "default-address-selection"),
161        internet_options: child_block(system, "internet-options").map(parse_internet_options),
162        commit: child_block(system, "commit").map(parse_commit),
163        processes: child_block(system, "processes")
164            .map(parse_processes)
165            .unwrap_or_default(),
166        dynamic_profile_options: child_block(system, "dynamic-profile-options").map(parse_dynamic_profile_options),
167        extensions: child_block(system, "extensions").map(parse_extensions),
168        autoinstallation: child_block(system, "autoinstallation").map(parse_autoinstallation),
169        schema: child_block(system, "schema").map(parse_schema),
170        pixel_tracing: child_block(system, "pixel-tracing").map(parse_pixel_tracing),
171        scripts: child_block(system, "scripts").map(parse_scripts),
172        no_multicast_echo: has_leaf(system, "no-multicast-echo"),
173        no_ping_record_route: has_leaf(system, "no-ping-record-route"),
174        no_ping_time_stamp: has_leaf(system, "no-ping-time-stamp"),
175        auto_snapshot: has_leaf(system, "auto-snapshot"),
176        compress_configuration_files: has_leaf(system, "compress-configuration-files"),
177        mirror_flash_on_disk: has_leaf(system, "mirror-flash-on-disk"),
178        switchover_on_routing_crash: has_leaf(system, "switchover-on-routing-crash"),
179        use_imported_time_zones: has_leaf(system, "use-imported-time-zones"),
180        archival: child_block(system, "archival").map(parse_archival),
181        accounting: child_block(system, "accounting").map(parse_accounting),
182        root_authentication: root_authentication(system),
183        master_password: child_block(system, "master-password").map(parse_master_password),
184        diag_port_authentication: password_methods(system, "diag-port-authentication"),
185        pic_console_authentication: password_methods(system, "pic-console-authentication"),
186        license: child_block(system, "license").map(parse_license),
187        name_servers: name_servers(system),
188        ntp: child_block(system, "ntp").map(parse_ntp),
189        syslog: child_block(system, "syslog").map(parse_syslog),
190        login: child_block(system, "login").map(parse_login),
191        services: child_block(system, "services").map(parse_services),
192    })
193}
194
195#[cfg(test)]
196mod tests {
197    use super::*;
198    use asterust::config::juniper;
199
200    // Not from documentation: written from the CLI Reference syntax pages. See the README "Sources".
201    #[test]
202    fn extracts_host_name() {
203        let system = system(&juniper::parse_braces("system {\n    host-name pe1;\n}\n")).unwrap();
204        assert_eq!(system.host_name.as_deref(), Some("pe1"));
205    }
206
207    #[test]
208    fn host_name_is_optional() {
209        let system = system(&juniper::parse_braces("system {\n    domain-name example.net;\n}\n")).unwrap();
210        assert_eq!(system.host_name, None);
211    }
212
213    #[test]
214    fn skips_inactive_host_name() {
215        let system = system(&juniper::parse_braces("system {\n    inactive: host-name pe1;\n}\n")).unwrap();
216        assert_eq!(system.host_name, None);
217    }
218
219    #[test]
220    fn extracts_domain_name() {
221        let system = system(&juniper::parse_braces(
222            "system {\n    host-name pe1;\n    domain-name example.net;\n}\n",
223        ))
224        .unwrap();
225        assert_eq!(system.domain_name.as_deref(), Some("example.net"));
226    }
227
228    #[test]
229    fn domain_name_is_optional() {
230        let system = system(&juniper::parse_braces("system {\n    host-name pe1;\n}\n")).unwrap();
231        assert_eq!(system.domain_name, None);
232    }
233
234    #[test]
235    fn extracts_time_zone() {
236        let system = system(&juniper::parse_braces("system {\n    time-zone America/New_York;\n}\n")).unwrap();
237        assert_eq!(system.time_zone.as_deref(), Some("America/New_York"));
238    }
239
240    #[test]
241    fn time_zone_is_optional() {
242        let system = system(&juniper::parse_braces("system {\n    host-name pe1;\n}\n")).unwrap();
243        assert_eq!(system.time_zone, None);
244    }
245
246    #[test]
247    fn extracts_configuration_limits_and_no_redirects() {
248        let system = system(&juniper::parse_braces(
249            "system {\n    max-configurations-on-flash 5;\n    max-configuration-rollbacks 20;\n    no-redirects;\n}\n",
250        ))
251        .unwrap();
252        assert_eq!(system.max_configurations_on_flash.as_deref(), Some("5"));
253        assert_eq!(system.max_configuration_rollbacks.as_deref(), Some("20"));
254        assert!(system.no_redirects);
255    }
256
257    #[test]
258    fn configuration_limits_are_optional() {
259        let system = system(&juniper::parse_braces("system {\n    host-name pe1;\n}\n")).unwrap();
260        assert_eq!(system.max_configurations_on_flash, None);
261        assert_eq!(system.max_configuration_rollbacks, None);
262        assert!(!system.no_redirects);
263    }
264
265    #[test]
266    fn extracts_system_flags() {
267        let system = system(&juniper::parse_braces(
268            "system {\n    no-multicast-echo;\n    no-ping-record-route;\n    no-ping-time-stamp;\n    auto-snapshot;\n    compress-configuration-files;\n    mirror-flash-on-disk;\n    switchover-on-routing-crash;\n    use-imported-time-zones;\n}\n",
269        ))
270        .unwrap();
271        assert!(system.no_multicast_echo);
272        assert!(system.no_ping_record_route);
273        assert!(system.no_ping_time_stamp);
274        assert!(system.auto_snapshot);
275        assert!(system.compress_configuration_files);
276        assert!(system.mirror_flash_on_disk);
277        assert!(system.switchover_on_routing_crash);
278        assert!(system.use_imported_time_zones);
279    }
280
281    #[test]
282    fn system_flags_default_to_off() {
283        let system = system(&juniper::parse_braces("system {\n    host-name pe1;\n}\n")).unwrap();
284        assert!(!system.no_multicast_echo);
285        assert!(!system.auto_snapshot);
286        assert!(!system.use_imported_time_zones);
287    }
288
289    #[test]
290    fn no_system_is_none() {
291        assert!(system(&juniper::parse_braces("interfaces {\n    lo0;\n}\n")).is_none());
292    }
293}