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