asterust-junos 0.5.13

Typed Juniper JunOS config views (interfaces, protocols, routing instances and options, policy options, firewall filters, class of service, system, services) extracted from an asterust config tree.
Documentation
//! `ppp-options`
//! `interfaces { <name> { ppp-options { ... } } }` and `interfaces { <name> { unit <n> { ppp-options { ... } } } }`.
//!
//! # See also
//!
//! - [ppp-options](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/ppp-options-edit-interfaces.html)
//! - [peer-ip-address-optional](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/peer-ip-address-optional-edit-dynamic-profiles.html)
//! - [initiate-ncp](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/initiate-ncp-edit-ppp-options.html)
//! - [on-demand-ip-address](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/on-demand-ip-address-edit-dynamic-profiles.html)
//! - [chap](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/chap-edit-interfaces.html)
//! - [pap](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/pap-edit-interfaces.html)
//! - [compression (PPP Properties)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/compression-edit-interfaces-ppp-options.html)
//! - [no-termination-request](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/no-termination-request-edit-interfaces.html)

use crate::{arg_of, child_block, has_leaf, list_values, secret_value, statement_words, value_after};
use asterust::config::ConfigLine;

/// The `chap` authentication of the PPP options.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct PppChap {
    pub access_profile: Option<String>,
    pub challenge_length_minimum: Option<String>,
    pub challenge_length_maximum: Option<String>,
    /// A `default-chap-secret` is configured.
    pub has_default_chap_secret: bool,
    /// The `default-chap-secret`; `None` unless the `secrets` feature is enabled.
    pub default_chap_secret: Option<crate::Secret>,
    pub local_name: Option<String>,
    pub passive: bool,
}

/// The `pap` authentication of the PPP options.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct PppPap {
    pub access_profile: Option<String>,
    /// A `default-pap-password` is configured.
    pub has_default_pap_password: bool,
    /// The `default-pap-password`; `None` unless the `secrets` feature is enabled.
    pub default_pap_password: Option<crate::Secret>,
    pub local_name: Option<String>,
    /// A `local-password` is configured.
    pub has_local_password: bool,
    /// The `local-password`; `None` unless the `secrets` feature is enabled.
    pub local_password: Option<crate::Secret>,
    pub passive: bool,
}

/// The `compression` of the PPP options.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct PppCompression {
    /// Address and control field compression.
    pub acfc: bool,
    /// Protocol field compression.
    pub pfc: bool,
}

/// The `ppp-options` of an interface.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct PppOptions {
    /// The protocols of `authentication [ ... ]`, e.g. `chap` and `pap`.
    pub authentication: Vec<String>,
    pub mru: Option<String>,
    /// A size or `use-lower-layer`.
    pub mtu: Option<String>,
    pub chap: Option<PppChap>,
    pub pap: Option<PppPap>,
    pub compression: Option<PppCompression>,
    pub dynamic_profile: Option<String>,
    /// `ip`, `ipv6` or `dual-stack-passive`.
    pub initiate_ncp: Option<String>,
    pub ipcp_suggest_dns_option: bool,
    pub lcp_max_conf_req: Option<String>,
    /// Milliseconds.
    pub lcp_restart_timer: Option<String>,
    /// Seconds.
    pub loopback_clear_timer: Option<String>,
    pub ncp_max_conf_req: Option<String>,
    /// Milliseconds.
    pub ncp_restart_timer: Option<String>,
    pub on_demand_ip_address: bool,
    pub peer_ip_address_optional: bool,
    pub no_termination_request: bool,
}

fn parse_chap(chap: &ConfigLine) -> PppChap {
    let length = statement_words(chap, "challenge-length");
    PppChap {
        access_profile: arg_of(chap, "access-profile").map(str::to_string),
        challenge_length_minimum: length.as_ref().and_then(|w| value_after(w, "minimum")),
        challenge_length_maximum: length.as_ref().and_then(|w| value_after(w, "maximum")),
        has_default_chap_secret: arg_of(chap, "default-chap-secret").is_some(),
        default_chap_secret: secret_value(arg_of(chap, "default-chap-secret")),
        local_name: arg_of(chap, "local-name").map(str::to_string),
        passive: has_leaf(chap, "passive"),
    }
}

fn parse_pap(pap: &ConfigLine) -> PppPap {
    PppPap {
        access_profile: arg_of(pap, "access-profile").map(str::to_string),
        has_default_pap_password: arg_of(pap, "default-pap-password").is_some(),
        default_pap_password: secret_value(arg_of(pap, "default-pap-password")),
        local_name: arg_of(pap, "local-name").map(str::to_string),
        has_local_password: arg_of(pap, "local-password").is_some(),
        local_password: secret_value(arg_of(pap, "local-password")),
        passive: has_leaf(pap, "passive"),
    }
}

