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
|
//! The cases of docs/migrations.md, as far as the first version goes: a renamed slot
//! and a rollback across it, a type that changes without a converter, and what is
//! kept. G1 and G2 are generations, S1 and S2 their schemas.
mod common;
use common::Host;
use enstate::view::Change;
use serde_json::{Value, json};
fn secrets(slots: Value, views: Value) -> Value {
json!({ "schema": 1, "channels": { "secrets": { "slots": slots } }, "views": views })
}
fn rotate(host: &Host, name: &str, slot: &str, bytes: &[u8]) -> std::path::PathBuf {
let address = format!("secrets/{slot}");
host.submission(
name,
json!({ "channel": "secrets", "items": [{ "address": address, "op": "set" }] }),
&[(address.as_str(), bytes)],
)
}
/// Case 1: S2 renames `tinc.key` to `retiolum.key`. Switching to G2 needs no
/// submission; a rotation under G2 stores the state under S2; rolling back to G1
/// gives it the rotated key, by S2's rename read backwards.
#[test]
fn a_renamed_slot_and_a_rollback() {
let host = Host::new("rename");
let s1 = host.schema(secrets(
json!({ "tinc.key": { "type": "application/octet-stream", "secret": true } }),
json!({ "tinc.key": { "from": ["secrets/tinc.key"], "reactions": { "restart": ["tinc.service"] } } }),
));
let s2_value = secrets(
json!({ "retiolum.key": { "type": "application/octet-stream", "secret": true, "renamedFrom": "tinc.key" } }),
json!({ "retiolum.key": { "from": ["secrets/retiolum.key"], "reactions": { "restart": ["tincr.service"] } } }),
);
// G1 runs, K submitted.
assert!(
host.apply(&s1, &rotate(&host, "k", "tinc.key", b"K"))
.accepted
);
let first = host.snapshot("secrets").unwrap();
// Before switching to G2: it would see K, under its new name.
let s2 = host.schema(s2_value.clone());
let migration = host.migrate(&s2, true);
assert!(migration.accepted, "{migration:?}");
assert!(
migration.channels["secrets"].seen.notes[0]
.contains("secrets/retiolum.key: from secrets/tinc.key, renamed"),
"{migration:?}"
);
assert!(host.view("tinc.key", "tinc.key").is_some());
// Switching: the views follow, the snapshot isn't rewritten.
let migration = host.migrate(&s2, false);
assert!(
migration.accepted && migration.failed.is_empty(),
"{migration:?}"
);
assert_eq!(host.view("retiolum.key", "retiolum.key").unwrap(), b"K");
assert!(host.view("tinc.key", "tinc.key").is_none());
assert!(
host.reactions()
.contains("--no-block try-restart tincr.service")
);
let stored = host.store().current("secrets").unwrap().unwrap();
assert_eq!(stored.id, first);
assert_eq!(stored.schema, s1.1);
// Rotating under G2 stores the state under S2, and nothing is kept: tinc.key went
// into retiolum.key.
let plan = host.apply(&s2, &rotate(&host, "k2", "retiolum.key", b"K'"));
assert!(plan.accepted && plan.warnings.is_empty(), "{plan:?}");
let stored = host.store().current("secrets").unwrap().unwrap();
assert_eq!(stored.schema, s2.1);
assert_eq!(stored.content.keys().collect::<Vec<_>>(), ["retiolum.key"]);
assert!(stored.kept.is_empty());
// Rolling back to G1: tinc.key is the rotated key, from S2's rename.
let s1 = host.schema(serde_json::from_slice(&s1.1).unwrap());
let migration = host.migrate(&s1, false);
assert!(migration.accepted, "{migration:?}");
assert!(
migration.channels["secrets"].seen.notes[0].contains(
"secrets/tinc.key: from secrets/retiolum.key, which the schema it was accepted under renamed from it"
),
"{migration:?}"
);
assert_eq!(host.view("tinc.key", "tinc.key").unwrap(), b"K'");
assert!(host.view("retiolum.key", "retiolum.key").is_none());
}
/// Trying an older generation and back costs nothing: the snapshot stays as it was.
#[test]
fn switching_back_and_forth_rewrites_nothing() {
let host = Host::new("back-and-forth");
let s1 = host.schema(secrets(
json!({ "a": { "type": "application/octet-stream" } }),
json!({ "a": { "from": ["secrets/a"] } }),
));
assert!(host.apply(&s1, &rotate(&host, "a", "a", b"A")).accepted);
let id = host.snapshot("secrets").unwrap();
let s2 = host.schema(secrets(
json!({ "b": { "type": "application/octet-stream", "renamedFrom": "a" } }),
json!({ "b": { "from": ["secrets/b"] } }),
));
assert!(host.migrate(&s2, false).accepted);
let s1 = host.schema(serde_json::from_slice(&s1.1).unwrap());
assert!(host.migrate(&s1, false).accepted);
assert!(host.migrate(&s2, false).accepted);
assert_eq!(host.snapshot("secrets").unwrap(), id);
assert_eq!(host.view("b", "b").unwrap(), b"A");
}
/// Case 2, without converters (not in the first version): a slot whose type changes
/// can't be filled, so the switch is refused before switching, naming the slot; after
/// a switch anyway (a boot), the views made from it aren't there.
#[test]
fn a_type_without_a_converter() {
let host = Host::new("type");
let fleet = |media_type: &str| {
json!({
"schema": 1,
"channels": { "fleet": { "slots": { "hosts": { "type": media_type } } } },
"views": { "hosts": { "from": ["fleet/hosts"] } }
})
};
let s1 = host.schema(fleet("application/x-hosts1+json"));
let first = host.submission(
"first",
json!({ "channel": "fleet", "items": [{ "address": "fleet/hosts/ne", "op": "set" }] }),
&[("fleet/hosts", br#"{"ne": {"addr": "a"}}"#)],
);
assert!(host.apply(&s1, &first).accepted);
let s2 = host.schema(fleet("application/x-hosts2+json"));
let migration = host.migrate(&s2, true);
assert!(!migration.accepted);
assert_eq!(
migration.refused,
["required slot fleet/hosts can't be filled"]
);
assert!(
migration.channels["fleet"].seen.notes[0].contains(
"fleet/hosts: type application/x-hosts1+json is now application/x-hosts2+json, and there is no converter"
),
"{migration:?}"
);
// The dry run changed nothing; a switch anyway takes the view away.
assert!(host.view("hosts", "hosts").is_some());
let migration = host.migrate(&s2, false);
assert!(!migration.accepted);
assert!(
migration
.views
.iter()
.any(|view| view.name == "hosts" && view.change == Change::Removed)
);
assert!(host.view("hosts", "hosts").is_none());
// A submission made for S2 fills it, and the old entry goes: it is older.
let fresh = host.submission(
"fresh",
json!({ "channel": "fleet", "items": [{ "address": "fleet/hosts/ne", "op": "set" }] }),
&[("fleet/hosts", br#"{"ne": {"addrs": ["a", "b"]}}"#)],
);
let plan = host.apply(&s2, &fresh);
assert!(plan.accepted, "{plan:?}");
assert_eq!(
plan.warnings,
[
"goes, as fleet/hosts is filled anew: the entry of type application/x-hosts1+json kept from before"
]
);
assert!(host.view("hosts", "hosts").is_some());
// Back to G1 is refused before switching: nothing in S2 makes S1's type.
let s1 = host.schema(serde_json::from_slice(&s1.1).unwrap());
let migration = host.migrate(&s1, true);
assert_eq!(
migration.refused,
["required slot fleet/hosts can't be filled"]
);
assert!(migration.channels["fleet"].seen.notes[0].contains("is now application/x-hosts1+json"));
}
/// What no slot wants is kept, and fills its slot again after a rollback; a complete
/// submission lets it go.
#[test]
fn what_is_kept() {
let host = Host::new("kept");
let both = secrets(
json!({
"a": { "type": "application/octet-stream" },
"extra": { "type": "application/octet-stream", "required": false }
}),
json!({ "extra": { "from": ["secrets/extra"] } }),
);
let s1 = host.schema(both.clone());
let first = host.submission(
"first",
json!({ "channel": "secrets", "complete": true, "items": [
{ "address": "secrets/a", "op": "set" },
{ "address": "secrets/extra", "op": "set" }
] }),
&[("secrets/a", b"A"), ("secrets/extra", b"E")],
);
assert!(host.apply(&s1, &first).accepted);
let s2 = host.schema(secrets(
json!({ "a": { "type": "application/octet-stream" } }),
json!({}),
));
let migration = host.migrate(&s2, false);
assert_eq!(migration.channels["secrets"].kept, ["extra"]);
assert!(host.view("extra", "extra").is_none());
let plan = host.apply(&s2, &rotate(&host, "a2", "a", b"A2"));
assert_eq!(
plan.warnings,
["kept, in no slot of the schema: secrets/extra"]
);
let stored = host.store().current("secrets").unwrap().unwrap();
assert_eq!(stored.kept["extra"].1, b"E");
// Back to G1: extra is there again.
let s1 = host.schema(both);
let migration = host.migrate(&s1, false);
assert!(migration.channels["secrets"].seen.notes[0].contains("from what was kept"));
assert_eq!(host.view("extra", "extra").unwrap(), b"E");
// Under G2 again, a complete submission lets it go.
let s2 = host.schema(secrets(
json!({ "a": { "type": "application/octet-stream" } }),
json!({}),
));
let complete = host.submission(
"complete",
json!({ "channel": "secrets", "complete": true, "items": [{ "address": "secrets/a", "op": "set" }] }),
&[("secrets/a", b"A3")],
);
let plan = host.apply(&s2, &complete);
assert_eq!(
plan.warnings,
["goes, as the submission is complete: secrets/extra, which was kept"]
);
assert!(
host.store()
.current("secrets")
.unwrap()
.unwrap()
.kept
.is_empty()
);
}
/// A generation that adds a required slot: the switch is refused until a submission
/// for it is staged, which changes nothing the running generation serves; a submission
/// under the running generation keeps the staged entry; the switch brings it. And a
/// first deployment, without any state.
#[test]
fn staging_for_the_next_generation() {
let host = Host::new("stage");
let s1_value = secrets(
json!({ "a": { "type": "application/octet-stream" } }),
json!({ "a": { "from": ["secrets/a"] } }),
);
let s1 = host.schema(s1_value.clone());
assert!(host.apply(&s1, &rotate(&host, "a", "a", b"A")).accepted);
let reactions_before = host.reactions();
let s2_value = secrets(
json!({
"a": { "type": "application/octet-stream" },
"b.key": { "type": "application/octet-stream", "secret": true }
}),
json!({
"a": { "from": ["secrets/a"] },
"b.key": { "from": ["secrets/b.key"], "reactions": { "restart": ["b.service"] } }
}),
);
let s2 = host.schema(s2_value.clone());
assert_eq!(
host.migrate(&s2, true).refused,
["required slot secrets/b.key can't be filled"]
);
// Staged: the snapshot, under S2; no views, no reactions.
let plan = host.stage(&s2, &rotate(&host, "b", "b.key", b"B"));
assert!(
plan.accepted && plan.staged && plan.snapshot.is_some(),
"{plan:?}"
);
assert!(host.view("b.key", "b.key").is_none());
assert_eq!(host.view("a", "a").unwrap(), b"A");
assert_eq!(host.reactions(), reactions_before);
assert_eq!(
host.store().current("secrets").unwrap().unwrap().schema,
s2.1
);
assert!(host.migrate(&s2, true).accepted);
// A marker, for the next switch to serve the views; a dry run leaves it.
assert!(host.root.join("state/migrate-due").exists());
// The running generation goes on: what it has no slot for is kept.
let s1 = host.schema(s1_value);
let plan = host.apply(&s1, &rotate(&host, "a2", "a", b"A2"));
assert_eq!(
plan.warnings,
["kept, in no slot of the schema: secrets/b.key"]
);
// The switch: the staged entry is there, with its view and reaction.
let s2 = host.schema(s2_value.clone());
let migration = host.migrate(&s2, false);
assert!(migration.accepted, "{migration:?}");
assert_eq!(host.view("b.key", "b.key").unwrap(), b"B");
assert_eq!(host.view("a", "a").unwrap(), b"A2");
assert!(
host.reactions()
.contains("--no-block try-restart b.service")
);
assert!(!host.root.join("state/migrate-due").exists());
// A first deployment: no state, no views; staged, then switched to.
let fresh = Host::new("stage-first");
let s2 = fresh.schema(s2_value);
let first = fresh.submission(
"first",
json!({ "channel": "secrets", "complete": true, "items": [
{ "address": "secrets/a", "op": "set" },
{ "address": "secrets/b.key", "op": "set" }
] }),
&[("secrets/a", b"A"), ("secrets/b.key", b"B")],
);
let plan = fresh.stage(&s2, &first);
assert!(plan.accepted && plan.snapshot.is_some(), "{plan:?}");
assert!(fresh.view("a", "a").is_none());
assert!(fresh.migrate(&s2, false).accepted);
assert_eq!(fresh.view("b.key", "b.key").unwrap(), b"B");
}
/// Submissions waiting in the incoming directory: planned against the next
/// generation's schema by the check before the switch (on a copy, changing nothing),
/// and taken by the switch, before the views; one refused refuses the switch, and is
/// moved aside.
#[test]
fn incoming_submissions() {
let host = Host::new("incoming");
let s2 = host.schema(secrets(
json!({ "a": { "type": "application/octet-stream" } }),
json!({ "a": { "from": ["secrets/a"], "reactions": { "restart": ["a.service"] } } }),
));
let put = |name: &str, dir: &std::path::Path| {
let incoming = host.root.join("state/incoming");
std::fs::create_dir_all(&incoming).unwrap();
std::fs::rename(dir, incoming.join(name)).unwrap();
};
// A first deployment: nothing but a waiting submission.
put("1", &rotate(&host, "first", "a", b"A"));
let check = host.migrate(&s2, true);
assert!(check.accepted, "{check:?}");
assert!(check.incoming[0].plan.as_ref().unwrap().accepted);
assert!(host.snapshot("secrets").is_none());
assert!(host.root.join("state/incoming/1").exists());
// The dry run's copy of the snapshots is gone.
assert_eq!(
std::fs::read_dir(host.root.join("run/tmp"))
.unwrap()
.count(),
0
);
// The switch.
let migration = host.migrate(&s2, false);
assert!(migration.accepted, "{migration:?}");
assert_eq!(host.view("a", "a").unwrap(), b"A");
assert!(host.snapshot("secrets").is_some());
assert!(!host.root.join("state/incoming/1").exists());
// The deploy created the state directory, readable by all; it is root's now.
use std::os::unix::fs::PermissionsExt;
let mode = std::fs::metadata(host.root.join("state"))
.unwrap()
.permissions()
.mode();
assert_eq!(mode & 0o777, 0o700);
// Two in order, the second building on what the first makes; then one refused,
// which refuses the switch, and is moved aside by it.
put("2", &rotate(&host, "second", "a", b"B"));
put("3", &rotate(&host, "third", "a", b"C"));
let migration = host.migrate(&s2, false);
assert!(
migration.accepted && migration.incoming.len() == 2,
"{migration:?}"
);
assert_eq!(host.view("a", "a").unwrap(), b"C");
let bad = host.submission(
"bad",
json!({ "channel": "secrets", "items": [{ "address": "secrets/nope", "op": "set" }] }),
&[("secrets/nope", b"x")],
);
put("4", &bad);
let check = host.migrate(&s2, true);
assert!(!check.accepted);
assert!(
check.refused[0].starts_with("incoming 4: required item secrets/nope"),
"{check:?}"
);
assert!(host.root.join("state/incoming/4").exists());
let migration = host.migrate(&s2, false);
assert!(!migration.accepted);
assert!(host.root.join("state/refused/4").exists());
assert!(!host.root.join("state/incoming/4").exists());
assert_eq!(host.view("a", "a").unwrap(), b"C");
}
|