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
//! `http`
//! `system { services { http { servers { server <name> { ... } } } } }`.
//!
//! # See also
//!
//! - [http (System Services)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/http-edit-system-services.html)
//! - [server (HTTP Servers)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/server-edit-system-services-http-servers.html)
//! - [tls (HTTP Servers)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/tls-edit-system-services-http-servers-server.html)

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

/// The `tls` block of an HTTP server.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct HttpServerTls {
    pub local_certificate: Option<String>,
    /// From `mutual-authentication { authentication-type }`, e.g. `request-cert-and-verify`.
    pub authentication_type: Option<String>,
    /// From `mutual-authentication { certificate-authority }`.
    pub certificate_authority: Option<String>,
}

/// A `server <name>` of `system services http servers`.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct HttpServer {
    pub name: String,
    /// The gRPC services the server exposes.
    pub grpc_services: Vec<String>,
    pub listen_address: Option<String>,
    pub max_connections: Option<String>,
    pub port: Option<String>,
    pub routing_instance: Option<String>,
    pub tls: Option<HttpServerTls>,
}

pub(super) fn parse_http_servers(http: &ConfigLine) -> Vec<HttpServer> {
    child_block(http, "servers")
        .map(|servers| {
            named_children(servers, "server")
                .map(|(name, server)| HttpServer {
                    name: name.to_string(),
                    grpc_services: args_of(server, "grpc").into_iter().flat_map(list_values).collect(),
                    listen_address: arg_of(server, "listen-address").map(str::to_string),
                    max_connections: arg_of(server, "max-connections").map(str::to_string),
                    port: arg_of(server, "port").map(str::to_string),
                    routing_instance: arg_of(server, "routing-instance").map(str::to_string),
                    tls: child_block(server, "tls").map(|tls| {
                        let mutual = child_block(tls, "mutual-authentication");
                        HttpServerTls {
                            local_certificate: arg_of(tls, "local-certificate").map(str::to_string),
                            authentication_type: mutual
                                .and_then(|m| arg_of(m, "authentication-type"))
                                .map(str::to_string),
                            certificate_authority: mutual
                                .and_then(|m| arg_of(m, "certificate-authority"))
                                .map(str::to_string),
                        }
                    }),
                })
                .collect()
        })
        .unwrap_or_default()
}

#[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_http_servers() {
        let servers = system(&juniper::parse_braces(
            r#"
system {
    services {
        http {
            servers {
                server gnmi {
                    grpc [ gnmi gnoi ];
                    listen-address 192.0.2.1;
                    max-connections 20;
                    port 9339;
                    routing-instance mgmt_junos;
                    tls {
                        local-certificate gnmi-cert;
                        mutual-authentication {
                            authentication-type request-cert-and-verify;
                            certificate-authority ca-profile;
                        }
                    }
                }
                server plain {
                    port 8080;
                }
            }
        }
    }
}
"#,
        ))
        .unwrap()
        .services
        .unwrap()
        .http_servers;
        assert_eq!(servers.len(), 2);
        let gnmi = &servers[0];
        assert_eq!(gnmi.name, "gnmi");
        assert_eq!(gnmi.grpc_services, ["gnmi", "gnoi"]);
        assert_eq!(gnmi.listen_address.as_deref(), Some("192.0.2.1"));
        assert_eq!(gnmi.max_connections.as_deref(), Some("20"));
        assert_eq!(gnmi.port.as_deref(), Some("9339"));
        assert_eq!(gnmi.routing_instance.as_deref(), Some("mgmt_junos"));
        let tls = gnmi.tls.as_ref().unwrap();
        assert_eq!(tls.local_certificate.as_deref(), Some("gnmi-cert"));
        assert_eq!(tls.authentication_type.as_deref(), Some("request-cert-and-verify"));
        assert_eq!(tls.certificate_authority.as_deref(), Some("ca-profile"));
        assert!(servers[1].tls.is_none());
        assert!(servers[1].grpc_services.is_empty());
    }

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