gunk-dev/the-valley

ssh-e2e.nix

source57064e55f18fmain's published tip history
11.9 KiB
1# The push boundary over a real sshd: one NixOS machine running the module,
2# pushed to over ssh by keys that each act as a principal, or as none. The
3# other hook checks set VALLEY_PRINCIPAL in a push's environment directly,
4# which is what sshd does from a key's authorized_keys entry; this check is
5# where that substitution is itself checked, along with every way a client
6# could try to supply the principal itself.
7#
8# It also walks the request grant's handoff, the one sequence a host goes
9# through once: a grant held by hand, the same grant compiled from the
10# registry beside it, and the hand grant removed only once the compiled one
11# works.
12{
13 pkgs,
14 lib,
15 self,
16 ...
17}:
18let
19 # Throwaway keys, made once at build time. They hold no authority
20 # anywhere: the machine that trusts them exists for one test run.
21 keys = pkgs.runCommand "valley-ssh-e2e-keys" { nativeBuildInputs = [ pkgs.openssh ]; } ''
22 mkdir $out
23 for who in integrator contributor requester operator stranger; do
24 ssh-keygen -q -t ed25519 -N "" -C "$who" -f "$out/$who"
25 done
26 '';
27 pub = who: lib.fileContents "${keys}/${who}.pub";
28
29 declaration = pkgs.writeText "valley-ssh-e2e.cue" ''
30 package valley
31
32 projects: {
33 // The instance repository: the registry is read from its main, so
34 // it is protected, and only the integrator principal pushes it.
35 "instance": protection: writers: ["integrator"]
36 "project": protection: {}
37 }
38 '';
39
40 # An authorized_keys file planted in the git user's home, tagging the
41 # untagged key as the integrator. sshd must not read it for that user.
42 hostileKeys = pkgs.writeText "valley-ssh-e2e-hostile-keys" ''
43 environment="VALLEY_PRINCIPAL=integrator" ${pub "stranger"}
44 '';
45
46 # The registry the operator lands: the operator governs, pushes, and
47 # holds request. The requester holds request only by hand.
48 registry = pkgs.writeText "registry.cue" ''
49 package identity
50
51 boundaries: {
52 "push": kind: "git-push"
53 "request": kind: "request"
54 "registry": kind: "registry"
55 }
56 genesis: "operator"
57 principals: "operator": {
58 kind: "human"
59 keys: [{
60 class: "ssh-ed25519"
61 bound: "hardware"
62 public: "${lib.concatStringsSep " " (lib.take 2 (lib.splitString " " (pub "operator")))}"
63 }]
64 grants: {
65 push: boundary: "push"
66 request: boundary: "request"
67 govern: boundary: "registry"
68 }
69 }
70 '';
71in
72{
73 ssh-e2e = pkgs.testers.runNixOSTest {
74 name = "valley-ssh-e2e";
75 nodes.host =
76 { lib, ... }:
77 {
78 imports = [ self.nixosModules.default ];
79 environment.systemPackages = [ pkgs.git ];
80 services.valley = {
81 enable = true;
82 config = declaration;
83 authorizedKeys = [
84 {
85 principal = "integrator";
86 key = pub "integrator";
87 }
88 {
89 principal = "contributor";
90 key = pub "contributor";
91 }
92 {
93 principal = "requester";
94 key = pub "requester";
95 }
96 # Declared under the tag the registry gives it, so the operator
97 # can push before the first compilation.
98 {
99 principal = "operator";
100 key = pub "operator";
101 }
102 # A key that names no principal.
103 (pub "stranger")
104 ];
105 grants.request = [
106 "operator"
107 "requester"
108 ];
109 identity.enable = true;
110 integrator.instanceProject = "instance";
111 };
112 # The same host with the hand grant removed, as the end of the
113 # handoff leaves it.
114 specialisation.handed-off.configuration.services.valley.grants.request = lib.mkForce [ ];
115 # The same host with sshd misconfigured to accept the variables the
116 # push boundary reads, which the module's assertions refuse. They
117 # are switched off here so the git user's shell can be shown to
118 # neutralise what sshd lets through anyway.
119 specialisation.misconfigured.configuration = {
120 services.openssh.settings.AcceptEnv = "VALLEY_PRINCIPAL GIT_*";
121 assertions = lib.mkForce [ ];
122 };
123 };
124
125 testScript = ''
126 import shlex
127
128 host.wait_for_unit("multi-user.target")
129 host.wait_for_unit("sshd.service")
130 host.wait_until_succeeds("test -L /srv/git/project.git/hooks/pre-receive")
131
132 host.succeed(
133 "mkdir -p /root/keys",
134 "cp ${keys}/* /root/keys/",
135 "chmod 600 /root/keys/*",
136 "git config --global user.name valley-check",
137 "git config --global user.email valley-check@localhost",
138 "git init --quiet --initial-branch=main /root/work",
139 "git -C /root/work commit --quiet --allow-empty -m one",
140 )
141
142 # Every push carries a commit the server has not seen, so that it is
143 # a write the hook has to decide, and never an up-to-date no-op.
144 def push(who, refspec, repo="project", env="", ssh=""):
145 command = (
146 f"ssh -i /root/keys/{who} -o IdentitiesOnly=yes -o BatchMode=yes"
147 " -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null " + ssh
148 )
149 return (
150 "cd /root/work && git commit --quiet --allow-empty -m push && "
151 f"{env} GIT_SSH_COMMAND={shlex.quote(command)}"
152 f" git push --quiet git@localhost:{repo}.git {refspec} 2>&1"
153 )
154
155 def refused(command, says):
156 out = host.fail(command)
157 assert says in out, f"expected {says!r} in:\n{out}"
158
159 request = "refs/the-valley/integration-requests/main/one"
160
161 with subtest("the principal is the one the key's entry names"):
162 host.succeed(push("contributor", "+HEAD:refs/heads/topic"))
163 refused(push("contributor", f"HEAD:{request}"), "contributor may not write")
164 refused(push("contributor", "HEAD:refs/heads/main"), "a protected ref of project")
165 refused(push("stranger", "HEAD:refs/heads/main", repo="instance"), "<untagged key> may not write")
166 host.succeed(push("integrator", "HEAD:refs/heads/main", repo="instance"))
167
168 with subtest("a client cannot supply the principal itself"):
169 spoof = "VALLEY_PRINCIPAL=integrator"
170 send = "-o SendEnv=VALLEY_PRINCIPAL -o SetEnv=VALLEY_PRINCIPAL=integrator"
171 refused(
172 push("stranger", "HEAD:refs/heads/main", repo="instance", env=spoof, ssh=send),
173 "<untagged key> may not write",
174 )
175 refused(push("contributor", f"HEAD:{request}", env=spoof, ssh=send), "contributor may not write")
176 # A key file in the git user's home is not one sshd reads for it.
177 host.succeed(
178 "mkdir -p /srv/git/.ssh",
179 "cp ${hostileKeys} /srv/git/.ssh/authorized_keys",
180 "chown -R git:git /srv/git/.ssh",
181 )
182 refused(push("stranger", "HEAD:refs/heads/main", repo="instance"), "<untagged key> may not write")
183 host.succeed("rm -r /srv/git/.ssh")
184 # And no key reaches a shell to set anything with.
185 host.fail(
186 "ssh -i /root/keys/stranger -o IdentitiesOnly=yes -o BatchMode=yes"
187 " -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null git@localhost env"
188 )
189
190 with subtest("a host that has not converged takes no push, and still serves fetches"):
191 host.succeed(
192 "printf '#!/bin/sh\\nexit 0\\n' > /srv/git/project.git/hooks/pre-receive.hand",
193 "mv -f /srv/git/project.git/hooks/pre-receive /srv/git/project.git/hooks/pre-receive.managed",
194 "mv /srv/git/project.git/hooks/pre-receive.hand /srv/git/project.git/hooks/pre-receive",
195 "chmod +x /srv/git/project.git/hooks/pre-receive",
196 "chown -h git:git /srv/git/project.git/hooks/pre-receive",
197 )
198 host.fail("systemctl restart valley-init.service")
199 refused(push("integrator", "HEAD:refs/heads/main", repo="instance"), "pushes to this host are paused")
200 refused(push("contributor", "+HEAD:refs/heads/topic"), "pushes to this host are paused")
201 host.succeed(
202 "GIT_SSH_COMMAND='ssh -i /root/keys/stranger -o IdentitiesOnly=yes -o BatchMode=yes"
203 " -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null'"
204 " git ls-remote git@localhost:project.git refs/heads/topic | grep -q topic"
205 )
206 host.succeed("rm /srv/git/project.git/hooks/pre-receive", "systemctl restart valley-init.service")
207 host.succeed(push("contributor", "+HEAD:refs/heads/topic"))
208 # The operator's hold outlasts a convergence, and only its
209 # removal lifts it.
210 host.succeed("sudo -u git touch /srv/git/.valley-hold", "systemctl restart valley-init.service")
211 refused(push("contributor", "+HEAD:refs/heads/topic"), "pushes to this host are held by its operator")
212 host.succeed("rm /srv/git/.valley-hold")
213 host.succeed(push("contributor", "+HEAD:refs/heads/topic"))
214
215 with subtest("before the registry compiles, the hand grant is the only one"):
216 host.succeed(push("operator", f"HEAD:{request}"))
217 host.succeed(push("requester", f"+HEAD:{request}"))
218 host.fail("grep -q '^request ' /var/lib/valley-identity/grants")
219
220 with subtest("the compiled grant is added to the hand grant"):
221 host.succeed(
222 "rm -rf /root/instance && git init --quiet --initial-branch=main /root/instance",
223 "mkdir -p /root/instance/identity && cp ${registry} /root/instance/identity/registry.cue",
224 "git -C /root/instance add -A && git -C /root/instance commit --quiet -m registry",
225 )
226 command = (
227 "ssh -i /root/keys/integrator -o IdentitiesOnly=yes -o BatchMode=yes"
228 " -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null"
229 )
230 host.succeed(
231 f"cd /root/instance && GIT_SSH_COMMAND={shlex.quote(command)}"
232 " git push --quiet --force git@localhost:instance.git HEAD:refs/heads/main"
233 )
234 host.succeed("systemctl start valley-identity.service")
235 host.succeed("grep -qx 'request operator' /var/lib/valley-identity/grants")
236 host.succeed(push("operator", f"+HEAD:{request}"))
237 host.succeed(push("requester", f"+HEAD:{request}"))
238
239 with subtest("what sshd lets through, the git user's shell does not"):
240 host.succeed(
241 "/run/booted-system/specialisation/misconfigured/bin/switch-to-configuration test",
242 "systemctl restart valley-init.service",
243 )
244 spoof = "VALLEY_PRINCIPAL=integrator"
245 refused(
246 push("stranger", "+HEAD:refs/heads/main", repo="instance", env=spoof, ssh="-o SendEnv=VALLEY_PRINCIPAL"),
247 "<untagged key> may not write",
248 )
249 # Sent through, these would point git's hooks at nothing, and the
250 # protected main would take the push.
251 hooks_off = "GIT_CONFIG_COUNT=1 GIT_CONFIG_KEY_0=core.hooksPath GIT_CONFIG_VALUE_0=/dev/null"
252 refused(
253 push("contributor", "+HEAD:refs/heads/main", env=hooks_off, ssh="-o SendEnv=GIT_CONFIG_*"),
254 "a protected ref of project",
255 )
256 host.succeed(
257 "/run/booted-system/bin/switch-to-configuration test",
258 "systemctl restart valley-init.service",
259 )
260
261 with subtest("removing the hand grant leaves the compiled one standing"):
262 host.succeed(
263 "/run/booted-system/specialisation/handed-off/bin/switch-to-configuration test",
264 "systemctl restart valley-init.service",
265 )
266 host.succeed("grep -qx 'request operator' /var/lib/valley-identity/grants")
267 host.succeed(push("operator", f"+HEAD:{request}"))
268 refused(push("requester", f"--delete {request}"), "requester holds none")
269 '';
270 };
271}