asterust_junos/system/services/
web_management.rs1use crate::{arg_of, args_of, child_block, has_leaf, list_values, statement_words, value_after};
9use asterust::config::ConfigLine;
10
11#[derive(Clone, Debug, PartialEq, Eq)]
13#[non_exhaustive]
14pub struct WebManagementHttp {
15 pub interfaces: Vec<String>,
16 pub port: Option<String>,
17}
18
19#[derive(Clone, Debug, PartialEq, Eq)]
21#[non_exhaustive]
22pub struct WebManagementHttps {
23 pub interfaces: Vec<String>,
24 pub port: Option<String>,
25 pub local_certificate: Option<String>,
26 pub pki_local_certificate: Option<String>,
27 pub system_generated_certificate: bool,
28}
29
30#[derive(Clone, Debug, PartialEq, Eq)]
32#[non_exhaustive]
33pub struct WebManagement {
34 pub control_max_threads: Option<String>,
36 pub http: Option<WebManagementHttp>,
37 pub https: Option<WebManagementHttps>,
38 pub management_url: Option<String>,
39 pub session_idle_timeout: Option<String>,
41 pub session_limit: Option<String>,
42}
43
44pub(super) fn parse_web_management(web: &ConfigLine) -> WebManagement {
45 let interfaces =
46 |block: &ConfigLine| -> Vec<String> { args_of(block, "interface").into_iter().flat_map(list_values).collect() };
47 let session = child_block(web, "session");
48 WebManagement {
49 control_max_threads: statement_words(web, "control").and_then(|words| value_after(&words, "max-threads")),
50 http: child_block(web, "http").map(|http| WebManagementHttp {
51 interfaces: interfaces(http),
52 port: arg_of(http, "port").map(str::to_string),
53 }),
54 https: child_block(web, "https").map(|https| WebManagementHttps {
55 interfaces: interfaces(https),
56 port: arg_of(https, "port").map(str::to_string),
57 local_certificate: arg_of(https, "local-certificate").map(str::to_string),
58 pki_local_certificate: arg_of(https, "pki-local-certificate").map(str::to_string),
59 system_generated_certificate: has_leaf(https, "system-generated-certificate"),
60 }),
61 management_url: arg_of(web, "management-url").map(str::to_string),
62 session_idle_timeout: session.and_then(|s| arg_of(s, "idle-timeout")).map(str::to_string),
63 session_limit: session.and_then(|s| arg_of(s, "session-limit")).map(str::to_string),
64 }
65}
66
67#[cfg(test)]
68mod tests {
69 use crate::system::system;
70 use asterust::config::juniper;
71
72 #[test]
73 fn extracts_web_management_details() {
74 let web = system(&juniper::parse_braces(
75 r#"
76system {
77 services {
78 web-management {
79 control {
80 max-threads 8;
81 }
82 management-url /manage;
83 http {
84 interface [ fxp0.0 ge-0/0/1.0 ];
85 port 8080;
86 }
87 https {
88 interface fxp0.0;
89 port 8443;
90 pki-local-certificate J-WEB;
91 }
92 session {
93 idle-timeout 20;
94 session-limit 4;
95 }
96 }
97 }
98}
99"#,
100 ))
101 .unwrap()
102 .services
103 .unwrap()
104 .web_management
105 .unwrap();
106 assert_eq!(web.control_max_threads.as_deref(), Some("8"));
107 assert_eq!(web.management_url.as_deref(), Some("/manage"));
108 assert_eq!(web.session_idle_timeout.as_deref(), Some("20"));
109 assert_eq!(web.session_limit.as_deref(), Some("4"));
110
111 let http = web.http.unwrap();
112 assert_eq!(http.interfaces, ["fxp0.0", "ge-0/0/1.0"]);
113 assert_eq!(http.port.as_deref(), Some("8080"));
114
115 let https = web.https.unwrap();
116 assert_eq!(https.interfaces, ["fxp0.0"]);
117 assert_eq!(https.port.as_deref(), Some("8443"));
118 assert_eq!(https.pki_local_certificate.as_deref(), Some("J-WEB"));
119 assert_eq!(https.local_certificate, None);
120 assert!(!https.system_generated_certificate);
121 }
122
123 #[test]
124 fn web_management_with_system_generated_certificate() {
125 let web = system(&juniper::parse_braces(
126 "system {\n services {\n web-management {\n https {\n system-generated-certificate;\n }\n }\n }\n}\n",
127 ))
128 .unwrap()
129 .services
130 .unwrap()
131 .web_management
132 .unwrap();
133 assert!(web.https.unwrap().system_generated_certificate);
134 assert!(web.http.is_none());
135 assert_eq!(web.control_max_threads, None);
136 }
137}