| 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. No push over ssh |
| 11 | # replaces that publication or deletes it. The controllers pass on a |
| 12 | # one-hour interval, so every landing here is started by a mark in a |
| 13 | # controller's wake directory: the host's hook on a push, and the instance |
| 14 | # controller's own mark after it lands the floor. |
| 15 | { |
| 16 | pkgs, |
| 17 | lib, |
| 18 | self, |
| 19 | packages, |
| 20 | ... |
| 21 | }: |
| 22 | let |
| 23 | # Throwaway keys, made once at build time, and the signers file the |
| 24 | # controller is given. They hold no authority anywhere: the machine that |
| 25 | # trusts them exists for one test run. forged_signer is the contributor's |
| 26 | # key under the integrator's name, what a forged publication would hold. |
| 27 | keys = |
| 28 | pkgs.runCommand "valley-publish-e2e-keys" |
| 29 | { |
| 30 | nativeBuildInputs = [ |
| 31 | pkgs.openssh |
| 32 | packages.attest |
| 33 | ]; |
| 34 | } |
| 35 | '' |
| 36 | mkdir $out |
| 37 | for who in integrator contributor; do |
| 38 | ssh-keygen -q -t ed25519 -N "" -C "$who" -f "$out/$who" |
| 39 | done |
| 40 | attest key --key "$out/contributor" --name contributor/attestations > "$out/known_signers" |
| 41 | attest key --key "$out/integrator" --name host/attestations > "$out/integrator_signer" |
| 42 | attest key --key "$out/contributor" --name host/attestations > "$out/forged_signer" |
| 43 | ''; |
| 44 | |
| 45 | declaration = pkgs.writeText "valley-publish-e2e.cue" '' |
| 46 | package valley |
| 47 | |
| 48 | projects: { |
| 49 | // The instance repository, which carries the floor. |
| 50 | "instance": protection: refs: ["refs/heads/main"] |
| 51 | "project": { |
| 52 | protection: {} |
| 53 | mirrors: ["/srv/mirror.git"] |
| 54 | } |
| 55 | } |
| 56 | ''; |
| 57 | |
| 58 | # A floor that requires nothing, so a change lands on the strength of its |
| 59 | # delta applying, and the test needs no evidence and no nix evaluation. |
| 60 | floor = pkgs.writeText "floor.cue" '' |
| 61 | package verification |
| 62 | |
| 63 | floor: { |
| 64 | checks: {} |
| 65 | classes: {} |
| 66 | unclassified: {} |
| 67 | } |
| 68 | ''; |
| 69 | in |
| 70 | { |
| 71 | publish-e2e = pkgs.testers.runNixOSTest { |
| 72 | name = "valley-publish-e2e"; |
| 73 | nodes.host = { |
| 74 | imports = [ self.nixosModules.default ]; |
| 75 | environment.systemPackages = [ |
| 76 | pkgs.git |
| 77 | pkgs.natscli |
| 78 | pkgs.jq |
| 79 | ]; |
| 80 | # The signers file is a copy made at boot, at a path the test |
| 81 | # replaces as the registry compiler replaces its own file. |
| 82 | systemd.tmpfiles.rules = [ |
| 83 | "d /var/lib/valley-signers-file 0755 root root - -" |
| 84 | "C /var/lib/valley-signers-file/known_signers 0644 root root - ${keys}/known_signers" |
| 85 | ]; |
| 86 | services.valley = { |
| 87 | enable = true; |
| 88 | config = declaration; |
| 89 | authorizedKeys = [ (lib.fileContents "${keys}/contributor.pub") ]; |
| 90 | bus.enable = true; |
| 91 | integrator = { |
| 92 | enable = true; |
| 93 | signingKeyFile = "${keys}/integrator"; |
| 94 | knownSignersFile = "/var/lib/valley-signers-file/known_signers"; |
| 95 | instanceProject = "instance"; |
| 96 | interval = "1h"; |
| 97 | }; |
| 98 | }; |
| 99 | }; |
| 100 | |
| 101 | testScript = '' |
| 102 | import json |
| 103 | import time |
| 104 | |
| 105 | host.wait_for_unit("multi-user.target") |
| 106 | host.wait_for_unit("valley-publish@project.path") |
| 107 | host.wait_until_succeeds("test -d /srv/git/project.git/valley-publish-queue") |
| 108 | |
| 109 | # The repositories belong to the git user, so git reads them as it. |
| 110 | git = "sudo -u git git" |
| 111 | queue = "/srv/git/project.git/valley-publish-queue" |
| 112 | |
| 113 | def unit(prop): |
| 114 | return host.succeed(f"systemctl show -p {prop} --value valley-publish@project").strip() |
| 115 | |
| 116 | # Every payload on the bus, as published. |
| 117 | def payloads(): |
| 118 | out = host.succeed( |
| 119 | "n=$(nats --server nats://127.0.0.1:4222 stream info valley --json | jq .state.messages);" |
| 120 | " for i in $(seq 1 $n); do" |
| 121 | " nats --server nats://127.0.0.1:4222 stream get valley $i --json | jq -r .data | base64 -d; echo;" |
| 122 | " done" |
| 123 | ) |
| 124 | return [line for line in out.splitlines() if line.strip()] |
| 125 | |
| 126 | def events(): |
| 127 | return [json.loads(line) for line in payloads()] |
| 128 | |
| 129 | # The mirror does not exist yet, so the first push to it fails. |
| 130 | |
| 131 | # Two commits: the floor, which is main on both repositories, and a |
| 132 | # change one commit ahead of it. |
| 133 | commit = "git -c user.name=valley-check -c user.email=valley-check@localhost commit --quiet" |
| 134 | host.succeed( |
| 135 | "mkdir -p /tmp/work/policy/instance", |
| 136 | "cp ${floor} /tmp/work/policy/instance/floor.cue", |
| 137 | f"cd /tmp/work && git init --quiet --initial-branch=main && git add -A && {commit} -m floor", |
| 138 | f"cd /tmp/work && echo change > change.txt && git add -A && {commit} -m change", |
| 139 | ) |
| 140 | base = host.succeed("git -C /tmp/work rev-parse HEAD~1").strip() |
| 141 | change = host.succeed("git -C /tmp/work rev-parse HEAD").strip() |
| 142 | host.succeed("chown -R git:git /tmp/work") |
| 143 | |
| 144 | # Seeded by fetching into the served repositories. A fetch runs no |
| 145 | # hook, so nothing here is published, and the mirror and the bus see |
| 146 | # only what the landing does. |
| 147 | def seed(repo, refspec): |
| 148 | host.succeed(f"{git} -C /srv/git/{repo}.git fetch --quiet /tmp/work {refspec}") |
| 149 | |
| 150 | seed("instance", f"{base}:refs/heads/main") |
| 151 | seed("project", f"{base}:refs/heads/main") |
| 152 | host.fail(f"{git} -C /srv/mirror.git rev-parse --verify --quiet refs/heads/main") |
| 153 | # The request ref alone files the change for integration. |
| 154 | seed("project", f"{change}:refs/the-valley/integration-requests/main/one") |
| 155 | |
| 156 | # The fetch ran no hook, so nothing marked the controller. The hook the |
| 157 | # host installed is run by hand as the git user, as a push runs it: |
| 158 | # moves of the integrator's own bookkeeping mark nothing, and the |
| 159 | # request's move marks the project's controller, which lands it at |
| 160 | # once on its one-hour interval. |
| 161 | wakes = "/run/valley-integrator-wake" |
| 162 | hook = "/srv/git/project.git/hooks/post-receive.d/valley-integrator-wake" |
| 163 | zero = "0" * 40 |
| 164 | |
| 165 | def due_passes(project): |
| 166 | return int(host.succeed(f"journalctl -u valley-integrator@{project} -o cat | grep -c '^pass: due$' || true").strip()) |
| 167 | |
| 168 | with subtest("the host's hook marks the controller for what it reads, and only that"): |
| 169 | host.succeed(f"test -L {hook}") |
| 170 | before = due_passes("project") |
| 171 | host.succeed( |
| 172 | f"printf '%s %s %s\\n' {zero} {change} refs/the-valley/integration-outcomes/main/one" |
| 173 | f" {zero} {change} refs/the-valley/knots | sudo -u git {hook}" |
| 174 | ) |
| 175 | host.sleep(2) |
| 176 | assert due_passes("project") == before, "bookkeeping marked the controller" |
| 177 | marked = time.monotonic() |
| 178 | host.succeed( |
| 179 | f"printf '%s %s %s\\n' {zero} {change} refs/the-valley/integration-requests/main/one | sudo -u git {hook}" |
| 180 | ) |
| 181 | |
| 182 | # The change lands as one commit the integrator makes on the base, |
| 183 | # with the change's tree. That commit is what main, the mirror and |
| 184 | # the events carry from here on. |
| 185 | with subtest("the landing moves main"): |
| 186 | host.wait_until_succeeds( |
| 187 | f"test \"$({git} -C /srv/git/project.git rev-parse refs/heads/main^)\" = {base}", timeout=120 |
| 188 | ) |
| 189 | took = time.monotonic() - marked |
| 190 | print(f"mark to landing, one-hour interval, VM: {took:.1f}s") |
| 191 | assert took < 60, took |
| 192 | assert due_passes("project") > before |
| 193 | landing = host.succeed(f"{git} -C /srv/git/project.git rev-parse refs/heads/main").strip() |
| 194 | assert landing != change, "the change's own commit landed" |
| 195 | tree = f"{git} -C /srv/git/project.git rev-parse" |
| 196 | assert host.succeed(f"{tree} {landing}^{{tree}}") == host.succeed(f"{tree} {change}^{{tree}}") |
| 197 | |
| 198 | with subtest("a failed mirror push keeps the move queued and fails the run"): |
| 199 | host.wait_until_succeeds( |
| 200 | "journalctl -t valley-mirror | grep -q 'project: push to /srv/mirror.git FAILED'", timeout=60 |
| 201 | ) |
| 202 | host.wait_until_succeeds("test \"$(systemctl show -p SubState --value valley-publish@project)\" = auto-restart") |
| 203 | host.succeed(f"test -n \"$(ls -A {queue})\"") |
| 204 | |
| 205 | # The integrator publishes the event itself, so the mirror being down |
| 206 | # does not hold it back. Its payload is byte for byte the one the host |
| 207 | # publishes for a push of the same move. |
| 208 | with subtest("the landing is on the bus while the mirror is down"): |
| 209 | host.wait_until_succeeds( |
| 210 | "nats --server nats://127.0.0.1:4222 stream get valley --last-for valley.git.project.ref-updated" |
| 211 | ) |
| 212 | host.succeed(f"test -n \"$(ls -A {queue})\"") |
| 213 | moved = [p for p in payloads() if '"event":"ref-updated"' in p and '"ref":"refs/heads/main"' in p] |
| 214 | assert moved == [ |
| 215 | f'{{"event":"ref-updated","repo":"project","ref":"refs/heads/main","old":"{base}","new":"{landing}"}}' |
| 216 | ], moved |
| 217 | |
| 218 | with subtest("systemd retries the push after a delay, and the retry reaches the mirror"): |
| 219 | host.succeed( |
| 220 | "mkdir /srv/mirror.git && chown git:git /srv/mirror.git", |
| 221 | f"{git} init --quiet --bare /srv/mirror.git", |
| 222 | ) |
| 223 | host.wait_until_succeeds( |
| 224 | f"test \"$({git} -C /srv/mirror.git rev-parse refs/heads/main)\" = {landing}", timeout=90 |
| 225 | ) |
| 226 | host.wait_until_succeeds("journalctl -t valley-mirror | grep -q 'project: pushed to /srv/mirror.git'") |
| 227 | host.wait_until_succeeds(f"test -z \"$(ls -A {queue})\"") |
| 228 | host.wait_until_succeeds("test \"$(systemctl show -p ActiveState --value valley-publish@project)\" = inactive") |
| 229 | # One failed run, then one retry 30 seconds later: no respin. |
| 230 | restarts = unit("NRestarts") |
| 231 | assert restarts == "1", restarts |
| 232 | |
| 233 | with subtest("a move that cannot be deleted fails the run, which waits to retry"): |
| 234 | # A directory in the queue is a move rm -f cannot delete. |
| 235 | host.succeed(f"sudo -u git mkdir {queue}/stuck", f"sudo -u git touch {queue}/stuck/x") |
| 236 | host.wait_until_succeeds( |
| 237 | "journalctl -u valley-publish@project | grep -q 'cannot remove .*stuck'", timeout=60 |
| 238 | ) |
| 239 | host.wait_until_succeeds("test \"$(systemctl show -p SubState --value valley-publish@project)\" = auto-restart") |
| 240 | restarts = unit("NRestarts") |
| 241 | host.sleep(10) |
| 242 | assert unit("NRestarts") == restarts, "the drain restarted without waiting" |
| 243 | assert unit("SubState") == "auto-restart" |
| 244 | host.succeed("systemctl is-active valley-publish@project.path") |
| 245 | host.succeed(f"rm -r {queue}/stuck") |
| 246 | host.wait_until_succeeds( |
| 247 | "test \"$(systemctl show -p ActiveState --value valley-publish@project)\" = inactive", timeout=300 |
| 248 | ) |
| 249 | host.succeed(f"test -z \"$(ls -A {queue})\"") |
| 250 | |
| 251 | with subtest("the landing is one ref-updated event on the bus"): |
| 252 | moved = [e for e in events() if e["event"] == "ref-updated" and e["ref"] == "refs/heads/main"] |
| 253 | assert moved == [ |
| 254 | {"event": "ref-updated", "repo": "project", "ref": "refs/heads/main", "old": base, "new": landing} |
| 255 | ], moved |
| 256 | landed = [e for e in events() if e["event"] == "integration-succeeded"] |
| 257 | assert len(landed) == 1 and landed[0]["new"] == landing, landed |
| 258 | |
| 259 | # What a client verifies the landing with, in the instance repository: |
| 260 | # the controllers' signers file, and the name they sign under, which |
| 261 | # this host leaves to the default, the machine's own name. The file |
| 262 | # replaced by a rename, as the registry compiler replaces its own, is |
| 263 | # published again with no activation. |
| 264 | with subtest("the host publishes the controllers' signers, and again when the file changes"): |
| 265 | ref = "refs/the-valley/signers" |
| 266 | instance = f"{git} -C /srv/git/instance.git" |
| 267 | signers = "/var/lib/valley-signers-file/known_signers" |
| 268 | host.succeed( |
| 269 | f"test \"$({instance} cat-file -t {ref})\" = tree", |
| 270 | f"test \"$({instance} cat-file blob {ref}:integrator-name)\" = host/attestations", |
| 271 | f"{instance} cat-file blob {ref}:known-signers | cmp - {signers}", |
| 272 | ) |
| 273 | host.succeed( |
| 274 | f"cat {signers} ${keys}/integrator_signer > /var/lib/valley-signers-file/next", |
| 275 | f"mv -f /var/lib/valley-signers-file/next {signers}", |
| 276 | ) |
| 277 | host.wait_until_succeeds(f"{instance} cat-file blob {ref}:known-signers | cmp - {signers}", timeout=60) |
| 278 | host.succeed(f"{instance} cat-file blob {ref}:known-signers | grep -q '^host/attestations+'") |
| 279 | |
| 280 | # Only the host's machinery writes the publication. A push over ssh, |
| 281 | # by a key the host authorizes, cannot replace it with a well-formed |
| 282 | # publication of a key the pusher holds, and cannot delete it. |
| 283 | with subtest("no push over ssh replaces or deletes the publication"): |
| 284 | host.wait_for_unit("sshd.service") |
| 285 | published = host.succeed(f"{instance} rev-parse {ref}").strip() |
| 286 | host.succeed( |
| 287 | "mkdir -p /root/keys", |
| 288 | "install -m 600 ${keys}/contributor /root/keys/contributor", |
| 289 | "git init --quiet --bare /root/forged.git", |
| 290 | ) |
| 291 | forged = host.succeed( |
| 292 | "cd /root/forged.git" |
| 293 | " && name=$(echo host/attestations | git hash-object -w --stdin)" |
| 294 | " && file=$(git hash-object -w ${keys}/forged_signer)" |
| 295 | " && printf '100644 blob %s\\tintegrator-name\\n100644 blob %s\\tknown-signers\\n' $name $file | git mktree" |
| 296 | ).strip() |
| 297 | ssh = ( |
| 298 | "GIT_SSH_COMMAND='ssh -i /root/keys/contributor -o IdentitiesOnly=yes -o BatchMode=yes" |
| 299 | " -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null'" |
| 300 | ) |
| 301 | for refspec in [f"+{forged}:{ref}", f":{ref}"]: |
| 302 | out = host.fail(f"cd /root/forged.git && {ssh} git push git@localhost:instance.git {refspec} 2>&1") |
| 303 | assert f"{ref} is written only by the machinery on the host, and no push may write it" in out, out |
| 304 | assert host.succeed(f"{instance} rev-parse {ref}").strip() == published, refspec |
| 305 | |
| 306 | # The wake directories are the host's: one per controller, owned by |
| 307 | # the integrator's user and the writers' group, which is the git user |
| 308 | # and the integrator's user. An idle one holds nothing. |
| 309 | with subtest("each controller's wake directory is its own, and a writer group's"): |
| 310 | for project in ["instance", "project"]: |
| 311 | assert host.succeed(f"stat -c '%a %U %G' {wakes}/{project}").strip() == "770 valley-integrator valley-integrator-wake" |
| 312 | host.wait_until_succeeds(f"test -z \"$(ls -A {wakes}/{project})\"") |
| 313 | members = host.succeed("getent group valley-integrator-wake").strip().split(":")[3].split(",") |
| 314 | assert sorted(members) == ["git", "valley-integrator"], members |
| 315 | |
| 316 | # A push over ssh runs the dispatcher, whose hooks run in turn. A topic |
| 317 | # branch, which anyone may push, marks the project's controller and |
| 318 | # not the instance's. |
| 319 | with subtest("a push over ssh wakes the controller of the project it moved, and no other"): |
| 320 | host.succeed( |
| 321 | f"git init --quiet /root/topic && cd /root/topic && {commit} --allow-empty -m topic" |
| 322 | ) |
| 323 | project_before, instance_before = due_passes("project"), due_passes("instance") |
| 324 | host.succeed(f"cd /root/topic && {ssh} git push --quiet git@localhost:project.git HEAD:refs/heads/topic") |
| 325 | host.wait_until_succeeds(f"test {project_before} -lt $(journalctl -u valley-integrator@project -o cat | grep -c '^pass: due$')", timeout=30) |
| 326 | host.sleep(2) |
| 327 | assert due_passes("instance") == instance_before, "a push to project woke the instance's controller" |
| 328 | |
| 329 | # The floor moves by the instance's controller, which runs no hook. It |
| 330 | # marks the project's controller itself, from inside its sandbox, after |
| 331 | # the landing. The request is fetched, so only a push to a topic branch |
| 332 | # of the instance marks the instance's controller. |
| 333 | with subtest("a floor landed by the instance's controller wakes the controller that reads it"): |
| 334 | floor_base = host.succeed(f"{git} -C /srv/git/instance.git rev-parse refs/heads/main").strip() |
| 335 | host.succeed( |
| 336 | f"cd /tmp/work && sudo -u git git checkout --quiet -b floor {floor_base}", |
| 337 | "cd /tmp/work && sudo -u git sh -c 'echo moved > moved.txt' && sudo -u git git add moved.txt", |
| 338 | f"cd /tmp/work && sudo -u git {commit} -m 'the floor, moved'", |
| 339 | ) |
| 340 | seed("instance", "floor:refs/the-valley/integration-requests/main/floor") |
| 341 | before = due_passes("project") |
| 342 | host.succeed(f"cd /root/topic && {ssh} git push --quiet git@localhost:instance.git HEAD:refs/heads/topic") |
| 343 | host.wait_until_succeeds( |
| 344 | f"test \"$({git} -C /srv/git/instance.git rev-parse refs/heads/main^)\" = {floor_base}", timeout=60 |
| 345 | ) |
| 346 | host.wait_until_succeeds( |
| 347 | f"journalctl -u valley-integrator@instance -o cat | grep -q '^ wake the floor reader at {wakes}/project is due$'", |
| 348 | timeout=30, |
| 349 | ) |
| 350 | host.wait_until_succeeds(f"test {before} -lt $(journalctl -u valley-integrator@project -o cat | grep -c '^pass: due$')", timeout=30) |
| 351 | ''; |
| 352 | }; |
| 353 | } |