gunk-dev/the-valley

host-module.nix

source88696b8bfad2main's published tip history
10.1 KiB
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}:
11let
12 inherit (hosts)
13 host
14 noBackupHost
15 noSecretsHost
16 protectedHost
17 busHost
18 mirrorHost
19 integratorHost
20 ;
21
22 failedAssertions = builtins.filter (a: !a.assertion) (
23 host.config.assertions
24 ++ noBackupHost.config.assertions
25 ++ busHost.config.assertions
26 ++ mirrorHost.config.assertions
27 ++ protectedHost.config.assertions
28 ++ integratorHost.config.assertions
29 );
30
31 missingSecretAssertions = builtins.filter (a: !a.assertion) noSecretsHost.config.assertions;
32
33 protectedKeyLines = protectedHost.config.users.users.git.openssh.authorizedKeys.keys;
34
35 resticRenderedWithoutDeclaration =
36 noBackupHost.config.systemd.services ? "restic-backups-valley"
37 || noBackupHost.config.systemd.timers ? "restic-backups-valley";
38
39 # The bus defaults off; a host that never asked for one must
40 # render zero bus machinery.
41 busRenderedWithoutEnable =
42 noBackupHost.config.systemd.services ? valley-bus
43 || noBackupHost.config.systemd.services ? valley-bus-init;
44
45 # A declaration written before the field renders the host it always
46 # did: no hook wired, no tag on a key, no sshd change. It still
47 # carries the sweep that removes a hook it once had — the same
48 # discipline the mirror and bus hooks keep, and the only way
49 # protection can be turned off by editing a declaration.
50 protectionRenderedWithoutDeclaration =
51 noBackupHost.config.services.openssh.settings ? PermitUserEnvironment
52 || lib.any (lib.hasInfix "environment=") (
53 noBackupHost.config.users.users.git.openssh.authorizedKeys.keys
54 )
55 || lib.hasInfix "valley-protect-" noBackupHost.config.systemd.services.valley-init.script;
56
57 # The integrator's units. What a controller *does* is not checked here
58 # and no VM is booted: that is integrator-e2e's job (the real binaries
59 # over real repositories) and, past it, live exercise on a host. These
60 # assertions cover the wiring — that a controller is rendered for what
61 # the declaration protects and for nothing else, under an identity that
62 # is not the git user's, pointed at the key and the signers the consumer
63 # supplied.
64 #
65 # There is a seam between those two halves that neither reaches, and in a
66 # single day it produced five deployment failures. Every assertion below is
67 # an eval of the unit's text; integrator-e2e runs the binaries with no unit
68 # around them at all. What sits between is the unit's sandbox and identity
69 # meeting the filesystem the binaries actually use at runtime:
70 #
71 # - git refusing a repository the service does not own (bd-500adf7)
72 # - a push authenticating as the operator rather than as the run
73 # (bd-353c59d)
74 # - no writable temporary directory, so no worktree could be made
75 # - the worktree metadata root, created by whichever service reached it
76 # first and then unchmodable by the other, which failed valley-init and
77 # took every valley unit down with it
78 # - the state directory not writable under ProtectSystem=strict unless
79 # the unit names it, which the previous fix had made the scratch root
80 #
81 # Each was found by a host, minutes of a live service after a deploy that
82 # every check here passed. They compose: the third's fix set up the fourth,
83 # and the fourth's fix set up the fifth. That is the argument. A seam whose
84 # failures feed each other is not one to pin an assertion at a time, and
85 # each assertion here that marks a piece of it — the git ownership
86 # exception, TMPDIR, the worktrees root, ReadWritePaths — marks a place a
87 # smoke boot would have spoken first.
88 #
89 # A nixosTest that boots one host, starts the units and reconciles one
90 # change end to end is the next check this module needs. Nothing else
91 # closes the seam, and five incidents is enough evidence of what it costs
92 # to leave open. Not built here.
93 #
94 # Protection and integration are separable declarations: protectedHost
95 # declares the same protected projects with the service off, and must
96 # evaluate to no controller at all.
97 integratorRenderedWithoutEnable =
98 protectedHost.config.systemd.services ? "valley-integrator@"
99 || protectedHost.config.systemd.targets ? valley-integrators
100 || lib.hasInfix "sharedRepository" protectedHost.config.systemd.services.valley-init.script;
101
102 controllers = integratorHost.config.systemd.targets.valley-integrators.wants;
103
104 integratorUser = integratorHost.config.systemd.services."valley-integrator@".serviceConfig.User;
105
106 # The controller is not the repositories' owner, which is the whole point
107 # of running it as itself — so git needs an ownership exception, and
108 # where that exception lives is load-bearing. GIT_CONFIG_* is command
109 # scope, the only scope besides system and global that safe.directory is
110 # read from, and the unit's environment is inherited by every git the
111 # controller drives. It names one repository: a wildcard here would hand
112 # a compromised controller every repository on the host.
113 integratorGitEnv = integratorHost.config.systemd.services."valley-integrator@".environment;
114
115 integratorSafeDirectory =
116 integratorGitEnv.GIT_CONFIG_COUNT or null != "1"
117 || integratorGitEnv.GIT_CONFIG_KEY_0 or null != "safe.directory"
118 || integratorGitEnv.GIT_CONFIG_VALUE_0 or null != "/srv/git/%i.git";
119
120 integratorService = integratorHost.config.systemd.services."valley-integrator@".serviceConfig;
121
122 # Scratch goes under the state directory, because the sandbox leaves no
123 # writable temporary directory and a verdict needs a checkout on disk. Two
124 # halves, and each was a live failure on its own. TMPDIR naming a path
125 # outside the state directory is the first; the state directory not being
126 # named in ReadWritePaths is the second, which failed even though
127 # ProtectSystem=strict is documented to leave a declared state directory
128 # writable. Everything the unit may write is enumerated, so what a reader
129 # of the unit sees is the whole of it.
130 integratorScratch =
131 integratorGitEnv.TMPDIR or null != "/var/lib/${integratorService.StateDirectory}"
132 || !(builtins.elem "/var/lib/${integratorService.StateDirectory}" integratorService.ReadWritePaths);
133in
134{
135 module-eval =
136 if failedAssertions != [ ] then
137 throw "valley module-eval: failed assertions: ${
138 lib.concatMapStringsSep "; " (a: a.message) failedAssertions
139 }"
140 else if resticRenderedWithoutDeclaration then
141 throw "valley module-eval: restic machinery rendered for a declaration without a backup block"
142 else if busRenderedWithoutEnable then
143 throw "valley module-eval: bus machinery rendered without services.valley.bus.enable"
144 else if
145 !(lib.any (a: lib.hasInfix "services.valley.backup." a.message) missingSecretAssertions)
146 then
147 throw "valley module-eval: enabling backup without the secret-path options must fail an assertion naming them"
148 else if protectionRenderedWithoutDeclaration then
149 throw "valley module-eval: write-protection machinery rendered for a declaration with no protection block"
150 else if
151 protectedHost.config.services.openssh.settings.PermitUserEnvironment or null != "VALLEY_PRINCIPAL"
152 then
153 throw "valley module-eval: a key naming a principal needs sshd to honour that one variable and no other"
154 else if integratorRenderedWithoutEnable then
155 throw "valley module-eval: integrator machinery rendered for a protected declaration that never enabled the service"
156 else if !(integratorHost.config.systemd.services ? "valley-integrator@") then
157 throw "valley module-eval: the integrator is enabled and no controller unit was rendered"
158 else if
159 controllers != [
160 "valley-integrator@guarded.service"
161 "valley-integrator@released.service"
162 ]
163 then
164 throw "valley module-eval: a controller runs for each protected project and no other, not ${lib.concatStringsSep ", " controllers}"
165 else if integratorUser == integratorHost.config.services.valley.user then
166 throw "valley module-eval: the integrator must not run as the git user — it has its own (bd-500adf7)"
167 else if !(integratorHost.config.users.users ? ${integratorUser}) then
168 throw "valley module-eval: the integrator runs as ${integratorUser}, which the module never declares"
169 else if integratorHost.config.users.users.${integratorUser}.group != "git" then
170 throw "valley module-eval: the integrator reaches the repositories through the git group, and holds no other grant"
171 else if integratorSafeDirectory then
172 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"
173 else if integratorScratch then
174 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"
175 else
176 pkgs.runCommand "valley-module-eval" {
177 initScript = host.config.systemd.services.valley-init.script;
178 sshdConfig = host.config.services.openssh.extraConfig;
179 resticService = host.config.systemd.units."restic-backups-valley.service".text;
180 resticTimer = host.config.systemd.units."restic-backups-valley.timer".text;
181 protectedInit = protectedHost.config.systemd.services.valley-init.script;
182 protectedKeys = lib.concatStringsSep "\n" protectedKeyLines;
183 integratorUnit = integratorHost.config.systemd.units."valley-integrator@.service".text;
184 integratorInit = integratorHost.config.systemd.services.valley-init.script;
185 passAsFile = [
186 "initScript"
187 "sshdConfig"
188 "resticService"
189 "resticTimer"
190 "protectedInit"
191 "protectedKeys"
192 "integratorUnit"
193 "integratorInit"
194 ];
195 } (builtins.readFile ./module-eval.sh);
196}