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