| 1 | # An integrator landing, published: one NixOS machine running the module |
| 2 | # with a controller, the bus and a push mirror, and a change that lands by |
| 3 | # integration rather than by push. The landing has to reach the mirror and |
| 4 | # the bus exactly as a push does, though the integrator moves the ref with |
| 5 | # update-ref under its own user, and post-receive never runs. The mirror is |
| 6 | # missing at first. The landing's event has to reach the bus anyway, at |
| 7 | # once, and the landing has to stay queued through the failed mirror push |
| 8 | # until systemd's retry publishes it. The host also publishes, in its |
| 9 | # instance repository, the signers a client verifies the landing with, and |
| 10 | # publishes them again when the signers file is replaced. |
| 11 | { |
| 12 | pkgs, |
| 13 | lib, |
| 14 | self, |
| 15 | packages, |
| 16 | ... |
| 17 | }: |
| 18 | let |
| 19 | # Throwaway keys, made once at build time, and the signers file the |
| 20 | # controller is given. They hold no authority anywhere: the machine that |
| 21 | # trusts them exists for one test run. |
| 22 | keys = |
| 23 | pkgs.runCommand "valley-publish-e2e-keys" |
| 24 | { |
| 25 | nativeBuildInputs = [ |
| 26 | pkgs.openssh |
| 27 | packages.attest |
| 28 | ]; |
| 29 | } |
| 30 | '' |
| 31 | mkdir $out |
| 32 | for who in integrator contributor; do |
| 33 | ssh-keygen -q -t ed25519 -N "" -C "$who" -f "$out/$who" |
| 34 | done |
| 35 | attest key --key "$out/contributor" --name contributor/attestations > "$out/known_signers" |
| 36 | attest key --key "$out/integrator" --name host/attestations > "$out/integrator_signer" |
| 37 | ''; |
| 38 | |
| 39 | declaration = pkgs.writeText "valley-publish-e2e.cue" '' |
| 40 | package valley |
| 41 | |
| 42 | projects: { |
| 43 | // The instance repository, which carries the floor. |
| 44 | "instance": protection: refs: ["refs/heads/main"] |
| 45 | "project": { |
| 46 | protection: {} |
| 47 | mirrors: ["/srv/mirror.git"] |
| 48 | } |
| 49 | } |
| 50 | ''; |
| 51 | |
| 52 | # A floor that requires nothing, so a change lands on the strength of its |
| 53 | # delta applying, and the test needs no evidence and no nix evaluation. |
| 54 | floor = pkgs.writeText "floor.cue" '' |
| 55 | package verification |
| 56 | |
| 57 | floor: { |
| 58 | checks: {} |
| 59 | classes: {} |
| 60 | unclassified: {} |
| 61 | } |
| 62 | ''; |
| 63 | in |
| 64 | { |
| 65 | publish-e2e = pkgs.testers.runNixOSTest { |
| 66 | name = "valley-publish-e2e"; |
| 67 | nodes.host = { |
| 68 | imports = [ self.nixosModules.default ]; |
| 69 | environment.systemPackages = [ |
| 70 | pkgs.git |
| 71 | pkgs.natscli |
| 72 | pkgs.jq |
| 73 | ]; |
| 74 | # The signers file is a copy made at boot, at a path the test |
| 75 | # replaces as the registry compiler replaces its own file. |
| 76 | systemd.tmpfiles.rules = [ |
| 77 | "d /var/lib/valley-signers-file 0755 root root - -" |
| 78 | "C /var/lib/valley-signers-file/known_signers 0644 root root - ${keys}/known_signers" |
| 79 | ]; |
| 80 | services.valley = { |
| 81 | enable = true; |
| 82 | config = declaration; |
| 83 | authorizedKeys = [ (lib.fileContents "${keys}/contributor.pub") ]; |
| 84 | bus.enable = true; |
| 85 | integrator = { |
| 86 | enable = true; |
| 87 | signingKeyFile = "${keys}/integrator"; |
| 88 | knownSignersFile = "/var/lib/valley-signers-file/known_signers"; |
| 89 | instanceProject = "instance"; |
| 90 | interval = "2s"; |
| 91 | }; |
| 92 | }; |
| 93 | }; |
| 94 | |
| 95 | testScript = '' |
| 96 | import json |
| 97 | |
| 98 | host.wait_for_unit("multi-user.target") |
| 99 | host.wait_for_unit("valley-publish@project.path") |
| 100 | host.wait_until_succeeds("test -d /srv/git/project.git/valley-publish-queue") |
| 101 | |
| 102 | # The repositories belong to the git user, so git reads them as it. |
| 103 | git = "sudo -u git git" |
| 104 | queue = "/srv/git/project.git/valley-publish-queue" |
| 105 | |
| 106 | def unit(prop): |
| 107 | return host.succeed(f"systemctl show -p {prop} --value valley-publish@project").strip() |
| 108 | |
| 109 | # Every payload on the bus, as published. |
| 110 | def payloads(): |
| 111 | out = host.succeed( |
| 112 | "n=$(nats --server nats://127.0.0.1:4222 stream info valley --json | jq .state.messages);" |
| 113 | " for i in $(seq 1 $n); do" |
| 114 | " nats --server nats://127.0.0.1:4222 stream get valley $i --json | jq -r .data | base64 -d; echo;" |
| 115 | " done" |
| 116 | ) |
| 117 | return [line for line in out.splitlines() if line.strip()] |
| 118 | |
| 119 | def events(): |
| 120 | return [json.loads(line) for line in payloads()] |
| 121 | |
| 122 | # The mirror does not exist yet, so the first push to it fails. |
| 123 | |
| 124 | # Two commits: the floor, which is main on both repositories, and a |
| 125 | # change one commit ahead of it. |
| 126 | commit = "git -c user.name=valley-check -c user.email=valley-check@localhost commit --quiet" |
| 127 | host.succeed( |
| 128 | "mkdir -p /tmp/work/policy/instance", |
| 129 | "cp ${floor} /tmp/work/policy/instance/floor.cue", |
| 130 | f"cd /tmp/work && git init --quiet --initial-branch=main && git add -A && {commit} -m floor", |
| 131 | f"cd /tmp/work && echo change > change.txt && git add -A && {commit} -m change", |
| 132 | ) |
| 133 | base = host.succeed("git -C /tmp/work rev-parse HEAD~1").strip() |
| 134 | change = host.succeed("git -C /tmp/work rev-parse HEAD").strip() |
| 135 | host.succeed("chown -R git:git /tmp/work") |
| 136 | |
| 137 | # Seeded by fetching into the served repositories. A fetch runs no |
| 138 | # hook, so nothing here is published, and the mirror and the bus see |
| 139 | # only what the landing does. |
| 140 | def seed(repo, refspec): |
| 141 | host.succeed(f"{git} -C /srv/git/{repo}.git fetch --quiet /tmp/work {refspec}") |
| 142 | |
| 143 | seed("instance", f"{base}:refs/heads/main") |
| 144 | seed("project", f"{base}:refs/heads/main") |
| 145 | host.fail(f"{git} -C /srv/mirror.git rev-parse --verify --quiet refs/heads/main") |
| 146 | # The request ref alone files the change for integration. |
| 147 | seed("project", f"{change}:refs/the-valley/integration-requests/main/one") |
| 148 | |
| 149 | with subtest("the landing moves main"): |
| 150 | host.wait_until_succeeds( |
| 151 | f"test \"$({git} -C /srv/git/project.git rev-parse refs/heads/main)\" = {change}", timeout=120 |
| 152 | ) |
| 153 | |
| 154 | with subtest("a failed mirror push keeps the move queued and fails the run"): |
| 155 | host.wait_until_succeeds( |
| 156 | "journalctl -t valley-mirror | grep -q 'project: push to /srv/mirror.git FAILED'", timeout=60 |
| 157 | ) |
| 158 | host.wait_until_succeeds("test \"$(systemctl show -p SubState --value valley-publish@project)\" = auto-restart") |
| 159 | host.succeed(f"test -n \"$(ls -A {queue})\"") |
| 160 | |
| 161 | # The integrator publishes the event itself, so the mirror being down |
| 162 | # does not hold it back. Its payload is byte for byte the one the host |
| 163 | # publishes for a push of the same move. |
| 164 | with subtest("the landing is on the bus while the mirror is down"): |
| 165 | host.wait_until_succeeds( |
| 166 | "nats --server nats://127.0.0.1:4222 stream get valley --last-for valley.git.project.ref-updated" |
| 167 | ) |
| 168 | host.succeed(f"test -n \"$(ls -A {queue})\"") |
| 169 | moved = [p for p in payloads() if '"event":"ref-updated"' in p] |
| 170 | assert moved == [ |
| 171 | f'{{"event":"ref-updated","repo":"project","ref":"refs/heads/main","old":"{base}","new":"{change}"}}' |
| 172 | ], moved |
| 173 | |
| 174 | with subtest("systemd retries the push after a delay, and the retry reaches the mirror"): |
| 175 | host.succeed( |
| 176 | "mkdir /srv/mirror.git && chown git:git /srv/mirror.git", |
| 177 | f"{git} init --quiet --bare /srv/mirror.git", |
| 178 | ) |
| 179 | host.wait_until_succeeds( |
| 180 | f"test \"$({git} -C /srv/mirror.git rev-parse refs/heads/main)\" = {change}", timeout=90 |
| 181 | ) |
| 182 | host.wait_until_succeeds("journalctl -t valley-mirror | grep -q 'project: pushed to /srv/mirror.git'") |
| 183 | host.wait_until_succeeds(f"test -z \"$(ls -A {queue})\"") |
| 184 | host.wait_until_succeeds("test \"$(systemctl show -p ActiveState --value valley-publish@project)\" = inactive") |
| 185 | # One failed run, then one retry 30 seconds later: no respin. |
| 186 | restarts = unit("NRestarts") |
| 187 | assert restarts == "1", restarts |
| 188 | |
| 189 | with subtest("a move that cannot be deleted fails the run, which waits to retry"): |
| 190 | # A directory in the queue is a move rm -f cannot delete. |
| 191 | host.succeed(f"sudo -u git mkdir {queue}/stuck", f"sudo -u git touch {queue}/stuck/x") |
| 192 | host.wait_until_succeeds( |
| 193 | "journalctl -u valley-publish@project | grep -q 'cannot remove .*stuck'", timeout=60 |
| 194 | ) |
| 195 | host.wait_until_succeeds("test \"$(systemctl show -p SubState --value valley-publish@project)\" = auto-restart") |
| 196 | restarts = unit("NRestarts") |
| 197 | host.sleep(10) |
| 198 | assert unit("NRestarts") == restarts, "the drain restarted without waiting" |
| 199 | assert unit("SubState") == "auto-restart" |
| 200 | host.succeed("systemctl is-active valley-publish@project.path") |
| 201 | host.succeed(f"rm -r {queue}/stuck") |
| 202 | host.wait_until_succeeds( |
| 203 | "test \"$(systemctl show -p ActiveState --value valley-publish@project)\" = inactive", timeout=300 |
| 204 | ) |
| 205 | host.succeed(f"test -z \"$(ls -A {queue})\"") |
| 206 | |
| 207 | with subtest("the landing is one ref-updated event on the bus"): |
| 208 | moved = [e for e in events() if e["event"] == "ref-updated"] |
| 209 | assert moved == [ |
| 210 | {"event": "ref-updated", "repo": "project", "ref": "refs/heads/main", "old": base, "new": change} |
| 211 | ], moved |
| 212 | landed = [e for e in events() if e["event"] == "integration-succeeded"] |
| 213 | assert len(landed) == 1 and landed[0]["new"] == change, landed |
| 214 | |
| 215 | # What a client verifies the landing with, in the instance repository: |
| 216 | # the controllers' signers file, and the name they sign under, which |
| 217 | # this host leaves to the default, the machine's own name. The file |
| 218 | # replaced by a rename, as the registry compiler replaces its own, is |
| 219 | # published again with no activation. |
| 220 | with subtest("the host publishes the controllers' signers, and again when the file changes"): |
| 221 | ref = "refs/the-valley/signers" |
| 222 | instance = f"{git} -C /srv/git/instance.git" |
| 223 | signers = "/var/lib/valley-signers-file/known_signers" |
| 224 | host.succeed( |
| 225 | f"test \"$({instance} cat-file -t {ref})\" = tree", |
| 226 | f"test \"$({instance} cat-file blob {ref}:integrator-name)\" = host/attestations", |
| 227 | f"{instance} cat-file blob {ref}:known-signers | cmp - {signers}", |
| 228 | ) |
| 229 | host.succeed( |
| 230 | f"cat {signers} ${keys}/integrator_signer > /var/lib/valley-signers-file/next", |
| 231 | f"mv -f /var/lib/valley-signers-file/next {signers}", |
| 232 | ) |
| 233 | host.wait_until_succeeds(f"{instance} cat-file blob {ref}:known-signers | cmp - {signers}", timeout=60) |
| 234 | host.succeed(f"{instance} cat-file blob {ref}:known-signers | grep -q '^host/attestations+'") |
| 235 | ''; |
| 236 | }; |
| 237 | } |