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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
|
//! Checks of the sources and the target, the same as krops' checks.nix
//! makes during evaluation, so sources from anywhere else get checked as
//! well. Messages are the same.
use crate::spec::{Source, Spec};
use serde_json::{Map, Value};
use std::collections::BTreeMap;
/// The checks in the order krops' checks.nix makes them, the first failing
/// one's message. The target's host and user, which a command line gives,
/// come first.
pub fn spec(spec: &Spec) -> Result<(), String> {
target_host(&spec.target.host, spec.target.user.as_deref())?;
target_path(&spec.target.path)?;
for (name, source) in &spec.source {
source_name(name)?;
settings(name, source)?;
}
inputs(&spec.source)
}
/// The target's host and user, which ssh gets as arguments, mustn't be
/// taken for its options, and the host can't be empty.
fn target_host(host: &str, user: Option<&str>) -> Result<(), String> {
if host.is_empty() || host.starts_with('-') {
return Err(format!(
"krops: target host {} can't be used: it must not be empty or start with -",
quote(host)
));
}
if user.is_some_and(|u| u.starts_with('-')) {
return Err(format!(
"krops: target user {} can't be used: it must not start with -",
quote(user.unwrap_or_default())
));
}
Ok(())
}
/// What a setting holds.
#[derive(Clone, Copy)]
enum Kind {
Bool,
String,
/// A path starting with /, as lib.types.absolute-pathname and
/// lib.types.path demand.
Absolute,
Absolutes,
Strings,
/// git's clean, with exclude, a list of strings.
Clean,
/// file's filters, each with type include or exclude, and a pattern.
Filters,
/// Inputs drawn from other sources, each with follows, a string, and
/// inputs, again inputs.
Inputs,
}
/// The settings of each source type, as lib/types/populate.nix defines
/// them, and whether a source needs them, because they have no default.
fn known_settings(kind: &str) -> &'static [(&'static str, Kind, bool)] {
use Kind::*;
match kind {
"derivation" => &[("text", String, true)],
"file" => &[
("path", Absolute, true),
("useChecksum", Bool, false),
("exclude", Strings, false),
("filters", Filters, false),
("deleteExcluded", Bool, false),
("insecureLinks", Bool, false),
("inputs", Inputs, false),
("rewriteLock", Bool, false),
],
"flake" => &[
("url", String, true),
("fetchOnTarget", Bool, false),
("inputs", Inputs, false),
("materialize", Bool, false),
("patches", Absolutes, false),
("rewriteLock", Bool, false),
],
"flake-input" => &[
("url", String, true),
("name", String, true),
("requireInputs", Bool, false),
("fetchOnTarget", Bool, false),
("inputs", Inputs, false),
("materialize", Bool, false),
("patches", Absolutes, false),
("rewriteLock", Bool, false),
],
"git" => &[
("url", String, true),
("ref", String, true),
("clean", Clean, false),
("fetchAlways", Bool, false),
("shallow", Bool, false),
("reclone", Bool, false),
("inputs", Inputs, false),
("rewriteLock", Bool, false),
],
"pass" => &[("dir", Absolute, true), ("name", String, true)],
"passage" => &[
("age", String, true),
("dir", String, true),
("identities_file", String, true),
],
"pipe" => &[("command", Absolute, true)],
"symlink" => &[("target", String, true), ("inputs", Inputs, false)],
_ => &[],
}
}
/// Whether value is what kind says.
fn is_kind(value: &Value, kind: Kind) -> bool {
let absolute = |v: &Value| v.as_str().is_some_and(|s| s.starts_with('/'));
let strings = |v: &Value| {
v.as_array()
.is_some_and(|a| a.iter().all(|x| x.is_string()))
};
let only = |o: &Map<String, Value>, keys: &[&str]| o.keys().all(|k| keys.contains(&k.as_str()));
match kind {
Kind::Bool => value.is_boolean(),
Kind::String => value.is_string(),
Kind::Absolute => absolute(value),
Kind::Absolutes => value.as_array().is_some_and(|a| a.iter().all(absolute)),
Kind::Strings => strings(value),
Kind::Clean => value
.as_object()
.is_some_and(|o| only(o, &["exclude"]) && o.get("exclude").map_or(true, strings)),
Kind::Filters => value.as_array().is_some_and(|a| {
a.iter().all(|f| {
f.as_object().is_some_and(|o| {
only(o, &["type", "pattern"])
&& matches!(
o.get("type").and_then(Value::as_str),
None | Some("include" | "exclude")
)
&& o.get("pattern").is_some_and(Value::is_string)
})
})
}),
Kind::Inputs => value.as_object().is_some_and(|inputs| {
inputs.values().all(|input| {
input.as_object().is_some_and(|o| {
only(o, &["follows", "inputs"])
&& o.get("follows").map_or(true, Value::is_string)
&& o.get("inputs").map_or(true, |i| is_kind(i, Kind::Inputs))
})
})
}),
}
}
/// The settings of a source are those of its type, of the kinds
/// lib/types/populate.nix demands, so a misspelt one doesn't go unnoticed,
/// and paths populate passes to commands can't be taken for options.
pub fn settings(name: &str, source: &Source) -> Result<(), String> {
let kind = &source.kind;
let known = known_settings(kind);
for key in source.settings.keys() {
if !known.iter().any(|(k, _, _)| k == key) {
return Err(format!(
"krops: source {name}: a {kind} source has no setting {key}"
));
}
}
for (key, setting, required) in known {
match source.settings.get(*key) {
None if *required => {
return Err(format!(
"krops: source {name}: a {kind} source needs the setting {key}"
))
}
None => {}
Some(value) if is_kind(value, *setting) => {}
Some(value) if matches!(setting, Kind::Absolute) && value.is_string() => {
return Err(format!(
"krops: source {name}: the {key} of a {kind} source must be an absolute path, not {}",
value
))
}
Some(value) => {
return Err(format!(
"krops: source {name}: the {key} of a {kind} source can't be {value}"
))
}
}
}
Ok(())
}
/// Like JSON, the way Nix' toJSON quotes strings in messages.
fn quote(s: &str) -> String {
serde_json::to_string(s).expect("strings serialize")
}
/// Populate replaces and removes what's at the target paths of sources, so
/// it refuses names that lead out of the target directory, to the directory
/// itself, or to its own state. The remaining names are portable file
/// names.
pub fn source_name(name: &str) -> Result<(), String> {
let portable = |c: char| c.is_ascii_alphanumeric() || c == '.' || c == '_' || c == '-';
let ok = !matches!(name, "." | ".." | ".populate")
&& !name.is_empty()
&& !name.starts_with('-')
&& name.chars().all(portable);
if ok {
Ok(())
} else {
Err(format!(
"krops: source name {} can't be used: it must consist of letters, digits, and the characters ._-, not start with -, and be neither ., .., nor .populate",
quote(name)
))
}
}
/// Populate removes what it finds in the target directory, so it refuses
/// paths that aren't absolute, that are the root directory, or that the
/// target would resolve to another directory than the one they name.
pub fn target_path(path: &str) -> Result<(), String> {
let fail = |why: &str| {
Err(format!(
"krops: target path {} can't be used: {why}",
quote(path)
))
};
let components: Vec<&str> = path
.split('/')
.filter(|c| !c.is_empty() && *c != ".")
.collect();
if !path.starts_with('/') {
fail("it must be absolute")
} else if components.is_empty() {
fail("it must not be the root directory")
} else if components.contains(&"..") || path.chars().any(|c| c.is_ascii_control()) {
fail("it must not contain .. or control characters")
} else {
Ok(())
}
}
/// Inputs get drawn from other sources, so they must exist, and drawing
/// must not go round in circles.
pub fn inputs(sources: &BTreeMap<String, Source>) -> Result<(), String> {
let drawn_from: BTreeMap<&str, Vec<String>> = sources
.iter()
.map(|(name, source)| {
(
name.as_str(),
source.follows().into_iter().map(|f| f.source).collect(),
)
})
.collect();
for (name, froms) in &drawn_from {
for from in froms {
if from == name {
return Err(format!("krops: source {name} draws inputs from itself"));
}
if !sources.contains_key(from) {
return Err(format!(
"krops: source {name} draws inputs from missing source {from}"
));
}
}
}
// Depth-first search, with the path of sources on the way, which repeats
// a source if drawing goes round in circles. The message names the
// whole path, the way into the circle included, like krops' checks.nix.
fn visit<'a>(
name: &'a str,
drawn_from: &'a BTreeMap<&str, Vec<String>>,
done: &mut Vec<&'a str>,
path: &mut Vec<&'a str>,
) -> Result<(), String> {
if path.contains(&name) {
return Err(format!(
"krops: sources draw inputs from each other: {}",
path.join(" ")
));
}
if done.contains(&name) {
return Ok(());
}
path.push(name);
for from in &drawn_from[name] {
visit(from, drawn_from, done, path)?;
}
path.pop();
done.push(name);
Ok(())
}
let mut done = Vec::new();
for name in drawn_from.keys() {
visit(name, &drawn_from, &mut done, &mut Vec::new())?;
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn source_names() {
for name in ["a", "A.b_c-d", ".x", "_", "0"] {
assert!(source_name(name).is_ok(), "{name:?}");
}
for name in [
"",
".",
"..",
".populate",
"a/b",
"../x",
"/etc",
"a\nb",
"a b",
"a*b",
"a?b",
"a+b",
"a=b",
"-a",
] {
assert!(source_name(name).is_err(), "{name:?}");
}
}
#[test]
fn target_paths() {
for path in ["/a", "/var/src", "/a/./b", "//a"] {
assert!(target_path(path).is_ok(), "{path:?}");
}
for path in [
"",
"/",
"//",
"/.",
"relative",
"/var/krops/..",
"/var/krops/../..",
"/var/krops/a\nb",
] {
assert!(target_path(path).is_err(), "{path:?}");
}
assert_eq!(
target_path("/a\nb").unwrap_err(),
r#"krops: target path "/a\nb" can't be used: it must not contain .. or control characters"#
);
}
fn sources(json: &str) -> BTreeMap<String, Source> {
serde_json::from_str(json).unwrap()
}
#[test]
fn drawing_inputs() {
let ok = sources(
r#"{
"a": {"type": "file", "file": {"path": "/a", "inputs": {"x": {"follows": "b"}}}},
"b": {"type": "file", "file": {"path": "/b"}}
}"#,
);
assert!(inputs(&ok).is_ok());
let missing = sources(
r#"{
"a": {"type": "file", "file": {"path": "/a", "inputs": {"x": {"follows": "c"}}}}
}"#,
);
assert_eq!(
inputs(&missing).unwrap_err(),
"krops: source a draws inputs from missing source c"
);
let cycle = sources(
r#"{
"a": {"type": "file", "file": {"path": "/a", "inputs": {"x": {"follows": "b"}}}},
"b": {"type": "file", "file": {"path": "/b",
"inputs": {"y": {"inputs": {"z": {"follows": "a"}}}}}}
}"#,
);
assert_eq!(
inputs(&cycle).unwrap_err(),
"krops: sources draw inputs from each other: a b"
);
}
#[test]
fn target_hosts() {
assert!(target_host("host", Some("root")).is_ok());
assert!(target_host("2001:db8::1", None).is_ok());
assert_eq!(
target_host("-oProxyCommand=x", None).unwrap_err(),
r#"krops: target host "-oProxyCommand=x" can't be used: it must not be empty or start with -"#
);
assert!(target_host("", None).is_err());
assert!(target_host("host", Some("-x")).is_err());
}
#[test]
fn settings() {
let spec = |file: &str| -> Spec {
Spec {
backup: false,
force: false,
prune: false,
source: serde_json::from_str(&format!(
r#"{{"x": {{"type": "file", "file": {file}}}}}"#
))
.unwrap(),
target: crate::spec::Target {
user: None,
host: "h".to_string(),
port: None,
path: "/a".to_string(),
sudo: false,
extra_options: vec![],
},
}
};
assert!(super::spec(&spec(r#"{"path": "/src"}"#)).is_ok());
assert!(super::spec(&spec(r#"{"path": "--rsync-path=x"}"#)).is_err());
assert_eq!(
super::spec(&spec(r#"{"path": "relative"}"#)).unwrap_err(),
r#"krops: source x: the path of a file source must be an absolute path, not "relative""#
);
assert_eq!(
super::spec(&spec(r#"{"path": "/src", "deleteExcludd": false}"#)).unwrap_err(),
"krops: source x: a file source has no setting deleteExcludd"
);
assert_eq!(
super::spec(&spec(r#"{"path": "/src", "useChecksum": "yes"}"#)).unwrap_err(),
r#"krops: source x: the useChecksum of a file source can't be "yes""#
);
assert_eq!(
super::spec(&spec("{}")).unwrap_err(),
"krops: source x: a file source needs the setting path"
);
assert!(super::spec(&spec(
r#"{"path": "/src", "filters": [{"type": "include", "pattern": "*.nix"}],
"inputs": {"a": {"follows": "x", "inputs": {"b": {"follows": "y"}}}}}"#
))
.is_err_and(|e| e.contains("draws inputs")));
assert!(super::spec(&spec(
r#"{"path": "/src", "filters": [{"type": "x", "pattern": "*"}]}"#
))
.is_err_and(|e| e.contains("filters")));
}
#[test]
fn cycle_reached_from_another_source() {
let cycle = sources(
r#"{
"a": {"type": "file", "file": {"path": "/a", "inputs": {"i": {"follows": "b"}}}},
"b": {"type": "file", "file": {"path": "/b", "inputs": {"i": {"follows": "c"}}}},
"c": {"type": "file", "file": {"path": "/c", "inputs": {"i": {"follows": "b"}}}}
}"#,
);
assert_eq!(
inputs(&cycle).unwrap_err(),
"krops: sources draw inputs from each other: a b c"
);
}
}
|