asterust_junos/system/syslog/
mod.rs1mod archive;
13mod file;
14mod host;
15mod user;
16
17pub use archive::SyslogArchive;
18pub use file::{SyslogStructuredData, SyslogTarget};
19pub use host::{SyslogHost, SyslogTlsDetails};
20
21use crate::{active_children, arg_of, child_block, has_leaf, named_children, value_after};
22use archive::parse_archive;
23use asterust::config::ConfigLine;
24use file::files;
25use host::parse_host;
26use user::users;
27
28#[derive(Clone, Debug, PartialEq, Eq)]
30#[non_exhaustive]
31pub struct SyslogRule {
32 pub facility: String,
34 pub severity: String,
36}
37
38#[derive(Clone, Debug, PartialEq, Eq)]
40#[non_exhaustive]
41pub struct GrpcReplay {
42 pub last_minutes: Option<String>,
44 pub facility: Option<String>,
45 pub priority: Option<String>,
46}
47
48#[derive(Clone, Debug, PartialEq, Eq)]
50#[non_exhaustive]
51pub struct Syslog {
52 pub hosts: Vec<SyslogHost>,
53 pub files: Vec<SyslogTarget>,
54 pub users: Vec<SyslogTarget>,
55 pub console: Vec<SyslogRule>,
56 pub source_address: Option<String>,
57 pub time_format: Option<String>,
59 pub archive: Option<SyslogArchive>,
61 pub allow_duplicates: bool,
62 pub log_rotate_frequency: Option<String>,
63 pub console_application_status: Option<String>,
65 pub routing_instance: Option<String>,
66 pub grpc_replay: Option<GrpcReplay>,
67 pub server_disabled_routing_instances: Vec<String>,
69}
70
71fn syslog_rules(destination: &ConfigLine) -> Vec<SyslogRule> {
74 const SEVERITIES: [&str; 10] = [
75 "any",
76 "none",
77 "emergency",
78 "alert",
79 "critical",
80 "error",
81 "warning",
82 "notice",
83 "info",
84 "debug",
85 ];
86 active_children(destination)
87 .filter_map(|c| {
88 let (facility, severity) = c.text.split_once(' ')?;
89 (facility != "application-status" && SEVERITIES.contains(&severity)).then(|| SyslogRule {
90 facility: facility.to_string(),
91 severity: severity.to_string(),
92 })
93 })
94 .collect()
95}
96
97pub(super) fn parse_syslog(syslog: &ConfigLine) -> Syslog {
98 Syslog {
99 hosts: named_children(syslog, "host")
100 .map(|(address, host)| parse_host(address, host))
101 .collect(),
102 files: files(syslog),
103 users: users(syslog),
104 console: child_block(syslog, "console").map(syslog_rules).unwrap_or_default(),
105 source_address: arg_of(syslog, "source-address").map(str::to_string),
106 time_format: arg_of(syslog, "time-format").map(str::to_string),
107 archive: parse_archive(syslog),
108 allow_duplicates: has_leaf(syslog, "allow-duplicates"),
109 log_rotate_frequency: arg_of(syslog, "log-rotate-frequency").map(str::to_string),
110 console_application_status: child_block(syslog, "console")
111 .and_then(|console| arg_of(console, "application-status"))
112 .map(str::to_string),
113 routing_instance: arg_of(syslog, "routing-instance").map(str::to_string),
114 grpc_replay: arg_of(syslog, "grpc-replay").map(|arg| {
115 let words: Vec<&str> = arg.split_whitespace().collect();
116 GrpcReplay {
117 last_minutes: value_after(&words, "last"),
118 facility: value_after(&words, "facility"),
119 priority: value_after(&words, "priority"),
120 }
121 }),
122 server_disabled_routing_instances: child_block(syslog, "server")
123 .map(|server| {
124 named_children(server, "routing-instances")
125 .filter(|(_, instance)| has_leaf(instance, "disable"))
126 .map(|(name, _)| name.to_string())
127 .collect()
128 })
129 .unwrap_or_default(),
130 }
131}
132
133#[cfg(test)]
134mod tests {
135 use crate::system::system;
136 use asterust::config::juniper;
137
138 const SYSLOG: &str = r#"
139system {
140 syslog {
141 user * {
142 any emergency;
143 }
144 host 192.0.2.5 {
145 any notice;
146 authorization info;
147 port 514;
148 source-address 192.0.2.1;
149 routing-instance mgmt_junos;
150 facility-override local3;
151 log-prefix pe1;
152 }
153 host other-routing-engine {
154 any critical;
155 }
156 file messages {
157 any notice;
158 authorization info;
159 explicit-priority;
160 }
161 file interactive-commands {
162 interactive-commands any;
163 }
164 inactive: file debug {
165 any debug;
166 }
167 console {
168 any critical;
169 }
170 source-address 192.0.2.1;
171 time-format year millisecond;
172 }
173}
174"#;
175
176 #[test]
177 fn extracts_syslog_hosts() {
178 let syslog = system(&juniper::parse_braces(SYSLOG)).unwrap().syslog.unwrap();
179 let addresses: Vec<_> = syslog.hosts.iter().map(|h| h.address.as_str()).collect();
180 assert_eq!(addresses, ["192.0.2.5", "other-routing-engine"]);
181
182 let host = &syslog.hosts[0];
183 assert_eq!(host.rules.len(), 2);
184 assert_eq!(host.rules[1].facility, "authorization");
185 assert_eq!(host.rules[1].severity, "info");
186 assert_eq!(host.port.as_deref(), Some("514"));
187 assert_eq!(host.source_address.as_deref(), Some("192.0.2.1"));
188 assert_eq!(host.routing_instance.as_deref(), Some("mgmt_junos"));
189 assert_eq!(host.facility_override.as_deref(), Some("local3"));
190 assert_eq!(host.log_prefix.as_deref(), Some("pe1"));
191 }
192
193 #[test]
194 fn extracts_syslog_files_users_and_console() {
195 let syslog = system(&juniper::parse_braces(SYSLOG)).unwrap().syslog.unwrap();
196
197 let files: Vec<_> = syslog.files.iter().map(|f| f.name.as_str()).collect();
198 assert_eq!(files, ["messages", "interactive-commands"]);
199 assert_eq!(syslog.files[0].rules.len(), 2);
201 assert_eq!(syslog.files[1].rules[0].facility, "interactive-commands");
202 assert_eq!(syslog.files[1].rules[0].severity, "any");
203
204 assert_eq!(syslog.users[0].name, "*");
205 assert_eq!(syslog.users[0].rules[0].severity, "emergency");
206 assert_eq!(syslog.console[0].facility, "any");
207 assert_eq!(syslog.console[0].severity, "critical");
208 }
209
210 #[test]
211 fn extracts_syslog_global_settings() {
212 let syslog = system(&juniper::parse_braces(SYSLOG)).unwrap().syslog.unwrap();
213 assert_eq!(syslog.source_address.as_deref(), Some("192.0.2.1"));
214 assert_eq!(syslog.time_format.as_deref(), Some("year millisecond"));
215 }
216
217 #[test]
218 fn extracts_syslog_destination_options() {
219 let syslog = system(&juniper::parse_braces(
220 r#"
221system {
222 syslog {
223 allow-duplicates;
224 log-rotate-frequency 5;
225 host 192.0.2.5 {
226 any notice;
227 allow-duplicates;
228 explicit-priority;
229 exclude-hostname;
230 match "UI_COMMIT";
231 }
232 file messages {
233 any notice;
234 allow-duplicates;
235 explicit-priority;
236 match "!(ssh)";
237 structured-data {
238 brief;
239 }
240 }
241 file plain {
242 any info;
243 structured-data;
244 }
245 user admin {
246 any alert;
247 match "commit";
248 }
249 }
250}
251"#,
252 ))
253 .unwrap()
254 .syslog
255 .unwrap();
256 assert!(syslog.allow_duplicates);
257 assert_eq!(syslog.log_rotate_frequency.as_deref(), Some("5"));
258
259 let host = &syslog.hosts[0];
260 assert!(host.allow_duplicates);
261 assert!(host.explicit_priority);
262 assert!(host.exclude_hostname);
263 assert_eq!(host.match_regex.as_deref(), Some("UI_COMMIT"));
264
265 let messages = &syslog.files[0];
266 assert!(messages.allow_duplicates);
267 assert!(messages.explicit_priority);
268 assert_eq!(messages.match_regex.as_deref(), Some("!(ssh)"));
269 assert!(messages.structured_data.as_ref().unwrap().brief);
270
271 let plain = &syslog.files[1];
272 assert!(!plain.structured_data.as_ref().unwrap().brief);
273 assert!(!plain.allow_duplicates);
274 assert_eq!(plain.match_regex, None);
275
276 assert_eq!(syslog.users[0].match_regex.as_deref(), Some("commit"));
277 }
278
279 #[test]
280 fn extracts_the_host_tls_details() {
281 let host = system(&juniper::parse_braces(
282 "system {\n syslog {\n host 192.0.2.5 {\n any notice;\n transport tls;\n tlsdetails {\n local-certificate syslog-cert;\n trusted-ca-group tcg1 {\n ca-profiles [ ca1 ca2 ];\n }\n }\n }\n }\n}\n",
283 ))
284 .unwrap()
285 .syslog
286 .unwrap()
287 .hosts
288 .remove(0);
289 let tls = host.tls_details.unwrap();
290 assert_eq!(tls.local_certificate.as_deref(), Some("syslog-cert"));
291 assert_eq!(tls.trusted_ca_group.as_deref(), Some("tcg1"));
292 assert_eq!(tls.ca_profiles, ["ca1", "ca2"]);
293 }
294
295 #[test]
296 fn extracts_grpc_replay() {
297 let replay = system(&juniper::parse_braces(
298 "system {\n syslog {\n grpc-replay last 10 facility daemon priority error;\n }\n}\n",
299 ))
300 .unwrap()
301 .syslog
302 .unwrap()
303 .grpc_replay
304 .unwrap();
305 assert_eq!(replay.last_minutes.as_deref(), Some("10"));
306 assert_eq!(replay.facility.as_deref(), Some("daemon"));
307 assert_eq!(replay.priority.as_deref(), Some("error"));
308 }
309
310 #[test]
311 fn extracts_console_application_status_and_server_settings() {
312 let syslog = system(&juniper::parse_braces(
313 r#"
314system {
315 syslog {
316 routing-instance mgmt_junos;
317 console {
318 any critical;
319 application-status error;
320 }
321 server {
322 routing-instances all {
323 disable;
324 }
325 routing-instances blue;
326 }
327 }
328}
329"#,
330 ))
331 .unwrap()
332 .syslog
333 .unwrap();
334 assert_eq!(syslog.routing_instance.as_deref(), Some("mgmt_junos"));
335 assert_eq!(syslog.console_application_status.as_deref(), Some("error"));
336 assert_eq!(syslog.console.len(), 1);
338 assert_eq!(syslog.server_disabled_routing_instances, ["all"]);
339 }
340
341 #[test]
342 fn extracts_match_strings_and_transport() {
343 let syslog = system(&juniper::parse_braces(
344 r#"
345system {
346 syslog {
347 host 192.0.2.5 {
348 any notice;
349 transport tls;
350 match-strings [ UI_COMMIT SNMP_TRAP ];
351 }
352 file messages {
353 any notice;
354 match-strings LINK_DOWN;
355 }
356 }
357}
358"#,
359 ))
360 .unwrap()
361 .syslog
362 .unwrap();
363 assert_eq!(syslog.hosts[0].transport.as_deref(), Some("tls"));
364 assert_eq!(syslog.hosts[0].match_strings, ["UI_COMMIT", "SNMP_TRAP"]);
365 assert_eq!(syslog.files[0].match_strings, ["LINK_DOWN"]);
366 assert_eq!(syslog.files[0].match_regex, None);
367 }
368
369 #[test]
370 fn syslog_destination_options_default_to_off() {
371 let syslog = system(&juniper::parse_braces(
372 "system {\n syslog {\n host 192.0.2.5 {\n any notice;\n }\n }\n}\n",
373 ))
374 .unwrap()
375 .syslog
376 .unwrap();
377 assert!(!syslog.allow_duplicates);
378 assert_eq!(syslog.log_rotate_frequency, None);
379 assert!(!syslog.hosts[0].explicit_priority);
380 assert_eq!(syslog.hosts[0].match_regex, None);
381 }
382
383 #[test]
384 fn no_syslog_is_none() {
385 assert!(system(&juniper::parse_braces("system {\n host-name pe1;\n}\n"))
386 .unwrap()
387 .syslog
388 .is_none());
389 }
390}