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