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