| 1 | # End-to-end VM test of the restacker host service: one NixOS machine |
| 2 | # running the module with services.valley.restacker enabled for one |
| 3 | # project. The test performs the two deployment steps a host requires: it |
| 4 | # declares the restacker's public key under its principal with the restack |
| 5 | # grant, and it provisions the private key outside the store as root-only. |
| 6 | # The service then restacks a ready change after main moves, pushing over |
| 7 | # the host's sshd as its own principal. Its Unix user has no git group, no |
| 8 | # repository on disk, and no integrator key. Over SSH its key can push |
| 9 | # restack versions and reports, and the hook refuses pushes to main, |
| 10 | # integration requests, record deletions, and outcomes. |
| 11 | { |
| 12 | pkgs, |
| 13 | lib, |
| 14 | self, |
| 15 | packages, |
| 16 | ... |
| 17 | }: |
| 18 | let |
| 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-restack-host-e2e-keys" { nativeBuildInputs = [ pkgs.openssh ]; } '' |
| 22 | mkdir $out |
| 23 | for who in alice restacker integrator; 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 | # main is protected and only the integrator lands on it. |
| 30 | declaration = pkgs.writeText "valley-restack-host-e2e.cue" '' |
| 31 | package valley |
| 32 | |
| 33 | projects: "app": protection: {} |
| 34 | ''; |
| 35 | in |
| 36 | { |
| 37 | restack-host-e2e = pkgs.testers.runNixOSTest { |
| 38 | name = "valley-restack-host-e2e"; |
| 39 | nodes.host = { |
| 40 | imports = [ self.nixosModules.default ]; |
| 41 | environment.systemPackages = [ |
| 42 | pkgs.git |
| 43 | pkgs.jq |
| 44 | packages.valley |
| 45 | ]; |
| 46 | services.valley = { |
| 47 | enable = true; |
| 48 | config = declaration; |
| 49 | # The operator's registration: the restacker's key under its |
| 50 | # principal, and the restack grant. |
| 51 | authorizedKeys = [ |
| 52 | { |
| 53 | principal = "alice"; |
| 54 | key = pub "alice"; |
| 55 | } |
| 56 | { |
| 57 | principal = "valley-restacker"; |
| 58 | key = pub "restacker"; |
| 59 | } |
| 60 | ]; |
| 61 | grants.restack = [ "valley-restacker" ]; |
| 62 | integrator = { |
| 63 | enable = true; |
| 64 | signingKeyFile = "/run/valley-keys/valley-integrator"; |
| 65 | knownSignersFile = pkgs.writeText "valley-restack-host-e2e-signers" ""; |
| 66 | instancePolicy = "${../../examples/policy/smoke/instance}"; |
| 67 | }; |
| 68 | restacker = { |
| 69 | enable = true; |
| 70 | projects = [ "app" ]; |
| 71 | sshKeyFile = "/run/valley-keys/valley-restacker"; |
| 72 | interval = "1s"; |
| 73 | }; |
| 74 | }; |
| 75 | }; |
| 76 | |
| 77 | testScript = '' |
| 78 | host.wait_for_unit("multi-user.target") |
| 79 | host.wait_for_unit("sshd.service") |
| 80 | host.wait_until_succeeds("test -L /srv/git/app.git/hooks/pre-receive") |
| 81 | |
| 82 | # The repositories belong to the git user, so git reads them as it. |
| 83 | git = "sudo -u git git" |
| 84 | commit = "git -c user.name=valley-check -c user.email=valley-check@localhost commit --quiet" |
| 85 | def ssh(who): |
| 86 | return ( |
| 87 | f"GIT_SSH_COMMAND='ssh -i /root/keys/{who} -o IdentitiesOnly=yes -o BatchMode=yes" |
| 88 | " -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null'" |
| 89 | ) |
| 90 | |
| 91 | # main moves by a local write as the git user, which runs no hook, as |
| 92 | # the integrator lands a change. |
| 93 | def land(name): |
| 94 | host.succeed( |
| 95 | f"echo {name} > /tmp/app/{name}", |
| 96 | f"{git} -C /tmp/app add {name}", |
| 97 | f"{git} -C /tmp/app -c user.name=valley-check -c user.email=valley-check@localhost commit --quiet -m {name}", |
| 98 | f"{git} -C /srv/git/app.git fetch --quiet /tmp/app main:refs/heads/main", |
| 99 | ) |
| 100 | |
| 101 | with subtest("the operator provisions the keys, outside the store"): |
| 102 | # The restacker's is readable by root alone: systemd reads it, |
| 103 | # and hands it to each controller as a credential. The |
| 104 | # integrator's is its own user's. |
| 105 | host.succeed( |
| 106 | "install -D -m 0400 -o root -g root ${keys}/restacker /run/valley-keys/valley-restacker", |
| 107 | "install -D -m 0400 -o valley-integrator -g root ${keys}/integrator /run/valley-keys/valley-integrator", |
| 108 | "systemctl restart valley-integrator@app.service", |
| 109 | ) |
| 110 | |
| 111 | with subtest("the project is seeded, and alice readies a change"): |
| 112 | host.succeed( |
| 113 | "mkdir -p /root/keys && install -m 600 ${keys}/alice ${keys}/restacker /root/keys/", |
| 114 | "mkdir -p /tmp/app/policy/project", |
| 115 | "printf 'package verification\\n\\nproject: restack: maxAutomaticVersions: 2\\n' > /tmp/app/policy/project/policy.cue", |
| 116 | f"cd /tmp/app && git init --quiet --initial-branch=main && git add -A && {commit} -m seed", |
| 117 | "chown -R git:git /tmp/app", |
| 118 | f"{git} -C /srv/git/app.git fetch --quiet /tmp/app main:refs/heads/main", |
| 119 | f"{ssh('alice')} git clone --quiet git@localhost:app.git /root/app", |
| 120 | "git -C /root/app config valley.principal alice", |
| 121 | "git -C /root/app config user.name alice", |
| 122 | "git -C /root/app config user.email alice@localhost", |
| 123 | f"cd /root/app && git checkout --quiet -b a && echo a > a && git add a && {commit} -m a", |
| 124 | f"cd /root/app && {ssh('alice')} git push --quiet origin a", |
| 125 | f"cd /root/app && {ssh('alice')} valley ready a", |
| 126 | ) |
| 127 | change = host.succeed( |
| 128 | f"{git} -C /srv/git/app.git for-each-ref --format='%(refname:lstrip=4)' refs/the-valley/changes/main/" |
| 129 | ).strip() |
| 130 | assert len(change) == 32, change |
| 131 | record = f"refs/the-valley/changes/main/{change}" |
| 132 | |
| 133 | with subtest("after main moves, the controller restacks the change as its own principal"): |
| 134 | # Restarted, so the backoff of the passes that failed before main |
| 135 | # existed starts over. |
| 136 | host.succeed("systemctl restart valley-restacker@app.service") |
| 137 | host.wait_for_unit("valley-restacker@app.service") |
| 138 | land("landed") |
| 139 | host.wait_until_succeeds( |
| 140 | f"{git} -C /srv/git/app.git cat-file blob {record}:record.json" |
| 141 | " | jq -e '.seq == 2 and .active == 2 and .owner == \"alice\" and .versions[1].uploader == \"valley-restacker\"'", |
| 142 | timeout=120, |
| 143 | ) |
| 144 | head = host.succeed(f"{git} -C /srv/git/app.git rev-parse refs/heads/a").strip() |
| 145 | main = host.succeed(f"{git} -C /srv/git/app.git rev-parse refs/heads/main").strip() |
| 146 | host.succeed(f"test \"$({git} -C /srv/git/app.git rev-parse {head}^)\" = {main}") |
| 147 | host.wait_until_succeeds( |
| 148 | f"{git} -C /srv/git/app.git cat-file blob refs/the-valley/restack/main/{change}:report.json" |
| 149 | " | jq -e '.result == \"complete\"'", |
| 150 | timeout=60, |
| 151 | ) |
| 152 | |
| 153 | with subtest("the controller's Unix user holds nothing but its own state"): |
| 154 | host.succeed( |
| 155 | "test \"$(systemctl show -p User --value valley-restacker@app.service)\" = valley-restacker", |
| 156 | "test \"$(id -Gn valley-restacker)\" = valley-restacker", |
| 157 | "test \"$(stat -c '%U %a' /var/lib/valley-restacker/app)\" = 'valley-restacker 700'", |
| 158 | ) |
| 159 | for denied in [ |
| 160 | # The served repositories, on disk. |
| 161 | "ls /srv/git", |
| 162 | "git -C /srv/git/app.git rev-parse refs/heads/main", |
| 163 | "touch /srv/git/app.git/refs/heads/written", |
| 164 | # The integrator's key and state. |
| 165 | "cat /run/valley-keys/valley-integrator", |
| 166 | "ls /var/lib/valley-integrator", |
| 167 | # Its own key file, which only systemd reads, as root. |
| 168 | "cat /run/valley-keys/valley-restacker", |
| 169 | ]: |
| 170 | host.fail(f"sudo -u valley-restacker {denied}") |
| 171 | host.fail("sudo -u valley-integrator cat /run/credentials/valley-restacker@app.service/key") |
| 172 | |
| 173 | with subtest("over ssh its key pushes as its principal, and the hook still closes what the grant does not give"): |
| 174 | host.succeed( |
| 175 | f"cd /root/app && {ssh('restacker')} git fetch --quiet origin", |
| 176 | "cd /root/app && git checkout --quiet -B probe origin/main && git commit --quiet --allow-empty -m probe", |
| 177 | ) |
| 178 | for refused in [ |
| 179 | "probe:refs/heads/main", |
| 180 | f"probe:refs/the-valley/integration-requests/main/c-{change}", |
| 181 | "probe:refs/the-valley/integration-outcomes/main/zz-probe", |
| 182 | f":{record}", |
| 183 | ]: |
| 184 | host.fail(f"cd /root/app && {ssh('restacker')} git push --quiet origin {refused}") |
| 185 | # A topic branch, as any named principal may push. |
| 186 | host.succeed(f"cd /root/app && {ssh('restacker')} git push --quiet origin probe:refs/heads/probe") |
| 187 | host.succeed(f"test \"$({git} -C /srv/git/app.git rev-parse refs/heads/main)\" = {main}") |
| 188 | host.succeed(f"test -z \"$({git} -C /srv/git/app.git for-each-ref refs/the-valley/integration-requests/ refs/the-valley/integration-outcomes/)\"") |
| 189 | ''; |
| 190 | }; |
| 191 | } |