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
|
# The NixOS module on a VM (docs/design.md, section 13): a key that a service reads by
# LoadCredential= of its own; applied from a source list, rotated without switching; a
# switch across a rename and back, with the rotated key; a switch refused before
# switching; a reboot.
{ pkgs }:
pkgs.testers.runNixOSTest {
name = "enstate";
nodes.machine =
{ config, lib, ... }:
let
key = {
type = "application/octet-stream";
secret = true;
reactions.restart = [ "consumer.service" ];
};
# The consumer gets its credential as hrm's services do, by LoadCredential= of
# its own; the ordering after enstate.service follows from that.
# (Given with a priority, so a specialisation's wins over the base's.)
credential = priority: config: name: {
environment.CRED = lib.mkOverride priority name;
serviceConfig.LoadCredential = lib.mkOverride priority "${name}:${config.enstate.channels.secrets.slots.${name}.path}";
};
in
{
imports = [ ./module.nix ];
enstate.enable = true;
enstate.channels.secrets.slots."tinc.key" = key;
# Writes down which credential it got, and what is in it.
systemd.services.consumer = lib.mkMerge [
{
wantedBy = [ "multi-user.target" ];
script = ''
echo "$CRED $(cat "$CREDENTIALS_DIRECTORY/$CRED")" > /run/consumer-seen
exec sleep infinity
'';
}
(credential 100 config "tinc.key")
];
specialisation.renamed.configuration =
{ config, ... }:
{
enstate.channels.secrets.slots = lib.mkForce {
"retiolum.key" = key // {
renamedFrom = "tinc.key";
};
};
systemd.services.consumer = credential 50 config "retiolum.key";
};
# Adds a required slot: a submission for it is staged before switching.
specialisation.added.configuration = {
enstate.channels.secrets.slots."extra.key" = key;
};
# Adds another; its submission waits in the incoming directory.
specialisation.more.configuration = {
enstate.channels.secrets.slots."more.key" = key;
};
specialisation.retyped.configuration = {
enstate.channels.secrets.slots = lib.mkForce {
"tinc.key" = key // {
type = "text/plain";
};
};
};
};
testScript = ''
def submit(name, key, command="--apply"):
machine.succeed(
f"mkdir -p /root/{name}",
f"printf '{key}' > /root/{name}/key",
f"""echo '{{"channel": "secrets", "complete": true, """
f""""items": {{"tinc.key": {{"source": {{"file": "key"}}}}}}}}' """
f"> /root/{name}/sources.json",
f"enstate --pack /root/{name}/sources.json --out /root/{name}/submission",
f"enstate --plan /root/{name}/submission",
f"enstate {command} /root/{name}/submission",
)
def seen(expected):
machine.wait_until_succeeds(f"grep -qx '{expected}' /run/consumer-seen")
machine.wait_for_unit("multi-user.target")
with subtest("the consumer reads a view by LoadCredential=, and is ordered after enstate"):
machine.succeed("systemctl show -p After consumer.service | grep -q enstate.service")
machine.succeed("systemctl show -p Wants consumer.service | grep -q enstate.service")
with subtest("without state, the required slot is missing, and its consumer fails"):
machine.succeed("enstate --status | grep -q 'missing tinc.key'")
machine.fail("systemctl is-active consumer.service")
with subtest("applied: the key is the consumer's credential, and it started"):
submit("first", "K")
machine.wait_for_unit("consumer.service")
seen("tinc.key K")
machine.succeed("test $(stat -c %a /run/enstate/views/secrets.tinc.key/tinc.key) = 600")
with subtest("rotated without switching: the consumer is restarted"):
submit("second", "L")
seen("tinc.key L")
with subtest("a switch across a rename: the key carries over"):
machine.succeed("/run/current-system/specialisation/renamed/bin/switch-to-configuration test")
seen("retiolum.key L")
machine.fail("test -e /run/enstate/views/secrets.tinc.key")
with subtest("and back: the old name, the same key"):
machine.succeed("/run/booted-system/bin/switch-to-configuration test")
seen("tinc.key L")
with subtest("a switch to a type the state can't fill is refused before switching"):
machine.fail("/run/booted-system/specialisation/retyped/bin/switch-to-configuration test")
machine.succeed("enstate --status | grep -q 'filled tinc.key'")
with subtest("a generation that adds a required slot: staged with its own enstate, then switched to"):
new = "/run/booted-system/specialisation/added"
machine.fail(f"{new}/bin/switch-to-configuration test")
machine.succeed(
"mkdir -p /root/added",
"printf X > /root/added/key",
"""echo '{"channel": "secrets", "items": {"extra.key": {"source": {"file": "key"}}}}' """
"> /root/added/sources.json",
f"{new}/sw/bin/enstate --pack /root/added/sources.json --out /root/added/submission",
f"{new}/sw/bin/enstate --plan /root/added/submission --schema {new}/etc/enstate/schema.json --config {new}/etc/enstate/config.json",
f"{new}/sw/bin/enstate --stage /root/added/submission --schema {new}/etc/enstate/schema.json --config {new}/etc/enstate/config.json",
)
machine.fail("test -e /run/enstate/views/secrets.extra.key")
seen("tinc.key L")
machine.succeed(f"{new}/bin/switch-to-configuration test")
machine.wait_until_succeeds("grep -qx X /run/enstate/views/secrets.extra.key/extra.key")
seen("tinc.key L")
machine.succeed("/run/booted-system/bin/switch-to-configuration test")
machine.succeed("enstate --status | grep -q 'kept extra.key'")
with subtest("staged under an unchanged schema: the switch brings it"):
submit("third", "M", "--stage")
seen("tinc.key L")
machine.succeed("test -e /var/lib/enstate/migrate-due")
out = machine.succeed("/run/current-system/bin/switch-to-configuration test 2>&1")
assert "deprecated" not in out, out
seen("tinc.key M")
machine.fail("test -e /var/lib/enstate/migrate-due")
with subtest("incoming: a submission waits for the switch that needs it"):
new = "/run/booted-system/specialisation/more"
machine.fail(f"{new}/bin/switch-to-configuration test")
machine.succeed(
"mkdir -p /root/more",
"printf Y > /root/more/key",
"""echo '{"channel": "secrets", "items": {"more.key": {"source": {"file": "key"}}}}' """
"> /root/more/sources.json",
"enstate --pack /root/more/sources.json --out /var/lib/enstate/incoming/1-more",
)
machine.succeed(f"{new}/bin/switch-to-configuration test")
machine.wait_until_succeeds("grep -qx Y /run/enstate/views/secrets.more.key/more.key")
machine.succeed("test -z \"$(ls -A /var/lib/enstate/incoming)\"")
seen("tinc.key M")
machine.succeed("/run/booted-system/bin/switch-to-configuration test")
with subtest("incoming: one refused refuses the switch, and stays"):
machine.succeed(
"mkdir -p /var/lib/enstate/incoming/2-bad/channels/secrets",
"printf x > /var/lib/enstate/incoming/2-bad/channels/secrets/nope",
"""echo '{"submission": 1, "channel": "secrets", "items": """
"""[{"address": "secrets/nope", "op": "set"}]}' """
"> /var/lib/enstate/incoming/2-bad/manifest.json",
)
machine.fail("/run/booted-system/specialisation/more/bin/switch-to-configuration test")
machine.succeed("test -d /var/lib/enstate/incoming/2-bad")
machine.succeed("rm -r /var/lib/enstate/incoming/2-bad")
with subtest("after a reboot, the views are there again"):
machine.shutdown()
machine.start()
machine.wait_for_unit("consumer.service")
seen("tinc.key M")
'';
}
|