gunk-dev/the-valley

hosts.nix

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