| 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. The registry's landing on the instance's main is what compiles |
| 12 | # it: with the compiler's timers stopped and no compilation under way, its |
| 13 | # path unit starts one, and valley-init does not run again, so no push is |
| 14 | # paused. While valley-init has failed before touching its converged |
| 15 | # record, the start a move of main causes fails and compiles nothing, and |
| 16 | # the repair of valley-init alone compiles what landed meanwhile. |
| 17 | { |
| 18 | pkgs, |
| 19 | lib, |
| 20 | self, |
| 21 | ... |
| 22 | }: |
| 23 | let |
| 24 | # Throwaway keys, made once at build time. They hold no authority |
| 25 | # anywhere: the machine that trusts them exists for one test run. |
| 26 | keys = pkgs.runCommand "valley-ssh-e2e-keys" { nativeBuildInputs = [ pkgs.openssh ]; } '' |
| 27 | mkdir $out |
| 28 | for who in integrator contributor requester operator reader stranger; do |
| 29 | ssh-keygen -q -t ed25519 -N "" -C "$who" -f "$out/$who" |
| 30 | done |
| 31 | ''; |
| 32 | pub = who: lib.fileContents "${keys}/${who}.pub"; |
| 33 | |
| 34 | declaration = pkgs.writeText "valley-ssh-e2e.cue" '' |
| 35 | package valley |
| 36 | |
| 37 | projects: { |
| 38 | // The instance repository: the registry is read from its main, so |
| 39 | // it is protected, and only the integrator principal pushes it. |
| 40 | "instance": protection: writers: ["integrator"] |
| 41 | "project": protection: {} |
| 42 | } |
| 43 | ''; |
| 44 | |
| 45 | # An authorized_keys file planted in the git user's home, tagging the |
| 46 | # untagged key as the integrator. sshd must not read it for that user. |
| 47 | hostileKeys = pkgs.writeText "valley-ssh-e2e-hostile-keys" '' |
| 48 | environment="VALLEY_PRINCIPAL=integrator" ${pub "stranger"} |
| 49 | ''; |
| 50 | |
| 51 | # The registry the operator lands: the operator governs, pushes, and |
| 52 | # holds request. The requester holds request only by hand. |
| 53 | registry = pkgs.writeText "registry.cue" '' |
| 54 | package identity |
| 55 | |
| 56 | boundaries: { |
| 57 | "push": kind: "git-push" |
| 58 | "request": kind: "request" |
| 59 | "registry": kind: "registry" |
| 60 | } |
| 61 | genesis: "operator" |
| 62 | principals: "operator": { |
| 63 | kind: "human" |
| 64 | keys: [{ |
| 65 | class: "ssh-ed25519" |
| 66 | bound: "hardware" |
| 67 | public: "${lib.concatStringsSep " " (lib.take 2 (lib.splitString " " (pub "operator")))}" |
| 68 | }] |
| 69 | grants: { |
| 70 | push: boundary: "push" |
| 71 | request: boundary: "request" |
| 72 | govern: boundary: "registry" |
| 73 | } |
| 74 | } |
| 75 | ''; |
| 76 | |
| 77 | # What the registry's next version adds, appended to it: the operator |
| 78 | # holds restack too. |
| 79 | restackGrant = pkgs.writeText "restack.cue" '' |
| 80 | boundaries: "restack": kind: "restack" |
| 81 | principals: "operator": grants: restack: boundary: "restack" |
| 82 | ''; |
| 83 | in |
| 84 | { |
| 85 | ssh-e2e = pkgs.testers.runNixOSTest { |
| 86 | name = "valley-ssh-e2e"; |
| 87 | nodes.host = |
| 88 | { lib, ... }: |
| 89 | { |
| 90 | imports = [ self.nixosModules.default ]; |
| 91 | environment.systemPackages = [ pkgs.git ]; |
| 92 | services.valley = { |
| 93 | enable = true; |
| 94 | config = declaration; |
| 95 | authorizedKeys = [ |
| 96 | { |
| 97 | principal = "integrator"; |
| 98 | key = pub "integrator"; |
| 99 | } |
| 100 | { |
| 101 | principal = "contributor"; |
| 102 | key = pub "contributor"; |
| 103 | } |
| 104 | { |
| 105 | principal = "requester"; |
| 106 | key = pub "requester"; |
| 107 | } |
| 108 | # Declared under the tag the registry gives it, so the operator |
| 109 | # can push before the first compilation. |
| 110 | { |
| 111 | principal = "operator"; |
| 112 | key = pub "operator"; |
| 113 | } |
| 114 | # A principal that holds no grant anywhere, which is what a |
| 115 | # host renders for a key that only reads. |
| 116 | { |
| 117 | principal = "reader"; |
| 118 | key = pub "reader"; |
| 119 | } |
| 120 | # A key that names no principal. |
| 121 | (pub "stranger") |
| 122 | ]; |
| 123 | grants.request = [ |
| 124 | "operator" |
| 125 | "requester" |
| 126 | ]; |
| 127 | identity.enable = true; |
| 128 | integrator.instanceProject = "instance"; |
| 129 | }; |
| 130 | # The same host with the hand grant removed, as the end of the |
| 131 | # handoff leaves it. |
| 132 | specialisation.handed-off.configuration.services.valley.grants.request = lib.mkForce [ ]; |
| 133 | # The same host with sshd misconfigured to accept the variables the |
| 134 | # push boundary reads, which the module's assertions refuse. They |
| 135 | # are switched off here so the git user's shell can be shown to |
| 136 | # neutralise what sshd lets through anyway. |
| 137 | specialisation.misconfigured.configuration = { |
| 138 | services.openssh.settings.AcceptEnv = "VALLEY_PRINCIPAL GIT_*"; |
| 139 | assertions = lib.mkForce [ ]; |
| 140 | }; |
| 141 | }; |
| 142 | |
| 143 | testScript = '' |
| 144 | import shlex |
| 145 | |
| 146 | host.wait_for_unit("multi-user.target") |
| 147 | host.wait_for_unit("sshd.service") |
| 148 | host.wait_until_succeeds("test -L /srv/git/project.git/hooks/pre-receive") |
| 149 | |
| 150 | host.succeed( |
| 151 | "mkdir -p /root/keys", |
| 152 | "cp ${keys}/* /root/keys/", |
| 153 | "chmod 600 /root/keys/*", |
| 154 | "git config --global user.name valley-check", |
| 155 | "git config --global user.email valley-check@localhost", |
| 156 | "git init --quiet --initial-branch=main /root/work", |
| 157 | "git -C /root/work commit --quiet --allow-empty -m one", |
| 158 | ) |
| 159 | |
| 160 | # Every push carries a commit the server has not seen, so that it is |
| 161 | # a write the hook has to decide, and never an up-to-date no-op. |
| 162 | def push(who, refspec, repo="project", env="", ssh=""): |
| 163 | command = ( |
| 164 | f"ssh -i /root/keys/{who} -o IdentitiesOnly=yes -o BatchMode=yes" |
| 165 | " -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null " + ssh |
| 166 | ) |
| 167 | return ( |
| 168 | "cd /root/work && git commit --quiet --allow-empty -m push && " |
| 169 | f"{env} GIT_SSH_COMMAND={shlex.quote(command)}" |
| 170 | f" git push --quiet git@localhost:{repo}.git {refspec} 2>&1" |
| 171 | ) |
| 172 | |
| 173 | def refused(command, says): |
| 174 | out = host.fail(command) |
| 175 | assert says in out, f"expected {says!r} in:\n{out}" |
| 176 | |
| 177 | request = "refs/the-valley/integration-requests/main/one" |
| 178 | |
| 179 | with subtest("the principal is the one the key's entry names"): |
| 180 | host.succeed(push("contributor", "+HEAD:refs/heads/topic")) |
| 181 | refused(push("contributor", f"HEAD:{request}"), "contributor may not write") |
| 182 | refused(push("contributor", "HEAD:refs/heads/main"), "a protected ref of project") |
| 183 | refused(push("stranger", "HEAD:refs/heads/main", repo="instance"), "<untagged key> may not write") |
| 184 | host.succeed(push("integrator", "HEAD:refs/heads/main", repo="instance")) |
| 185 | |
| 186 | with subtest("a key that holds no grant can fetch, is refused the writes that need one, and gets no shell"): |
| 187 | reader = ( |
| 188 | "ssh -i /root/keys/reader -o IdentitiesOnly=yes -o BatchMode=yes" |
| 189 | " -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null" |
| 190 | ) |
| 191 | host.succeed( |
| 192 | f"GIT_SSH_COMMAND={shlex.quote(reader)} git ls-remote git@localhost:project.git refs/heads/topic | grep -q topic", |
| 193 | f"rm -rf /root/reader-clone && GIT_SSH_COMMAND={shlex.quote(reader)} git clone --quiet git@localhost:project.git /root/reader-clone", |
| 194 | "git -C /root/reader-clone rev-parse --verify --quiet origin/topic", |
| 195 | ) |
| 196 | refused(push("reader", "HEAD:refs/heads/main"), "reader may not write refs/heads/main") |
| 197 | refused(push("reader", "HEAD:refs/tags/v1"), "reader may not write refs/tags/v1") |
| 198 | refused(push("reader", f"HEAD:{request}"), "reader holds none") |
| 199 | # Topic branches are open to any named key, and git-shell serves |
| 200 | # git-upload-archive, so neither is refused here. |
| 201 | # git-shell has no interactive mode, and runs no command but git's. |
| 202 | refused(f"{reader} git@localhost </dev/null 2>&1", "Interactive git shell is not enabled") |
| 203 | refused(f"{reader} git@localhost env 2>&1", "unrecognized command 'env'") |
| 204 | |
| 205 | with subtest("a client cannot supply the principal itself"): |
| 206 | spoof = "VALLEY_PRINCIPAL=integrator" |
| 207 | send = "-o SendEnv=VALLEY_PRINCIPAL -o SetEnv=VALLEY_PRINCIPAL=integrator" |
| 208 | refused( |
| 209 | push("stranger", "HEAD:refs/heads/main", repo="instance", env=spoof, ssh=send), |
| 210 | "<untagged key> may not write", |
| 211 | ) |
| 212 | refused(push("contributor", f"HEAD:{request}", env=spoof, ssh=send), "contributor may not write") |
| 213 | # A key file in the git user's home is not one sshd reads for it. |
| 214 | host.succeed( |
| 215 | "mkdir -p /srv/git/.ssh", |
| 216 | "cp ${hostileKeys} /srv/git/.ssh/authorized_keys", |
| 217 | "chown -R git:git /srv/git/.ssh", |
| 218 | ) |
| 219 | refused(push("stranger", "HEAD:refs/heads/main", repo="instance"), "<untagged key> may not write") |
| 220 | host.succeed("rm -r /srv/git/.ssh") |
| 221 | # And no key reaches a shell to set anything with. |
| 222 | host.fail( |
| 223 | "ssh -i /root/keys/stranger -o IdentitiesOnly=yes -o BatchMode=yes" |
| 224 | " -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null git@localhost env" |
| 225 | ) |
| 226 | |
| 227 | with subtest("a host that has not converged takes no push, and still serves fetches"): |
| 228 | host.succeed( |
| 229 | "printf '#!/bin/sh\\nexit 0\\n' > /srv/git/project.git/hooks/pre-receive.hand", |
| 230 | "mv -f /srv/git/project.git/hooks/pre-receive /srv/git/project.git/hooks/pre-receive.managed", |
| 231 | "mv /srv/git/project.git/hooks/pre-receive.hand /srv/git/project.git/hooks/pre-receive", |
| 232 | "chmod +x /srv/git/project.git/hooks/pre-receive", |
| 233 | "chown -h git:git /srv/git/project.git/hooks/pre-receive", |
| 234 | ) |
| 235 | host.fail("systemctl restart valley-init.service") |
| 236 | refused(push("integrator", "HEAD:refs/heads/main", repo="instance"), "pushes to this host are paused") |
| 237 | refused(push("contributor", "+HEAD:refs/heads/topic"), "pushes to this host are paused") |
| 238 | host.succeed( |
| 239 | "GIT_SSH_COMMAND='ssh -i /root/keys/stranger -o IdentitiesOnly=yes -o BatchMode=yes" |
| 240 | " -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null'" |
| 241 | " git ls-remote git@localhost:project.git refs/heads/topic | grep -q topic" |
| 242 | ) |
| 243 | host.succeed("rm /srv/git/project.git/hooks/pre-receive", "systemctl restart valley-init.service") |
| 244 | host.succeed(push("contributor", "+HEAD:refs/heads/topic")) |
| 245 | # The operator's hold outlasts a convergence, and only its |
| 246 | # removal lifts it. |
| 247 | host.succeed("sudo -u git touch /srv/git/.valley-hold", "systemctl restart valley-init.service") |
| 248 | refused(push("contributor", "+HEAD:refs/heads/topic"), "pushes to this host are held by its operator") |
| 249 | host.succeed("rm /srv/git/.valley-hold") |
| 250 | host.succeed(push("contributor", "+HEAD:refs/heads/topic")) |
| 251 | |
| 252 | with subtest("before the registry compiles, the hand grant is the only one"): |
| 253 | host.succeed(push("operator", f"HEAD:{request}")) |
| 254 | host.succeed(push("requester", f"+HEAD:{request}")) |
| 255 | host.fail("grep -q '^request ' /var/lib/valley-identity/grants") |
| 256 | |
| 257 | # Nothing but a move of the registry's ref can start the compiler: |
| 258 | # neither timer runs, and no compilation is under way or queued. |
| 259 | def quiesce_compiler(): |
| 260 | host.succeed("systemctl stop valley-identity.timer valley-identity-midnight.timer") |
| 261 | host.wait_until_succeeds( |
| 262 | "test \"$(systemctl show -P ActiveState valley-identity)\" != activating" |
| 263 | " && ! systemctl list-jobs --no-legend | grep -q valley-identity", |
| 264 | timeout=60, |
| 265 | ) |
| 266 | |
| 267 | # Which run of valley-init the host is on. Each run removes the |
| 268 | # converged record first, pausing every push, and writes it last. |
| 269 | def init_run(): |
| 270 | return host.succeed("systemctl show -P InvocationID valley-init").strip() |
| 271 | |
| 272 | with subtest("a landing on main compiles the registry, reruns no valley-init, and adds the compiled grant to the hand grant"): |
| 273 | quiesce_compiler() |
| 274 | before = init_run() |
| 275 | host.succeed( |
| 276 | "rm -rf /root/instance && git init --quiet --initial-branch=main /root/instance", |
| 277 | "mkdir -p /root/instance/identity && cp ${registry} /root/instance/identity/registry.cue", |
| 278 | "git -C /root/instance add -A && git -C /root/instance commit --quiet -m registry", |
| 279 | ) |
| 280 | command = ( |
| 281 | "ssh -i /root/keys/integrator -o IdentitiesOnly=yes -o BatchMode=yes" |
| 282 | " -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null" |
| 283 | ) |
| 284 | host.succeed( |
| 285 | f"cd /root/instance && GIT_SSH_COMMAND={shlex.quote(command)}" |
| 286 | " git push --quiet --force git@localhost:instance.git HEAD:refs/heads/main" |
| 287 | ) |
| 288 | host.wait_until_succeeds("grep -qx 'request operator' /var/lib/valley-identity/grants", timeout=60) |
| 289 | assert init_run() == before, "the compilation ran valley-init again, pausing every push" |
| 290 | host.succeed(push("operator", f"+HEAD:{request}")) |
| 291 | host.succeed(push("requester", f"+HEAD:{request}")) |
| 292 | |
| 293 | with subtest("a valley-init that fails before touching its record holds the compiler back, and its repair compiles"): |
| 294 | host.succeed( |
| 295 | "cat ${registry} ${restackGrant} > /root/instance/identity/registry.cue", |
| 296 | "git -C /root/instance commit --quiet -am restack", |
| 297 | ) |
| 298 | host.succeed( |
| 299 | f"cd /root/instance && GIT_SSH_COMMAND={shlex.quote(command)}" |
| 300 | " git push --quiet --force git@localhost:instance.git HEAD:refs/heads/restack" |
| 301 | ) |
| 302 | quiesce_compiler() |
| 303 | # valley-init fails before its script runs, so the record of its |
| 304 | # last success stays where the compiler could still read it. |
| 305 | host.succeed( |
| 306 | "mkdir -p /run/systemd/system/valley-init.service.d", |
| 307 | "printf '[Service]\\nExecStartPre=/run/current-system/sw/bin/false\\n'" |
| 308 | " > /run/systemd/system/valley-init.service.d/fail.conf", |
| 309 | "systemctl daemon-reload", |
| 310 | ) |
| 311 | host.fail("systemctl restart valley-init.service") |
| 312 | host.succeed("test -e /srv/git/.valley-converged") |
| 313 | compiled = host.succeed("systemctl show -P InvocationID valley-identity").strip() |
| 314 | cursor = host.succeed("journalctl -n 0 --show-cursor").split("cursor: ")[-1].strip() |
| 315 | # The move starts the compiler through its path unit, and that |
| 316 | # start fails for valley-init's failure. |
| 317 | host.succeed("sudo -u git git -C /srv/git/instance.git update-ref refs/heads/main refs/heads/restack") |
| 318 | host.wait_until_succeeds( |
| 319 | f"journalctl -q --after-cursor={shlex.quote(cursor)}" |
| 320 | " UNIT=valley-identity.service JOB_TYPE=start JOB_RESULT=dependency | grep -q .", |
| 321 | timeout=60, |
| 322 | ) |
| 323 | assert host.succeed("systemctl show -P InvocationID valley-identity").strip() == compiled, "the compiler ran after valley-init failed" |
| 324 | host.fail("grep -qx 'restack operator' /var/lib/valley-identity/grants") |
| 325 | host.succeed( |
| 326 | "rm /run/systemd/system/valley-init.service.d/fail.conf", |
| 327 | "systemctl daemon-reload", |
| 328 | "systemctl restart valley-init.service", |
| 329 | ) |
| 330 | host.wait_until_succeeds("grep -qx 'restack operator' /var/lib/valley-identity/grants", timeout=60) |
| 331 | |
| 332 | with subtest("what sshd lets through, the git user's shell does not"): |
| 333 | host.succeed( |
| 334 | "/run/booted-system/specialisation/misconfigured/bin/switch-to-configuration test", |
| 335 | "systemctl restart valley-init.service", |
| 336 | ) |
| 337 | spoof = "VALLEY_PRINCIPAL=integrator" |
| 338 | refused( |
| 339 | push("stranger", "+HEAD:refs/heads/main", repo="instance", env=spoof, ssh="-o SendEnv=VALLEY_PRINCIPAL"), |
| 340 | "<untagged key> may not write", |
| 341 | ) |
| 342 | # Sent through, these would point git's hooks at nothing, and the |
| 343 | # protected main would take the push. |
| 344 | hooks_off = "GIT_CONFIG_COUNT=1 GIT_CONFIG_KEY_0=core.hooksPath GIT_CONFIG_VALUE_0=/dev/null" |
| 345 | refused( |
| 346 | push("contributor", "+HEAD:refs/heads/main", env=hooks_off, ssh="-o SendEnv=GIT_CONFIG_*"), |
| 347 | "a protected ref of project", |
| 348 | ) |
| 349 | host.succeed( |
| 350 | "/run/booted-system/bin/switch-to-configuration test", |
| 351 | "systemctl restart valley-init.service", |
| 352 | ) |
| 353 | |
| 354 | with subtest("removing the hand grant leaves the compiled one standing"): |
| 355 | host.succeed( |
| 356 | "/run/booted-system/specialisation/handed-off/bin/switch-to-configuration test", |
| 357 | "systemctl restart valley-init.service", |
| 358 | ) |
| 359 | host.succeed("grep -qx 'request operator' /var/lib/valley-identity/grants") |
| 360 | host.succeed(push("operator", f"+HEAD:{request}")) |
| 361 | refused(push("requester", f"--delete {request}"), "requester holds none") |
| 362 | ''; |
| 363 | }; |
| 364 | } |