gunk-dev/the-valley

host-module.nix

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