pub(in crate::interfaces) fn parse_ppp_options(options: &ConfigLine) -> PppOptions {
    let value = |keyword| arg_of(options, keyword).map(str::to_string);
    let initiate = statement_words(options, "initiate-ncp");
    PppOptions {
        authentication: arg_of(options, "authentication").map(list_values).unwrap_or_default(),
        mru: value("mru"),
        mtu: value("mtu"),
        chap: child_block(options, "chap").map(parse_chap),
        pap: child_block(options, "pap").map(parse_pap),
        compression: child_block(options, "compression").map(|c| PppCompression {
            acfc: has_leaf(c, "acfc"),
            pfc: has_leaf(c, "pfc"),
        }),
        dynamic_profile: value("dynamic-profile"),
        initiate_ncp: initiate.as_ref().and_then(|w| w.first()).map(|w| w.to_string()),
        ipcp_suggest_dns_option: initiate
            .as_ref()
            .is_some_and(|w| w.contains(&"ipcp-suggest-dns-option")),
        lcp_max_conf_req: value("lcp-max-conf-req"),
        lcp_restart_timer: value("lcp-restart-timer"),
        loopback_clear_timer: value("loopback-clear-timer"),
        ncp_max_conf_req: value("ncp-max-conf-req"),
        ncp_restart_timer: value("ncp-restart-timer"),
        on_demand_ip_address: has_leaf(options, "on-demand-ip-address"),
        peer_ip_address_optional: has_leaf(options, "peer-ip-address-optional"),
        no_termination_request: has_leaf(options, "no-termination-request"),
    }
}

#[cfg(test)]
mod tests {
    use crate::interfaces::interfaces;
    use crate::secret::expected;
    use asterust::config::juniper;

    // Not from documentation: written from the CLI Reference syntax pages. See the README "Sources".
    #[test]
    fn extracts_ppp_options() {
        let ifaces = interfaces(&juniper::parse_braces(
            r#"
interfaces {
    so-0/0/0 {
        ppp-options {
            authentication [ chap pap ];
            mru 1500;
            mtu use-lower-layer;
            dynamic-profile ppp-prof;
            initiate-ncp ip ipcp-suggest-dns-option;
            lcp-max-conf-req 10;
            lcp-restart-timer 3000;
            loopback-clear-timer 20;
            ncp-max-conf-req 8;
            ncp-restart-timer 1500;
            on-demand-ip-address;
            no-termination-request;
            compression {
                acfc;
            }
            chap {
                access-profile radius-prof;
                challenge-length minimum 16 maximum 32;
                default-chap-secret "$9$chap";
                local-name r1;
                passive;
            }
            pap {
                access-profile pap-prof;
                default-pap-password "$9$pap";
                local-name r2;
                local-password "$9$local";
            }
        }
    }
    so-0/0/1 {
        ppp-options {
            initiate-ncp dual-stack-passive;
        }
    }
}
"#,
        ));
        let ppp = ifaces[0].ppp_options.as_ref().unwrap();
        assert_eq!(ppp.authentication, ["chap", "pap"]);
        assert_eq!(ppp.mru.as_deref(), Some("1500"));
        assert_eq!(ppp.mtu.as_deref(), Some("use-lower-layer"));
        assert_eq!(ppp.dynamic_profile.as_deref(), Some("ppp-prof"));
        assert_eq!(ppp.initiate_ncp.as_deref(), Some("ip"));
        assert!(ppp.ipcp_suggest_dns_option);
        assert_eq!(ppp.lcp_max_conf_req.as_deref(), Some("10"));
        assert_eq!(ppp.lcp_restart_timer.as_deref(), Some("3000"));
        assert_eq!(ppp.loopback_clear_timer.as_deref(), Some("20"));
        assert_eq!(ppp.ncp_max_conf_req.as_deref(), Some("8"));
        assert_eq!(ppp.ncp_restart_timer.as_deref(), Some("1500"));
        assert!(ppp.on_demand_ip_address && ppp.no_termination_request);
        let compression = ppp.compression.as_ref().unwrap();
        assert!(compression.acfc && !compression.pfc);
        let chap = ppp.chap.as_ref().unwrap();
        assert_eq!(chap.access_profile.as_deref(), Some("radius-prof"));
        assert_eq!(chap.challenge_length_minimum.as_deref(), Some("16"));
        assert_eq!(chap.challenge_length_maximum.as_deref(), Some("32"));
        assert!(chap.has_default_chap_secret);
        assert_eq!(
            chap.default_chap_secret.as_ref().map(crate::Secret::expose),
            expected("$9$chap")
        );
        assert_eq!(chap.local_name.as_deref(), Some("r1"));
        assert!(chap.passive);
        let pap = ppp.pap.as_ref().unwrap();
        assert_eq!(pap.access_profile.as_deref(), Some("pap-prof"));
        assert!(pap.has_default_pap_password && pap.has_local_password);
        assert_eq!(
            pap.default_pap_password.as_ref().map(crate::Secret::expose),
            expected("$9$pap")
        );
        assert_eq!(
            pap.local_password.as_ref().map(crate::Secret::expose),
            expected("$9$local")
        );
        assert_eq!(pap.local_name.as_deref(), Some("r2"));
        assert!(!pap.passive);

        let minimal = ifaces[1].ppp_options.as_ref().unwrap();
        assert_eq!(minimal.initiate_ncp.as_deref(), Some("dual-stack-passive"));
        assert!(!minimal.ipcp_suggest_dns_option && minimal.chap.is_none() && minimal.pap.is_none());
        assert!(minimal.authentication.is_empty() && minimal.compression.is_none());
    }
}