Skip to main content

asterust_junos/system/
root_authentication.rs

1//! `root-authentication`
2//! `system { root-authentication { ... } }`.
3//!
4//! # See also
5//!
6//! - [root-authentication](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/root-authentication-edit-system.html)
7
8use super::authentication_methods;
9use super::authentication_secret::authentication_secrets;
10use super::AuthenticationSecret;
11use crate::child_block;
12use asterust::config::ConfigLine;
13
14/// The `system root-authentication` methods. Passwords and keys are read only with the `secrets` feature.
15#[derive(Clone, Debug, PartialEq, Eq)]
16#[non_exhaustive]
17pub struct RootAuthentication {
18    /// One entry per statement, e.g. `"encrypted-password"`, `"ssh-rsa"`, `"no-public-keys"`.
19    pub methods: Vec<String>,
20    /// The passwords and keys; empty unless the `secrets` feature is enabled.
21    pub secrets: Vec<AuthenticationSecret>,
22}
23
24pub(super) fn root_authentication(system: &ConfigLine) -> Option<RootAuthentication> {
25    child_block(system, "root-authentication").map(|auth| RootAuthentication {
26        methods: authentication_methods(auth),
27        secrets: authentication_secrets(auth),
28    })
29}
30
31#[cfg(test)]
32mod tests {
33    use crate::system::system;
34    use asterust::config::juniper;
35
36    #[test]
37    fn extracts_root_authentication_methods_without_secrets() {
38        let system = system(&juniper::parse_braces(
39            r#"
40system {
41    root-authentication {
42        encrypted-password "$6$salt$hash"; ## SECRET-DATA
43        ssh-rsa "ssh-rsa AAAAB3Nza root@host"; ## SECRET-DATA
44        ssh-rsa "ssh-rsa AAAAB3Nzb ops@host" {
45            from 192.0.2.0/24;
46        }
47    }
48}
49"#,
50        ))
51        .unwrap();
52        let root = system.root_authentication.unwrap();
53        assert_eq!(root.methods, ["encrypted-password", "ssh-rsa", "ssh-rsa"]);
54        let secrets: Vec<_> = root
55            .secrets
56            .iter()
57            .map(|s| (s.method.as_str(), s.value.expose()))
58            .collect();
59        let expected: &[_] = if cfg!(feature = "secrets") {
60            &[
61                ("encrypted-password", "$6$salt$hash"),
62                ("ssh-rsa", "ssh-rsa AAAAB3Nza root@host"),
63                ("ssh-rsa", "ssh-rsa AAAAB3Nzb ops@host"),
64            ]
65        } else {
66            &[]
67        };
68        assert_eq!(secrets, expected);
69    }
70
71    #[test]
72    fn root_authentication_can_disable_public_keys() {
73        let system = system(&juniper::parse_braces(
74            "system {\n    root-authentication {\n        no-public-keys;\n    }\n}\n",
75        ))
76        .unwrap();
77        assert_eq!(system.root_authentication.unwrap().methods, ["no-public-keys"]);
78    }
79
80    #[test]
81    fn no_root_authentication_is_none() {
82        let system = system(&juniper::parse_braces("system {\n    host-name pe1;\n}\n")).unwrap();
83        assert!(system.root_authentication.is_none());
84    }
85}