Skip to main content

asterust_junos/system/
accounting.rs

1//! `system { accounting { ... } }`.
2
3use super::auth::{radius_servers, tacplus_servers};
4use super::{RadiusServer, TacplusServer};
5use crate::{arg_of, args_of, child_block, list_values};
6use asterust::config::ConfigLine;
7
8/// The `destination` block of `system accounting`. Server secrets are not extracted.
9#[derive(Clone, Debug, PartialEq, Eq)]
10#[non_exhaustive]
11pub struct AccountingDestination {
12    pub radius: Vec<RadiusServer>,
13    pub tacplus: Vec<TacplusServer>,
14}
15
16/// The `system accounting` configuration.
17#[derive(Clone, Debug, PartialEq, Eq)]
18#[non_exhaustive]
19pub struct Accounting {
20    /// `change-log`, `interactive-commands` and `login`.
21    pub events: Vec<String>,
22    pub enhanced_avs_max: Option<String>,
23    pub destination: Option<AccountingDestination>,
24}
25
26pub(super) fn parse_accounting(accounting: &ConfigLine) -> Accounting {
27    Accounting {
28        events: args_of(accounting, "events")
29            .into_iter()
30            .flat_map(list_values)
31            .collect(),
32        enhanced_avs_max: arg_of(accounting, "enhanced-avs-max").map(str::to_string),
33        destination: child_block(accounting, "destination").map(|destination| AccountingDestination {
34            radius: child_block(destination, "radius")
35                .map(|radius| radius_servers(radius, "server"))
36                .unwrap_or_default(),
37            tacplus: child_block(destination, "tacplus")
38                .map(|tacplus| tacplus_servers(tacplus, "server"))
39                .unwrap_or_default(),
40        }),
41    }
42}
43
44#[cfg(test)]
45mod tests {
46    use crate::system::system;
47    use asterust::config::juniper;
48
49    // Not from documentation: written from the CLI Reference syntax page. See the README "Sources".
50    const ACCOUNTING: &str = r#"
51system {
52    accounting {
53        events [ login change-log interactive-commands ];
54        enhanced-avs-max 10;
55        destination {
56            radius {
57                server 192.0.2.10 {
58                    secret "$9$secret"; ## SECRET-DATA
59                    accounting-port 1813;
60                    timeout 5;
61                    routing-instance mgmt_junos;
62                }
63            }
64            tacplus {
65                server 192.0.2.20 {
66                    secret "$9$other"; ## SECRET-DATA
67                    port 49;
68                    single-connection;
69                }
70                server 192.0.2.21 {
71                    secret "$9$third"; ## SECRET-DATA
72                }
73            }
74        }
75    }
76}
77"#;
78
79    #[test]
80    fn extracts_accounting_events() {
81        let accounting = system(&juniper::parse_braces(ACCOUNTING)).unwrap().accounting.unwrap();
82        assert_eq!(accounting.events, ["login", "change-log", "interactive-commands"]);
83        assert_eq!(accounting.enhanced_avs_max.as_deref(), Some("10"));
84    }
85
86    #[test]
87    fn extracts_accounting_destination_servers() {
88        let accounting = system(&juniper::parse_braces(ACCOUNTING)).unwrap().accounting.unwrap();
89        let destination = accounting.destination.unwrap();
90
91        assert_eq!(destination.radius.len(), 1);
92        assert_eq!(destination.radius[0].address, "192.0.2.10");
93        assert_eq!(destination.radius[0].accounting_port.as_deref(), Some("1813"));
94        assert_eq!(destination.radius[0].timeout.as_deref(), Some("5"));
95        assert_eq!(destination.radius[0].routing_instance.as_deref(), Some("mgmt_junos"));
96
97        let addresses: Vec<_> = destination.tacplus.iter().map(|s| s.address.as_str()).collect();
98        assert_eq!(addresses, ["192.0.2.20", "192.0.2.21"]);
99        assert!(destination.tacplus[0].single_connection);
100        assert_eq!(destination.tacplus[0].port.as_deref(), Some("49"));
101    }
102
103    #[test]
104    fn accounting_without_destination() {
105        let accounting = system(&juniper::parse_braces(
106            "system {\n    accounting {\n        events login;\n    }\n}\n",
107        ))
108        .unwrap()
109        .accounting
110        .unwrap();
111        assert_eq!(accounting.events, ["login"]);
112        assert!(accounting.destination.is_none());
113    }
114
115    #[test]
116    fn no_accounting_is_none() {
117        let system = system(&juniper::parse_braces("system {\n    host-name pe1;\n}\n")).unwrap();
118        assert!(system.accounting.is_none());
119    }
120}