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