use crate::{active_children, arg_of, args_of, child_block, has_leaf, secret_value, Secret};
use asterust::config::ConfigLine;
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct WebapiHttps {
pub certificate: Option<String>,
pub certificate_key: Option<String>,
pub default_certificate: bool,
pub pki_local_certificate: Option<String>,
pub port: Option<String>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct WebapiUser {
pub name: Option<String>,
pub password: Option<Secret>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct Webapi {
pub clients: Vec<String>,
pub debug_level: Option<String>,
pub debug_log: Option<String>,
pub http: bool,
pub http_port: Option<String>,
pub https: Option<WebapiHttps>,
pub user: Option<WebapiUser>,
}
pub(super) fn parse_webapi(webapi: &ConfigLine) -> Webapi {
let http = child_block(webapi, "http");
Webapi {
clients: args_of(webapi, "client").into_iter().map(str::to_string).collect(),
debug_level: arg_of(webapi, "debug-level").map(str::to_string),
debug_log: arg_of(webapi, "debug-log").map(str::to_string),
http: http.is_some(),
http_port: http.and_then(|h| arg_of(h, "port")).map(str::to_string),
https: child_block(webapi, "https").map(|https| WebapiHttps {
certificate: arg_of(https, "certificate").map(str::to_string),
certificate_key: arg_of(https, "certificate-key").map(str::to_string),
default_certificate: has_leaf(https, "default-certificate"),
pki_local_certificate: arg_of(https, "pki-local-certificate").map(str::to_string),
port: arg_of(https, "port").map(str::to_string),
}),
user: active_children(webapi)
.find(|c| c.text == "user" || c.arg("user").is_some())
.map(|user| WebapiUser {
name: user
.arg("user")
.map(|name| name.trim_matches('"').to_string())
.or_else(|| {
active_children(user)
.find(|c| !c.text.starts_with("password"))
.map(|c| c.text.trim_matches('"').to_string())
}),
password: secret_value(arg_of(user, "password")),
}),
}
}
#[cfg(test)]
mod tests {
use crate::system::system;
use asterust::config::juniper;
#[test]
fn extracts_the_web_api_without_the_password() {
let webapi = system(&juniper::parse_braces(
r#"
system {
services {
webapi {
client 192.0.2.60;
debug-level warn;
debug-log webapi.log;
http {
port 8080;
}
https {
certificate api-cert;
certificate-key api-key;
pki-local-certificate api-pki;
port 8443;
}
user cppm {
password "$9$secret"; ## SECRET-DATA
}
}
}
}
"#,
))
.unwrap()
.services
.unwrap()
.webapi
.unwrap();
assert_eq!(webapi.clients, ["192.0.2.60"]);
assert_eq!(webapi.debug_level.as_deref(), Some("warn"));
assert_eq!(webapi.debug_log.as_deref(), Some("webapi.log"));
assert!(webapi.http);
assert_eq!(webapi.http_port.as_deref(), Some("8080"));
let https = webapi.https.unwrap();
assert_eq!(https.certificate.as_deref(), Some("api-cert"));
assert_eq!(https.certificate_key.as_deref(), Some("api-key"));
assert!(!https.default_certificate);
assert_eq!(https.pki_local_certificate.as_deref(), Some("api-pki"));
assert_eq!(https.port.as_deref(), Some("8443"));
let user = webapi.user.unwrap();
assert_eq!(user.name.as_deref(), Some("cppm"));
assert_eq!(
user.password.as_ref().map(crate::Secret::expose),
crate::secret::expected("$9$secret")
);
}
#[test]
fn reads_the_user_name_written_inside_the_block() {
let user = system(&juniper::parse_braces(
"system {\n services {\n webapi {\n user {\n cppm;\n }\n https {\n default-certificate;\n }\n }\n }\n}\n",
))
.unwrap()
.services
.unwrap()
.webapi
.unwrap();
assert_eq!(user.user.unwrap().name.as_deref(), Some("cppm"));
assert!(user.https.unwrap().default_certificate);
}
#[test]
fn no_webapi_is_none() {
let services = system(&juniper::parse_braces(
"system {\n services {\n telnet;\n }\n}\n",
))
.unwrap()
.services
.unwrap();
assert!(services.webapi.is_none());
}
}