Skip to main content

asterust_junos/system/services/
rest.rs

1//! `rest`
2//! `system { services { rest { ... } } }`.
3//!
4//! # See also
5//!
6//! - [rest](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/rest-edit-system-services.html)
7//! - [http (REST API)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/http-edit-system-services-rest.html)
8//! - [https (REST API)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/https-edit-system-services-rest.html)
9//! - [allowed-sources (REST API)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/allowed-sources-edit-system-services-rest.html)
10//! - [enable-explorer (REST API)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/enable-explorer-edit-system-services-rest.html)
11//! - [cipher-list (REST API)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/cipher-list-edit-system-services-rest-https.html)
12//! - [mutual-authentication (REST API)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/mutual-authentication-edit-system-services-rest-https.html)
13//! - [ca-chain (REST API)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/ca-chain.html)
14//! - [server-certificate (REST API)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/server-certificate-edit-system-services-rest-https.html)
15
16use crate::protocols::traceoptions::{parse_traceoptions, Traceoptions};
17use crate::{arg_of, args_of, child_block, has_leaf, list_values};
18use asterust::config::ConfigLine;
19
20/// The `rest http` block.
21#[derive(Clone, Debug, PartialEq, Eq)]
22#[non_exhaustive]
23pub struct RestHttp {
24    pub addresses: Vec<String>,
25    pub port: Option<String>,
26}
27
28/// The `rest https` block.
29#[derive(Clone, Debug, PartialEq, Eq)]
30#[non_exhaustive]
31pub struct RestHttps {
32    pub addresses: Vec<String>,
33    pub cipher_list: Vec<String>,
34    /// From `mutual-authentication { certificate-authority }`.
35    pub certificate_authority: Option<String>,
36    pub ca_chain: Option<String>,
37    pub port: Option<String>,
38    pub server_certificate: Option<String>,
39}
40
41/// The `system services rest` settings.
42#[derive(Clone, Debug, PartialEq, Eq)]
43#[non_exhaustive]
44pub struct Rest {
45    /// From `control allowed-sources`.
46    pub allowed_sources: Vec<String>,
47    /// From `control connection-limit`.
48    pub connection_limit: Option<String>,
49    pub enable_explorer: bool,
50    pub http: Option<RestHttp>,
51    pub https: Option<RestHttps>,
52    pub routing_instance: Option<String>,
53    pub traceoptions: Option<Traceoptions>,
54}
55
56fn addresses(block: &ConfigLine, keyword: &str) -> Vec<String> {
57    args_of(block, keyword).into_iter().flat_map(list_values).collect()
58}
59
60pub(super) fn parse_rest(rest: &ConfigLine) -> Rest {
61    let control = child_block(rest, "control");
62    Rest {
63        allowed_sources: control.map(|c| addresses(c, "allowed-sources")).unwrap_or_default(),
64        connection_limit: control.and_then(|c| arg_of(c, "connection-limit")).map(str::to_string),
65        enable_explorer: has_leaf(rest, "enable-explorer"),
66        http: child_block(rest, "http").map(|http| RestHttp {
67            addresses: addresses(http, "addresses"),
68            port: arg_of(http, "port").map(str::to_string),
69        }),
70        https: child_block(rest, "https").map(|https| RestHttps {
71            addresses: addresses(https, "addresses"),
72            cipher_list: addresses(https, "cipher-list"),
73            certificate_authority: child_block(https, "mutual-authentication")
74                .and_then(|m| arg_of(m, "certificate-authority"))
75                .map(str::to_string),
76            ca_chain: arg_of(https, "ca-chain").map(str::to_string),
77            port: arg_of(https, "port").map(str::to_string),
78            server_certificate: arg_of(https, "server-certificate").map(str::to_string),
79        }),
80        routing_instance: arg_of(rest, "routing-instance").map(str::to_string),
81        traceoptions: parse_traceoptions(rest),
82    }
83}
84
85#[cfg(test)]
86mod tests {
87    use crate::system::system;
88    use asterust::config::juniper;
89
90    // Not from documentation: written from the CLI Reference syntax pages. See the README "Sources".
91    #[test]
92    fn extracts_the_rest_api_settings() {
93        let rest = system(&juniper::parse_braces(
94            r#"
95system {
96    services {
97        rest {
98            control {
99                allowed-sources [ 192.0.2.0/24 198.51.100.7 ];
100                connection-limit 100;
101            }
102            enable-explorer;
103            http {
104                addresses 192.0.2.1;
105                port 3000;
106            }
107            https {
108                addresses [ 192.0.2.1 192.0.2.2 ];
109                cipher-list [ rsa-with-aes-128-cbc-sha dhe-rsa-with-aes-256-cbc-sha ];
110                mutual-authentication {
111                    certificate-authority ca-profile;
112                }
113                port 3443;
114                server-certificate rest-cert;
115                ca-chain chain-profile;
116            }
117            routing-instance mgmt_junos;
118            traceoptions {
119                flag all;
120            }
121        }
122    }
123}
124"#,
125        ))
126        .unwrap()
127        .services
128        .unwrap()
129        .rest
130        .unwrap();
131        assert_eq!(rest.allowed_sources, ["192.0.2.0/24", "198.51.100.7"]);
132        assert_eq!(rest.connection_limit.as_deref(), Some("100"));
133        assert!(rest.enable_explorer);
134        let http = rest.http.unwrap();
135        assert_eq!(http.addresses, ["192.0.2.1"]);
136        assert_eq!(http.port.as_deref(), Some("3000"));
137        let https = rest.https.unwrap();
138        assert_eq!(https.addresses, ["192.0.2.1", "192.0.2.2"]);
139        assert_eq!(https.cipher_list.len(), 2);
140        assert_eq!(https.certificate_authority.as_deref(), Some("ca-profile"));
141        assert_eq!(https.port.as_deref(), Some("3443"));
142        assert_eq!(https.server_certificate.as_deref(), Some("rest-cert"));
143        assert_eq!(https.ca_chain.as_deref(), Some("chain-profile"));
144        assert_eq!(rest.routing_instance.as_deref(), Some("mgmt_junos"));
145        assert_eq!(rest.traceoptions.unwrap().flags[0].name, "all");
146    }
147
148    #[test]
149    fn bare_rest_has_no_settings() {
150        let rest = system(&juniper::parse_braces(
151            "system {\n    services {\n        rest;\n    }\n}\n",
152        ))
153        .unwrap()
154        .services
155        .unwrap()
156        .rest
157        .unwrap();
158        assert!(rest.allowed_sources.is_empty());
159        assert!(!rest.enable_explorer);
160        assert!(rest.http.is_none());
161        assert!(rest.https.is_none());
162    }
163
164    #[test]
165    fn no_rest_is_none() {
166        let services = system(&juniper::parse_braces(
167            "system {\n    services {\n        telnet;\n    }\n}\n",
168        ))
169        .unwrap()
170        .services
171        .unwrap();
172        assert!(services.rest.is_none());
173    }
174}