gunk-dev/the-valley

attest-e2e.sh

source0a94c1215767main's published tip history
11.9 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 -q "^signer $host+" run.out
113
114# Stored at a ref keyed by the subject digest, as one note
115# rather than a statement and a signature beside it.
116ref="$(refs)"
117case "$ref" in
118 refs/the-valley/attestations/"$digest"/*) ;;
119 *) echo "attest-e2e: ref $ref is not keyed by the subject digest" >&2; exit 1 ;;
120esac
121git cat-file -p "$ref:ok/statement.note" > statement.note
122head -n 1 statement.note | grep -qx 'the-valley/attestation/v1'
123grep -qx "subject.digest.valley-tree-v1 $digest" statement.note
124grep -qx 'provenance.harness valley-check' statement.note
125grep -q "^— $host " statement.note
126
127# No git object is signed: the signature is over the
128# statement's text and nothing else.
129if git cat-file commit HEAD | grep -q '^gpgsig'; then
130 echo "attest-e2e: attest signed a git object" >&2
131 exit 1
132fi
133
134verify() {
135 attest verify --note "$1" --known-keys "$2" --repo "$repo"
136}
137
138verify statement.note "$TMPDIR/known_keys" > verify.out
139grep -q "^signature ok $host+" verify.out
140grep -q 'matches the recorded subject digest' verify.out
141
142# One atomic native-git push carries the branch and the
143# attestation ref together.
144git init --quiet --bare "$TMPDIR/host.git"
145git remote add valley "$TMPDIR/host.git"
146attest run --key "$TMPDIR/host" --name "$host" --command 'ok=true' \
147 --push valley > push.out
148grep -q -- '--atomic' push.out
149git -C "$TMPDIR/host.git" for-each-ref --format='%(refname)' > published.txt
150grep -qx 'refs/heads/main' published.txt
151grep -qx "$ref" published.txt
152
153# A second party attests to the same statement, and its
154# signature lands beside the first under the text both cover.
155# This is what the note format is here for.
156attest sign --key "$TMPDIR/witness" --name "$witness" statement.note \
157 > countersigned.note
158if [ "$(grep -c '^— ' countersigned.note)" -ne 2 ]; then
159 echo "attest-e2e: countersigning did not add a sibling signature line" >&2
160 cat countersigned.note >&2
161 exit 1
162fi
163sed '/^$/q' countersigned.note | sed '$d' | cmp - <(sed '/^$/q' statement.note | sed '$d')
164attest verify --note countersigned.note --known-keys "$TMPDIR/known_keys" \
165 --repo "$repo" --signer "$host" --signer "$witness" > countersigned.out
166if [ "$(grep -c '^signature ok' countersigned.out)" -ne 2 ]; then
167 echo "attest-e2e: both signatures over one statement must verify" >&2
168 cat countersigned.out >&2
169 exit 1
170fi
171
172# A verifier that does not hold the witness's key still
173# accepts the note, and says which signature it cannot speak
174# to. Requiring that signer is how it refuses instead.
175attest key --key "$TMPDIR/host" --name "$host" > "$TMPDIR/host_only"
176attest verify --note countersigned.note --known-keys "$TMPDIR/host_only" \
177 --repo "$repo" > partial.out
178grep -q "^signature ok $host+" partial.out
179grep -q "^signature unknown $witness+" partial.out
180if attest verify --note countersigned.note --known-keys "$TMPDIR/host_only" \
181 --repo "$repo" --signer "$witness" > required.out 2>&1; then
182 echo "attest-e2e: a required signer with no verified signature must fail" >&2
183 exit 1
184fi
185grep -q "no signature on this note is by $witness" required.out
186
187# A note signed by a key the verifier does not hold is not a
188# bad signature — it is not a signer.
189if verify statement.note "$TMPDIR/other_keys" > stranger.out 2>&1; then
190 echo "attest-e2e: a note signed by nobody we hold a key for must not verify" >&2
191 exit 1
192fi
193grep -q 'no signature on this note is by a key the verifier holds' stranger.out
194
195# Tampering with the signed bytes: one byte of the text the
196# signature covers.
197sed "s/$digest/00000000000000000000000000000000000000000000000000000000000000ff/" \
198 statement.note > tampered.note
199if verify tampered.note "$TMPDIR/known_keys" > tampered.out 2>&1; then
200 echo "attest-e2e: a tampered statement must not verify" >&2
201 exit 1
202fi
203grep -q 'does not check out over this text' tampered.out
204
205# A signature cannot be lifted onto a statement it was not
206# made about: it sits under the text it covers, so moving it
207# means moving that text with it.
208sed '/^$/q' statement.note | sed '$d' > lifted.txt
209sed 's/^provenance.harness .*/provenance.harness somebody-else/' lifted.txt > lifted.body
210sed -n '/^$/,$p' statement.note | tail -n +2 > lifted.sigs
211{ cat lifted.body; echo; cat lifted.sigs; } > lifted.note
212if verify lifted.note "$TMPDIR/known_keys" > lifted.out 2>&1; then
213 echo "attest-e2e: a signature was lifted onto another statement" >&2
214 exit 1
215fi
216grep -q 'does not check out over this text' lifted.out
217
218# A statement written some other way than the one written
219# form, signed properly over those bytes. The signature is
220# good and the statement is still refused: it would verify
221# here and fail wherever it was written out again, so the
222# form is a rule rather than a convention.
223{ head -n 1 lifted.txt; tail -n +2 lifted.txt | sort -r; } > loose.txt
224cmp -s loose.txt lifted.txt \
225 && { echo "attest-e2e: the reordering changed nothing" >&2; exit 1; }
226attest sign --key "$TMPDIR/host" --name "$host" loose.txt > loose.note
227if verify loose.note "$TMPDIR/known_keys" > loose.out 2>&1; then
228 echo "attest-e2e: a statement not in the written form must not verify" >&2
229 exit 1
230fi
231grep -q 'not the written form of what it says' loose.out
232
233# The subject is the tree. Change the tree and the same
234# signed statement stops being about it …
235echo goodbye > hello.txt
236git commit --quiet -am "change the tree"
237if verify statement.note "$TMPDIR/known_keys" > moved.out 2>&1; then
238 echo "attest-e2e: a statement about another tree must not verify" >&2
239 exit 1
240fi
241grep -q 'tree digest mismatch' moved.out
242
243# … while rewriting the commit over the same tree leaves the
244# attestation standing, which is what a subject that is a
245# tree digest buys.
246git commit --quiet --amend -m "same tree, new commit"
247rewritten="$(attest digest | sed 's/^valley-tree-v1://')"
248git revert --quiet --no-edit HEAD
249restored="$(attest digest | sed 's/^valley-tree-v1://')"
250if [ "$restored" != "$digest" ]; then
251 echo "attest-e2e: restoring the tree did not restore its digest" >&2
252 echo " original $digest" >&2
253 echo " restored $restored" >&2
254 exit 1
255fi
256if [ "$rewritten" = "$digest" ]; then
257 echo "attest-e2e: a changed tree kept its digest" >&2
258 exit 1
259fi
260verify statement.note "$TMPDIR/known_keys" > rebased.out
261grep -q 'matches the recorded subject digest' rebased.out
262
263# A replacement ref in the repository changes nothing a standalone
264# verify reads. refs/replace/<commit> makes git read another commit
265# wherever it looks that one up, so a reader that left replacement
266# refs on would digest the attested tree for a revision holding a
267# different one, and pass it. The caller's environment asking for
268# replacement refs is not listened to either.
269attested="$(git rev-parse HEAD)"
270echo "a change nobody attested" > evil.txt
271git add evil.txt
272git commit --quiet -m "a change nobody attested"
273evil="$(git rev-parse HEAD)"
274git update-ref "refs/replace/$evil" "$attested"
275if [ "$(git rev-parse "$evil^{tree}")" != "$(git rev-parse "$attested^{tree}")" ]; then
276 echo "attest-e2e: the replacement did not take, so this case proves nothing" >&2
277 exit 1
278fi
279if GIT_CONFIG_PARAMETERS="'core.usereplacerefs'='true'" attest verify --note statement.note \
280 --known-keys "$TMPDIR/known_keys" --repo "$repo" --rev "$evil" > replaced.out 2>&1; then
281 echo "attest-e2e: a replacement ref made an unattested revision verify" >&2
282 cat replaced.out >&2
283 exit 1
284fi
285if grep -q 'matches the recorded subject digest' replaced.out; then
286 echo "attest-e2e: the digest of a replaced revision matched the attested one" >&2
287 exit 1
288fi
289[ "$(attest digest --rev "$evil" | sed 's/^valley-tree-v1://')" != "$digest" ] || {
290 echo "attest-e2e: attest digested the replacement, not the revision" >&2
291 exit 1
292}
293git update-ref -d "refs/replace/$evil"
294touch "$out"