1use crate::{active_children, arg_of, child_block, has_leaf, split_name, unquote};
4use asterust::config::ConfigLine;
5
6#[derive(Clone, Debug, PartialEq, Eq)]
8#[non_exhaustive]
9pub struct RootAuthentication {
10 pub methods: Vec<String>,
12}
13
14#[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#[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
39fn 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
52pub(super) fn radius_servers(system: &ConfigLine) -> Vec<RadiusServer> {
53 servers(system, "radius-server")
54 .into_iter()
55 .map(|(address, server)| RadiusServer {
56 address: address.to_string(),
57 port: arg_of(server, "port").map(str::to_string),
58 accounting_port: arg_of(server, "accounting-port").map(str::to_string),
59 retry: arg_of(server, "retry").map(str::to_string),
60 timeout: arg_of(server, "timeout").map(str::to_string),
61 source_address: arg_of(server, "source-address").map(str::to_string),
62 routing_instance: arg_of(server, "routing-instance").map(str::to_string),
63 })
64 .collect()
65}
66
67pub(super) fn tacplus_servers(system: &ConfigLine) -> Vec<TacplusServer> {
68 servers(system, "tacplus-server")
69 .into_iter()
70 .map(|(address, server)| TacplusServer {
71 address: address.to_string(),
72 port: arg_of(server, "port").map(str::to_string),
73 timeout: arg_of(server, "timeout").map(str::to_string),
74 source_address: arg_of(server, "source-address").map(str::to_string),
75 routing_instance: arg_of(server, "routing-instance").map(str::to_string),
76 single_connection: has_leaf(server, "single-connection"),
77 })
78 .collect()
79}
80
81pub(super) fn root_authentication(system: &ConfigLine) -> Option<RootAuthentication> {
82 child_block(system, "root-authentication").map(|auth| RootAuthentication {
83 methods: authentication_methods(auth),
84 })
85}
86
87pub(super) fn authentication_methods(auth: &ConfigLine) -> Vec<String> {
90 active_children(auth)
91 .filter_map(|c| c.text.split_whitespace().next())
92 .map(str::to_string)
93 .collect()
94}
95
96#[cfg(test)]
97mod tests {
98 use crate::system::system;
99 use asterust::config::juniper;
100
101 #[test]
102 fn extracts_root_authentication_methods_without_secrets() {
103 let system = system(&juniper::parse_braces(
104 r#"
105system {
106 root-authentication {
107 encrypted-password "$6$salt$hash"; ## SECRET-DATA
108 ssh-rsa "ssh-rsa AAAAB3Nza root@host"; ## SECRET-DATA
109 ssh-rsa "ssh-rsa AAAAB3Nzb ops@host" {
110 from 192.0.2.0/24;
111 }
112 }
113}
114"#,
115 ))
116 .unwrap();
117 let root = system.root_authentication.unwrap();
118 assert_eq!(root.methods, ["encrypted-password", "ssh-rsa", "ssh-rsa"]);
119 }
120
121 #[test]
122 fn root_authentication_can_disable_public_keys() {
123 let system = system(&juniper::parse_braces(
124 "system {\n root-authentication {\n no-public-keys;\n }\n}\n",
125 ))
126 .unwrap();
127 assert_eq!(system.root_authentication.unwrap().methods, ["no-public-keys"]);
128 }
129
130 #[test]
131 fn no_root_authentication_is_none() {
132 let system = system(&juniper::parse_braces("system {\n host-name pe1;\n}\n")).unwrap();
133 assert!(system.root_authentication.is_none());
134 }
135
136 #[test]
137 fn extracts_authentication_order() {
138 let system = system(&juniper::parse_braces(
139 "system {\n authentication-order [ tacplus radius password ];\n}\n",
140 ))
141 .unwrap();
142 assert_eq!(system.authentication_order, ["tacplus", "radius", "password"]);
143 }
144
145 #[test]
146 fn extracts_radius_servers_without_secrets() {
147 let system = system(&juniper::parse_braces(
148 r#"
149system {
150 radius-server 192.0.2.10 {
151 secret "$9$secret"; ## SECRET-DATA
152 port 1812;
153 accounting-port 1813;
154 retry 5;
155 timeout 10;
156 source-address 192.0.2.1;
157 routing-instance mgmt_junos;
158 }
159 radius-server 192.0.2.11 {
160 secret "$9$other"; ## SECRET-DATA
161 }
162}
163"#,
164 ))
165 .unwrap();
166 assert_eq!(system.radius_servers.len(), 2);
167
168 let first = &system.radius_servers[0];
169 assert_eq!(first.address, "192.0.2.10");
170 assert_eq!(first.port.as_deref(), Some("1812"));
171 assert_eq!(first.accounting_port.as_deref(), Some("1813"));
172 assert_eq!(first.retry.as_deref(), Some("5"));
173 assert_eq!(first.timeout.as_deref(), Some("10"));
174 assert_eq!(first.source_address.as_deref(), Some("192.0.2.1"));
175 assert_eq!(first.routing_instance.as_deref(), Some("mgmt_junos"));
176
177 assert_eq!(system.radius_servers[1].address, "192.0.2.11");
178 assert_eq!(system.radius_servers[1].port, None);
179 }
180
181 #[test]
182 fn extracts_radius_servers_in_nested_block_form() {
183 let system = system(&juniper::parse_braces(
184 "system {\n radius-server {\n 192.0.2.10 {\n port 1645;\n }\n }\n}\n",
185 ))
186 .unwrap();
187 assert_eq!(system.radius_servers[0].address, "192.0.2.10");
188 assert_eq!(system.radius_servers[0].port.as_deref(), Some("1645"));
189 }
190
191 #[test]
192 fn extracts_tacplus_servers_without_secrets() {
193 let system = system(&juniper::parse_braces(
194 r#"
195system {
196 tacplus-server 192.0.2.20 {
197 port 49;
198 secret "$9$secret"; ## SECRET-DATA
199 single-connection;
200 source-address 192.0.2.1;
201 timeout 5;
202 }
203 inactive: tacplus-server 192.0.2.21 {
204 secret "$9$other"; ## SECRET-DATA
205 }
206}
207"#,
208 ))
209 .unwrap();
210 assert_eq!(system.tacplus_servers.len(), 1);
211 let server = &system.tacplus_servers[0];
212 assert_eq!(server.address, "192.0.2.20");
213 assert_eq!(server.port.as_deref(), Some("49"));
214 assert_eq!(server.timeout.as_deref(), Some("5"));
215 assert_eq!(server.source_address.as_deref(), Some("192.0.2.1"));
216 assert!(server.single_connection);
217 assert_eq!(server.routing_instance, None);
218 }
219
220 #[test]
221 fn authentication_settings_are_optional() {
222 let system = system(&juniper::parse_braces("system {\n host-name pe1;\n}\n")).unwrap();
223 assert!(system.authentication_order.is_empty());
224 assert!(system.radius_servers.is_empty());
225 assert!(system.tacplus_servers.is_empty());
226 }
227}