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