gunk-dev/the-valley

attest-e2e.sh

source23915b53d9camain's published tip history
17.6 KiB
1# shellcheck shell=bash
2# Paths and fixtures come from the derivation environment.
3# shellcheck disable=SC2154
4export HOME="$TMPDIR"
5export GIT_CONFIG_NOSYSTEM=1
6git config --global user.name valley-check
7git config --global user.email valley-check@localhost
8git config --global init.defaultBranch main
9
10# Throwaway keys in a scratch directory. The signer is a
11# parameter: provisioning a host identity is deployment, and
12# nothing here reads or writes anything under a real ~/.ssh.
13# A verifier is given key names and hashes, so what it holds
14# is what `attest key` prints and nothing more.
15ssh-keygen -q -t ed25519 -N "" -C valley-check -f "$TMPDIR/host"
16ssh-keygen -q -t ed25519 -N "" -C witness -f "$TMPDIR/witness"
17ssh-keygen -q -t ed25519 -N "" -C stranger -f "$TMPDIR/stranger"
18host=host.valley.invalid/attestations
19witness=witness.valley.invalid/attestations
20stranger=stranger.elsewhere.invalid/attestations
21attest key --key "$TMPDIR/host" --name "$host" > "$TMPDIR/known_keys"
22attest key --key "$TMPDIR/witness" --name "$witness" >> "$TMPDIR/known_keys"
23attest key --key "$TMPDIR/stranger" --name "$stranger" > "$TMPDIR/other_keys"
24grep -q "^$host+[0-9a-f]\{8\}+A" "$TMPDIR/known_keys"
25
26# A key behind a passphrase is refused rather than prompted
27# for: attest runs unattended.
28ssh-keygen -q -t ed25519 -N "a passphrase" -C locked -f "$TMPDIR/locked"
29if attest key --key "$TMPDIR/locked" --name "$host" 2> locked.err; then
30 echo "attest-e2e: an encrypted key must be refused" >&2
31 exit 1
32fi
33grep -q 'cannot ask for a passphrase' locked.err
34
35repo="$TMPDIR/tree"
36git init --quiet "$repo"
37cd "$repo" || exit 1
38echo hello > hello.txt
39mkdir -p sub
40echo nested > sub/nested.txt
41ln -s hello.txt link
42printf '#!/bin/sh\ntrue\n' > run.sh
43chmod 755 run.sh
44git add -A
45git commit --quiet -m base
46
47digest="$(attest digest | sed 's/^valley-tree-v1://')"
48case "$digest" in
49 [0-9a-f][0-9a-f]*) ;;
50 *) echo "attest-e2e: no tree digest: $digest" >&2; exit 1 ;;
51esac
52
53refs() { git for-each-ref --format='%(refname)' refs/the-valley; }
54nothing_published() {
55 if [ -n "$(refs)" ]; then
56 echo "attest-e2e: $1" >&2
57 git for-each-ref refs/the-valley >&2
58 exit 1
59 fi
60}
61
62# Without a key, nothing is emitted — an unsigned statement
63# is not an attestation.
64if attest run --command 'ok=true' 2> nokey.err; then
65 echo "attest-e2e: a run with no signing key must fail" >&2
66 exit 1
67fi
68grep -q 'no signing key' nokey.err
69nothing_published "a run with no signing key stored a ref"
70
71# A failing check publishes nothing, and says which check.
72if attest run --key "$TMPDIR/host" --name "$host" \
73 --command 'ok=true' --command 'nope=false' > failing.out 2> failing.err; then
74 echo "attest-e2e: a failing check must fail the run" >&2
75 exit 1
76fi
77grep -qE '^check nope +command failed$' failing.out
78grep -q 'nothing published' failing.err
79nothing_published "a failing check left a ref behind"
80
81# A statement the schema rejects is stopped before it is
82# signed or stored. Provenance is the caller's, so a
83# malformed one is how a malformed statement gets composed
84# at all.
85echo '{"harness":"valley-check","authority":"root"}' > bad-provenance.json
86if attest run --key "$TMPDIR/host" --name "$host" --command 'ok=true' \
87 --provenance bad-provenance.json > malformed.out 2> malformed.err; then
88 echo "attest-e2e: a malformed statement must fail the run" >&2
89 exit 1
90fi
91grep -q 'provenance.authority: field not allowed' malformed.err
92grep -q 'nothing published' malformed.err
93nothing_published "a malformed statement left a ref behind"
94
95# A statement the schema admits but no line can carry is
96# stopped too, at the same point and for the same reason.
97printf '{"harness":"valley\\ncheck"}\n' > unwritable-provenance.json
98if attest run --key "$TMPDIR/host" --name "$host" --command 'ok=true' \
99 --provenance unwritable-provenance.json > unwritable.out 2> unwritable.err; then
100 echo "attest-e2e: a statement with no written form must fail the run" >&2
101 exit 1
102fi
103grep -q 'nothing published' unwritable.err
104nothing_published "a statement with no written form left a ref behind"
105
106# The successful run.
107echo '{"harness":"valley-check","model":"none","delegation":[{"principal":"human:integrator","grant":"land changes"}]}' > provenance.json
108attest run --key "$TMPDIR/host" --name "$host" --command 'ok=true' \
109 --provenance provenance.json > run.out
110grep -q "^subject valley-tree-v1:$digest\$" run.out
111grep -q '^check ok *command passed$' run.out
112grep -qE '^time ok +passed +[0-9][0-9.hms]* \(ran\)$' run.out
113grep -q "^signer $host+" run.out
114# A timing is printed, and logged only when --timings asks.
115if [ -e "$HOME/.local/state/valley/timings.jsonl" ]; then
116 echo "attest-e2e: a run without --timings wrote the timing log" >&2
117 exit 1
118fi
119
120# Stored at a ref keyed by the subject digest, as one note
121# rather than a statement and a signature beside it.
122ref="$(refs)"
123case "$ref" in
124 refs/the-valley/attestations/"$digest"/*) ;;
125 *) echo "attest-e2e: ref $ref is not keyed by the subject digest" >&2; exit 1 ;;
126esac
127git cat-file -p "$ref:ok/statement.note" > statement.note
128head -n 1 statement.note | grep -qx 'the-valley/attestation/v1'
129grep -qx "subject.digest.valley-tree-v1 $digest" statement.note
130grep -qx 'provenance.harness valley-check' statement.note
131grep -q "^— $host " statement.note
132
133# No git object is signed: the signature is over the
134# statement's text and nothing else.
135if git cat-file commit HEAD | grep -q '^gpgsig'; then
136 echo "attest-e2e: attest signed a git object" >&2
137 exit 1
138fi
139
140verify() {
141 attest verify --note "$1" --known-keys "$2" --repo "$repo"
142}
143
144verify statement.note "$TMPDIR/known_keys" > verify.out
145grep -q "^signature ok $host+" verify.out
146grep -q 'matches the recorded subject digest' verify.out
147
148# One atomic native-git push carries the branch and the
149# attestation ref together.
150git init --quiet --bare "$TMPDIR/host.git"
151git remote add valley "$TMPDIR/host.git"
152attest run --key "$TMPDIR/host" --name "$host" --command 'ok=true' \
153 --push valley > push.out
154grep -q -- '--atomic' push.out
155git -C "$TMPDIR/host.git" for-each-ref --format='%(refname)' > published.txt
156grep -qx 'refs/heads/main' published.txt
157grep -qx "$ref" published.txt
158
159# A second party attests to the same statement, and its
160# signature lands beside the first under the text both cover.
161# This is what the note format is here for.
162attest sign --key "$TMPDIR/witness" --name "$witness" statement.note \
163 > countersigned.note
164if [ "$(grep -c '^— ' countersigned.note)" -ne 2 ]; then
165 echo "attest-e2e: countersigning did not add a sibling signature line" >&2
166 cat countersigned.note >&2
167 exit 1
168fi
169sed '/^$/q' countersigned.note | sed '$d' | cmp - <(sed '/^$/q' statement.note | sed '$d')
170attest verify --note countersigned.note --known-keys "$TMPDIR/known_keys" \
171 --repo "$repo" --signer "$host" --signer "$witness" > countersigned.out
172if [ "$(grep -c '^signature ok' countersigned.out)" -ne 2 ]; then
173 echo "attest-e2e: both signatures over one statement must verify" >&2
174 cat countersigned.out >&2
175 exit 1
176fi
177
178# A verifier that does not hold the witness's key still
179# accepts the note, and says which signature it cannot speak
180# to. Requiring that signer is how it refuses instead.
181attest key --key "$TMPDIR/host" --name "$host" > "$TMPDIR/host_only"
182attest verify --note countersigned.note --known-keys "$TMPDIR/host_only" \
183 --repo "$repo" > partial.out
184grep -q "^signature ok $host+" partial.out
185grep -q "^signature unknown $witness+" partial.out
186if attest verify --note countersigned.note --known-keys "$TMPDIR/host_only" \
187 --repo "$repo" --signer "$witness" > required.out 2>&1; then
188 echo "attest-e2e: a required signer with no verified signature must fail" >&2
189 exit 1
190fi
191grep -q "no signature on this note is by $witness" required.out
192
193# A note signed by a key the verifier does not hold is not a
194# bad signature — it is not a signer.
195if verify statement.note "$TMPDIR/other_keys" > stranger.out 2>&1; then
196 echo "attest-e2e: a note signed by nobody we hold a key for must not verify" >&2
197 exit 1
198fi
199grep -q 'no signature on this note is by a key the verifier holds' stranger.out
200
201# Tampering with the signed bytes: one byte of the text the
202# signature covers.
203sed "s/$digest/00000000000000000000000000000000000000000000000000000000000000ff/" \
204 statement.note > tampered.note
205if verify tampered.note "$TMPDIR/known_keys" > tampered.out 2>&1; then
206 echo "attest-e2e: a tampered statement must not verify" >&2
207 exit 1
208fi
209grep -q 'does not check out over this text' tampered.out
210
211# A signature cannot be lifted onto a statement it was not
212# made about: it sits under the text it covers, so moving it
213# means moving that text with it.
214sed '/^$/q' statement.note | sed '$d' > lifted.txt
215sed 's/^provenance.harness .*/provenance.harness somebody-else/' lifted.txt > lifted.body
216sed -n '/^$/,$p' statement.note | tail -n +2 > lifted.sigs
217{ cat lifted.body; echo; cat lifted.sigs; } > lifted.note
218if verify lifted.note "$TMPDIR/known_keys" > lifted.out 2>&1; then
219 echo "attest-e2e: a signature was lifted onto another statement" >&2
220 exit 1
221fi
222grep -q 'does not check out over this text' lifted.out
223
224# A statement written some other way than the one written
225# form, signed properly over those bytes. The signature is
226# good and the statement is still refused: it would verify
227# here and fail wherever it was written out again, so the
228# form is a rule rather than a convention.
229{ head -n 1 lifted.txt; tail -n +2 lifted.txt | sort -r; } > loose.txt
230cmp -s loose.txt lifted.txt \
231 && { echo "attest-e2e: the reordering changed nothing" >&2; exit 1; }
232attest sign --key "$TMPDIR/host" --name "$host" loose.txt > loose.note
233if verify loose.note "$TMPDIR/known_keys" > loose.out 2>&1; then
234 echo "attest-e2e: a statement not in the written form must not verify" >&2
235 exit 1
236fi
237grep -q 'not the written form of what it says' loose.out
238
239# The subject is the tree. Change the tree and the same
240# signed statement stops being about it …
241echo goodbye > hello.txt
242git commit --quiet -am "change the tree"
243if verify statement.note "$TMPDIR/known_keys" > moved.out 2>&1; then
244 echo "attest-e2e: a statement about another tree must not verify" >&2
245 exit 1
246fi
247grep -q 'tree digest mismatch' moved.out
248
249# … while rewriting the commit over the same tree leaves the
250# attestation standing, which is what a subject that is a
251# tree digest buys.
252git commit --quiet --amend -m "same tree, new commit"
253rewritten="$(attest digest | sed 's/^valley-tree-v1://')"
254git revert --quiet --no-edit HEAD
255restored="$(attest digest | sed 's/^valley-tree-v1://')"
256if [ "$restored" != "$digest" ]; then
257 echo "attest-e2e: restoring the tree did not restore its digest" >&2
258 echo " original $digest" >&2
259 echo " restored $restored" >&2
260 exit 1
261fi
262if [ "$rewritten" = "$digest" ]; then
263 echo "attest-e2e: a changed tree kept its digest" >&2
264 exit 1
265fi
266verify statement.note "$TMPDIR/known_keys" > rebased.out
267grep -q 'matches the recorded subject digest' rebased.out
268
269# A replacement ref in the repository changes nothing a standalone
270# verify reads. refs/replace/<commit> makes git read another commit
271# wherever it looks that one up, so a reader that left replacement
272# refs on would digest the attested tree for a revision holding a
273# different one, and pass it. The caller's environment asking for
274# replacement refs is not listened to either.
275attested="$(git rev-parse HEAD)"
276echo "a change nobody attested" > evil.txt
277git add evil.txt
278git commit --quiet -m "a change nobody attested"
279evil="$(git rev-parse HEAD)"
280git update-ref "refs/replace/$evil" "$attested"
281if [ "$(git rev-parse "$evil^{tree}")" != "$(git rev-parse "$attested^{tree}")" ]; then
282 echo "attest-e2e: the replacement did not take, so this case proves nothing" >&2
283 exit 1
284fi
285if GIT_CONFIG_PARAMETERS="'core.usereplacerefs'='true'" attest verify --note statement.note \
286 --known-keys "$TMPDIR/known_keys" --repo "$repo" --rev "$evil" > replaced.out 2>&1; then
287 echo "attest-e2e: a replacement ref made an unattested revision verify" >&2
288 cat replaced.out >&2
289 exit 1
290fi
291if grep -q 'matches the recorded subject digest' replaced.out; then
292 echo "attest-e2e: the digest of a replaced revision matched the attested one" >&2
293 exit 1
294fi
295[ "$(attest digest --rev "$evil" | sed 's/^valley-tree-v1://')" != "$digest" ] || {
296 echo "attest-e2e: attest digested the replacement, not the revision" >&2
297 exit 1
298}
299git update-ref -d "refs/replace/$evil"
300
301# ----------------------------------------------------------------------
302# Pure checks, through integrator-e2e's stand-in for nix, which logs every
303# call here. One shared evaluation names every check, and each check is
304# built by a nix build of its own, whose result is that check's. A check
305# that declares a failure (checks/<name>.fails) fails with what it
306# declares. Two checks can share an output, as fixed-output derivations
307# can (checks/<name>.output): one whose own build failed stays failed when
308# the other builds that output. A run with any check that failed or did
309# not evaluate publishes nothing, neither here nor on the remote.
310cp -r "$nixStandIn" "$TMPDIR/stand-ins"
311chmod -R +w "$TMPDIR/stand-ins"
312patchShebangs "$TMPDIR/stand-ins" > /dev/null
313nixlog="$TMPDIR/nix.log"
314mkdir -p "$TMPDIR/nix-bin"
315for tool in nix nix-store; do
316 cat > "$TMPDIR/nix-bin/$tool" <<EOF
317#!$(command -v bash)
318printf '%s %s\n' "$tool" "\$*" >> "$nixlog"
319exec "$TMPDIR/stand-ins/$tool" "\$@"
320EOF
321 chmod +x "$TMPDIR/nix-bin/$tool"
322done
323export PATH="$TMPDIR/nix-bin:$PATH" VALLEY_FAKE_STORE="$TMPDIR/store"
324export XDG_STATE_HOME="$TMPDIR/state"
325
326pure="$TMPDIR/pure"
327remote="$TMPDIR/pure-remote.git"
328git init --quiet "$pure"
329git init --quiet --bare "$remote"
330cd "$pure" || exit 1
331mkdir -p src checks
332echo source > src/main.txt
333for check in one two broken maker loser; do echo src > "checks/$check.inputs"; done
334echo "the broken check says why" > checks/broken.fails
335echo shared > checks/maker.output
336echo shared > checks/loser.output
337echo "the loser's own build failed" > checks/loser.fails
338git add -A
339git commit --quiet -m "pure checks"
340nothing_on_remote() {
341 if [ -n "$(git --git-dir="$remote" for-each-ref)" ]; then
342 echo "attest-e2e: $1" >&2
343 git --git-dir="$remote" for-each-ref >&2
344 exit 1
345 fi
346}
347pure_run() {
348 : > "$nixlog"
349 attest run --key "$TMPDIR/host" --name "$host" --push "$remote" --branch main "$@"
350}
351
352# Two checks pass and one fails: each keeps its own result, the failing
353# one keeps its own log under its own name, and nothing is published.
354if pure_run --timings --check one --check broken --check two > mixed.out 2> mixed.err; then
355 echo "attest-e2e: a run with a failing pure check succeeded" >&2
356 exit 1
357fi
358grep -qE '^check one +nix +passed$' mixed.out
359grep -qE '^check broken +nix +failed$' mixed.out
360grep -qE '^check two +nix +passed$' mixed.out
361[ "$(grep -c '^--- ' mixed.err)" = 1 ]
362sed -n '/^--- broken ---$/,$p' mixed.err > broken.log
363grep -qx 'the broken check says why' broken.log
364grep -q '1 of 3 checks failed; nothing published' mixed.err
365nothing_published "a run with a failing pure check stored a ref"
366nothing_on_remote "a run with a failing pure check pushed"
367# One evaluation named all three checks, and each had a build of its own.
368[ "$(grep -c '^nix eval ' "$nixlog")" = 1 ]
369grep '^nix eval --json --apply ' "$nixlog" > shared-eval.log
370for check in one broken two; do
371 grep -qF "\"$check\"" shared-eval.log
372 [ "$(grep -cE "^nix build .*/$check-[A-Za-z0-9]{6}\.drv\^\*\$" "$nixlog")" = 1 ]
373done
374[ "$(grep -c '^nix build ' "$nixlog")" = 3 ]
375# Each check is logged with its evaluation time.
376[ "$(grep -c '"runner":"nix".*"eval_ms":' "$XDG_STATE_HOME/valley/timings.jsonl")" = 3 ]
377
378# A check whose own build failed stays failed, though the other check that
379# shares its output built that output.
380if pure_run --check loser --check maker > shared.out 2> shared.err; then
381 echo "attest-e2e: a check whose build failed passed on another's output" >&2
382 exit 1
383fi
384grep -qE '^check loser +nix +failed$' shared.out
385grep -qE '^check maker +nix +passed$' shared.out
386[ -f "$VALLEY_FAKE_STORE/shared.out/result" ]
387sed -n '/^--- loser ---$/,$p' shared.err > loser.log
388grep -qx "the loser's own build failed" loser.log
389grep -q '1 of 2 checks failed; nothing published' shared.err
390nothing_published "a run with a failed build behind a shared output stored a ref"
391nothing_on_remote "a run with a failed build behind a shared output pushed"
392
393# A check that does not evaluate is an error for that check alone: the
394# shared evaluation names it as nothing, it is evaluated alone for its own
395# error, and the other check is still built and reported.
396if pure_run --check one --check absent > absent.out 2> absent.err; then
397 echo "attest-e2e: a run with a check that does not evaluate succeeded" >&2
398 exit 1
399fi
400grep -qE '^check one +nix +passed$' absent.out
401grep -qE '^time absent +error ' absent.out
402grep -q 'declares no check absent' absent.err
403grep -qE '^nix build .*/one-[A-Za-z0-9]{6}\.drv\^\*$' "$nixlog"
404nothing_published "a run with a check that does not evaluate stored a ref"
405nothing_on_remote "a run with a check that does not evaluate pushed"
406
407# When every check passes, the run publishes the branch and one note per
408# check, and each note verifies.
409pure_run --check one --check two --check maker > passing.out 2> passing.err || {
410 echo "attest-e2e: a run whose pure checks pass failed" >&2
411 cat passing.out passing.err >&2
412 exit 1
413}
414puredigest="$(attest digest | sed 's/^valley-tree-v1://')"
415pureref="$(git --git-dir="$remote" for-each-ref --format='%(refname)' "refs/the-valley/attestations/$puredigest")"
416[ -n "$pureref" ]
417[ "$(git --git-dir="$remote" rev-parse refs/heads/main)" = "$(git rev-parse HEAD)" ]
418for check in one two maker; do
419 git --git-dir="$remote" cat-file blob "$pureref:$check/statement.note" > "$check.note"
420 attest verify --note "$check.note" --known-keys "$TMPDIR/known_keys" --repo "$pure" > "$check.verify"
421 grep -q 'matches the recorded subject digest' "$check.verify"
422done
423touch "$out"