Skip to main content

asterust_junos/system/login/
mod.rs

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