asterust_junos/system/
autoinstallation.rs1use super::archival::{redact_password, server_secret};
9use crate::{active_children, child_block, has_leaf, unquote, Secret};
10use asterust::config::ConfigLine;
11
12#[derive(Clone, Debug, PartialEq, Eq)]
14#[non_exhaustive]
15pub struct AutoinstallationInterface {
16 pub name: String,
17 pub bootp: bool,
18 pub rarp: bool,
19}
20
21#[derive(Clone, Debug, PartialEq, Eq)]
24#[non_exhaustive]
25pub struct Autoinstallation {
26 pub configuration_servers: Vec<String>,
28 pub configuration_server_passwords: Vec<Option<Secret>>,
31 pub interfaces: Vec<AutoinstallationInterface>,
32 pub usb_disabled: bool,
34}
35
36pub(super) fn parse_autoinstallation(autoinstallation: &ConfigLine) -> Autoinstallation {
37 Autoinstallation {
38 configuration_servers: child_block(autoinstallation, "configuration-servers")
39 .map(|servers| {
40 active_children(servers)
41 .filter_map(|server| server.text.split_whitespace().next())
42 .map(|url| redact_password(unquote(url)))
43 .collect()
44 })
45 .unwrap_or_default(),
46 configuration_server_passwords: child_block(autoinstallation, "configuration-servers")
47 .map(|servers| {
48 active_children(servers)
49 .filter(|server| server.text.split_whitespace().next().is_some())
50 .map(server_secret)
51 .collect()
52 })
53 .unwrap_or_default(),
54 interfaces: child_block(autoinstallation, "interfaces")
55 .map(|interfaces| {
56 active_children(interfaces)
57 .map(|interface| AutoinstallationInterface {
58 name: interface.text.clone(),
59 bootp: has_leaf(interface, "bootp"),
60 rarp: has_leaf(interface, "rarp"),
61 })
62 .collect()
63 })
64 .unwrap_or_default(),
65 usb_disabled: child_block(autoinstallation, "usb").is_some_and(|usb| has_leaf(usb, "disable")),
66 }
67}
68
69#[cfg(test)]
70mod tests {
71 use crate::system::system;
72 use asterust::config::juniper;
73
74 #[test]
76 fn extracts_autoinstallation_settings() {
77 let autoinstallation = system(&juniper::parse_braces(
78 r#"
79system {
80 autoinstallation {
81 configuration-servers {
82 tftp://192.0.2.10/config;
83 "ftp://svc:hunter2@192.0.2.11/config" {
84 password "$9$secret"; ## SECRET-DATA
85 }
86 }
87 interfaces {
88 ge-0/0/0 {
89 bootp;
90 }
91 ge-0/0/1 {
92 bootp;
93 rarp;
94 }
95 }
96 usb {
97 disable;
98 }
99 }
100}
101"#,
102 ))
103 .unwrap()
104 .autoinstallation
105 .unwrap();
106 assert_eq!(
107 autoinstallation.configuration_servers,
108 ["tftp://192.0.2.10/config", "ftp://svc@192.0.2.11/config"]
109 );
110 let passwords: Vec<_> = autoinstallation
111 .configuration_server_passwords
112 .iter()
113 .map(|p| p.as_ref().map(crate::Secret::expose))
114 .collect();
115 assert_eq!(passwords, [None, crate::secret::expected("$9$secret")]);
116 assert_eq!(autoinstallation.interfaces.len(), 2);
117 assert_eq!(autoinstallation.interfaces[0].name, "ge-0/0/0");
118 assert!(autoinstallation.interfaces[0].bootp);
119 assert!(!autoinstallation.interfaces[0].rarp);
120 assert!(autoinstallation.interfaces[1].rarp);
121 assert!(autoinstallation.usb_disabled);
122 }
123
124 #[test]
125 fn bare_autoinstallation_is_enabled_with_no_settings() {
126 let autoinstallation = system(&juniper::parse_braces("system {\n autoinstallation;\n}\n"))
127 .unwrap()
128 .autoinstallation
129 .unwrap();
130 assert!(autoinstallation.configuration_servers.is_empty());
131 assert!(autoinstallation.interfaces.is_empty());
132 assert!(!autoinstallation.usb_disabled);
133 }
134
135 #[test]
136 fn no_autoinstallation_is_none() {
137 let system = system(&juniper::parse_braces("system {\n host-name pe1;\n}\n")).unwrap();
138 assert!(system.autoinstallation.is_none());
139 }
140}