gunk-dev/the-valley

hosts.nix

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