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
//! `extension-service`
//! `system { services { extension-service { ... } } }`.
//!
//! Not to be confused with `system extensions extension-service`, which defines the JET applications.
//!
//! # See also
//!
//! - [request-response](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/request-response-edit-system-services-jet.html)
//! - [grpc](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/grpc-edit-system-jet.html)
//! - [ssl (gRPC)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/ssl-edit-system-services-grpc-jet.html)
//! - [notification (System Services)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/notification-edit-system-services-jet.html)
//! - [traceoptions (Services)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/traceoptions-edit-system-services-jet.html)

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

/// The `ssl` block of the gRPC request-response service.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct GrpcSsl {
    pub address: Option<String>,
    pub hot_reloading: bool,
    pub local_certificate: Option<String>,
    /// From `mutual-authentication { certificate-authority }`.
    pub certificate_authority: Option<String>,
    /// From `mutual-authentication { client-certificate-request }`, e.g. `require-certificate`.
    pub client_certificate_request: Option<String>,
    pub port: Option<String>,
    pub use_pki: bool,
}

/// The `request-response grpc` block.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct GrpcService {
    pub max_connections: Option<String>,
    pub routing_instance: Option<String>,
    pub ssl: Option<GrpcSsl>,
}

/// The `notification` block.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct JetNotification {
    /// From `allow-clients { address <ip>; }`.
    pub allow_clients: Vec<String>,
    pub broker_socket_send_buffer_size: Option<String>,
    pub max_connections: Option<String>,
    pub port: Option<String>,
}

/// The `system services extension-service` settings of the JET service process.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct ExtensionService {
    /// From `request-response { grpc { ... } }`.
    pub grpc: Option<GrpcService>,
    pub notification: Option<JetNotification>,
    pub traceoptions: Option<Traceoptions>,
}

fn parse_ssl(ssl: &ConfigLine) -> GrpcSsl {
    let mutual = child_block(ssl, "mutual-authentication");
    GrpcSsl {
        address: arg_of(ssl, "address").map(str::to_string),
        hot_reloading: has_leaf(ssl, "hot-reloading"),
        local_certificate: arg_of(ssl, "local-certificate").map(str::to_string),
        certificate_authority: mutual
            .and_then(|m| arg_of(m, "certificate-authority"))
            .map(str::to_string),
        client_certificate_request: mutual
            .and_then(|m| arg_of(m, "client-certificate-request"))
            .map(str::to_string),
        port: arg_of(ssl, "port").map(str::to_string),
        use_pki: has_leaf(ssl, "use-pki"),
    }
}

pub(super) fn parse_extension_service(service: &ConfigLine) -> ExtensionService {
    ExtensionService {
        grpc: child_block(service, "request-response")
            .and_then(|rr| child_block(rr, "grpc"))
            .map(|grpc| GrpcService {
                max_connections: arg_of(grpc, "max-connections").map(str::to_string),
                routing_instance: arg_of(grpc, "routing-instance").map(str::to_string),
                ssl: child_block(grpc, "ssl").map(parse_ssl),
            }),
        notification: child_block(service, "notification").map(|n| JetNotification {
            allow_clients: child_block(n, "allow-clients")
                .map(|clients| args_of(clients, "address").into_iter().map(str::to_string).collect())
                .unwrap_or_default(),
            broker_socket_send_buffer_size: arg_of(n, "broker-socket-send-buffer-size").map(str::to_string),
            max_connections: arg_of(n, "max-connections").map(str::to_string),
            port: arg_of(n, "port").map(str::to_string),
        }),
        traceoptions: parse_traceoptions(service),
    }
}

#[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_jet_service_settings() {
        let service = system(&juniper::parse_braces(
            r#"
system {
    services {
        extension-service {
            request-response {
                grpc {
                    max-connections 10;
                    routing-instance mgmt_junos;
                    ssl {
                        address 192.0.2.1;
                        hot-reloading;
                        local-certificate jet-cert;
                        mutual-authentication {
                            certificate-authority ca-profile;
                            client-certificate-request require-certificate;
                        }
                        port 32767;
                        use-pki;
                    }
                }
            }
            notification {
                allow-clients {
                    address 192.0.2.10;
                    address 192.0.2.11;
                }
                broker-socket-send-buffer-size 4096;
                max-connections 5;
                port 1883;
            }
            traceoptions {
                file jet.log;
                level error;
            }
        }
    }
}
"#,
        ))
        .unwrap()
        .services
        .unwrap()
        .extension_service
        .unwrap();
        let grpc = service.grpc.unwrap();
        assert_eq!(grpc.max_connections.as_deref(), Some("10"));
        assert_eq!(grpc.routing_instance.as_deref(), Some("mgmt_junos"));
        let ssl = grpc.ssl.unwrap();
        assert_eq!(ssl.address.as_deref(), Some("192.0.2.1"));
        assert!(ssl.hot_reloading);
        assert_eq!(ssl.local_certificate.as_deref(), Some("jet-cert"));
        assert_eq!(ssl.certificate_authority.as_deref(), Some("ca-profile"));
        assert_eq!(ssl.client_certificate_request.as_deref(), Some("require-certificate"));
        assert_eq!(ssl.port.as_deref(), Some("32767"));
        assert!(ssl.use_pki);
        let notification = service.notification.unwrap();
        assert_eq!(notification.allow_clients, ["192.0.2.10", "192.0.2.11"]);
        assert_eq!(notification.broker_socket_send_buffer_size.as_deref(), Some("4096"));
        assert_eq!(notification.max_connections.as_deref(), Some("5"));
        assert_eq!(notification.port.as_deref(), Some("1883"));
        assert_eq!(service.traceoptions.unwrap().level.as_deref(), Some("error"));
    }

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