Skip to main content

asterust_junos/system/login/
mod.rs

1//! `login`
2//! `system { login { ... } }`.
3//!
4//! # See also
5//!
6//! - [login](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/login-edit-system.html)
7
8mod class;
9mod password;
10mod retry_options;
11mod user;
12
13pub use class::LoginClass;
14pub use password::LoginPassword;
15pub use retry_options::LoginRetryOptions;
16pub use user::LoginUser;
17
18use crate::{arg_of, args_of, child_block, list_values, named_children};
19use asterust::config::ConfigLine;
20use class::parse_class;
21use password::parse_password;
22use retry_options::parse_retry_options;
23use user::parse_user;
24
25/// The `system login` configuration.
26#[derive(Clone, Debug, PartialEq, Eq)]
27#[non_exhaustive]
28pub struct Login {
29    /// The banner shown before login.
30    pub message: Option<String>,
31    /// The text shown after login.
32    pub announcement: Option<String>,
33    /// Minutes.
34    pub idle_timeout: Option<String>,
35    /// Addresses from `deny-sources { address [ ... ]; }`.
36    pub deny_sources: Vec<String>,
37    pub password: Option<LoginPassword>,
38    pub retry_options: Option<LoginRetryOptions>,
39    pub classes: Vec<LoginClass>,
40    pub users: Vec<LoginUser>,
41}
42
43/// Values of every `<keyword> ...` child, whether written as a bare value or as a `[ list ]`.
44fn list(line: &ConfigLine, keyword: &str) -> Vec<String> {
45    args_of(line, keyword).into_iter().flat_map(list_values).collect()
46}
47
48pub(super) fn parse_login(login: &ConfigLine) -> Login {
49    Login {
50        message: arg_of(login, "message").map(str::to_string),
51        announcement: arg_of(login, "announcement").map(str::to_string),
52        idle_timeout: arg_of(login, "idle-timeout").map(str::to_string),
53        deny_sources: child_block(login, "deny-sources")
54            .map(|sources| list(sources, "address"))
55            .unwrap_or_default(),
56        password: child_block(login, "password").map(parse_password),
57        retry_options: child_block(login, "retry-options").map(parse_retry_options),
58        classes: named_children(login, "class")
59            .map(|(name, class)| parse_class(name, class))
60            .collect(),
61        users: named_children(login, "user")
62            .map(|(name, user)| parse_user(name, user))
63            .collect(),
64    }
65}
66
67#[cfg(test)]
68mod tests {
69    use crate::system::system;
70    use asterust::config::juniper;
71
72    const LOGIN: &str = r#"
73system {
74    login {
75        message "Authorized access only";
76        announcement "Maintenance window Sunday";
77        class NOC {
78            permissions [ view view-configuration ];
79            idle-timeout 15;
80            allow-commands "(ping)|(traceroute)";
81            deny-configuration ".*";
82        }
83        class OPS {
84            permissions all;
85            deny-commands "(request system)";
86            allow-configuration "(interfaces)|(protocols)";
87        }
88        user admin {
89            uid 2000;
90            class super-user;
91            full-name "Network Admin";
92            authentication {
93                encrypted-password "$6$salt$hash"; ## SECRET-DATA
94                ssh-ed25519 "ssh-ed25519 AAAAC3Nza admin@host"; ## SECRET-DATA
95            }
96        }
97        user monitor {
98            class NOC;
99        }
100    }
101}
102"#;
103
104    #[test]
105    fn extracts_login_banner_and_classes() {
106        let login = system(&juniper::parse_braces(LOGIN)).unwrap().login.unwrap();
107        assert_eq!(login.message.as_deref(), Some("Authorized access only"));
108        assert_eq!(login.announcement.as_deref(), Some("Maintenance window Sunday"));
109
110        assert_eq!(login.classes.len(), 2);
111        let noc = &login.classes[0];
112        assert_eq!(noc.name, "NOC");
113        assert_eq!(noc.permissions, ["view", "view-configuration"]);
114        assert_eq!(noc.idle_timeout.as_deref(), Some("15"));
115        assert_eq!(noc.allow_commands.as_deref(), Some("(ping)|(traceroute)"));
116        assert_eq!(noc.deny_configuration.as_deref(), Some(".*"));
117        assert_eq!(noc.deny_commands, None);
118
119        let ops = &login.classes[1];
120        assert_eq!(ops.permissions, ["all"]);
121        assert_eq!(ops.deny_commands.as_deref(), Some("(request system)"));
122        assert_eq!(ops.allow_configuration.as_deref(), Some("(interfaces)|(protocols)"));
123    }
124
125    #[test]
126    fn extracts_login_users_without_secrets() {
127        let login = system(&juniper::parse_braces(LOGIN)).unwrap().login.unwrap();
128        assert_eq!(login.users.len(), 2);
129
130        let admin = &login.users[0];
131        assert_eq!(admin.name, "admin");
132        assert_eq!(admin.class.as_deref(), Some("super-user"));
133        assert_eq!(admin.uid.as_deref(), Some("2000"));
134        assert_eq!(admin.full_name.as_deref(), Some("Network Admin"));
135        assert_eq!(admin.authentication_methods, ["encrypted-password", "ssh-ed25519"]);
136        let secrets: Vec<_> = admin
137            .authentication_secrets
138            .iter()
139            .map(|s| (s.method.as_str(), s.value.expose()))
140            .collect();
141        let expected: &[_] = if cfg!(feature = "secrets") {
142            &[
143                ("encrypted-password", "$6$salt$hash"),
144                ("ssh-ed25519", "ssh-ed25519 AAAAC3Nza admin@host"),
145            ]
146        } else {
147            &[]
148        };
149        assert_eq!(secrets, expected);
150
151        let monitor = &login.users[1];
152        assert_eq!(monitor.class.as_deref(), Some("NOC"));
153        assert!(monitor.authentication_methods.is_empty());
154    }
155
156    // Not from documentation: written from the CLI Reference syntax page. See the README "Sources".
157    const LOGIN_OPTIONS: &str = r#"
158system {
159    login {
160        idle-timeout 30;
161        deny-sources {
162            address [ 203.0.113.0/24 198.51.100.7 ];
163        }
164        password {
165            format sha512;
166            change-type character-sets;
167            minimum-length 12;
168            maximum-length 64;
169            minimum-changes 3;
170            minimum-upper-cases 1;
171            minimum-lower-cases 1;
172            minimum-numerics 1;
173            minimum-punctuations 1;
174            minimum-reuse 5;
175            maximum-lifetime 90;
176            minimum-lifetime 1;
177            minimum-character-changes 2;
178        }
179        retry-options {
180            tries-before-disconnect 3;
181            backoff-threshold 2;
182            backoff-factor 5;
183            minimum-time 20;
184            maximum-time 60;
185            lockout-period 10;
186        }
187        class OPS {
188            access-start 08:00;
189            access-end 18:00;
190            allowed-days [ monday tuesday friday ];
191            allow-sources [ 192.0.2.0/24 ];
192            deny-sources 192.0.2.99;
193            allow-times [ 08:00-12:00 ];
194            deny-times [ 22:00-23:00 ];
195        }
196    }
197}
198"#;
199
200    #[test]
201    fn extracts_login_level_options() {
202        let login = system(&juniper::parse_braces(LOGIN_OPTIONS)).unwrap().login.unwrap();
203        assert_eq!(login.idle_timeout.as_deref(), Some("30"));
204        assert_eq!(login.deny_sources, ["203.0.113.0/24", "198.51.100.7"]);
205    }
206
207    #[test]
208    fn extracts_password_policy() {
209        let login = system(&juniper::parse_braces(LOGIN_OPTIONS)).unwrap().login.unwrap();
210        let password = login.password.unwrap();
211        assert_eq!(password.format.as_deref(), Some("sha512"));
212        assert_eq!(password.change_type.as_deref(), Some("character-sets"));
213        assert_eq!(password.minimum_length.as_deref(), Some("12"));
214        assert_eq!(password.maximum_length.as_deref(), Some("64"));
215        assert_eq!(password.minimum_changes.as_deref(), Some("3"));
216        assert_eq!(password.minimum_upper_cases.as_deref(), Some("1"));
217        assert_eq!(password.minimum_lower_cases.as_deref(), Some("1"));
218        assert_eq!(password.minimum_numerics.as_deref(), Some("1"));
219        assert_eq!(password.minimum_punctuations.as_deref(), Some("1"));
220        assert_eq!(password.minimum_reuse.as_deref(), Some("5"));
221        assert_eq!(password.maximum_lifetime.as_deref(), Some("90"));
222        assert_eq!(password.minimum_lifetime.as_deref(), Some("1"));
223        assert_eq!(password.minimum_character_changes.as_deref(), Some("2"));
224    }
225
226    #[test]
227    fn extracts_retry_options() {
228        let login = system(&juniper::parse_braces(LOGIN_OPTIONS)).unwrap().login.unwrap();
229        let retry = login.retry_options.unwrap();
230        assert_eq!(retry.tries_before_disconnect.as_deref(), Some("3"));
231        assert_eq!(retry.backoff_threshold.as_deref(), Some("2"));
232        assert_eq!(retry.backoff_factor.as_deref(), Some("5"));
233        assert_eq!(retry.minimum_time.as_deref(), Some("20"));
234        assert_eq!(retry.maximum_time.as_deref(), Some("60"));
235        assert_eq!(retry.lockout_period.as_deref(), Some("10"));
236    }
237
238    #[test]
239    fn extracts_class_access_restrictions() {
240        let login = system(&juniper::parse_braces(LOGIN_OPTIONS)).unwrap().login.unwrap();
241        let class = &login.classes[0];
242        assert_eq!(class.access_start.as_deref(), Some("08:00"));
243        assert_eq!(class.access_end.as_deref(), Some("18:00"));
244        assert_eq!(class.allowed_days, ["monday", "tuesday", "friday"]);
245        assert_eq!(class.allow_sources, ["192.0.2.0/24"]);
246        assert_eq!(class.deny_sources, ["192.0.2.99"]);
247        assert_eq!(class.allow_times, ["08:00-12:00"]);
248        assert_eq!(class.deny_times, ["22:00-23:00"]);
249    }
250
251    #[test]
252    fn login_options_are_optional() {
253        let login = system(&juniper::parse_braces(
254            "system {\n    login {\n        class OPS;\n    }\n}\n",
255        ))
256        .unwrap()
257        .login
258        .unwrap();
259        assert_eq!(login.idle_timeout, None);
260        assert!(login.deny_sources.is_empty());
261        assert!(login.password.is_none());
262        assert!(login.retry_options.is_none());
263    }
264
265    #[test]
266    fn no_login_is_none() {
267        assert!(system(&juniper::parse_braces("system {\n    host-name pe1;\n}\n"))
268            .unwrap()
269            .login
270            .is_none());
271    }
272}