gunk-dev/the-valley

hosts.nix

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