gunk-dev/the-valley

run.sh

source61d173ce178fmain's published tip history
23.0 KiB
1#!/usr/bin/env bash
2# The integrator, driven end to end over scratch repositories and throwaway
3# keys. It runs the real binaries: `attest` produces the evidence, the
4# `valley` deriver composes the policy, and the `integrator` judges and
5# lands. Only the nix evaluator is stood in for (integrator/e2e/nix), and
6# only because a nix build has no daemon to call.
7#
8# Each scenario below is one of the phase's exit criteria, observed rather
9# than asserted about. Nothing here touches a repository outside its own
10# scratch directory, and no ref is ever created anywhere but there.
11set -euo pipefail
12
13work="$(mktemp -d)"
14trap 'rm -rf "$work"' EXIT
15here="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
16
17export HOME="$work"
18export GIT_CONFIG_NOSYSTEM=1
19export VALLEY_FAKE_STORE="$work/store"
20export PATH="$here:$PATH" # the nix stand-ins, ahead of anything real
21
22git config --global user.name valley-check
23git config --global user.email valley-check@localhost
24git config --global init.defaultBranch main
25git config --global advice.detachedHead false
26
27say() { printf '\n=== %s\n' "$*"; }
28die() {
29 printf '\nintegrator-e2e: %s\n' "$*" >&2
30 exit 1
31}
32holds() { printf ' ok: %s\n' "$*"; }
33
34# ----------------------------------------------------------------------
35say "keys: a contributor, an integrator, and a stranger"
36
37for who in contributor integrator stranger; do
38 ssh-keygen -q -t ed25519 -N "" -C "$who" -f "$work/$who"
39done
40contributor=contributor.valley.invalid/attestations
41integrator_name=integrator.valley.invalid/attestations
42stranger=stranger.elsewhere.invalid/attestations
43
44# The known-signers file is the registry's interim compilation
45# (dcr-b87f6e8): the integrator accepts the contributor and nobody else.
46attest key --key "$work/contributor" --name "$contributor" > "$work/known_signers"
47# What a reader of a landed note holds: both parties to it.
48cat "$work/known_signers" > "$work/reader_keys"
49attest key --key "$work/integrator" --name "$integrator_name" >> "$work/reader_keys"
50attest key --key "$work/stranger" --name "$stranger" > "$work/stranger_key"
51# The key hash the integrator's own attestation refs are keyed by.
52ihash="$(attest key --key "$work/integrator" --name "$integrator_name" | cut -d+ -f2)"
53holds "the integrator holds one signer: $(cut -d+ -f1 < "$work/known_signers")"
54
55# ----------------------------------------------------------------------
56say "the served repository, and the instance policy layer"
57
58origin="$work/project.git"
59git init --quiet --bare "$origin"
60repo="$work/project"
61git init --quiet "$repo"
62cd "$repo"
63git remote add origin "$origin"
64
65mkdir -p docs src deploy checks policy
66echo "the readme" > docs/readme.md
67echo "the source" > src/main.txt
68echo "the deployment" > deploy/target.txt
69# What each check reads, which the nix stand-in turns into its closure.
70echo docs > checks/prose-format.inputs
71echo src > checks/code-build.inputs
72
73# The project layer: it declares its own classes and checks above the
74# floor, and declines nothing.
75cat > policy/project.cue <<'EOF'
76package verification
77
78project: {
79 checks: {
80 "prose-format": {runner: "nix", attribute: "prose-format"}
81 "code-build": {runner: "nix", attribute: "code-build"}
82 // An effectful check, with the validity window dcr-439b771 calls
83 // Δ. Its observation transfers while nothing in the class that
84 // requires it has landed under it and it is younger than this.
85 reachable: {runner: "command", command: "true", validity: "1h"}
86 }
87 classes: {
88 prose: {paths: "docs/**": true, requires: "prose-format": true}
89 code: {paths: "src/**": true, requires: "code-build": true}
90 deploy: {paths: "deploy/**": true, requires: reachable: true}
91 }
92 unclassified: {}
93}
94EOF
95
96git add -A
97git commit --quiet -m "seed the project"
98git push --quiet origin main
99
100# The instance layer lives outside the project's tree, and the integrator
101# reads it from there: a change never supplies the policy that gates it.
102instance="$work/instance"
103mkdir -p "$instance"
104cat > "$instance/floor.cue" <<'EOF'
105package verification
106
107floor: {
108 checks: {}
109 classes: {}
110 unclassified: {}
111}
112EOF
113holds "policy composes from $instance and the project's own policy/"
114
115# ----------------------------------------------------------------------
116# Helpers.
117
118# attest_change <branch> <check spec…> — run the checks over the branch's
119# tip and publish the branch with its attestation ref, atomically.
120attest_change() {
121 local branch="$1" key="${2:-$work/contributor}" name="${3:-$contributor}"
122 shift 3
123 attest run --repo "$repo" --key "$key" --name "$name" \
124 --push origin --branch "$branch" "$@"
125}
126
127# request <change> <target> <commit> — the contributor asks for integration
128# by pushing one ref. That ref is the whole of the request.
129request() {
130 git push --quiet origin "$3:refs/the-valley/integration-requests/$2/$1"
131}
132
133integrate() {
134 integrator reconcile \
135 --repo "$origin" \
136 --key "$work/integrator" --name "$integrator_name" \
137 --known-signers "$work/known_signers" \
138 --instance "$instance" --project-policy policy \
139 --schema "$SCHEMA_VERIFICATION" \
140 --attest-schema "$SCHEMA_ATTESTATION" \
141 --event-schema "$SCHEMA_EVENTS" \
142 ${VALLEY_E2E_NOW:+--now "$VALLEY_E2E_NOW"} \
143 2>&1 | tee "$work/last.out"
144 grep -q . "$work/last.out" || die "the integrator said nothing"
145}
146
147tip() { git -C "$origin" rev-parse refs/heads/main; }
148forget() { git -C "$origin" update-ref -d "refs/the-valley/integration-requests/main/$1"; }
149
150# ----------------------------------------------------------------------
151say "1. a change with valid transferring evidence lands"
152
153before="$(tip)"
154git checkout --quiet -b c1
155echo "a better readme" > docs/readme.md
156echo "a new target" > deploy/second.txt
157git add -A
158git commit --quiet -m "improve the readme and add a target"
159attest_change c1 "" "" --check prose-format --command reachable=true
160request c1 main "$(git rev-parse c1)"
161integrate
162
163grep -q "^c1 -> refs/heads/main .*: land$" "$work/last.out" || die "c1 did not land"
164grep -q "check prose-format .*transferred" "$work/last.out" || die "prose-format did not transfer"
165grep -q "check reachable .*transferred (untouched)" "$work/last.out" || die "the effectful check did not transfer"
166grep -q '"event":"integration-succeeded"' "$work/last.out" || die "no integration-succeeded event"
167[ "$(tip)" != "$before" ] || die "the protected ref did not move"
168[ "$(tip)" = "$(git rev-parse c1)" ] || die "main is not at the change's head"
169git -C "$origin" rev-parse --verify --quiet refs/the-valley/integration-requests/main/c1 > /dev/null \
170 && die "the request ref outlived the landing"
171holds "main moved $(git rev-parse --short "$before") -> $(git -C "$origin" rev-parse --short refs/heads/main)"
172
173# The landed note carries both signatures, under one text, and verifies.
174# The integrator writes under its own key hash, so its ref never races the
175# contributor's over the same subject.
176landed="refs/the-valley/attestations/$(attest digest --repo "$repo" --rev c1 | cut -d: -f2)/$ihash"
177git -C "$origin" rev-parse --verify --quiet "$landed" > /dev/null \
178 || die "no attestation ref by the integrator for the landed tree"
179git -C "$origin" cat-file blob "$landed:prose-format/statement.note" > "$work/landed.note"
180siblings="$(grep -c '^— ' "$work/landed.note")"
181[ "$siblings" -eq 2 ] || die "the landed note carries $siblings signatures, not two"
182attest verify --note "$work/landed.note" --known-keys "$work/reader_keys" \
183 --schema "$SCHEMA_ATTESTATION" --repo "$repo" --rev c1 \
184 --signer "$contributor" --signer "$integrator_name" > "$work/landed.verify"
185grep -q 'matches the recorded subject digest' "$work/landed.verify" || die "the landed note does not bind to the tree"
186holds "the landed note carries contributor and integrator sibling signatures and verifies"
187
188# And the integrator's own transfer statement sits beside it.
189git -C "$origin" cat-file blob "$landed:transfer.note" > "$work/transfer.note"
190head -n1 "$work/transfer.note" | grep -qx 'the-valley/attestation/v1' || die "the transfer note is not a statement"
191grep -qx 'predicateType the-valley/integration/transfer/v1' "$work/transfer.note" || die "wrong predicate type"
192grep -qx 'predicate.change c1' "$work/transfer.note" || die "the transfer statement names no change"
193grep -q '^predicate.policy.sha256 ' "$work/transfer.note" || die "the transfer statement names no policy"
194attest verify --note "$work/transfer.note" --known-keys "$work/reader_keys" \
195 --schema "$SCHEMA_ATTESTATION" --signer "$integrator_name" > /dev/null
196holds "the transfer statement records the verdict and the policy it was derived under"
197
198# ----------------------------------------------------------------------
199say "2. an intervening landing invalidates one check's closure"
200
201git checkout --quiet main
202git pull --quiet --ff-only origin main
203
204# c2 is authored against the current tip and reads both classes.
205git checkout --quiet -b c2
206echo "a readme with more in it" > docs/readme.md
207echo "the source, revised" > src/main.txt
208git add -A
209git commit --quiet -m "revise the readme and the source"
210attest_change c2 "" "" --check prose-format --check code-build
211c2="$(git rev-parse c2)"
212
213# Meanwhile another change lands, touching only what code-build reads.
214git checkout --quiet main
215git checkout --quiet -b intervening
216echo "a second source file" > src/other.txt
217git add -A
218git commit --quiet -m "add a second source file"
219attest_change intervening "" "" --check code-build
220request intervening main "$(git rev-parse intervening)"
221integrate
222grep -q "^intervening -> refs/heads/main .*: land$" "$work/last.out" || die "the intervening change did not land"
223holds "the intervening change landed first; c2's base has moved"
224
225request c2 main "$c2"
226integrate
227grep -q "^c2 -> refs/heads/main .*: stale$" "$work/last.out" || die "c2 was not stale"
228grep -q "check prose-format .*transferred (closure)" "$work/last.out" \
229 || die "prose-format should have transferred by closure equality"
230grep -q "check code-build .*input closure changed" "$work/last.out" \
231 || die "code-build's closure should have moved"
232grep -q '"event":"request-stale"' "$work/last.out" || die "no request-stale event"
233grep -q '"checks":\["code-build"\]' "$work/last.out" || die "the event does not name exactly code-build"
234git -C "$origin" rev-parse --verify --quiet refs/the-valley/integration-requests/main/c2 > /dev/null \
235 || die "the request ref was dropped; there is nothing to resubmit"
236holds "one request-stale naming exactly code-build; prose-format reported transferred"
237forget c2
238
239# ----------------------------------------------------------------------
240say "2b. evidence that survives the rebase lands on the moved tip"
241
242# The same shape as 2, with the intervening landing outside what the
243# change's checks read. The tree that lands is not the tree that was
244# attested, and the evidence stands anyway — which is the whole of what
245# content-addressed evidence buys.
246git checkout --quiet main
247git pull --quiet --ff-only origin main
248git checkout --quiet -b c2b
249echo "a readme revised again" > docs/readme.md
250git add -A
251git commit --quiet -m "revise the readme once more"
252attest_change c2b "" "" --check prose-format
253c2b="$(git rev-parse c2b)"
254attested="$(attest digest --repo "$repo" --rev c2b | cut -d: -f2)"
255
256git checkout --quiet main
257git checkout --quiet -b elsewhere
258echo "a third source file" > src/third.txt
259git add -A
260git commit --quiet -m "add a third source file"
261attest_change elsewhere "" "" --check code-build
262request elsewhere main "$(git rev-parse elsewhere)"
263integrate
264grep -q "^elsewhere -> refs/heads/main .*: land$" "$work/last.out" || die "the intervening change did not land"
265
266request c2b main "$c2b"
267integrate
268grep -q "^c2b -> refs/heads/main .*: land$" "$work/last.out" || die "c2b did not land"
269grep -q "check prose-format .*transferred (closure)" "$work/last.out" \
270 || die "the evidence did not transfer by closure equality"
271landedtree="$(git -C "$origin" rev-parse refs/heads/main^{tree})"
272[ "$(git -C "$origin" rev-parse "refs/heads/main^")" = "$(git rev-parse elsewhere)" ] \
273 || die "the landing is not a fast-forward from the tip it was judged against"
274[ "$(git -C "$origin" rev-parse refs/heads/main^{tree})" != "$(git rev-parse "$c2b^{tree}")" ] \
275 || die "the tree did not actually move; the scenario proves nothing"
276holds "the delta landed on a tree it was never attested over, and no check re-ran"
277
278# Two attestation refs now, because the two statements are about two
279# different trees: the contributor's is keyed by the tree it was made over,
280# and the integrator's transfer statement by the tree that landed.
281git fetch --quiet origin main
282landed_digest="$(attest digest --repo "$repo" --rev "$(git -C "$origin" rev-parse refs/heads/main)" | cut -d: -f2)"
283[ "$landed_digest" != "$attested" ] || die "the landed tree is the attested tree; the scenario proves nothing"
284git -C "$origin" cat-file blob \
285 "refs/the-valley/attestations/$attested/$ihash:prose-format/statement.note" > "$work/rebased.note"
286[ "$(grep -c '^— ' "$work/rebased.note")" -eq 2 ] \
287 || die "the countersigned note lost a sibling signature"
288grep -qx "subject.digest.valley-tree-v1 $attested" "$work/rebased.note" \
289 || die "the countersigned statement is not about the tree it was made over"
290git -C "$origin" cat-file blob \
291 "refs/the-valley/attestations/$landed_digest/$ihash:transfer.note" > "$work/rebased.transfer"
292grep -qx "subject.digest.valley-tree-v1 $landed_digest" "$work/rebased.transfer" \
293 || die "the transfer statement is not about the landed tree"
294grep -qx "predicate.base.valley-tree-v1 $attested" "$work/rebased.transfer" \
295 || die "the transfer statement does not cite the base the evidence was produced over"
296grep -qx "predicate.checks.0.rule closure" "$work/rebased.transfer" \
297 || die "the transfer statement does not record the rule that carried the evidence"
298holds "the contributor's statement stays about its own tree; the transfer statement names the landed one"
299
300# ----------------------------------------------------------------------
301say "3. a conflicting delta is stale, and demands full re-attestation"
302
303git checkout --quiet main
304git pull --quiet --ff-only origin main
305git checkout --quiet -b c3
306echo "the readme, as c3 would have it" > docs/readme.md
307git add -A
308git commit --quiet -m "rewrite the readme one way"
309attest_change c3 "" "" --check prose-format
310c3="$(git rev-parse c3)"
311
312git checkout --quiet main
313git checkout --quiet -b conflicting
314echo "the readme, as the other change would have it" > docs/readme.md
315git add -A
316git commit --quiet -m "rewrite the readme another way"
317attest_change conflicting "" "" --check prose-format
318request conflicting main "$(git rev-parse conflicting)"
319integrate
320grep -q "^conflicting -> refs/heads/main .*: land$" "$work/last.out" || die "the conflicting change did not land"
321
322request c3 main "$c3"
323integrate
324grep -q "^c3 -> refs/heads/main .*: stale$" "$work/last.out" || die "c3 was not stale"
325grep -q 'resolving it is new authorship' "$work/last.out" || die "the conflict was not reported as new authorship"
326grep -q 'CONFLICT (content): Merge conflict in docs/readme.md' "$work/last.out" \
327 || die "the conflict does not name what could not be merged"
328grep -q '"reason":"conflict"' "$work/last.out" || die "the event does not say conflict"
329grep -q '"checks":\["prose-format"\]' "$work/last.out" || die "a conflict must re-demand every required check"
330holds "a conflict is stale, and every required check is re-demanded"
331forget c3
332
333# ----------------------------------------------------------------------
334say "4. an unknown signer's attestation does not transfer"
335
336git checkout --quiet main
337git pull --quiet --ff-only origin main
338git checkout --quiet -b c4
339echo "a readme signed by a stranger" > docs/readme.md
340git add -A
341git commit --quiet -m "a change a stranger attested"
342attest_change c4 "$work/stranger" "$stranger" --check prose-format
343request c4 main "$(git rev-parse c4)"
344integrate
345grep -q "^c4 -> refs/heads/main .*: stale$" "$work/last.out" || die "c4 was not stale"
346grep -q 'no signer the integrator holds' "$work/last.out" || die "the unknown signer was not reported"
347grep -q '"checks":\["prose-format"\]' "$work/last.out" || die "the event does not name prose-format"
348[ "$(tip)" = "$(git -C "$origin" rev-parse refs/heads/main)" ] || die "main moved"
349holds "evidence from a signer the integrator does not hold does not transfer"
350forget c4
351
352# ----------------------------------------------------------------------
353say "5. a tampered statement is rejected, not called stale"
354
355git checkout --quiet main
356git checkout --quiet -b c5
357echo "an honest readme" > docs/readme.md
358git add -A
359git commit --quiet -m "an honest change"
360attest_change c5 "" "" --check prose-format
361c5="$(git rev-parse c5)"
362c5digest="$(attest digest --repo "$repo" --rev c5 | cut -d: -f2)"
363
364# Edit one value in the signed text and store the result where the real
365# note was. Everything else about the note is untouched.
366c5ref="$(git -C "$origin" for-each-ref --format='%(refname)' \
367 "refs/the-valley/attestations/$c5digest" | head -n1)"
368git -C "$origin" cat-file blob "$c5ref:prose-format/statement.note" \
369 | sed 's/^predicate.result passed$/predicate.result failed/' > "$work/tampered.note"
370cmp -s "$work/tampered.note" <(git -C "$origin" cat-file blob "$c5ref:prose-format/statement.note") \
371 && die "the tamper changed nothing; the test proves nothing"
372blob="$(git -C "$origin" hash-object -w --stdin < "$work/tampered.note")"
373sub="$(printf '100644 blob %s\tstatement.note\n' "$blob" | git -C "$origin" mktree)"
374top="$(printf '040000 tree %s\tprose-format\n' "$sub" | git -C "$origin" mktree)"
375git -C "$origin" update-ref "$c5ref" "$top"
376
377request c5 main "$c5"
378integrate
379grep -q "^c5 -> refs/heads/main .*: reject$" "$work/last.out" || die "a tampered statement was not rejected"
380grep -q 'does not verify as the statement it claims to be' "$work/last.out" || die "the refusal was not reported"
381grep -q 'request-stale' "$work/last.out" && die "a forgery was announced as staleness"
382holds "a tampered statement is a rejection, and no outcome event claims otherwise"
383forget c5
384
385# ----------------------------------------------------------------------
386say "6. an attestation about a different tree is rejected"
387
388git checkout --quiet main
389git checkout --quiet -b c6
390echo "the tree that was actually attested" > docs/readme.md
391git add -A
392git commit --quiet -m "the attested change"
393attest_change c6 "" "" --check prose-format
394c6ref="$(git -C "$origin" for-each-ref --format='%(refname)' \
395 "refs/the-valley/attestations/$(attest digest --repo "$repo" --rev c6 | cut -d: -f2)" | head -n1)"
396
397# A second, different tree — with no attestation of its own — and the
398# first tree's valid, correctly signed note filed under its digest.
399git checkout --quiet -b c6-lifted
400echo "the tree that was not" > docs/readme.md
401git add -A
402git commit --quiet -m "the change the evidence was lifted onto"
403git push --quiet origin c6-lifted
404lifted="$(attest digest --repo "$repo" --rev c6-lifted | cut -d: -f2)"
405git -C "$origin" update-ref \
406 "refs/the-valley/attestations/$lifted/${c6ref##*/}" \
407 "$(git -C "$origin" rev-parse "$c6ref^{tree}")"
408
409request c6 main "$(git rev-parse c6-lifted)"
410integrate
411grep -q "^c6 -> refs/heads/main .*: reject$" "$work/last.out" || die "a lifted attestation was not rejected"
412grep -q 'tree digest mismatch' "$work/last.out" || die "the refusal does not name the tree digest"
413holds "a signature cannot be lifted onto a tree it was not made about"
414forget c6
415
416# ----------------------------------------------------------------------
417say "7. an attestation lying about its kind is rejected"
418
419# The policy runs prose-format with nix, so its evidence is content-
420# addressed. This attestation claims a notarization instead, and its class
421# was untouched by the intervening landing — so under the rule it nominated
422# for itself it would sail onto a base it was never produced over.
423git checkout --quiet main
424git pull --quiet --ff-only origin main
425git checkout --quiet -b c7
426echo "a readme attested as a notarization" > docs/readme.md
427git add -A
428git commit --quiet -m "a change whose attestation lies about its kind"
429attest_change c7 "" "" --command prose-format=true
430c7="$(git rev-parse c7)"
431
432git checkout --quiet main
433git checkout --quiet -b under-c7
434echo "a fourth source file" > src/fourth.txt
435git add -A
436git commit --quiet -m "add a fourth source file"
437attest_change under-c7 "" "" --check code-build
438request under-c7 main "$(git rev-parse under-c7)"
439integrate
440grep -q "^under-c7 -> refs/heads/main .*: land$" "$work/last.out" || die "the intervening change did not land"
441
442request c7 main "$c7"
443integrate
444grep -q "^c7 -> refs/heads/main .*: reject$" "$work/last.out" || die "a lie about the kind was not rejected"
445grep -q 'requires a pure check and the attestation claims an effectful check' "$work/last.out" \
446 || die "the refusal does not say the policy decides the rule"
447holds "the transfer rule follows the policy's runner, never the attestation's own claim"
448forget c7
449
450# ----------------------------------------------------------------------
451say "8. evidence pushed after a no-evidence verdict re-judges and lands"
452
453git checkout --quiet main
454git pull --quiet --ff-only origin main
455git checkout --quiet -b c8
456echo "a readme with no evidence yet" > docs/readme.md
457git add -A
458git commit --quiet -m "a change submitted before its checks ran"
459git push --quiet origin c8
460request c8 main "$(git rev-parse c8)"
461integrate
462grep -q "^c8 -> refs/heads/main .*: stale$" "$work/last.out" || die "a change with no evidence was not stale"
463grep -q 'no attestation for this check accompanies the change' "$work/last.out" \
464 || die "the absent attestation was not reported"
465
466# Nothing about the request or the tip moves; only the evidence arrives.
467attest_change c8 "" "" --check prose-format
468integrate
469grep -q "^c8 -> refs/heads/main .*: land$" "$work/last.out" \
470 || die "the request was not re-judged after its evidence arrived"
471[ "$(tip)" = "$(git rev-parse c8)" ] || die "main is not at the change's head"
472holds "a pass re-judges when evidence arrives, with the request and the tip unmoved"
473
474# ----------------------------------------------------------------------
475say "9. a ref that is not a request does not hold up one that is"
476
477git checkout --quiet main
478git pull --quiet --ff-only origin main
479git checkout --quiet -b c9
480echo "a readme beside a garbage ref" > docs/readme.md
481git add -A
482git commit --quiet -m "a change beside a malformed request"
483attest_change c9 "" "" --check prose-format
484# A name with no target segment at all, and one with a segment too many.
485# Anyone with push access can write this namespace.
486git push --quiet origin "$(git rev-parse c9):refs/the-valley/integration-requests/garbage"
487git push --quiet origin "$(git rev-parse c9):refs/the-valley/integration-requests/main/too/deep"
488request c9 main "$(git rev-parse c9)"
489integrate
490grep -q 'refs/the-valley/integration-requests/garbage: not a request ref' "$work/last.out" \
491 || die "the malformed ref was not reported"
492grep -q 'refs/the-valley/integration-requests/main/too/deep: not a request ref' "$work/last.out" \
493 || die "the over-deep ref was not reported"
494grep -q "^c9 -> refs/heads/main .*: land$" "$work/last.out" \
495 || die "a malformed ref stopped the pass reaching a valid request"
496holds "malformed refs are reported and skipped; the valid request beside them landed"
497git -C "$origin" update-ref -d refs/the-valley/integration-requests/garbage
498git -C "$origin" update-ref -d refs/the-valley/integration-requests/main/too/deep
499
500printf '\nintegrator-e2e: every scenario held\n'