| 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 | pkgs, |
| 7 | lib, |
| 8 | hosts, |
| 9 | ... |
| 10 | }: |
| 11 | let |
| 12 | inherit (hosts) |
| 13 | host |
| 14 | noBackupHost |
| 15 | noSecretsHost |
| 16 | protectedHost |
| 17 | busHost |
| 18 | mirrorHost |
| 19 | integratorHost |
| 20 | integratorSelfHost |
| 21 | identityHost |
| 22 | ; |
| 23 | |
| 24 | failedAssertions = builtins.filter (a: !a.assertion) ( |
| 25 | host.config.assertions |
| 26 | ++ noBackupHost.config.assertions |
| 27 | ++ busHost.config.assertions |
| 28 | ++ mirrorHost.config.assertions |
| 29 | ++ protectedHost.config.assertions |
| 30 | ++ integratorHost.config.assertions |
| 31 | ++ integratorSelfHost.config.assertions |
| 32 | ++ identityHost.config.assertions |
| 33 | ); |
| 34 | |
| 35 | missingSecretAssertions = builtins.filter (a: !a.assertion) noSecretsHost.config.assertions; |
| 36 | |
| 37 | protectedKeyLines = protectedHost.config.users.users.git.openssh.authorizedKeys.keys; |
| 38 | |
| 39 | resticRenderedWithoutDeclaration = |
| 40 | noBackupHost.config.systemd.services ? "restic-backups-valley" |
| 41 | || noBackupHost.config.systemd.timers ? "restic-backups-valley"; |
| 42 | |
| 43 | # The bus defaults off; a host that never asked for one must |
| 44 | # render zero bus machinery. |
| 45 | busRenderedWithoutEnable = |
| 46 | noBackupHost.config.systemd.services ? valley-bus |
| 47 | || noBackupHost.config.systemd.services ? valley-bus-init; |
| 48 | |
| 49 | # A declaration written before the field renders the host it always |
| 50 | # did: no hook wired, no tag on a key, no sshd change. It still |
| 51 | # carries the sweep that removes a hook it once had — the same |
| 52 | # discipline the mirror and bus hooks keep, and the only way |
| 53 | # protection can be turned off by editing a declaration. |
| 54 | protectionRenderedWithoutDeclaration = |
| 55 | noBackupHost.config.services.openssh.settings ? PermitUserEnvironment |
| 56 | || lib.any (lib.hasInfix "environment=") ( |
| 57 | noBackupHost.config.users.users.git.openssh.authorizedKeys.keys |
| 58 | ) |
| 59 | || lib.hasInfix "valley-protect-" noBackupHost.config.systemd.services.valley-init.script; |
| 60 | |
| 61 | # The integrator's units. What a controller *does* is not checked here |
| 62 | # and no VM is booted: that is integrator-e2e's job (the real binaries |
| 63 | # over real repositories) and, past it, live exercise on a host. These |
| 64 | # assertions cover the wiring — that a controller is rendered for what |
| 65 | # the declaration protects and for nothing else, under an identity that |
| 66 | # is not the git user's, pointed at the key and the signers the consumer |
| 67 | # supplied. |
| 68 | # |
| 69 | # There is a seam between those two halves that neither reaches, and in a |
| 70 | # single day it produced five deployment failures. Every assertion below is |
| 71 | # an eval of the unit's text; integrator-e2e runs the binaries with no unit |
| 72 | # around them at all. What sits between is the unit's sandbox and identity |
| 73 | # meeting the filesystem the binaries actually use at runtime: |
| 74 | # |
| 75 | # - git refusing a repository the service does not own (bd-500adf7) |
| 76 | # - a push authenticating as the operator rather than as the run |
| 77 | # (bd-353c59d) |
| 78 | # - no writable temporary directory, so no worktree could be made |
| 79 | # - the worktree metadata root, created by whichever service reached it |
| 80 | # first and then unchmodable by the other, which failed valley-init and |
| 81 | # took every valley unit down with it |
| 82 | # - the state directory not writable under ProtectSystem=strict unless |
| 83 | # the unit names it, which the previous fix had made the scratch root |
| 84 | # |
| 85 | # Each was found by a host, minutes of a live service after a deploy that |
| 86 | # every check here passed. They compose: the third's fix set up the fourth, |
| 87 | # and the fourth's fix set up the fifth. That is the argument. A seam whose |
| 88 | # failures feed each other is not one to pin an assertion at a time, and |
| 89 | # each assertion here that marks a piece of it — the git ownership |
| 90 | # exception, TMPDIR, the worktrees root, ReadWritePaths — marks a place a |
| 91 | # smoke boot would have spoken first. |
| 92 | # |
| 93 | # A nixosTest that boots one host, starts the units and reconciles one |
| 94 | # change end to end is the next check this module needs. Nothing else |
| 95 | # closes the seam, and five incidents is enough evidence of what it costs |
| 96 | # to leave open. Not built here. |
| 97 | # |
| 98 | # Protection and integration are separable declarations: protectedHost |
| 99 | # declares the same protected projects with the service off, and must |
| 100 | # evaluate to no controller at all. |
| 101 | integratorRenderedWithoutEnable = |
| 102 | protectedHost.config.systemd.services ? "valley-integrator@" |
| 103 | || protectedHost.config.systemd.targets ? valley-integrators |
| 104 | || lib.hasInfix "sharedRepository" protectedHost.config.systemd.services.valley-init.script; |
| 105 | |
| 106 | controllers = integratorHost.config.systemd.targets.valley-integrators.wants; |
| 107 | |
| 108 | integratorUser = integratorHost.config.systemd.services."valley-integrator@".serviceConfig.User; |
| 109 | |
| 110 | # The controller is not the repositories' owner, which is the whole point |
| 111 | # of running it as itself — so git needs an ownership exception, and |
| 112 | # where that exception lives is load-bearing. GIT_CONFIG_* is command |
| 113 | # scope, the only scope besides system and global that safe.directory is |
| 114 | # read from, and the unit's environment is inherited by every git the |
| 115 | # controller drives. It names one repository: a wildcard here would hand |
| 116 | # a compromised controller every repository on the host. |
| 117 | integratorGitEnv = integratorHost.config.systemd.services."valley-integrator@".environment; |
| 118 | |
| 119 | # The count is what git reads; an entry past it is an entry git ignores, |
| 120 | # silently. So it is checked against the entries actually rendered rather |
| 121 | # than against a number written here, which would have to be edited every |
| 122 | # time a grant is added and would go on passing if it were not. |
| 123 | integratorSafeDirectory = |
| 124 | integratorGitEnv.GIT_CONFIG_KEY_0 or null != "safe.directory" |
| 125 | || integratorGitEnv.GIT_CONFIG_VALUE_0 or null != "/srv/git/%i.git" |
| 126 | || integratorGitEnv.GIT_CONFIG_COUNT or null |
| 127 | != toString ( |
| 128 | lib.length (lib.filter (lib.hasPrefix "GIT_CONFIG_KEY_") (lib.attrNames integratorGitEnv)) |
| 129 | ); |
| 130 | |
| 131 | integratorService = integratorHost.config.systemd.services."valley-integrator@".serviceConfig; |
| 132 | |
| 133 | # Scratch goes under the state directory, because the sandbox leaves no |
| 134 | # writable temporary directory and a verdict needs a checkout on disk. Two |
| 135 | # halves, and each was a live failure on its own. TMPDIR naming a path |
| 136 | # outside the state directory is the first; the state directory not being |
| 137 | # named in ReadWritePaths is the second, which failed even though |
| 138 | # ProtectSystem=strict is documented to leave a declared state directory |
| 139 | # writable. Everything the unit may write is enumerated, so what a reader |
| 140 | # of the unit sees is the whole of it. |
| 141 | # The floor is read from the instance repository's integrated tip |
| 142 | # (dcr-f41f718, dcr-b87f6e8), so a controller is told which repository |
| 143 | # carries it and reads it there. Three things have to be true of that, and |
| 144 | # each was a way for it to be silently wrong. |
| 145 | # |
| 146 | # The controller has to be told at all — the failure this replaced was a |
| 147 | # binary looking for a materialized directory nothing populated. |
| 148 | # |
| 149 | # It has to work when the repository carrying the floor is not the one the |
| 150 | # controller serves, which is the ordinary case: on integratorHost every |
| 151 | # controller serves a protected project and reads the floor out of |
| 152 | # "open", which no controller serves. git refuses a repository the caller |
| 153 | # does not own, so that read needs its own safe.directory entry, named and |
| 154 | # not wildcarded — the same failure as the served repository's, one |
| 155 | # repository over. |
| 156 | # |
| 157 | # And it has to keep working when the two coincide, which is what an |
| 158 | # instance's own host looks like: integratorSelfHost's controller for |
| 159 | # "guarded" reads the floor from "guarded". |
| 160 | integratorFloor = |
| 161 | let |
| 162 | command = integratorHost.config.systemd.services."valley-integrator@".serviceConfig.ExecStart; |
| 163 | selfCommand = integratorSelfHost.config.systemd.services."valley-integrator@".serviceConfig.ExecStart; |
| 164 | env = integratorHost.config.systemd.services."valley-integrator@".environment; |
| 165 | in |
| 166 | !(lib.hasInfix "--instance-repo /srv/git/open.git" command) |
| 167 | || !(lib.hasInfix "--instance-repo /srv/git/guarded.git" selfCommand) |
| 168 | || env.GIT_CONFIG_COUNT or null != "2" |
| 169 | || env.GIT_CONFIG_KEY_1 or null != "safe.directory" |
| 170 | || env.GIT_CONFIG_VALUE_1 or null != "/srv/git/open.git"; |
| 171 | |
| 172 | integratorScratch = |
| 173 | integratorGitEnv.TMPDIR or null != "/var/lib/${integratorService.StateDirectory}" |
| 174 | || !(builtins.elem "/var/lib/${integratorService.StateDirectory}" integratorService.ReadWritePaths); |
| 175 | |
| 176 | # The registry compiler. It is off by default and must stay inert: a |
| 177 | # consumer that has not asked for it renders the system it always did. |
| 178 | # integratorHost is the strongest witness available — the integrator on, |
| 179 | # the signers file supplied by hand, and every option of the identity |
| 180 | # block left alone. |
| 181 | identityRenderedWithoutEnable = |
| 182 | integratorHost.config.systemd.services ? valley-identity |
| 183 | || integratorHost.config.systemd.timers ? valley-identity |
| 184 | || integratorHost.config.services.openssh.settings ? PermitUserEnvironment |
| 185 | || lib.any (lib.hasInfix "valley-identity") integratorHost.config.services.openssh.authorizedKeysFiles |
| 186 | || lib.hasInfix "valley-identity" ( |
| 187 | integratorHost.config.systemd.services."valley-integrator@".serviceConfig.ExecStart |
| 188 | ) |
| 189 | || lib.hasInfix "--name " ( |
| 190 | integratorHost.config.systemd.services."valley-integrator@".serviceConfig.ExecStart |
| 191 | ); |
| 192 | |
| 193 | identityService = identityHost.config.systemd.services.valley-identity.serviceConfig; |
| 194 | |
| 195 | identityStateDir = "/var/lib/${identityService.StateDirectory or ""}"; |
| 196 | |
| 197 | # What the compiled artifacts have to be true of, and each was a way for |
| 198 | # the machinery to be quietly wrong. |
| 199 | # |
| 200 | # The compiler writes into its state directory and nowhere else, so that |
| 201 | # directory has to be the one path the unit may write. |
| 202 | # |
| 203 | # sshd walks every directory above an authorized-keys file and refuses a |
| 204 | # group- or world-writable one, so the state directory is 0755 rather than |
| 205 | # the 0700 a service's state usually takes — and the unit runs as the git |
| 206 | # user, because sshd equally refuses a file owned by anyone but root or |
| 207 | # the account it authorizes. |
| 208 | # |
| 209 | # The compiled file is added to what sshd reads rather than replacing it, |
| 210 | # and it carries the %u token: a fixed path in that global list would |
| 211 | # authorize every registry key for every account on the host. |
| 212 | identityCompiler = |
| 213 | identityService.User or null != identityHost.config.services.valley.user |
| 214 | || identityService.StateDirectoryMode or null != "0755" |
| 215 | || identityService.ReadWritePaths or [ ] != [ identityStateDir ] |
| 216 | || !(lib.hasInfix "--repo /srv/git/open.git" identityService.ExecStart) |
| 217 | || !(lib.hasInfix "--known-signers ${identityStateDir}/known-signers" identityService.ExecStart) |
| 218 | || !(lib.hasInfix "--authorized-keys ${identityStateDir}/authorized_keys.git" identityService.ExecStart) |
| 219 | || !(builtins.elem "${identityStateDir}/authorized_keys.%u" |
| 220 | identityHost.config.services.openssh.authorizedKeysFiles |
| 221 | ) |
| 222 | || !(builtins.elem "%h/.ssh/authorized_keys" identityHost.config.services.openssh.authorizedKeysFiles); |
| 223 | |
| 224 | # A controller under a compiled registry reads the compiled signers file |
| 225 | # and signs under the name the registry publishes its key under. The key |
| 226 | # hash binds name to key, so a controller signing under the host's default |
| 227 | # name while the registry publishes it as "integrator" would produce notes |
| 228 | # no verifier can find a key for. |
| 229 | identityIntegrator = |
| 230 | let |
| 231 | command = identityHost.config.systemd.services."valley-integrator@".serviceConfig.ExecStart; |
| 232 | in |
| 233 | !(lib.hasInfix "--known-signers ${identityStateDir}/known-signers" command) |
| 234 | || !(lib.hasInfix "--name integrator" command) |
| 235 | || !(builtins.elem "valley-identity.service" |
| 236 | identityHost.config.systemd.services."valley-integrator@".after |
| 237 | ); |
| 238 | |
| 239 | # The declared keys stay authorized beside the compiled ones. A compiled |
| 240 | # file that replaced them would make a bad compilation — or a first boot |
| 241 | # before the first one — a locked-out git user, which is the one failure |
| 242 | # this machinery must never have. |
| 243 | identityKeepsDeclaredKeys = |
| 244 | identityHost.config.users.users.git.openssh.authorizedKeys.keys == [ ]; |
| 245 | in |
| 246 | { |
| 247 | module-eval = |
| 248 | if failedAssertions != [ ] then |
| 249 | throw "valley module-eval: failed assertions: ${ |
| 250 | lib.concatMapStringsSep "; " (a: a.message) failedAssertions |
| 251 | }" |
| 252 | else if resticRenderedWithoutDeclaration then |
| 253 | throw "valley module-eval: restic machinery rendered for a declaration without a backup block" |
| 254 | else if busRenderedWithoutEnable then |
| 255 | throw "valley module-eval: bus machinery rendered without services.valley.bus.enable" |
| 256 | else if |
| 257 | !(lib.any (a: lib.hasInfix "services.valley.backup." a.message) missingSecretAssertions) |
| 258 | then |
| 259 | throw "valley module-eval: enabling backup without the secret-path options must fail an assertion naming them" |
| 260 | else if protectionRenderedWithoutDeclaration then |
| 261 | throw "valley module-eval: write-protection machinery rendered for a declaration with no protection block" |
| 262 | else if |
| 263 | protectedHost.config.services.openssh.settings.PermitUserEnvironment or null != "VALLEY_PRINCIPAL" |
| 264 | then |
| 265 | throw "valley module-eval: a key naming a principal needs sshd to honour that one variable and no other" |
| 266 | else if integratorRenderedWithoutEnable then |
| 267 | throw "valley module-eval: integrator machinery rendered for a protected declaration that never enabled the service" |
| 268 | else if !(integratorHost.config.systemd.services ? "valley-integrator@") then |
| 269 | throw "valley module-eval: the integrator is enabled and no controller unit was rendered" |
| 270 | else if |
| 271 | controllers != [ |
| 272 | "valley-integrator@guarded.service" |
| 273 | "valley-integrator@released.service" |
| 274 | "valley-integrator@sealed.service" |
| 275 | ] |
| 276 | then |
| 277 | throw "valley module-eval: a controller runs for each protected project and no other, not ${lib.concatStringsSep ", " controllers}" |
| 278 | else if integratorUser == integratorHost.config.services.valley.user then |
| 279 | throw "valley module-eval: the integrator must not run as the git user — it has its own (bd-500adf7)" |
| 280 | else if !(integratorHost.config.users.users ? ${integratorUser}) then |
| 281 | throw "valley module-eval: the integrator runs as ${integratorUser}, which the module never declares" |
| 282 | else if integratorHost.config.users.users.${integratorUser}.group != "git" then |
| 283 | throw "valley module-eval: the integrator reaches the repositories through the git group, and holds no other grant" |
| 284 | else if integratorSafeDirectory then |
| 285 | 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" |
| 286 | else if integratorFloor then |
| 287 | 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" |
| 288 | else if integratorScratch then |
| 289 | 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" |
| 290 | else if identityRenderedWithoutEnable then |
| 291 | throw "valley module-eval: registry-compilation machinery rendered for a host that never enabled it — nothing may change until a consumer opts in" |
| 292 | else if identityCompiler then |
| 293 | 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, 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" |
| 294 | else if identityIntegrator then |
| 295 | 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" |
| 296 | else if identityKeepsDeclaredKeys then |
| 297 | 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" |
| 298 | else |
| 299 | pkgs.runCommand "valley-module-eval" { |
| 300 | initScript = host.config.systemd.services.valley-init.script; |
| 301 | sshdConfig = host.config.services.openssh.extraConfig; |
| 302 | resticService = host.config.systemd.units."restic-backups-valley.service".text; |
| 303 | resticTimer = host.config.systemd.units."restic-backups-valley.timer".text; |
| 304 | protectedInit = protectedHost.config.systemd.services.valley-init.script; |
| 305 | protectedKeys = lib.concatStringsSep "\n" protectedKeyLines; |
| 306 | integratorUnit = integratorHost.config.systemd.units."valley-integrator@.service".text; |
| 307 | integratorInit = integratorHost.config.systemd.services.valley-init.script; |
| 308 | passAsFile = [ |
| 309 | "initScript" |
| 310 | "sshdConfig" |
| 311 | "resticService" |
| 312 | "resticTimer" |
| 313 | "protectedInit" |
| 314 | "protectedKeys" |
| 315 | "integratorUnit" |
| 316 | "integratorInit" |
| 317 | ]; |
| 318 | } (builtins.readFile ./module-eval.sh); |
| 319 | } |