gunk-dev/the-valley

ssh-e2e.nix

source1ef1288a69f6main's published tip history
13.4 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 reader 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 principal that holds no grant anywhere, which is what a
103 # host renders for a key that only reads.
104 {
105 principal = "reader";
106 key = pub "reader";
107 }
108 # A key that names no principal.
109 (pub "stranger")
110 ];
111 grants.request = [
112 "operator"
113 "requester"
114 ];
115 identity.enable = true;
116 integrator.instanceProject = "instance";
117 };
118 # The same host with the hand grant removed, as the end of the
119 # handoff leaves it.
120 specialisation.handed-off.configuration.services.valley.grants.request = lib.mkForce [ ];
121 # The same host with sshd misconfigured to accept the variables the
122 # push boundary reads, which the module's assertions refuse. They
123 # are switched off here so the git user's shell can be shown to
124 # neutralise what sshd lets through anyway.
125 specialisation.misconfigured.configuration = {
126 services.openssh.settings.AcceptEnv = "VALLEY_PRINCIPAL GIT_*";
127 assertions = lib.mkForce [ ];
128 };
129 };
130
131 testScript = ''
132 import shlex
133
134 host.wait_for_unit("multi-user.target")
135 host.wait_for_unit("sshd.service")
136 host.wait_until_succeeds("test -L /srv/git/project.git/hooks/pre-receive")
137
138 host.succeed(
139 "mkdir -p /root/keys",
140 "cp ${keys}/* /root/keys/",
141 "chmod 600 /root/keys/*",
142 "git config --global user.name valley-check",
143 "git config --global user.email valley-check@localhost",
144 "git init --quiet --initial-branch=main /root/work",
145 "git -C /root/work commit --quiet --allow-empty -m one",
146 )
147
148 # Every push carries a commit the server has not seen, so that it is
149 # a write the hook has to decide, and never an up-to-date no-op.
150 def push(who, refspec, repo="project", env="", ssh=""):
151 command = (
152 f"ssh -i /root/keys/{who} -o IdentitiesOnly=yes -o BatchMode=yes"
153 " -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null " + ssh
154 )
155 return (
156 "cd /root/work && git commit --quiet --allow-empty -m push && "
157 f"{env} GIT_SSH_COMMAND={shlex.quote(command)}"
158 f" git push --quiet git@localhost:{repo}.git {refspec} 2>&1"
159 )
160
161 def refused(command, says):
162 out = host.fail(command)
163 assert says in out, f"expected {says!r} in:\n{out}"
164
165 request = "refs/the-valley/integration-requests/main/one"
166
167 with subtest("the principal is the one the key's entry names"):
168 host.succeed(push("contributor", "+HEAD:refs/heads/topic"))
169 refused(push("contributor", f"HEAD:{request}"), "contributor may not write")
170 refused(push("contributor", "HEAD:refs/heads/main"), "a protected ref of project")
171 refused(push("stranger", "HEAD:refs/heads/main", repo="instance"), "<untagged key> may not write")
172 host.succeed(push("integrator", "HEAD:refs/heads/main", repo="instance"))
173
174 with subtest("a key that holds no grant can fetch, is refused the writes that need one, and gets no shell"):
175 reader = (
176 "ssh -i /root/keys/reader -o IdentitiesOnly=yes -o BatchMode=yes"
177 " -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null"
178 )
179 host.succeed(
180 f"GIT_SSH_COMMAND={shlex.quote(reader)} git ls-remote git@localhost:project.git refs/heads/topic | grep -q topic",
181 f"rm -rf /root/reader-clone && GIT_SSH_COMMAND={shlex.quote(reader)} git clone --quiet git@localhost:project.git /root/reader-clone",
182 "git -C /root/reader-clone rev-parse --verify --quiet origin/topic",
183 )
184 refused(push("reader", "HEAD:refs/heads/main"), "reader may not write refs/heads/main")
185 refused(push("reader", "HEAD:refs/tags/v1"), "reader may not write refs/tags/v1")
186 refused(push("reader", f"HEAD:{request}"), "reader holds none")
187 # Topic branches are open to any named key, and git-shell serves
188 # git-upload-archive, so neither is refused here.
189 # git-shell has no interactive mode, and runs no command but git's.
190 refused(f"{reader} git@localhost </dev/null 2>&1", "Interactive git shell is not enabled")
191 refused(f"{reader} git@localhost env 2>&1", "unrecognized command 'env'")
192
193 with subtest("a client cannot supply the principal itself"):
194 spoof = "VALLEY_PRINCIPAL=integrator"
195 send = "-o SendEnv=VALLEY_PRINCIPAL -o SetEnv=VALLEY_PRINCIPAL=integrator"
196 refused(
197 push("stranger", "HEAD:refs/heads/main", repo="instance", env=spoof, ssh=send),
198 "<untagged key> may not write",
199 )
200 refused(push("contributor", f"HEAD:{request}", env=spoof, ssh=send), "contributor may not write")
201 # A key file in the git user's home is not one sshd reads for it.
202 host.succeed(
203 "mkdir -p /srv/git/.ssh",
204 "cp ${hostileKeys} /srv/git/.ssh/authorized_keys",
205 "chown -R git:git /srv/git/.ssh",
206 )
207 refused(push("stranger", "HEAD:refs/heads/main", repo="instance"), "<untagged key> may not write")
208 host.succeed("rm -r /srv/git/.ssh")
209 # And no key reaches a shell to set anything with.
210 host.fail(
211 "ssh -i /root/keys/stranger -o IdentitiesOnly=yes -o BatchMode=yes"
212 " -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null git@localhost env"
213 )
214
215 with subtest("a host that has not converged takes no push, and still serves fetches"):
216 host.succeed(
217 "printf '#!/bin/sh\\nexit 0\\n' > /srv/git/project.git/hooks/pre-receive.hand",
218 "mv -f /srv/git/project.git/hooks/pre-receive /srv/git/project.git/hooks/pre-receive.managed",
219 "mv /srv/git/project.git/hooks/pre-receive.hand /srv/git/project.git/hooks/pre-receive",
220 "chmod +x /srv/git/project.git/hooks/pre-receive",
221 "chown -h git:git /srv/git/project.git/hooks/pre-receive",
222 )
223 host.fail("systemctl restart valley-init.service")
224 refused(push("integrator", "HEAD:refs/heads/main", repo="instance"), "pushes to this host are paused")
225 refused(push("contributor", "+HEAD:refs/heads/topic"), "pushes to this host are paused")
226 host.succeed(
227 "GIT_SSH_COMMAND='ssh -i /root/keys/stranger -o IdentitiesOnly=yes -o BatchMode=yes"
228 " -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null'"
229 " git ls-remote git@localhost:project.git refs/heads/topic | grep -q topic"
230 )
231 host.succeed("rm /srv/git/project.git/hooks/pre-receive", "systemctl restart valley-init.service")
232 host.succeed(push("contributor", "+HEAD:refs/heads/topic"))
233 # The operator's hold outlasts a convergence, and only its
234 # removal lifts it.
235 host.succeed("sudo -u git touch /srv/git/.valley-hold", "systemctl restart valley-init.service")
236 refused(push("contributor", "+HEAD:refs/heads/topic"), "pushes to this host are held by its operator")
237 host.succeed("rm /srv/git/.valley-hold")
238 host.succeed(push("contributor", "+HEAD:refs/heads/topic"))
239
240 with subtest("before the registry compiles, the hand grant is the only one"):
241 host.succeed(push("operator", f"HEAD:{request}"))
242 host.succeed(push("requester", f"+HEAD:{request}"))
243 host.fail("grep -q '^request ' /var/lib/valley-identity/grants")
244
245 with subtest("the compiled grant is added to the hand grant"):
246 host.succeed(
247 "rm -rf /root/instance && git init --quiet --initial-branch=main /root/instance",
248 "mkdir -p /root/instance/identity && cp ${registry} /root/instance/identity/registry.cue",
249 "git -C /root/instance add -A && git -C /root/instance commit --quiet -m registry",
250 )
251 command = (
252 "ssh -i /root/keys/integrator -o IdentitiesOnly=yes -o BatchMode=yes"
253 " -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null"
254 )
255 host.succeed(
256 f"cd /root/instance && GIT_SSH_COMMAND={shlex.quote(command)}"
257 " git push --quiet --force git@localhost:instance.git HEAD:refs/heads/main"
258 )
259 host.succeed("systemctl start valley-identity.service")
260 host.succeed("grep -qx 'request operator' /var/lib/valley-identity/grants")
261 host.succeed(push("operator", f"+HEAD:{request}"))
262 host.succeed(push("requester", f"+HEAD:{request}"))
263
264 with subtest("what sshd lets through, the git user's shell does not"):
265 host.succeed(
266 "/run/booted-system/specialisation/misconfigured/bin/switch-to-configuration test",
267 "systemctl restart valley-init.service",
268 )
269 spoof = "VALLEY_PRINCIPAL=integrator"
270 refused(
271 push("stranger", "+HEAD:refs/heads/main", repo="instance", env=spoof, ssh="-o SendEnv=VALLEY_PRINCIPAL"),
272 "<untagged key> may not write",
273 )
274 # Sent through, these would point git's hooks at nothing, and the
275 # protected main would take the push.
276 hooks_off = "GIT_CONFIG_COUNT=1 GIT_CONFIG_KEY_0=core.hooksPath GIT_CONFIG_VALUE_0=/dev/null"
277 refused(
278 push("contributor", "+HEAD:refs/heads/main", env=hooks_off, ssh="-o SendEnv=GIT_CONFIG_*"),
279 "a protected ref of project",
280 )
281 host.succeed(
282 "/run/booted-system/bin/switch-to-configuration test",
283 "systemctl restart valley-init.service",
284 )
285
286 with subtest("removing the hand grant leaves the compiled one standing"):
287 host.succeed(
288 "/run/booted-system/specialisation/handed-off/bin/switch-to-configuration test",
289 "systemctl restart valley-init.service",
290 )
291 host.succeed("grep -qx 'request operator' /var/lib/valley-identity/grants")
292 host.succeed(push("operator", f"+HEAD:{request}"))
293 refused(push("requester", f"--delete {request}"), "requester holds none")
294 '';
295 };
296}