Skip to main content

asterust_junos/protocols/ospf/
authentication.rs

1//! `authentication`
2//! `protocols { ospf { area <id> { interface <name> { authentication { ... } } } } }`; also valid on a `virtual-link`.
3//!
4//! # See also
5//!
6//! - [authentication (Protocols OSPF)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/authentication-edit-protocols-ospf.html)
7
8use crate::{arg_of, child_block, named_children, split_keyword, split_name};
9use asterust::config::ConfigLine;
10
11/// The `authentication` of an OSPF interface or virtual link. The `simple-password` and the MD5 key values are read
12/// only with the `secrets` feature.
13#[derive(Clone, Debug, Default, PartialEq, Eq)]
14#[non_exhaustive]
15pub struct OspfAuthentication {
16    pub simple_password: bool,
17    /// The `simple-password`; `None` unless the `secrets` feature is enabled.
18    pub simple_password_value: Option<crate::Secret>,
19    pub md5: Vec<OspfMd5Key>,
20    /// Key identifiers of `multi-active-md5`.
21    pub multi_active_md5: Vec<String>,
22    /// The `multi-active-md5` entries with their keys, in the order of `multi_active_md5`.
23    pub multi_active_md5_keys: Vec<OspfMd5Key>,
24    /// The name of the key chain, from `keychain` (23.4 and later).
25    pub keychain: Option<String>,
26}
27
28/// An `md5 <key-id>` entry. The key is `None` unless the `secrets` feature is enabled.
29#[derive(Clone, Debug, PartialEq, Eq)]
30#[non_exhaustive]
31pub struct OspfMd5Key {
32    pub id: String,
33    pub start_time: Option<String>,
34    pub key: Option<crate::Secret>,
35}
36
37/// `arg` is the text after `md5` or `multi-active-md5`: `1` for a block, `1 key "..."` for a leaf.
38fn parse_key_entry(arg: &str, entry: &ConfigLine) -> OspfMd5Key {
39    let (id, rest) = split_name(arg);
40    let (keyword, value) = split_keyword(rest);
41    let key = arg_of(entry, "key").or_else(|| (keyword == "key").then(|| split_name(value).0));
42    OspfMd5Key {
43        id: id.to_string(),
44        start_time: arg_of(entry, "start-time").map(str::to_string),
45        key: crate::secret_value(key),
46    }
47}
48
49pub(super) fn parse_authentication(iface: &ConfigLine) -> Option<OspfAuthentication> {
50    let auth = child_block(iface, "authentication")?;
51    Some(OspfAuthentication {
52        simple_password: arg_of(auth, "simple-password").is_some(),
53        simple_password_value: crate::secret_value(arg_of(auth, "simple-password")),
54        md5: named_children(auth, "md5")
55            .map(|(arg, entry)| parse_key_entry(arg, entry))
56            .collect(),
57        multi_active_md5: named_children(auth, "multi-active-md5")
58            .map(|(arg, _)| split_name(arg).0.to_string())
59            .collect(),
60        multi_active_md5_keys: named_children(auth, "multi-active-md5")
61            .map(|(arg, entry)| parse_key_entry(arg, entry))
62            .collect(),
63        keychain: arg_of(auth, "keychain").map(str::to_string),
64    })
65}
66
67#[cfg(test)]
68mod tests {
69    use crate::protocols::ospf::ospf;
70    use asterust::config::juniper;
71
72    // Not from documentation: written from the CLI Reference syntax pages. See the README "Sources".
73    const CONFIG: &str = r#"
74protocols {
75    ospf {
76        area 0.0.0.0 {
77            interface ge-0/0/0.0 {
78                authentication {
79                    md5 1 {
80                        key "$9$secret"; ## SECRET-DATA
81                        start-time "2026-1-1.00:00";
82                    }
83                    md5 2 key "$9$other"; ## SECRET-DATA
84                }
85            }
86            interface ge-0/0/1.0 {
87                authentication {
88                    simple-password "$9$secret"; ## SECRET-DATA
89                }
90            }
91            interface ge-0/0/2.0 {
92                authentication {
93                    multi-active-md5 5 key "$9$secret" delete-if-not-inuse; ## SECRET-DATA
94                    keychain OSPF-KEYS;
95                }
96            }
97        }
98    }
99}
100"#;
101
102    #[test]
103    fn reads_md5_key_ids_without_the_keys() {
104        let ospf = ospf(&juniper::parse_braces(CONFIG)).unwrap();
105        let auth = ospf.areas[0].interfaces[0].authentication.as_ref().unwrap();
106        assert_eq!(auth.md5.len(), 2);
107        assert_eq!(auth.md5[0].id, "1");
108        assert_eq!(auth.md5[0].start_time.as_deref(), Some("2026-1-1.00:00"));
109        assert_eq!(auth.md5[1].id, "2");
110        assert_eq!(auth.md5[1].start_time, None);
111        assert!(!auth.simple_password);
112        assert_eq!(
113            auth.md5[0].key.as_ref().map(crate::Secret::expose),
114            crate::secret::expected("$9$secret")
115        );
116        assert_eq!(
117            auth.md5[1].key.as_ref().map(crate::Secret::expose),
118            crate::secret::expected("$9$other")
119        );
120        assert_eq!(auth.simple_password_value, None);
121    }
122
123    #[test]
124    fn reads_simple_password_multi_active_md5_and_keychain() {
125        let ospf = ospf(&juniper::parse_braces(CONFIG)).unwrap();
126        let interfaces = &ospf.areas[0].interfaces;
127        assert!(interfaces[1].authentication.as_ref().unwrap().simple_password);
128        let simple = &interfaces[1].authentication.as_ref().unwrap().simple_password_value;
129        assert_eq!(
130            simple.as_ref().map(crate::Secret::expose),
131            crate::secret::expected("$9$secret")
132        );
133
134        let multi = interfaces[2].authentication.as_ref().unwrap();
135        assert_eq!(multi.multi_active_md5, ["5"]);
136        assert_eq!(multi.multi_active_md5_keys.len(), 1);
137        assert_eq!(multi.multi_active_md5_keys[0].id, "5");
138        assert_eq!(
139            multi.multi_active_md5_keys[0].key.as_ref().map(crate::Secret::expose),
140            crate::secret::expected("$9$secret")
141        );
142        assert_eq!(multi.keychain.as_deref(), Some("OSPF-KEYS"));
143        assert!(multi.md5.is_empty());
144    }
145}