asterust_junos/system/syslog/
mod.rs1mod archive;
4mod file;
5mod host;
6mod user;
7
8pub use archive::SyslogArchive;
9pub use file::{SyslogStructuredData, SyslogTarget};
10pub use host::SyslogHost;
11
12use crate::{active_children, arg_of, child_block, has_leaf, named_children};
13use archive::parse_archive;
14use asterust::config::ConfigLine;
15use file::files;
16use host::parse_host;
17use user::users;
18
19#[derive(Clone, Debug, PartialEq, Eq)]
21#[non_exhaustive]
22pub struct SyslogRule {
23 pub facility: String,
25 pub severity: String,
27}
28
29#[derive(Clone, Debug, PartialEq, Eq)]
31#[non_exhaustive]
32pub struct Syslog {
33 pub hosts: Vec<SyslogHost>,
34 pub files: Vec<SyslogTarget>,
35 pub users: Vec<SyslogTarget>,
36 pub console: Vec<SyslogRule>,
37 pub source_address: Option<String>,
38 pub time_format: Option<String>,
40 pub archive: Option<SyslogArchive>,
42 pub allow_duplicates: bool,
43 pub log_rotate_frequency: Option<String>,
44}
45
46fn syslog_rules(destination: &ConfigLine) -> Vec<SyslogRule> {
48 const SEVERITIES: [&str; 10] = [
49 "any",
50 "none",
51 "emergency",
52 "alert",
53 "critical",
54 "error",
55 "warning",
56 "notice",
57 "info",
58 "debug",
59 ];
60 active_children(destination)
61 .filter_map(|c| {
62 let (facility, severity) = c.text.split_once(' ')?;
63 SEVERITIES.contains(&severity).then(|| SyslogRule {
64 facility: facility.to_string(),
65 severity: severity.to_string(),
66 })
67 })
68 .collect()
69}
70
71pub(super) fn parse_syslog(syslog: &ConfigLine) -> Syslog {
72 Syslog {
73 hosts: named_children(syslog, "host")
74 .map(|(address, host)| parse_host(address, host))
75 .collect(),
76 files: files(syslog),
77 users: users(syslog),
78 console: child_block(syslog, "console").map(syslog_rules).unwrap_or_default(),
79 source_address: arg_of(syslog, "source-address").map(str::to_string),
80 time_format: arg_of(syslog, "time-format").map(str::to_string),
81 archive: parse_archive(syslog),
82 allow_duplicates: has_leaf(syslog, "allow-duplicates"),
83 log_rotate_frequency: arg_of(syslog, "log-rotate-frequency").map(str::to_string),
84 }
85}
86
87#[cfg(test)]
88mod tests {
89 use crate::system::system;
90 use asterust::config::juniper;
91
92 const SYSLOG: &str = r#"
93system {
94 syslog {
95 user * {
96 any emergency;
97 }
98 host 192.0.2.5 {
99 any notice;
100 authorization info;
101 port 514;
102 source-address 192.0.2.1;
103 routing-instance mgmt_junos;
104 facility-override local3;
105 log-prefix pe1;
106 }
107 host other-routing-engine {
108 any critical;
109 }
110 file messages {
111 any notice;
112 authorization info;
113 explicit-priority;
114 }
115 file interactive-commands {
116 interactive-commands any;
117 }
118 inactive: file debug {
119 any debug;
120 }
121 console {
122 any critical;
123 }
124 source-address 192.0.2.1;
125 time-format year millisecond;
126 }
127}
128"#;
129
130 #[test]
131 fn extracts_syslog_hosts() {
132 let syslog = system(&juniper::parse_braces(SYSLOG)).unwrap().syslog.unwrap();
133 let addresses: Vec<_> = syslog.hosts.iter().map(|h| h.address.as_str()).collect();
134 assert_eq!(addresses, ["192.0.2.5", "other-routing-engine"]);
135
136 let host = &syslog.hosts[0];
137 assert_eq!(host.rules.len(), 2);
138 assert_eq!(host.rules[1].facility, "authorization");
139 assert_eq!(host.rules[1].severity, "info");
140 assert_eq!(host.port.as_deref(), Some("514"));
141 assert_eq!(host.source_address.as_deref(), Some("192.0.2.1"));
142 assert_eq!(host.routing_instance.as_deref(), Some("mgmt_junos"));
143 assert_eq!(host.facility_override.as_deref(), Some("local3"));
144 assert_eq!(host.log_prefix.as_deref(), Some("pe1"));
145 }
146
147 #[test]
148 fn extracts_syslog_files_users_and_console() {
149 let syslog = system(&juniper::parse_braces(SYSLOG)).unwrap().syslog.unwrap();
150
151 let files: Vec<_> = syslog.files.iter().map(|f| f.name.as_str()).collect();
152 assert_eq!(files, ["messages", "interactive-commands"]);
153 assert_eq!(syslog.files[0].rules.len(), 2);
155 assert_eq!(syslog.files[1].rules[0].facility, "interactive-commands");
156 assert_eq!(syslog.files[1].rules[0].severity, "any");
157
158 assert_eq!(syslog.users[0].name, "*");
159 assert_eq!(syslog.users[0].rules[0].severity, "emergency");
160 assert_eq!(syslog.console[0].facility, "any");
161 assert_eq!(syslog.console[0].severity, "critical");
162 }
163
164 #[test]
165 fn extracts_syslog_global_settings() {
166 let syslog = system(&juniper::parse_braces(SYSLOG)).unwrap().syslog.unwrap();
167 assert_eq!(syslog.source_address.as_deref(), Some("192.0.2.1"));
168 assert_eq!(syslog.time_format.as_deref(), Some("year millisecond"));
169 }
170
171 #[test]
172 fn extracts_syslog_destination_options() {
173 let syslog = system(&juniper::parse_braces(
174 r#"
175system {
176 syslog {
177 allow-duplicates;
178 log-rotate-frequency 5;
179 host 192.0.2.5 {
180 any notice;
181 allow-duplicates;
182 explicit-priority;
183 exclude-hostname;
184 match "UI_COMMIT";
185 }
186 file messages {
187 any notice;
188 allow-duplicates;
189 explicit-priority;
190 match "!(ssh)";
191 structured-data {
192 brief;
193 }
194 }
195 file plain {
196 any info;
197 structured-data;
198 }
199 user admin {
200 any alert;
201 match "commit";
202 }
203 }
204}
205"#,
206 ))
207 .unwrap()
208 .syslog
209 .unwrap();
210 assert!(syslog.allow_duplicates);
211 assert_eq!(syslog.log_rotate_frequency.as_deref(), Some("5"));
212
213 let host = &syslog.hosts[0];
214 assert!(host.allow_duplicates);
215 assert!(host.explicit_priority);
216 assert!(host.exclude_hostname);
217 assert_eq!(host.match_regex.as_deref(), Some("UI_COMMIT"));
218
219 let messages = &syslog.files[0];
220 assert!(messages.allow_duplicates);
221 assert!(messages.explicit_priority);
222 assert_eq!(messages.match_regex.as_deref(), Some("!(ssh)"));
223 assert!(messages.structured_data.as_ref().unwrap().brief);
224
225 let plain = &syslog.files[1];
226 assert!(!plain.structured_data.as_ref().unwrap().brief);
227 assert!(!plain.allow_duplicates);
228 assert_eq!(plain.match_regex, None);
229
230 assert_eq!(syslog.users[0].match_regex.as_deref(), Some("commit"));
231 }
232
233 #[test]
234 fn syslog_destination_options_default_to_off() {
235 let syslog = system(&juniper::parse_braces(
236 "system {\n syslog {\n host 192.0.2.5 {\n any notice;\n }\n }\n}\n",
237 ))
238 .unwrap()
239 .syslog
240 .unwrap();
241 assert!(!syslog.allow_duplicates);
242 assert_eq!(syslog.log_rotate_frequency, None);
243 assert!(!syslog.hosts[0].explicit_priority);
244 assert_eq!(syslog.hosts[0].match_regex, None);
245 }
246
247 #[test]
248 fn no_syslog_is_none() {
249 assert!(system(&juniper::parse_braces("system {\n host-name pe1;\n}\n"))
250 .unwrap()
251 .syslog
252 .is_none());
253 }
254}