asterust_junos/system/
root_authentication.rs1use super::authentication_methods;
9use super::authentication_secret::authentication_secrets;
10use super::AuthenticationSecret;
11use crate::child_block;
12use asterust::config::ConfigLine;
13
14#[derive(Clone, Debug, PartialEq, Eq)]
16#[non_exhaustive]
17pub struct RootAuthentication {
18 pub methods: Vec<String>,
20 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}