gunk-dev/the-valley

valley-request.sh

source6e9190fcc0e2main's published tip history
20.5 KiB
1# shellcheck shell=bash
2# Paths and fixtures come from the derivation environment.
3# shellcheck disable=SC2154
4export HOME="$TMPDIR"
5export XDG_STATE_HOME="$TMPDIR/state"
6export GIT_CONFIG_NOSYSTEM=1
7# What this check writes down, kept out of the repository under test: an
8# answer file or a captured stream sitting in the working tree would be
9# committed by the next `git add -A` and change the tree being attested.
10w="$TMPDIR/scratch"
11mkdir -p "$w"
12git config --global user.name valley-check
13git config --global user.email valley-check@localhost
14git config --global init.defaultBranch main
15git config --global advice.detachedHead false
16
17# The operator's signing key. Provisioning one is deployment, so the verb
18# takes it as a parameter and this check supplies a throwaway.
19ssh-keygen -q -t ed25519 -N "" -C valley-check -f "$TMPDIR/host"
20export VALLEY_ATTEST_KEY="$TMPDIR/host"
21export VALLEY_ATTEST_NAME=laddie.valley.invalid/attestations
22keyhash="$(attest key --key "$TMPDIR/host" --name "$VALLEY_ATTEST_NAME" | cut -d+ -f2)"
23
24origin="$TMPDIR/project.git"
25git init --quiet --bare "$origin"
26
27# Every push the bare repo receives, as its hook sees it. The claim [a]sk
28# makes is that ONE push carries the evidence and the request together, and
29# that claim is only observable from the receiving side.
30pushlog="$TMPDIR/pushlog"
31cat > "$origin/hooks/pre-receive" <<EOF
32#!/bin/sh
33echo '--- push' >> $pushlog
34cat >> $pushlog
35EOF
36chmod +x "$origin/hooks/pre-receive"
37
38# ----------------------------------------------------------------------
39# The owning valley's repository, carrying the floor at policy/instance.
40# The floor is read from here and never from the project, which is the
41# tree under judgment: what governs a change has to come from outside it.
42# Fetching is all the deriver does to this repository, which is what
43# anyone the valley governs necessarily holds.
44valley="$TMPDIR/valley.git"
45git init --quiet --bare "$valley"
46valleywork="$TMPDIR/valley-work"
47git init --quiet "$valleywork"
48mkdir -p "$valleywork/policy/instance"
49cat > "$valleywork/policy/instance/floor.cue" <<'EOF'
50package verification
51
52floor: {
53 checks: "tree-ok": {
54 runner: "command"
55 command: "test -f docs/readme.md"
56 }
57 classes: prose: {
58 paths: "docs/**": true
59 requires: "tree-ok": true
60 }
61 // A class that covers paths and asks nothing of them. Owing no check
62 // is an ordinary outcome, not a hole: the policy has read these paths
63 // and has nothing to require of them.
64 classes: quiet: {
65 paths: "quiet/**": true
66 requires: {}
67 }
68 unclassified: "tree-ok": true
69}
70EOF
71git -C "$valleywork" add -A
72git -C "$valleywork" commit --quiet -m floor
73git -C "$valleywork" push --quiet "$valley" main
74
75# ----------------------------------------------------------------------
76# The project, laid out the way the deriver reads one without being told:
77# its own layer at policy/project, and its owning valley named at
78# policy/valley. Both checks use the command runner: the nix runner cannot
79# be exercised from inside a nix build, since the sandbox has no daemon to
80# call, and what is under test here is the sequencing rather than the
81# runner.
82#
83# examples/policy/ holds a second, contradictory pair of layers, and every
84# check they require is named `phantom`. Nothing may derive it: a gate that
85# came from a worked example is a gate nobody wrote, and the name is here
86# so that the failure would be visible rather than plausible.
87repo="$TMPDIR/project"
88git init --quiet "$repo"
89cd "$repo" || exit 1
90git remote add origin "$origin"
91
92mkdir -p docs src schema policy/project examples/policy/instance examples/policy/project
93cp "$schemaFile" schema/verification.cue
94echo "the readme" > docs/readme.md
95echo "$valley" > policy/valley
96cat > policy/project/policy.cue <<'EOF'
97package verification
98
99project: {
100 checks: "no-secrets": {
101 runner: "command"
102 command: "! grep -rqF SECRET src"
103 }
104 classes: source: {
105 paths: "src/**": true
106 requires: "no-secrets": true
107 }
108}
109EOF
110cat > examples/policy/instance/floor.cue <<'EOF'
111package verification
112
113floor: {
114 checks: phantom: {
115 runner: "command"
116 command: "false"
117 }
118 classes: everything: {
119 paths: "**": true
120 requires: phantom: true
121 }
122 unclassified: phantom: true
123}
124EOF
125cat > examples/policy/project/policy.cue <<'EOF'
126package verification
127
128project: {}
129EOF
130git add -A
131git commit --quiet -m base
132git push --quiet -u origin main
133first="$(git rev-parse main)"
134
135# ----------------------------------------------------------------------
136# 1. A branch whose required check passes: the evidence and the request
137# reach origin in one push, and main does not move.
138git checkout --quiet -b topic/one
139echo "a better readme" > docs/readme.md
140git commit --quiet -am "improve the readme"
141git push --quiet origin topic/one
142digest="$(attest digest --rev topic/one | cut -d: -f2)"
143
144: > "$pushlog"
145printf 'a\n' > "$w/answers"
146valley review topic/one < "$w/answers" > "$w/ask.out" 2> "$w/ask.err"
147
148grep -q '^checking topic/one at .*: tree-ok$' "$w/ask.out"
149grep -qE '^check tree-ok +command passed$' "$w/ask.out"
150grep -qx 'requested topic/one -> refs/heads/main' "$w/ask.out"
151grep -qx " request refs/the-valley/integration-requests/main/topic-one -> $(git rev-parse --short topic/one)" "$w/ask.out"
152grep -qx " evidence refs/the-valley/attestations/$digest/$keyhash" "$w/ask.out"
153grep -qx ' checks tree-ok' "$w/ask.out"
154grep -q 'main is untouched' "$w/ask.out"
155
156# What origin holds now. The attestation is keyed by the digest of the
157# BRANCH's tree, which is what says the checks ran over the right tree and
158# not over the operator's working copy.
159git -C "$origin" for-each-ref --format='%(refname)' refs/the-valley > "$w/published.txt"
160grep -qx "refs/the-valley/integration-requests/main/topic-one" "$w/published.txt"
161grep -qx "refs/the-valley/attestations/$digest/$keyhash" "$w/published.txt"
162if [ "$(git -C "$origin" rev-parse refs/the-valley/integration-requests/main/topic-one)" \
163 != "$(git rev-parse topic/one)" ]; then
164 echo "valley-request: the request ref does not name the branch's head" >&2
165 exit 1
166fi
167if [ "$(git -C "$origin" rev-parse refs/heads/main)" != "$first" ]; then
168 echo "valley-request: [a]sk moved main" >&2
169 exit 1
170fi
171
172# One push carried both. This is the friction the verb exists to remove:
173# `attest run --push` publishes the branch and the evidence and knows
174# nothing about a request ref, so doing this by hand is two beats.
175pushes="$(grep -c '^--- push' "$pushlog" || true)"
176if [ "$pushes" != 1 ]; then
177 echo "valley-request: the evidence and the request took $pushes pushes, not 1" >&2
178 cat "$pushlog" >&2
179 exit 1
180fi
181grep -q "refs/the-valley/attestations/$digest/$keyhash" "$pushlog"
182grep -q 'refs/the-valley/integration-requests/main/topic-one' "$pushlog"
183
184# The note origin holds is the one the operator signed, over that tree.
185git cat-file -p "refs/the-valley/attestations/$digest/$keyhash:tree-ok/statement.note" \
186 > "$w/statement.note"
187attest key --key "$TMPDIR/host" --name "$VALLEY_ATTEST_NAME" > "$w/known_keys"
188attest verify --note "$w/statement.note" --repo "$repo" --rev topic/one \
189 --known-keys "$w/known_keys" --signer "$VALLEY_ATTEST_NAME" > "$w/verify.out"
190grep -q 'matches the recorded subject digest' "$w/verify.out"
191
192# ----------------------------------------------------------------------
193# 2. The layers the derivation used are the ones that govern, and it says
194# which they were: the floor fetched from the owning valley, and the
195# project's own layer at policy/project in the TARGET's tip.
196# examples/policy/ is in this tree and requires `phantom` of every path,
197# and no default reaches it — it is derivable only by naming it, which
198# is what the options are for.
199valley checks "$first" topic/one > "$w/checks.out" 2> "$w/checks.err"
200grep -qx "policy: $valley policy/instance@refs/heads/main + policy/project@origin/main $(git rev-parse --short origin/main)" "$w/checks.out"
201grep -qx 'classes matched: prose' "$w/checks.out"
202grep -qE '^ tree-ok +mandatory +prose$' "$w/checks.out"
203if grep -q phantom "$w/checks.out" "$w/checks.err"; then
204 echo "valley-request: a default derived a check from examples/policy/" >&2
205 cat "$w/checks.out" "$w/checks.err" >&2
206 exit 1
207fi
208
209# Named, the example layers derive exactly what they declare. This is the
210# other half of the claim above: examples/policy/ is readable, and reading
211# it is something a person asks for.
212valley checks --instance examples/policy/instance --project examples/policy/project \
213 "$first" topic/one > "$w/example.out"
214grep -qE '^ phantom +mandatory' "$w/example.out"
215grep -qx 'policy: examples/policy/instance + examples/policy/project' "$w/example.out"
216
217# ----------------------------------------------------------------------
218# 3. main moves on, so the next branch is stale. [a]sk is offered there
219# too — the integrator applies a delta to the tip rather than
220# fast-forwarding — and a branch whose check fails publishes nothing
221# and leaves the review loop usable.
222git checkout --quiet main
223echo "a note" > docs/notes.md
224git add -A
225git commit --quiet -m "a landing that moves main"
226git push --quiet origin main
227
228git checkout --quiet -b topic/two "$first"
229printf 'SECRET\n' > src/leak.txt
230git add -A
231git commit --quiet -m "a leak"
232git push --quiet origin topic/two
233
234: > "$pushlog"
235printf 'a\ns\n' > "$w/answers"
236valley review topic/two < "$w/answers" > "$w/fail.out" 2> "$w/fail.err"
237
238# The prompt is the whole of what the operator is offered, so it is pinned
239# character for character: a verb that stops being offered, or one that
240# starts, is a change to the review loop and not an incidental one.
241grep -qF 'topic/two does not fast-forward from main: [a]sk, [b]ase onto main, [r]eject, [s]kip? ' "$w/fail.out"
242grep -q '^checking topic/two at .*: no-secrets$' "$w/fail.out"
243grep -qE '^check no-secrets +command failed$' "$w/fail.out"
244grep -q 'the checks did not pass; nothing published, main untouched' "$w/fail.err"
245# The prompt is printed without a newline, so what follows a refusal shares
246# its line — the answer to it is not anchored.
247grep -q 'skipped topic/two; nothing done' "$w/fail.out"
248if [ -s "$pushlog" ]; then
249 echo "valley-request: a failing check pushed something" >&2
250 cat "$pushlog" >&2
251 exit 1
252fi
253git -C "$origin" for-each-ref --format='%(refname)' refs/the-valley > "$w/after.txt"
254if grep -q 'topic-two' "$w/after.txt"; then
255 echo "valley-request: a failing check left a ref on origin" >&2
256 cat "$w/after.txt" >&2
257 exit 1
258fi
259
260# ----------------------------------------------------------------------
261# 4. No signing key, with a check owed: refused before any check is run,
262# and the prompt still comes back. The host signs, and there is no
263# unsigned mode.
264: > "$pushlog"
265printf 'a\ns\n' > "$w/answers"
266env -u VALLEY_ATTEST_KEY valley review topic/two < "$w/answers" > "$w/nokey.out" 2> "$w/nokey.err"
267grep -q 'no signing key: set VALLEY_ATTEST_KEY' "$w/nokey.err"
268grep -q 'skipped topic/two; nothing done' "$w/nokey.out"
269if [ -s "$pushlog" ]; then
270 echo "valley-request: a run with no signing key pushed something" >&2
271 exit 1
272fi
273
274# ----------------------------------------------------------------------
275# 5. A branch the policy requires no check of is filed as a request with
276# no evidence. The integrator lands one and records it as a landing
277# that required nothing, so refusing to file it here would strand
278# exactly the changes a policy leaves unrequired. Nothing is owed, so
279# nothing is signed: this runs with no signing key at all.
280git checkout --quiet main
281git checkout --quiet -b topic/quiet
282mkdir -p quiet
283echo "a quiet note" > quiet/note.md
284git add -A
285git commit --quiet -m "a change under a class that asks nothing"
286git push --quiet origin topic/quiet
287quiethead="$(git rev-parse --short topic/quiet)"
288
289: > "$pushlog"
290printf 'a\n' > "$w/answers"
291env -u VALLEY_ATTEST_KEY valley review topic/quiet < "$w/answers" \
292 > "$w/quiet.out" 2> "$w/quiet.err"
293
294grep -q 'owes no check under .* + policy/project@origin/main .*; filing the request with no evidence' "$w/quiet.out"
295grep -qF "$valley policy/instance@refs/heads/main" "$w/quiet.out"
296grep -qx 'requested topic/quiet -> refs/heads/main' "$w/quiet.out"
297grep -qx " request refs/the-valley/integration-requests/main/topic-quiet -> $quiethead" "$w/quiet.out"
298grep -qx ' evidence none — nothing was owed, so nothing was signed' "$w/quiet.out"
299grep -qx ' checks none — the policy requires no check of this change' "$w/quiet.out"
300
301# One push, carrying the request and nothing else.
302pushes="$(grep -c '^--- push' "$pushlog" || true)"
303if [ "$pushes" != 1 ]; then
304 echo "valley-request: the bare request took $pushes pushes, not 1" >&2
305 cat "$pushlog" >&2
306 exit 1
307fi
308if grep -q 'refs/the-valley/attestations/' "$pushlog"; then
309 echo "valley-request: a request owing nothing published evidence" >&2
310 cat "$pushlog" >&2
311 exit 1
312fi
313if [ "$(git -C "$origin" rev-parse refs/the-valley/integration-requests/main/topic-quiet)" \
314 != "$(git rev-parse topic/quiet)" ]; then
315 echo "valley-request: the bare request does not name the branch's head" >&2
316 exit 1
317fi
318
319# ----------------------------------------------------------------------
320# 6. [b]ase, and the prompt it comes back to. A stale branch is rebased
321# onto main in a throwaway worktree and re-reviewed, and the prompt the
322# loop then prints is the fast-forward one — the path a reader of
323# bin/valley has to trace two loops to reach, so it is exercised here
324# rather than argued about.
325git checkout --quiet main
326git checkout --quiet -b topic/three "$first"
327echo "another readme" > docs/readme.md
328git commit --quiet -am "another readme"
329git push --quiet origin topic/three
330before_base="$(git -C "$origin" rev-parse refs/heads/main)"
331
332printf 'b\ns\n' > "$w/answers"
333valley review topic/three < "$w/answers" > "$w/base.out" 2>&1
334grep -qF 'topic/three does not fast-forward from main: [a]sk, [b]ase onto main, [r]eject, [s]kip? ' "$w/base.out"
335grep -qF 'rebased topic/three onto origin/main, pushed with --force-with-lease' "$w/base.out"
336# The post-[b]ase prompt: the branch fast-forwards now, so the loop falls
337# through to the prompt that has no [b]ase in it.
338grep -qF 'topic/three: [a]sk, [r]eject, [s]kip? ' "$w/base.out"
339grep -q 'skipped topic/three; nothing done' "$w/base.out"
340git merge-base --is-ancestor refs/remotes/origin/main refs/remotes/origin/topic/three \
341 || { echo "valley-request: [b]ase left the branch stale" >&2; exit 1; }
342if [ "$(git -C "$origin" rev-parse refs/heads/main)" != "$before_base" ]; then
343 echo "valley-request: the review loop moved main" >&2
344 exit 1
345fi
346git checkout --quiet main
347
348# The request filed in 1 is still pending: nothing here consumes it, which
349# is the integrator's own step.
350git -C "$origin" rev-parse --verify --quiet \
351 refs/the-valley/integration-requests/main/topic-one > /dev/null
352
353# ----------------------------------------------------------------------
354# 7. A change does not supply the policy that gates it. This branch
355# deletes its own policy/project and repoints policy/valley at a
356# repository that does not exist. Both layers come from the target's
357# tip, so neither edit reaches the derivation: it composes the same two
358# layers it would for any other branch, and the fetch that a branch-read
359# pointer would have attempted never happens.
360git checkout --quiet -b topic/nolayer
361git rm --quiet -r policy/project
362echo "$TMPDIR/there-is-no-such-valley.git" > policy/valley
363git add -A
364git commit --quiet -m "delete the project layer and repoint the valley"
365git push --quiet origin topic/nolayer
366
367valley checks "$first" topic/nolayer > "$w/nolayer.out" 2> "$w/nolayer.err"
368grep -qx "policy: $valley policy/instance@refs/heads/main + policy/project@origin/main $(git rev-parse --short origin/main)" "$w/nolayer.out"
369if grep -q 'there-is-no-such-valley' "$w/nolayer.out" "$w/nolayer.err"; then
370 echo "valley-request: the deriver followed the branch's own valley pointer" >&2
371 cat "$w/nolayer.out" "$w/nolayer.err" >&2
372 exit 1
373fi
374
375: > "$pushlog"
376printf 'a\n' > "$w/answers"
377valley review topic/nolayer < "$w/answers" \
378 > "$w/nolayer.review.out" 2> "$w/nolayer.review.err"
379grep -qx 'requested topic/nolayer -> refs/heads/main' "$w/nolayer.review.out"
380
381# ----------------------------------------------------------------------
382# 8. The reviewer's checkout contributes nothing. This is the case that
383# failed in live use: the operator's checkout was an older branch
384# without the landed policy layer, and the deriver read the working
385# tree and refused. topic/nolayer is exactly such a checkout — no
386# policy/project in it, and a policy/valley naming nowhere — so every
387# derivation below runs from it.
388#
389# Byte-identical output is the claim, not merely a successful one:
390# review works the same from any branch, however stale.
391git checkout --quiet main
392valley checks "$first" topic/one > "$w/from-main.out"
393git checkout --quiet topic/nolayer
394valley checks "$first" topic/one > "$w/from-stale.out"
395if ! cmp -s "$w/from-main.out" "$w/from-stale.out"; then
396 echo "valley-request: the checkout changed what the derivation said" >&2
397 diff -u "$w/from-main.out" "$w/from-stale.out" >&2 || true
398 exit 1
399fi
400
401# And [a]sk, which is where it failed: a request filed from that same
402# checkout, over a branch the stale tree knows nothing about.
403git checkout --quiet -b topic/four origin/main
404echo "one more readme" > docs/readme.md
405git commit --quiet -am "one more readme"
406git push --quiet origin topic/four
407git checkout --quiet topic/nolayer
408
409: > "$pushlog"
410printf 'a\n' > "$w/answers"
411valley review topic/four < "$w/answers" > "$w/stale.out" 2> "$w/stale.err"
412grep -q '^checking topic/four at .*: tree-ok$' "$w/stale.out"
413grep -qx 'requested topic/four -> refs/heads/main' "$w/stale.out"
414grep -qx ' checks tree-ok' "$w/stale.out"
415
416# ----------------------------------------------------------------------
417# 9. A target carrying no project layer at all is derived under the floor
418# alone, and says so. The project layer only ever adds, so the identity
419# of "adds" is "adds nothing"; refusing instead would make a repository
420# underivable until its first policy commit, and that commit could not
421# derive either. This is the integrator's own reading of an absent
422# layer (integrator/policy.go), and the two have to agree.
423bare2="$TMPDIR/floorless.git"
424git init --quiet --bare "$bare2"
425repo2="$TMPDIR/floorless"
426git init --quiet "$repo2"
427cd "$repo2" || exit 1
428git remote add origin "$bare2"
429mkdir -p docs schema policy
430cp "$schemaFile" schema/verification.cue
431echo "the readme" > docs/readme.md
432echo "$valley" > policy/valley
433git add -A
434git commit --quiet -m base
435git push --quiet -u origin main
436base2="$(git rev-parse main)"
437echo "a second readme" > docs/readme.md
438git commit --quiet -am "a second readme"
439
440valley checks "$base2" HEAD > "$w/floorless.out" 2> "$w/floorless.err"
441grep -qx "policy: $valley policy/instance@refs/heads/main + no policy/project@origin/main $(git rev-parse --short origin/main) — the floor alone" "$w/floorless.out"
442grep -qE '^ tree-ok +mandatory +prose$' "$w/floorless.out"
443if grep -q no-secrets "$w/floorless.out"; then
444 echo "valley-request: the floor-alone derivation saw another project's layer" >&2
445 exit 1
446fi
447cd "$repo" || exit 1
448git checkout --quiet main
449
450# ----------------------------------------------------------------------
451# The verbs the loop offers are the whole of what an operator can do to a
452# branch, so no path may offer one outside the set: [a]sk, [b]ase,
453# [r]eject, [s]kip. Every stream this check captured is scanned at the end,
454# which is what makes the claim about paths rather than about the two
455# prompts pinned character for character above — the fast-forward prompt,
456# the stale one, the one [b]ase comes back to, and the ones a refused [a]sk
457# returns to are all in here. A verb that writes the protected ref from the
458# operator's own checkout — the retired [i]ntegrate, or any successor to
459# it — fails this wherever it is printed.
460offered="$(cat "$w"/*.out | grep -o '\[[a-z]\][a-z]*' | sort -u || true)"
461outside="$(printf '%s\n' "$offered" | grep -vxE '\[a\]sk|\[b\]ase|\[r\]eject|\[s\]kip' || true)"
462if [ -n "$outside" ]; then
463 echo "valley-request: a review path offered a verb outside the set:" >&2
464 printf '%s\n' "$outside" >&2
465 exit 1
466fi
467# The scan is only worth something if it saw the offers at all.
468for verb in '[a]sk' '[b]ase' '[r]eject' '[s]kip'; do
469 printf '%s\n' "$offered" | grep -qxF "$verb" \
470 || { echo "valley-request: no captured session offered $verb" >&2; exit 1; }
471done
472
473# No throwaway worktree outlived any of it: only the checkout is left.
474worktrees="$(git worktree list --porcelain | grep -c '^worktree ' || true)"
475if [ "$worktrees" != 1 ]; then
476 echo "valley-request: a throwaway worktree was left behind" >&2
477 git worktree list >&2
478 exit 1
479fi
480
481echo "every scenario held"
482{
483 cat "$w/ask.out"
484 printf '\n--- the scratch origin, after all of it ---\n'
485 git -C "$origin" for-each-ref --format='%(objectname) %(refname)'
486} > "$out"