use crate::{arg_of, child_block, secret_value, Secret};
use asterust::config::ConfigLine;
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct JebTls {
pub cert_bundle: Option<String>,
pub certificate: Option<String>,
pub key: Option<Secret>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct Jeb {
pub max_seed_size: Option<String>,
pub port: Option<String>,
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;
#[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());
}
}