gunk-dev/the-valley

signers-e2e.sh

source695d65eb4898main's published tip history
16.7 KiB
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
14set -euo pipefail
15
16w="$TMPDIR/signers-e2e"
17mkdir -p "$w"
18export HOME="$w"
19export GIT_CONFIG_NOSYSTEM=1
20unset VALLEY_KNOWN_SIGNERS VALLEY_INTEGRATOR_SIGNER VALLEY_PRINCIPAL
21git config --global user.name signers-e2e
22git config --global user.email signers-e2e@localhost
23git config --global init.defaultBranch main
24git config --global advice.detachedHead false
25
26fail() {
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
34cp -r "$integratorE2e" "$w/stand-ins"
35chmod -R +w "$w/stand-ins"
36patchShebangs "$w/stand-ins" > /dev/null
37export PATH="$w/stand-ins:$PATH" VALLEY_FAKE_STORE="$w/store"
38
39mkdir -p "$w/keys"
40for who in contributor integrator rotated; do install -m 600 "$keys/$who" "$w/keys/$who"; done
41contributor=contributor.valley.invalid/attestations
42# The name the controller signs under: the binary's default, which is this
43# machine's name.
44name="$(uname -n)/attestations"
45# The file the controller is given as --known-signers. signers_for writes it
46# for the integrator key given.
47signers="$w/known-signers"
48signers_for() {
49 {
50 attest key --key "$w/keys/contributor" --name "$contributor"
51 attest key --key "$1" --name "$name"
52 } > "$signers"
53}
54signers_for "$w/keys/integrator"
55
56hook="$(grep -o '/nix/store/[^ ]*-valley-protect-open' "$initScriptPath" | head -n1)"
57test -x "$hook"
58instance="$w/instance.git"
59served="$w/served.git"
60for 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"
64done
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.
69read -ra publish <<< "$publishCommand"
70publish=("${publish[@]//\/srv\/git\/sealed.git/$instance}")
71publish=("${publish[@]//\/var\/lib\/valley-instance\/known_signers/$signers}")
72case " ${publish[*]} " in
73 *" publish-signers --instance-repo $instance --known-signers $signers ") ;;
74 *) fail "the rendered publisher is not the one this test drives: $publishCommand" ;;
75esac
76
77# The instance repository carries the floor, and the project names it at
78# policy/valley.
79mkdir -p "$w/floor"
80printf 'package verification\n\nfloor: {\n\tchecks: {}\n\tclasses: {}\n\tunclassified: {}\n}\n' > "$w/floor/floor.cue"
81git init --quiet "$w/seed"
82mkdir -p "$w/seed/policy/instance"
83cp "$w/floor/floor.cue" "$w/seed/policy/instance/"
84git -C "$w/seed" add -A
85git -C "$w/seed" commit --quiet -m 'the floor'
86git -C "$w/seed" push --quiet "$instance" main
87# Written on disk, as valley-init writes it: the host's project list.
88projects="$(printf '%s\n' instance served | git -C "$instance" hash-object -w --stdin)"
89git -C "$instance" update-ref refs/the-valley/projects "$projects"
90
91git init --quiet "$w/fixture"
92cd "$w/fixture"
93git remote add origin "$served"
94mkdir -p docs src checks policy/project
95printf '%s\n' "$instance" > policy/valley
96echo "the readme" > docs/readme.md
97echo "the source" > src/main.txt
98echo docs > checks/prose-format.inputs
99echo src > checks/code-build.inputs
100cat > policy/project/policy.cue << 'CUE'
101package verification
102
103project: {
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}
114CUE
115git add -A
116git commit --quiet -m 'seed the project'
117git push --quiet origin main
118
119as() {
120 local who="$1"
121 shift
122 git config valley.principal "$who"
123 VALLEY_PRINCIPAL="$who" "$@"
124}
125ok() { "$@" > "$w/out" 2> "$w/err" || fail "$* failed"; }
126id() { 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.
128readied() {
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.
135attested() {
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.
142moves() {
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
151git fetch --quiet origin
152# quick lands by fast-forward. waiting is ready and never requested.
153git checkout --quiet -b quick origin/main
154echo "a quick note" > docs/quick.md
155git add docs/quick.md
156git commit --quiet -m 'a change that lands by fast-forward'
157quick="$(readied quick)"
158git push --quiet origin quick:main
159git fetch --quiet origin
160git checkout --quiet -b waiting origin/main
161echo "waiting" > docs/waiting.md
162git add docs/waiting.md
163git commit --quiet -m 'a change nobody approves'
164waiting="$(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.
167git checkout --quiet -b lands origin/main
168echo "a second source line" >> src/main.txt
169git commit --quiet -am 'a change the integrator lands'
170lands="$(attested lands code-build)"
171git checkout --quiet -b above lands
172echo "above" > src/above.txt
173git add src/above.txt
174git commit --quiet -m 'a change on top of lands'
175above="$(readied above)"
176moves one
177
178integrate() {
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}
185as requester git push --quiet origin "lands:refs/the-valley/integration-requests/main/c-$lands"
186integrate "$w/keys/integrator" "$lands"
187landed="$(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.
194n=0
195client() {
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}
202listing() { (cd "$c" && env "$@") > "$w/out" 2> "$w/err"; }
203changes() { listing "$@" valley changes --all --json || fail "valley changes failed"; }
204landing() {
205 jq -r --arg id "$1" '.repos[] | select(.repo == "served") | .changes[] | select(.change_id == $id) | .versions[-1].landing' "$w/out"
206}
207expect() {
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.
213landings() {
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}
216said() { grep -qF -- "$1" "$w/err" || fail "stderr does not say: $1"; }
217quiet() { 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.
222client
223changes
224expect "$lands" uncertain 'with nothing published and nothing set'
225expect "$quick" landed 'a fast-forward'
226expect "$waiting" not-landed 'a change nobody approved'
227said 'could not be verified'
228landings > "$w/unpublished"
229listing 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")"
231stack() { cut -f4 "$w/out" | tr '\n' ' '; }
232listing 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.
237changes VALLEY_KNOWN_SIGNERS="$signers" VALLEY_INTEGRATOR_SIGNER="$name"
238expect "$lands" landed-recorded 'with both variables set'
239
240# ----------------------------------------------------------------------
241# The publisher refuses a file that is not verifier keys, and publishes
242# nothing.
243echo 'not a verifier key' > "$w/garbage"
244if 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'
246fi
247said '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'
253published="$(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'
259git -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'
264grep -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.
270client
271changes
272expect "$lands" landed-recorded 'on a fresh client with nothing set'
273quiet 'could not be verified'
274quiet 'no transfer statement is verified'
275landings | sed "s/^served $lands v1 landed-recorded\$/served $lands v1 uncertain/" > "$w/published"
276diff "$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'
280listing 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")"
282quiet '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.
285listing 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")"
288listing 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.
296attest key --key "$w/keys/rotated" --name other.valley.invalid/attestations > "$w/other-signers"
297other=(VALLEY_KNOWN_SIGNERS="$w/other-signers" VALLEY_INTEGRATOR_SIGNER=other.valley.invalid/attestations)
298changes "${other[@]}"
299expect "$lands" uncertain 'with both variables naming another signer'
300quiet 'no transfer statement is verified'
301# A flag beats a variable.
302listing "${other[@]}" valley changes --all --json --known-signers "$signers" --integrator-name "$name" \
303 || fail 'changes failed with the flags'
304expect "$lands" landed-recorded 'with the flags set over the variables'
305listing "${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.
310changes VALLEY_INTEGRATOR_SIGNER=other.valley.invalid/attestations
311expect "$lands" uncertain 'with the published file and another name'
312said "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.
315changes VALLEY_KNOWN_SIGNERS="$w/missing"
316expect "$lands" uncertain 'with a variable naming no file'
317said "the known-signers file $w/missing (from VALLEY_KNOWN_SIGNERS) cannot be read"
318listing valley changes --all --json --known-signers "$w/garbage" || fail 'changes failed with a garbage file'
319expect "$lands" uncertain 'with a flag naming a file that is not verifier keys'
320said "the known-signers file $w/garbage (from --known-signers) does not read as verifier keys"
321if listing valley changes --all --json --integrator-name; then fail 'a flag with no value was taken'; fi
322said '--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.
327cd "$c"
328for 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"
335done
336cd "$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.
343signers_for "$w/keys/rotated"
344git fetch --quiet origin
345git checkout --quiet -b second origin/main
346echo "a third source line" >> src/main.txt
347git commit --quiet -am 'a change landed after the rotation'
348second="$(attested second code-build)"
349moves two
350as requester git push --quiet origin "second:refs/the-valley/integration-requests/main/c-$second"
351integrate "$w/keys/rotated" "$second"
352client
353changes
354expect "$second" uncertain 'before the rotated signers are published'
355"${publish[@]}" > "$w/out" 2> "$w/err" || fail 'the publisher failed after the rotation'
356rotated="$(git -C "$instance" rev-parse refs/the-valley/signers)"
357[ "$rotated" != "$published" ] || fail 'the rotated signers were not published'
358git -C "$instance" cat-file blob "$rotated:known-signers" | cmp -s - "$signers" \
359 || fail 'the publication does not hold the rotated signers file'
360client
361changes
362expect "$second" landed-recorded 'after the rotated signers are published'
363
364touch "$out"