asterust-junos 0.3.14

Typed Juniper JunOS config views (interfaces, OSPF, BGP, IS-IS, routing instances, policy options, firewall filters, system) extracted from an asterust config tree.
Documentation
//! The password and key statements of an `authentication` block, read only with the `secrets` feature.

use crate::{active_children, secret_value, split_keyword, split_name, Secret};
use asterust::config::ConfigLine;

/// A password or key statement, e.g. `encrypted-password "$6$..."` or `ssh-rsa "ssh-rsa AAAA... user@host"`.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct AuthenticationSecret {
    /// The statement name, e.g. `"encrypted-password"` or `"ssh-rsa"`.
    pub method: String,
    pub value: Secret,
}

/// `<method> <value>`; `None` for a bare statement such as `no-public-keys`, or without the `secrets` feature.
fn from_statement(statement: &str) -> Option<AuthenticationSecret> {
    let (method, rest) = split_keyword(statement);
    let value = secret_value((!rest.is_empty()).then(|| split_name(rest).0))?;
    Some(AuthenticationSecret {
        method: method.to_string(),
        value,
    })
}

/// The secrets of each statement of an `authentication` block.
pub(super) fn authentication_secrets(auth: &ConfigLine) -> Vec<AuthenticationSecret> {
    active_children(auth).filter_map(|c| from_statement(&c.text)).collect()
}

/// The secrets of a setting such as `diag-port-authentication`, written either as
/// `<keyword> encrypted-password "...";` or as a block.
pub(super) fn password_secrets(system: &ConfigLine, keyword: &str) -> Vec<AuthenticationSecret> {
    let mut secrets = Vec::new();
    for child in active_children(system) {
        if child.text == keyword {
            secrets.extend(authentication_secrets(child));
        } else if let Some(secret) = child.arg(keyword).and_then(from_statement) {
            secrets.push(secret);
        }
    }
    secrets
}