gunk-dev/the-valley

protect-e2e.sh

source076c2e6e73f4main's published tip history
46.2 KiB
1# shellcheck shell=bash
2# Paths and fixtures come from the derivation environment.
3# shellcheck disable=SC2154
4export HOME="$TMPDIR"
5export GIT_CONFIG_NOSYSTEM=1
6git config --global user.name valley-check
7git config --global user.email valley-check@localhost
8git config --global init.defaultBranch main
9git config --global advice.detachedHead false
10cd "$TMPDIR" || exit 1
11
12# The keys this host accepts evidence from, as a controller would: the
13# signer's, and nobody else's.
14ssh-keygen -q -t ed25519 -N "" -C signer -f "$TMPDIR/signer"
15ssh-keygen -q -t ed25519 -N "" -C other -f "$TMPDIR/other"
16attest key --key "$TMPDIR/signer" --name signer.valley.invalid/attestations > "$TMPDIR/known_signers"
17
18# A bare repo wired as valley-init wires it, with the hook followed from
19# the rendered init script. Every project the host serves gets the hook,
20# protected or not. The one edit is where the verifier keys are, which a
21# host keeps under /var/lib and this sandbox cannot; open's points at no
22# file at all, as a host that names no keys would.
23serve() {
24 local hook keys="$2"
25 hook="$(grep -o "/nix/store/[^ ]*-valley-protect-$1" "$initScriptPath" | head -n1)"
26 test -x "$hook"
27 grep -qF -- '--known-signers /var/lib/valley-instance/known_signers' "$hook"
28 git init --quiet --bare "$1.git"
29 sed "s|/var/lib/valley-instance/known_signers|$keys|" "$hook" > "$1.git/hooks/pre-receive"
30 chmod +x "$1.git/hooks/pre-receive"
31}
32serve sealed "$TMPDIR/known_signers"
33serve guarded "$TMPDIR/known_signers"
34serve released "$TMPDIR/known_signers"
35serve open "$TMPDIR/no_such_keys"
36
37# Pushing as a principal is pushing with the tag on; pushing
38# with an untagged key is pushing with it off. That sshd sets
39# the tag from a key's entry, and nothing else does, is what
40# ssh-e2e checks against a real sshd.
41as() {
42 local who="$1"
43 shift
44 if [ "$who" = anonymous ]; then
45 env -u VALLEY_PRINCIPAL "$@"
46 else
47 VALLEY_PRINCIPAL="$who" "$@"
48 fi
49}
50refs() { git -C "$TMPDIR/$1.git" for-each-ref --format='%(refname)'; }
51has_ref() {
52 if ! refs "$1" | grep -qx "$2"; then
53 echo "protect-e2e: $1 has no $2" >&2
54 refs "$1" >&2
55 exit 1
56 fi
57}
58lacks_ref() {
59 if refs "$1" | grep -qx "$2"; then
60 echo "protect-e2e: $1 kept $2 through a rejected push" >&2
61 exit 1
62 fi
63}
64# refused <file> <what> <command…> — the push must fail.
65refused() {
66 local err="$1" what="$2"
67 shift 2
68 if "$@" 2> "$err"; then
69 echo "protect-e2e: $what" >&2
70 exit 1
71 fi
72}
73
74git init --quiet work
75cd work || exit 1
76git commit --quiet --allow-empty -m one
77for name in sealed guarded released open; do
78 git remote add "$name" "$TMPDIR/$name.git"
79done
80
81# ----------------------------------------------------------------------
82# Protected refs.
83
84# A non-writer cannot create a protected ref, and the
85# rejection says who was pushing, what they were writing,
86# and who may.
87refused denied.err "a non-writer wrote a protected ref" \
88 as contributor git push --quiet guarded main
89grep -q 'contributor' denied.err
90grep -q 'refs/heads/main' denied.err
91grep -q 'writers: integrator' denied.err
92lacks_ref guarded refs/heads/main
93
94# An untagged key is nobody, and nobody is not a writer.
95refused untagged.err "an untagged key wrote a protected ref" \
96 as anonymous git push --quiet guarded main
97grep -q '<untagged key>' untagged.err
98lacks_ref guarded refs/heads/main
99
100# Topic branches are open to the same non-writer, including a
101# branch some other project's declaration protects.
102git branch idea/one
103git tag v1
104git branch release/1.0
105as contributor git push --quiet guarded idea/one release/1.0
106has_ref guarded refs/heads/idea/one
107has_ref guarded refs/heads/release/1.0
108
109# The same ref is protected where a declaration says so: the
110# glob in released's set matches it.
111refused glob.err "a glob in the protected set did not match" \
112 as contributor git push --quiet released release/1.0
113grep -q 'refs/heads/release/1.0' glob.err
114lacks_ref released refs/heads/release/1.0
115
116# The writer creates it, and updates it.
117as integrator git push --quiet guarded main
118has_ref guarded refs/heads/main
119git commit --quiet --allow-empty -m two
120as integrator git push --quiet guarded main
121second="$(git rev-parse HEAD)"
122[ "$(git -C "$TMPDIR/guarded.git" rev-parse main)" = "$second" ]
123
124# A delete is a write.
125refused deleted.err "a non-writer deleted a protected ref" \
126 as contributor git push --quiet --delete guarded main
127grep -q 'refs/heads/main' deleted.err
128has_ref guarded refs/heads/main
129
130# pre-receive answers for the whole push: an open ref
131# travelling with a protected one lands only if the
132# protected one does, atomic push or not.
133git commit --quiet --allow-empty -m three
134git branch idea/two
135for mode in --atomic --no-atomic; do
136 refused mixed.err "a protected ref rode in on a $mode push" \
137 as contributor git push --quiet "$mode" guarded idea/two main
138 grep -q 'refs/heads/main' mixed.err
139 lacks_ref guarded refs/heads/idea/two
140 [ "$(git -C "$TMPDIR/guarded.git" rev-parse main)" = "$second" ]
141done
142
143# The norm, on a project that declares no writer at all: the
144# protected ref takes no push from anyone. Even the principal
145# that writes guarded's main is refused here, and told how a
146# change lands instead of being told who may.
147refused sealed.err "a protected ref with no declared writer took a push" \
148 as integrator git push --quiet sealed main
149grep -q 'integrator' sealed.err
150grep -q 'no writer is declared' sealed.err
151grep -q 'integration request' sealed.err
152lacks_ref sealed refs/heads/main
153
154# A project with no protection block protects nothing: its
155# main is a branch like any other. Everything else the policy
156# says still holds there, as the sections below show.
157as contributor git push --quiet open main
158has_ref open refs/heads/main
159
160# ----------------------------------------------------------------------
161# A symbolic ref takes no push. One cannot be pushed, but one
162# can exist on the server, and git would write the ref it
163# points at: here, a topic-branch name standing for the
164# protected main. Its writer is refused too.
165git -C "$TMPDIR/guarded.git" symbolic-ref refs/heads/alias refs/heads/main
166for who in contributor integrator; do
167 refused symref.err "$who wrote through a symbolic ref" \
168 as "$who" git push --quiet --force guarded HEAD:refs/heads/alias
169 grep -q 'symbolic ref' symref.err
170 [ "$(git -C "$TMPDIR/guarded.git" rev-parse main)" = "$second" ]
171done
172git -C "$TMPDIR/guarded.git" symbolic-ref --delete refs/heads/alias
173
174# git also reads a loose ref holding "ref:" followed by a tab, or by
175# nothing, as a symbolic ref, and a filesystem symlink too. Each is refused
176# the same way, to main's own writer, who could otherwise write main
177# through it.
178heads="$TMPDIR/guarded.git/refs/heads"
179mkdir -p "$heads"
180printf 'ref:\trefs/heads/main\n' > "$heads/tabbed"
181printf 'ref:refs/heads/main\n' > "$heads/tight"
182ln -s refs/heads/main "$heads/linked"
183for alias in tabbed tight linked; do
184 refused symref.err "integrator wrote through the symbolic ref $alias" \
185 as integrator git push --quiet --force guarded "HEAD:refs/heads/$alias"
186 grep -q 'symbolic ref' symref.err
187 [ "$(git -C "$TMPDIR/guarded.git" rev-parse main)" = "$second" ]
188done
189rm "$heads/tabbed" "$heads/tight" "$heads/linked"
190
191# ----------------------------------------------------------------------
192# Namespaces no push may write, on a protected project and an
193# unprotected one, by the protected ref's own writer.
194
195# Tags open only through a named grant, and guarded has none.
196refused tag.err "a tag was pushed that no grant opens" \
197 as integrator git push --quiet guarded v1
198grep -q 'refs/tags/ opens only through a named grant' tag.err
199grep -q 'no grant of guarded' tag.err
200lacks_ref guarded refs/tags/v1
201
202# Notes, replacement refs and the valley's own namespace are
203# closed outright, wherever they are pushed and by whom.
204git notes add -m "a note" HEAD
205git commit --quiet --allow-empty -m decoy
206replaced="refs/replace/$(git rev-parse HEAD~1)"
207git update-ref "$replaced" HEAD
208outcome=refs/the-valley/integration-outcomes/main/idea
209for project in guarded open; do
210 for who in contributor integrator; do
211 refused notes.err "$who pushed a notes ref to $project" \
212 as "$who" git push --quiet "$project" refs/notes/commits
213 grep -q 'refs/notes/ is closed to every push' notes.err
214 refused replace.err "$who pushed a replacement ref to $project" \
215 as "$who" git push --quiet "$project" "$replaced:$replaced"
216 grep -q 'refs/replace/ is closed to every push' replace.err
217 refused outcome.err "$who pushed an integration outcome to $project" \
218 as "$who" git push --quiet "$project" "HEAD:$outcome"
219 grep -q 'refs/the-valley/integration-outcomes/ is written only by the machinery' outcome.err
220 done
221 lacks_ref "$project" refs/notes/commits
222 lacks_ref "$project" "$replaced"
223 lacks_ref "$project" "$outcome"
224done
225git update-ref -d "$replaced"
226git reset --quiet --hard HEAD~1
227
228# A grant opens what it names to whom it names, and nothing
229# more. released grants release tags to the integrator
230# principal: that principal pushes one, nobody else does, and
231# a tag outside the pattern stays closed to it too.
232git tag release/1.0
233tag=refs/tags/release/1.0
234refused opened.err "a grant opened a ref to a principal it does not name" \
235 as contributor git push --quiet released "$tag:$tag"
236grep -q 'the grant release-tags of released opens this ref to integrator only' opened.err
237lacks_ref released "$tag"
238as integrator git push --quiet released "$tag:$tag"
239has_ref released "$tag"
240refused unopened.err "a grant opened a ref outside its pattern" \
241 as integrator git push --quiet released v1
242grep -q 'no grant of released' unopened.err
243lacks_ref released refs/tags/v1
244
245# ----------------------------------------------------------------------
246# A ref named like another ref takes no create and no update. When a ref
247# is missing, git reads, fetches and pushes to refs/, refs/tags/,
248# refs/heads/ or refs/remotes/ followed by its name instead, so a branch,
249# tag or other ref named that way could stand in for a ref the valley
250# reads. Each shape is refused to an open project's contributor and to
251# the writer of guarded's main, and no ref is made.
252read_exact() { git -C "$TMPDIR/$1.git" show-ref --verify --hash "$2"; }
253lookalikes=(
254 refs/heads/refs/the-valley/knots
255 refs/heads/refs/heads/main
256 refs/tags/refs/heads/main
257 refs/refs/the-valley/signers
258 refs/remotes/refs/heads/main
259)
260before="$(git rev-parse HEAD)"
261git commit --quiet --allow-empty -m lookalike
262for pair in open:contributor guarded:integrator; do
263 project="${pair%%:*}" who="${pair#*:}"
264 for ref in "${lookalikes[@]}"; do
265 refused lookalike.err "$who created $ref on $project" \
266 as "$who" git push --quiet "$project" "HEAD:$ref"
267 grep -qF "valley: $who may not create $ref — no push may create or update a ref named" lookalike.err
268 lacks_ref "$project" "$ref"
269 done
270done
271
272# Names that hold refs anywhere else are ordinary: branches pushed by a
273# contributor, and a release tag pushed under released's grant.
274for branch in fix-refs a/refs/b a/refs/heads/main; do
275 as contributor git push --quiet open "HEAD:refs/heads/$branch"
276 has_ref open "refs/heads/$branch"
277done
278tag=refs/tags/release/refs/heads/main
279git update-ref "$tag" HEAD
280as integrator git push --quiet released "$tag:$tag"
281has_ref released "$tag"
282
283# A host may hold such refs already, made before the hook refused them;
284# they are written here on the host. A push updates none of them, and
285# each is left where it was.
286for ref in "${lookalikes[@]}"; do
287 git -C "$TMPDIR/open.git" update-ref "$ref" "$before"
288done
289for ref in "${lookalikes[@]}"; do
290 refused lookalike.err "contributor updated $ref" \
291 as contributor git push --quiet --force open "HEAD:$ref"
292 grep -qF "valley: contributor may not update $ref — no push may create or update a ref named" lookalike.err
293 [ "$(read_exact open "$ref")" = "$before" ]
294done
295
296# The one-off report docs/guide.md gives lists each of them, and none of
297# the ordinary names.
298git -C "$TMPDIR/open.git" for-each-ref --format='%(refname)' \
299 refs/refs/ refs/heads/refs/ refs/tags/refs/ refs/remotes/refs/ > reported.txt
300printf '%s\n' "${lookalikes[@]}" | sort > expected.txt
301if ! sort reported.txt | cmp -s - expected.txt; then
302 echo "protect-e2e: the report of refs named like other refs is wrong" >&2
303 cat reported.txt >&2
304 exit 1
305fi
306
307# A delete is decided by the namespace's own rule: a contributor deletes
308# the branches. No grant of open opens the others, and the operator
309# deletes those on the host.
310as contributor git push --quiet --delete open refs/heads/refs/the-valley/knots refs/heads/refs/heads/main
311lacks_ref open refs/heads/refs/the-valley/knots
312lacks_ref open refs/heads/refs/heads/main
313refused lookalike.err "contributor deleted a tag no grant opens" \
314 as contributor git push --quiet --delete open refs/tags/refs/heads/main
315grep -q 'refs/tags/ opens only through a named grant' lookalike.err
316for ref in refs/tags/refs/heads/main refs/refs/the-valley/signers refs/remotes/refs/heads/main; do
317 git -C "$TMPDIR/open.git" update-ref -d "$ref"
318done
319for branch in fix-refs a/refs/b a/refs/heads/main; do
320 as contributor git push --quiet --delete open "refs/heads/$branch"
321done
322git update-ref -d "$tag"
323git reset --quiet --hard "$before"
324
325# ----------------------------------------------------------------------
326# Integration requests take writes only from a holder of the
327# approve grant. Pushing topic branches does not make a
328# principal one, and neither does being a declared writer.
329request=refs/the-valley/integration-requests/main/idea
330for who in contributor integrator anonymous; do
331 refused request.err "$who filed a request without the approve grant" \
332 as "$who" git push --quiet guarded "idea/one:$request"
333 grep -q 'refs/the-valley/integration-requests/' request.err
334 grep -q 'approve grant (also accepted as request)' request.err
335 lacks_ref guarded "$request"
336done
337
338# Each holder files one, replaces it with a head that does not
339# descend from the first, and withdraws it: requester, declared
340# under both names of the approve grant, and approver, declared
341# under approve alone. In between, a principal without the grant
342# can neither replace nor withdraw what the holder filed.
343for holder in requester approver; do
344 git commit --quiet --allow-empty -m "first request"
345 as "$holder" git push --quiet guarded "HEAD:$request"
346 filed="$(git rev-parse HEAD)"
347 git reset --quiet --hard HEAD~1
348 git commit --quiet --allow-empty -m "second request"
349 refused replaced.err "a principal without the grant replaced $holder's request" \
350 as contributor git push --quiet --force guarded "HEAD:$request"
351 grep -q 'approve grant (also accepted as request)' replaced.err
352 refused withdrawn.err "a principal without the grant withdrew $holder's request" \
353 as contributor git push --quiet --delete guarded "$request"
354 grep -q 'approve grant (also accepted as request)' withdrawn.err
355 [ "$(git -C "$TMPDIR/guarded.git" rev-parse "$request")" = "$filed" ]
356 as "$holder" git push --quiet --force guarded "HEAD:$request"
357 [ "$(git -C "$TMPDIR/guarded.git" rev-parse "$request")" = "$(git rev-parse HEAD)" ]
358 as "$holder" git push --quiet --delete guarded "$request"
359 lacks_ref guarded "$request"
360 git reset --quiet --hard HEAD~1
361done
362
363# Where protection covers the request namespace too, each rule
364# still applies and neither stands in for the other: released's
365# writer lacks the grant, and the holder of the grant is not
366# released's writer.
367refused overlap-writer.err "a writer filed a request without the grant" \
368 as integrator git push --quiet released "idea/one:$request"
369grep -q 'approve grant (also accepted as request)' overlap-writer.err
370refused overlap-holder.err "an approve holder wrote a protected request ref" \
371 as requester git push --quiet released "idea/one:$request"
372grep -q 'a protected ref of released' overlap-holder.err
373lacks_ref released "$request"
374
375# The grant is the same on a project that declares no
376# protection at all.
377refused open-request.err "a request was filed on an unprotected project without the grant" \
378 as contributor git push --quiet open "idea/one:$request"
379as requester git push --quiet open "idea/one:$request"
380has_ref open "$request"
381
382# ----------------------------------------------------------------------
383# A change record moves only by the rows of the hook's table
384# (valleyhook/record.go), here the rows that change its state. Each is
385# pushed as the change's owner or the holder of the approve grant, and
386# each refusal leaves the record where it was. A principal that is
387# neither may not move it at all.
388change_id=nxvpwlkmqtsrzyoukklnpmqrstuvwxyz
389record_ref="refs/the-valley/changes/main/$change_id"
390# record_json <state> <seq> <active> <head…>: the record of the change,
391# owned by contributor, with one version per head.
392record_json() {
393 local state="$1" seq="$2" active="$3" head n=0 versions=""
394 shift 3
395 for head in "$@"; do
396 n=$((n + 1))
397 [ -z "$versions" ] || versions="$versions, "
398 versions="$versions{\"n\": $n, \"head\": \"git-sha1:$head\", \"title\": \"v$n\", \"branch\": \"topic\", \"uploader\": \"contributor\"}"
399 done
400 printf '{"schema": "valley-change-record/v1", "change_id": "%s", "target": "main", "owner": "contributor", "state": "%s", "seq": %s, "versions": [%s], "active": %s}\n' \
401 "$change_id" "$state" "$seq" "$versions" "$active"
402}
403# record_commit <json> <parent…>: a record commit holding the JSON.
404record_commit() {
405 local json="$1" blob tree parent
406 local -a parents=()
407 shift
408 for parent in "$@"; do parents+=(-p "$parent"); done
409 blob="$(printf '%s' "$json" | git hash-object -w --stdin)"
410 tree="$(printf '100644 blob %s\trecord.json\n' "$blob" | git mktree)"
411 git commit-tree "$tree" "${parents[@]}" -m 'a change record'
412}
413record_at() {
414 if [ "$(git -C "$TMPDIR/open.git" rev-parse "$record_ref")" != "$1" ]; then
415 echo "protect-e2e: the change record is not where $2 left it" >&2
416 exit 1
417 fi
418}
419h1="$(git commit-tree "HEAD^{tree}" -p HEAD -m 'the change, v1')"
420h2="$(git commit-tree "HEAD^{tree}" -p HEAD -m 'the change, v2')"
421
422created="$(record_commit "$(record_json ready 1 1 "$h1")" "$h1")"
423as contributor git push --quiet open "$created:$record_ref"
424record_at "$created" "the create"
425
426# unready: ready to draft. A draft is not made a draft again.
427drafted="$(record_commit "$(record_json draft 2 1 "$h1")" "$created")"
428refused unready.err "a principal neither owner nor holder unreadied a change" \
429 as integrator git push --quiet open "$drafted:$record_ref"
430grep -q "only the change's owner, contributor" unready.err
431as contributor git push --quiet open "$drafted:$record_ref"
432record_at "$drafted" "unready"
433again="$(record_commit "$(record_json draft 3 1 "$h1")" "$drafted")"
434refused redraft.err "a draft was made a draft again" \
435 as contributor git push --quiet open "$again:$record_ref"
436grep -q 'the change is draft already' redraft.err
437record_at "$drafted" "unready"
438
439# re-ready at the same version, by the operator, and unready once more.
440# It changes the state and the seq by one, and is the owner's or the
441# operator's to make.
442resumed="$(record_commit "$(record_json ready 3 1 "$h1")" "$drafted")"
443refused reready.err "a principal neither owner nor holder readied a draft" \
444 as integrator git push --quiet open "$resumed:$record_ref"
445grep -q "only the change's owner, contributor" reready.err
446stuck="$(record_commit "$(record_json ready 2 1 "$h1")" "$drafted")"
447refused reready-seq.err "a draft was readied with its seq left as it was" \
448 as contributor git push --quiet open "$stuck:$record_ref"
449grep -q 'its seq is 2' reready-seq.err
450record_at "$drafted" "unready"
451as requester git push --quiet open "$resumed:$record_ref"
452record_at "$resumed" "a re-ready at the same version"
453paused="$(record_commit "$(record_json draft 4 1 "$h1")" "$resumed")"
454as requester git push --quiet open "$paused:$record_ref"
455record_at "$paused" "the operator's unready"
456
457# re-ready at a new version, which needs a new head.
458same="$(record_commit "$(record_json ready 5 2 "$h1" "$h1")" "$paused" "$h1")"
459refused samehead.err "a draft was readied at a new version at its active head" \
460 as contributor git push --quiet open "$same:$record_ref"
461grep -q 'a new version needs a new head' samehead.err
462record_at "$paused" "the operator's unready"
463versioned="$(record_commit "$(record_json ready 5 2 "$h1" "$h2")" "$paused" "$h2")"
464as contributor git push --quiet open "$versioned:$record_ref"
465record_at "$versioned" "a re-ready at a new version"
466
467# withdraw, from a draft. A withdrawn change never comes back as a draft,
468# by its owner or by the operator.
469redrafted="$(record_commit "$(record_json draft 6 2 "$h1" "$h2")" "$versioned")"
470as contributor git push --quiet open "$redrafted:$record_ref"
471gone="$(record_commit "$(record_json withdrawn 7 2 "$h1" "$h2")" "$redrafted")"
472refused withdraw-draft.err "a principal neither owner nor holder withdrew a draft" \
473 as integrator git push --quiet open "$gone:$record_ref"
474grep -q "only the change's owner, contributor" withdraw-draft.err
475record_at "$redrafted" "an unready of the new version"
476as contributor git push --quiet open "$gone:$record_ref"
477record_at "$gone" "a withdrawal from a draft"
478back="$(record_commit "$(record_json draft 8 2 "$h1" "$h2")" "$gone")"
479for who in contributor requester; do
480 refused withdrawn-draft.err "$who made a withdrawn change a draft" \
481 as "$who" git push --quiet open "$back:$record_ref"
482 grep -q 'never as a draft' withdrawn-draft.err
483 record_at "$gone" "a withdrawal from a draft"
484done
485
486# re-ready a withdrawn change at the version it was withdrawn at, which
487# keeps the active version.
488earlier="$(record_commit "$(record_json ready 8 1 "$h1" "$h2")" "$gone")"
489refused revive-active.err "a withdrawn change was readied at another version" \
490 as contributor git push --quiet open "$earlier:$record_ref"
491grep -q 'changes active from 2 to 1' revive-active.err
492record_at "$gone" "a withdrawal from a draft"
493revived="$(record_commit "$(record_json ready 8 2 "$h1" "$h2")" "$gone")"
494as contributor git push --quiet open "$revived:$record_ref"
495record_at "$revived" "a re-ready from withdrawn at the same version"
496
497# approver, declared under the approve grant's new name alone, moves
498# another's record as requester does: it withdraws the change, and deletes
499# the record, which the change's owner may not.
500refused owner-delete.err "the owner deleted a change record" \
501 as contributor git push --quiet --delete open "$record_ref"
502grep -q 'only a principal holding the approve grant (also accepted as request) may delete' owner-delete.err
503record_at "$revived" "a refused delete"
504retired="$(record_commit "$(record_json withdrawn 9 2 "$h1" "$h2")" "$revived")"
505as approver git push --quiet open "$retired:$record_ref"
506record_at "$retired" "a withdrawal under the approve grant"
507as approver git push --quiet --delete open "$record_ref"
508lacks_ref open "$record_ref"
509
510# ----------------------------------------------------------------------
511# The restack grant: the host restacker's one record write. restacker holds
512# it alone, and requester holds it beside the approve grant. Neither owns
513# the change, which contributor does. A holder may add exactly one version
514# to the change while it is ready, on the active version's branch and under
515# its title, naming itself as the uploader. Every other write the grant
516# would have to authorise is refused, and leaves the record where it was.
517restack_id=pqrstuvwxyzklmnopqrstuvwxyzklmno
518restack_ref="refs/the-valley/changes/main/$restack_id"
519# restack_json <state> <seq> <active> <version…>: the record of the change,
520# owned by contributor. Each version is "<head>|<uploader>|<title>|<branch>".
521restack_json() {
522 local state="$1" seq="$2" active="$3" spec head who title branch n=0 versions=""
523 shift 3
524 for spec in "$@"; do
525 IFS='|' read -r head who title branch <<< "$spec"
526 n=$((n + 1))
527 [ -z "$versions" ] || versions="$versions, "
528 versions="$versions{\"n\": $n, \"head\": \"git-sha1:$head\", \"title\": \"$title\", \"branch\": \"$branch\", \"uploader\": \"$who\"}"
529 done
530 printf '{"schema": "valley-change-record/v1", "change_id": "%s", "target": "main", "owner": "contributor", "state": "%s", "seq": %s, "versions": [%s], "active": %s}\n' \
531 "$restack_id" "$state" "$seq" "$versions" "$active"
532}
533restack_at() {
534 if [ "$(git -C "$TMPDIR/open.git" rev-parse "$restack_ref")" != "$1" ]; then
535 echo "protect-e2e: the restacked change's record is not where $2 left it" >&2
536 exit 1
537 fi
538}
539# restack_refused <file> <what> <commit> [<who>]: a push of the record
540# commit is refused, and the record stays where it was.
541restack_refused() {
542 local before
543 before="$(git -C "$TMPDIR/open.git" rev-parse "$restack_ref")"
544 refused "$1" "$2" as "${4:-restacker}" git push --quiet open "$3:$restack_ref"
545 restack_at "$before" "a refused push: $2"
546}
547r1="$(git commit-tree "HEAD^{tree}" -p HEAD -m 'the restacked change, v1')"
548r2="$(git commit-tree "HEAD^{tree}" -p HEAD -m 'the restacked change, rebased')"
549r3="$(git commit-tree "HEAD^{tree}" -p HEAD -m 'the restacked change, rebased again')"
550v1="$r1|contributor|the change|topic"
551at_r2="$r2|restacker|the change|topic"
552
553rcreated="$(record_commit "$(restack_json ready 1 1 "$v1")" "$r1")"
554as contributor git push --quiet open "$rcreated:$restack_ref"
555restack_at "$rcreated" "the create"
556
557# A principal without the grant may not add the version, and neither may a
558# key that names no principal.
559appended="$(record_commit "$(restack_json ready 2 2 "$v1" "$at_r2")" "$rcreated" "$r2")"
560for who in integrator anonymous; do
561 restack_refused nogrant.err "$who added a version to another's change without the restack grant" "$appended" "$who"
562done
563grep -q 'a key that names no principal' nogrant.err
564refused nogrant.err "integrator added a version without the grant" \
565 as integrator git push --quiet open "$appended:$restack_ref"
566grep -q "only the change's owner, contributor, or a principal holding the approve grant (also accepted as request)" nogrant.err
567
568# Each way to write more than the one version is refused.
569two="$(record_commit "$(restack_json ready 2 3 "$v1" "$at_r2" "$r3|restacker|the change|topic")" "$rcreated" "$r3")"
570restack_refused two.err "a holder added two versions" "$two"
571grep -q 'a new version adds one version to the 1 before it, and this record holds 3' two.err
572rewritten="$(record_commit "$(restack_json ready 2 2 "$r1|contributor|rewritten|topic" "$at_r2")" "$rcreated" "$r2")"
573restack_refused rewritten.err "a holder changed an earlier version" "$rewritten"
574grep -q 'it changes version 1' rewritten.err
575owned="$(record_commit "$(restack_json ready 2 2 "$v1" "$at_r2" | sed 's/"owner": "contributor"/"owner": "restacker"/')" "$rcreated" "$r2")"
576restack_refused owned.err "a holder changed the owner" "$owned"
577grep -q 'changes the owner from contributor to restacker' owned.err
578retargeted="$(record_commit "$(restack_json ready 2 2 "$v1" "$at_r2" | sed 's/"target": "main"/"target": "release"/')" "$rcreated" "$r2")"
579restack_refused retargeted.err "a holder changed the target" "$retargeted"
580grep -q 'names the target release' retargeted.err
581retitled="$(record_commit "$(restack_json ready 2 2 "$v1" "$r2|restacker|another title|topic")" "$rcreated" "$r2")"
582restack_refused retitled.err "a holder changed the title" "$retitled"
583grep -q 'is titled "another title"' retitled.err
584moved="$(record_commit "$(restack_json ready 2 2 "$v1" "$r2|restacker|the change|elsewhere")" "$rcreated" "$r2")"
585restack_refused moved.err "a holder moved the change to another branch" "$moved"
586grep -q 'names the branch "elsewhere"' moved.err
587forged="$(record_commit "$(restack_json ready 2 2 "$v1" "$r2|contributor|the change|topic")" "$rcreated" "$r2")"
588restack_refused forged.err "a holder forged the owner as the uploader" "$forged"
589grep -q 'names contributor as its uploader, and restacker is pushing it' forged.err
590for state in draft withdrawn; do
591 stated="$(record_commit "$(restack_json "$state" 2 1 "$v1")" "$rcreated")"
592 restack_refused stated.err "a holder moved a ready change to $state" "$stated"
593 grep -q 'the restack grant changes no state' stated.err
594done
595
596# The one version is added.
597as restacker git push --quiet open "$appended:$restack_ref"
598restack_at "$appended" "a version added under the restack grant"
599
600# Not to a draft, nor to a withdrawn change. The owner's moves between
601# states are as they were.
602paused="$(record_commit "$(restack_json draft 3 2 "$v1" "$at_r2")" "$appended")"
603as contributor git push --quiet open "$paused:$restack_ref"
604on_draft="$(record_commit "$(restack_json ready 4 3 "$v1" "$at_r2" "$r3|restacker|the change|topic")" "$paused" "$r3")"
605restack_refused draft.err "a holder added a version to a draft" "$on_draft"
606grep -q 'the change is draft, and the restack grant adds a version only to a change that is ready' draft.err
607gone_r="$(record_commit "$(restack_json withdrawn 4 2 "$v1" "$at_r2")" "$paused")"
608as contributor git push --quiet open "$gone_r:$restack_ref"
609on_withdrawn="$(record_commit "$(restack_json ready 5 3 "$v1" "$at_r2" "$r3|restacker|the change|topic")" "$gone_r" "$r3")"
610restack_refused withdrawn.err "a holder added a version to a withdrawn change" "$on_withdrawn"
611grep -q 'the change is withdrawn, and the restack grant adds a version only to a change that is ready' withdrawn.err
612back_r="$(record_commit "$(restack_json ready 5 2 "$v1" "$at_r2")" "$gone_r")"
613as contributor git push --quiet open "$back_r:$restack_ref"
614restack_at "$back_r" "the owner's re-ready"
615
616# Deleting stays the approve grant's alone.
617refused rdelete.err "a holder of restack alone deleted a record" \
618 as restacker git push --quiet --delete open "$restack_ref"
619grep -q 'only a principal holding the approve grant (also accepted as request) may delete' rdelete.err
620restack_at "$back_r" "a refused delete"
621
622# Holding restack beside approve leaves approve's powers as they were:
623# requester unreadies the change, which restack alone may not.
624held="$(record_commit "$(restack_json draft 6 2 "$v1" "$at_r2")" "$back_r")"
625as requester git push --quiet open "$held:$restack_ref"
626restack_at "$held" "an unready under the approve grant"
627
628# A holder keeps a named principal's own powers: it creates its own
629# record, and unreadies it.
630own_id=lmnopqrstuvwxyzklmnopqrstuvwxyzk
631own_ref="refs/the-valley/changes/main/$own_id"
632own_json() {
633 printf '{"schema": "valley-change-record/v1", "change_id": "%s", "target": "main", "owner": "restacker", "state": "%s", "seq": %s, "versions": [{"n": 1, "head": "git-sha1:%s", "title": "its own", "branch": "own", "uploader": "restacker"}], "active": 1}\n' \
634 "$own_id" "$1" "$2" "$r1"
635}
636own="$(record_commit "$(own_json ready 1)" "$r1")"
637as restacker git push --quiet open "$own:$own_ref"
638own_draft="$(record_commit "$(own_json draft 2)" "$own")"
639as restacker git push --quiet open "$own_draft:$own_ref"
640[ "$(git -C "$TMPDIR/open.git" rev-parse "$own_ref")" = "$own_draft" ]
641
642# The grant opens nothing on integration requests: restacker files none,
643# and neither replaces nor withdraws the one requester filed.
644refused rrequest.err "a holder of restack alone filed a request" \
645 as restacker git push --quiet open "$r2:refs/the-valley/integration-requests/main/c-$restack_id"
646grep -q 'approve grant (also accepted as request)' rrequest.err
647lacks_ref open "refs/the-valley/integration-requests/main/c-$restack_id"
648filed_request="$(git -C "$TMPDIR/open.git" rev-parse "$request")"
649refused rreplace.err "a holder of restack alone replaced a request" \
650 as restacker git push --quiet --force open "$r2:$request"
651grep -q 'approve grant (also accepted as request)' rreplace.err
652refused rwithdraw.err "a holder of restack alone withdrew a request" \
653 as restacker git push --quiet --delete open "$request"
654grep -q 'approve grant (also accepted as request)' rwithdraw.err
655[ "$(git -C "$TMPDIR/open.git" rev-parse "$request")" = "$filed_request" ]
656
657# ----------------------------------------------------------------------
658# Restack reports, at refs/the-valley/restack/main/<change-id>: a holder of
659# the restack grant creates one, and extends it with a report whose one
660# parent is the one before. Nothing else writes one, and nothing replaces
661# its history or deletes it.
662report_ref="refs/the-valley/restack/main/$restack_id"
663# report_json [<result>]: a report about the restacked change, observed at
664# its record and head now, with its branch at that head.
665report_json() {
666 printf '{"schema": "valley-restack-report/v1", "change_id": "%s", "target": "main", "target_tip": "git-sha1:%s", "operation": "op-1", "result": "%s", "layers": [{"change_id": "%s", "record": "git-sha1:%s", "version": 2, "head": "git-sha1:%s", "branch": "topic", "branch_tip": "git-sha1:%s", "published": null}], "failing_layer": "%s", "conflict_paths": ["readme.txt"], "blocking_requests": [], "refusal": null}\n' \
667 "$restack_id" "$(git -C "$TMPDIR/open.git" rev-parse main)" "${1:-conflict}" "$restack_id" "$held" "$r2" "$r2" "$restack_id"
668}
669# report_commit <json> [<file>] [<parent…>]: a commit holding the JSON as
670# the one file given, report.json by default.
671report_commit() {
672 local json="$1" file="${2:-report.json}" blob tree parent
673 local -a parents=()
674 shift
675 [ "$#" = 0 ] || shift
676 for parent in "$@"; do parents+=(-p "$parent"); done
677 blob="$(printf '%s' "$json" | git hash-object -w --stdin)"
678 tree="$(printf '100644 blob %s\t%s\n' "$blob" "$file" | git mktree)"
679 git commit-tree "$tree" "${parents[@]}" -m 'a restack report'
680}
681report_at() {
682 if [ "$(git -C "$TMPDIR/open.git" rev-parse "$report_ref")" != "$1" ]; then
683 echo "protect-e2e: the restack report is not where $2 left it" >&2
684 exit 1
685 fi
686}
687# report_refused <file> <what> <commit> [<who>] [<ref>]: the push is
688# refused, and the report stays where it was.
689report_refused() {
690 local before
691 before="$(git -C "$TMPDIR/open.git" rev-parse --verify --quiet "$report_ref" || true)"
692 refused "$1" "$2" as "${4:-restacker}" git push --quiet --force open "$3:${5:-$report_ref}"
693 if [ -n "$before" ]; then report_at "$before" "a refused push: $2"; else lacks_ref open "$report_ref"; fi
694}
695report="$(report_json)"
696
697# Who.
698first_report="$(report_commit "$report")"
699for who in contributor integrator anonymous; do
700 report_refused report-who.err "$who wrote a restack report without the grant" "$first_report" "$who"
701 grep -q 'refs/the-valley/restack/ takes writes only from a principal holding the restack grant' report-who.err
702done
703
704# The ref: main alone, and a well-formed change ID that names a change the
705# repository records.
706report_refused report-target.err "a report for another target was written" "$first_report" restacker \
707 "refs/the-valley/restack/release/$restack_id"
708grep -q 'a restack report.s ref is refs/the-valley/restack/main/<change ID>' report-target.err
709lacks_ref open "refs/the-valley/restack/release/$restack_id"
710report_refused report-id.err "a report under a malformed change ID was written" "$first_report" restacker \
711 "refs/the-valley/restack/main/${restack_id:0:31}"
712grep -q '32 letters from k to z' report-id.err
713unknown_id=zyxwvutsrqponmlkzyxwvutsrqponmlk
714report_refused report-unknown.err "a report about a change nobody records was written" \
715 "$(report_commit "${report//$restack_id/$unknown_id}")" restacker "refs/the-valley/restack/main/$unknown_id"
716grep -q "holds no record of the change $unknown_id" report-unknown.err
717
718# The first report has no parent.
719report_refused report-parent.err "a first report with a parent was written" \
720 "$(report_commit "$report" report.json "$r1")"
721grep -q 'first restack report has no parent' report-parent.err
722as restacker git push --quiet open "$first_report:$report_ref"
723report_at "$first_report" "the first report"
724
725# The report itself.
726report_refused report-file.err "a report with a second file was written" \
727 "$(git commit-tree "$(printf '100644 blob %s\treport.json\n100644 blob %s\textra\n' \
728 "$(printf '%s' "$report" | git hash-object -w --stdin)" "$(printf x | git hash-object -w --stdin)" | git mktree)" \
729 -p "$first_report" -m 'a restack report')"
730grep -q "a restack report's tree holds report.json alone" report-file.err
731report_refused report-name.err "a report under another name was written" \
732 "$(report_commit "$report" record.json "$first_report")"
733grep -q 'holds "record.json"' report-name.err
734report_refused report-big.err "an oversized report was written" \
735 "$(report_commit "$report$(head -c 65536 /dev/zero | tr '\0' ' ')" report.json "$first_report")"
736grep -q 'more than the 65536' report-big.err
737report_refused report-unknown-field.err "a report with an unknown field was written" \
738 "$(report_commit "$(printf '%s' "$report" | sed 's/"refusal": null/"refusal": null, "note": "x"/')" report.json "$first_report")"
739grep -q 'which the schema does not have' report-unknown-field.err
740report_refused report-malformed.err "a report that is no JSON was written" \
741 "$(report_commit "conflict in readme.txt" report.json "$first_report")"
742grep -q 'its report.json does not read' report-malformed.err
743report_refused report-other.err "a report about another change was written" \
744 "$(report_commit "$(report_json | sed "s/\"change_id\": \"$restack_id\", \"target\"/\"change_id\": \"$own_id\", \"target\"/")" report.json "$first_report")"
745grep -q "no layer names the change $own_id" report-other.err
746
747# History: a later report extends the one before, and nothing replaces the
748# history or deletes it.
749report_refused report-restart.err "a report that starts the history over was written" \
750 "$(report_commit "$(report_json blocked | sed 's/"failing_layer": "[a-z]*"/"failing_layer": null/')")"
751grep -q 'as its one parent, so a push only adds to the history of reports' report-restart.err
752report_refused report-elsewhere.err "a report on another parent was written" \
753 "$(report_commit "$report" report.json "$r1")"
754grep -q 'as its one parent' report-elsewhere.err
755second_report="$(report_commit "$report" report.json "$first_report")"
756as restacker git push --quiet open "$second_report:$report_ref"
757report_at "$second_report" "the report that extends the first"
758refused report-delete.err "a restack report was deleted" \
759 as restacker git push --quiet --delete open "$report_ref"
760grep -q 'no push deletes one' report-delete.err
761report_at "$second_report" "a refused delete"
762
763# A principal holding both grants writes a report too. This one is a
764# block: it saw the branch missing, and names the request filed above at
765# the head it read.
766blocked_report="$(report_json blocked | sed \
767 -e 's/"failing_layer": "[a-z]*"/"failing_layer": null/' \
768 -e 's/"branch_tip": "[^"]*"/"branch_tip": null/' \
769 -e "s|\"blocking_requests\": \[\]|\"blocking_requests\": [{\"ref\": \"$request\", \"tip\": \"git-sha1:$filed_request\"}]|")"
770grep -q '"branch_tip": null' <<< "$blocked_report"
771grep -q "\"blocking_requests\": \[{\"ref\": \"$request\"" <<< "$blocked_report"
772third_report="$(report_commit "$blocked_report" report.json "$second_report")"
773as requester git push --quiet open "$third_report:$report_ref"
774report_at "$third_report" "a report by a holder of both grants"
775
776# ----------------------------------------------------------------------
777# Attestation refs: anyone may create one, if it is what its
778# name says, and it opens under the keys this host accepts.
779attest run --key "$TMPDIR/signer" --name signer.valley.invalid/attestations \
780 --command ok=true > run.out
781att="$(sed -n 's/^stored \(refs[^ ]*\) -> .*/\1/p' run.out)"
782digest="$(echo "$att" | cut -d/ -f4)"
783keyhash="$(echo "$att" | cut -d/ -f5)"
784as contributor git push --quiet guarded "$att:$att"
785has_ref guarded "$att"
786landed="$(git -C "$TMPDIR/guarded.git" rev-parse "$att")"
787
788# … and nobody may rewrite or drop one, writer or not.
789git update-ref "$att" "$(git rev-parse HEAD^{tree})"
790refused rewritten.err "an attestation ref was rewritten" \
791 as contributor git push --quiet --force guarded "$att:$att"
792grep -q 'create-only' rewritten.err
793grep -q "$att" rewritten.err
794refused rewritten-by-writer.err "a writer rewrote an attestation ref" \
795 as integrator git push --quiet --force guarded "$att:$att"
796grep -q 'create-only' rewritten-by-writer.err
797refused dropped.err "an attestation ref was deleted" \
798 as integrator git push --quiet --delete guarded "$att"
799grep -q 'create-only' dropped.err
800[ "$(git -C "$TMPDIR/guarded.git" rev-parse "$att")" = "$landed" ]
801git update-ref "$att" "$landed"
802
803# Occupation: a name in a create-only namespace is held for
804# good by whoever creates it first, so a name may only hold
805# what it says. Each of these would keep the real signer's
806# evidence out, and each is refused on a project that has
807# none yet.
808other_hash="$(attest key --key "$TMPDIR/other" --name other.valley.invalid/attestations | cut -d+ -f2)"
809occupy() {
810 local name="$1" target="$2" err="$3" why="$4"
811 refused "$err" "an attestation ref took a name it does not hold: $why" \
812 as contributor git push --quiet sealed "$target:refs/the-valley/attestations/$name"
813 lacks_ref sealed "refs/the-valley/attestations/$name"
814}
815# A ref named for the digest alone would block every ref
816# beneath it, and a name that is not hex is no signer's.
817occupy "$digest" "$landed" shape.err "the digest alone"
818grep -q '<digest>/<key hash>' shape.err
819occupy "$digest/$keyhash/extra" "$landed" deep.err "a segment too many"
820occupy "$digest/NOTAHASH" "$landed" nothex.err "a key hash that is not hex"
821# The real signer's note, under another signer's key hash.
822occupy "$digest/$other_hash" "$landed" squat.err "another signer's slot"
823grep -q "no signature under the key hash $other_hash" squat.err
824# The real signer's note, under another tree's digest.
825elsewhere="$(printf '%064d' 7)"
826occupy "$elsewhere/$keyhash" "$landed" subject.err "another tree's slot"
827grep -q 'is about the tree' subject.err
828# Something that is not a tree of notes at all.
829occupy "$elsewhere/$keyhash" HEAD commit.err "a commit"
830grep -q 'points at a commit' commit.err
831# The real signer's note with a line appended that no verifier
832# would read: the signature in it is still genuine, and the
833# note is still unusable, so it may not take the name either.
834git cat-file blob "$landed:ok/statement.note" > genuine.note
835{ cat genuine.note; printf '— x\t AAAAAAA=\n'; } > poisoned.note
836blob="$(git hash-object -w poisoned.note)"
837sub="$(printf '100644 blob %s\tstatement.note\n' "$blob" | git mktree)"
838poisoned="$(printf '040000 tree %s\tok\n' "$sub" | git mktree)"
839occupy "$digest/$keyhash" "$poisoned" poisoned.err "a poisoned note"
840grep -q 'does not open' poisoned.err
841# A tree naming one subtree over and over, which describes more
842# paths than any walk could visit, is refused at a bound.
843wide="$sub"
844for _ in 1 2; do
845 wide="$(for i in $(seq -w 1 64); do printf '040000 tree %s\tt%s\n' "$wide" "$i"; done | git mktree)"
846done
847occupy "$digest/$keyhash" "$wide" wide.err "a tree of shared subtrees"
848grep -q 'entries in all' wide.err
849# The genuine note, at the root of a tree written by hand under the name
850# ok/statement.note, a name with a slash in it, which git never writes and
851# mktree refuses to. The tree is written byte by byte: the mode, the name,
852# a NUL, and the note's object ID in binary.
853flat_file="$TMPDIR/flat-tree"
854printf '100644 %s\0' ok/statement.note > "$flat_file"
855# shellcheck disable=SC2059 # the format is the object ID as \x escapes
856printf "$(git rev-parse "$landed:ok/statement.note" | sed 's/../\\x&/g')" >> "$flat_file"
857flat="$(git hash-object -t tree --literally -w "$flat_file")"
858occupy "$digest/$keyhash" "$flat" flat.err "a note named with a slash"
859grep -q 'it holds an entry named "ok/statement.note", and git writes no name with a slash' flat.err
860# The ref the real evidence wants is still free, and takes it.
861as anonymous git push --quiet sealed "$att:$att"
862has_ref sealed "$att"
863
864# A host that names no keys accepts no attestation at all.
865refused nokeys.err "an attestation was accepted where no key is named" \
866 as anonymous git push --quiet open "$att:$att"
867grep -q 'names no keys it accepts evidence from' nokeys.err
868lacks_ref open "$att"
869
870# ----------------------------------------------------------------------
871# A hook a project composes after the policy runs once the
872# policy accepts, and can only refuse more: released's refuses
873# branches under frozen/.
874git branch frozen/one
875refused frozen.err "the composed hook did not run" \
876 as contributor git push --quiet released frozen/one
877grep -q 'released: refs/heads/frozen/one is frozen' frozen.err
878lacks_ref released refs/heads/frozen/one
879as contributor git push --quiet released idea/one
880has_ref released refs/heads/idea/one
881
882# ----------------------------------------------------------------------
883# A protected project's first main. No push may create it — sealed's main
884# has refused every one above — and the integrator lands only onto a main
885# that exists. So it is seeded on the host, by the git user, from a source
886# whose commit the operator has checked: fetched, compared with the commit
887# expected, and created only if no main exists yet. A local write is not a
888# push, so no hook is asked.
889expected="$(git rev-parse HEAD)"
890git -C "$TMPDIR/sealed.git" fetch --quiet --no-tags "$TMPDIR/work" HEAD
891[ "$(git -C "$TMPDIR/sealed.git" rev-parse FETCH_HEAD)" = "$expected" ]
892git -C "$TMPDIR/sealed.git" update-ref refs/heads/main "$expected" ""
893has_ref sealed refs/heads/main
894# The empty old value makes the seed a creation: over a main that exists,
895# it refuses.
896if git -C "$TMPDIR/sealed.git" update-ref refs/heads/main "$expected~1" "" 2> /dev/null; then
897 echo "protect-e2e: a seed overwrote a main that existed" >&2
898 exit 1
899fi
900[ "$(git -C "$TMPDIR/sealed.git" rev-parse main)" = "$expected" ]
901
902# ----------------------------------------------------------------------
903# The hook valley-init converges on is live. init-e2e holds the rendered
904# init script to installing each project's declared hook over repositories
905# that already exist, over a stand-in for valleyhook. Here the same script
906# wires a repository that already has history, with the real valleyhook,
907# and the hook it installed is pushed to. Relocating the data directory is
908# the only edit to the script: the sandbox cannot write /srv.
909data="$TMPDIR/host/srv/git"
910mkdir -p "$data"
911sed "s|/srv/git|$data|g" "$initScriptPath" > "$TMPDIR/init.sh"
912git init --quiet --bare "$data/guarded.git"
913git clone --quiet "$data/guarded.git" "$TMPDIR/existing"
914git -C "$TMPDIR/existing" commit --quiet --allow-empty -m one
915git -C "$TMPDIR/existing" push --quiet origin main
916before="$(git -C "$TMPDIR/existing" rev-parse HEAD)"
917bash "$TMPDIR/init.sh"
918git -C "$TMPDIR/existing" commit --quiet --allow-empty -m two
919refused existing.err "an untagged key wrote a protected ref of a pre-existing repository" \
920 as anonymous git -C "$TMPDIR/existing" push --quiet origin main
921grep -q '<untagged key>' existing.err
922grep -q 'refs/heads/main' existing.err
923if [ "$(git -C "$data/guarded.git" rev-parse main)" != "$before" ]; then
924 echo "protect-e2e: guarded kept a ref through a rejected push" >&2
925 exit 1
926fi
927as integrator git -C "$TMPDIR/existing" push --quiet origin main
928if [ "$(git -C "$data/guarded.git" rev-parse main)" != "$(git -C "$TMPDIR/existing" rev-parse HEAD)" ]; then
929 echo "protect-e2e: a declared writer could not write a protected ref" >&2
930 exit 1
931fi
932
933touch "$out"