asterust_junos/system/
ntp.rs1use crate::{active_children, arg_of, args_of, first_word, has_leaf, list_values, split_name};
4use asterust::config::ConfigLine;
5
6#[derive(Clone, Debug, PartialEq, Eq)]
8#[non_exhaustive]
9pub struct NtpHost {
10 pub address: String,
12 pub key: Option<String>,
14 pub prefer: bool,
15 pub version: Option<String>,
16 pub routing_instance: Option<String>,
17}
18
19#[derive(Clone, Debug, PartialEq, Eq)]
21#[non_exhaustive]
22pub struct NtpAuthenticationKey {
23 pub number: String,
24 pub key_type: Option<String>,
26}
27
28#[derive(Clone, Debug, PartialEq, Eq)]
30#[non_exhaustive]
31pub struct Ntp {
32 pub boot_server: Option<String>,
33 pub servers: Vec<NtpHost>,
34 pub peers: Vec<NtpHost>,
35 pub source_address: Option<String>,
36 pub trusted_keys: Vec<String>,
37 pub authentication_keys: Vec<NtpAuthenticationKey>,
38 pub broadcast_client: bool,
39}
40
41pub(super) fn parse_ntp(ntp: &ConfigLine) -> Ntp {
42 let hosts = |keyword: &str| -> Vec<NtpHost> {
43 active_children(ntp)
44 .filter_map(|c| Some(ntp_host(c.arg(keyword)?, c)))
45 .collect()
46 };
47 Ntp {
48 boot_server: arg_of(ntp, "boot-server").map(str::to_string),
49 servers: hosts("server"),
50 peers: hosts("peer"),
51 source_address: arg_of(ntp, "source-address").and_then(first_word).map(str::to_string),
52 trusted_keys: args_of(ntp, "trusted-key").into_iter().flat_map(list_values).collect(),
53 authentication_keys: args_of(ntp, "authentication-key")
54 .into_iter()
55 .filter_map(ntp_authentication_key)
56 .collect(),
57 broadcast_client: has_leaf(ntp, "broadcast-client"),
58 }
59}
60
61fn ntp_host(arg: &str, line: &ConfigLine) -> NtpHost {
64 let (address, options) = split_name(arg);
65 let mut words = options.split_whitespace();
66 let mut host = NtpHost {
67 address: address.to_string(),
68 key: arg_of(line, "key").map(str::to_string),
69 prefer: has_leaf(line, "prefer"),
70 version: arg_of(line, "version").map(str::to_string),
71 routing_instance: arg_of(line, "routing-instance").map(str::to_string),
72 };
73 while let Some(word) = words.next() {
74 match word {
75 "prefer" => host.prefer = true,
76 "key" => host.key = words.next().map(str::to_string),
77 "version" => host.version = words.next().map(str::to_string),
78 "routing-instance" => host.routing_instance = words.next().map(str::to_string),
79 _ => {}
80 }
81 }
82 host
83}
84
85fn ntp_authentication_key(arg: &str) -> Option<NtpAuthenticationKey> {
87 let mut words = arg.split_whitespace();
88 let number = words.next()?.to_string();
89 let key_type = words.skip_while(|w| *w != "type").nth(1).map(str::to_string);
90 Some(NtpAuthenticationKey { number, key_type })
91}
92
93#[cfg(test)]
94mod tests {
95 use crate::system::system;
96 use asterust::config::juniper;
97
98 const NTP: &str = r#"
99system {
100 ntp {
101 boot-server 192.0.2.10;
102 source-address 192.0.2.1;
103 authentication-key 1 type md5 value "$9$secret";
104 authentication-key 2 type sha256 value "$9$other";
105 trusted-key [ 1 2 ];
106 server 192.0.2.10 key 1 prefer version 4;
107 server 192.0.2.11;
108 server ntp.example.net {
109 key 2;
110 prefer;
111 version 3;
112 routing-instance mgmt_junos;
113 }
114 inactive: server 192.0.2.12;
115 peer 192.0.2.20 key 1;
116 broadcast-client;
117 }
118}
119"#;
120
121 #[test]
122 fn extracts_ntp_servers_in_line_and_block_form() {
123 let ntp = system(&juniper::parse_braces(NTP)).unwrap().ntp.unwrap();
124 let addresses: Vec<_> = ntp.servers.iter().map(|s| s.address.as_str()).collect();
125 assert_eq!(addresses, ["192.0.2.10", "192.0.2.11", "ntp.example.net"]);
126
127 let line = &ntp.servers[0];
128 assert_eq!(line.key.as_deref(), Some("1"));
129 assert!(line.prefer);
130 assert_eq!(line.version.as_deref(), Some("4"));
131
132 assert_eq!(ntp.servers[1].key, None);
133 assert!(!ntp.servers[1].prefer);
134
135 let block = &ntp.servers[2];
136 assert_eq!(block.key.as_deref(), Some("2"));
137 assert!(block.prefer);
138 assert_eq!(block.version.as_deref(), Some("3"));
139 assert_eq!(block.routing_instance.as_deref(), Some("mgmt_junos"));
140 }
141
142 #[test]
143 fn extracts_ntp_peers_keys_and_global_settings() {
144 let ntp = system(&juniper::parse_braces(NTP)).unwrap().ntp.unwrap();
145 assert_eq!(ntp.peers.len(), 1);
146 assert_eq!(ntp.peers[0].address, "192.0.2.20");
147 assert_eq!(ntp.peers[0].key.as_deref(), Some("1"));
148 assert_eq!(ntp.boot_server.as_deref(), Some("192.0.2.10"));
149 assert_eq!(ntp.source_address.as_deref(), Some("192.0.2.1"));
150 assert_eq!(ntp.trusted_keys, ["1", "2"]);
151 assert!(ntp.broadcast_client);
152 }
153
154 #[test]
155 fn ntp_authentication_keys_omit_the_secret() {
156 let ntp = system(&juniper::parse_braces(NTP)).unwrap().ntp.unwrap();
157 assert_eq!(ntp.authentication_keys.len(), 2);
158 assert_eq!(ntp.authentication_keys[0].number, "1");
159 assert_eq!(ntp.authentication_keys[0].key_type.as_deref(), Some("md5"));
160 assert_eq!(ntp.authentication_keys[1].key_type.as_deref(), Some("sha256"));
161 }
162
163 #[test]
164 fn no_ntp_is_none() {
165 assert!(system(&juniper::parse_braces("system {\n host-name pe1;\n}\n"))
166 .unwrap()
167 .ntp
168 .is_none());
169 }
170}