gunk-dev/the-valley

valley-request.sh

sourceedde5dd8af83main's published tip history
18.6 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
227moved="$(git rev-parse main)"
228
229git checkout --quiet -b topic/two "$first"
230printf 'SECRET\n' > src/leak.txt
231git add -A
232git commit --quiet -m "a leak"
233git push --quiet origin topic/two
234
235: > "$pushlog"
236printf 'a\ns\n' > "$w/answers"
237valley review topic/two < "$w/answers" > "$w/fail.out" 2> "$w/fail.err"
238
239# The prompt is the whole of what the operator is offered, so it is pinned
240# character for character: a verb that stops being offered, or one that
241# starts, is a change to the review loop and not an incidental one.
242grep -qF 'topic/two does not fast-forward from main: [a]sk, [b]ase onto main, [r]eject, [s]kip? ' "$w/fail.out"
243grep -q '^checking topic/two at .*: no-secrets$' "$w/fail.out"
244grep -qE '^check no-secrets +command failed$' "$w/fail.out"
245grep -q 'the checks did not pass; nothing published, main untouched' "$w/fail.err"
246# The prompt is printed without a newline, so what follows a refusal shares
247# its line — the answer to it is not anchored.
248grep -q 'skipped topic/two; nothing done' "$w/fail.out"
249if [ -s "$pushlog" ]; then
250 echo "valley-request: a failing check pushed something" >&2
251 cat "$pushlog" >&2
252 exit 1
253fi
254git -C "$origin" for-each-ref --format='%(refname)' refs/the-valley > "$w/after.txt"
255if grep -q 'topic-two' "$w/after.txt"; then
256 echo "valley-request: a failing check left a ref on origin" >&2
257 cat "$w/after.txt" >&2
258 exit 1
259fi
260
261# ----------------------------------------------------------------------
262# 4. No signing key, with a check owed: refused before any check is run,
263# and the prompt still comes back. The host signs, and there is no
264# unsigned mode.
265: > "$pushlog"
266printf 'a\ns\n' > "$w/answers"
267env -u VALLEY_ATTEST_KEY valley review topic/two < "$w/answers" > "$w/nokey.out" 2> "$w/nokey.err"
268grep -q 'no signing key: set VALLEY_ATTEST_KEY' "$w/nokey.err"
269grep -q 'skipped topic/two; nothing done' "$w/nokey.out"
270if [ -s "$pushlog" ]; then
271 echo "valley-request: a run with no signing key pushed something" >&2
272 exit 1
273fi
274
275# ----------------------------------------------------------------------
276# 5. A branch the policy requires no check of is filed as a request with
277# no evidence. The integrator lands one and records it as a landing
278# that required nothing, so refusing to file it here would strand
279# exactly the changes a policy leaves unrequired. Nothing is owed, so
280# nothing is signed: this runs with no signing key at all.
281git checkout --quiet main
282git checkout --quiet -b topic/quiet
283mkdir -p quiet
284echo "a quiet note" > quiet/note.md
285git add -A
286git commit --quiet -m "a change under a class that asks nothing"
287git push --quiet origin topic/quiet
288quiethead="$(git rev-parse --short topic/quiet)"
289
290: > "$pushlog"
291printf 'a\n' > "$w/answers"
292env -u VALLEY_ATTEST_KEY valley review topic/quiet < "$w/answers" \
293 > "$w/quiet.out" 2> "$w/quiet.err"
294
295grep -q 'owes no check under .* + policy/project@origin/main .*; filing the request with no evidence' "$w/quiet.out"
296grep -qF "$valley policy/instance@refs/heads/main" "$w/quiet.out"
297grep -qx 'requested topic/quiet -> refs/heads/main' "$w/quiet.out"
298grep -qx " request refs/the-valley/integration-requests/main/topic-quiet -> $quiethead" "$w/quiet.out"
299grep -qx ' evidence none — nothing was owed, so nothing was signed' "$w/quiet.out"
300grep -qx ' checks none — the policy requires no check of this change' "$w/quiet.out"
301
302# One push, carrying the request and nothing else.
303pushes="$(grep -c '^--- push' "$pushlog" || true)"
304if [ "$pushes" != 1 ]; then
305 echo "valley-request: the bare request took $pushes pushes, not 1" >&2
306 cat "$pushlog" >&2
307 exit 1
308fi
309if grep -q 'refs/the-valley/attestations/' "$pushlog"; then
310 echo "valley-request: a request owing nothing published evidence" >&2
311 cat "$pushlog" >&2
312 exit 1
313fi
314if [ "$(git -C "$origin" rev-parse refs/the-valley/integration-requests/main/topic-quiet)" \
315 != "$(git rev-parse topic/quiet)" ]; then
316 echo "valley-request: the bare request does not name the branch's head" >&2
317 exit 1
318fi
319
320# ----------------------------------------------------------------------
321# 6. [i]ntegrate is untouched: both paths coexist until the writers change
322# lands, and neither verb knows which one the operator may use.
323git checkout --quiet main
324git checkout --quiet -b topic/three
325echo "another readme" > docs/readme.md
326git commit --quiet -am "another readme"
327git push --quiet origin topic/three
328
329printf 'i\n' > "$w/answers"
330valley review topic/three < "$w/answers" > "$w/integrate.out" 2>&1
331grep -qF 'topic/three: [a]sk, [i]ntegrate, [r]eject, [s]kip? ' "$w/integrate.out"
332grep -q 'integrated topic/three: main is now' "$w/integrate.out"
333if [ "$(git -C "$origin" rev-parse refs/heads/main)" = "$moved" ]; then
334 echo "valley-request: [i]ntegrate did not move main" >&2
335 exit 1
336fi
337
338# The request filed in 1 is still pending: nothing here consumes it, which
339# is the integrator's own step.
340git -C "$origin" rev-parse --verify --quiet \
341 refs/the-valley/integration-requests/main/topic-one > /dev/null
342
343# ----------------------------------------------------------------------
344# 7. A change does not supply the policy that gates it. This branch
345# deletes its own policy/project and repoints policy/valley at a
346# repository that does not exist. Both layers come from the target's
347# tip, so neither edit reaches the derivation: it composes the same two
348# layers it would for any other branch, and the fetch that a branch-read
349# pointer would have attempted never happens.
350git checkout --quiet -b topic/nolayer
351git rm --quiet -r policy/project
352echo "$TMPDIR/there-is-no-such-valley.git" > policy/valley
353git add -A
354git commit --quiet -m "delete the project layer and repoint the valley"
355git push --quiet origin topic/nolayer
356
357valley checks "$first" topic/nolayer > "$w/nolayer.out" 2> "$w/nolayer.err"
358grep -qx "policy: $valley policy/instance@refs/heads/main + policy/project@origin/main $(git rev-parse --short origin/main)" "$w/nolayer.out"
359if grep -q 'there-is-no-such-valley' "$w/nolayer.out" "$w/nolayer.err"; then
360 echo "valley-request: the deriver followed the branch's own valley pointer" >&2
361 cat "$w/nolayer.out" "$w/nolayer.err" >&2
362 exit 1
363fi
364
365: > "$pushlog"
366printf 'a\n' > "$w/answers"
367valley review topic/nolayer < "$w/answers" \
368 > "$w/nolayer.review.out" 2> "$w/nolayer.review.err"
369grep -qx 'requested topic/nolayer -> refs/heads/main' "$w/nolayer.review.out"
370
371# ----------------------------------------------------------------------
372# 8. The reviewer's checkout contributes nothing. This is the case that
373# failed in live use: the operator's checkout was an older branch
374# without the landed policy layer, and the deriver read the working
375# tree and refused. topic/nolayer is exactly such a checkout — no
376# policy/project in it, and a policy/valley naming nowhere — so every
377# derivation below runs from it.
378#
379# Byte-identical output is the claim, not merely a successful one:
380# review works the same from any branch, however stale.
381git checkout --quiet main
382valley checks "$first" topic/one > "$w/from-main.out"
383git checkout --quiet topic/nolayer
384valley checks "$first" topic/one > "$w/from-stale.out"
385if ! cmp -s "$w/from-main.out" "$w/from-stale.out"; then
386 echo "valley-request: the checkout changed what the derivation said" >&2
387 diff -u "$w/from-main.out" "$w/from-stale.out" >&2 || true
388 exit 1
389fi
390
391# And [a]sk, which is where it failed: a request filed from that same
392# checkout, over a branch the stale tree knows nothing about.
393git checkout --quiet -b topic/four origin/main
394echo "one more readme" > docs/readme.md
395git commit --quiet -am "one more readme"
396git push --quiet origin topic/four
397git checkout --quiet topic/nolayer
398
399: > "$pushlog"
400printf 'a\n' > "$w/answers"
401valley review topic/four < "$w/answers" > "$w/stale.out" 2> "$w/stale.err"
402grep -q '^checking topic/four at .*: tree-ok$' "$w/stale.out"
403grep -qx 'requested topic/four -> refs/heads/main' "$w/stale.out"
404grep -qx ' checks tree-ok' "$w/stale.out"
405
406# ----------------------------------------------------------------------
407# 9. A target carrying no project layer at all is derived under the floor
408# alone, and says so. The project layer only ever adds, so the identity
409# of "adds" is "adds nothing"; refusing instead would make a repository
410# underivable until its first policy commit, and that commit could not
411# derive either. This is the integrator's own reading of an absent
412# layer (integrator/policy.go), and the two have to agree.
413bare2="$TMPDIR/floorless.git"
414git init --quiet --bare "$bare2"
415repo2="$TMPDIR/floorless"
416git init --quiet "$repo2"
417cd "$repo2" || exit 1
418git remote add origin "$bare2"
419mkdir -p docs schema policy
420cp "$schemaFile" schema/verification.cue
421echo "the readme" > docs/readme.md
422echo "$valley" > policy/valley
423git add -A
424git commit --quiet -m base
425git push --quiet -u origin main
426base2="$(git rev-parse main)"
427echo "a second readme" > docs/readme.md
428git commit --quiet -am "a second readme"
429
430valley checks "$base2" HEAD > "$w/floorless.out" 2> "$w/floorless.err"
431grep -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"
432grep -qE '^ tree-ok +mandatory +prose$' "$w/floorless.out"
433if grep -q no-secrets "$w/floorless.out"; then
434 echo "valley-request: the floor-alone derivation saw another project's layer" >&2
435 exit 1
436fi
437cd "$repo" || exit 1
438git checkout --quiet main
439
440# No throwaway worktree outlived any of it: only the checkout is left.
441worktrees="$(git worktree list --porcelain | grep -c '^worktree ' || true)"
442if [ "$worktrees" != 1 ]; then
443 echo "valley-request: a throwaway worktree was left behind" >&2
444 git worktree list >&2
445 exit 1
446fi
447
448echo "every scenario held"
449{
450 cat "$w/ask.out"
451 printf '\n--- the scratch origin, after all of it ---\n'
452 git -C "$origin" for-each-ref --format='%(objectname) %(refname)'
453} > "$out"