Skip to main content

asterust_junos/system/
proxy.rs

1//! `proxy`
2//! `system { proxy { ... } }`.
3//!
4//! # See also
5//!
6//! - [proxy (System)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/proxy-edit-system-proxy.html)
7
8use crate::{arg_of, secret_value, Secret};
9use asterust::config::ConfigLine;
10
11/// The `system proxy` server settings. The `password` is read only with the `secrets` feature.
12#[derive(Clone, Debug, PartialEq, Eq)]
13#[non_exhaustive]
14pub struct Proxy {
15    /// A hostname or IP address.
16    pub server: Option<String>,
17    pub port: Option<String>,
18    pub username: Option<String>,
19    /// `None` unless the `secrets` feature is enabled.
20    pub password: Option<Secret>,
21}
22
23pub(super) fn parse_proxy(proxy: &ConfigLine) -> Proxy {
24    Proxy {
25        server: arg_of(proxy, "server").map(str::to_string),
26        port: arg_of(proxy, "port").map(str::to_string),
27        username: arg_of(proxy, "username").map(str::to_string),
28        password: secret_value(arg_of(proxy, "password")),
29    }
30}
31
32#[cfg(test)]
33mod tests {
34    use crate::system::system;
35    use asterust::config::juniper;
36
37    // Not from documentation: written from the CLI Reference syntax page. See the README "Sources".
38    #[test]
39    fn extracts_proxy_settings_without_the_password() {
40        let proxy = system(&juniper::parse_braces(
41            r#"
42system {
43    proxy {
44        server proxy.example.net;
45        port 3128;
46        username ops;
47        password "$9$secret"; ## SECRET-DATA
48    }
49}
50"#,
51        ))
52        .unwrap()
53        .proxy
54        .unwrap();
55        assert_eq!(proxy.server.as_deref(), Some("proxy.example.net"));
56        assert_eq!(proxy.port.as_deref(), Some("3128"));
57        assert_eq!(proxy.username.as_deref(), Some("ops"));
58        assert_eq!(
59            proxy.password.as_ref().map(crate::Secret::expose),
60            crate::secret::expected("$9$secret")
61        );
62    }
63
64    #[test]
65    fn no_proxy_is_none() {
66        let system = system(&juniper::parse_braces("system {\n    host-name pe1;\n}\n")).unwrap();
67        assert!(system.proxy.is_none());
68    }
69}