Skip to main content

asterust_junos/system/
auth.rs

1//! Authentication settings under `system`: root, RADIUS and TACACS+.
2
3use crate::{active_children, arg_of, child_block, has_leaf, split_name, unquote};
4use asterust::config::ConfigLine;
5
6/// The `system root-authentication` methods. Passwords and keys are not extracted.
7#[derive(Clone, Debug, PartialEq, Eq)]
8#[non_exhaustive]
9pub struct RootAuthentication {
10    /// One entry per statement, e.g. `"encrypted-password"`, `"ssh-rsa"`, `"no-public-keys"`.
11    pub methods: Vec<String>,
12}
13
14/// A `radius-server <address>`. The `secret` is not extracted.
15#[derive(Clone, Debug, PartialEq, Eq)]
16#[non_exhaustive]
17pub struct RadiusServer {
18    pub address: String,
19    pub port: Option<String>,
20    pub accounting_port: Option<String>,
21    pub retry: Option<String>,
22    pub timeout: Option<String>,
23    pub source_address: Option<String>,
24    pub routing_instance: Option<String>,
25}
26
27/// A `tacplus-server <address>`. The `secret` is not extracted.
28#[derive(Clone, Debug, PartialEq, Eq)]
29#[non_exhaustive]
30pub struct TacplusServer {
31    pub address: String,
32    pub port: Option<String>,
33    pub timeout: Option<String>,
34    pub source_address: Option<String>,
35    pub routing_instance: Option<String>,
36    pub single_connection: bool,
37}
38
39/// Servers written as `<keyword> <address> { ... }`, or as `<keyword> { <address> { ... } }`, with their option lines.
40fn servers<'a>(system: &'a ConfigLine, keyword: &str) -> Vec<(&'a str, &'a ConfigLine)> {
41    let mut found = Vec::new();
42    for child in active_children(system) {
43        if child.text == keyword {
44            found.extend(active_children(child).map(|server| (unquote(&server.text), server)));
45        } else if let Some(arg) = child.arg(keyword) {
46            found.push((split_name(arg).0, child));
47        }
48    }
49    found
50}
51
52/// The servers under `parent` written as `<keyword> ...`: `radius-server` in `system`, `server` in accounting.
53pub(super) fn radius_servers(parent: &ConfigLine, keyword: &str) -> Vec<RadiusServer> {
54    servers(parent, keyword)
55        .into_iter()
56        .map(|(address, server)| RadiusServer {
57            address: address.to_string(),
58            port: arg_of(server, "port").map(str::to_string),
59            accounting_port: arg_of(server, "accounting-port").map(str::to_string),
60            retry: arg_of(server, "retry").map(str::to_string),
61            timeout: arg_of(server, "timeout").map(str::to_string),
62            source_address: arg_of(server, "source-address").map(str::to_string),
63            routing_instance: arg_of(server, "routing-instance").map(str::to_string),
64        })
65        .collect()
66}
67
68pub(super) fn tacplus_servers(parent: &ConfigLine, keyword: &str) -> Vec<TacplusServer> {
69    servers(parent, keyword)
70        .into_iter()
71        .map(|(address, server)| TacplusServer {
72            address: address.to_string(),
73            port: arg_of(server, "port").map(str::to_string),
74            timeout: arg_of(server, "timeout").map(str::to_string),
75            source_address: arg_of(server, "source-address").map(str::to_string),
76            routing_instance: arg_of(server, "routing-instance").map(str::to_string),
77            single_connection: has_leaf(server, "single-connection"),
78        })
79        .collect()
80}
81
82pub(super) fn root_authentication(system: &ConfigLine) -> Option<RootAuthentication> {
83    child_block(system, "root-authentication").map(|auth| RootAuthentication {
84        methods: authentication_methods(auth),
85    })
86}
87
88/// The name of each statement in an `authentication` block. Secrets can carry a trailing `## SECRET-DATA` marker,
89/// so only the first word is kept.
90pub(super) fn authentication_methods(auth: &ConfigLine) -> Vec<String> {
91    active_children(auth)
92        .filter_map(|c| c.text.split_whitespace().next())
93        .map(str::to_string)
94        .collect()
95}
96
97#[cfg(test)]
98mod tests {
99    use crate::system::system;
100    use asterust::config::juniper;
101
102    #[test]
103    fn extracts_root_authentication_methods_without_secrets() {
104        let system = system(&juniper::parse_braces(
105            r#"
106system {
107    root-authentication {
108        encrypted-password "$6$salt$hash"; ## SECRET-DATA
109        ssh-rsa "ssh-rsa AAAAB3Nza root@host"; ## SECRET-DATA
110        ssh-rsa "ssh-rsa AAAAB3Nzb ops@host" {
111            from 192.0.2.0/24;
112        }
113    }
114}
115"#,
116        ))
117        .unwrap();
118        let root = system.root_authentication.unwrap();
119        assert_eq!(root.methods, ["encrypted-password", "ssh-rsa", "ssh-rsa"]);
120    }
121
122    #[test]
123    fn root_authentication_can_disable_public_keys() {
124        let system = system(&juniper::parse_braces(
125            "system {\n    root-authentication {\n        no-public-keys;\n    }\n}\n",
126        ))
127        .unwrap();
128        assert_eq!(system.root_authentication.unwrap().methods, ["no-public-keys"]);
129    }
130
131    #[test]
132    fn no_root_authentication_is_none() {
133        let system = system(&juniper::parse_braces("system {\n    host-name pe1;\n}\n")).unwrap();
134        assert!(system.root_authentication.is_none());
135    }
136
137    #[test]
138    fn extracts_authentication_order() {
139        let system = system(&juniper::parse_braces(
140            "system {\n    authentication-order [ tacplus radius password ];\n}\n",
141        ))
142        .unwrap();
143        assert_eq!(system.authentication_order, ["tacplus", "radius", "password"]);
144    }
145
146    #[test]
147    fn extracts_radius_servers_without_secrets() {
148        let system = system(&juniper::parse_braces(
149            r#"
150system {
151    radius-server 192.0.2.10 {
152        secret "$9$secret"; ## SECRET-DATA
153        port 1812;
154        accounting-port 1813;
155        retry 5;
156        timeout 10;
157        source-address 192.0.2.1;
158        routing-instance mgmt_junos;
159    }
160    radius-server 192.0.2.11 {
161        secret "$9$other"; ## SECRET-DATA
162    }
163}
164"#,
165        ))
166        .unwrap();
167        assert_eq!(system.radius_servers.len(), 2);
168
169        let first = &system.radius_servers[0];
170        assert_eq!(first.address, "192.0.2.10");
171        assert_eq!(first.port.as_deref(), Some("1812"));
172        assert_eq!(first.accounting_port.as_deref(), Some("1813"));
173        assert_eq!(first.retry.as_deref(), Some("5"));
174        assert_eq!(first.timeout.as_deref(), Some("10"));
175        assert_eq!(first.source_address.as_deref(), Some("192.0.2.1"));
176        assert_eq!(first.routing_instance.as_deref(), Some("mgmt_junos"));
177
178        assert_eq!(system.radius_servers[1].address, "192.0.2.11");
179        assert_eq!(system.radius_servers[1].port, None);
180    }
181
182    #[test]
183    fn extracts_radius_servers_in_nested_block_form() {
184        let system = system(&juniper::parse_braces(
185            "system {\n    radius-server {\n        192.0.2.10 {\n            port 1645;\n        }\n    }\n}\n",
186        ))
187        .unwrap();
188        assert_eq!(system.radius_servers[0].address, "192.0.2.10");
189        assert_eq!(system.radius_servers[0].port.as_deref(), Some("1645"));
190    }
191
192    #[test]
193    fn extracts_tacplus_servers_without_secrets() {
194        let system = system(&juniper::parse_braces(
195            r#"
196system {
197    tacplus-server 192.0.2.20 {
198        port 49;
199        secret "$9$secret"; ## SECRET-DATA
200        single-connection;
201        source-address 192.0.2.1;
202        timeout 5;
203    }
204    inactive: tacplus-server 192.0.2.21 {
205        secret "$9$other"; ## SECRET-DATA
206    }
207}
208"#,
209        ))
210        .unwrap();
211        assert_eq!(system.tacplus_servers.len(), 1);
212        let server = &system.tacplus_servers[0];
213        assert_eq!(server.address, "192.0.2.20");
214        assert_eq!(server.port.as_deref(), Some("49"));
215        assert_eq!(server.timeout.as_deref(), Some("5"));
216        assert_eq!(server.source_address.as_deref(), Some("192.0.2.1"));
217        assert!(server.single_connection);
218        assert_eq!(server.routing_instance, None);
219    }
220
221    #[test]
222    fn authentication_settings_are_optional() {
223        let system = system(&juniper::parse_braces("system {\n    host-name pe1;\n}\n")).unwrap();
224        assert!(system.authentication_order.is_empty());
225        assert!(system.radius_servers.is_empty());
226        assert!(system.tacplus_servers.is_empty());
227    }
228}