| 1 | # shellcheck shell=bash |
| 2 | # The signers publication, end to end. The publisher the module renders |
| 3 | # writes it into an instance repository, and fresh clones of a project |
| 4 | # that names that instance verify a landing with it. Both repositories run |
| 5 | # the fixture host's rendered pre-receive hook. The integrator is the real |
| 6 | # one, driving the nix stand-in of integrator/e2e, and it signs under its |
| 7 | # default name, as a host that sets no signing name has it do. |
| 8 | # |
| 9 | # Two things are written on disk rather than pushed, as the host's |
| 10 | # machinery writes them: valley-init's project list, and the publication |
| 11 | # itself. The hook refuses a push to either. Each is marked where it is |
| 12 | # made. |
| 13 | # Paths and fixtures come from the derivation environment. |
| 14 | # shellcheck disable=SC2154 |
| 15 | set -euo pipefail |
| 16 | |
| 17 | w="$TMPDIR/signers-e2e" |
| 18 | mkdir -p "$w" |
| 19 | export HOME="$w" |
| 20 | export GIT_CONFIG_NOSYSTEM=1 |
| 21 | unset VALLEY_KNOWN_SIGNERS VALLEY_INTEGRATOR_SIGNER VALLEY_PRINCIPAL |
| 22 | git config --global user.name signers-e2e |
| 23 | git config --global user.email signers-e2e@localhost |
| 24 | git config --global init.defaultBranch main |
| 25 | git config --global advice.detachedHead false |
| 26 | |
| 27 | fail() { |
| 28 | printf 'signers-e2e: %s\n' "$*" >&2 |
| 29 | for f in "$w"/out "$w"/err; do |
| 30 | if [ -s "$f" ]; then cat "$f" >&2; fi |
| 31 | done |
| 32 | exit 1 |
| 33 | } |
| 34 | |
| 35 | cp -r "$integratorE2e" "$w/stand-ins" |
| 36 | chmod -R +w "$w/stand-ins" |
| 37 | patchShebangs "$w/stand-ins" > /dev/null |
| 38 | export PATH="$w/stand-ins:$PATH" VALLEY_FAKE_STORE="$w/store" |
| 39 | |
| 40 | mkdir -p "$w/keys" |
| 41 | for who in contributor integrator rotated; do install -m 600 "$keys/$who" "$w/keys/$who"; done |
| 42 | contributor=contributor.valley.invalid/attestations |
| 43 | # The name the controller signs under: the binary's default, which is this |
| 44 | # machine's name. |
| 45 | name="$(uname -n)/attestations" |
| 46 | # The file the controller is given as --known-signers. signers_for writes it |
| 47 | # for the integrator key given. |
| 48 | signers="$w/known-signers" |
| 49 | signers_for() { |
| 50 | { |
| 51 | attest key --key "$w/keys/contributor" --name "$contributor" |
| 52 | attest key --key "$1" --name "$name" |
| 53 | } > "$signers" |
| 54 | } |
| 55 | signers_for "$w/keys/integrator" |
| 56 | |
| 57 | hook="$(grep -o '/nix/store/[^ ]*-valley-protect-open' "$initScriptPath" | head -n1)" |
| 58 | test -x "$hook" |
| 59 | instance="$w/instance.git" |
| 60 | served="$w/served.git" |
| 61 | for repo in "$instance" "$served"; do |
| 62 | git init --quiet --bare "$repo" |
| 63 | cp "$hook" "$repo/hooks/pre-receive" |
| 64 | chmod +x "$repo/hooks/pre-receive" |
| 65 | done |
| 66 | |
| 67 | # The publisher as the module renders it for a host that serves sealed as |
| 68 | # its instance repository and sets no signing name, pointed at this test's |
| 69 | # instance repository and signers file. |
| 70 | read -ra publish <<< "$publishCommand" |
| 71 | publish=("${publish[@]//\/srv\/git\/sealed.git/$instance}") |
| 72 | publish=("${publish[@]//\/var\/lib\/valley-instance\/known_signers/$signers}") |
| 73 | case " ${publish[*]} " in |
| 74 | *" publish-signers --instance-repo $instance --known-signers $signers ") ;; |
| 75 | *) fail "the rendered publisher is not the one this test drives: $publishCommand" ;; |
| 76 | esac |
| 77 | |
| 78 | # The instance repository carries the floor, and the project names it at |
| 79 | # policy/valley. |
| 80 | mkdir -p "$w/floor" |
| 81 | printf 'package verification\n\nfloor: {\n\tchecks: {}\n\tclasses: {}\n\tunclassified: {}\n}\n' > "$w/floor/floor.cue" |
| 82 | git init --quiet "$w/seed" |
| 83 | mkdir -p "$w/seed/policy/instance" |
| 84 | cp "$w/floor/floor.cue" "$w/seed/policy/instance/" |
| 85 | git -C "$w/seed" add -A |
| 86 | git -C "$w/seed" commit --quiet -m 'the floor' |
| 87 | git -C "$w/seed" push --quiet "$instance" main |
| 88 | # Written on disk, as valley-init writes it: the host's project list. |
| 89 | projects="$(printf '%s\n' instance served | git -C "$instance" hash-object -w --stdin)" |
| 90 | git -C "$instance" update-ref refs/the-valley/projects "$projects" |
| 91 | |
| 92 | git init --quiet "$w/fixture" |
| 93 | cd "$w/fixture" |
| 94 | git remote add origin "$served" |
| 95 | mkdir -p docs src checks policy/project |
| 96 | printf '%s\n' "$instance" > policy/valley |
| 97 | echo "the readme" > docs/readme.md |
| 98 | echo "the source" > src/main.txt |
| 99 | echo docs > checks/prose-format.inputs |
| 100 | echo src > checks/code-build.inputs |
| 101 | cat > policy/project/policy.cue << 'CUE' |
| 102 | package verification |
| 103 | |
| 104 | project: { |
| 105 | checks: { |
| 106 | "prose-format": {runner: "nix", attribute: "prose-format"} |
| 107 | "code-build": {runner: "nix", attribute: "code-build"} |
| 108 | } |
| 109 | classes: { |
| 110 | prose: {paths: "docs/**": true, requires: "prose-format": true} |
| 111 | code: {paths: "src/**": true, requires: "code-build": true} |
| 112 | } |
| 113 | unclassified: {} |
| 114 | } |
| 115 | CUE |
| 116 | git add -A |
| 117 | git commit --quiet -m 'seed the project' |
| 118 | git push --quiet origin main |
| 119 | |
| 120 | as() { |
| 121 | local who="$1" |
| 122 | shift |
| 123 | git config valley.principal "$who" |
| 124 | VALLEY_PRINCIPAL="$who" "$@" |
| 125 | } |
| 126 | ok() { "$@" > "$w/out" 2> "$w/err" || fail "$* failed"; } |
| 127 | id() { sed -n 's/^The change ID \([k-z]*\) is new\..*/\1/p' "$w/out"; } |
| 128 | # readied <branch>: alice's change, pushed and readied; prints the change ID. |
| 129 | readied() { |
| 130 | as alice git push --quiet origin "$1" |
| 131 | ok as alice valley ready |
| 132 | id |
| 133 | } |
| 134 | # attested <branch> <check>: readied, with its evidence signed by the |
| 135 | # contributor's key and pushed with it through the hook. |
| 136 | attested() { |
| 137 | as alice attest run --repo . --key "$w/keys/contributor" --name "$contributor" \ |
| 138 | --push origin --branch "$1" --check "$2" > "$w/out" 2> "$w/err" || fail "attest run on $1 failed" |
| 139 | ok as alice valley ready |
| 140 | id |
| 141 | } |
| 142 | # moves: main moves on, so a later landing is no fast-forward. |
| 143 | moves() { |
| 144 | git checkout --quiet -B main origin/main |
| 145 | echo "$1" >> moved.txt |
| 146 | git add moved.txt |
| 147 | git commit --quiet -m "main moves on: $1" |
| 148 | git push --quiet origin main |
| 149 | git fetch --quiet origin |
| 150 | } |
| 151 | |
| 152 | git fetch --quiet origin |
| 153 | # quick lands by fast-forward. waiting is ready and never requested. |
| 154 | git checkout --quiet -b quick origin/main |
| 155 | echo "a quick note" > docs/quick.md |
| 156 | git add docs/quick.md |
| 157 | git commit --quiet -m 'a change that lands by fast-forward' |
| 158 | quick="$(readied quick)" |
| 159 | git push --quiet origin quick:main |
| 160 | git fetch --quiet origin |
| 161 | git checkout --quiet -b waiting origin/main |
| 162 | echo "waiting" > docs/waiting.md |
| 163 | git add docs/waiting.md |
| 164 | git commit --quiet -m 'a change nobody approves' |
| 165 | waiting="$(readied waiting)" |
| 166 | # lands is landed by the integrator after main moves, so by a commit of |
| 167 | # its own. above is a change on top of it. |
| 168 | git checkout --quiet -b lands origin/main |
| 169 | echo "a second source line" >> src/main.txt |
| 170 | git commit --quiet -am 'a change the integrator lands' |
| 171 | lands="$(attested lands code-build)" |
| 172 | git checkout --quiet -b above lands |
| 173 | echo "above" > src/above.txt |
| 174 | git add src/above.txt |
| 175 | git commit --quiet -m 'a change on top of lands' |
| 176 | above="$(readied above)" |
| 177 | moves one |
| 178 | |
| 179 | integrate() { |
| 180 | integrator reconcile --repo "$served" --key "$1" --known-signers "$signers" --instance "$w/floor" \ |
| 181 | --schema "$SCHEMA_VERIFICATION" --attest-schema "$SCHEMA_ATTESTATION" --event-schema "$SCHEMA_EVENTS" \ |
| 182 | > "$w/integrator.out" 2>&1 || fail "the integrator failed: $(cat "$w/integrator.out")" |
| 183 | grep -q "^c-$2 -> refs/heads/main .*: land\$" "$w/integrator.out" \ |
| 184 | || fail "the integrator did not land c-$2: $(cat "$w/integrator.out")" |
| 185 | } |
| 186 | as requester git push --quiet origin "lands:refs/the-valley/integration-requests/main/c-$lands" |
| 187 | integrate "$w/keys/integrator" "$lands" |
| 188 | landed="$(git -C "$served" rev-parse main)" |
| 189 | [ "$(git -C "$served" rev-parse main^)" = "$(git rev-parse origin/main)" ] \ |
| 190 | || fail 'the integrator did not land lands by a commit on the moved main' |
| 191 | |
| 192 | # client: a fresh clone of the project, with a cache of its own, and |
| 193 | # neither variable set. listing runs a command in it, with any variables |
| 194 | # given first, into out and err. |
| 195 | n=0 |
| 196 | client() { |
| 197 | n=$((n + 1)) |
| 198 | c="$w/client-$n" |
| 199 | git clone --quiet "$served" "$c" |
| 200 | git -C "$c" config valley.principal alice |
| 201 | export XDG_CACHE_HOME="$w/cache-$n" |
| 202 | } |
| 203 | listing() { (cd "$c" && env "$@") > "$w/out" 2> "$w/err"; } |
| 204 | changes() { listing "$@" valley changes --all --json || fail "valley changes failed"; } |
| 205 | landing() { |
| 206 | jq -r --arg id "$1" '.repos[] | select(.repo == "served") | .changes[] | select(.change_id == $id) | .versions[-1].landing' "$w/out" |
| 207 | } |
| 208 | expect() { |
| 209 | local got |
| 210 | got="$(landing "$1")" |
| 211 | [ "$got" = "$2" ] || fail "$3: $1 reads $got, not $2" |
| 212 | } |
| 213 | # Every version's landing, one line each. |
| 214 | landings() { |
| 215 | jq -r '.repos[] | .repo as $r | .changes[] | .change_id as $id | .versions[] | "\($r) \($id) v\(.n) \(.landing)"' "$w/out" | LC_ALL=C sort |
| 216 | } |
| 217 | said() { grep -qF -- "$1" "$w/err" || fail "stderr does not say: $1"; } |
| 218 | quiet() { if grep -qF -- "$1" "$w/err"; then fail "stderr says: $1"; fi; } |
| 219 | |
| 220 | # ---------------------------------------------------------------------- |
| 221 | # With nothing published and nothing set, the landing reads uncertain and |
| 222 | # the command says why. Every other landing reads as it always did. |
| 223 | client |
| 224 | changes |
| 225 | expect "$lands" uncertain 'with nothing published and nothing set' |
| 226 | expect "$quick" landed 'a fast-forward' |
| 227 | expect "$waiting" not-landed 'a change nobody approved' |
| 228 | said 'could not be verified' |
| 229 | landings > "$w/unpublished" |
| 230 | listing valley status --json "$lands" || fail 'valley status failed' |
| 231 | [ "$(jq -r .landing "$w/out")" = uncertain ] || fail "status reads lands as $(jq -r .landing "$w/out")" |
| 232 | stack() { cut -f4 "$w/out" | tr '\n' ' '; } |
| 233 | listing valley stack "$above" || fail 'valley stack failed' |
| 234 | [ "$(stack)" = "$(git rev-parse lands) $(git rev-parse above) " ] \ |
| 235 | || fail "the stack under above is not lands and above: $(cat "$w/out")" |
| 236 | # The variables still verify it, as they did before there was a |
| 237 | # publication. |
| 238 | changes VALLEY_KNOWN_SIGNERS="$signers" VALLEY_INTEGRATOR_SIGNER="$name" |
| 239 | expect "$lands" landed-recorded 'with both variables set' |
| 240 | |
| 241 | # ---------------------------------------------------------------------- |
| 242 | # The publisher refuses a file that is not verifier keys, and publishes |
| 243 | # nothing. |
| 244 | echo 'not a verifier key' > "$w/garbage" |
| 245 | if integrator publish-signers --instance-repo "$instance" --known-signers "$w/garbage" > "$w/out" 2> "$w/err"; then |
| 246 | fail 'the publisher published a file that is not verifier keys' |
| 247 | fi |
| 248 | said 'the known-signers file does not read' |
| 249 | [ -z "$(git -C "$instance" for-each-ref refs/the-valley/signers)" ] || fail 'a refused run published something' |
| 250 | |
| 251 | # Written on disk, as the host's publisher writes it: one tree holding the |
| 252 | # controller's signers file and the name it signs under, beside the |
| 253 | # approval signers files, empty on a host that compiles no registry, and |
| 254 | # the instance's identity. The clients below read it as they read a tree |
| 255 | # of the first two alone. |
| 256 | "${publish[@]}" > "$w/out" 2> "$w/err" || fail 'the publisher failed' |
| 257 | published="$(git -C "$instance" rev-parse refs/the-valley/signers)" |
| 258 | [ "$(git -C "$instance" cat-file -t "$published")" = tree ] || fail 'the publication is not a tree' |
| 259 | [ "$(git -C "$instance" ls-tree --name-only "$published" | tr '\n' ' ')" = "approval-signers approval-signers-hardware instance-id integrator-name known-signers " ] \ |
| 260 | || fail "the publication holds $(git -C "$instance" ls-tree --name-only "$published")" |
| 261 | [ "$(git -C "$instance" cat-file blob "$published:integrator-name")" = "$name" ] \ |
| 262 | || fail 'the publication does not name the signer the controller signs under' |
| 263 | git -C "$instance" cat-file blob "$published:known-signers" | cmp -s - "$signers" \ |
| 264 | || fail "the publication does not hold the controller's signers file" |
| 265 | "${publish[@]}" > "$w/out" 2> "$w/err" || fail 'the publisher failed the second time' |
| 266 | [ "$(git -C "$instance" rev-parse refs/the-valley/signers)" = "$published" ] \ |
| 267 | || fail 'publishing what is already published moved the ref' |
| 268 | grep -q 'already publishes' "$w/out" || fail 'the publisher did not say nothing changed' |
| 269 | |
| 270 | # ---------------------------------------------------------------------- |
| 271 | # A fresh client with nothing set reads the landing as landed-recorded, |
| 272 | # and says nothing about verifying. Every other landing reads as it did |
| 273 | # with nothing published. |
| 274 | client |
| 275 | changes |
| 276 | expect "$lands" landed-recorded 'on a fresh client with nothing set' |
| 277 | quiet 'could not be verified' |
| 278 | quiet 'no transfer statement is verified' |
| 279 | landings | sed "s/^served $lands v1 landed-recorded\$/served $lands v1 uncertain/" > "$w/published" |
| 280 | diff "$w/unpublished" "$w/published" > /dev/null \ |
| 281 | || fail "the publication changed another landing: $(diff "$w/unpublished" "$w/published")" |
| 282 | [ "$(jq -r --arg id "$lands" '.repos[] | .changes[] | select(.change_id == $id) | .versions[-1].landed_commit' "$w/out")" = "$landed" ] \ |
| 283 | || fail 'the landing does not name the commit it landed as' |
| 284 | listing valley status --json "$lands" || fail 'valley status failed' |
| 285 | [ "$(jq -r .landing "$w/out")" = landed-recorded ] || fail "status reads lands as $(jq -r .landing "$w/out")" |
| 286 | quiet 'could not be verified' |
| 287 | # lands is no layer under above any more, and rebase drops its commits: |
| 288 | # above's own commit goes onto main alone. |
| 289 | listing valley stack "$above" || fail 'valley stack failed' |
| 290 | [ "$(stack)" = "$(git rev-parse above) " ] \ |
| 291 | || fail "the stack under above still holds more than above: $(cat "$w/out")" |
| 292 | listing VALLEY_PRINCIPAL=alice valley rebase "$above" || fail 'valley rebase failed' |
| 293 | [ "$(git -C "$served" rev-parse above^)" = "$landed" ] || fail 'above was not rebased onto the landing' |
| 294 | [ "$(git -C "$served" diff --name-only main above)" = src/above.txt ] \ |
| 295 | || fail "above carries more than its own commit: $(git -C "$served" diff --name-only main above)" |
| 296 | |
| 297 | # ---------------------------------------------------------------------- |
| 298 | # Overrides. A variable beats the publication: a file that reads, under a |
| 299 | # name it holds a key for, is used, and verifies nothing here. |
| 300 | attest key --key "$w/keys/rotated" --name other.valley.invalid/attestations > "$w/other-signers" |
| 301 | other=(VALLEY_KNOWN_SIGNERS="$w/other-signers" VALLEY_INTEGRATOR_SIGNER=other.valley.invalid/attestations) |
| 302 | changes "${other[@]}" |
| 303 | expect "$lands" uncertain 'with both variables naming another signer' |
| 304 | quiet 'no transfer statement is verified' |
| 305 | # A flag beats a variable. |
| 306 | listing "${other[@]}" valley changes --all --json --known-signers "$signers" --integrator-name "$name" \ |
| 307 | || fail 'changes failed with the flags' |
| 308 | expect "$lands" landed-recorded 'with the flags set over the variables' |
| 309 | listing "${other[@]}" valley status --json --known-signers="$signers" --integrator-name="$name" "$lands" \ |
| 310 | || fail 'status failed with the flags' |
| 311 | [ "$(jq -r .landing "$w/out")" = landed-recorded ] || fail "status with the flags reads lands as $(jq -r .landing "$w/out")" |
| 312 | # Each value comes from its own first source: a name from the variable, |
| 313 | # with the published file, which holds no key under it, is reported. |
| 314 | changes VALLEY_INTEGRATOR_SIGNER=other.valley.invalid/attestations |
| 315 | expect "$lands" uncertain 'with the published file and another name' |
| 316 | said "the known-signers file published at refs/the-valley/signers in $instance holds no key under the integrator name other.valley.invalid/attestations (from VALLEY_INTEGRATOR_SIGNER), so no transfer statement is verified" |
| 317 | # An override that does not read is reported, and the publication is not |
| 318 | # used in its place. |
| 319 | changes VALLEY_KNOWN_SIGNERS="$w/missing" |
| 320 | expect "$lands" uncertain 'with a variable naming no file' |
| 321 | said "the known-signers file $w/missing (from VALLEY_KNOWN_SIGNERS) cannot be read" |
| 322 | listing valley changes --all --json --known-signers "$w/garbage" || fail 'changes failed with a garbage file' |
| 323 | expect "$lands" uncertain 'with a flag naming a file that is not verifier keys' |
| 324 | said "the known-signers file $w/garbage (from --known-signers) does not read as verifier keys" |
| 325 | if listing valley changes --all --json --integrator-name; then fail 'a flag with no value was taken'; fi |
| 326 | said '--integrator-name needs a value' |
| 327 | |
| 328 | # ---------------------------------------------------------------------- |
| 329 | # No push replaces the publication or deletes it, forced or not, whatever |
| 330 | # the pusher holds: requester holds the request and restack grants. |
| 331 | cd "$c" |
| 332 | for refspec in "+HEAD^{tree}:refs/the-valley/signers" "+HEAD:refs/the-valley/signers" ":refs/the-valley/signers"; do |
| 333 | if VALLEY_PRINCIPAL=requester git push "$instance" "$refspec" > "$w/out" 2> "$w/err"; then |
| 334 | fail "the push of $refspec was taken" |
| 335 | fi |
| 336 | said 'refs/the-valley/signers is written only by the machinery on the host, and no push may write it' |
| 337 | [ "$(git -C "$instance" rev-parse refs/the-valley/signers)" = "$published" ] \ |
| 338 | || fail "the push of $refspec moved the publication" |
| 339 | done |
| 340 | cd "$w/fixture" |
| 341 | |
| 342 | # ---------------------------------------------------------------------- |
| 343 | # A signer the signer projects file names is never this host's integrator. |
| 344 | # The publisher refuses a list that names the name it publishes. A client |
| 345 | # refuses one too, however it came to be published, and a published list |
| 346 | # that does not read or is not a file: it verifies no transfer statement. |
| 347 | # A list naming only a worker leaves the landing as it was. Each list is |
| 348 | # written on disk beside the published signers, as no publisher writes it. |
| 349 | printf '%s served\n' "$name" > "$w/restricts-the-integrator" |
| 350 | if integrator publish-signers --instance-repo "$instance" --known-signers "$signers" \ |
| 351 | --signer-projects "$w/restricts-the-integrator" > "$w/out" 2> "$w/err"; then |
| 352 | fail 'the publisher published a signer projects file that restricts the integrator name' |
| 353 | fi |
| 354 | said "names $name, the name the integrator signs under" |
| 355 | [ "$(git -C "$instance" rev-parse refs/the-valley/signers)" = "$published" ] || fail 'a refused run moved the publication' |
| 356 | # with_list <file>: the publication, with the file beside the signers. |
| 357 | with_list() { |
| 358 | local blob |
| 359 | blob="$(git -C "$instance" hash-object -w "$1")" |
| 360 | { git -C "$instance" ls-tree "$published" && printf '100644 blob %s\tsigner-projects\n' "$blob"; } \ |
| 361 | | git -C "$instance" mktree > "$w/tree" |
| 362 | git -C "$instance" update-ref refs/the-valley/signers "$(cat "$w/tree")" |
| 363 | } |
| 364 | with_list "$w/restricts-the-integrator" |
| 365 | client |
| 366 | changes |
| 367 | expect "$lands" uncertain 'with a published list that restricts the integrator name' |
| 368 | said "is restricted to projects by the published signer-projects" |
| 369 | printf 'worker.valley.invalid/attestations served\302\240instance\n' > "$w/unreadable-list" |
| 370 | with_list "$w/unreadable-list" |
| 371 | client |
| 372 | changes |
| 373 | expect "$lands" uncertain 'with a published list that does not read' |
| 374 | said "signer-projects in refs/the-valley/signers in $instance does not read" |
| 375 | # An entry that is a tree, not a file, would restrict nobody if it were |
| 376 | # read as absent. |
| 377 | { git -C "$instance" ls-tree "$published" \ |
| 378 | && printf '040000 tree %s\tsigner-projects\n' "$(git -C "$instance" mktree < /dev/null)"; } \ |
| 379 | | git -C "$instance" mktree > "$w/tree" |
| 380 | git -C "$instance" update-ref refs/the-valley/signers "$(cat "$w/tree")" |
| 381 | client |
| 382 | changes |
| 383 | expect "$lands" uncertain 'with a published signer-projects that is a tree' |
| 384 | said "signer-projects in refs/the-valley/signers in $instance is not a file" |
| 385 | printf 'worker.valley.invalid/attestations served\n' > "$w/worker-list" |
| 386 | with_list "$w/worker-list" |
| 387 | client |
| 388 | changes |
| 389 | expect "$lands" landed-recorded 'with a published list that restricts only a worker' |
| 390 | quiet 'could not be verified' |
| 391 | git -C "$instance" update-ref refs/the-valley/signers "$published" |
| 392 | |
| 393 | # ---------------------------------------------------------------------- |
| 394 | # The integrator's key is rotated: the signers file now holds the new key |
| 395 | # under the same name, and the next landing is signed with it. Until the |
| 396 | # publisher runs again, a fresh client cannot verify that landing. After |
| 397 | # it, the client reads it landed-recorded. |
| 398 | cp "$signers" "$w/before-rotation" |
| 399 | signers_for "$w/keys/rotated" |
| 400 | git fetch --quiet origin |
| 401 | git checkout --quiet -b second origin/main |
| 402 | echo "a third source line" >> src/main.txt |
| 403 | git commit --quiet -am 'a change landed after the rotation' |
| 404 | second="$(attested second code-build)" |
| 405 | moves two |
| 406 | as requester git push --quiet origin "second:refs/the-valley/integration-requests/main/c-$second" |
| 407 | integrate "$w/keys/rotated" "$second" |
| 408 | client |
| 409 | changes |
| 410 | expect "$second" uncertain 'before the rotated signers are published' |
| 411 | "${publish[@]}" > "$w/out" 2> "$w/err" || fail 'the publisher failed after the rotation' |
| 412 | rotated="$(git -C "$instance" rev-parse refs/the-valley/signers)" |
| 413 | [ "$rotated" != "$published" ] || fail 'the rotated signers were not published' |
| 414 | git -C "$instance" cat-file blob "$rotated:known-signers" | cmp -s - "$signers" \ |
| 415 | || fail 'the publication does not hold the rotated signers file' |
| 416 | client |
| 417 | changes |
| 418 | expect "$second" landed-recorded 'after the rotated signers are published' |
| 419 | |
| 420 | # ---------------------------------------------------------------------- |
| 421 | # Two runs at once take turns, so an older run cannot undo the rotation. |
| 422 | # The older run reads the signers file as it was before the rotation. It |
| 423 | # reads it from a FIFO, so it stops mid-read, holding the lock, until the |
| 424 | # test writes the file and closes the FIFO. Once it has the FIFO open, the |
| 425 | # path holds that file, which the run reads again after it publishes. The newer run, the host's, |
| 426 | # reads the rotated file. It must wait for the older run's lock rather |
| 427 | # than publish first, so it publishes last and the rotation stands. |
| 428 | # Without the lock, the newer run would publish while the older one |
| 429 | # reads, and the older one would then put the old keys back. |
| 430 | # |
| 431 | # flocked <pid> <holds|waits>: /proc/locks shows the process holding a |
| 432 | # flock, or waiting for one. |
| 433 | flocked() { |
| 434 | local arrow='' |
| 435 | if [ "$2" = waits ]; then arrow='-> '; fi |
| 436 | grep -Eq "^[0-9]+: ${arrow}FLOCK +ADVISORY +WRITE +$1 " /proc/locks |
| 437 | } |
| 438 | # running <pid>: the process has not ended. |
| 439 | running() { [ -e "/proc/$1" ] && [ "$(sed 's/.*) //' "/proc/$1/stat" | cut -d' ' -f1)" != Z ]; } |
| 440 | # until_flocked <pid> <holds|waits> <what>: waits a minute at most for |
| 441 | # flocked to say so, and fails if the process ends first. |
| 442 | until_flocked() { |
| 443 | for _ in $(seq 600); do |
| 444 | if flocked "$1" "$2"; then return 0; fi |
| 445 | running "$1" || fail "$3" |
| 446 | sleep 0.1 |
| 447 | done |
| 448 | fail "$3, after a minute" |
| 449 | } |
| 450 | mkfifo "$w/stale" |
| 451 | integrator publish-signers --instance-repo "$instance" --known-signers "$w/stale" > "$w/older.out" 2>&1 & |
| 452 | older=$! |
| 453 | until_flocked "$older" holds "the older run held no lock while it read the signers file: $(cat "$w/older.out")" |
| 454 | # This returns once the older run has opened the FIFO to read it. The |
| 455 | # newer run is not given the FIFO, which would keep the older run's read |
| 456 | # from ending. |
| 457 | exec 3> "$w/stale" |
| 458 | rm "$w/stale" && cp "$w/before-rotation" "$w/stale" |
| 459 | "${publish[@]}" > "$w/newer.out" 2>&1 3>&- & |
| 460 | newer=$! |
| 461 | until_flocked "$newer" waits "the newer run did not wait for the older run: $(cat "$w/newer.out")" |
| 462 | cat "$w/before-rotation" >&3 |
| 463 | exec 3>&- |
| 464 | wait "$older" || fail "the older run failed: $(cat "$w/older.out")" |
| 465 | wait "$newer" || fail "the newer run failed: $(cat "$w/newer.out")" |
| 466 | grep -q 'now publishes' "$w/newer.out" || fail "the newer run did not publish after the older run: $(cat "$w/newer.out")" |
| 467 | git -C "$instance" cat-file blob refs/the-valley/signers:known-signers | cmp -s - "$signers" \ |
| 468 | || fail 'an older run put back the signers the rotation replaced' |
| 469 | client |
| 470 | changes |
| 471 | expect "$second" landed-recorded 'after two runs at once' |
| 472 | |
| 473 | # ---------------------------------------------------------------------- |
| 474 | # Every signing key is revoked. The registry in the instance repository |
| 475 | # keeps the integrator's key but no longer lets it sign, and nobody else |
| 476 | # signs either, so the compiler renders a known-signers file with no key |
| 477 | # in it. The host publishes that file all the same. A fresh clone, with |
| 478 | # the cache the last client filled as it verified the landing signed |
| 479 | # with the rotated key, then verifies no transfer statement, so both |
| 480 | # landings proved only by one read uncertain, not landed-recorded. |
| 481 | public() { cut -d' ' -f1,2 < "$keys/$1.pub"; } |
| 482 | mkdir -p "$w/seed/identity" |
| 483 | cat > "$w/seed/identity/registry.cue" << CUE |
| 484 | package identity |
| 485 | |
| 486 | boundaries: "registry": kind: "registry" |
| 487 | genesis: "operator" |
| 488 | principals: { |
| 489 | "operator": { |
| 490 | kind: "human" |
| 491 | keys: [{class: "ssh-ed25519", bound: "host", public: "$(public contributor)"}] |
| 492 | grants: govern: boundary: "registry" |
| 493 | } |
| 494 | "integrator": { |
| 495 | kind: "service" |
| 496 | keys: [{class: "ssh-ed25519", bound: "host", public: "$(public rotated)"}] |
| 497 | expires: "2099-01-01" |
| 498 | } |
| 499 | } |
| 500 | CUE |
| 501 | git -C "$w/seed" add -A |
| 502 | git -C "$w/seed" commit --quiet -m 'revoke every signing key' |
| 503 | git -C "$w/seed" push --quiet "$instance" main |
| 504 | identity compile --repo "$instance" --known-signers "$signers" --authorized-keys "$w/authorized_keys" \ |
| 505 | --grants "$w/grants" --now 2026-10-21 > "$w/out" 2> "$w/err" || fail 'the registry did not compile' |
| 506 | if grep -v '^#' "$signers" > "$w/out"; then fail 'the compiled signers file still holds a key'; fi |
| 507 | "${publish[@]}" > "$w/out" 2> "$w/err" || fail 'the publisher refused a signers file with no key' |
| 508 | said "holds no key under $name" |
| 509 | revoked="$(git -C "$instance" rev-parse refs/the-valley/signers)" |
| 510 | [ "$revoked" != "$rotated" ] || fail 'the signers file with no key was not published' |
| 511 | git -C "$instance" cat-file blob "$revoked:known-signers" | cmp -s - "$signers" \ |
| 512 | || fail 'the publication does not hold the signers file with no key' |
| 513 | warm="$XDG_CACHE_HOME" |
| 514 | client |
| 515 | export XDG_CACHE_HOME="$warm" |
| 516 | changes |
| 517 | expect "$second" uncertain 'after every signing key is revoked' |
| 518 | expect "$lands" uncertain 'after every signing key is revoked' |
| 519 | expect "$quick" landed 'a fast-forward, after every signing key is revoked' |
| 520 | said "holds no key under the integrator name $name" |
| 521 | |
| 522 | # ---------------------------------------------------------------------- |
| 523 | # A host that restricts a worker's signer to projects gives its publisher |
| 524 | # the signer projects file it gives the controllers, and the publication |
| 525 | # carries it beside the signers. The file is in the publisher's command, so |
| 526 | # a changed list is a changed unit, which an activation runs again. Here |
| 527 | # each host's command runs as an activation would run it. |
| 528 | # publish_as <command>: the rendered publisher, pointed at this test's |
| 529 | # instance repository and signers file. |
| 530 | publish_as() { |
| 531 | local -a cmd |
| 532 | read -ra cmd <<< "$1" |
| 533 | cmd=("${cmd[@]//\/srv\/git\/sealed.git/$instance}") |
| 534 | cmd=("${cmd[@]//\/var\/lib\/valley-instance\/known_signers/$signers}") |
| 535 | "${cmd[@]}" > "$w/out" 2> "$w/err" || fail "the publisher failed: $1" |
| 536 | } |
| 537 | # list_of <command>: the signer projects file the command is given. |
| 538 | list_of() { grep -o -- '--signer-projects [^ ]*' <<< "$1" | cut -d' ' -f2; } |
| 539 | names() { git -C "$instance" ls-tree --name-only "$1" | tr '\n' ' '; } |
| 540 | |
| 541 | restricted_list="$(list_of "$restrictedPublishCommand")" |
| 542 | [ -n "$restricted_list" ] || fail "the restricting host's publisher is given no signer projects file: $restrictedPublishCommand" |
| 543 | [ "$(cat "$restricted_list")" = "$(printf '%s\n' 'retired.valley.invalid/attestations' 'worker.valley.invalid/attestations guarded sealed')" ] \ |
| 544 | || fail "the rendered signer projects file holds: $(cat "$restricted_list")" |
| 545 | publish_as "$restrictedPublishCommand" |
| 546 | listed="$(git -C "$instance" rev-parse refs/the-valley/signers)" |
| 547 | [ "$(names "$listed")" = "approval-signers approval-signers-hardware instance-id integrator-name known-signers signer-projects " ] \ |
| 548 | || fail "the publication of a restricting host holds $(names "$listed")" |
| 549 | git -C "$instance" cat-file blob "$listed:signer-projects" | cmp -s - "$restricted_list" \ |
| 550 | || fail 'the publication does not hold the signer projects file the controllers read' |
| 551 | git -C "$instance" cat-file blob "$listed:known-signers" | cmp -s - "$signers" \ |
| 552 | || fail 'the signers file changed with the list' |
| 553 | |
| 554 | changed_list="$(list_of "$changedPublishCommand")" |
| 555 | [ -n "$changed_list" ] && [ "$changed_list" != "$restricted_list" ] \ |
| 556 | || fail 'a changed list renders the same publisher command, so no activation would run it again' |
| 557 | publish_as "$changedPublishCommand" |
| 558 | changed="$(git -C "$instance" rev-parse refs/the-valley/signers)" |
| 559 | [ "$changed" != "$listed" ] || fail 'the changed list was not published' |
| 560 | git -C "$instance" cat-file blob "$changed:signer-projects" | cmp -s - "$changed_list" \ |
| 561 | || fail 'the publication does not hold the changed list' |
| 562 | |
| 563 | # A list that does not read publishes nothing, and the last publication |
| 564 | # stands. |
| 565 | printf '%s\n' 'worker.valley.invalid/attestations sealed' 'worker.valley.invalid/attestations guarded' > "$w/named-twice" |
| 566 | if integrator publish-signers --instance-repo "$instance" --known-signers "$signers" \ |
| 567 | --signer-projects "$w/named-twice" > "$w/out" 2> "$w/err"; then |
| 568 | fail 'the publisher published a signer projects file that names a signer twice' |
| 569 | fi |
| 570 | said "the signer projects file $w/named-twice does not read" |
| 571 | [ "$(git -C "$instance" rev-parse refs/the-valley/signers)" = "$changed" ] || fail 'a refused run moved the publication' |
| 572 | |
| 573 | # A host that restricts nobody publishes no list. |
| 574 | "${publish[@]}" > "$w/out" 2> "$w/err" || fail 'the publisher failed once the list was gone' |
| 575 | [ "$(names refs/the-valley/signers)" = "approval-signers approval-signers-hardware instance-id integrator-name known-signers " ] \ |
| 576 | || fail "the list outlived the restriction: the publication holds $(names refs/the-valley/signers)" |
| 577 | |
| 578 | touch "$out" |