Skip to main content

asterust_junos/system/services/
web_management.rs

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