Skip to main content

asterust_junos/system/
license.rs

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