asterust_junos/system/login/
mod.rs1mod 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#[derive(Clone, Debug, PartialEq, Eq)]
27#[non_exhaustive]
28pub struct Login {
29 pub message: Option<String>,
31 pub announcement: Option<String>,
33 pub idle_timeout: Option<String>,
35 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
43fn 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
137 let monitor = &login.users[1];
138 assert_eq!(monitor.class.as_deref(), Some("NOC"));
139 assert!(monitor.authentication_methods.is_empty());
140 }
141
142 const LOGIN_OPTIONS: &str = r#"
144system {
145 login {
146 idle-timeout 30;
147 deny-sources {
148 address [ 203.0.113.0/24 198.51.100.7 ];
149 }
150 password {
151 format sha512;
152 change-type character-sets;
153 minimum-length 12;
154 maximum-length 64;
155 minimum-changes 3;
156 minimum-upper-cases 1;
157 minimum-lower-cases 1;
158 minimum-numerics 1;
159 minimum-punctuations 1;
160 minimum-reuse 5;
161 maximum-lifetime 90;
162 minimum-lifetime 1;
163 minimum-character-changes 2;
164 }
165 retry-options {
166 tries-before-disconnect 3;
167 backoff-threshold 2;
168 backoff-factor 5;
169 minimum-time 20;
170 maximum-time 60;
171 lockout-period 10;
172 }
173 class OPS {
174 access-start 08:00;
175 access-end 18:00;
176 allowed-days [ monday tuesday friday ];
177 allow-sources [ 192.0.2.0/24 ];
178 deny-sources 192.0.2.99;
179 allow-times [ 08:00-12:00 ];
180 deny-times [ 22:00-23:00 ];
181 }
182 }
183}
184"#;
185
186 #[test]
187 fn extracts_login_level_options() {
188 let login = system(&juniper::parse_braces(LOGIN_OPTIONS)).unwrap().login.unwrap();
189 assert_eq!(login.idle_timeout.as_deref(), Some("30"));
190 assert_eq!(login.deny_sources, ["203.0.113.0/24", "198.51.100.7"]);
191 }
192
193 #[test]
194 fn extracts_password_policy() {
195 let login = system(&juniper::parse_braces(LOGIN_OPTIONS)).unwrap().login.unwrap();
196 let password = login.password.unwrap();
197 assert_eq!(password.format.as_deref(), Some("sha512"));
198 assert_eq!(password.change_type.as_deref(), Some("character-sets"));
199 assert_eq!(password.minimum_length.as_deref(), Some("12"));
200 assert_eq!(password.maximum_length.as_deref(), Some("64"));
201 assert_eq!(password.minimum_changes.as_deref(), Some("3"));
202 assert_eq!(password.minimum_upper_cases.as_deref(), Some("1"));
203 assert_eq!(password.minimum_lower_cases.as_deref(), Some("1"));
204 assert_eq!(password.minimum_numerics.as_deref(), Some("1"));
205 assert_eq!(password.minimum_punctuations.as_deref(), Some("1"));
206 assert_eq!(password.minimum_reuse.as_deref(), Some("5"));
207 assert_eq!(password.maximum_lifetime.as_deref(), Some("90"));
208 assert_eq!(password.minimum_lifetime.as_deref(), Some("1"));
209 assert_eq!(password.minimum_character_changes.as_deref(), Some("2"));
210 }
211
212 #[test]
213 fn extracts_retry_options() {
214 let login = system(&juniper::parse_braces(LOGIN_OPTIONS)).unwrap().login.unwrap();
215 let retry = login.retry_options.unwrap();
216 assert_eq!(retry.tries_before_disconnect.as_deref(), Some("3"));
217 assert_eq!(retry.backoff_threshold.as_deref(), Some("2"));
218 assert_eq!(retry.backoff_factor.as_deref(), Some("5"));
219 assert_eq!(retry.minimum_time.as_deref(), Some("20"));
220 assert_eq!(retry.maximum_time.as_deref(), Some("60"));
221 assert_eq!(retry.lockout_period.as_deref(), Some("10"));
222 }
223
224 #[test]
225 fn extracts_class_access_restrictions() {
226 let login = system(&juniper::parse_braces(LOGIN_OPTIONS)).unwrap().login.unwrap();
227 let class = &login.classes[0];
228 assert_eq!(class.access_start.as_deref(), Some("08:00"));
229 assert_eq!(class.access_end.as_deref(), Some("18:00"));
230 assert_eq!(class.allowed_days, ["monday", "tuesday", "friday"]);
231 assert_eq!(class.allow_sources, ["192.0.2.0/24"]);
232 assert_eq!(class.deny_sources, ["192.0.2.99"]);
233 assert_eq!(class.allow_times, ["08:00-12:00"]);
234 assert_eq!(class.deny_times, ["22:00-23:00"]);
235 }
236
237 #[test]
238 fn login_options_are_optional() {
239 let login = system(&juniper::parse_braces(
240 "system {\n login {\n class OPS;\n }\n}\n",
241 ))
242 .unwrap()
243 .login
244 .unwrap();
245 assert_eq!(login.idle_timeout, None);
246 assert!(login.deny_sources.is_empty());
247 assert!(login.password.is_none());
248 assert!(login.retry_options.is_none());
249 }
250
251 #[test]
252 fn no_login_is_none() {
253 assert!(system(&juniper::parse_braces("system {\n host-name pe1;\n}\n"))
254 .unwrap()
255 .login
256 .is_none());
257 }
258}