Skip to main content

asterust_junos/system/
authentication_secret.rs

1//! The password and key statements of an `authentication` block, read only with the `secrets` feature.
2
3use crate::{active_children, secret_value, split_keyword, split_name, Secret};
4use asterust::config::ConfigLine;
5
6/// A password or key statement, e.g. `encrypted-password "$6$..."` or `ssh-rsa "ssh-rsa AAAA... user@host"`.
7#[derive(Clone, Debug, PartialEq, Eq)]
8#[non_exhaustive]
9pub struct AuthenticationSecret {
10    /// The statement name, e.g. `"encrypted-password"` or `"ssh-rsa"`.
11    pub method: String,
12    pub value: Secret,
13}
14
15/// `<method> <value>`; `None` for a bare statement such as `no-public-keys`, or without the `secrets` feature.
16fn from_statement(statement: &str) -> Option<AuthenticationSecret> {
17    let (method, rest) = split_keyword(statement);
18    let value = secret_value((!rest.is_empty()).then(|| split_name(rest).0))?;
19    Some(AuthenticationSecret {
20        method: method.to_string(),
21        value,
22    })
23}
24
25/// The secrets of each statement of an `authentication` block.
26pub(super) fn authentication_secrets(auth: &ConfigLine) -> Vec<AuthenticationSecret> {
27    active_children(auth).filter_map(|c| from_statement(&c.text)).collect()
28}
29
30/// The secrets of a setting such as `diag-port-authentication`, written either as
31/// `<keyword> encrypted-password "...";` or as a block.
32pub(super) fn password_secrets(system: &ConfigLine, keyword: &str) -> Vec<AuthenticationSecret> {
33    let mut secrets = Vec::new();
34    for child in active_children(system) {
35        if child.text == keyword {
36            secrets.extend(authentication_secrets(child));
37        } else if let Some(secret) = child.arg(keyword).and_then(from_statement) {
38            secrets.push(secret);
39        }
40    }
41    secrets
42}