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 crate::child_block;
10use asterust::config::ConfigLine;
11
12/// The `system root-authentication` methods. Passwords and keys are not extracted.
13#[derive(Clone, Debug, PartialEq, Eq)]
14#[non_exhaustive]
15pub struct RootAuthentication {
16    /// One entry per statement, e.g. `"encrypted-password"`, `"ssh-rsa"`, `"no-public-keys"`.
17    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}