Skip to main content

asterust_junos/system/
mod.rs

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