Skip to main content

asterust_junos/system/
login.rs

1//! `system { login { ... } }`.
2
3use super::auth::authentication_methods;
4use crate::{arg_of, args_of, child_block, list_values, named_children};
5use asterust::config::ConfigLine;
6
7/// A login `class`.
8#[derive(Clone, Debug, PartialEq, Eq)]
9#[non_exhaustive]
10pub struct LoginClass {
11    pub name: String,
12    pub permissions: Vec<String>,
13    /// Minutes.
14    pub idle_timeout: Option<String>,
15    /// The regular expressions as written, e.g. `"(show)|(ping)"`.
16    pub allow_commands: Option<String>,
17    pub deny_commands: Option<String>,
18    pub allow_configuration: Option<String>,
19    pub deny_configuration: Option<String>,
20}
21
22/// A login `user`. Passwords and keys are not extracted, only which authentication methods are present.
23#[derive(Clone, Debug, PartialEq, Eq)]
24#[non_exhaustive]
25pub struct LoginUser {
26    pub name: String,
27    pub class: Option<String>,
28    pub uid: Option<String>,
29    pub full_name: Option<String>,
30    /// One entry per authentication statement, e.g. `"encrypted-password"`, `"ssh-rsa"`.
31    pub authentication_methods: Vec<String>,
32}
33
34/// The `system login` configuration.
35#[derive(Clone, Debug, PartialEq, Eq)]
36#[non_exhaustive]
37pub struct Login {
38    /// The banner shown before login.
39    pub message: Option<String>,
40    /// The text shown after login.
41    pub announcement: Option<String>,
42    pub classes: Vec<LoginClass>,
43    pub users: Vec<LoginUser>,
44}
45
46pub(super) fn parse_login(login: &ConfigLine) -> Login {
47    Login {
48        message: arg_of(login, "message").map(str::to_string),
49        announcement: arg_of(login, "announcement").map(str::to_string),
50        classes: named_children(login, "class")
51            .map(|(name, class)| LoginClass {
52                name: name.to_string(),
53                permissions: args_of(class, "permissions")
54                    .into_iter()
55                    .flat_map(list_values)
56                    .collect(),
57                idle_timeout: arg_of(class, "idle-timeout").map(str::to_string),
58                allow_commands: arg_of(class, "allow-commands").map(str::to_string),
59                deny_commands: arg_of(class, "deny-commands").map(str::to_string),
60                allow_configuration: arg_of(class, "allow-configuration").map(str::to_string),
61                deny_configuration: arg_of(class, "deny-configuration").map(str::to_string),
62            })
63            .collect(),
64        users: named_children(login, "user")
65            .map(|(name, user)| LoginUser {
66                name: name.to_string(),
67                class: arg_of(user, "class").map(str::to_string),
68                uid: arg_of(user, "uid").map(str::to_string),
69                full_name: arg_of(user, "full-name").map(str::to_string),
70                authentication_methods: child_block(user, "authentication")
71                    .map(authentication_methods)
72                    .unwrap_or_default(),
73            })
74            .collect(),
75    }
76}
77
78#[cfg(test)]
79mod tests {
80    use crate::system::system;
81    use asterust::config::juniper;
82
83    const LOGIN: &str = r#"
84system {
85    login {
86        message "Authorized access only";
87        announcement "Maintenance window Sunday";
88        class NOC {
89            permissions [ view view-configuration ];
90            idle-timeout 15;
91            allow-commands "(ping)|(traceroute)";
92            deny-configuration ".*";
93        }
94        class OPS {
95            permissions all;
96            deny-commands "(request system)";
97            allow-configuration "(interfaces)|(protocols)";
98        }
99        user admin {
100            uid 2000;
101            class super-user;
102            full-name "Network Admin";
103            authentication {
104                encrypted-password "$6$salt$hash"; ## SECRET-DATA
105                ssh-ed25519 "ssh-ed25519 AAAAC3Nza admin@host"; ## SECRET-DATA
106            }
107        }
108        user monitor {
109            class NOC;
110        }
111    }
112}
113"#;
114
115    #[test]
116    fn extracts_login_banner_and_classes() {
117        let login = system(&juniper::parse_braces(LOGIN)).unwrap().login.unwrap();
118        assert_eq!(login.message.as_deref(), Some("Authorized access only"));
119        assert_eq!(login.announcement.as_deref(), Some("Maintenance window Sunday"));
120
121        assert_eq!(login.classes.len(), 2);
122        let noc = &login.classes[0];
123        assert_eq!(noc.name, "NOC");
124        assert_eq!(noc.permissions, ["view", "view-configuration"]);
125        assert_eq!(noc.idle_timeout.as_deref(), Some("15"));
126        assert_eq!(noc.allow_commands.as_deref(), Some("(ping)|(traceroute)"));
127        assert_eq!(noc.deny_configuration.as_deref(), Some(".*"));
128        assert_eq!(noc.deny_commands, None);
129
130        let ops = &login.classes[1];
131        assert_eq!(ops.permissions, ["all"]);
132        assert_eq!(ops.deny_commands.as_deref(), Some("(request system)"));
133        assert_eq!(ops.allow_configuration.as_deref(), Some("(interfaces)|(protocols)"));
134    }
135
136    #[test]
137    fn extracts_login_users_without_secrets() {
138        let login = system(&juniper::parse_braces(LOGIN)).unwrap().login.unwrap();
139        assert_eq!(login.users.len(), 2);
140
141        let admin = &login.users[0];
142        assert_eq!(admin.name, "admin");
143        assert_eq!(admin.class.as_deref(), Some("super-user"));
144        assert_eq!(admin.uid.as_deref(), Some("2000"));
145        assert_eq!(admin.full_name.as_deref(), Some("Network Admin"));
146        assert_eq!(admin.authentication_methods, ["encrypted-password", "ssh-ed25519"]);
147
148        let monitor = &login.users[1];
149        assert_eq!(monitor.class.as_deref(), Some("NOC"));
150        assert!(monitor.authentication_methods.is_empty());
151    }
152
153    #[test]
154    fn no_login_is_none() {
155        assert!(system(&juniper::parse_braces("system {\n    host-name pe1;\n}\n"))
156            .unwrap()
157            .login
158            .is_none());
159    }
160}