use crate::{
active_children, arg_of, args_of, first_word, has_leaf, list_values, named_children, split_name, statement_words,
unquote, value_after,
};
use asterust::config::ConfigLine;
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct NtpHostNts {
pub remote_identity_container: Option<String>,
pub remote_identity_wildcard: Option<String>,
pub hostname: Option<String>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct NtpBroadcast {
pub address: Option<String>,
pub key: Option<String>,
pub routing_instance_name: Option<String>,
pub ttl: Option<String>,
pub version: Option<String>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct NtpRestriction {
pub address: String,
pub mask: Option<String>,
pub noquery: bool,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct NtpNts {
pub local_certificate: Option<String>,
pub trusted_ca_group: Option<String>,
pub trusted_ca_profile: Option<String>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct NtpThreshold {
pub value: String,
pub action: 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>,
pub nts: Option<NtpHostNts>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct NtpAuthenticationKey {
pub number: String,
pub key_type: Option<String>,
pub value: Option<crate::Secret>,
}
#[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,
pub broadcasts: Vec<NtpBroadcast>,
pub interval_range: Option<String>,
pub multicast_client: bool,
pub multicast_client_address: Option<String>,
pub nts: Option<NtpNts>,
pub restrictions: Vec<NtpRestriction>,
pub threshold: Option<NtpThreshold>,
}
pub(super) 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"),
broadcasts: active_children(ntp)
.filter(|c| c.text == "broadcast" || c.arg("broadcast").is_some())
.map(parse_broadcast)
.collect(),
interval_range: arg_of(ntp, "interval-range").map(str::to_string),
multicast_client: active_children(ntp)
.any(|c| c.text == "multicast-client" || c.arg("multicast-client").is_some()),
multicast_client_address: arg_of(ntp, "multicast-client").and_then(first_word).map(str::to_string),
nts: statement_words(ntp, "nts").map(|words| NtpNts {
local_certificate: value_after(&words, "local-certificate"),
trusted_ca_group: value_after(&words, "trusted-ca-group"),
trusted_ca_profile: value_after(&words, "trusted-ca-profile"),
}),
restrictions: named_children(ntp, "restrict")
.map(|(address, restriction)| NtpRestriction {
address: address.to_string(),
mask: arg_of(restriction, "mask").map(str::to_string),
noquery: has_leaf(restriction, "noquery"),
})
.collect(),
threshold: arg_of(ntp, "threshold").map(|arg| {
let words: Vec<&str> = arg.split_whitespace().collect();
NtpThreshold {
value: words.first().unwrap_or(&"").to_string(),
action: value_after(&words, "action"),
}
}),
}
}
fn parse_broadcast(line: &ConfigLine) -> NtpBroadcast {
let words: Vec<&str> = line
.text
.split_whitespace()
.skip(1)
.chain(active_children(line).flat_map(|c| c.text.split_whitespace()))
.collect();
let address = words
.first()
.filter(|w| !matches!(**w, "key" | "routing-instance-name" | "ttl" | "version"))
.map(|w| w.to_string());
NtpBroadcast {
address,
key: value_after(&words, "key"),
routing_instance_name: value_after(&words, "routing-instance-name"),
ttl: value_after(&words, "ttl"),
version: value_after(&words, "version"),
}
}
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),
nts: statement_words(line, "nts").map(|words| NtpHostNts {
remote_identity_container: value_after(&words, "container").map(|v| unquote(&v).to_string()),
remote_identity_wildcard: value_after(&words, "wildcard").map(|v| unquote(&v).to_string()),
hostname: value_after(&words, "hostname"),
}),
};
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 mut rest = words.skip_while(|w| *w != "type").skip(1);
let key_type = rest.next().map(str::to_string);
let value = rest.skip_while(|w| *w != "value").nth(1);
Some(NtpAuthenticationKey {
number,
key_type,
value: crate::secret_value(value),
})
}
#[cfg(test)]
mod tests {
use crate::system::system;
use asterust::config::juniper;
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"));
assert_eq!(
ntp.authentication_keys[0].value.as_ref().map(crate::Secret::expose),
crate::secret::expected("$9$secret")
);
assert_eq!(
ntp.authentication_keys[1].value.as_ref().map(crate::Secret::expose),
crate::secret::expected("$9$other")
);
}
#[test]
fn extracts_the_broadcast_restrict_nts_and_threshold_statements() {
let ntp = system(&juniper::parse_braces(
r#"
system {
ntp {
broadcast 192.0.2.255 key 3 routing-instance-name mgmt_junos ttl 4 version 4;
broadcast {
version 3;
}
interval-range 2;
multicast-client 224.0.1.1;
nts {
local-certificate ntp-cert;
trusted-ca trusted-ca-profile ca-profile;
}
restrict 192.0.2.0 {
mask 255.255.255.0;
noquery;
}
restrict 198.51.100.7;
threshold 400 action reject;
server pool.example.net {
nts {
remote-identity distinguished-name wildcard "*.example.net" hostname pool.example.net;
}
}
}
}
"#,
))
.unwrap()
.ntp
.unwrap();
assert_eq!(ntp.broadcasts.len(), 2);
assert_eq!(ntp.broadcasts[0].address.as_deref(), Some("192.0.2.255"));
assert_eq!(ntp.broadcasts[0].key.as_deref(), Some("3"));
assert_eq!(ntp.broadcasts[0].routing_instance_name.as_deref(), Some("mgmt_junos"));
assert_eq!(ntp.broadcasts[0].ttl.as_deref(), Some("4"));
assert_eq!(ntp.broadcasts[0].version.as_deref(), Some("4"));
assert_eq!(ntp.broadcasts[1].address, None);
assert_eq!(ntp.broadcasts[1].version.as_deref(), Some("3"));
assert_eq!(ntp.interval_range.as_deref(), Some("2"));
assert!(ntp.multicast_client);
assert_eq!(ntp.multicast_client_address.as_deref(), Some("224.0.1.1"));
let nts = ntp.nts.unwrap();
assert_eq!(nts.local_certificate.as_deref(), Some("ntp-cert"));
assert_eq!(nts.trusted_ca_profile.as_deref(), Some("ca-profile"));
assert_eq!(nts.trusted_ca_group, None);
assert_eq!(ntp.restrictions.len(), 2);
assert_eq!(ntp.restrictions[0].address, "192.0.2.0");
assert_eq!(ntp.restrictions[0].mask.as_deref(), Some("255.255.255.0"));
assert!(ntp.restrictions[0].noquery);
assert!(!ntp.restrictions[1].noquery);
let threshold = ntp.threshold.unwrap();
assert_eq!(threshold.value, "400");
assert_eq!(threshold.action.as_deref(), Some("reject"));
let host_nts = ntp.servers[0].nts.as_ref().unwrap();
assert_eq!(host_nts.remote_identity_wildcard.as_deref(), Some("*.example.net"));
assert_eq!(host_nts.hostname.as_deref(), Some("pool.example.net"));
assert_eq!(host_nts.remote_identity_container, None);
}
#[test]
fn ntp_extras_are_optional() {
let ntp = system(&juniper::parse_braces(NTP)).unwrap().ntp.unwrap();
assert!(ntp.broadcasts.is_empty());
assert!(!ntp.multicast_client);
assert!(ntp.nts.is_none());
assert!(ntp.restrictions.is_empty());
assert!(ntp.threshold.is_none());
assert!(ntp.servers[0].nts.is_none());
}
#[test]
fn no_ntp_is_none() {
assert!(system(&juniper::parse_braces("system {\n host-name pe1;\n}\n"))
.unwrap()
.ntp
.is_none());
}
}