asterust_junos/system/services/
ssh.rs1use crate::{arg_of, args_of, child_block, has_leaf, list_values};
10use asterust::config::ConfigLine;
11
12#[derive(Clone, Debug, Default, PartialEq, Eq)]
14#[non_exhaustive]
15pub struct SshModuli {
16 pub modulus_type: Option<String>,
18 pub refresh: Option<String>,
20 pub count: Option<String>,
22}
23
24#[derive(Clone, Debug, Default, PartialEq, Eq)]
26#[non_exhaustive]
27pub struct SshService {
28 pub root_login: Option<String>,
30 pub protocol_versions: Vec<String>,
31 pub port: Option<String>,
32 pub connection_limit: Option<String>,
33 pub rate_limit: Option<String>,
34 pub ciphers: Vec<String>,
35 pub sftp_server: bool,
36 pub authentication_order: Vec<String>,
38 pub key_exchange: Vec<String>,
39 pub macs: Vec<String>,
40 pub client_alive_count_max: Option<String>,
41 pub client_alive_interval: Option<String>,
42 pub fingerprint_hash: Option<String>,
44 pub max_sessions_per_connection: Option<String>,
45 pub max_pre_authentication_packets: Option<String>,
46 pub no_challenge_response: bool,
47 pub no_password_authentication: bool,
48 pub no_passwords: bool,
49 pub no_public_keys: bool,
50 pub moduli: Option<SshModuli>,
51}
52
53pub(super) fn parse_ssh(ssh: &ConfigLine) -> SshService {
54 let list = |keyword: &str| args_of(ssh, keyword).into_iter().flat_map(list_values).collect();
55 let field = |keyword: &str| arg_of(ssh, keyword).map(str::to_string);
56 SshService {
57 root_login: field("root-login"),
58 protocol_versions: list("protocol-version"),
59 port: field("port"),
60 connection_limit: field("connection-limit"),
61 rate_limit: field("rate-limit"),
62 ciphers: list("ciphers"),
63 sftp_server: has_leaf(ssh, "sftp-server"),
64 authentication_order: list("authentication-order"),
65 key_exchange: list("key-exchange"),
66 macs: list("macs"),
67 client_alive_count_max: field("client-alive-count-max"),
68 client_alive_interval: field("client-alive-interval"),
69 fingerprint_hash: field("fingerprint-hash"),
70 max_sessions_per_connection: field("max-sessions-per-connection"),
71 max_pre_authentication_packets: field("max-pre-authentication-packets"),
72 no_challenge_response: has_leaf(ssh, "no-challenge-response"),
73 no_password_authentication: has_leaf(ssh, "no-password-authentication"),
74 no_passwords: has_leaf(ssh, "no-passwords"),
75 no_public_keys: has_leaf(ssh, "no-public-keys"),
76 moduli: child_block(ssh, "moduli").map(|moduli| SshModuli {
77 modulus_type: arg_of(moduli, "type").map(str::to_string),
78 refresh: arg_of(moduli, "refresh").map(str::to_string),
79 count: arg_of(moduli, "count").map(str::to_string),
80 }),
81 }
82}
83
84#[cfg(test)]
85mod tests {
86 use crate::system::system;
87 use asterust::config::juniper;
88
89 #[test]
90 fn extracts_more_ssh_settings() {
91 let ssh = system(&juniper::parse_braces(
92 r#"
93system {
94 services {
95 ssh {
96 authentication-order [ tacplus password ];
97 key-exchange [ curve25519-sha256 ecdh-sha2-nistp256 ];
98 macs [ hmac-sha2-256 hmac-sha2-512 ];
99 client-alive-count-max 3;
100 client-alive-interval 60;
101 fingerprint-hash sha2-256;
102 max-sessions-per-connection 5;
103 max-pre-authentication-packets 64;
104 no-challenge-response;
105 no-password-authentication;
106 no-public-keys;
107 }
108 }
109}
110"#,
111 ))
112 .unwrap()
113 .services
114 .unwrap()
115 .ssh
116 .unwrap();
117 assert_eq!(ssh.authentication_order, ["tacplus", "password"]);
118 assert_eq!(ssh.key_exchange, ["curve25519-sha256", "ecdh-sha2-nistp256"]);
119 assert_eq!(ssh.macs, ["hmac-sha2-256", "hmac-sha2-512"]);
120 assert_eq!(ssh.client_alive_count_max.as_deref(), Some("3"));
121 assert_eq!(ssh.client_alive_interval.as_deref(), Some("60"));
122 assert_eq!(ssh.fingerprint_hash.as_deref(), Some("sha2-256"));
123 assert_eq!(ssh.max_sessions_per_connection.as_deref(), Some("5"));
124 assert_eq!(ssh.max_pre_authentication_packets.as_deref(), Some("64"));
125 assert!(ssh.no_challenge_response);
126 assert!(ssh.no_password_authentication);
127 assert!(ssh.no_public_keys);
128 assert!(!ssh.no_passwords);
129 }
130
131 #[test]
133 fn extracts_ssh_moduli() {
134 let ssh = system(&juniper::parse_braces(
135 "system {\n services {\n ssh {\n moduli {\n type safe;\n refresh weekly;\n count 5;\n }\n }\n }\n}\n",
136 ))
137 .unwrap()
138 .services
139 .unwrap()
140 .ssh
141 .unwrap();
142 let moduli = ssh.moduli.unwrap();
143 assert_eq!(moduli.modulus_type.as_deref(), Some("safe"));
144 assert_eq!(moduli.refresh.as_deref(), Some("weekly"));
145 assert_eq!(moduli.count.as_deref(), Some("5"));
146 }
147}