asterust-junos 0.5.14

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
//! `rest`
//! `system { services { rest { ... } } }`.
//!
//! # See also
//!
//! - [rest](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/rest-edit-system-services.html)
//! - [http (REST API)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/http-edit-system-services-rest.html)
//! - [https (REST API)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/https-edit-system-services-rest.html)
//! - [allowed-sources (REST API)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/allowed-sources-edit-system-services-rest.html)
//! - [enable-explorer (REST API)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/enable-explorer-edit-system-services-rest.html)
//! - [cipher-list (REST API)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/cipher-list-edit-system-services-rest-https.html)
//! - [mutual-authentication (REST API)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/mutual-authentication-edit-system-services-rest-https.html)
//! - [ca-chain (REST API)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/ca-chain.html)
//! - [server-certificate (REST API)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/server-certificate-edit-system-services-rest-https.html)

use crate::protocols::traceoptions::{parse_traceoptions, Traceoptions};
use crate::{arg_of, args_of, child_block, has_leaf, list_values};
use asterust::config::ConfigLine;

/// The `rest http` block.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct RestHttp {
    pub addresses: Vec<String>,
    pub port: Option<String>,
}

/// The `rest https` block.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct RestHttps {
    pub addresses: Vec<String>,
    pub cipher_list: Vec<String>,
    /// From `mutual-authentication { certificate-authority }`.
    pub certificate_authority: Option<String>,
    pub ca_chain: Option<String>,
    pub port: Option<String>,
    pub server_certificate: Option<String>,
}

/// The `system services rest` settings.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct Rest {
    /// From `control allowed-sources`.
    pub allowed_sources: Vec<String>,
    /// From `control connection-limit`.
    pub connection_limit: Option<String>,
    pub enable_explorer: bool,
    pub http: Option<RestHttp>,
    pub https: Option<RestHttps>,
    pub routing_instance: Option<String>,
    pub traceoptions: Option<Traceoptions>,
}

fn addresses(block: &ConfigLine, keyword: &str) -> Vec<String> {
    args_of(block, keyword).into_iter().flat_map(list_values).collect()
}

pub(super) fn parse_rest(rest: &ConfigLine) -> Rest {
    let control = child_block(rest, "control");
    Rest {
        allowed_sources: control.map(|c| addresses(c, "allowed-sources")).unwrap_or_default(),
        connection_limit: control.and_then(|c| arg_of(c, "connection-limit")).map(str::to_string),
        enable_explorer: has_leaf(rest, "enable-explorer"),
        http: child_block(rest, "http").map(|http| RestHttp {
            addresses: addresses(http, "addresses"),
            port: arg_of(http, "port").map(str::to_string),
        }),
        https: child_block(rest, "https").map(|https| RestHttps {
            addresses: addresses(https, "addresses"),
            cipher_list: addresses(https, "cipher-list"),
            certificate_authority: child_block(https, "mutual-authentication")
                .and_then(|m| arg_of(m, "certificate-authority"))
                .map(str::to_string),
            ca_chain: arg_of(https, "ca-chain").map(str::to_string),
            port: arg_of(https, "port").map(str::to_string),
            server_certificate: arg_of(https, "server-certificate").map(str::to_string),
        }),
        routing_instance: arg_of(rest, "routing-instance").map(str::to_string),
        traceoptions: parse_traceoptions(rest),
    }
}

#[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_rest_api_settings() {
        let rest = system(&juniper::parse_braces(
            r#"
system {
    services {
        rest {
            control {
                allowed-sources [ 192.0.2.0/24 198.51.100.7 ];
                connection-limit 100;
            }
            enable-explorer;
            http {
                addresses 192.0.2.1;
                port 3000;
            }
            https {
                addresses [ 192.0.2.1 192.0.2.2 ];
                cipher-list [ rsa-with-aes-128-cbc-sha dhe-rsa-with-aes-256-cbc-sha ];
                mutual-authentication {
                    certificate-authority ca-profile;
                }
                port 3443;
                server-certificate rest-cert;
                ca-chain chain-profile;
            }
            routing-instance mgmt_junos;
            traceoptions {
                flag all;
            }
        }
    }
}
"#,
        ))
        .unwrap()
        .services
        .unwrap()
        .rest
        .unwrap();
        assert_eq!(rest.allowed_sources, ["192.0.2.0/24", "198.51.100.7"]);
        assert_eq!(rest.connection_limit.as_deref(), Some("100"));
        assert!(rest.enable_explorer);
        let http = rest.http.unwrap();
        assert_eq!(http.addresses, ["192.0.2.1"]);
        assert_eq!(http.port.as_deref(), Some("3000"));
        let https = rest.https.unwrap();
        assert_eq!(https.addresses, ["192.0.2.1", "192.0.2.2"]);
        assert_eq!(https.cipher_list.len(), 2);
        assert_eq!(https.certificate_authority.as_deref(), Some("ca-profile"));
        assert_eq!(https.port.as_deref(), Some("3443"));
        assert_eq!(https.server_certificate.as_deref(), Some("rest-cert"));
        assert_eq!(https.ca_chain.as_deref(), Some("chain-profile"));
        assert_eq!(rest.routing_instance.as_deref(), Some("mgmt_junos"));
        assert_eq!(rest.traceoptions.unwrap().flags[0].name, "all");
    }

    #[test]
    fn bare_rest_has_no_settings() {
        let rest = system(&juniper::parse_braces(
            "system {\n    services {\n        rest;\n    }\n}\n",
        ))
        .unwrap()
        .services
        .unwrap()
        .rest
        .unwrap();
        assert!(rest.allowed_sources.is_empty());
        assert!(!rest.enable_explorer);
        assert!(rest.http.is_none());
        assert!(rest.https.is_none());
    }

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