gunk-dev/the-valley

attest-e2e.sh

source6a6791929c36main's published tip history
18.5 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 later run under the same key, for another check of the same tree,
160# adds its note to the ref the host holds. A check the ref already has
161# a note for keeps that note: the run says so and adds nothing for it.
162held="$(git -C "$TMPDIR/host.git" rev-parse "$ref")"
163attest run --key "$TMPDIR/host" --name "$host" --command 'ok=true' \
164 --command 'also=true' --push valley > added.out
165grep -q "^held ok: valley already holds" added.out
166grep -q "^added 1 of 2 notes to $ref" added.out
167git -C "$TMPDIR/host.git" cat-file blob "$held:ok/statement.note" > held.note
168for check in ok also; do
169 git -C "$TMPDIR/host.git" cat-file blob "$ref:$check/statement.note" > "published-$check.note"
170 verify "published-$check.note" "$TMPDIR/known_keys" > "published-$check.out"
171 grep -qx "predicate.check.name $check" "published-$check.note"
172done
173cmp held.note published-ok.note
174[ "$(git rev-parse "$ref")" = "$(git -C "$TMPDIR/host.git" rev-parse "$ref")" ]
175
176# A second party attests to the same statement, and its
177# signature lands beside the first under the text both cover.
178# This is what the note format is here for.
179attest sign --key "$TMPDIR/witness" --name "$witness" statement.note \
180 > countersigned.note
181if [ "$(grep -c '^— ' countersigned.note)" -ne 2 ]; then
182 echo "attest-e2e: countersigning did not add a sibling signature line" >&2
183 cat countersigned.note >&2
184 exit 1
185fi
186sed '/^$/q' countersigned.note | sed '$d' | cmp - <(sed '/^$/q' statement.note | sed '$d')
187attest verify --note countersigned.note --known-keys "$TMPDIR/known_keys" \
188 --repo "$repo" --signer "$host" --signer "$witness" > countersigned.out
189if [ "$(grep -c '^signature ok' countersigned.out)" -ne 2 ]; then
190 echo "attest-e2e: both signatures over one statement must verify" >&2
191 cat countersigned.out >&2
192 exit 1
193fi
194
195# A verifier that does not hold the witness's key still
196# accepts the note, and says which signature it cannot speak
197# to. Requiring that signer is how it refuses instead.
198attest key --key "$TMPDIR/host" --name "$host" > "$TMPDIR/host_only"
199attest verify --note countersigned.note --known-keys "$TMPDIR/host_only" \
200 --repo "$repo" > partial.out
201grep -q "^signature ok $host+" partial.out
202grep -q "^signature unknown $witness+" partial.out
203if attest verify --note countersigned.note --known-keys "$TMPDIR/host_only" \
204 --repo "$repo" --signer "$witness" > required.out 2>&1; then
205 echo "attest-e2e: a required signer with no verified signature must fail" >&2
206 exit 1
207fi
208grep -q "no signature on this note is by $witness" required.out
209
210# A note signed by a key the verifier does not hold is not a
211# bad signature — it is not a signer.
212if verify statement.note "$TMPDIR/other_keys" > stranger.out 2>&1; then
213 echo "attest-e2e: a note signed by nobody we hold a key for must not verify" >&2
214 exit 1
215fi
216grep -q 'no signature on this note is by a key the verifier holds' stranger.out
217
218# Tampering with the signed bytes: one byte of the text the
219# signature covers.
220sed "s/$digest/00000000000000000000000000000000000000000000000000000000000000ff/" \
221 statement.note > tampered.note
222if verify tampered.note "$TMPDIR/known_keys" > tampered.out 2>&1; then
223 echo "attest-e2e: a tampered statement must not verify" >&2
224 exit 1
225fi
226grep -q 'does not check out over this text' tampered.out
227
228# A signature cannot be lifted onto a statement it was not
229# made about: it sits under the text it covers, so moving it
230# means moving that text with it.
231sed '/^$/q' statement.note | sed '$d' > lifted.txt
232sed 's/^provenance.harness .*/provenance.harness somebody-else/' lifted.txt > lifted.body
233sed -n '/^$/,$p' statement.note | tail -n +2 > lifted.sigs
234{ cat lifted.body; echo; cat lifted.sigs; } > lifted.note
235if verify lifted.note "$TMPDIR/known_keys" > lifted.out 2>&1; then
236 echo "attest-e2e: a signature was lifted onto another statement" >&2
237 exit 1
238fi
239grep -q 'does not check out over this text' lifted.out
240
241# A statement written some other way than the one written
242# form, signed properly over those bytes. The signature is
243# good and the statement is still refused: it would verify
244# here and fail wherever it was written out again, so the
245# form is a rule rather than a convention.
246{ head -n 1 lifted.txt; tail -n +2 lifted.txt | sort -r; } > loose.txt
247cmp -s loose.txt lifted.txt \
248 && { echo "attest-e2e: the reordering changed nothing" >&2; exit 1; }
249attest sign --key "$TMPDIR/host" --name "$host" loose.txt > loose.note
250if verify loose.note "$TMPDIR/known_keys" > loose.out 2>&1; then
251 echo "attest-e2e: a statement not in the written form must not verify" >&2
252 exit 1
253fi
254grep -q 'not the written form of what it says' loose.out
255
256# The subject is the tree. Change the tree and the same
257# signed statement stops being about it …
258echo goodbye > hello.txt
259git commit --quiet -am "change the tree"
260if verify statement.note "$TMPDIR/known_keys" > moved.out 2>&1; then
261 echo "attest-e2e: a statement about another tree must not verify" >&2
262 exit 1
263fi
264grep -q 'tree digest mismatch' moved.out
265
266# … while rewriting the commit over the same tree leaves the
267# attestation standing, which is what a subject that is a
268# tree digest buys.
269git commit --quiet --amend -m "same tree, new commit"
270rewritten="$(attest digest | sed 's/^valley-tree-v1://')"
271git revert --quiet --no-edit HEAD
272restored="$(attest digest | sed 's/^valley-tree-v1://')"
273if [ "$restored" != "$digest" ]; then
274 echo "attest-e2e: restoring the tree did not restore its digest" >&2
275 echo " original $digest" >&2
276 echo " restored $restored" >&2
277 exit 1
278fi
279if [ "$rewritten" = "$digest" ]; then
280 echo "attest-e2e: a changed tree kept its digest" >&2
281 exit 1
282fi
283verify statement.note "$TMPDIR/known_keys" > rebased.out
284grep -q 'matches the recorded subject digest' rebased.out
285
286# A replacement ref in the repository changes nothing a standalone
287# verify reads. refs/replace/<commit> makes git read another commit
288# wherever it looks that one up, so a reader that left replacement
289# refs on would digest the attested tree for a revision holding a
290# different one, and pass it. The caller's environment asking for
291# replacement refs is not listened to either.
292attested="$(git rev-parse HEAD)"
293echo "a change nobody attested" > evil.txt
294git add evil.txt
295git commit --quiet -m "a change nobody attested"
296evil="$(git rev-parse HEAD)"
297git update-ref "refs/replace/$evil" "$attested"
298if [ "$(git rev-parse "$evil^{tree}")" != "$(git rev-parse "$attested^{tree}")" ]; then
299 echo "attest-e2e: the replacement did not take, so this case proves nothing" >&2
300 exit 1
301fi
302if GIT_CONFIG_PARAMETERS="'core.usereplacerefs'='true'" attest verify --note statement.note \
303 --known-keys "$TMPDIR/known_keys" --repo "$repo" --rev "$evil" > replaced.out 2>&1; then
304 echo "attest-e2e: a replacement ref made an unattested revision verify" >&2
305 cat replaced.out >&2
306 exit 1
307fi
308if grep -q 'matches the recorded subject digest' replaced.out; then
309 echo "attest-e2e: the digest of a replaced revision matched the attested one" >&2
310 exit 1
311fi
312[ "$(attest digest --rev "$evil" | sed 's/^valley-tree-v1://')" != "$digest" ] || {
313 echo "attest-e2e: attest digested the replacement, not the revision" >&2
314 exit 1
315}
316git update-ref -d "refs/replace/$evil"
317
318# ----------------------------------------------------------------------
319# Pure checks, through integrator-e2e's stand-in for nix, which logs every
320# call here. One shared evaluation names every check, and each check is
321# built by a nix build of its own, whose result is that check's. A check
322# that declares a failure (checks/<name>.fails) fails with what it
323# declares. Two checks can share an output, as fixed-output derivations
324# can (checks/<name>.output): one whose own build failed stays failed when
325# the other builds that output. A run with any check that failed or did
326# not evaluate publishes nothing, neither here nor on the remote.
327cp -r "$nixStandIn" "$TMPDIR/stand-ins"
328chmod -R +w "$TMPDIR/stand-ins"
329patchShebangs "$TMPDIR/stand-ins" > /dev/null
330nixlog="$TMPDIR/nix.log"
331mkdir -p "$TMPDIR/nix-bin"
332for tool in nix nix-store; do
333 cat > "$TMPDIR/nix-bin/$tool" <<EOF
334#!$(command -v bash)
335printf '%s %s\n' "$tool" "\$*" >> "$nixlog"
336exec "$TMPDIR/stand-ins/$tool" "\$@"
337EOF
338 chmod +x "$TMPDIR/nix-bin/$tool"
339done
340export PATH="$TMPDIR/nix-bin:$PATH" VALLEY_FAKE_STORE="$TMPDIR/store"
341export XDG_STATE_HOME="$TMPDIR/state"
342
343pure="$TMPDIR/pure"
344remote="$TMPDIR/pure-remote.git"
345git init --quiet "$pure"
346git init --quiet --bare "$remote"
347cd "$pure" || exit 1
348mkdir -p src checks
349echo source > src/main.txt
350for check in one two broken maker loser; do echo src > "checks/$check.inputs"; done
351echo "the broken check says why" > checks/broken.fails
352echo shared > checks/maker.output
353echo shared > checks/loser.output
354echo "the loser's own build failed" > checks/loser.fails
355git add -A
356git commit --quiet -m "pure checks"
357nothing_on_remote() {
358 if [ -n "$(git --git-dir="$remote" for-each-ref)" ]; then
359 echo "attest-e2e: $1" >&2
360 git --git-dir="$remote" for-each-ref >&2
361 exit 1
362 fi
363}
364pure_run() {
365 : > "$nixlog"
366 attest run --key "$TMPDIR/host" --name "$host" --push "$remote" --branch main "$@"
367}
368
369# Two checks pass and one fails: each keeps its own result, the failing
370# one keeps its own log under its own name, and nothing is published.
371if pure_run --timings --check one --check broken --check two > mixed.out 2> mixed.err; then
372 echo "attest-e2e: a run with a failing pure check succeeded" >&2
373 exit 1
374fi
375grep -qE '^check one +nix +passed$' mixed.out
376grep -qE '^check broken +nix +failed$' mixed.out
377grep -qE '^check two +nix +passed$' mixed.out
378[ "$(grep -c '^--- ' mixed.err)" = 1 ]
379sed -n '/^--- broken ---$/,$p' mixed.err > broken.log
380grep -qx 'the broken check says why' broken.log
381grep -q '1 of 3 checks failed; nothing published' mixed.err
382nothing_published "a run with a failing pure check stored a ref"
383nothing_on_remote "a run with a failing pure check pushed"
384# One evaluation named all three checks, and each had a build of its own.
385[ "$(grep -c '^nix eval ' "$nixlog")" = 1 ]
386grep '^nix eval --json --apply ' "$nixlog" > shared-eval.log
387for check in one broken two; do
388 grep -qF "\"$check\"" shared-eval.log
389 [ "$(grep -cE "^nix build .*/$check-[A-Za-z0-9]{6}\.drv\^\*\$" "$nixlog")" = 1 ]
390done
391[ "$(grep -c '^nix build ' "$nixlog")" = 3 ]
392# Each check is logged with its evaluation time.
393[ "$(grep -c '"runner":"nix".*"eval_ms":' "$XDG_STATE_HOME/valley/timings.jsonl")" = 3 ]
394
395# A check whose own build failed stays failed, though the other check that
396# shares its output built that output.
397if pure_run --check loser --check maker > shared.out 2> shared.err; then
398 echo "attest-e2e: a check whose build failed passed on another's output" >&2
399 exit 1
400fi
401grep -qE '^check loser +nix +failed$' shared.out
402grep -qE '^check maker +nix +passed$' shared.out
403[ -f "$VALLEY_FAKE_STORE/shared.out/result" ]
404sed -n '/^--- loser ---$/,$p' shared.err > loser.log
405grep -qx "the loser's own build failed" loser.log
406grep -q '1 of 2 checks failed; nothing published' shared.err
407nothing_published "a run with a failed build behind a shared output stored a ref"
408nothing_on_remote "a run with a failed build behind a shared output pushed"
409
410# A check that does not evaluate is an error for that check alone: the
411# shared evaluation names it as nothing, it is evaluated alone for its own
412# error, and the other check is still built and reported.
413if pure_run --check one --check absent > absent.out 2> absent.err; then
414 echo "attest-e2e: a run with a check that does not evaluate succeeded" >&2
415 exit 1
416fi
417grep -qE '^check one +nix +passed$' absent.out
418grep -qE '^time absent +error ' absent.out
419grep -q 'declares no check absent' absent.err
420grep -qE '^nix build .*/one-[A-Za-z0-9]{6}\.drv\^\*$' "$nixlog"
421nothing_published "a run with a check that does not evaluate stored a ref"
422nothing_on_remote "a run with a check that does not evaluate pushed"
423
424# When every check passes, the run publishes the branch and one note per
425# check, and each note verifies.
426pure_run --check one --check two --check maker > passing.out 2> passing.err || {
427 echo "attest-e2e: a run whose pure checks pass failed" >&2
428 cat passing.out passing.err >&2
429 exit 1
430}
431puredigest="$(attest digest | sed 's/^valley-tree-v1://')"
432pureref="$(git --git-dir="$remote" for-each-ref --format='%(refname)' "refs/the-valley/attestations/$puredigest")"
433[ -n "$pureref" ]
434[ "$(git --git-dir="$remote" rev-parse refs/heads/main)" = "$(git rev-parse HEAD)" ]
435for check in one two maker; do
436 git --git-dir="$remote" cat-file blob "$pureref:$check/statement.note" > "$check.note"
437 attest verify --note "$check.note" --known-keys "$TMPDIR/known_keys" --repo "$pure" > "$check.verify"
438 grep -q 'matches the recorded subject digest' "$check.verify"
439done
440touch "$out"