gunk-dev/the-valley

valley-request.sh

source28ba29740930main's published tip history
9.0 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 project, with the two policy layers where the deriver looks without
40# being told. Both checks use the command runner: the nix runner cannot be
41# exercised from inside a nix build, since the sandbox has no daemon to
42# call, and what is under test here is the sequencing rather than the
43# runner.
44repo="$TMPDIR/project"
45git init --quiet "$repo"
46cd "$repo" || exit 1
47git remote add origin "$origin"
48
49mkdir -p docs src schema examples/policy/instance examples/policy/project
50cp "$schemaFile" schema/verification.cue
51echo "the readme" > docs/readme.md
52cat > examples/policy/instance/floor.cue <<'EOF'
53package verification
54
55floor: {
56 checks: "tree-ok": {
57 runner: "command"
58 command: "test -f docs/readme.md"
59 }
60 classes: prose: {
61 paths: "docs/**": true
62 requires: "tree-ok": true
63 }
64 unclassified: "tree-ok": true
65}
66EOF
67cat > examples/policy/project/policy.cue <<'EOF'
68package verification
69
70project: {
71 checks: "no-secrets": {
72 runner: "command"
73 command: "! grep -rqF SECRET src"
74 }
75 classes: source: {
76 paths: "src/**": true
77 requires: "no-secrets": true
78 }
79}
80EOF
81git add -A
82git commit --quiet -m base
83git push --quiet -u origin main
84first="$(git rev-parse main)"
85
86# ----------------------------------------------------------------------
87# 1. A branch whose required check passes: the evidence and the request
88# reach origin in one push, and main does not move.
89git checkout --quiet -b topic/one
90echo "a better readme" > docs/readme.md
91git commit --quiet -am "improve the readme"
92git push --quiet origin topic/one
93digest="$(attest digest --rev topic/one | cut -d: -f2)"
94
95: > "$pushlog"
96printf 'a\n' > "$w/answers"
97valley review topic/one < "$w/answers" > "$w/ask.out" 2> "$w/ask.err"
98
99grep -q '^checking topic/one at .*: tree-ok$' "$w/ask.out"
100grep -qE '^check tree-ok +command passed$' "$w/ask.out"
101grep -qx 'requested topic/one -> refs/heads/main' "$w/ask.out"
102grep -qx " request refs/the-valley/integration-requests/main/topic-one -> $(git rev-parse --short topic/one)" "$w/ask.out"
103grep -qx " evidence refs/the-valley/attestations/$digest/$keyhash" "$w/ask.out"
104grep -qx ' checks tree-ok' "$w/ask.out"
105grep -q 'main is untouched' "$w/ask.out"
106
107# What origin holds now. The attestation is keyed by the digest of the
108# BRANCH's tree, which is what says the checks ran over the right tree and
109# not over the operator's working copy.
110git -C "$origin" for-each-ref --format='%(refname)' refs/the-valley > "$w/published.txt"
111grep -qx "refs/the-valley/integration-requests/main/topic-one" "$w/published.txt"
112grep -qx "refs/the-valley/attestations/$digest/$keyhash" "$w/published.txt"
113if [ "$(git -C "$origin" rev-parse refs/the-valley/integration-requests/main/topic-one)" \
114 != "$(git rev-parse topic/one)" ]; then
115 echo "valley-request: the request ref does not name the branch's head" >&2
116 exit 1
117fi
118if [ "$(git -C "$origin" rev-parse refs/heads/main)" != "$first" ]; then
119 echo "valley-request: [a]sk moved main" >&2
120 exit 1
121fi
122
123# One push carried both. This is the friction the verb exists to remove:
124# `attest run --push` publishes the branch and the evidence and knows
125# nothing about a request ref, so doing this by hand is two beats.
126pushes="$(grep -c '^--- push' "$pushlog" || true)"
127if [ "$pushes" != 1 ]; then
128 echo "valley-request: the evidence and the request took $pushes pushes, not 1" >&2
129 cat "$pushlog" >&2
130 exit 1
131fi
132grep -q "refs/the-valley/attestations/$digest/$keyhash" "$pushlog"
133grep -q 'refs/the-valley/integration-requests/main/topic-one' "$pushlog"
134
135# The note origin holds is the one the operator signed, over that tree.
136git cat-file -p "refs/the-valley/attestations/$digest/$keyhash:tree-ok/statement.note" \
137 > "$w/statement.note"
138attest key --key "$TMPDIR/host" --name "$VALLEY_ATTEST_NAME" > "$w/known_keys"
139attest verify --note "$w/statement.note" --repo "$repo" --rev topic/one \
140 --known-keys "$w/known_keys" --signer "$VALLEY_ATTEST_NAME" > "$w/verify.out"
141grep -q 'matches the recorded subject digest' "$w/verify.out"
142
143# ----------------------------------------------------------------------
144# 2. main moves on, so the next branch is stale. [a]sk is offered there
145# too โ€” the integrator applies a delta to the tip rather than
146# fast-forwarding โ€” and a branch whose check fails publishes nothing
147# and leaves the review loop usable.
148git checkout --quiet main
149echo "a note" > docs/notes.md
150git add -A
151git commit --quiet -m "a landing that moves main"
152git push --quiet origin main
153moved="$(git rev-parse main)"
154
155git checkout --quiet -b topic/two "$first"
156printf 'SECRET\n' > src/leak.txt
157git add -A
158git commit --quiet -m "a leak"
159git push --quiet origin topic/two
160
161: > "$pushlog"
162printf 'a\ns\n' > "$w/answers"
163valley review topic/two < "$w/answers" > "$w/fail.out" 2> "$w/fail.err"
164
165grep -q 'does not fast-forward from main: \[a\]sk, \[b\]ase onto main, \[r\]eject, \[s\]kip?' "$w/fail.out"
166grep -q '^checking topic/two at .*: no-secrets$' "$w/fail.out"
167grep -qE '^check no-secrets +command failed$' "$w/fail.out"
168grep -q 'the checks did not pass; nothing published, main untouched' "$w/fail.err"
169# The prompt is printed without a newline, so what follows a refusal shares
170# its line โ€” the answer to it is not anchored.
171grep -q 'skipped topic/two; nothing done' "$w/fail.out"
172if [ -s "$pushlog" ]; then
173 echo "valley-request: a failing check pushed something" >&2
174 cat "$pushlog" >&2
175 exit 1
176fi
177git -C "$origin" for-each-ref --format='%(refname)' refs/the-valley > "$w/after.txt"
178if grep -q 'topic-two' "$w/after.txt"; then
179 echo "valley-request: a failing check left a ref on origin" >&2
180 cat "$w/after.txt" >&2
181 exit 1
182fi
183
184# ----------------------------------------------------------------------
185# 3. No signing key: refused before anything is built, and the prompt
186# still comes back. The host signs, and there is no unsigned mode.
187: > "$pushlog"
188printf 'a\ns\n' > "$w/answers"
189env -u VALLEY_ATTEST_KEY valley review topic/two < "$w/answers" > "$w/nokey.out" 2> "$w/nokey.err"
190grep -q 'no signing key: set VALLEY_ATTEST_KEY' "$w/nokey.err"
191grep -q 'skipped topic/two; nothing done' "$w/nokey.out"
192if [ -s "$pushlog" ]; then
193 echo "valley-request: a run with no signing key pushed something" >&2
194 exit 1
195fi
196
197# ----------------------------------------------------------------------
198# 4. [i]ntegrate is untouched: both paths coexist until the writers change
199# lands, and neither verb knows which one the operator may use.
200git checkout --quiet main
201git checkout --quiet -b topic/three
202echo "another readme" > docs/readme.md
203git commit --quiet -am "another readme"
204git push --quiet origin topic/three
205
206printf 'i\n' > "$w/answers"
207valley review topic/three < "$w/answers" > "$w/integrate.out" 2>&1
208grep -q '^topic/three: \[a\]sk, \[i\]ntegrate, \[r\]eject, \[s\]kip? ' "$w/integrate.out"
209grep -q 'integrated topic/three: main is now' "$w/integrate.out"
210if [ "$(git -C "$origin" rev-parse refs/heads/main)" = "$moved" ]; then
211 echo "valley-request: [i]ntegrate did not move main" >&2
212 exit 1
213fi
214
215# The request filed in 1 is still pending: nothing here consumes it, which
216# is the integrator's own step.
217git -C "$origin" rev-parse --verify --quiet \
218 refs/the-valley/integration-requests/main/topic-one > /dev/null
219
220# No throwaway worktree outlived any of it: only the checkout is left.
221worktrees="$(git worktree list --porcelain | grep -c '^worktree ' || true)"
222if [ "$worktrees" != 1 ]; then
223 echo "valley-request: a throwaway worktree was left behind" >&2
224 git worktree list >&2
225 exit 1
226fi
227
228echo "every scenario held"
229{
230 cat "$w/ask.out"
231 printf '\n--- the scratch origin, after all of it ---\n'
232 git -C "$origin" for-each-ref --format='%(objectname) %(refname)'
233} > "$out"