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