use crate::{
active_children, arg_of, args_of, child_block, first_word, has_leaf, list_values, named_children, split_name,
top_level, unquote,
};
use asterust::config::{Config, ConfigLine};
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct NameServer {
pub address: String,
pub routing_instance: Option<String>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct NtpHost {
pub address: String,
pub key: Option<String>,
pub prefer: bool,
pub version: Option<String>,
pub routing_instance: Option<String>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct NtpAuthenticationKey {
pub number: String,
pub key_type: Option<String>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct Ntp {
pub boot_server: Option<String>,
pub servers: Vec<NtpHost>,
pub peers: Vec<NtpHost>,
pub source_address: Option<String>,
pub trusted_keys: Vec<String>,
pub authentication_keys: Vec<NtpAuthenticationKey>,
pub broadcast_client: bool,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct SyslogRule {
pub facility: String,
pub severity: String,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct SyslogHost {
pub address: String,
pub rules: Vec<SyslogRule>,
pub port: Option<String>,
pub source_address: Option<String>,
pub routing_instance: Option<String>,
pub facility_override: Option<String>,
pub log_prefix: Option<String>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct SyslogTarget {
pub name: String,
pub rules: Vec<SyslogRule>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct Syslog {
pub hosts: Vec<SyslogHost>,
pub files: Vec<SyslogTarget>,
pub users: Vec<SyslogTarget>,
pub console: Vec<SyslogRule>,
pub source_address: Option<String>,
pub time_format: Option<String>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct System {
pub host_name: Option<String>,
pub name_servers: Vec<NameServer>,
pub ntp: Option<Ntp>,
pub syslog: Option<Syslog>,
}
pub fn system(config: &Config) -> Option<System> {
let system = top_level(config, "system")?;
Some(System {
host_name: arg_of(system, "host-name").map(str::to_string),
name_servers: name_servers(system),
ntp: child_block(system, "ntp").map(parse_ntp),
syslog: child_block(system, "syslog").map(parse_syslog),
})
}
fn parse_syslog(syslog: &ConfigLine) -> Syslog {
Syslog {
hosts: named_children(syslog, "host")
.map(|(address, host)| SyslogHost {
address: address.to_string(),
rules: syslog_rules(host),
port: arg_of(host, "port").map(str::to_string),
source_address: arg_of(host, "source-address").map(str::to_string),
routing_instance: arg_of(host, "routing-instance").map(str::to_string),
facility_override: arg_of(host, "facility-override").map(str::to_string),
log_prefix: arg_of(host, "log-prefix").map(str::to_string),
})
.collect(),
files: syslog_targets(syslog, "file"),
users: syslog_targets(syslog, "user"),
console: child_block(syslog, "console").map(syslog_rules).unwrap_or_default(),
source_address: arg_of(syslog, "source-address").map(str::to_string),
time_format: arg_of(syslog, "time-format").map(str::to_string),
}
}
fn syslog_targets(syslog: &ConfigLine, keyword: &str) -> Vec<SyslogTarget> {
named_children(syslog, keyword)
.map(|(name, target)| SyslogTarget {
name: name.to_string(),
rules: syslog_rules(target),
})
.collect()
}
fn syslog_rules(destination: &ConfigLine) -> Vec<SyslogRule> {
const SEVERITIES: [&str; 10] = [
"any",
"none",
"emergency",
"alert",
"critical",
"error",
"warning",
"notice",
"info",
"debug",
];
active_children(destination)
.filter_map(|c| {
let (facility, severity) = c.text.split_once(' ')?;
SEVERITIES.contains(&severity).then(|| SyslogRule {
facility: facility.to_string(),
severity: severity.to_string(),
})
})
.collect()
}
fn parse_ntp(ntp: &ConfigLine) -> Ntp {
let hosts = |keyword: &str| -> Vec<NtpHost> {
active_children(ntp)
.filter_map(|c| Some(ntp_host(c.arg(keyword)?, c)))
.collect()
};
Ntp {
boot_server: arg_of(ntp, "boot-server").map(str::to_string),
servers: hosts("server"),
peers: hosts("peer"),
source_address: arg_of(ntp, "source-address").and_then(first_word).map(str::to_string),
trusted_keys: args_of(ntp, "trusted-key").into_iter().flat_map(list_values).collect(),
authentication_keys: args_of(ntp, "authentication-key")
.into_iter()
.filter_map(ntp_authentication_key)
.collect(),
broadcast_client: has_leaf(ntp, "broadcast-client"),
}
}
fn ntp_host(arg: &str, line: &ConfigLine) -> NtpHost {
let (address, options) = split_name(arg);
let mut words = options.split_whitespace();
let mut host = NtpHost {
address: address.to_string(),
key: arg_of(line, "key").map(str::to_string),
prefer: has_leaf(line, "prefer"),
version: arg_of(line, "version").map(str::to_string),
routing_instance: arg_of(line, "routing-instance").map(str::to_string),
};
while let Some(word) = words.next() {
match word {
"prefer" => host.prefer = true,
"key" => host.key = words.next().map(str::to_string),
"version" => host.version = words.next().map(str::to_string),
"routing-instance" => host.routing_instance = words.next().map(str::to_string),
_ => {}
}
}
host
}
fn ntp_authentication_key(arg: &str) -> Option<NtpAuthenticationKey> {
let mut words = arg.split_whitespace();
let number = words.next()?.to_string();
let key_type = words.skip_while(|w| *w != "type").nth(1).map(str::to_string);
Some(NtpAuthenticationKey { number, key_type })
}
fn name_servers(system: &ConfigLine) -> Vec<NameServer> {
let mut found = Vec::new();
for child in active_children(system) {
if child.text == "name-server" {
found.extend(active_children(child).map(|server| name_server(&server.text, server)));
} else if let Some(address) = child.arg("name-server") {
found.push(name_server(address, child));
}
}
found
}
fn name_server(address: &str, line: &ConfigLine) -> NameServer {
NameServer {
address: unquote(address).to_string(),
routing_instance: arg_of(line, "routing-instance").map(str::to_string),
}
}
#[cfg(test)]
mod tests {
use super::*;
use asterust::config::juniper;
#[test]
fn extracts_host_name() {
let system = system(&juniper::parse_braces("system {\n host-name pe1;\n}\n")).unwrap();
assert_eq!(system.host_name.as_deref(), Some("pe1"));
}
#[test]
fn host_name_is_optional() {
let system = system(&juniper::parse_braces("system {\n domain-name example.net;\n}\n")).unwrap();
assert_eq!(system.host_name, None);
}
#[test]
fn skips_inactive_host_name() {
let system = system(&juniper::parse_braces("system {\n inactive: host-name pe1;\n}\n")).unwrap();
assert_eq!(system.host_name, None);
}
#[test]
fn extracts_name_server_block() {
let system = system(&juniper::parse_braces(
"system {\n name-server {\n 192.0.2.53;\n 2001:db8::53;\n }\n}\n",
))
.unwrap();
let addresses: Vec<_> = system.name_servers.iter().map(|s| s.address.as_str()).collect();
assert_eq!(addresses, ["192.0.2.53", "2001:db8::53"]);
assert_eq!(system.name_servers[0].routing_instance, None);
}
#[test]
fn name_server_in_management_instance() {
let system = system(&juniper::parse_braces(
"system {\n name-server {\n 192.0.2.53 {\n routing-instance mgmt_junos;\n }\n }\n}\n",
))
.unwrap();
assert_eq!(system.name_servers[0].address, "192.0.2.53");
assert_eq!(system.name_servers[0].routing_instance.as_deref(), Some("mgmt_junos"));
}
#[test]
fn extracts_one_line_name_servers_and_skips_inactive() {
let system = system(&juniper::parse_braces(
"system {\n name-server 192.0.2.53;\n name-server 192.0.2.54;\n inactive: name-server 192.0.2.55;\n}\n",
))
.unwrap();
let addresses: Vec<_> = system.name_servers.iter().map(|s| s.address.as_str()).collect();
assert_eq!(addresses, ["192.0.2.53", "192.0.2.54"]);
}
const NTP: &str = r#"
system {
ntp {
boot-server 192.0.2.10;
source-address 192.0.2.1;
authentication-key 1 type md5 value "$9$secret";
authentication-key 2 type sha256 value "$9$other";
trusted-key [ 1 2 ];
server 192.0.2.10 key 1 prefer version 4;
server 192.0.2.11;
server ntp.example.net {
key 2;
prefer;
version 3;
routing-instance mgmt_junos;
}
inactive: server 192.0.2.12;
peer 192.0.2.20 key 1;
broadcast-client;
}
}
"#;
#[test]
fn extracts_ntp_servers_in_line_and_block_form() {
let ntp = system(&juniper::parse_braces(NTP)).unwrap().ntp.unwrap();
let addresses: Vec<_> = ntp.servers.iter().map(|s| s.address.as_str()).collect();
assert_eq!(addresses, ["192.0.2.10", "192.0.2.11", "ntp.example.net"]);
let line = &ntp.servers[0];
assert_eq!(line.key.as_deref(), Some("1"));
assert!(line.prefer);
assert_eq!(line.version.as_deref(), Some("4"));
assert_eq!(ntp.servers[1].key, None);
assert!(!ntp.servers[1].prefer);
let block = &ntp.servers[2];
assert_eq!(block.key.as_deref(), Some("2"));
assert!(block.prefer);
assert_eq!(block.version.as_deref(), Some("3"));
assert_eq!(block.routing_instance.as_deref(), Some("mgmt_junos"));
}
#[test]
fn extracts_ntp_peers_keys_and_global_settings() {
let ntp = system(&juniper::parse_braces(NTP)).unwrap().ntp.unwrap();
assert_eq!(ntp.peers.len(), 1);
assert_eq!(ntp.peers[0].address, "192.0.2.20");
assert_eq!(ntp.peers[0].key.as_deref(), Some("1"));
assert_eq!(ntp.boot_server.as_deref(), Some("192.0.2.10"));
assert_eq!(ntp.source_address.as_deref(), Some("192.0.2.1"));
assert_eq!(ntp.trusted_keys, ["1", "2"]);
assert!(ntp.broadcast_client);
}
#[test]
fn ntp_authentication_keys_omit_the_secret() {
let ntp = system(&juniper::parse_braces(NTP)).unwrap().ntp.unwrap();
assert_eq!(ntp.authentication_keys.len(), 2);
assert_eq!(ntp.authentication_keys[0].number, "1");
assert_eq!(ntp.authentication_keys[0].key_type.as_deref(), Some("md5"));
assert_eq!(ntp.authentication_keys[1].key_type.as_deref(), Some("sha256"));
}
#[test]
fn no_ntp_is_none() {
assert!(system(&juniper::parse_braces("system {\n host-name pe1;\n}\n"))
.unwrap()
.ntp
.is_none());
}
const SYSLOG: &str = r#"
system {
syslog {
user * {
any emergency;
}
host 192.0.2.5 {
any notice;
authorization info;
port 514;
source-address 192.0.2.1;
routing-instance mgmt_junos;
facility-override local3;
log-prefix pe1;
}
host other-routing-engine {
any critical;
}
file messages {
any notice;
authorization info;
explicit-priority;
}
file interactive-commands {
interactive-commands any;
}
inactive: file debug {
any debug;
}
console {
any critical;
}
source-address 192.0.2.1;
time-format year millisecond;
}
}
"#;
#[test]
fn extracts_syslog_hosts() {
let syslog = system(&juniper::parse_braces(SYSLOG)).unwrap().syslog.unwrap();
let addresses: Vec<_> = syslog.hosts.iter().map(|h| h.address.as_str()).collect();
assert_eq!(addresses, ["192.0.2.5", "other-routing-engine"]);
let host = &syslog.hosts[0];
assert_eq!(host.rules.len(), 2);
assert_eq!(host.rules[1].facility, "authorization");
assert_eq!(host.rules[1].severity, "info");
assert_eq!(host.port.as_deref(), Some("514"));
assert_eq!(host.source_address.as_deref(), Some("192.0.2.1"));
assert_eq!(host.routing_instance.as_deref(), Some("mgmt_junos"));
assert_eq!(host.facility_override.as_deref(), Some("local3"));
assert_eq!(host.log_prefix.as_deref(), Some("pe1"));
}
#[test]
fn extracts_syslog_files_users_and_console() {
let syslog = system(&juniper::parse_braces(SYSLOG)).unwrap().syslog.unwrap();
let files: Vec<_> = syslog.files.iter().map(|f| f.name.as_str()).collect();
assert_eq!(files, ["messages", "interactive-commands"]);
assert_eq!(syslog.files[0].rules.len(), 2);
assert_eq!(syslog.files[1].rules[0].facility, "interactive-commands");
assert_eq!(syslog.files[1].rules[0].severity, "any");
assert_eq!(syslog.users[0].name, "*");
assert_eq!(syslog.users[0].rules[0].severity, "emergency");
assert_eq!(syslog.console[0].facility, "any");
assert_eq!(syslog.console[0].severity, "critical");
}
#[test]
fn extracts_syslog_global_settings() {
let syslog = system(&juniper::parse_braces(SYSLOG)).unwrap().syslog.unwrap();
assert_eq!(syslog.source_address.as_deref(), Some("192.0.2.1"));
assert_eq!(syslog.time_format.as_deref(), Some("year millisecond"));
}
#[test]
fn no_syslog_is_none() {
assert!(system(&juniper::parse_braces("system {\n host-name pe1;\n}\n"))
.unwrap()
.syslog
.is_none());
}
#[test]
fn no_system_is_none() {
assert!(system(&juniper::parse_braces("interfaces {\n lo0;\n}\n")).is_none());
}
}