| 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 writes |
| 10 | # them: valley-init's project list, and the publication itself. No push |
| 11 | # may write either. Each is marked where it is made. |
| 12 | # Paths and fixtures come from the derivation environment. |
| 13 | # shellcheck disable=SC2154 |
| 14 | set -euo pipefail |
| 15 | |
| 16 | w="$TMPDIR/signers-e2e" |
| 17 | mkdir -p "$w" |
| 18 | export HOME="$w" |
| 19 | export GIT_CONFIG_NOSYSTEM=1 |
| 20 | unset VALLEY_KNOWN_SIGNERS VALLEY_INTEGRATOR_SIGNER VALLEY_PRINCIPAL |
| 21 | git config --global user.name signers-e2e |
| 22 | git config --global user.email signers-e2e@localhost |
| 23 | git config --global init.defaultBranch main |
| 24 | git config --global advice.detachedHead false |
| 25 | |
| 26 | fail() { |
| 27 | printf 'signers-e2e: %s\n' "$*" >&2 |
| 28 | for f in "$w"/out "$w"/err; do |
| 29 | if [ -s "$f" ]; then cat "$f" >&2; fi |
| 30 | done |
| 31 | exit 1 |
| 32 | } |
| 33 | |
| 34 | cp -r "$integratorE2e" "$w/stand-ins" |
| 35 | chmod -R +w "$w/stand-ins" |
| 36 | patchShebangs "$w/stand-ins" > /dev/null |
| 37 | export PATH="$w/stand-ins:$PATH" VALLEY_FAKE_STORE="$w/store" |
| 38 | |
| 39 | mkdir -p "$w/keys" |
| 40 | for who in contributor integrator rotated; do install -m 600 "$keys/$who" "$w/keys/$who"; done |
| 41 | contributor=contributor.valley.invalid/attestations |
| 42 | # The name the controller signs under: the binary's default, which is this |
| 43 | # machine's name. |
| 44 | name="$(uname -n)/attestations" |
| 45 | # The file the controller is given as --known-signers. signers_for writes it |
| 46 | # for the integrator key given. |
| 47 | signers="$w/known-signers" |
| 48 | signers_for() { |
| 49 | { |
| 50 | attest key --key "$w/keys/contributor" --name "$contributor" |
| 51 | attest key --key "$1" --name "$name" |
| 52 | } > "$signers" |
| 53 | } |
| 54 | signers_for "$w/keys/integrator" |
| 55 | |
| 56 | hook="$(grep -o '/nix/store/[^ ]*-valley-protect-open' "$initScriptPath" | head -n1)" |
| 57 | test -x "$hook" |
| 58 | instance="$w/instance.git" |
| 59 | served="$w/served.git" |
| 60 | for repo in "$instance" "$served"; do |
| 61 | git init --quiet --bare "$repo" |
| 62 | cp "$hook" "$repo/hooks/pre-receive" |
| 63 | chmod +x "$repo/hooks/pre-receive" |
| 64 | done |
| 65 | |
| 66 | # The publisher as the module renders it for a host that serves sealed as |
| 67 | # its instance repository and sets no signing name, pointed at this test's |
| 68 | # instance repository and signers file. |
| 69 | read -ra publish <<< "$publishCommand" |
| 70 | publish=("${publish[@]//\/srv\/git\/sealed.git/$instance}") |
| 71 | publish=("${publish[@]//\/var\/lib\/valley-instance\/known_signers/$signers}") |
| 72 | case " ${publish[*]} " in |
| 73 | *" publish-signers --instance-repo $instance --known-signers $signers ") ;; |
| 74 | *) fail "the rendered publisher is not the one this test drives: $publishCommand" ;; |
| 75 | esac |
| 76 | |
| 77 | # The instance repository carries the floor, and the project names it at |
| 78 | # policy/valley. |
| 79 | mkdir -p "$w/floor" |
| 80 | printf 'package verification\n\nfloor: {\n\tchecks: {}\n\tclasses: {}\n\tunclassified: {}\n}\n' > "$w/floor/floor.cue" |
| 81 | git init --quiet "$w/seed" |
| 82 | mkdir -p "$w/seed/policy/instance" |
| 83 | cp "$w/floor/floor.cue" "$w/seed/policy/instance/" |
| 84 | git -C "$w/seed" add -A |
| 85 | git -C "$w/seed" commit --quiet -m 'the floor' |
| 86 | git -C "$w/seed" push --quiet "$instance" main |
| 87 | # Written on disk, as valley-init writes it: the host's project list. |
| 88 | projects="$(printf '%s\n' instance served | git -C "$instance" hash-object -w --stdin)" |
| 89 | git -C "$instance" update-ref refs/the-valley/projects "$projects" |
| 90 | |
| 91 | git init --quiet "$w/fixture" |
| 92 | cd "$w/fixture" |
| 93 | git remote add origin "$served" |
| 94 | mkdir -p docs src checks policy/project |
| 95 | printf '%s\n' "$instance" > policy/valley |
| 96 | echo "the readme" > docs/readme.md |
| 97 | echo "the source" > src/main.txt |
| 98 | echo docs > checks/prose-format.inputs |
| 99 | echo src > checks/code-build.inputs |
| 100 | cat > policy/project/policy.cue << 'CUE' |
| 101 | package verification |
| 102 | |
| 103 | project: { |
| 104 | checks: { |
| 105 | "prose-format": {runner: "nix", attribute: "prose-format"} |
| 106 | "code-build": {runner: "nix", attribute: "code-build"} |
| 107 | } |
| 108 | classes: { |
| 109 | prose: {paths: "docs/**": true, requires: "prose-format": true} |
| 110 | code: {paths: "src/**": true, requires: "code-build": true} |
| 111 | } |
| 112 | unclassified: {} |
| 113 | } |
| 114 | CUE |
| 115 | git add -A |
| 116 | git commit --quiet -m 'seed the project' |
| 117 | git push --quiet origin main |
| 118 | |
| 119 | as() { |
| 120 | local who="$1" |
| 121 | shift |
| 122 | git config valley.principal "$who" |
| 123 | VALLEY_PRINCIPAL="$who" "$@" |
| 124 | } |
| 125 | ok() { "$@" > "$w/out" 2> "$w/err" || fail "$* failed"; } |
| 126 | id() { sed -n 's/^The change ID \([k-z]*\) is new\..*/\1/p' "$w/out"; } |
| 127 | # readied <branch>: alice's change, pushed and readied; prints the change ID. |
| 128 | readied() { |
| 129 | as alice git push --quiet origin "$1" |
| 130 | ok as alice valley ready |
| 131 | id |
| 132 | } |
| 133 | # attested <branch> <check>: readied, with its evidence signed by the |
| 134 | # contributor's key and pushed with it through the hook. |
| 135 | attested() { |
| 136 | as alice attest run --repo . --key "$w/keys/contributor" --name "$contributor" \ |
| 137 | --push origin --branch "$1" --check "$2" > "$w/out" 2> "$w/err" || fail "attest run on $1 failed" |
| 138 | ok as alice valley ready |
| 139 | id |
| 140 | } |
| 141 | # moves: main moves on, so a later landing is no fast-forward. |
| 142 | moves() { |
| 143 | git checkout --quiet -B main origin/main |
| 144 | echo "$1" >> moved.txt |
| 145 | git add moved.txt |
| 146 | git commit --quiet -m "main moves on: $1" |
| 147 | git push --quiet origin main |
| 148 | git fetch --quiet origin |
| 149 | } |
| 150 | |
| 151 | git fetch --quiet origin |
| 152 | # quick lands by fast-forward. waiting is ready and never requested. |
| 153 | git checkout --quiet -b quick origin/main |
| 154 | echo "a quick note" > docs/quick.md |
| 155 | git add docs/quick.md |
| 156 | git commit --quiet -m 'a change that lands by fast-forward' |
| 157 | quick="$(readied quick)" |
| 158 | git push --quiet origin quick:main |
| 159 | git fetch --quiet origin |
| 160 | git checkout --quiet -b waiting origin/main |
| 161 | echo "waiting" > docs/waiting.md |
| 162 | git add docs/waiting.md |
| 163 | git commit --quiet -m 'a change nobody approves' |
| 164 | waiting="$(readied waiting)" |
| 165 | # lands is landed by the integrator after main moves, so by a commit of |
| 166 | # its own. above is a change on top of it. |
| 167 | git checkout --quiet -b lands origin/main |
| 168 | echo "a second source line" >> src/main.txt |
| 169 | git commit --quiet -am 'a change the integrator lands' |
| 170 | lands="$(attested lands code-build)" |
| 171 | git checkout --quiet -b above lands |
| 172 | echo "above" > src/above.txt |
| 173 | git add src/above.txt |
| 174 | git commit --quiet -m 'a change on top of lands' |
| 175 | above="$(readied above)" |
| 176 | moves one |
| 177 | |
| 178 | integrate() { |
| 179 | integrator reconcile --repo "$served" --key "$1" --known-signers "$signers" --instance "$w/floor" \ |
| 180 | --schema "$SCHEMA_VERIFICATION" --attest-schema "$SCHEMA_ATTESTATION" --event-schema "$SCHEMA_EVENTS" \ |
| 181 | > "$w/integrator.out" 2>&1 || fail "the integrator failed: $(cat "$w/integrator.out")" |
| 182 | grep -q "^c-$2 -> refs/heads/main .*: land\$" "$w/integrator.out" \ |
| 183 | || fail "the integrator did not land c-$2: $(cat "$w/integrator.out")" |
| 184 | } |
| 185 | as requester git push --quiet origin "lands:refs/the-valley/integration-requests/main/c-$lands" |
| 186 | integrate "$w/keys/integrator" "$lands" |
| 187 | landed="$(git -C "$served" rev-parse main)" |
| 188 | [ "$(git -C "$served" rev-parse main^)" = "$(git rev-parse origin/main)" ] \ |
| 189 | || fail 'the integrator did not land lands by a commit on the moved main' |
| 190 | |
| 191 | # client: a fresh clone of the project, with a cache of its own, and |
| 192 | # neither variable set. listing runs a command in it, with any variables |
| 193 | # given first, into out and err. |
| 194 | n=0 |
| 195 | client() { |
| 196 | n=$((n + 1)) |
| 197 | c="$w/client-$n" |
| 198 | git clone --quiet "$served" "$c" |
| 199 | git -C "$c" config valley.principal alice |
| 200 | export XDG_CACHE_HOME="$w/cache-$n" |
| 201 | } |
| 202 | listing() { (cd "$c" && env "$@") > "$w/out" 2> "$w/err"; } |
| 203 | changes() { listing "$@" valley changes --all --json || fail "valley changes failed"; } |
| 204 | landing() { |
| 205 | jq -r --arg id "$1" '.repos[] | select(.repo == "served") | .changes[] | select(.change_id == $id) | .versions[-1].landing' "$w/out" |
| 206 | } |
| 207 | expect() { |
| 208 | local got |
| 209 | got="$(landing "$1")" |
| 210 | [ "$got" = "$2" ] || fail "$3: $1 reads $got, not $2" |
| 211 | } |
| 212 | # Every version's landing, one line each. |
| 213 | landings() { |
| 214 | jq -r '.repos[] | .repo as $r | .changes[] | .change_id as $id | .versions[] | "\($r) \($id) v\(.n) \(.landing)"' "$w/out" | LC_ALL=C sort |
| 215 | } |
| 216 | said() { grep -qF -- "$1" "$w/err" || fail "stderr does not say: $1"; } |
| 217 | quiet() { if grep -qF -- "$1" "$w/err"; then fail "stderr says: $1"; fi; } |
| 218 | |
| 219 | # ---------------------------------------------------------------------- |
| 220 | # With nothing published and nothing set, the landing reads uncertain and |
| 221 | # the command says why. Every other landing reads as it always did. |
| 222 | client |
| 223 | changes |
| 224 | expect "$lands" uncertain 'with nothing published and nothing set' |
| 225 | expect "$quick" landed 'a fast-forward' |
| 226 | expect "$waiting" not-landed 'a change nobody approved' |
| 227 | said 'could not be verified' |
| 228 | landings > "$w/unpublished" |
| 229 | listing valley status --json "$lands" || fail 'valley status failed' |
| 230 | [ "$(jq -r .landing "$w/out")" = uncertain ] || fail "status reads lands as $(jq -r .landing "$w/out")" |
| 231 | stack() { cut -f4 "$w/out" | tr '\n' ' '; } |
| 232 | listing valley-go stack "$above" || fail 'valley-go stack failed' |
| 233 | [ "$(stack)" = "$(git rev-parse lands) $(git rev-parse above) " ] \ |
| 234 | || fail "the stack under above is not lands and above: $(cat "$w/out")" |
| 235 | # The variables still verify it, as they did before there was a |
| 236 | # publication. |
| 237 | changes VALLEY_KNOWN_SIGNERS="$signers" VALLEY_INTEGRATOR_SIGNER="$name" |
| 238 | expect "$lands" landed-recorded 'with both variables set' |
| 239 | |
| 240 | # ---------------------------------------------------------------------- |
| 241 | # The publisher refuses a file that is not verifier keys, and publishes |
| 242 | # nothing. |
| 243 | echo 'not a verifier key' > "$w/garbage" |
| 244 | if integrator publish-signers --instance-repo "$instance" --known-signers "$w/garbage" > "$w/out" 2> "$w/err"; then |
| 245 | fail 'the publisher published a file that is not verifier keys' |
| 246 | fi |
| 247 | said 'the known-signers file does not read' |
| 248 | [ -z "$(git -C "$instance" for-each-ref refs/the-valley/signers)" ] || fail 'a refused run published something' |
| 249 | |
| 250 | # Written on disk, as the host's publisher writes it: one tree holding the |
| 251 | # controller's signers file and the name it signs under. |
| 252 | "${publish[@]}" > "$w/out" 2> "$w/err" || fail 'the publisher failed' |
| 253 | published="$(git -C "$instance" rev-parse refs/the-valley/signers)" |
| 254 | [ "$(git -C "$instance" cat-file -t "$published")" = tree ] || fail 'the publication is not a tree' |
| 255 | [ "$(git -C "$instance" ls-tree --name-only "$published" | tr '\n' ' ')" = "integrator-name known-signers " ] \ |
| 256 | || fail "the publication holds $(git -C "$instance" ls-tree --name-only "$published")" |
| 257 | [ "$(git -C "$instance" cat-file blob "$published:integrator-name")" = "$name" ] \ |
| 258 | || fail 'the publication does not name the signer the controller signs under' |
| 259 | git -C "$instance" cat-file blob "$published:known-signers" | cmp -s - "$signers" \ |
| 260 | || fail "the publication does not hold the controller's signers file" |
| 261 | "${publish[@]}" > "$w/out" 2> "$w/err" || fail 'the publisher failed the second time' |
| 262 | [ "$(git -C "$instance" rev-parse refs/the-valley/signers)" = "$published" ] \ |
| 263 | || fail 'publishing what is already published moved the ref' |
| 264 | grep -q 'already publishes' "$w/out" || fail 'the publisher did not say nothing changed' |
| 265 | |
| 266 | # ---------------------------------------------------------------------- |
| 267 | # A fresh client with nothing set reads the landing as landed-recorded, |
| 268 | # and says nothing about verifying. Every other landing reads as it did |
| 269 | # with nothing published. |
| 270 | client |
| 271 | changes |
| 272 | expect "$lands" landed-recorded 'on a fresh client with nothing set' |
| 273 | quiet 'could not be verified' |
| 274 | quiet 'no transfer statement is verified' |
| 275 | landings | sed "s/^served $lands v1 landed-recorded\$/served $lands v1 uncertain/" > "$w/published" |
| 276 | diff "$w/unpublished" "$w/published" > /dev/null \ |
| 277 | || fail "the publication changed another landing: $(diff "$w/unpublished" "$w/published")" |
| 278 | [ "$(jq -r --arg id "$lands" '.repos[] | .changes[] | select(.change_id == $id) | .versions[-1].landed_commit' "$w/out")" = "$landed" ] \ |
| 279 | || fail 'the landing does not name the commit it landed as' |
| 280 | listing valley status --json "$lands" || fail 'valley status failed' |
| 281 | [ "$(jq -r .landing "$w/out")" = landed-recorded ] || fail "status reads lands as $(jq -r .landing "$w/out")" |
| 282 | quiet 'could not be verified' |
| 283 | # lands is no layer under above any more, and rebase drops its commits: |
| 284 | # above's own commit goes onto main alone. |
| 285 | listing valley-go stack "$above" || fail 'valley-go stack failed' |
| 286 | [ "$(stack)" = "$(git rev-parse above) " ] \ |
| 287 | || fail "the stack under above still holds more than above: $(cat "$w/out")" |
| 288 | listing VALLEY_PRINCIPAL=alice valley rebase "$above" || fail 'valley rebase failed' |
| 289 | [ "$(git -C "$served" rev-parse above^)" = "$landed" ] || fail 'above was not rebased onto the landing' |
| 290 | [ "$(git -C "$served" diff --name-only main above)" = src/above.txt ] \ |
| 291 | || fail "above carries more than its own commit: $(git -C "$served" diff --name-only main above)" |
| 292 | |
| 293 | # ---------------------------------------------------------------------- |
| 294 | # Overrides. A variable beats the publication: a file that reads, under a |
| 295 | # name it holds a key for, is used, and verifies nothing here. |
| 296 | attest key --key "$w/keys/rotated" --name other.valley.invalid/attestations > "$w/other-signers" |
| 297 | other=(VALLEY_KNOWN_SIGNERS="$w/other-signers" VALLEY_INTEGRATOR_SIGNER=other.valley.invalid/attestations) |
| 298 | changes "${other[@]}" |
| 299 | expect "$lands" uncertain 'with both variables naming another signer' |
| 300 | quiet 'no transfer statement is verified' |
| 301 | # A flag beats a variable. |
| 302 | listing "${other[@]}" valley changes --all --json --known-signers "$signers" --integrator-name "$name" \ |
| 303 | || fail 'changes failed with the flags' |
| 304 | expect "$lands" landed-recorded 'with the flags set over the variables' |
| 305 | listing "${other[@]}" valley status --json --known-signers="$signers" --integrator-name="$name" "$lands" \ |
| 306 | || fail 'status failed with the flags' |
| 307 | [ "$(jq -r .landing "$w/out")" = landed-recorded ] || fail "status with the flags reads lands as $(jq -r .landing "$w/out")" |
| 308 | # Each value comes from its own first source: a name from the variable, |
| 309 | # with the published file, which holds no key under it, is reported. |
| 310 | changes VALLEY_INTEGRATOR_SIGNER=other.valley.invalid/attestations |
| 311 | expect "$lands" uncertain 'with the published file and another name' |
| 312 | 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" |
| 313 | # An override that does not read is reported, and the publication is not |
| 314 | # used in its place. |
| 315 | changes VALLEY_KNOWN_SIGNERS="$w/missing" |
| 316 | expect "$lands" uncertain 'with a variable naming no file' |
| 317 | said "the known-signers file $w/missing (from VALLEY_KNOWN_SIGNERS) cannot be read" |
| 318 | listing valley changes --all --json --known-signers "$w/garbage" || fail 'changes failed with a garbage file' |
| 319 | expect "$lands" uncertain 'with a flag naming a file that is not verifier keys' |
| 320 | said "the known-signers file $w/garbage (from --known-signers) does not read as verifier keys" |
| 321 | if listing valley changes --all --json --integrator-name; then fail 'a flag with no value was taken'; fi |
| 322 | said '--integrator-name needs a value' |
| 323 | |
| 324 | # ---------------------------------------------------------------------- |
| 325 | # No push replaces the publication or deletes it, forced or not, whatever |
| 326 | # the pusher holds: requester holds the request and restack grants. |
| 327 | cd "$c" |
| 328 | for refspec in "+HEAD^{tree}:refs/the-valley/signers" "+HEAD:refs/the-valley/signers" ":refs/the-valley/signers"; do |
| 329 | if VALLEY_PRINCIPAL=requester git push "$instance" "$refspec" > "$w/out" 2> "$w/err"; then |
| 330 | fail "the push of $refspec was taken" |
| 331 | fi |
| 332 | said 'refs/the-valley/signers is written only by the machinery on the host, and no push may write it' |
| 333 | [ "$(git -C "$instance" rev-parse refs/the-valley/signers)" = "$published" ] \ |
| 334 | || fail "the push of $refspec moved the publication" |
| 335 | done |
| 336 | cd "$w/fixture" |
| 337 | |
| 338 | # ---------------------------------------------------------------------- |
| 339 | # The integrator's key is rotated: the signers file now holds the new key |
| 340 | # under the same name, and the next landing is signed with it. Until the |
| 341 | # publisher runs again, a fresh client cannot verify that landing. After |
| 342 | # it, the client reads it landed-recorded. |
| 343 | signers_for "$w/keys/rotated" |
| 344 | git fetch --quiet origin |
| 345 | git checkout --quiet -b second origin/main |
| 346 | echo "a third source line" >> src/main.txt |
| 347 | git commit --quiet -am 'a change landed after the rotation' |
| 348 | second="$(attested second code-build)" |
| 349 | moves two |
| 350 | as requester git push --quiet origin "second:refs/the-valley/integration-requests/main/c-$second" |
| 351 | integrate "$w/keys/rotated" "$second" |
| 352 | client |
| 353 | changes |
| 354 | expect "$second" uncertain 'before the rotated signers are published' |
| 355 | "${publish[@]}" > "$w/out" 2> "$w/err" || fail 'the publisher failed after the rotation' |
| 356 | rotated="$(git -C "$instance" rev-parse refs/the-valley/signers)" |
| 357 | [ "$rotated" != "$published" ] || fail 'the rotated signers were not published' |
| 358 | git -C "$instance" cat-file blob "$rotated:known-signers" | cmp -s - "$signers" \ |
| 359 | || fail 'the publication does not hold the rotated signers file' |
| 360 | client |
| 361 | changes |
| 362 | expect "$second" landed-recorded 'after the rotated signers are published' |
| 363 | |
| 364 | touch "$out" |