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

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

/// The `tls` block of a Juniper Entropy Beacon server. The `key` is read only with the `secrets` feature.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct JebTls {
    pub cert_bundle: Option<String>,
    pub certificate: Option<String>,
    /// `None` unless the `secrets` feature is enabled.
    pub key: Option<Secret>,
}

/// The `system services jeb` Juniper Entropy Beacon server.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct Jeb {
    /// Bytes.
    pub max_seed_size: Option<String>,
    pub port: Option<String>,
    /// `default-rng` or `hmac-drbg`.
    pub rbg: Option<String>,
    pub tls: Option<JebTls>,
}

pub(super) fn parse_jeb(jeb: &ConfigLine) -> Jeb {
    Jeb {
        max_seed_size: arg_of(jeb, "max-seed-size").map(str::to_string),
        port: arg_of(jeb, "port").map(str::to_string),
        rbg: arg_of(jeb, "rbg").map(str::to_string),
        tls: child_block(jeb, "tls").map(|tls| JebTls {
            cert_bundle: arg_of(tls, "cert-bundle").map(str::to_string),
            certificate: arg_of(tls, "certificate").map(str::to_string),
            key: secret_value(arg_of(tls, "key")),
        }),
    }
}

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

    // Not from documentation: written from the CLI Reference syntax page. See the README "Sources".
    #[test]
    fn extracts_the_entropy_beacon_without_the_key() {
        let jeb = system(&juniper::parse_braces(
            r#"
system {
    services {
        jeb {
            max-seed-size 8192;
            port 57005;
            rbg hmac-drbg;
            tls {
                cert-bundle ca-bundle;
                certificate jeb-cert;
                key "$9$secret"; ## SECRET-DATA
            }
        }
    }
}
"#,
        ))
        .unwrap()
        .services
        .unwrap()
        .jeb
        .unwrap();
        assert_eq!(jeb.max_seed_size.as_deref(), Some("8192"));
        assert_eq!(jeb.port.as_deref(), Some("57005"));
        assert_eq!(jeb.rbg.as_deref(), Some("hmac-drbg"));
        let tls = jeb.tls.unwrap();
        assert_eq!(tls.cert_bundle.as_deref(), Some("ca-bundle"));
        assert_eq!(tls.certificate.as_deref(), Some("jeb-cert"));
        assert_eq!(
            tls.key.as_ref().map(crate::Secret::expose),
            crate::secret::expected("$9$secret")
        );
    }

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