asterust_junos/system/
root_authentication.rs1use super::authentication_methods;
9use crate::child_block;
10use asterust::config::ConfigLine;
11
12#[derive(Clone, Debug, PartialEq, Eq)]
14#[non_exhaustive]
15pub struct RootAuthentication {
16 pub methods: Vec<String>,
18}
19
20pub(super) fn root_authentication(system: &ConfigLine) -> Option<RootAuthentication> {
21 child_block(system, "root-authentication").map(|auth| RootAuthentication {
22 methods: authentication_methods(auth),
23 })
24}
25
26#[cfg(test)]
27mod tests {
28 use crate::system::system;
29 use asterust::config::juniper;
30
31 #[test]
32 fn extracts_root_authentication_methods_without_secrets() {
33 let system = system(&juniper::parse_braces(
34 r#"
35system {
36 root-authentication {
37 encrypted-password "$6$salt$hash"; ## SECRET-DATA
38 ssh-rsa "ssh-rsa AAAAB3Nza root@host"; ## SECRET-DATA
39 ssh-rsa "ssh-rsa AAAAB3Nzb ops@host" {
40 from 192.0.2.0/24;
41 }
42 }
43}
44"#,
45 ))
46 .unwrap();
47 let root = system.root_authentication.unwrap();
48 assert_eq!(root.methods, ["encrypted-password", "ssh-rsa", "ssh-rsa"]);
49 }
50
51 #[test]
52 fn root_authentication_can_disable_public_keys() {
53 let system = system(&juniper::parse_braces(
54 "system {\n root-authentication {\n no-public-keys;\n }\n}\n",
55 ))
56 .unwrap();
57 assert_eq!(system.root_authentication.unwrap().methods, ["no-public-keys"]);
58 }
59
60 #[test]
61 fn no_root_authentication_is_none() {
62 let system = system(&juniper::parse_braces("system {\n host-name pe1;\n}\n")).unwrap();
63 assert!(system.root_authentication.is_none());
64 }
65}