gunk-dev/the-valley

host-module.nix

sourceb70056c25abamain's published tip history
9.3 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 it
66 # produced four bugs in a single day. Every assertion below is an eval of
67 # the unit's text; integrator-e2e runs the binaries with no unit around
68 # 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 temporary directory the sandbox does not leave writable
73 # - the worktree metadata root, created by whichever service reached it
74 # first and then unchmodable by the other
75 # - the last of those failing valley-init, which every valley unit
76 # requires, so one repository's permissions took the host's git
77 # service down with them
78 #
79 # Each was found by a host, minutes of a live service after a deploy that
80 # every check here passed. The last two are the same seam feeding itself:
81 # the debris of the second bug's failed runs is what the third tripped
82 # over. That is the case for covering it rather than pinning it one
83 # assertion at a time — the failures compose, and the checks do not.
84 #
85 # A nixosTest that boots one host and reconciles one change end to end is
86 # what covers that seam, and each assertion pinning a piece of it — the
87 # git ownership exception, TMPDIR, the worktrees root — is a place a
88 # smoke boot would have spoken first. Not built here.
89 #
90 # Protection and integration are separable declarations: protectedHost
91 # declares the same protected projects with the service off, and must
92 # evaluate to no controller at all.
93 integratorRenderedWithoutEnable =
94 protectedHost.config.systemd.services ? "valley-integrator@"
95 || protectedHost.config.systemd.targets ? valley-integrators
96 || lib.hasInfix "sharedRepository" protectedHost.config.systemd.services.valley-init.script;
97
98 controllers = integratorHost.config.systemd.targets.valley-integrators.wants;
99
100 integratorUser = integratorHost.config.systemd.services."valley-integrator@".serviceConfig.User;
101
102 # The controller is not the repositories' owner, which is the whole point
103 # of running it as itself — so git needs an ownership exception, and
104 # where that exception lives is load-bearing. GIT_CONFIG_* is command
105 # scope, the only scope besides system and global that safe.directory is
106 # read from, and the unit's environment is inherited by every git the
107 # controller drives. It names one repository: a wildcard here would hand
108 # a compromised controller every repository on the host.
109 integratorGitEnv = integratorHost.config.systemd.services."valley-integrator@".environment;
110
111 integratorSafeDirectory =
112 integratorGitEnv.GIT_CONFIG_COUNT or null != "1"
113 || integratorGitEnv.GIT_CONFIG_KEY_0 or null != "safe.directory"
114 || integratorGitEnv.GIT_CONFIG_VALUE_0 or null != "/srv/git/%i.git";
115
116 # Scratch goes under the state directory, because the sandbox leaves no
117 # writable temporary directory and a verdict needs a checkout on disk.
118 # Pinned to the state directory the unit actually declares: TMPDIR naming
119 # a path outside it is the failure this replaced, arriving silently.
120 integratorScratch =
121 integratorGitEnv.TMPDIR or null
122 != "%S/${integratorHost.config.systemd.services."valley-integrator@".serviceConfig.StateDirectory}";
123in
124{
125 module-eval =
126 if failedAssertions != [ ] then
127 throw "valley module-eval: failed assertions: ${
128 lib.concatMapStringsSep "; " (a: a.message) failedAssertions
129 }"
130 else if resticRenderedWithoutDeclaration then
131 throw "valley module-eval: restic machinery rendered for a declaration without a backup block"
132 else if busRenderedWithoutEnable then
133 throw "valley module-eval: bus machinery rendered without services.valley.bus.enable"
134 else if
135 !(lib.any (a: lib.hasInfix "services.valley.backup." a.message) missingSecretAssertions)
136 then
137 throw "valley module-eval: enabling backup without the secret-path options must fail an assertion naming them"
138 else if protectionRenderedWithoutDeclaration then
139 throw "valley module-eval: write-protection machinery rendered for a declaration with no protection block"
140 else if
141 protectedHost.config.services.openssh.settings.PermitUserEnvironment or null != "VALLEY_PRINCIPAL"
142 then
143 throw "valley module-eval: a key naming a principal needs sshd to honour that one variable and no other"
144 else if integratorRenderedWithoutEnable then
145 throw "valley module-eval: integrator machinery rendered for a protected declaration that never enabled the service"
146 else if !(integratorHost.config.systemd.services ? "valley-integrator@") then
147 throw "valley module-eval: the integrator is enabled and no controller unit was rendered"
148 else if
149 controllers != [
150 "valley-integrator@guarded.service"
151 "valley-integrator@released.service"
152 ]
153 then
154 throw "valley module-eval: a controller runs for each protected project and no other, not ${lib.concatStringsSep ", " controllers}"
155 else if integratorUser == integratorHost.config.services.valley.user then
156 throw "valley module-eval: the integrator must not run as the git user — it has its own (bd-500adf7)"
157 else if !(integratorHost.config.users.users ? ${integratorUser}) then
158 throw "valley module-eval: the integrator runs as ${integratorUser}, which the module never declares"
159 else if integratorHost.config.users.users.${integratorUser}.group != "git" then
160 throw "valley module-eval: the integrator reaches the repositories through the git group, and holds no other grant"
161 else if integratorSafeDirectory then
162 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"
163 else if integratorScratch then
164 throw "valley module-eval: a controller's TMPDIR must be its state directory — the sandbox leaves no writable temporary directory, and a verdict needs a worktree on disk"
165 else
166 pkgs.runCommand "valley-module-eval" {
167 initScript = host.config.systemd.services.valley-init.script;
168 sshdConfig = host.config.services.openssh.extraConfig;
169 resticService = host.config.systemd.units."restic-backups-valley.service".text;
170 resticTimer = host.config.systemd.units."restic-backups-valley.timer".text;
171 protectedInit = protectedHost.config.systemd.services.valley-init.script;
172 protectedKeys = lib.concatStringsSep "\n" protectedKeyLines;
173 integratorUnit = integratorHost.config.systemd.units."valley-integrator@.service".text;
174 integratorInit = integratorHost.config.systemd.services.valley-init.script;
175 passAsFile = [
176 "initScript"
177 "sshdConfig"
178 "resticService"
179 "resticTimer"
180 "protectedInit"
181 "protectedKeys"
182 "integratorUnit"
183 "integratorInit"
184 ];
185 } (builtins.readFile ./module-eval.sh);
186}