asterust-junos 0.5.1

Typed Juniper JunOS config views (interfaces, OSPF, BGP, IS-IS, routing instances, policy options, firewall filters, system) extracted from an asterust config tree.
Documentation
//! `authentication`
//! `protocols { ospf { area <id> { interface <name> { authentication { ... } } } } }`; also valid on a `virtual-link`.
//!
//! # See also
//!
//! - [authentication (Protocols OSPF)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/authentication-edit-protocols-ospf.html)

use crate::{arg_of, child_block, named_children, split_keyword, split_name};
use asterust::config::ConfigLine;

/// The `authentication` of an OSPF interface or virtual link. The `simple-password` and the MD5 key values are read
/// only with the `secrets` feature.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct OspfAuthentication {
    pub simple_password: bool,
    /// The `simple-password`; `None` unless the `secrets` feature is enabled.
    pub simple_password_value: Option<crate::Secret>,
    pub md5: Vec<OspfMd5Key>,
    /// Key identifiers of `multi-active-md5`.
    pub multi_active_md5: Vec<String>,
    /// The `multi-active-md5` entries with their keys, in the order of `multi_active_md5`.
    pub multi_active_md5_keys: Vec<OspfMd5Key>,
    /// The name of the key chain, from `keychain` (23.4 and later).
    pub keychain: Option<String>,
}

/// An `md5 <key-id>` entry. The key is `None` unless the `secrets` feature is enabled.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct OspfMd5Key {
    pub id: String,
    pub start_time: Option<String>,
    pub key: Option<crate::Secret>,
}

/// `arg` is the text after `md5` or `multi-active-md5`: `1` for a block, `1 key "..."` for a leaf.
fn parse_key_entry(arg: &str, entry: &ConfigLine) -> OspfMd5Key {
    let (id, rest) = split_name(arg);
    let (keyword, value) = split_keyword(rest);
    let key = arg_of(entry, "key").or_else(|| (keyword == "key").then(|| split_name(value).0));
    OspfMd5Key {
        id: id.to_string(),
        start_time: arg_of(entry, "start-time").map(str::to_string),
        key: crate::secret_value(key),
    }
}

pub(super) fn parse_authentication(iface: &ConfigLine) -> Option<OspfAuthentication> {
    let auth = child_block(iface, "authentication")?;
    Some(OspfAuthentication {
        simple_password: arg_of(auth, "simple-password").is_some(),
        simple_password_value: crate::secret_value(arg_of(auth, "simple-password")),
        md5: named_children(auth, "md5")
            .map(|(arg, entry)| parse_key_entry(arg, entry))
            .collect(),
        multi_active_md5: named_children(auth, "multi-active-md5")
            .map(|(arg, _)| split_name(arg).0.to_string())
            .collect(),
        multi_active_md5_keys: named_children(auth, "multi-active-md5")
            .map(|(arg, entry)| parse_key_entry(arg, entry))
            .collect(),
        keychain: arg_of(auth, "keychain").map(str::to_string),
    })
}

#[cfg(test)]
mod tests {
    use crate::protocols::ospf::ospf;
    use asterust::config::juniper;

    // Not from documentation: written from the CLI Reference syntax pages. See the README "Sources".
    const CONFIG: &str = r#"
protocols {
    ospf {
        area 0.0.0.0 {
            interface ge-0/0/0.0 {
                authentication {
                    md5 1 {
                        key "$9$secret"; ## SECRET-DATA
                        start-time "2026-1-1.00:00";
                    }
                    md5 2 key "$9$other"; ## SECRET-DATA
                }
            }
            interface ge-0/0/1.0 {
                authentication {
                    simple-password "$9$secret"; ## SECRET-DATA
                }
            }
            interface ge-0/0/2.0 {
                authentication {
                    multi-active-md5 5 key "$9$secret" delete-if-not-inuse; ## SECRET-DATA
                    keychain OSPF-KEYS;
                }
            }
        }
    }
}
"#;

    #[test]
    fn reads_md5_key_ids_without_the_keys() {
        let ospf = ospf(&juniper::parse_braces(CONFIG)).unwrap();
        let auth = ospf.areas[0].interfaces[0].authentication.as_ref().unwrap();
        assert_eq!(auth.md5.len(), 2);
        assert_eq!(auth.md5[0].id, "1");
        assert_eq!(auth.md5[0].start_time.as_deref(), Some("2026-1-1.00:00"));
        assert_eq!(auth.md5[1].id, "2");
        assert_eq!(auth.md5[1].start_time, None);
        assert!(!auth.simple_password);
        assert_eq!(
            auth.md5[0].key.as_ref().map(crate::Secret::expose),
            crate::secret::expected("$9$secret")
        );
        assert_eq!(
            auth.md5[1].key.as_ref().map(crate::Secret::expose),
            crate::secret::expected("$9$other")
        );
        assert_eq!(auth.simple_password_value, None);
    }

    #[test]
    fn reads_simple_password_multi_active_md5_and_keychain() {
        let ospf = ospf(&juniper::parse_braces(CONFIG)).unwrap();
        let interfaces = &ospf.areas[0].interfaces;
        assert!(interfaces[1].authentication.as_ref().unwrap().simple_password);
        let simple = &interfaces[1].authentication.as_ref().unwrap().simple_password_value;
        assert_eq!(
            simple.as_ref().map(crate::Secret::expose),
            crate::secret::expected("$9$secret")
        );

        let multi = interfaces[2].authentication.as_ref().unwrap();
        assert_eq!(multi.multi_active_md5, ["5"]);
        assert_eq!(multi.multi_active_md5_keys.len(), 1);
        assert_eq!(multi.multi_active_md5_keys[0].id, "5");
        assert_eq!(
            multi.multi_active_md5_keys[0].key.as_ref().map(crate::Secret::expose),
            crate::secret::expected("$9$secret")
        );
        assert_eq!(multi.keychain.as_deref(), Some("OSPF-KEYS"));
        assert!(multi.md5.is_empty());
    }
}