Repository navigation
Expand file tree
/
Copy pathconfig.rs
More file actions
293 lines (270 loc) · 9.45 KB
/
Copy pathconfig.rs
File metadata and controls
293 lines (270 loc) · 9.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
use std::{fmt, net::SocketAddr};
/// The service binds IPv4 loopback; remote TLS termination is configured separately.
#[derive(Debug, Clone, Copy)]
pub struct Config {
port: u16,
lifecycle_enabled: bool,
provisioning_enabled: bool,
snapshots_enabled: bool,
restores_enabled: bool,
deletions_enabled: bool,
}
impl Config {
/// An absent port uses 8080. Empty, zero and invalid ports are rejected.
pub fn from_port(port: Option<&str>) -> Result<Self, InvalidPort> {
let port = match port {
None => 8080,
Some(value) => value.parse::<u16>().map_err(|_| InvalidPort)?,
};
if port == 0 {
return Err(InvalidPort);
}
Ok(Self {
port,
lifecycle_enabled: false,
provisioning_enabled: false,
snapshots_enabled: false,
restores_enabled: false,
deletions_enabled: false,
})
}
/// Lifecycle execution is opt-in. Only exact lowercase true/false are accepted.
pub fn with_lifecycle(mut self, value: Option<&str>) -> Result<Self, InvalidLifecycle> {
self.lifecycle_enabled = match value {
None | Some("false") => false,
Some("true") => true,
_ => return Err(InvalidLifecycle),
};
Ok(self)
}
pub fn with_provisioning(mut self, value: Option<&str>) -> Result<Self, InvalidProvisioning> {
self.provisioning_enabled = match value {
None | Some("false") => false,
Some("true") => true,
_ => return Err(InvalidProvisioning),
};
Ok(self)
}
pub fn with_snapshots(mut self, value: Option<&str>) -> Result<Self, InvalidSnapshots> {
self.snapshots_enabled = match value {
None | Some("false") => false,
Some("true") => true,
_ => return Err(InvalidSnapshots),
};
Ok(self)
}
pub fn snapshots_enabled(self) -> bool {
self.snapshots_enabled
}
pub fn with_restores(mut self, value: Option<&str>) -> Result<Self, InvalidRestores> {
self.restores_enabled = match value {
None | Some("false") => false,
Some("true") => true,
_ => return Err(InvalidRestores),
};
Ok(self)
}
pub fn restores_enabled(self) -> bool {
self.restores_enabled
}
pub fn with_deletions(mut self, value: Option<&str>) -> Result<Self, InvalidDeletions> {
self.deletions_enabled = match value {
None | Some("false") => false,
Some("true") => true,
_ => return Err(InvalidDeletions),
};
Ok(self)
}
pub fn deletions_enabled(self) -> bool {
self.deletions_enabled
}
pub fn provisioning_enabled(self) -> bool {
self.provisioning_enabled
}
pub fn lifecycle_enabled(self) -> bool {
self.lifecycle_enabled
}
pub fn address(self) -> SocketAddr {
SocketAddr::from(([127, 0, 0, 1], self.port))
}
}
#[derive(Debug)]
pub struct InvalidPort;
impl fmt::Display for InvalidPort {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("VMANYWARE_PORT must be an integer between 1 and 65535")
}
}
impl std::error::Error for InvalidPort {}
#[derive(Debug)]
pub struct InvalidLifecycle;
impl fmt::Display for InvalidLifecycle {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("VMANYWARE_LIFECYCLE_ENABLED must be true or false")
}
}
impl std::error::Error for InvalidLifecycle {}
#[derive(Debug)]
pub struct InvalidProvisioning;
impl fmt::Display for InvalidProvisioning {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("VMANYWARE_PROVISIONING_ENABLED must be true or false")
}
}
impl std::error::Error for InvalidProvisioning {}
#[derive(Debug)]
pub struct InvalidSnapshots;
impl fmt::Display for InvalidSnapshots {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("VMANYWARE_SNAPSHOTS_ENABLED must be true or false")
}
}
impl std::error::Error for InvalidSnapshots {}
#[derive(Debug)]
pub struct InvalidRestores;
impl fmt::Display for InvalidRestores {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("VMANYWARE_RESTORES_ENABLED must be true or false")
}
}
impl std::error::Error for InvalidRestores {}
#[derive(Debug)]
pub struct InvalidDeletions;
impl fmt::Display for InvalidDeletions {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("VMANYWARE_DELETIONS_ENABLED must be true or false")
}
}
impl std::error::Error for InvalidDeletions {}
#[cfg(test)]
mod tests {
use super::Config;
#[test]
fn deletions_require_independent_exact_opt_in_without_echoing_input() {
let config = Config::from_port(None)
.unwrap()
.with_lifecycle(Some("true"))
.unwrap()
.with_provisioning(Some("true"))
.unwrap()
.with_snapshots(Some("true"))
.unwrap()
.with_restores(Some("true"))
.unwrap();
assert!(!config.deletions_enabled());
for (value, enabled) in [(None, false), (Some("false"), false), (Some("true"), true)] {
assert_eq!(
config.with_deletions(value).unwrap().deletions_enabled(),
enabled
);
}
let only = Config::from_port(None)
.unwrap()
.with_deletions(Some("true"))
.unwrap();
assert!(
!only.lifecycle_enabled()
&& !only.provisioning_enabled()
&& !only.snapshots_enabled()
&& !only.restores_enabled()
);
for value in ["", "1", "TRUE", " true", "false\n", "private-input"] {
assert_eq!(
config.with_deletions(Some(value)).unwrap_err().to_string(),
"VMANYWARE_DELETIONS_ENABLED must be true or false"
);
}
}
#[test]
fn restores_require_independent_exact_opt_in_without_echoing_input() {
let config = Config::from_port(None)
.unwrap()
.with_lifecycle(Some("true"))
.unwrap()
.with_provisioning(Some("true"))
.unwrap()
.with_snapshots(Some("true"))
.unwrap();
assert!(!config.restores_enabled());
for (value, enabled) in [(None, false), (Some("false"), false), (Some("true"), true)] {
assert_eq!(
config.with_restores(value).unwrap().restores_enabled(),
enabled
);
}
let restore_only = Config::from_port(None)
.unwrap()
.with_restores(Some("true"))
.unwrap();
assert!(
!restore_only.snapshots_enabled()
&& !restore_only.lifecycle_enabled()
&& !restore_only.provisioning_enabled()
);
for value in ["", "1", "TRUE", " true", "false\n", "private-input"] {
assert_eq!(
config.with_restores(Some(value)).unwrap_err().to_string(),
"VMANYWARE_RESTORES_ENABLED must be true or false"
);
}
}
#[test]
fn snapshots_require_independent_exact_opt_in_without_echoing_input() {
let config = Config::from_port(None).unwrap();
assert!(!config.snapshots_enabled());
for (value, enabled) in [(None, false), (Some("false"), false), (Some("true"), true)] {
let result = config.with_snapshots(value).unwrap();
assert_eq!(result.snapshots_enabled(), enabled);
assert!(!result.provisioning_enabled() && !result.lifecycle_enabled());
}
for value in ["", "1", "TRUE", " true", "private-input"] {
assert_eq!(
config.with_snapshots(Some(value)).unwrap_err().to_string(),
"VMANYWARE_SNAPSHOTS_ENABLED must be true or false"
);
}
}
#[test]
fn default_and_custom_ports_always_bind_loopback() {
for (input, expected) in [(None, 8080), (Some("9000"), 9000)] {
let address = Config::from_port(input).unwrap().address();
assert!(address.ip().is_loopback());
assert_eq!(address.port(), expected);
}
}
#[test]
fn lifecycle_requires_explicit_exact_opt_in() {
let config = Config::from_port(None).unwrap();
assert!(!config.lifecycle_enabled());
for (input, enabled) in [(None, false), (Some("false"), false), (Some("true"), true)] {
let configured = config.with_lifecycle(input).unwrap();
assert_eq!(configured.lifecycle_enabled(), enabled);
assert!(configured.address().ip().is_loopback());
}
for input in [
"",
"1",
"0",
"TRUE",
"yes",
" true",
"false\n",
"private-input",
] {
assert_eq!(
config.with_lifecycle(Some(input)).unwrap_err().to_string(),
"VMANYWARE_LIFECYCLE_ENABLED must be true or false"
);
}
}
#[test]
fn rejects_invalid_ports_without_echoing_input() {
for input in ["", "0", "65536", "-1", "localhost:8080", "secret"] {
let error = Config::from_port(Some(input)).unwrap_err();
assert_eq!(
error.to_string(),
"VMANYWARE_PORT must be an integer between 1 and 65535"
);
}
}
}