use crate::protocols::traceoptions::{parse_traceoptions, Traceoptions};
use crate::{arg_of, args_of, child_block, has_leaf, list_values};
use asterust::config::ConfigLine;
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct RestHttp {
pub addresses: Vec<String>,
pub port: Option<String>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct RestHttps {
pub addresses: Vec<String>,
pub cipher_list: Vec<String>,
pub certificate_authority: Option<String>,
pub port: Option<String>,
pub server_certificate: Option<String>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct Rest {
pub allowed_sources: Vec<String>,
pub connection_limit: Option<String>,
pub enable_explorer: bool,
pub http: Option<RestHttp>,
pub https: Option<RestHttps>,
pub routing_instance: Option<String>,
pub traceoptions: Option<Traceoptions>,
}
fn addresses(block: &ConfigLine, keyword: &str) -> Vec<String> {
args_of(block, keyword).into_iter().flat_map(list_values).collect()
}
pub(super) fn parse_rest(rest: &ConfigLine) -> Rest {
let control = child_block(rest, "control");
Rest {
allowed_sources: control.map(|c| addresses(c, "allowed-sources")).unwrap_or_default(),
connection_limit: control.and_then(|c| arg_of(c, "connection-limit")).map(str::to_string),
enable_explorer: has_leaf(rest, "enable-explorer"),
http: child_block(rest, "http").map(|http| RestHttp {
addresses: addresses(http, "addresses"),
port: arg_of(http, "port").map(str::to_string),
}),
https: child_block(rest, "https").map(|https| RestHttps {
addresses: addresses(https, "addresses"),
cipher_list: addresses(https, "cipher-list"),
certificate_authority: child_block(https, "mutual-authentication")
.and_then(|m| arg_of(m, "certificate-authority"))
.map(str::to_string),
port: arg_of(https, "port").map(str::to_string),
server_certificate: arg_of(https, "server-certificate").map(str::to_string),
}),
routing_instance: arg_of(rest, "routing-instance").map(str::to_string),
traceoptions: parse_traceoptions(rest),
}
}
#[cfg(test)]
mod tests {
use crate::system::system;
use asterust::config::juniper;
#[test]
fn extracts_the_rest_api_settings() {
let rest = system(&juniper::parse_braces(
r#"
system {
services {
rest {
control {
allowed-sources [ 192.0.2.0/24 198.51.100.7 ];
connection-limit 100;
}
enable-explorer;
http {
addresses 192.0.2.1;
port 3000;
}
https {
addresses [ 192.0.2.1 192.0.2.2 ];
cipher-list [ rsa-with-aes-128-cbc-sha dhe-rsa-with-aes-256-cbc-sha ];
mutual-authentication {
certificate-authority ca-profile;
}
port 3443;
server-certificate rest-cert;
}
routing-instance mgmt_junos;
traceoptions {
flag all;
}
}
}
}
"#,
))
.unwrap()
.services
.unwrap()
.rest
.unwrap();
assert_eq!(rest.allowed_sources, ["192.0.2.0/24", "198.51.100.7"]);
assert_eq!(rest.connection_limit.as_deref(), Some("100"));
assert!(rest.enable_explorer);
let http = rest.http.unwrap();
assert_eq!(http.addresses, ["192.0.2.1"]);
assert_eq!(http.port.as_deref(), Some("3000"));
let https = rest.https.unwrap();
assert_eq!(https.addresses, ["192.0.2.1", "192.0.2.2"]);
assert_eq!(https.cipher_list.len(), 2);
assert_eq!(https.certificate_authority.as_deref(), Some("ca-profile"));
assert_eq!(https.port.as_deref(), Some("3443"));
assert_eq!(https.server_certificate.as_deref(), Some("rest-cert"));
assert_eq!(rest.routing_instance.as_deref(), Some("mgmt_junos"));
assert_eq!(rest.traceoptions.unwrap().flags[0].name, "all");
}
#[test]
fn bare_rest_has_no_settings() {
let rest = system(&juniper::parse_braces(
"system {\n services {\n rest;\n }\n}\n",
))
.unwrap()
.services
.unwrap()
.rest
.unwrap();
assert!(rest.allowed_sources.is_empty());
assert!(!rest.enable_explorer);
assert!(rest.http.is_none());
assert!(rest.https.is_none());
}
#[test]
fn no_rest_is_none() {
let services = system(&juniper::parse_braces(
"system {\n services {\n telnet;\n }\n}\n",
))
.unwrap()
.services
.unwrap();
assert!(services.rest.is_none());
}
}