asterust-junos 0.5.10

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
//! `ssh`
//! `system { services { ssh { ... } } }`.
//!
//! # See also
//!
//! - [ssh (System Services)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/ssh-edit-system.html)
//! - [moduli](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/moduli-edit-system-services-ssh.html)
//! - [hostkey-algorithm-list](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/system-edit-ssh-hostkey-algorithm-list.html)

use crate::{arg_of, args_of, child_block, has_leaf, list_values, statements};
use asterust::config::ConfigLine;

/// The `moduli` block: how Diffie-Hellman prime moduli are generated for group exchange.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct SshModuli {
    /// `designer` or `safe`, from `type`.
    pub modulus_type: Option<String>,
    /// `monthly` or `weekly`.
    pub refresh: Option<String>,
    /// Number of moduli to generate.
    pub count: Option<String>,
}

/// The `system services ssh` settings; every field is `None` or empty when only `ssh;` is written.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct SshService {
    /// `allow`, `deny` or `deny-password`.
    pub root_login: Option<String>,
    pub protocol_versions: Vec<String>,
    pub port: Option<String>,
    pub connection_limit: Option<String>,
    pub rate_limit: Option<String>,
    pub ciphers: Vec<String>,
    pub sftp_server: bool,
    /// Methods tried in order: `password`, `radius`, `tacplus`.
    pub authentication_order: Vec<String>,
    pub key_exchange: Vec<String>,
    pub macs: Vec<String>,
    pub client_alive_count_max: Option<String>,
    pub client_alive_interval: Option<String>,
    /// `md5` or `sha2-256`.
    pub fingerprint_hash: Option<String>,
    pub max_sessions_per_connection: Option<String>,
    pub max_pre_authentication_packets: Option<String>,
    pub no_challenge_response: bool,
    pub no_password_authentication: bool,
    pub no_passwords: bool,
    pub no_public_keys: bool,
    /// The `hostkey-algorithm` statements, e.g. `ssh-ed25519`.
    pub hostkey_algorithms: Vec<String>,
    /// The algorithms of `hostkey-algorithm-list { ... }`, e.g. `ed25519` or `rsa-sha2-512`.
    pub hostkey_algorithm_list: Vec<String>,
    pub moduli: Option<SshModuli>,
}

pub(super) fn parse_ssh(ssh: &ConfigLine) -> SshService {
    let list = |keyword: &str| args_of(ssh, keyword).into_iter().flat_map(list_values).collect();
    let field = |keyword: &str| arg_of(ssh, keyword).map(str::to_string);
    SshService {
        root_login: field("root-login"),
        protocol_versions: list("protocol-version"),
        port: field("port"),
        connection_limit: field("connection-limit"),
        rate_limit: field("rate-limit"),
        ciphers: list("ciphers"),
        sftp_server: has_leaf(ssh, "sftp-server"),
        authentication_order: list("authentication-order"),
        key_exchange: list("key-exchange"),
        macs: list("macs"),
        client_alive_count_max: field("client-alive-count-max"),
        client_alive_interval: field("client-alive-interval"),
        fingerprint_hash: field("fingerprint-hash"),
        max_sessions_per_connection: field("max-sessions-per-connection"),
        max_pre_authentication_packets: field("max-pre-authentication-packets"),
        no_challenge_response: has_leaf(ssh, "no-challenge-response"),
        no_password_authentication: has_leaf(ssh, "no-password-authentication"),
        no_passwords: has_leaf(ssh, "no-passwords"),
        no_public_keys: has_leaf(ssh, "no-public-keys"),
        hostkey_algorithms: list("hostkey-algorithm"),
        hostkey_algorithm_list: statements(ssh, "hostkey-algorithm-list")
            .into_iter()
            .flat_map(list_values)
            .collect(),
        moduli: child_block(ssh, "moduli").map(|moduli| SshModuli {
            modulus_type: arg_of(moduli, "type").map(str::to_string),
            refresh: arg_of(moduli, "refresh").map(str::to_string),
            count: arg_of(moduli, "count").map(str::to_string),
        }),
    }
}

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

    #[test]
    fn extracts_more_ssh_settings() {
        let ssh = system(&juniper::parse_braces(
            r#"
system {
    services {
        ssh {
            authentication-order [ tacplus password ];
            key-exchange [ curve25519-sha256 ecdh-sha2-nistp256 ];
            macs [ hmac-sha2-256 hmac-sha2-512 ];
            client-alive-count-max 3;
            client-alive-interval 60;
            fingerprint-hash sha2-256;
            max-sessions-per-connection 5;
            max-pre-authentication-packets 64;
            no-challenge-response;
            no-password-authentication;
            no-public-keys;
        }
    }
}
"#,
        ))
        .unwrap()
        .services
        .unwrap()
        .ssh
        .unwrap();
        assert_eq!(ssh.authentication_order, ["tacplus", "password"]);
        assert_eq!(ssh.key_exchange, ["curve25519-sha256", "ecdh-sha2-nistp256"]);
        assert_eq!(ssh.macs, ["hmac-sha2-256", "hmac-sha2-512"]);
        assert_eq!(ssh.client_alive_count_max.as_deref(), Some("3"));
        assert_eq!(ssh.client_alive_interval.as_deref(), Some("60"));
        assert_eq!(ssh.fingerprint_hash.as_deref(), Some("sha2-256"));
        assert_eq!(ssh.max_sessions_per_connection.as_deref(), Some("5"));
        assert_eq!(ssh.max_pre_authentication_packets.as_deref(), Some("64"));
        assert!(ssh.no_challenge_response);
        assert!(ssh.no_password_authentication);
        assert!(ssh.no_public_keys);
        assert!(!ssh.no_passwords);
    }

    // Not from documentation: written from the CLI Reference syntax page. See the README "Sources".
    #[test]
    fn extracts_ssh_hostkey_algorithms() {
        let ssh = system(&juniper::parse_braces(
            "system {\n    services {\n        ssh {\n            hostkey-algorithm ssh-ed25519;\n            hostkey-algorithm-list {\n                ed25519;\n                rsa-sha2-512;\n            }\n        }\n    }\n}\n",
        ))
        .unwrap()
        .services
        .unwrap()
        .ssh
        .unwrap();
        assert_eq!(ssh.hostkey_algorithms, ["ssh-ed25519"]);
        assert_eq!(ssh.hostkey_algorithm_list, ["ed25519", "rsa-sha2-512"]);
    }

    // Not from documentation: written from the CLI Reference syntax page. See the README "Sources".
    #[test]
    fn extracts_ssh_moduli() {
        let ssh = system(&juniper::parse_braces(
            "system {\n    services {\n        ssh {\n            moduli {\n                type safe;\n                refresh weekly;\n                count 5;\n            }\n        }\n    }\n}\n",
        ))
        .unwrap()
        .services
        .unwrap()
        .ssh
        .unwrap();
        let moduli = ssh.moduli.unwrap();
        assert_eq!(moduli.modulus_type.as_deref(), Some("safe"));
        assert_eq!(moduli.refresh.as_deref(), Some("weekly"));
        assert_eq!(moduli.count.as_deref(), Some("5"));
    }
}