1use crate::{active_children, arg_of, args_of, child_block, has_leaf, list_values, named_children};
4use asterust::config::ConfigLine;
5
6#[derive(Clone, Debug, Default, PartialEq, Eq)]
8#[non_exhaustive]
9pub struct SshService {
10 pub root_login: Option<String>,
12 pub protocol_versions: Vec<String>,
13 pub port: Option<String>,
14 pub connection_limit: Option<String>,
15 pub rate_limit: Option<String>,
16 pub ciphers: Vec<String>,
17 pub sftp_server: bool,
18}
19
20#[derive(Clone, Debug, PartialEq, Eq)]
22#[non_exhaustive]
23pub struct OutboundSshServer {
24 pub address: String,
25 pub port: Option<String>,
26 pub retry: Option<String>,
27 pub timeout: Option<String>,
28}
29
30#[derive(Clone, Debug, PartialEq, Eq)]
32#[non_exhaustive]
33pub struct OutboundSshClient {
34 pub id: String,
35 pub device_id: Option<String>,
36 pub servers: Vec<OutboundSshServer>,
37 pub services: Vec<String>,
39 pub reconnect_strategy: Option<String>,
41 pub routing_instance: Option<String>,
42}
43
44#[derive(Clone, Debug, PartialEq, Eq)]
46#[non_exhaustive]
47pub struct Services {
48 pub ssh: Option<SshService>,
49 pub telnet: bool,
50 pub ftp: bool,
51 pub netconf_ssh: bool,
53 pub http: bool,
55 pub https: bool,
57 pub outbound_ssh: Vec<OutboundSshClient>,
58}
59
60pub(super) fn parse_services(services: &ConfigLine) -> Services {
61 let web = child_block(services, "web-management");
62 let netconf = child_block(services, "netconf");
63 Services {
64 ssh: child_block(services, "ssh").map(parse_ssh),
65 telnet: child_block(services, "telnet").is_some(),
66 ftp: child_block(services, "ftp").is_some(),
67 netconf_ssh: netconf.is_some_and(|n| child_block(n, "ssh").is_some()),
68 http: web.is_some_and(|w| child_block(w, "http").is_some()),
69 https: web.is_some_and(|w| child_block(w, "https").is_some()),
70 outbound_ssh: child_block(services, "outbound-ssh")
71 .map(|outbound| {
72 named_children(outbound, "client")
73 .map(|(id, client)| parse_outbound_client(id, client))
74 .collect()
75 })
76 .unwrap_or_default(),
77 }
78}
79
80fn parse_outbound_client(id: &str, client: &ConfigLine) -> OutboundSshClient {
81 OutboundSshClient {
82 id: id.to_string(),
83 device_id: arg_of(client, "device-id").map(str::to_string),
84 servers: active_children(client)
85 .filter(|c| !c.text.contains(' ') && !matches!(c.text.as_str(), "keep-alive" | "proxy-server"))
87 .map(|server| OutboundSshServer {
88 address: server.text.clone(),
89 port: arg_of(server, "port").map(str::to_string),
90 retry: arg_of(server, "retry").map(str::to_string),
91 timeout: arg_of(server, "timeout").map(str::to_string),
92 })
93 .collect(),
94 services: args_of(client, "services").into_iter().flat_map(list_values).collect(),
95 reconnect_strategy: arg_of(client, "reconnect-strategy").map(str::to_string),
96 routing_instance: arg_of(client, "routing-instance").map(str::to_string),
97 }
98}
99
100fn parse_ssh(ssh: &ConfigLine) -> SshService {
101 let list = |keyword: &str| args_of(ssh, keyword).into_iter().flat_map(list_values).collect();
102 SshService {
103 root_login: arg_of(ssh, "root-login").map(str::to_string),
104 protocol_versions: list("protocol-version"),
105 port: arg_of(ssh, "port").map(str::to_string),
106 connection_limit: arg_of(ssh, "connection-limit").map(str::to_string),
107 rate_limit: arg_of(ssh, "rate-limit").map(str::to_string),
108 ciphers: list("ciphers"),
109 sftp_server: has_leaf(ssh, "sftp-server"),
110 }
111}
112
113#[cfg(test)]
114mod tests {
115 use super::*;
116 use crate::system::system;
117 use asterust::config::juniper;
118
119 const SERVICES: &str = r#"
120system {
121 services {
122 ssh {
123 root-login deny;
124 protocol-version v2;
125 port 2222;
126 connection-limit 10;
127 rate-limit 5;
128 ciphers [ aes256-ctr aes128-ctr ];
129 sftp-server;
130 }
131 netconf {
132 ssh;
133 }
134 web-management {
135 https {
136 system-generated-certificate;
137 }
138 }
139 telnet;
140 }
141}
142"#;
143
144 #[test]
145 fn extracts_ssh_service_settings() {
146 let services = system(&juniper::parse_braces(SERVICES)).unwrap().services.unwrap();
147 let ssh = services.ssh.unwrap();
148 assert_eq!(ssh.root_login.as_deref(), Some("deny"));
149 assert_eq!(ssh.protocol_versions, ["v2"]);
150 assert_eq!(ssh.port.as_deref(), Some("2222"));
151 assert_eq!(ssh.connection_limit.as_deref(), Some("10"));
152 assert_eq!(ssh.rate_limit.as_deref(), Some("5"));
153 assert_eq!(ssh.ciphers, ["aes256-ctr", "aes128-ctr"]);
154 assert!(ssh.sftp_server);
155 }
156
157 #[test]
158 fn extracts_enabled_services() {
159 let services = system(&juniper::parse_braces(SERVICES)).unwrap().services.unwrap();
160 assert!(services.telnet);
161 assert!(services.netconf_ssh);
162 assert!(services.https);
163 assert!(!services.http);
164 assert!(!services.ftp);
165 }
166
167 #[test]
168 fn bare_service_statements_enable_with_defaults() {
169 let services = system(&juniper::parse_braces(
170 "system {\n services {\n ssh;\n ftp;\n web-management {\n http;\n }\n }\n}\n",
171 ))
172 .unwrap()
173 .services
174 .unwrap();
175 assert_eq!(services.ssh, Some(SshService::default()));
176 assert!(services.ftp);
177 assert!(services.http);
178 assert!(!services.telnet);
179 assert!(!services.netconf_ssh);
180 }
181
182 #[test]
183 fn disabled_service_is_not_reported() {
184 let services = system(&juniper::parse_braces(
185 "system {\n services {\n inactive: telnet;\n ssh;\n }\n}\n",
186 ))
187 .unwrap()
188 .services
189 .unwrap();
190 assert!(!services.telnet);
191 }
192
193 #[test]
194 fn extracts_outbound_ssh_clients() {
195 let services = system(&juniper::parse_braces(
196 r#"
197system {
198 services {
199 outbound-ssh {
200 client mgmt {
201 device-id pe1;
202 secret "$9$secret"; ## SECRET-DATA
203 reconnect-strategy sticky;
204 routing-instance mgmt_junos;
205 services netconf;
206 192.0.2.50 {
207 port 7804;
208 retry 5;
209 timeout 20;
210 }
211 }
212 client backup {
213 192.0.2.60;
214 keep-alive {
215 retry 3;
216 timeout 15;
217 }
218 }
219 }
220 }
221}
222"#,
223 ))
224 .unwrap()
225 .services
226 .unwrap();
227 assert_eq!(services.outbound_ssh.len(), 2);
228
229 let mgmt = &services.outbound_ssh[0];
230 assert_eq!(mgmt.id, "mgmt");
231 assert_eq!(mgmt.device_id.as_deref(), Some("pe1"));
232 assert_eq!(mgmt.reconnect_strategy.as_deref(), Some("sticky"));
233 assert_eq!(mgmt.routing_instance.as_deref(), Some("mgmt_junos"));
234 assert_eq!(mgmt.services, ["netconf"]);
235 assert_eq!(mgmt.servers.len(), 1);
236 assert_eq!(mgmt.servers[0].address, "192.0.2.50");
237 assert_eq!(mgmt.servers[0].port.as_deref(), Some("7804"));
238 assert_eq!(mgmt.servers[0].retry.as_deref(), Some("5"));
239 assert_eq!(mgmt.servers[0].timeout.as_deref(), Some("20"));
240
241 let backup = &services.outbound_ssh[1];
242 assert_eq!(backup.id, "backup");
243 assert_eq!(backup.servers.len(), 1);
245 assert_eq!(backup.servers[0].address, "192.0.2.60");
246 assert_eq!(backup.servers[0].port, None);
247 }
248
249 #[test]
250 fn no_outbound_ssh_is_empty() {
251 let services = system(&juniper::parse_braces(
252 "system {\n services {\n ssh;\n }\n}\n",
253 ))
254 .unwrap()
255 .services
256 .unwrap();
257 assert!(services.outbound_ssh.is_empty());
258 }
259
260 #[test]
261 fn no_services_is_none() {
262 assert!(system(&juniper::parse_braces("system {\n host-name pe1;\n}\n"))
263 .unwrap()
264 .services
265 .is_none());
266 }
267}