Skip to main content

asterust_junos/system/services/
http_servers.rs

1//! `http`
2//! `system { services { http { servers { server <name> { ... } } } } }`.
3//!
4//! # See also
5//!
6//! - [http (System Services)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/http-edit-system-services.html)
7//! - [server (HTTP Servers)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/server-edit-system-services-http-servers.html)
8//! - [tls (HTTP Servers)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/tls-edit-system-services-http-servers-server.html)
9
10use crate::{arg_of, args_of, child_block, list_values, named_children};
11use asterust::config::ConfigLine;
12
13/// The `tls` block of an HTTP server.
14#[derive(Clone, Debug, PartialEq, Eq)]
15#[non_exhaustive]
16pub struct HttpServerTls {
17    pub local_certificate: Option<String>,
18    /// From `mutual-authentication { authentication-type }`, e.g. `request-cert-and-verify`.
19    pub authentication_type: Option<String>,
20    /// From `mutual-authentication { certificate-authority }`.
21    pub certificate_authority: Option<String>,
22}
23
24/// A `server <name>` of `system services http servers`.
25#[derive(Clone, Debug, PartialEq, Eq)]
26#[non_exhaustive]
27pub struct HttpServer {
28    pub name: String,
29    /// The gRPC services the server exposes.
30    pub grpc_services: Vec<String>,
31    pub listen_address: Option<String>,
32    pub max_connections: Option<String>,
33    pub port: Option<String>,
34    pub routing_instance: Option<String>,
35    pub tls: Option<HttpServerTls>,
36}
37
38pub(super) fn parse_http_servers(http: &ConfigLine) -> Vec<HttpServer> {
39    child_block(http, "servers")
40        .map(|servers| {
41            named_children(servers, "server")
42                .map(|(name, server)| HttpServer {
43                    name: name.to_string(),
44                    grpc_services: args_of(server, "grpc").into_iter().flat_map(list_values).collect(),
45                    listen_address: arg_of(server, "listen-address").map(str::to_string),
46                    max_connections: arg_of(server, "max-connections").map(str::to_string),
47                    port: arg_of(server, "port").map(str::to_string),
48                    routing_instance: arg_of(server, "routing-instance").map(str::to_string),
49                    tls: child_block(server, "tls").map(|tls| {
50                        let mutual = child_block(tls, "mutual-authentication");
51                        HttpServerTls {
52                            local_certificate: arg_of(tls, "local-certificate").map(str::to_string),
53                            authentication_type: mutual
54                                .and_then(|m| arg_of(m, "authentication-type"))
55                                .map(str::to_string),
56                            certificate_authority: mutual
57                                .and_then(|m| arg_of(m, "certificate-authority"))
58                                .map(str::to_string),
59                        }
60                    }),
61                })
62                .collect()
63        })
64        .unwrap_or_default()
65}
66
67#[cfg(test)]
68mod tests {
69    use crate::system::system;
70    use asterust::config::juniper;
71
72    // Not from documentation: written from the CLI Reference syntax pages. See the README "Sources".
73    #[test]
74    fn extracts_http_servers() {
75        let servers = system(&juniper::parse_braces(
76            r#"
77system {
78    services {
79        http {
80            servers {
81                server gnmi {
82                    grpc [ gnmi gnoi ];
83                    listen-address 192.0.2.1;
84                    max-connections 20;
85                    port 9339;
86                    routing-instance mgmt_junos;
87                    tls {
88                        local-certificate gnmi-cert;
89                        mutual-authentication {
90                            authentication-type request-cert-and-verify;
91                            certificate-authority ca-profile;
92                        }
93                    }
94                }
95                server plain {
96                    port 8080;
97                }
98            }
99        }
100    }
101}
102"#,
103        ))
104        .unwrap()
105        .services
106        .unwrap()
107        .http_servers;
108        assert_eq!(servers.len(), 2);
109        let gnmi = &servers[0];
110        assert_eq!(gnmi.name, "gnmi");
111        assert_eq!(gnmi.grpc_services, ["gnmi", "gnoi"]);
112        assert_eq!(gnmi.listen_address.as_deref(), Some("192.0.2.1"));
113        assert_eq!(gnmi.max_connections.as_deref(), Some("20"));
114        assert_eq!(gnmi.port.as_deref(), Some("9339"));
115        assert_eq!(gnmi.routing_instance.as_deref(), Some("mgmt_junos"));
116        let tls = gnmi.tls.as_ref().unwrap();
117        assert_eq!(tls.local_certificate.as_deref(), Some("gnmi-cert"));
118        assert_eq!(tls.authentication_type.as_deref(), Some("request-cert-and-verify"));
119        assert_eq!(tls.certificate_authority.as_deref(), Some("ca-profile"));
120        assert!(servers[1].tls.is_none());
121        assert!(servers[1].grpc_services.is_empty());
122    }
123
124    #[test]
125    fn no_http_servers_is_empty() {
126        let services = system(&juniper::parse_braces(
127            "system {\n    services {\n        telnet;\n    }\n}\n",
128        ))
129        .unwrap()
130        .services
131        .unwrap();
132        assert!(services.http_servers.is_empty());
133    }
134}