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_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 never extracted.
12#[derive(Clone, Debug, Default, PartialEq, Eq)]
13#[non_exhaustive]
14pub struct OspfAuthentication {
15    pub simple_password: bool,
16    pub md5: Vec<OspfMd5Key>,
17    /// Key identifiers of `multi-active-md5`.
18    pub multi_active_md5: Vec<String>,
19    /// The name of the key chain, from `keychain` (23.4 and later).
20    pub keychain: Option<String>,
21}
22
23/// An `md5 <key-id>` entry; only the identifier and the start time are kept.
24#[derive(Clone, Debug, PartialEq, Eq)]
25#[non_exhaustive]
26pub struct OspfMd5Key {
27    pub id: String,
28    pub start_time: Option<String>,
29}
30
31pub(super) fn parse_authentication(iface: &ConfigLine) -> Option<OspfAuthentication> {
32    let auth = child_block(iface, "authentication")?;
33    Some(OspfAuthentication {
34        simple_password: arg_of(auth, "simple-password").is_some(),
35        md5: named_children(auth, "md5")
36            .map(|(arg, key)| OspfMd5Key {
37                id: split_name(arg).0.to_string(),
38                start_time: arg_of(key, "start-time").map(str::to_string),
39            })
40            .collect(),
41        multi_active_md5: named_children(auth, "multi-active-md5")
42            .map(|(arg, _)| split_name(arg).0.to_string())
43            .collect(),
44        keychain: arg_of(auth, "keychain").map(str::to_string),
45    })
46}
47
48#[cfg(test)]
49mod tests {
50    use crate::protocols::ospf::ospf;
51    use asterust::config::juniper;
52
53    // Not from documentation: written from the CLI Reference syntax pages. See the README "Sources".
54    const CONFIG: &str = r#"
55protocols {
56    ospf {
57        area 0.0.0.0 {
58            interface ge-0/0/0.0 {
59                authentication {
60                    md5 1 {
61                        key "$9$secret"; ## SECRET-DATA
62                        start-time "2026-1-1.00:00";
63                    }
64                    md5 2 key "$9$other"; ## SECRET-DATA
65                }
66            }
67            interface ge-0/0/1.0 {
68                authentication {
69                    simple-password "$9$secret"; ## SECRET-DATA
70                }
71            }
72            interface ge-0/0/2.0 {
73                authentication {
74                    multi-active-md5 5 key "$9$secret" delete-if-not-inuse; ## SECRET-DATA
75                    keychain OSPF-KEYS;
76                }
77            }
78        }
79    }
80}
81"#;
82
83    #[test]
84    fn reads_md5_key_ids_without_the_keys() {
85        let ospf = ospf(&juniper::parse_braces(CONFIG)).unwrap();
86        let auth = ospf.areas[0].interfaces[0].authentication.as_ref().unwrap();
87        assert_eq!(auth.md5.len(), 2);
88        assert_eq!(auth.md5[0].id, "1");
89        assert_eq!(auth.md5[0].start_time.as_deref(), Some("2026-1-1.00:00"));
90        assert_eq!(auth.md5[1].id, "2");
91        assert_eq!(auth.md5[1].start_time, None);
92        assert!(!auth.simple_password);
93    }
94
95    #[test]
96    fn reads_simple_password_multi_active_md5_and_keychain() {
97        let ospf = ospf(&juniper::parse_braces(CONFIG)).unwrap();
98        let interfaces = &ospf.areas[0].interfaces;
99        assert!(interfaces[1].authentication.as_ref().unwrap().simple_password);
100
101        let multi = interfaces[2].authentication.as_ref().unwrap();
102        assert_eq!(multi.multi_active_md5, ["5"]);
103        assert_eq!(multi.keychain.as_deref(), Some("OSPF-KEYS"));
104        assert!(multi.md5.is_empty());
105    }
106}