Skip to main content

asterust_junos/system/services/
extension_service.rs

1//! `extension-service`
2//! `system { services { extension-service { ... } } }`.
3//!
4//! Not to be confused with `system extensions extension-service`, which defines the JET applications.
5//!
6//! # See also
7//!
8//! - [request-response](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/request-response-edit-system-services-jet.html)
9//! - [grpc](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/grpc-edit-system-jet.html)
10//! - [ssl (gRPC)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/ssl-edit-system-services-grpc-jet.html)
11//! - [notification (System Services)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/notification-edit-system-services-jet.html)
12//! - [traceoptions (Services)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/traceoptions-edit-system-services-jet.html)
13
14use crate::protocols::traceoptions::{parse_traceoptions, Traceoptions};
15use crate::{arg_of, args_of, child_block, has_leaf};
16use asterust::config::ConfigLine;
17
18/// The `ssl` block of the gRPC request-response service.
19#[derive(Clone, Debug, PartialEq, Eq)]
20#[non_exhaustive]
21pub struct GrpcSsl {
22    pub address: Option<String>,
23    pub hot_reloading: bool,
24    pub local_certificate: Option<String>,
25    /// From `mutual-authentication { certificate-authority }`.
26    pub certificate_authority: Option<String>,
27    /// From `mutual-authentication { client-certificate-request }`, e.g. `require-certificate`.
28    pub client_certificate_request: Option<String>,
29    pub port: Option<String>,
30    pub use_pki: bool,
31}
32
33/// The `request-response grpc` block.
34#[derive(Clone, Debug, PartialEq, Eq)]
35#[non_exhaustive]
36pub struct GrpcService {
37    pub max_connections: Option<String>,
38    pub routing_instance: Option<String>,
39    pub ssl: Option<GrpcSsl>,
40}
41
42/// The `notification` block.
43#[derive(Clone, Debug, PartialEq, Eq)]
44#[non_exhaustive]
45pub struct JetNotification {
46    /// From `allow-clients { address <ip>; }`.
47    pub allow_clients: Vec<String>,
48    pub broker_socket_send_buffer_size: Option<String>,
49    pub max_connections: Option<String>,
50    pub port: Option<String>,
51}
52
53/// The `system services extension-service` settings of the JET service process.
54#[derive(Clone, Debug, PartialEq, Eq)]
55#[non_exhaustive]
56pub struct ExtensionService {
57    /// From `request-response { grpc { ... } }`.
58    pub grpc: Option<GrpcService>,
59    pub notification: Option<JetNotification>,
60    pub traceoptions: Option<Traceoptions>,
61}
62
63fn parse_ssl(ssl: &ConfigLine) -> GrpcSsl {
64    let mutual = child_block(ssl, "mutual-authentication");
65    GrpcSsl {
66        address: arg_of(ssl, "address").map(str::to_string),
67        hot_reloading: has_leaf(ssl, "hot-reloading"),
68        local_certificate: arg_of(ssl, "local-certificate").map(str::to_string),
69        certificate_authority: mutual
70            .and_then(|m| arg_of(m, "certificate-authority"))
71            .map(str::to_string),
72        client_certificate_request: mutual
73            .and_then(|m| arg_of(m, "client-certificate-request"))
74            .map(str::to_string),
75        port: arg_of(ssl, "port").map(str::to_string),
76        use_pki: has_leaf(ssl, "use-pki"),
77    }
78}
79
80pub(super) fn parse_extension_service(service: &ConfigLine) -> ExtensionService {
81    ExtensionService {
82        grpc: child_block(service, "request-response")
83            .and_then(|rr| child_block(rr, "grpc"))
84            .map(|grpc| GrpcService {
85                max_connections: arg_of(grpc, "max-connections").map(str::to_string),
86                routing_instance: arg_of(grpc, "routing-instance").map(str::to_string),
87                ssl: child_block(grpc, "ssl").map(parse_ssl),
88            }),
89        notification: child_block(service, "notification").map(|n| JetNotification {
90            allow_clients: child_block(n, "allow-clients")
91                .map(|clients| args_of(clients, "address").into_iter().map(str::to_string).collect())
92                .unwrap_or_default(),
93            broker_socket_send_buffer_size: arg_of(n, "broker-socket-send-buffer-size").map(str::to_string),
94            max_connections: arg_of(n, "max-connections").map(str::to_string),
95            port: arg_of(n, "port").map(str::to_string),
96        }),
97        traceoptions: parse_traceoptions(service),
98    }
99}
100
101#[cfg(test)]
102mod tests {
103    use crate::system::system;
104    use asterust::config::juniper;
105
106    // Not from documentation: written from the CLI Reference syntax pages. See the README "Sources".
107    #[test]
108    fn extracts_the_jet_service_settings() {
109        let service = system(&juniper::parse_braces(
110            r#"
111system {
112    services {
113        extension-service {
114            request-response {
115                grpc {
116                    max-connections 10;
117                    routing-instance mgmt_junos;
118                    ssl {
119                        address 192.0.2.1;
120                        hot-reloading;
121                        local-certificate jet-cert;
122                        mutual-authentication {
123                            certificate-authority ca-profile;
124                            client-certificate-request require-certificate;
125                        }
126                        port 32767;
127                        use-pki;
128                    }
129                }
130            }
131            notification {
132                allow-clients {
133                    address 192.0.2.10;
134                    address 192.0.2.11;
135                }
136                broker-socket-send-buffer-size 4096;
137                max-connections 5;
138                port 1883;
139            }
140            traceoptions {
141                file jet.log;
142                level error;
143            }
144        }
145    }
146}
147"#,
148        ))
149        .unwrap()
150        .services
151        .unwrap()
152        .extension_service
153        .unwrap();
154        let grpc = service.grpc.unwrap();
155        assert_eq!(grpc.max_connections.as_deref(), Some("10"));
156        assert_eq!(grpc.routing_instance.as_deref(), Some("mgmt_junos"));
157        let ssl = grpc.ssl.unwrap();
158        assert_eq!(ssl.address.as_deref(), Some("192.0.2.1"));
159        assert!(ssl.hot_reloading);
160        assert_eq!(ssl.local_certificate.as_deref(), Some("jet-cert"));
161        assert_eq!(ssl.certificate_authority.as_deref(), Some("ca-profile"));
162        assert_eq!(ssl.client_certificate_request.as_deref(), Some("require-certificate"));
163        assert_eq!(ssl.port.as_deref(), Some("32767"));
164        assert!(ssl.use_pki);
165        let notification = service.notification.unwrap();
166        assert_eq!(notification.allow_clients, ["192.0.2.10", "192.0.2.11"]);
167        assert_eq!(notification.broker_socket_send_buffer_size.as_deref(), Some("4096"));
168        assert_eq!(notification.max_connections.as_deref(), Some("5"));
169        assert_eq!(notification.port.as_deref(), Some("1883"));
170        assert_eq!(service.traceoptions.unwrap().level.as_deref(), Some("error"));
171    }
172
173    #[test]
174    fn no_extension_service_is_none() {
175        let services = system(&juniper::parse_braces(
176            "system {\n    services {\n        telnet;\n    }\n}\n",
177        ))
178        .unwrap()
179        .services
180        .unwrap();
181        assert!(services.extension_service.is_none());
182    }
183}