Skip to main content

asterust_junos/system/
license.rs

1//! `license`
2//! `system { license { ... } }`.
3//!
4//! # See also
5//!
6//! - [license](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/license-edit-system.html)
7
8use super::archival::redact_password;
9use crate::{active_children, arg_of, child_block, secret_value, statement_words, unquote, value_after, Secret};
10use asterust::config::ConfigLine;
11
12/// The `autoupdate` block of `system license`.
13#[derive(Clone, Debug, PartialEq, Eq)]
14#[non_exhaustive]
15pub struct LicenseAutoupdate {
16    /// The license server URL, without any password embedded in it.
17    pub url: Option<String>,
18    /// The `password` option; `None` unless the `secrets` feature is enabled.
19    pub password: Option<Secret>,
20}
21
22/// The `renew` block of `system license`.
23#[derive(Clone, Debug, PartialEq, Eq)]
24#[non_exhaustive]
25pub struct LicenseRenew {
26    /// Days before expiration to renew.
27    pub before_expiration: Option<String>,
28    /// Hours between license checks.
29    pub interval: Option<String>,
30}
31
32/// The `system license` configuration. License keys are not extracted, only how many are
33/// configured.
34#[derive(Clone, Debug, PartialEq, Eq)]
35#[non_exhaustive]
36pub struct License {
37    pub autoupdate: Option<LicenseAutoupdate>,
38    pub key_count: usize,
39    pub renew: Option<LicenseRenew>,
40}
41
42pub(super) fn parse_license(license: &ConfigLine) -> License {
43    License {
44        autoupdate: statement_words(license, "autoupdate").map(|words| LicenseAutoupdate {
45            url: value_after(&words, "url").map(|url| redact_password(unquote(&url))),
46            password: secret_value(value_after(&words, "password").as_deref()),
47        }),
48        key_count: child_block(license, "keys")
49            .map(|keys| active_children(keys).filter(|k| k.arg("key").is_some()).count())
50            .unwrap_or(0),
51        renew: child_block(license, "renew").map(|renew| LicenseRenew {
52            before_expiration: arg_of(renew, "before-expiration").map(str::to_string),
53            interval: arg_of(renew, "interval").map(str::to_string),
54        }),
55    }
56}
57
58#[cfg(test)]
59mod tests {
60    use crate::system::system;
61    use asterust::config::juniper;
62
63    // Not from documentation: written from the CLI Reference syntax page. See the README "Sources".
64    const LICENSE: &str = r#"
65system {
66    license {
67        autoupdate {
68            url "https://user:hunter2@ae1.juniper.net/junos/key_retrieval" password "$9$secret"; ## SECRET-DATA
69        }
70        keys {
71            key "JUNOS000001 aeaqcaj...";
72            key "JUNOS000002 bfbrdbk...";
73        }
74        renew {
75            before-expiration 30;
76            interval 24;
77        }
78    }
79}
80"#;
81
82    #[test]
83    fn extracts_license_without_secrets() {
84        let license = system(&juniper::parse_braces(LICENSE)).unwrap().license.unwrap();
85        assert_eq!(
86            license.autoupdate.as_ref().unwrap().url.as_deref(),
87            Some("https://user@ae1.juniper.net/junos/key_retrieval")
88        );
89        assert_eq!(
90            license
91                .autoupdate
92                .as_ref()
93                .and_then(|a| a.password.as_ref())
94                .map(crate::Secret::expose),
95            crate::secret::expected("$9$secret")
96        );
97        assert_eq!(license.key_count, 2);
98        let renew = license.renew.unwrap();
99        assert_eq!(renew.before_expiration.as_deref(), Some("30"));
100        assert_eq!(renew.interval.as_deref(), Some("24"));
101    }
102
103    #[test]
104    fn license_with_only_autoupdate() {
105        let license = system(&juniper::parse_braces(
106            "system {\n    license {\n        autoupdate {\n            url https://ae1.juniper.net/key;\n        }\n    }\n}\n",
107        ))
108        .unwrap()
109        .license
110        .unwrap();
111        assert_eq!(
112            license.autoupdate.unwrap().url.as_deref(),
113            Some("https://ae1.juniper.net/key")
114        );
115        assert_eq!(license.key_count, 0);
116        assert!(license.renew.is_none());
117    }
118
119    #[test]
120    fn no_license_is_none() {
121        let system = system(&juniper::parse_braces("system {\n    host-name pe1;\n}\n")).unwrap();
122        assert!(system.license.is_none());
123    }
124}