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