gunk-dev/the-valley

valley-ready.sh

source301e803586a4main's published tip history
17.6 KiB
1# shellcheck shell=bash
2# valley ready and valley withdraw, end to end and unmocked: every record
3# they write is pushed for real to a bare repository wired with the real
4# rendered pre-receive hook, as each principal, and what that repository
5# then holds is read back. What sshd would set from a key's
6# authorized_keys entry, these pushes set in the environment directly, as
7# protect-e2e's do.
8# Paths and fixtures come from the derivation environment.
9# shellcheck disable=SC2154
10set -euo pipefail
11
12w="$TMPDIR/valley-ready"
13mkdir -p "$w"
14export HOME="$w"
15export GIT_CONFIG_NOSYSTEM=1
16git config --global user.name valley-ready-check
17git config --global user.email valley-ready-check@localhost
18git config --global init.defaultBranch main
19git config --global advice.detachedHead false
20
21fail() {
22 printf 'valley-ready: %s\n' "$*" >&2
23 for f in "$w"/out "$w"/err; do
24 if [ -s "$f" ]; then cat "$f" >&2; fi
25 done
26 exit 1
27}
28
29# The project "open" of the protected host: it protects nothing, and the
30# host grants request to the principal named requester. Its hook is the
31# one valley-init installs, followed from the rendered init script.
32origin="$w/project.git"
33hook="$(grep -o '/nix/store/[^ ]*-valley-protect-open' "$initScriptPath" | head -n1)"
34test -x "$hook"
35git init --quiet --bare "$origin"
36cp "$hook" "$origin/hooks/pre-receive"
37chmod +x "$origin/hooks/pre-receive"
38
39repo="$w/project"
40git init --quiet "$repo"
41cd "$repo"
42git remote add origin "$origin"
43echo base > readme
44git add readme
45git commit --quiet -m base
46env -u VALLEY_PRINCIPAL git push --quiet origin main
47
48# as <principal> <command…>: run a command as a principal. The record names
49# the principal git config valley.principal does, and the hook reads the
50# one the push carries; an honest client has the two agree. anonymous is a
51# key that names no principal.
52as() {
53 local who="$1"
54 shift
55 if [ "$who" = anonymous ]; then
56 git config --unset valley.principal || true
57 env -u VALLEY_PRINCIPAL "$@"
58 else
59 git config valley.principal "$who"
60 VALLEY_PRINCIPAL="$who" "$@"
61 fi
62}
63
64# ok <command…>: the command succeeds, and its output is kept.
65ok() {
66 "$@" > "$w/out" 2> "$w/err" || fail "$* failed"
67}
68
69# refused <phrase> <command…>: the command fails, saying the phrase.
70refused() {
71 local says="$1"
72 shift
73 if "$@" > "$w/out" 2> "$w/err"; then
74 fail "$* succeeded where it had to be refused"
75 fi
76 grep -qF -- "$says" "$w/err" || fail "$* was refused without saying: $says"
77}
78
79# record <id> <jq condition…>: origin's record of the change holds each.
80record() {
81 local id="$1" cond
82 shift
83 git -C "$origin" cat-file blob "refs/the-valley/changes/main/$id:record.json" > "$w/record.json" \
84 || fail "origin holds no record of $id"
85 for cond in "$@"; do
86 jq -e "$cond" "$w/record.json" > /dev/null || fail "the record of $id: $cond does not hold"
87 done
88}
89
90# unchanged <id> <command…>: the command leaves origin's record as it was.
91unchanged() {
92 local id="$1" before
93 shift
94 before="$(git -C "$origin" rev-parse "refs/the-valley/changes/main/$id")"
95 "$@"
96 [ "$(git -C "$origin" rev-parse "refs/the-valley/changes/main/$id")" = "$before" ] \
97 || fail "the record of $id moved"
98}
99
100no_records() {
101 [ -z "$(git -C "$origin" for-each-ref refs/the-valley/changes)" ] || fail "$1"
102}
103
104# commit <subject> [<trailer>]: a new commit on the current branch, adding
105# a file of its own, so that any two branches merge cleanly.
106commit() {
107 echo "$1" > "$1.txt"
108 git add "$1.txt"
109 if [ $# -gt 1 ]; then
110 git commit --quiet -m "$1" -m "$2"
111 else
112 git commit --quiet -m "$1"
113 fi
114}
115
116# with_header <id>: HEAD rewritten to carry a change-id header, the way jj
117# writes one, after the committer.
118with_header() {
119 local amended
120 amended="$(git cat-file commit HEAD | sed "/^committer /a change-id $1" |
121 git hash-object -t commit -w --stdin)"
122 git update-ref refs/heads/"$(git symbolic-ref --short HEAD)" "$amended"
123}
124
125# ----------------------------------------------------------------------
126# A head marked ready for the first time is v1 of a new change. Its head
127# carries no change ID, so ready makes one and prints it.
128git checkout --quiet -b topic
129commit one
130first="$(git rev-parse HEAD)"
131refused 'push it there first' as contributor valley ready
132no_records 'a head not on origin was recorded'
133as contributor git push --quiet origin topic
134ok as contributor valley ready
135grep -qE '^project [k-z]{4} v1 ready$' "$w/out" || fail 'ready did not print the handle and v1'
136id="$(sed -n 's/^The change ID \([k-z]*\) is new\..*/\1/p' "$w/out")"
137[ "${#id}" = 32 ] || fail 'ready did not print the change ID it made'
138handle="${id:0:4}"
139grep -qx "project $handle v1 ready" "$w/out" || fail 'the handle is not the ID'"'"'s first four letters'
140record "$id" '.schema == "valley-change-record/v1"' ".change_id == \"$id\"" '.target == "main"' \
141 '.owner == "contributor"' '.state == "ready"' '.seq == 1' '.active == 1' \
142 ".versions == [{n: 1, head: \"git-sha1:$first\", title: \"one\", branch: \"topic\", uploader: \"contributor\"}]"
143# The record commit holds on to the head it records.
144[ "$(git -C "$origin" rev-parse "refs/the-valley/changes/main/$id^1")" = "$first" ] \
145 || fail 'the record commit does not have the head as its parent'
146
147# A second ready at a new head is v2, named by the ID it printed.
148commit two
149second="$(git rev-parse HEAD)"
150as contributor git push --quiet origin topic
151ok as contributor valley ready --change "$id"
152grep -qx "project $handle v2 ready" "$w/out" || fail 'the second ready was not v2'
153record "$id" '.seq == 2' '.active == 2' '.state == "ready"' '.versions | length == 2' \
154 ".versions[0].head == \"git-sha1:$first\"" ".versions[1] == {n: 2, head: \"git-sha1:$second\", title: \"two\", branch: \"topic\", uploader: \"contributor\"}"
155
156# The same head again is no new version.
157unchanged "$id" refused 'a new version needs a new head' as contributor valley ready --change "$id"
158
159# Withdrawn, the change comes back only by a new version.
160ok as contributor valley withdraw "$handle"
161grep -qx "project $handle v2 withdrawn" "$w/out" || fail 'withdraw did not say what it withdrew'
162record "$id" '.state == "withdrawn"' '.seq == 3' '.active == 2' '.versions | length == 2'
163unchanged "$id" refused 'withdrawn already' as contributor valley withdraw "$handle"
164unchanged "$id" refused 'a new version needs a new head' as contributor valley ready --change "$id"
165commit three
166third="$(git rev-parse HEAD)"
167as contributor git push --quiet origin topic
168ok as contributor valley ready --change "$id"
169grep -qx "project $handle v3 ready" "$w/out" || fail 'the revived change was not v3'
170record "$id" '.state == "ready"' '.seq == 4' '.active == 3' ".versions[2].head == \"git-sha1:$third\""
171
172# ----------------------------------------------------------------------
173# Who may write the record. A principal that neither owns the change nor
174# holds the request grant is refused by the hook, whatever the command
175# writes, and so is a key that names no principal.
176commit four
177fourth="$(git rev-parse HEAD)"
178as contributor git push --quiet origin topic
179unchanged "$id" refused "only the change's owner, contributor" as integrator valley ready --change "$id"
180unchanged "$id" refused "only the change's owner, contributor" as integrator valley withdraw "$handle"
181unchanged "$id" refused 'a key that names no principal' env -u VALLEY_PRINCIPAL valley withdraw "$handle"
182refused 'git config valley.principal names none' as anonymous valley ready --change "$id"
183# The operator, who holds the request grant, may add a version and
184# withdraw, and the owner stays the owner.
185ok as requester valley ready --head "$fourth" --change "$id"
186grep -qx "project $handle v4 ready" "$w/out" || fail "the operator's version was not v4"
187record "$id" '.owner == "contributor"' '.versions[3].uploader == "requester"' '.seq == 5'
188ok as requester valley withdraw "$id"
189record "$id" '.state == "withdrawn"' '.seq == 6' '.owner == "contributor"'
190
191# --head marks exactly the head given, which must be on origin. An older
192# head of the branch is on origin, and a commit nobody pushed is not.
193commit five
194unpushed="$(git rev-parse HEAD)"
195unchanged "$id" refused 'push it there first' as contributor valley ready --head "$unpushed" --change "$id"
196git reset --quiet --hard "$fourth"
197
198# ----------------------------------------------------------------------
199# Change identity. A jj change-id header in the head commit names the
200# change; so does a Change-Id trailer in its message. The two disagreeing
201# is refused, and so is a value that is no change ID.
202header_id=zyxwvutsrqponmlkzyxwvutsrqponmlk
203trailer_id=kkkkllllmmmmnnnnooooppppqqqqrrrr
204git checkout --quiet -b from-header main
205commit header
206with_header "$header_id"
207as contributor git push --quiet origin from-header
208ok as contributor valley ready
209grep -qx "project zyxw v1 ready" "$w/out" || fail 'the header did not name the change'
210grep -q 'is new' "$w/out" && fail 'a change ID read from the header was reported as made'
211record "$header_id" '.versions[0].branch == "from-header"'
212
213git checkout --quiet -b from-trailer main
214commit trailer "Change-Id: $trailer_id"
215as contributor git push --quiet origin from-trailer
216ok as contributor valley ready
217grep -qx "project kkkk v1 ready" "$w/out" || fail 'the trailer did not name the change'
218record "$trailer_id" '.versions[0].branch == "from-trailer"'
219
220git checkout --quiet -b disagreeing main
221commit disagreeing "Change-Id: $trailer_id"
222with_header "$header_id"
223as contributor git push --quiet origin disagreeing
224unchanged "$trailer_id" unchanged "$header_id" \
225 refused 'they must agree' as contributor valley ready
226refused 'they must agree' as contributor valley ready --change "$id"
227
228git checkout --quiet -b malformed main
229commit malformed "Change-Id: I0123456789abcdef0123456789abcdef01234567"
230as contributor git push --quiet origin malformed
231refused 'which is not a change ID' as contributor valley ready
232
233# A change is a line of commits from its merge base with main. A merge
234# in that range is refused.
235git checkout --quiet -b merged main
236commit merged
237git merge --quiet --no-ff -m merge from-trailer
238as contributor git push --quiet origin merged
239refused 'include the merge' as contributor valley ready
240
241# ----------------------------------------------------------------------
242# A change is named by its handle wherever a change is taken: status
243# reads the branch its active version names, and withdraw takes a branch
244# name too.
245ok valley status kkkk
246grep -q '^from-trailer: not approved' "$w/out" || fail 'status did not read the change by its handle'
247ok as contributor valley withdraw from-trailer
248grep -qx 'project kkkk v1 withdrawn' "$w/out" || fail 'withdraw did not take a branch name'
249
250# --stamp carries a new change's ID in an unpushed head as a trailer,
251# pushes the branch, and marks it ready. It will not rewrite a head that
252# is on origin in a change that is not the user's.
253git checkout --quiet -b stamped main
254commit stamped
255ok as contributor valley ready --stamp
256stamped_id="$(sed -n 's/^The change ID \([k-z]*\) is new\..*/\1/p' "$w/out")"
257[ "${#stamped_id}" = 32 ] || fail '--stamp made no change ID'
258[ "$(git log -1 --format='%(trailers:key=Change-Id,valueonly)' | head -n1)" = "$stamped_id" ] \
259 || fail '--stamp did not carry the ID in the head'
260[ "$(git rev-parse HEAD)" = "$(git -C "$origin" rev-parse refs/heads/stamped)" ] \
261 || fail '--stamp did not push the stamped head'
262record "$stamped_id" ".versions[0].head == \"git-sha1:$(git rev-parse HEAD)\""
263git checkout --quiet -b pushed main
264commit pushed
265as contributor git push --quiet origin pushed
266refused 'will not rewrite it' as integrator valley ready --stamp
267# A change that has a record already is never stamped, whoever
268# valley.principal says is asking: neither the local branch nor origin's
269# moves.
270git checkout --quiet stamped
271commit restamped
272restamped="$(git rev-parse HEAD)"
273unchanged "$stamped_id" refused 'has a record on origin already' \
274 as contributor valley ready --stamp --change "$stamped_id"
275[ "$(git rev-parse HEAD)" = "$restamped" ] || fail '--stamp rewrote the head of a change with a record'
276[ "$(git -C "$origin" rev-parse refs/heads/stamped)" != "$restamped" ] \
277 || fail '--stamp pushed for a change with a record'
278
279# ----------------------------------------------------------------------
280# A change named by its handle is approved, reviewed and rebased at the
281# head its record names, never at its branch's tip. The record's branch is
282# only where that head is expected to be, and whoever can push the branch
283# can move it, in the same push as the record if they like.
284#
285# approve derives what the change owes from the owning valley's floor,
286# named at policy/valley on main. This floor owes nothing, so approving
287# files a request with no evidence, and no signing key is needed.
288valley="$w/valley.git"
289git init --quiet --bare "$valley"
290git init --quiet "$w/valley-work"
291mkdir -p "$w/valley-work/policy/instance"
292cat > "$w/valley-work/policy/instance/floor.cue" << 'CUE'
293package verification
294
295floor: {
296 checks: {}
297 classes: {}
298 unclassified: {}
299}
300CUE
301git -C "$w/valley-work" add -A
302git -C "$w/valley-work" commit --quiet -m floor
303git -C "$w/valley-work" push --quiet "$valley" HEAD:main
304git checkout --quiet main
305mkdir -p policy
306echo "$valley" > policy/valley
307git add policy/valley
308git commit --quiet -m 'name the owning valley'
309env -u VALLEY_PRINCIPAL git push --quiet origin main
310
311# write_record <json> <parent…>: a record commit holding the JSON, written
312# by hand, the way anyone holding a key could write one.
313write_record() {
314 local json="$1" blob tree parent
315 local -a parents=()
316 shift
317 for parent in "$@"; do parents+=(-p "$parent"); done
318 blob="$(printf '%s\n' "$json" | git hash-object -w --stdin)"
319 tree="$(printf '100644 blob %s\trecord.json\n' "$blob" | git mktree)"
320 git commit-tree "$tree" "${parents[@]}" -m 'a change record'
321}
322request=refs/the-valley/integration-requests/main/victim
323no_request() {
324 if git -C "$origin" rev-parse --verify --quiet "$request" > /dev/null; then
325 fail "$1"
326 fi
327}
328
329# The record names H on victim, and the same push puts B on victim.
330target_id=ppppqqqqrrrrssssttttuuuuvvvvwwww
331git checkout --quiet -b victim main
332commit honest
333honest="$(git rev-parse HEAD)"
334commit substituted
335substituted="$(git rev-parse HEAD)"
336v1="$(jq -cn --arg id "$target_id" --arg head "git-sha1:$honest" '{schema: "valley-change-record/v1",
337 change_id: $id, target: "main", owner: "contributor", state: "ready", seq: 1,
338 versions: [{n: 1, head: $head, title: "honest", branch: "victim", uploader: "contributor"}], active: 1}')"
339r1="$(write_record "$v1" "$honest")"
340as contributor git push --quiet origin "$r1:refs/the-valley/changes/main/$target_id" \
341 "$substituted:refs/heads/victim"
342[ "$(git -C "$origin" rev-parse refs/heads/victim)" = "$substituted" ] || fail 'the branch did not move with the record'
343
344refused "records the head $honest, and origin/victim is at $substituted" as requester valley approve pppp
345no_request 'approve filed a request for a branch tip no version records'
346refused "origin/victim is at $substituted; nothing rebased" as requester valley rebase pppp
347[ "$(git -C "$origin" rev-parse refs/heads/victim)" = "$substituted" ] || fail 'rebase moved the branch'
348# review shows the recorded head, and none of what the branch added.
349printf 's\n' | valley review pppp > "$w/out" 2> "$w/err" || fail 'review of a handle failed'
350grep -q '^change pppp v1 at' "$w/out" || fail 'review did not say which version it shows'
351grep -q ' honest$' "$w/out" || fail 'review did not show the recorded head'
352if grep -q 'substituted' "$w/out"; then fail 'review showed the branch tip, not the recorded head'; fi
353# Rejecting it there deletes nothing while the branch is elsewhere: the
354# refusal names both commits, and the next answer goes to the prompt.
355printf 'r\nyes\ns\n' | as requester valley review pppp > "$w/out" 2> "$w/err" \
356 || fail 'review of a handle failed'
357grep -qF "records the head $honest, and origin/victim is at $substituted; nothing deleted" "$w/err" \
358 || fail 'reject did not refuse a branch whose tip is not the recorded head'
359[ "$(git -C "$origin" rev-parse refs/heads/victim)" = "$substituted" ] \
360 || fail 'reject deleted a branch whose tip is not the recorded head'
361
362# With the branch at the recorded head, approve files exactly that head.
363as contributor git push --quiet --force origin "$honest:refs/heads/victim"
364ok as requester valley approve pppp
365[ "$(git -C "$origin" rev-parse "$request")" = "$honest" ] || fail 'approve did not file the recorded head'
366
367# A second agent acting as the same principal adds v2 at a head that is
368# not on victim, while its record says victim. approve refuses, and the
369# request stays where it was.
370git checkout --quiet -b elsewhere main
371commit elsewhere
372elsewhere="$(git rev-parse HEAD)"
373as contributor git push --quiet origin elsewhere
374v2="$(jq -c --arg head "git-sha1:$elsewhere" '.versions += [{n: 2, head: $head, title: "elsewhere",
375 branch: "victim", uploader: "contributor"}] | .active = 2 | .seq = 2' <<< "$v1")"
376r2="$(write_record "$v2" "$r1" "$elsewhere")"
377as contributor git push --quiet origin "$r2:refs/the-valley/changes/main/$target_id"
378refused "records the head $elsewhere, and origin/victim is at $honest" as requester valley approve pppp
379[ "$(git -C "$origin" rev-parse "$request")" = "$honest" ] || fail 'a redirected version moved the request'
380# With the branch at the recorded head, rejecting it in review deletes it.
381as contributor git push --quiet --force origin "$elsewhere:refs/heads/victim"
382printf 'r\nyes\n' | as requester valley review pppp > "$w/out" 2> "$w/err" \
383 || fail 'rejecting a handle at its recorded head failed'
384grep -qF 'rejected victim: deleted from origin' "$w/out" || fail 'reject did not say it deleted the branch'
385if git -C "$origin" rev-parse --verify --quiet refs/heads/victim > /dev/null; then
386 fail 'reject kept a branch at the recorded head'
387fi
388
389echo 'valley-ready: change records through real pushes passed'
390touch "$out"