Skip to main content

asterust_junos/system/services/
jeb.rs

1//! `jeb`
2//! `system { services { jeb { ... } } }`.
3//!
4//! # See also
5//!
6//! - [jeb](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/jeb-edit-system-services.html)
7
8use crate::{arg_of, child_block, secret_value, Secret};
9use asterust::config::ConfigLine;
10
11/// The `tls` block of a Juniper Entropy Beacon server. The `key` is read only with the `secrets` feature.
12#[derive(Clone, Debug, PartialEq, Eq)]
13#[non_exhaustive]
14pub struct JebTls {
15    pub cert_bundle: Option<String>,
16    pub certificate: Option<String>,
17    /// `None` unless the `secrets` feature is enabled.
18    pub key: Option<Secret>,
19}
20
21/// The `system services jeb` Juniper Entropy Beacon server.
22#[derive(Clone, Debug, PartialEq, Eq)]
23#[non_exhaustive]
24pub struct Jeb {
25    /// Bytes.
26    pub max_seed_size: Option<String>,
27    pub port: Option<String>,
28    /// `default-rng` or `hmac-drbg`.
29    pub rbg: Option<String>,
30    pub tls: Option<JebTls>,
31}
32
33pub(super) fn parse_jeb(jeb: &ConfigLine) -> Jeb {
34    Jeb {
35        max_seed_size: arg_of(jeb, "max-seed-size").map(str::to_string),
36        port: arg_of(jeb, "port").map(str::to_string),
37        rbg: arg_of(jeb, "rbg").map(str::to_string),
38        tls: child_block(jeb, "tls").map(|tls| JebTls {
39            cert_bundle: arg_of(tls, "cert-bundle").map(str::to_string),
40            certificate: arg_of(tls, "certificate").map(str::to_string),
41            key: secret_value(arg_of(tls, "key")),
42        }),
43    }
44}
45
46#[cfg(test)]
47mod tests {
48    use crate::system::system;
49    use asterust::config::juniper;
50
51    // Not from documentation: written from the CLI Reference syntax page. See the README "Sources".
52    #[test]
53    fn extracts_the_entropy_beacon_without_the_key() {
54        let jeb = system(&juniper::parse_braces(
55            r#"
56system {
57    services {
58        jeb {
59            max-seed-size 8192;
60            port 57005;
61            rbg hmac-drbg;
62            tls {
63                cert-bundle ca-bundle;
64                certificate jeb-cert;
65                key "$9$secret"; ## SECRET-DATA
66            }
67        }
68    }
69}
70"#,
71        ))
72        .unwrap()
73        .services
74        .unwrap()
75        .jeb
76        .unwrap();
77        assert_eq!(jeb.max_seed_size.as_deref(), Some("8192"));
78        assert_eq!(jeb.port.as_deref(), Some("57005"));
79        assert_eq!(jeb.rbg.as_deref(), Some("hmac-drbg"));
80        let tls = jeb.tls.unwrap();
81        assert_eq!(tls.cert_bundle.as_deref(), Some("ca-bundle"));
82        assert_eq!(tls.certificate.as_deref(), Some("jeb-cert"));
83        assert_eq!(
84            tls.key.as_ref().map(crate::Secret::expose),
85            crate::secret::expected("$9$secret")
86        );
87    }
88
89    #[test]
90    fn no_jeb_is_none() {
91        let services = system(&juniper::parse_braces(
92            "system {\n    services {\n        telnet;\n    }\n}\n",
93        ))
94        .unwrap()
95        .services
96        .unwrap();
97        assert!(services.jeb.is_none());
98    }
99}