gunk-dev/the-valley

host-module.nix

sourced04b6e8fd721main's published tip history
7.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 # Protection and integration are separable declarations: protectedHost
66 # declares the same protected projects with the service off, and must
67 # evaluate to no controller at all.
68 integratorRenderedWithoutEnable =
69 protectedHost.config.systemd.services ? "valley-integrator@"
70 || protectedHost.config.systemd.targets ? valley-integrators
71 || lib.hasInfix "sharedRepository" protectedHost.config.systemd.services.valley-init.script;
72
73 controllers = integratorHost.config.systemd.targets.valley-integrators.wants;
74
75 integratorUser = integratorHost.config.systemd.services."valley-integrator@".serviceConfig.User;
76
77 # The controller is not the repositories' owner, which is the whole point
78 # of running it as itself — so git needs an ownership exception, and
79 # where that exception lives is load-bearing. GIT_CONFIG_* is command
80 # scope, the only scope besides system and global that safe.directory is
81 # read from, and the unit's environment is inherited by every git the
82 # controller drives. It names one repository: a wildcard here would hand
83 # a compromised controller every repository on the host.
84 integratorGitEnv = integratorHost.config.systemd.services."valley-integrator@".environment;
85
86 integratorSafeDirectory =
87 integratorGitEnv.GIT_CONFIG_COUNT or null != "1"
88 || integratorGitEnv.GIT_CONFIG_KEY_0 or null != "safe.directory"
89 || integratorGitEnv.GIT_CONFIG_VALUE_0 or null != "/srv/git/%i.git";
90in
91{
92 module-eval =
93 if failedAssertions != [ ] then
94 throw "valley module-eval: failed assertions: ${
95 lib.concatMapStringsSep "; " (a: a.message) failedAssertions
96 }"
97 else if resticRenderedWithoutDeclaration then
98 throw "valley module-eval: restic machinery rendered for a declaration without a backup block"
99 else if busRenderedWithoutEnable then
100 throw "valley module-eval: bus machinery rendered without services.valley.bus.enable"
101 else if
102 !(lib.any (a: lib.hasInfix "services.valley.backup." a.message) missingSecretAssertions)
103 then
104 throw "valley module-eval: enabling backup without the secret-path options must fail an assertion naming them"
105 else if protectionRenderedWithoutDeclaration then
106 throw "valley module-eval: write-protection machinery rendered for a declaration with no protection block"
107 else if
108 protectedHost.config.services.openssh.settings.PermitUserEnvironment or null != "VALLEY_PRINCIPAL"
109 then
110 throw "valley module-eval: a key naming a principal needs sshd to honour that one variable and no other"
111 else if integratorRenderedWithoutEnable then
112 throw "valley module-eval: integrator machinery rendered for a protected declaration that never enabled the service"
113 else if !(integratorHost.config.systemd.services ? "valley-integrator@") then
114 throw "valley module-eval: the integrator is enabled and no controller unit was rendered"
115 else if
116 controllers != [
117 "valley-integrator@guarded.service"
118 "valley-integrator@released.service"
119 ]
120 then
121 throw "valley module-eval: a controller runs for each protected project and no other, not ${lib.concatStringsSep ", " controllers}"
122 else if integratorUser == integratorHost.config.services.valley.user then
123 throw "valley module-eval: the integrator must not run as the git user — it has its own (bd-500adf7)"
124 else if !(integratorHost.config.users.users ? ${integratorUser}) then
125 throw "valley module-eval: the integrator runs as ${integratorUser}, which the module never declares"
126 else if integratorHost.config.users.users.${integratorUser}.group != "git" then
127 throw "valley module-eval: the integrator reaches the repositories through the git group, and holds no other grant"
128 else if integratorSafeDirectory then
129 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"
130 else
131 pkgs.runCommand "valley-module-eval" {
132 initScript = host.config.systemd.services.valley-init.script;
133 sshdConfig = host.config.services.openssh.extraConfig;
134 resticService = host.config.systemd.units."restic-backups-valley.service".text;
135 resticTimer = host.config.systemd.units."restic-backups-valley.timer".text;
136 protectedInit = protectedHost.config.systemd.services.valley-init.script;
137 protectedKeys = lib.concatStringsSep "\n" protectedKeyLines;
138 integratorUnit = integratorHost.config.systemd.units."valley-integrator@.service".text;
139 integratorInit = integratorHost.config.systemd.services.valley-init.script;
140 passAsFile = [
141 "initScript"
142 "sshdConfig"
143 "resticService"
144 "resticTimer"
145 "protectedInit"
146 "protectedKeys"
147 "integratorUnit"
148 "integratorInit"
149 ];
150 } (builtins.readFile ./module-eval.sh);
151}