Skip to main content

asterust_junos/system/
root_authentication.rs

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