gunk-dev/the-valley

hosts.nix

sourced7aca57f4c15main's published tip history
15.2 KiB
1# The hosts the module checks are about: eval-only instantiations of the
2# NixOS module, one per declaration worth holding it to. Evaluating one
3# catches option-surface and cue-export regressions without building or
4# booting anything, and the hook checks read the scripts these render.
5{
6 pkgs,
7 lib,
8 self,
9 # Whether the hosts run stand-ins for this flake's programs rather than
10 # the programs themselves.
11 stubbed ? false,
12}:
13let
14 # The stand-ins: the integrator, the identity compiler and valleyhook,
15 # each a script with the program's name that fails if anything runs it.
16 # A host rendered over them renders the same units, hooks and scripts,
17 # with the stand-ins' store paths where the programs' would be. None of
18 # them changes when a program does, so a check that reads only what the
19 # module renders changes only when the module or the check does. A check
20 # that runs a program takes the hosts that run the real one.
21 stubs = lib.genAttrs [ "integrator" "identity" "valleyhook" ] (
22 name:
23 pkgs.writeShellScriptBin name ''
24 echo "${name}: a stand-in the module checks render hosts over, which runs nothing" >&2
25 exit 1
26 ''
27 );
28
29 # Each check host shares the machine baseline and supplies its own
30 # declaration (and, where the declaration asks for backup, the secret
31 # paths).
32 #
33 # Each host is also handed the package set the checks already use,
34 # rather than importing nixpkgs again for itself. It is the same nixpkgs
35 # with the same configuration, so every host renders the same
36 # derivations. module-eval evaluates every host here, and importing
37 # nixpkgs once per host was half of its evaluation time.
38 mkHost =
39 module:
40 lib.nixosSystem {
41 inherit pkgs;
42 modules = [
43 self.nixosModules.default
44 {
45 fileSystems."/" = {
46 device = "/dev/disk/by-label/nixos";
47 fsType = "ext4";
48 };
49 boot.loader.grub.enable = false;
50 system.stateVersion = "25.11";
51 services.valley = {
52 enable = true;
53 authorizedKeys = [
54 "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIPlaceholderKeyForEvalOnlyCheck0 valley-check"
55 ];
56 };
57 }
58 (lib.mkIf stubbed { services.valley.packages = stubs; })
59 module
60 ];
61 };
62
63 # The same controllers as restrictedHost's with the check worker on, for
64 # two of the projects they serve, and its signer restricted to exactly
65 # those two. It is bound here so the hosts the module must refuse can be
66 # made from it.
67 workerHost = mkHost {
68 services.valley = {
69 config = ../../examples/hosts/protected.cue;
70 integrator = {
71 enable = true;
72 signingKeyFile = "/run/agenix/valley-integrator-key";
73 knownSignersFile = "/var/lib/valley-instance/known_signers";
74 instanceProject = "sealed";
75 signerProjects."worker.valley.invalid/attestations" = [
76 "sealed"
77 "guarded"
78 ];
79 };
80 worker = {
81 enable = true;
82 projects = [
83 "guarded"
84 "sealed"
85 ];
86 signerName = "worker.valley.invalid/attestations";
87 };
88 };
89 };
90in
91{
92 # The example declaration enables backup, so this host supplies
93 # the four secret paths (eval-only placeholders).
94 host = mkHost {
95 services.valley = {
96 config = ../../examples/host.cue;
97 backup = {
98 repositoryFile = "/run/agenix/valley-restic-repo";
99 passwordFile = "/run/agenix/valley-restic-password";
100 sshKeyFile = "/run/agenix/valley-git-ssh-key";
101 knownHostsFile = "/var/lib/valley-backup/known_hosts";
102 };
103 };
104 };
105
106 # A declaration with no optional field set at all — no backup, no
107 # protection — must keep evaluating exactly as it did before those
108 # fields existed: zero restic machinery, no hook.
109 noBackupHost = mkHost {
110 services.valley.config = ../../examples/hosts/no-backup.cue;
111 };
112
113 # The example declaration with the secret paths left unset: the
114 # module must refuse with assertions naming the missing options.
115 noSecretsHost = mkHost {
116 services.valley.config = ../../examples/host.cue;
117 };
118
119 # Bus enabled, minimal declaration. The bus-e2e check drives this
120 # host's rendered hooks and stream-init against a real server.
121 busHost = mkHost {
122 services.valley = {
123 config = ../../examples/hosts/bus.cue;
124 bus.enable = true;
125 };
126 };
127
128 # Two mirror URLs the sandbox can serve: git resolves a relative
129 # URL against the pushing repo's directory, so the mirror-e2e
130 # check drives the rendered script with no substitution at all —
131 # one reachable sibling repo, one that does not exist.
132 mirrorHost = mkHost {
133 services.valley.config = ../../examples/hosts/mirrors.cue;
134 # Client environment the push boundary does not read stays acceptable.
135 services.openssh.settings.AcceptEnv = "LANG LC_*";
136 };
137
138 # The protection declaration, plus the machine half it needs: keys
139 # bound to the principal names it uses, and the approve and restack
140 # grants held by hand. requester holds both grants, so the hook checks
141 # read a principal whose approve grant the restack grant leaves as it
142 # is. The approve grant is declared under both its names: requester is
143 # named under both, and approver under approve alone. The baseline key
144 # above is anonymous and stays in the list, so this host also renders
145 # the two forms of key side by side.
146 protectedHost = mkHost {
147 services.valley = {
148 config = ../../examples/hosts/protected.cue;
149 authorizedKeys = [
150 {
151 principal = "integrator";
152 key = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIPlaceholderKeyForEvalOnlyCheck1 integrator";
153 }
154 {
155 principal = "contributor";
156 key = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIPlaceholderKeyForEvalOnlyCheck2 contributor";
157 }
158 {
159 principal = "requester";
160 key = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIPlaceholderKeyForEvalOnlyCheck3 requester";
161 }
162 {
163 principal = "restacker";
164 key = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIPlaceholderKeyForEvalOnlyCheck4 restacker";
165 }
166 {
167 principal = "approver";
168 key = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIPlaceholderKeyForEvalOnlyCheck5 approver";
169 }
170 ];
171 grants.request = [ "requester" ];
172 grants.approve = [
173 "approver"
174 "requester"
175 ];
176 grants.restack = [
177 "restacker"
178 "requester"
179 ];
180 # The keys attestations pushed here are checked against: the ones a
181 # controller would accept evidence from, though this host runs none.
182 integrator.knownSignersFile = "/var/lib/valley-instance/known_signers";
183 # released composes a hook of its own after the push policy: it
184 # refuses branches under frozen/, which the policy would accept.
185 extraPreReceive.released = pkgs.writeShellScript "valley-check-released-pre-receive" ''
186 while read -r _ _ ref; do
187 case "$ref" in
188 refs/heads/frozen/*)
189 echo "released: $ref is frozen" >&2
190 exit 1
191 ;;
192 esac
193 done
194 '';
195 };
196 };
197
198 # The same protection declaration with the controllers switched on. The
199 # integrator serves exactly what that declaration protects, so this host
200 # renders two controllers and no third; the paths are eval-only
201 # placeholders, like the backup host's.
202 integratorHost = mkHost {
203 services.valley = {
204 config = ../../examples/hosts/protected.cue;
205 integrator = {
206 enable = true;
207 signingKeyFile = "/run/agenix/valley-integrator-key";
208 knownSignersFile = "/var/lib/valley-instance/known_signers";
209 # The floor comes from "sealed", which declares protection, as the
210 # instance repository has to. Every other controller here reads the
211 # floor out of a repository that is not the one it serves, which is
212 # the cross-repository read the unit has to grant.
213 instanceProject = "sealed";
214 };
215 };
216 };
217
218 # The same controllers with a check worker's signer restricted to two of
219 # the projects they serve, and a retired signer restricted to none.
220 restrictedHost = mkHost {
221 services.valley = {
222 config = ../../examples/hosts/protected.cue;
223 integrator = {
224 enable = true;
225 signingKeyFile = "/run/agenix/valley-integrator-key";
226 knownSignersFile = "/var/lib/valley-instance/known_signers";
227 instanceProject = "sealed";
228 signerProjects = {
229 "worker.valley.invalid/attestations" = [
230 "guarded"
231 "sealed"
232 ];
233 "retired.valley.invalid/attestations" = [ ];
234 };
235 };
236 };
237 };
238
239 inherit workerHost;
240
241 # The registry machinery switched on, over the same declaration. The
242 # signers file is not supplied: the compiler renders it, and a consumer
243 # that also named one would be asking which of the two won. The
244 # integrator's signing name is set here too, because a host that compiles
245 # a registry is a host whose registry publishes the integrator's key under
246 # a name, and the controller has to sign under that same name.
247 identityHost = mkHost {
248 services.valley = {
249 config = ../../examples/hosts/protected.cue;
250 identity.enable = true;
251 integrator = {
252 enable = true;
253 signingKeyFile = "/run/agenix/valley-integrator-key";
254 signingName = "integrator";
255 instanceProject = "sealed";
256 };
257 };
258 };
259
260 # The same host with the instance repository among the served projects —
261 # the shape a group's own instance takes, where the repository carrying
262 # the floor is itself integrated. The controller serving it reads the
263 # floor from the repository it is already serving.
264 integratorSelfHost = mkHost {
265 services.valley = {
266 config = ../../examples/hosts/protected.cue;
267 integrator = {
268 enable = true;
269 signingKeyFile = "/run/agenix/valley-integrator-key";
270 knownSignersFile = "/var/lib/valley-instance/known_signers";
271 instanceProject = "guarded";
272 };
273 };
274 };
275
276 # Hosts the module must refuse. Each is one way for the push policy to be
277 # quietly wrong, and each must fail an assertion naming what is wrong.
278 #
279 # The instance repository unprotected: the floor and the registry would
280 # be read from a main any key could push.
281 unprotectedInstanceHost = mkHost {
282 services.valley = {
283 config = ../../examples/hosts/protected.cue;
284 integrator = {
285 enable = true;
286 signingKeyFile = "/run/agenix/valley-integrator-key";
287 knownSignersFile = "/var/lib/valley-instance/known_signers";
288 instanceProject = "open";
289 };
290 };
291 };
292
293 # sshd accepting the principal variable from the client.
294 acceptEnvHost = mkHost {
295 services.valley.config = ../../examples/hosts/protected.cue;
296 services.openssh.settings.AcceptEnv = "LANG LC_* VALLEY_*";
297 };
298
299 # The same, in the git user's own Match block, with the keyword in another
300 # case: sshd reads keywords in any case.
301 matchAcceptEnvHost = mkHost {
302 services.valley.config = ../../examples/hosts/protected.cue;
303 services.openssh.extraConfig = lib.mkAfter ''
304 Match User git
305 ACCEPTENV GIT_CONFIG_*
306 Match All
307 '';
308 };
309
310 # The principal, single-quoted: sshd strips the quotes and admits it.
311 quotedAcceptEnvHost = mkHost {
312 services.valley.config = ../../examples/hosts/protected.cue;
313 services.openssh.extraConfig = lib.mkAfter "AcceptEnv LANG 'VALLEY_PRINCIPAL'";
314 };
315
316 # The principal, separated from the pattern before it by a tab.
317 tabAcceptEnvHost = mkHost {
318 services.valley.config = ../../examples/hosts/protected.cue;
319 services.openssh.settings.AcceptEnv = "LANG\tVALLEY_PRINCIPAL";
320 };
321
322 # A pattern that names neither variable outright and still admits one.
323 wildcardAcceptEnvHost = mkHost {
324 services.valley.config = ../../examples/hosts/protected.cue;
325 services.openssh.settings.AcceptEnv = "LANG G?T_*";
326 };
327
328 # sshd setting the principal itself, over every key.
329 setEnvHost = mkHost {
330 services.valley.config = ../../examples/hosts/protected.cue;
331 services.openssh.extraConfig = lib.mkAfter "SetEnv VALLEY_PRINCIPAL=patrick";
332 };
333
334 # sshd honouring every variable a key's entry or ~/.ssh/environment sets.
335 permitUserEnvironmentHost = mkHost {
336 services.valley.config = ../../examples/hosts/protected.cue;
337 services.openssh.settings.PermitUserEnvironment = lib.mkForce "yes";
338 };
339
340 # Two projects written as one: rendered one signer per line, the worker
341 # would read back as restricted to guarded and sealed, two projects the
342 # declaration never listed for it.
343 spacedSignerProjectsHost = mkHost {
344 services.valley = {
345 config = ../../examples/hosts/protected.cue;
346 integrator = {
347 enable = true;
348 signingKeyFile = "/run/agenix/valley-integrator-key";
349 knownSignersFile = "/var/lib/valley-instance/known_signers";
350 instanceProject = "sealed";
351 signerProjects."worker.valley.invalid/attestations" = [ "guarded sealed" ];
352 };
353 };
354 };
355
356 # The same two projects joined by a no-break space, U+00A0, which Nix's
357 # [:space:] does not match and Go's unicode.IsSpace does. Nix strings
358 # have no escape for it, so it is decoded from JSON.
359 unicodeSpacedSignerProjectsHost = mkHost {
360 services.valley = {
361 config = ../../examples/hosts/protected.cue;
362 integrator = {
363 enable = true;
364 signingKeyFile = "/run/agenix/valley-integrator-key";
365 knownSignersFile = "/var/lib/valley-instance/known_signers";
366 instanceProject = "sealed";
367 signerProjects."worker.valley.invalid/attestations" = [
368 "guarded${builtins.fromJSON ''"\u00a0"''}sealed"
369 ];
370 };
371 };
372 };
373
374 # The check worker with its signer restricted to no project list at all:
375 # every controller holding its key would count its evidence everywhere.
376 unrestrictedWorkerHost = workerHost.extendModules {
377 modules = [ { services.valley.integrator.signerProjects = lib.mkForce { }; } ];
378 };
379
380 # The worker's signer restricted to a project the worker does not build:
381 # its key would be trusted where it does no work.
382 widerWorkerHost = workerHost.extendModules {
383 modules = [
384 {
385 services.valley.integrator.signerProjects = lib.mkForce {
386 "worker.valley.invalid/attestations" = [
387 "guarded"
388 "released"
389 "sealed"
390 ];
391 };
392 }
393 ];
394 };
395
396 # The worker signing under the controllers' own name.
397 integratorNamedWorkerHost = workerHost.extendModules {
398 modules = [
399 {
400 services.valley.integrator.signingName = "integrator";
401 services.valley.integrator.signerProjects = lib.mkForce {
402 integrator = [
403 "guarded"
404 "sealed"
405 ];
406 };
407 services.valley.worker.signerName = lib.mkForce "integrator";
408 }
409 ];
410 };
411
412 # One key declared untagged and tagged: which principal it pushes as
413 # would depend on which line sshd reads first.
414 conflictingKeysHost = mkHost {
415 services.valley = {
416 config = ../../examples/hosts/protected.cue;
417 authorizedKeys = [
418 "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIPlaceholderKeyForEvalOnlyCheck2 contributor"
419 {
420 principal = "contributor";
421 key = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIPlaceholderKeyForEvalOnlyCheck2 contributor";
422 }
423 ];
424 };
425 };
426}