gunk-dev/the-valley

host-module.nix

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