gunk-dev/the-valley

host-module.nix

source215012f1aaa7main's published tip history
37.9 KiB
1# What the NixOS module renders from a declaration, and what it must refuse.
2# The web publisher's module, which sits beside it, is held here too.
3# The refusals are eval-time: a host that must not evaluate is caught by a
4# throw here, since there is nothing to build once it has. What a host does
5# render is pinned by module-eval.sh over the rendered scripts.
6#
7# Every host here runs the stand-ins for this flake's programs
8# (nix/checks/hosts.nix). Nothing here runs a program: each assertion is
9# about the units, hooks and scripts the module renders, which name a
10# program's path and the flags it is given. So module-eval changes when the
11# module, a host declaration or this check does, and not when a program
12# does.
13{
14 pkgs,
15 lib,
16 self,
17 stubbedHosts,
18 ...
19}:
20let
21 inherit (stubbedHosts)
22 unprotectedInstanceHost
23 acceptEnvHost
24 matchAcceptEnvHost
25 wildcardAcceptEnvHost
26 quotedAcceptEnvHost
27 tabAcceptEnvHost
28 setEnvHost
29 permitUserEnvironmentHost
30 conflictingKeysHost
31 host
32 noBackupHost
33 noSecretsHost
34 protectedHost
35 busHost
36 mirrorHost
37 integratorHost
38 integratorSelfHost
39 restrictedHost
40 spacedSignerProjectsHost
41 unicodeSpacedSignerProjectsHost
42 identityHost
43 workerHost
44 buildSliceHost
45 noSliceWorkerHost
46 unrestrictedWorkerHost
47 widerWorkerHost
48 integratorNamedWorkerHost
49 ;
50
51 # The web publisher's module (nix/webui-publisher.nix) beside the host
52 # module, over integratorHost: its instance repository is the one the
53 # controllers read the floor from. The host also has a backup key the
54 # publisher must not read. The publisher is a stand-in, as the host's
55 # programs are. webPublisherOffHost imports the module and leaves it off.
56 webPublisherStub = pkgs.writeShellScriptBin "valley-web-publish" ''
57 echo "valley-web-publish: a stand-in the module checks render hosts over, which runs nothing" >&2
58 exit 1
59 '';
60 webPublisherOffHost = stubbedHosts.integratorHost.extendModules {
61 modules = [
62 self.nixosModules.webui-publisher
63 { services.valley.webPublisher.package = webPublisherStub; }
64 ];
65 };
66 webPublisherHost = webPublisherOffHost.extendModules {
67 modules = [
68 {
69 networking.hostName = "valley-host";
70 services.valley.backup.sshKeyFile = "/run/agenix/valley-git-ssh-key";
71 services.valley.webPublisher = {
72 enable = true;
73 destination = "example.org";
74 receiver = "https://example.org";
75 principals = {
76 operator = "operator";
77 alice = "agent";
78 };
79 };
80 }
81 ];
82 };
83 # The publisher with no instance repository to discover the projects and
84 # the signers through.
85 noInstanceWebPublisherHost = webPublisherHost.extendModules {
86 modules = [
87 {
88 services.valley.integrator = {
89 instanceProject = lib.mkForce null;
90 instancePolicy = "/var/lib/valley-instance/policy/instance";
91 };
92 }
93 ];
94 };
95
96 failedAssertions = builtins.filter (a: !a.assertion) (
97 host.config.assertions
98 ++ noBackupHost.config.assertions
99 ++ busHost.config.assertions
100 ++ mirrorHost.config.assertions
101 ++ protectedHost.config.assertions
102 ++ integratorHost.config.assertions
103 ++ integratorSelfHost.config.assertions
104 ++ restrictedHost.config.assertions
105 ++ identityHost.config.assertions
106 ++ workerHost.config.assertions
107 ++ buildSliceHost.config.assertions
108 ++ webPublisherHost.config.assertions
109 ++ webPublisherOffHost.config.assertions
110 );
111
112 missingSecretAssertions = builtins.filter (a: !a.assertion) noSecretsHost.config.assertions;
113
114 # The hosts the module must refuse, each with the words its refusal must
115 # say. An assertion that stopped firing would leave the push policy
116 # quietly wrong, so each is held to naming what is wrong.
117 refusedHosts = [
118 {
119 host = unprotectedInstanceHost;
120 says = "whose refs/heads/main is not protected";
121 why = "an instance repository without protection would let any key that can push rewrite the floor and the registry";
122 }
123 {
124 host = acceptEnvHost;
125 says = "AcceptEnv";
126 why = "sshd accepting the principal variable from the client would let any key name any principal";
127 }
128 {
129 host = matchAcceptEnvHost;
130 says = "GIT_CONFIG_*";
131 why = "sshd accepting GIT_ variables in the git user's Match block, in whatever case the keyword is written, would let a client set git's configuration";
132 }
133 {
134 host = wildcardAcceptEnvHost;
135 says = "G?T_*";
136 why = "a wildcard that admits GIT_ variables admits them however it is spelled";
137 }
138 {
139 host = quotedAcceptEnvHost;
140 says = "holds a quote or a backslash";
141 why = "a quoted name is one sshd unquotes and admits, so a check that read it differently would pass it";
142 }
143 {
144 host = tabAcceptEnvHost;
145 says = "VALLEY_PRINCIPAL from the client";
146 why = "a name after a tab is a name sshd admits";
147 }
148 {
149 host = setEnvHost;
150 says = "SetEnv";
151 why = "sshd setting the principal itself would make every key push as it";
152 }
153 {
154 host = permitUserEnvironmentHost;
155 says = "PermitUserEnvironment";
156 why = "a PermitUserEnvironment wider than the principal variable would let a key's entry or ~/.ssh/environment set what the push policy does not expect";
157 }
158 {
159 host = spacedSignerProjectsHost;
160 says = "services.valley.integrator.signerProjects holds";
161 why = "a project with a space in it would be read back as two projects the declaration never listed";
162 }
163 {
164 host = unicodeSpacedSignerProjectsHost;
165 says = "services.valley.integrator.signerProjects holds";
166 why = "a project holding a no-break space would be read back by the integrator as two projects, or refused at start";
167 }
168 {
169 host = noSliceWorkerHost;
170 says = "services.valley.worker.enable needs services.valley.buildSlice.enable";
171 why = "a worker on a host without the build slice would build uncapped, and capping every build of the daemon is the host's decision, not the worker's";
172 }
173 {
174 host = unrestrictedWorkerHost;
175 says = "must restrict worker.valley.invalid/attestations, the worker's signer, to exactly the worker's projects";
176 why = "a worker whose signer no project list restricts would have its evidence counted in every project by every controller that holds its key";
177 }
178 {
179 host = widerWorkerHost;
180 says = "must restrict worker.valley.invalid/attestations, the worker's signer, to exactly the worker's projects";
181 why = "a worker trusted in a project it does not build would be trusted where it does no work";
182 }
183 {
184 host = integratorNamedWorkerHost;
185 says = "services.valley.worker.signerName must differ from integrator";
186 why = "a worker signing under the controllers' name would make every controller refuse to start";
187 }
188 {
189 host = conflictingKeysHost;
190 says = "more than one principal tag";
191 why = "a key declared under two tags would push as whichever entry sshd reads first";
192 }
193 {
194 host = noInstanceWebPublisherHost;
195 says = "services.valley.webPublisher needs services.valley.integrator.instanceProject";
196 why = "a publisher with no instance repository has no publication to discover the projects and their signers through";
197 }
198 ];
199
200 unrefusedHosts = builtins.filter (
201 r: !(lib.any (a: !a.assertion && lib.hasInfix r.says a.message) r.host.config.assertions)
202 ) refusedHosts;
203
204 protectedKeyLines = protectedHost.config.users.users.git.openssh.authorizedKeys.keys;
205
206 resticRenderedWithoutDeclaration =
207 noBackupHost.config.systemd.services ? "restic-backups-valley"
208 || noBackupHost.config.systemd.timers ? "restic-backups-valley";
209
210 # The bus defaults off; a host that never asked for one must
211 # render zero bus machinery.
212 busRenderedWithoutEnable =
213 noBackupHost.config.systemd.services ? valley-bus
214 || noBackupHost.config.systemd.services ? valley-bus-init;
215
216 # A declaration with no protection block and no tagged key still gets
217 # the push policy on every project — the policy is the host's, and a
218 # protection block only adds protected refs to it — and nothing of the
219 # principal machinery: no tag on a key, no sshd change.
220 protectionRenderedWithoutDeclaration =
221 noBackupHost.config.services.openssh.settings ? PermitUserEnvironment
222 || lib.any (lib.hasInfix "environment=") (
223 noBackupHost.config.users.users.git.openssh.authorizedKeys.keys
224 );
225
226 policyMissingWithoutDeclaration =
227 !(lib.hasInfix "valley-protect-the-valley" noBackupHost.config.systemd.services.valley-init.script);
228
229 # The integrator's units. What a controller *does* is not checked here
230 # and no VM is booted: that is integrator-e2e's job (the real binaries
231 # over real repositories) and, past it, live exercise on a host. These
232 # assertions cover the wiring — that a controller is rendered for what
233 # the declaration protects and for nothing else, under an identity that
234 # is not the git user's, pointed at the key and the signers the consumer
235 # supplied.
236 #
237 # There is a seam between those two halves that neither reaches, and in a
238 # single day it produced five deployment failures. Every assertion below is
239 # an eval of the unit's text; integrator-e2e runs the binaries with no unit
240 # around them at all. What sits between is the unit's sandbox and identity
241 # meeting the filesystem the binaries actually use at runtime:
242 #
243 # - git refusing a repository the service does not own (bd-500adf7)
244 # - a push authenticating as the operator rather than as the run
245 # (bd-353c59d)
246 # - no writable temporary directory, so no worktree could be made
247 # - the worktree metadata root, created by whichever service reached it
248 # first and then unchmodable by the other, which failed valley-init and
249 # took every valley unit down with it
250 # - the state directory not writable under ProtectSystem=strict unless
251 # the unit names it, which the previous fix had made the scratch root
252 #
253 # Each was found by a host, minutes of a live service after a deploy that
254 # every check here passed. They compose: the third's fix set up the fourth,
255 # and the fourth's fix set up the fifth. That is the argument. A seam whose
256 # failures feed each other is not one to pin an assertion at a time, and
257 # each assertion here that marks a piece of it — the git ownership
258 # exception, TMPDIR, the worktrees root, ReadWritePaths — marks a place a
259 # smoke boot would have spoken first.
260 #
261 # A nixosTest that boots one host, starts the units and reconciles one
262 # change end to end is the next check this module needs. Nothing else
263 # closes the seam, and five incidents is enough evidence of what it costs
264 # to leave open. Not built here.
265 #
266 # Protection and integration are separable declarations: protectedHost
267 # declares the same protected projects with the service off, and must
268 # evaluate to no controller at all.
269 integratorRenderedWithoutEnable =
270 protectedHost.config.systemd.services ? "valley-integrator@"
271 || protectedHost.config.systemd.targets ? valley-integrators
272 || lib.hasInfix "sharedRepository" protectedHost.config.systemd.services.valley-init.script;
273
274 # One target pulls in each controller, and another the path unit that
275 # publishes each project's queued moves, landings included.
276 integratorWants = integratorHost.config.systemd.targets.valley-integrators.wants;
277 controllers = lib.filter (lib.hasPrefix "valley-integrator@") integratorWants;
278 publishers = integratorHost.config.systemd.targets.valley-publish.wants;
279
280 integratorUser = integratorHost.config.systemd.services."valley-integrator@".serviceConfig.User;
281
282 # The controller is not the repositories' owner, which is the whole point
283 # of running it as itself — so git needs an ownership exception, and
284 # where that exception lives is load-bearing. GIT_CONFIG_* is command
285 # scope, the only scope besides system and global that safe.directory is
286 # read from, and the unit's environment is inherited by every git the
287 # controller drives. It names one repository: a wildcard here would hand
288 # a compromised controller every repository on the host.
289 integratorGitEnv = integratorHost.config.systemd.services."valley-integrator@".environment;
290
291 # The count is what git reads; an entry past it is an entry git ignores,
292 # silently. So it is checked against the entries actually rendered rather
293 # than against a number written here, which would have to be edited every
294 # time a grant is added and would go on passing if it were not.
295 integratorSafeDirectory =
296 integratorGitEnv.GIT_CONFIG_KEY_0 or null != "safe.directory"
297 || integratorGitEnv.GIT_CONFIG_VALUE_0 or null != "/srv/git/%i.git"
298 || integratorGitEnv.GIT_CONFIG_COUNT or null
299 != toString (
300 lib.length (lib.filter (lib.hasPrefix "GIT_CONFIG_KEY_") (lib.attrNames integratorGitEnv))
301 );
302
303 integratorService = integratorHost.config.systemd.services."valley-integrator@".serviceConfig;
304
305 # Scratch goes under the state directory, because the sandbox leaves no
306 # writable temporary directory and a verdict needs a checkout on disk. Two
307 # halves, and each was a live failure on its own. TMPDIR naming a path
308 # outside the state directory is the first; the state directory not being
309 # named in ReadWritePaths is the second, which failed even though
310 # ProtectSystem=strict is documented to leave a declared state directory
311 # writable. Everything the unit may write is enumerated, so what a reader
312 # of the unit sees is the whole of it.
313 # The floor is read from the instance repository's integrated tip
314 # (dcr-f41f718, dcr-b87f6e8), so a controller is told which repository
315 # carries it and reads it there. Three things have to be true of that, and
316 # each was a way for it to be silently wrong.
317 #
318 # The controller has to be told at all — the failure this replaced was a
319 # binary looking for a materialized directory nothing populated.
320 #
321 # It has to work when the repository carrying the floor is not the one the
322 # controller serves, which is the ordinary case: on integratorHost every
323 # controller serves a protected project and reads the floor out of
324 # "open", which no controller serves. git refuses a repository the caller
325 # does not own, so that read needs its own safe.directory entry, named and
326 # not wildcarded — the same failure as the served repository's, one
327 # repository over.
328 #
329 # And it has to keep working when the two coincide, which is what an
330 # instance's own host looks like: integratorSelfHost's controller for
331 # "guarded" reads the floor from "guarded".
332 integratorFloor =
333 let
334 command = integratorHost.config.systemd.services."valley-integrator@".serviceConfig.ExecStart;
335 selfCommand = integratorSelfHost.config.systemd.services."valley-integrator@".serviceConfig.ExecStart;
336 env = integratorHost.config.systemd.services."valley-integrator@".environment;
337 in
338 !(lib.hasInfix "--instance-repo /srv/git/sealed.git" command)
339 || !(lib.hasInfix "--instance-repo /srv/git/guarded.git" selfCommand)
340 || env.GIT_CONFIG_COUNT or null != "2"
341 || env.GIT_CONFIG_KEY_1 or null != "safe.directory"
342 || env.GIT_CONFIG_VALUE_1 or null != "/srv/git/sealed.git";
343
344 integratorScratch =
345 integratorGitEnv.TMPDIR or null != "/var/lib/${integratorService.StateDirectory}"
346 || !(builtins.elem "/var/lib/${integratorService.StateDirectory}" integratorService.ReadWritePaths);
347
348 # The registry compiler. It is off by default and must stay inert: a
349 # consumer that has not asked for it renders the system it always did.
350 # integratorHost is the strongest witness available — the integrator on,
351 # the signers file supplied by hand, and every option of the identity
352 # block left alone.
353 identityRenderedWithoutEnable =
354 integratorHost.config.systemd.services ? valley-identity
355 || integratorHost.config.systemd.timers ? valley-identity
356 || integratorHost.config.services.openssh.settings ? PermitUserEnvironment
357 || lib.any (lib.hasInfix "valley-identity") integratorHost.config.services.openssh.authorizedKeysFiles
358 || lib.hasInfix "valley-identity" integratorHost.config.services.openssh.extraConfig
359 || lib.hasInfix "valley-identity" (
360 integratorHost.config.systemd.services."valley-integrator@".serviceConfig.ExecStart
361 )
362 || lib.hasInfix "--name " (
363 integratorHost.config.systemd.services."valley-integrator@".serviceConfig.ExecStart
364 );
365
366 identityService = identityHost.config.systemd.services.valley-identity.serviceConfig;
367
368 identityStateDir = "/var/lib/${identityService.StateDirectory or ""}";
369
370 # What the compiled artifacts have to be true of, and each was a way for
371 # the machinery to be quietly wrong.
372 #
373 # The compiler writes into its state directory and nowhere else, so that
374 # directory has to be the one path the unit may write.
375 #
376 # sshd walks every directory above an authorized-keys file and refuses a
377 # group- or world-writable one, so the state directory is 0755 rather than
378 # the 0700 a service's state usually takes — and the unit runs as the git
379 # user, because sshd equally refuses a file owned by anyone but root or
380 # the account it authorizes.
381 #
382 # The compiled file is added to what sshd reads for the git user rather
383 # than replacing it, and for the git user alone: the user's Match block
384 # names the declared keys and the compiled ones, and nothing else — no
385 # ~/.ssh/authorized_keys, no AuthorizedKeysCommand — so every key the git
386 # user accepts carries a tag this module or the compiler wrote. The
387 # compiler is handed the declared keys, so it can refuse a registry key
388 # declared under another tag.
389 identityCompiler =
390 identityService.User or null != identityHost.config.services.valley.user
391 || identityService.StateDirectoryMode or null != "0755"
392 || identityService.ReadWritePaths or [ ] != [ identityStateDir ]
393 || !(lib.hasInfix "--repo /srv/git/sealed.git" identityService.ExecStart)
394 || !(lib.hasInfix "--known-signers ${identityStateDir}/known-signers" identityService.ExecStart)
395 || !(lib.hasInfix "--authorized-keys ${identityStateDir}/authorized_keys.git" identityService.ExecStart)
396 || !(lib.hasInfix "--grants ${identityStateDir}/grants" identityService.ExecStart)
397 || !(lib.hasInfix "-valley-declared-keys" identityService.ExecStart)
398 || !(lib.hasInfix "AuthorizedKeysFile /etc/ssh/authorized_keys.d/%u ${identityStateDir}/authorized_keys.git\n" identityHost.config.services.openssh.extraConfig)
399 || !(lib.hasInfix "AuthorizedKeysCommand none" identityHost.config.services.openssh.extraConfig);
400
401 # A controller under a compiled registry reads the compiled signers file
402 # and signs under the name the registry publishes its key under. The key
403 # hash binds name to key, so a controller signing under the host's default
404 # name while the registry publishes it as "integrator" would produce notes
405 # no verifier can find a key for.
406 identityIntegrator =
407 let
408 command = identityHost.config.systemd.services."valley-integrator@".serviceConfig.ExecStart;
409 in
410 !(lib.hasInfix "--known-signers ${identityStateDir}/known-signers" command)
411 || !(lib.hasInfix "--name integrator" command)
412 || !(builtins.elem "valley-identity.service"
413 identityHost.config.systemd.services."valley-integrator@".after
414 );
415
416 # The signers publication. A host whose integrator runs and which serves
417 # the instance repository publishes there what a client verifies its
418 # transfer statements with. What it publishes has to be what the
419 # controllers use: the same signers file and the same --name, or none on
420 # either, in which case both take the binary's default. The publisher
421 # writes as the git user, which owns the instance repository, may write
422 # that repository and nothing else, runs after the compilation when there
423 # is one, and runs again whenever the signers file changes. The signer
424 # projects file is the controllers' too, or neither has one.
425 flagValue =
426 flag: command:
427 let
428 m = builtins.match ".*--${flag} ([^ ]+).*" command;
429 in
430 if m == null then null else lib.head m;
431
432 signersPublisher =
433 host: repo:
434 let
435 unit = host.config.systemd.services.valley-signers;
436 publish = unit.serviceConfig.ExecStart;
437 controller = host.config.systemd.services."valley-integrator@".serviceConfig.ExecStart;
438 in
439 !(lib.hasInfix "/bin/integrator publish-signers " publish)
440 || flagValue "instance-repo" publish != repo
441 || flagValue "known-signers" publish != flagValue "known-signers" controller
442 || flagValue "name" publish != flagValue "name" controller
443 || flagValue "signer-projects" publish != flagValue "signer-projects" controller
444 || unit.serviceConfig.User or null != "git"
445 || unit.serviceConfig.ReadWritePaths or [ ] != [ repo ]
446 || !(builtins.elem "valley-init.service" unit.after)
447 || !(builtins.elem "multi-user.target" unit.wantedBy)
448 || host.config.systemd.paths.valley-signers.pathConfig.PathChanged or null
449 != flagValue "known-signers" controller
450 || !(builtins.elem "multi-user.target" host.config.systemd.paths.valley-signers.wantedBy);
451
452 signersRenderedWithoutIntegrator =
453 protectedHost.config.systemd.services ? valley-signers
454 || protectedHost.config.systemd.paths ? valley-signers;
455
456 signersUnpublished =
457 signersPublisher integratorHost "/srv/git/sealed.git"
458 || signersPublisher restrictedHost "/srv/git/sealed.git"
459 || signersPublisher integratorSelfHost "/srv/git/guarded.git"
460 || signersPublisher identityHost "/srv/git/sealed.git"
461 || flagValue "name" identityHost.config.systemd.services.valley-signers.serviceConfig.ExecStart
462 != "integrator"
463 || !(builtins.elem "valley-identity.service" identityHost.config.systemd.services.valley-signers.after);
464
465 # A host that restricts no signer gives its controllers no signer
466 # projects file, and one that restricts some gives them one.
467 signerProjectsMisrendered =
468 flagValue "signer-projects" integratorHost.config.systemd.services."valley-integrator@".serviceConfig.ExecStart
469 != null
470 || flagValue "signer-projects" restrictedHost.config.systemd.services."valley-integrator@".serviceConfig.ExecStart
471 == null;
472
473 # The declared keys stay authorized beside the compiled ones. A compiled
474 # file that replaced them would make a bad compilation — or a first boot
475 # before the first one — a locked-out git user, which is the one failure
476 # this machinery must never have.
477 identityKeepsDeclaredKeys =
478 identityHost.config.users.users.git.openssh.authorizedKeys.keys == [ ];
479
480 # The check worker and the build slice. Both are off by default, and a
481 # host that never asked for them renders none of either: no unit, no
482 # account, no slice, and a nix daemon where NixOS puts it. integratorHost
483 # is the strongest witness, with the integrator the worker would need
484 # already on.
485 workerRenderedWithoutEnable =
486 integratorHost.config.systemd.services ? valley-worker
487 || integratorHost.config.systemd.timers ? valley-worker
488 || integratorHost.config.systemd.slices ? valley-builds
489 || integratorHost.config.users.users ? valley-worker
490 || integratorHost.config.systemd.services.nix-daemon.serviceConfig ? Slice;
491
492 # The build slice is a host decision of its own. On, it puts the daemon
493 # in the slice at the caps the host names, with no swap, and nothing of
494 # the worker.
495 buildSliceMisrendered =
496 let
497 slice = buildSliceHost.config.systemd.slices.valley-builds.sliceConfig or { };
498 in
499 buildSliceHost.config.systemd.services.nix-daemon.serviceConfig.Slice or null
500 != "valley-builds.slice"
501 || slice.CPUQuota or null != "200%"
502 || slice.MemoryMax or null != "4G"
503 || slice.MemorySwapMax or null != "0"
504 || buildSliceHost.config.systemd.services ? valley-worker
505 || buildSliceHost.config.users.users ? valley-worker;
506
507 # What the VM check runs, held at the operator's values. worker-e2e
508 # boots the worker and lowers the memory cap at runtime to make a build
509 # run past it, so the values themselves are held here. The caps are on
510 # a slice that holds the worker and the nix daemon both, since a build
511 # runs in the daemon's cgroup and never in the worker's; the slice and
512 # the daemon set no device policy, so a NixOS test still gets /dev/kvm.
513 # The worker runs on one CPU, so attest, which runs one nix build per CPU
514 # it may run on, runs one at a time, and each nix build builds one
515 # derivation at a time with 4 cores. The account is not in the git
516 # group, so the repositories on disk are not the worker's to read or
517 # write, and it reaches them only through this host's sshd, checking the
518 # host's key.
519 workerMisrendered =
520 let
521 unit = workerHost.config.systemd.services.valley-worker;
522 service = unit.serviceConfig;
523 daemon = workerHost.config.systemd.services.nix-daemon.serviceConfig;
524 slice = workerHost.config.systemd.slices.valley-builds.sliceConfig;
525 account = workerHost.config.users.users.valley-worker;
526 timer = workerHost.config.systemd.timers.valley-worker;
527 devicePolicy = c: c ? DevicePolicy || c ? DeviceAllow || c ? PrivateDevices;
528 in
529 service.User or null != "valley-worker"
530 || account.group != "valley-worker"
531 || account.extraGroups != [ ]
532 || service.Type or null != "oneshot"
533 || service.Slice or null != "valley-builds.slice"
534 || daemon.Slice or null != "valley-builds.slice"
535 || slice.CPUQuota or null != "400%"
536 || slice.MemoryMax or null != "8G"
537 || slice.MemorySwapMax or null != "0"
538 || devicePolicy slice
539 || devicePolicy daemon
540 || service.CPUAffinity or null != "0"
541 || !(lib.hasInfix "max-jobs = 1\n" unit.environment.NIX_CONFIG)
542 || !(lib.hasInfix "cores = 4\n" unit.environment.NIX_CONFIG)
543 || !(lib.hasInfix "builders =\n" unit.environment.NIX_CONFIG)
544 || !(lib.hasInfix "-o StrictHostKeyChecking=yes -o HostKeyAlias=localhost" unit.environment.GIT_SSH_COMMAND)
545 || service.ReadWritePaths or [ ] != [
546 "/var/lib/valley-worker"
547 "/nix/var/nix/daemon-socket"
548 ]
549 || !(lib.hasInfix "exec valley-worker --repo guarded --repo sealed --key \"$key\" --name worker.valley.invalid/attestations --state \"$state\"" unit.script)
550 || !(lib.hasInfix "git -C \"$origin\" remote add origin git@localhost:sealed.git\n" unit.script)
551 || timer.timerConfig.OnUnitInactiveSec or null != "1m"
552 || !(builtins.elem "timers.target" timer.wantedBy);
553
554 # The web publisher (nix/webui-publisher.nix). Off, it renders nothing.
555 # On, one pass is a oneshot on a five-minute timer whose first pass waits
556 # five minutes after the timer starts, and which no switch starts or
557 # restarts. It runs as a dynamic user in the git group, with an exact
558 # safe.directory entry for each repository the host serves and no
559 # wildcard, the repositories read-only, nothing writable but its state
560 # and cache, and the mirror, backup and signing keys out of reach. It
561 # reads the instance repository the controllers read, names the host as
562 # the instance, and is given the receiver, the destination and the
563 # display mapping.
564 webPublisherRenderedWithoutEnable =
565 webPublisherOffHost.config.systemd.services ? valley-web-publish
566 || webPublisherOffHost.config.systemd.timers ? valley-web-publish;
567
568 webPublisherMisrendered =
569 let
570 unit = webPublisherHost.config.systemd.services.valley-web-publish;
571 service = unit.serviceConfig;
572 env = unit.environment;
573 timer = webPublisherHost.config.systemd.timers.valley-web-publish;
574 repos = [
575 "/srv/git/guarded.git"
576 "/srv/git/open.git"
577 "/srv/git/released.git"
578 "/srv/git/sealed.git"
579 ];
580 entries = lib.genList (i: {
581 key = env."GIT_CONFIG_KEY_${toString i}" or null;
582 value = env."GIT_CONFIG_VALUE_${toString i}" or null;
583 }) (lib.length (lib.filter (lib.hasPrefix "GIT_CONFIG_KEY_") (lib.attrNames env)));
584 command = service.ExecStart;
585 in
586 service.Type or null != "oneshot"
587 || service.DynamicUser or null != true
588 || service ? User
589 || service.SupplementaryGroups or [ ] != [ "git" ]
590 || env.GIT_CONFIG_COUNT or null != toString (lib.length repos)
591 || entries != map (r: {
592 key = "safe.directory";
593 value = r;
594 }) repos
595 || lib.any (e: lib.hasInfix "*" e.value) entries
596 || service.ProtectSystem or null != "strict"
597 || service ? ReadWritePaths
598 || service.ReadOnlyPaths or [ ] != map (r: "-${r}") repos
599 || service.StateDirectory or null != "valley-web-publish"
600 || service.CacheDirectory or null != "valley-web-publish"
601 || env.XDG_CACHE_HOME or null != "/var/cache/valley-web-publish"
602 || !(lib.all (p: builtins.elem p service.InaccessiblePaths) [
603 "-/srv/git/.ssh"
604 "-/run/agenix/valley-integrator-key"
605 "-/run/agenix/valley-git-ssh-key"
606 "-/var/lib/valley-integrator"
607 ])
608 || !(lib.hasInfix "/bin/valley-web-publish --instance-repo /srv/git/sealed.git --instance valley-host --destination example.org --receiver https://example.org --principals /nix/store/" command)
609 || !(lib.hasInfix "-valley-web-principals.json --state /var/lib/valley-web-publish" command)
610 || unit.restartIfChanged != false
611 || unit.wantedBy != [ ]
612 || timer.timerConfig.OnActiveSec or null != "5min"
613 || timer.timerConfig.OnUnitInactiveSec or null != "5min"
614 || !(builtins.elem "timers.target" timer.wantedBy);
615in
616{
617 module-eval =
618 if failedAssertions != [ ] then
619 throw "valley module-eval: failed assertions: ${
620 lib.concatMapStringsSep "; " (a: a.message) failedAssertions
621 }"
622 else if resticRenderedWithoutDeclaration then
623 throw "valley module-eval: restic machinery rendered for a declaration without a backup block"
624 else if busRenderedWithoutEnable then
625 throw "valley module-eval: bus machinery rendered without services.valley.bus.enable"
626 else if
627 !(lib.any (a: lib.hasInfix "services.valley.backup." a.message) missingSecretAssertions)
628 then
629 throw "valley module-eval: enabling backup without the secret-path options must fail an assertion naming them"
630 else if unrefusedHosts != [ ] then
631 throw "valley module-eval: ${lib.concatMapStringsSep "; " (r: "a host was not refused although ${r.why}") unrefusedHosts}"
632 else if protectionRenderedWithoutDeclaration then
633 throw "valley module-eval: principal machinery rendered for a declaration with no protection block and no tagged key"
634 else if policyMissingWithoutDeclaration then
635 throw "valley module-eval: a project with no protection block got no pre-receive hook — the push policy is installed on every project a host serves"
636 else if protectedHost.config.services.openssh.settings ? PermitUserEnvironment then
637 throw "valley module-eval: sshd must pass no key's environment on: the git user's shell derives the principal from the authenticated key"
638 else if
639 !(lib.hasInfix "valley-git-shell" protectedHost.config.users.users.git.shell)
640 then
641 throw "valley module-eval: the git user's login shell must be the valley's, which derives the principal and pauses pushes until init converges"
642 else if integratorRenderedWithoutEnable then
643 throw "valley module-eval: integrator machinery rendered for a protected declaration that never enabled the service"
644 else if !(integratorHost.config.systemd.services ? "valley-integrator@") then
645 throw "valley module-eval: the integrator is enabled and no controller unit was rendered"
646 else if
647 controllers != [
648 "valley-integrator@guarded.service"
649 "valley-integrator@released.service"
650 "valley-integrator@sealed.service"
651 ]
652 then
653 throw "valley module-eval: a controller runs for each protected project and no other, not ${lib.concatStringsSep ", " controllers}"
654 else if
655 publishers != [
656 "valley-publish@guarded.path"
657 "valley-publish@released.path"
658 "valley-publish@sealed.path"
659 ]
660 then
661 throw "valley module-eval: each controller's landings are published by a path unit of its own, not ${lib.concatStringsSep ", " publishers}"
662 else if integratorHost.config.systemd.services."valley-publish@".serviceConfig.User != "git" then
663 throw "valley module-eval: a landing is published as the git user, which holds the mirror credentials"
664 else if integratorUser == integratorHost.config.services.valley.user then
665 throw "valley module-eval: the integrator must not run as the git user — it has its own (bd-500adf7)"
666 else if !(integratorHost.config.users.users ? ${integratorUser}) then
667 throw "valley module-eval: the integrator runs as ${integratorUser}, which the module never declares"
668 else if integratorHost.config.users.users.${integratorUser}.group != "git" then
669 throw "valley module-eval: the integrator reaches the repositories through the git group, and holds no other grant"
670 else if integratorSafeDirectory then
671 throw "valley module-eval: a controller must carry safe.directory for the one repository it serves in its own environment — it does not own the repositories, and git refuses what it does not own"
672 else if integratorFloor then
673 throw "valley module-eval: a controller must be told which repository carries the floor, and must carry a safe.directory exception for it — the floor is read from that repository's integrated tip, and git refuses a repository its caller does not own"
674 else if integratorScratch then
675 throw "valley module-eval: a controller's TMPDIR must be its state directory and that directory must be named in ReadWritePaths — under ProtectSystem=strict a path the unit does not enumerate is one it cannot write, and a verdict needs a worktree on disk"
676 else if identityRenderedWithoutEnable then
677 throw "valley module-eval: registry-compilation machinery rendered for a host that never enabled it — nothing may change until a consumer opts in"
678 else if identityCompiler then
679 throw "valley module-eval: the compiler must run as the git user, write only its own 0755 state directory, read the registry from the instance repository, write the grants the pre-receive hook reads, and have its authorized_keys added to what sshd reads under the %u token — sshd refuses a keys file owned by anyone but root or the account it authorizes, and a fixed path in that global list would authorize every registry key for every account on the host"
680 else if identityIntegrator then
681 throw "valley module-eval: a controller under a compiled registry must read the compiled signers file, start after the compilation, and sign under the name the registry publishes its key under"
682 else if signersRenderedWithoutIntegrator then
683 throw "valley module-eval: a signers publication rendered for a host whose integrator is off — there is no transfer statement to verify"
684 else if signersUnpublished then
685 throw "valley module-eval: the signers publisher must publish the controllers' own signers file and signing name into the instance repository, as the git user, writing nothing else, after valley-init and the compilation, at boot and whenever the signers file changes"
686 else if signerProjectsMisrendered then
687 throw "valley module-eval: a controller is given a signer projects file exactly when the host restricts a signer to projects"
688 else if identityKeepsDeclaredKeys then
689 throw "valley module-eval: the declared authorized keys must stay authorized beside the compiled ones — a compiled file that replaced them would make a bad compilation a locked-out git user"
690 else if workerRenderedWithoutEnable then
691 throw "valley module-eval: check-worker machinery rendered for a host that never enabled it — nothing, the nix daemon's slice included, may change until a consumer opts in"
692 else if buildSliceMisrendered then
693 throw "valley module-eval: the build slice must put the nix daemon in valley-builds.slice at the quota and memory cap the host names, with no swap, and render nothing of the worker"
694 else if workerMisrendered then
695 throw "valley module-eval: the check worker must run as its own account outside the git group, on one CPU, with one derivation built at a time on 4 cores, in a slice that holds it and the nix daemon to 4 CPUs and 8 GiB with no swap and no device policy, reaching this host's projects through its sshd under the host's own key, on a timer counted from the end of each pass"
696 else if webPublisherRenderedWithoutEnable then
697 throw "valley module-eval: web publisher machinery rendered for a host that imports its module and never enabled it"
698 else if webPublisherMisrendered then
699 throw "valley module-eval: the web publisher must be a oneshot on a five-minute timer whose first pass waits five minutes and which no switch starts, run as a dynamic user in the git group with one exact safe.directory entry per served repository, the repositories read-only, only its state and cache writable and the host's keys inaccessible, reading the instance repository and naming the host, the receiver, the destination and the display mapping"
700 else
701 pkgs.runCommand "valley-module-eval" {
702 initScript = host.config.systemd.services.valley-init.script;
703 sshdConfig = host.config.services.openssh.extraConfig;
704 resticService = host.config.systemd.units."restic-backups-valley.service".text;
705 resticTimer = host.config.systemd.units."restic-backups-valley.timer".text;
706 protectedInit = protectedHost.config.systemd.services.valley-init.script;
707 protectedKeys = lib.concatStringsSep "\n" protectedKeyLines;
708 protectedShell = protectedHost.config.users.users.git.shell;
709 integratorUnit = integratorHost.config.systemd.units."valley-integrator@.service".text;
710 integratorInit = integratorHost.config.systemd.services.valley-init.script;
711 publishUnit = host.config.systemd.units."valley-publish@.service".text;
712 identityInit = identityHost.config.systemd.services.valley-init.script;
713 passAsFile = [
714 "initScript"
715 "sshdConfig"
716 "resticService"
717 "resticTimer"
718 "protectedInit"
719 "protectedKeys"
720 "integratorUnit"
721 "integratorInit"
722 "publishUnit"
723 "identityInit"
724 ];
725 } (builtins.readFile ./module-eval.sh);
726}