asterust_junos/system/services/
webapi.rs1use crate::{active_children, arg_of, args_of, child_block, has_leaf, secret_value, Secret};
16use asterust::config::ConfigLine;
17
18#[derive(Clone, Debug, PartialEq, Eq)]
20#[non_exhaustive]
21pub struct WebapiHttps {
22 pub certificate: Option<String>,
23 pub certificate_key: Option<String>,
24 pub default_certificate: bool,
25 pub pki_local_certificate: Option<String>,
26 pub port: Option<String>,
27}
28
29#[derive(Clone, Debug, PartialEq, Eq)]
31#[non_exhaustive]
32pub struct WebapiUser {
33 pub name: Option<String>,
34 pub password: Option<Secret>,
36}
37
38#[derive(Clone, Debug, PartialEq, Eq)]
40#[non_exhaustive]
41pub struct Webapi {
42 pub clients: Vec<String>,
44 pub debug_level: Option<String>,
46 pub debug_log: Option<String>,
47 pub http: bool,
49 pub http_port: Option<String>,
50 pub https: Option<WebapiHttps>,
51 pub user: Option<WebapiUser>,
52}
53
54pub(super) fn parse_webapi(webapi: &ConfigLine) -> Webapi {
55 let http = child_block(webapi, "http");
56 Webapi {
57 clients: args_of(webapi, "client").into_iter().map(str::to_string).collect(),
58 debug_level: arg_of(webapi, "debug-level").map(str::to_string),
59 debug_log: arg_of(webapi, "debug-log").map(str::to_string),
60 http: http.is_some(),
61 http_port: http.and_then(|h| arg_of(h, "port")).map(str::to_string),
62 https: child_block(webapi, "https").map(|https| WebapiHttps {
63 certificate: arg_of(https, "certificate").map(str::to_string),
64 certificate_key: arg_of(https, "certificate-key").map(str::to_string),
65 default_certificate: has_leaf(https, "default-certificate"),
66 pki_local_certificate: arg_of(https, "pki-local-certificate").map(str::to_string),
67 port: arg_of(https, "port").map(str::to_string),
68 }),
69 user: active_children(webapi)
70 .find(|c| c.text == "user" || c.arg("user").is_some())
71 .map(|user| WebapiUser {
72 name: user
74 .arg("user")
75 .map(|name| name.trim_matches('"').to_string())
76 .or_else(|| {
77 active_children(user)
78 .find(|c| !c.text.starts_with("password"))
79 .map(|c| c.text.trim_matches('"').to_string())
80 }),
81 password: secret_value(arg_of(user, "password")),
82 }),
83 }
84}
85
86#[cfg(test)]
87mod tests {
88 use crate::system::system;
89 use asterust::config::juniper;
90
91 #[test]
93 fn extracts_the_web_api_without_the_password() {
94 let webapi = system(&juniper::parse_braces(
95 r#"
96system {
97 services {
98 webapi {
99 client 192.0.2.60;
100 debug-level warn;
101 debug-log webapi.log;
102 http {
103 port 8080;
104 }
105 https {
106 certificate api-cert;
107 certificate-key api-key;
108 pki-local-certificate api-pki;
109 port 8443;
110 }
111 user cppm {
112 password "$9$secret"; ## SECRET-DATA
113 }
114 }
115 }
116}
117"#,
118 ))
119 .unwrap()
120 .services
121 .unwrap()
122 .webapi
123 .unwrap();
124 assert_eq!(webapi.clients, ["192.0.2.60"]);
125 assert_eq!(webapi.debug_level.as_deref(), Some("warn"));
126 assert_eq!(webapi.debug_log.as_deref(), Some("webapi.log"));
127 assert!(webapi.http);
128 assert_eq!(webapi.http_port.as_deref(), Some("8080"));
129 let https = webapi.https.unwrap();
130 assert_eq!(https.certificate.as_deref(), Some("api-cert"));
131 assert_eq!(https.certificate_key.as_deref(), Some("api-key"));
132 assert!(!https.default_certificate);
133 assert_eq!(https.pki_local_certificate.as_deref(), Some("api-pki"));
134 assert_eq!(https.port.as_deref(), Some("8443"));
135 let user = webapi.user.unwrap();
136 assert_eq!(user.name.as_deref(), Some("cppm"));
137 assert_eq!(
138 user.password.as_ref().map(crate::Secret::expose),
139 crate::secret::expected("$9$secret")
140 );
141 }
142
143 #[test]
144 fn reads_the_user_name_written_inside_the_block() {
145 let user = system(&juniper::parse_braces(
146 "system {\n services {\n webapi {\n user {\n cppm;\n }\n https {\n default-certificate;\n }\n }\n }\n}\n",
147 ))
148 .unwrap()
149 .services
150 .unwrap()
151 .webapi
152 .unwrap();
153 assert_eq!(user.user.unwrap().name.as_deref(), Some("cppm"));
154 assert!(user.https.unwrap().default_certificate);
155 }
156
157 #[test]
158 fn no_webapi_is_none() {
159 let services = system(&juniper::parse_braces(
160 "system {\n services {\n telnet;\n }\n}\n",
161 ))
162 .unwrap()
163 .services
164 .unwrap();
165 assert!(services.webapi.is_none());
166 }
167}