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
//! `webapi`
//! `system { services { webapi { ... } } }`.
//!
//! The Web API process of the SRX integrated ClearPass authentication and enforcement feature.
//!
//! # See also
//!
//! - [webapi (System Services)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/services-webapi-user-cli.html)
//! - [client (System Services)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/client-webapi-cli.html)
//! - [debug-level (System Services)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/debug-level-webapi-cli.html)
//! - [debug-log (System Services)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/services-debug-aruba-cli.html)
//! - [http (Web API)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/security-http-webapi-cli.html)
//! - [https (Web API)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/security-https-webapi-cli.html)

use crate::{active_children, arg_of, args_of, child_block, has_leaf, secret_value, Secret};
use asterust::config::ConfigLine;

/// The `https` block of the Web API.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct WebapiHttps {
    pub certificate: Option<String>,
    pub certificate_key: Option<String>,
    pub default_certificate: bool,
    pub pki_local_certificate: Option<String>,
    pub port: Option<String>,
}

/// The `user` block of the Web API. The `password` is read only with the `secrets` feature.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct WebapiUser {
    pub name: Option<String>,
    /// `None` unless the `secrets` feature is enabled.
    pub password: Option<Secret>,
}

/// The `system services webapi` settings.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct Webapi {
    /// The addresses the client (the ClearPass Policy Manager) may connect from.
    pub clients: Vec<String>,
    /// `alert`, `crit`, `emerg`, `error`, `info`, `notice` or `warn`.
    pub debug_level: Option<String>,
    pub debug_log: Option<String>,
    /// Whether `http` is written, and its port.
    pub http: bool,
    pub http_port: Option<String>,
    pub https: Option<WebapiHttps>,
    pub user: Option<WebapiUser>,
}

pub(super) fn parse_webapi(webapi: &ConfigLine) -> Webapi {
    let http = child_block(webapi, "http");
    Webapi {
        clients: args_of(webapi, "client").into_iter().map(str::to_string).collect(),
        debug_level: arg_of(webapi, "debug-level").map(str::to_string),
        debug_log: arg_of(webapi, "debug-log").map(str::to_string),
        http: http.is_some(),
        http_port: http.and_then(|h| arg_of(h, "port")).map(str::to_string),
        https: child_block(webapi, "https").map(|https| WebapiHttps {
            certificate: arg_of(https, "certificate").map(str::to_string),
            certificate_key: arg_of(https, "certificate-key").map(str::to_string),
            default_certificate: has_leaf(https, "default-certificate"),
            pki_local_certificate: arg_of(https, "pki-local-certificate").map(str::to_string),
            port: arg_of(https, "port").map(str::to_string),
        }),
        user: active_children(webapi)
            .find(|c| c.text == "user" || c.arg("user").is_some())
            .map(|user| WebapiUser {
                // Written `user <name> { password ...; }` or `user { <name>; password ...; }`.
                name: user
                    .arg("user")
                    .map(|name| name.trim_matches('"').to_string())
                    .or_else(|| {
                        active_children(user)
                            .find(|c| !c.text.starts_with("password"))
                            .map(|c| c.text.trim_matches('"').to_string())
                    }),
                password: secret_value(arg_of(user, "password")),
            }),
    }
}

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

    // Not from documentation: written from the CLI Reference syntax pages. See the README "Sources".
    #[test]
    fn extracts_the_web_api_without_the_password() {
        let webapi = system(&juniper::parse_braces(
            r#"
system {
    services {
        webapi {
            client 192.0.2.60;
            debug-level warn;
            debug-log webapi.log;
            http {
                port 8080;
            }
            https {
                certificate api-cert;
                certificate-key api-key;
                pki-local-certificate api-pki;
                port 8443;
            }
            user cppm {
                password "$9$secret"; ## SECRET-DATA
            }
        }
    }
}
"#,
        ))
        .unwrap()
        .services
        .unwrap()
        .webapi
        .unwrap();
        assert_eq!(webapi.clients, ["192.0.2.60"]);
        assert_eq!(webapi.debug_level.as_deref(), Some("warn"));
        assert_eq!(webapi.debug_log.as_deref(), Some("webapi.log"));
        assert!(webapi.http);
        assert_eq!(webapi.http_port.as_deref(), Some("8080"));
        let https = webapi.https.unwrap();
        assert_eq!(https.certificate.as_deref(), Some("api-cert"));
        assert_eq!(https.certificate_key.as_deref(), Some("api-key"));
        assert!(!https.default_certificate);
        assert_eq!(https.pki_local_certificate.as_deref(), Some("api-pki"));
        assert_eq!(https.port.as_deref(), Some("8443"));
        let user = webapi.user.unwrap();
        assert_eq!(user.name.as_deref(), Some("cppm"));
        assert_eq!(
            user.password.as_ref().map(crate::Secret::expose),
            crate::secret::expected("$9$secret")
        );
    }

    #[test]
    fn reads_the_user_name_written_inside_the_block() {
        let user = system(&juniper::parse_braces(
            "system {\n    services {\n        webapi {\n            user {\n                cppm;\n            }\n            https {\n                default-certificate;\n            }\n        }\n    }\n}\n",
        ))
        .unwrap()
        .services
        .unwrap()
        .webapi
        .unwrap();
        assert_eq!(user.user.unwrap().name.as_deref(), Some("cppm"));
        assert!(user.https.unwrap().default_certificate);
    }

    #[test]
    fn no_webapi_is_none() {
        let services = system(&juniper::parse_braces(
            "system {\n    services {\n        telnet;\n    }\n}\n",
        ))
        .unwrap()
        .services
        .unwrap();
        assert!(services.webapi.is_none());
    }
}