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