Skip to main content

asterust_junos/system/services/
web_management.rs

1//! `web-management`
2//! `system { services { web-management { ... } } }`.
3//!
4//! # See also
5//!
6//! - [web-management (System Services)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/web-management-edit-system.html)
7//! - [https (Web Management)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/https-edit-system.html)
8
9use crate::{arg_of, args_of, child_block, has_leaf, list_values, named_children, statement_words, value_after};
10use asterust::config::ConfigLine;
11
12/// A `virtual-domain <name>` of the `web-management https` block.
13#[derive(Clone, Debug, PartialEq, Eq)]
14#[non_exhaustive]
15pub struct WebManagementVirtualDomain {
16    pub name: String,
17    pub pki_local_certificate: Option<String>,
18}
19
20/// The `web-management http` block.
21#[derive(Clone, Debug, PartialEq, Eq)]
22#[non_exhaustive]
23pub struct WebManagementHttp {
24    pub interfaces: Vec<String>,
25    pub port: Option<String>,
26}
27
28/// The `web-management https` block.
29#[derive(Clone, Debug, PartialEq, Eq)]
30#[non_exhaustive]
31pub struct WebManagementHttps {
32    pub interfaces: Vec<String>,
33    pub port: Option<String>,
34    pub local_certificate: Option<String>,
35    pub pki_local_certificate: Option<String>,
36    pub system_generated_certificate: bool,
37    pub virtual_domains: Vec<WebManagementVirtualDomain>,
38}
39
40/// The `system services web-management` settings.
41#[derive(Clone, Debug, PartialEq, Eq)]
42#[non_exhaustive]
43pub struct WebManagement {
44    /// From `control max-threads`.
45    pub control_max_threads: Option<String>,
46    pub http: Option<WebManagementHttp>,
47    pub https: Option<WebManagementHttps>,
48    pub management_url: Option<String>,
49    /// Minutes, from `session idle-timeout`.
50    pub session_idle_timeout: Option<String>,
51    pub session_limit: Option<String>,
52}
53
54pub(super) fn parse_web_management(web: &ConfigLine) -> WebManagement {
55    let interfaces =
56        |block: &ConfigLine| -> Vec<String> { args_of(block, "interface").into_iter().flat_map(list_values).collect() };
57    let session = child_block(web, "session");
58    WebManagement {
59        control_max_threads: statement_words(web, "control").and_then(|words| value_after(&words, "max-threads")),
60        http: child_block(web, "http").map(|http| WebManagementHttp {
61            interfaces: interfaces(http),
62            port: arg_of(http, "port").map(str::to_string),
63        }),
64        https: child_block(web, "https").map(|https| WebManagementHttps {
65            interfaces: interfaces(https),
66            port: arg_of(https, "port").map(str::to_string),
67            local_certificate: arg_of(https, "local-certificate").map(str::to_string),
68            pki_local_certificate: arg_of(https, "pki-local-certificate").map(str::to_string),
69            system_generated_certificate: has_leaf(https, "system-generated-certificate"),
70            virtual_domains: named_children(https, "virtual-domain")
71                .map(|(name, domain)| WebManagementVirtualDomain {
72                    name: name.to_string(),
73                    pki_local_certificate: arg_of(domain, "pki-local-certificate").map(str::to_string),
74                })
75                .collect(),
76        }),
77        management_url: arg_of(web, "management-url").map(str::to_string),
78        session_idle_timeout: session.and_then(|s| arg_of(s, "idle-timeout")).map(str::to_string),
79        session_limit: session.and_then(|s| arg_of(s, "session-limit")).map(str::to_string),
80    }
81}
82
83#[cfg(test)]
84mod tests {
85    use crate::system::system;
86    use asterust::config::juniper;
87
88    #[test]
89    fn extracts_web_management_details() {
90        let web = system(&juniper::parse_braces(
91            r#"
92system {
93    services {
94        web-management {
95            control {
96                max-threads 8;
97            }
98            management-url /manage;
99            http {
100                interface [ fxp0.0 ge-0/0/1.0 ];
101                port 8080;
102            }
103            https {
104                interface fxp0.0;
105                port 8443;
106                pki-local-certificate J-WEB;
107            }
108            session {
109                idle-timeout 20;
110                session-limit 4;
111            }
112        }
113    }
114}
115"#,
116        ))
117        .unwrap()
118        .services
119        .unwrap()
120        .web_management
121        .unwrap();
122        assert_eq!(web.control_max_threads.as_deref(), Some("8"));
123        assert_eq!(web.management_url.as_deref(), Some("/manage"));
124        assert_eq!(web.session_idle_timeout.as_deref(), Some("20"));
125        assert_eq!(web.session_limit.as_deref(), Some("4"));
126
127        let http = web.http.unwrap();
128        assert_eq!(http.interfaces, ["fxp0.0", "ge-0/0/1.0"]);
129        assert_eq!(http.port.as_deref(), Some("8080"));
130
131        let https = web.https.unwrap();
132        assert_eq!(https.interfaces, ["fxp0.0"]);
133        assert_eq!(https.port.as_deref(), Some("8443"));
134        assert_eq!(https.pki_local_certificate.as_deref(), Some("J-WEB"));
135        assert_eq!(https.local_certificate, None);
136        assert!(!https.system_generated_certificate);
137    }
138
139    #[test]
140    fn web_management_with_system_generated_certificate() {
141        let web = system(&juniper::parse_braces(
142            "system {\n    services {\n        web-management {\n            https {\n                system-generated-certificate;\n            }\n        }\n    }\n}\n",
143        ))
144        .unwrap()
145        .services
146        .unwrap()
147        .web_management
148        .unwrap();
149        assert!(web.https.unwrap().system_generated_certificate);
150        assert!(web.http.is_none());
151        assert_eq!(web.control_max_threads, None);
152    }
153
154    // Not from documentation: written from the CLI Reference syntax page. See the README "Sources".
155    #[test]
156    fn extracts_https_virtual_domains() {
157        let https = system(&juniper::parse_braces(
158            "system {\n    services {\n        web-management {\n            https {\n                virtual-domain portal {\n                    pki-local-certificate portal-cert;\n                }\n                virtual-domain bare;\n            }\n        }\n    }\n}\n",
159        ))
160        .unwrap()
161        .services
162        .unwrap()
163        .web_management
164        .unwrap()
165        .https
166        .unwrap();
167        assert_eq!(https.virtual_domains.len(), 2);
168        assert_eq!(https.virtual_domains[0].name, "portal");
169        assert_eq!(
170            https.virtual_domains[0].pki_local_certificate.as_deref(),
171            Some("portal-cert")
172        );
173        assert_eq!(https.virtual_domains[1].pki_local_certificate, None);
174    }
175}