gunk-dev/the-valley

protect-e2e.sh

source2359cfca7334main's published tip history
23.5 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# Integration requests take writes only from a holder of the
247# request grant. Pushing topic branches does not make a
248# principal one, and neither does being a declared writer.
249request=refs/the-valley/integration-requests/main/idea
250for who in contributor integrator anonymous; do
251 refused request.err "$who filed a request without the request grant" \
252 as "$who" git push --quiet guarded "idea/one:$request"
253 grep -q 'refs/the-valley/integration-requests/' request.err
254 grep -q 'request grant' request.err
255 lacks_ref guarded "$request"
256done
257
258# The holder files one, replaces it with a head that does not
259# descend from the first, and withdraws it. In between, a
260# principal without the grant can neither replace nor
261# withdraw what the holder filed.
262git commit --quiet --allow-empty -m "first request"
263as requester git push --quiet guarded "HEAD:$request"
264filed="$(git rev-parse HEAD)"
265git reset --quiet --hard HEAD~1
266git commit --quiet --allow-empty -m "second request"
267refused replaced.err "a principal without the grant replaced a request" \
268 as contributor git push --quiet --force guarded "HEAD:$request"
269grep -q 'request grant' replaced.err
270refused withdrawn.err "a principal without the grant withdrew a request" \
271 as contributor git push --quiet --delete guarded "$request"
272grep -q 'request grant' withdrawn.err
273[ "$(git -C "$TMPDIR/guarded.git" rev-parse "$request")" = "$filed" ]
274as requester git push --quiet --force guarded "HEAD:$request"
275[ "$(git -C "$TMPDIR/guarded.git" rev-parse "$request")" = "$(git rev-parse HEAD)" ]
276as requester git push --quiet --delete guarded "$request"
277lacks_ref guarded "$request"
278git reset --quiet --hard HEAD~1
279
280# Where protection covers the request namespace too, each rule
281# still applies and neither stands in for the other: released's
282# writer lacks the grant, and the holder of the grant is not
283# released's writer.
284refused overlap-writer.err "a writer filed a request without the grant" \
285 as integrator git push --quiet released "idea/one:$request"
286grep -q 'request grant' overlap-writer.err
287refused overlap-holder.err "a request holder wrote a protected request ref" \
288 as requester git push --quiet released "idea/one:$request"
289grep -q 'a protected ref of released' overlap-holder.err
290lacks_ref released "$request"
291
292# The grant is the same on a project that declares no
293# protection at all.
294refused open-request.err "a request was filed on an unprotected project without the grant" \
295 as contributor git push --quiet open "idea/one:$request"
296as requester git push --quiet open "idea/one:$request"
297has_ref open "$request"
298
299# ----------------------------------------------------------------------
300# A change record moves only by the rows of the hook's table
301# (valleyhook/record.go), here the rows that change its state. Each is
302# pushed as the change's owner or the holder of the request grant, and
303# each refusal leaves the record where it was. A principal that is
304# neither may not move it at all.
305change_id=nxvpwlkmqtsrzyoukklnpmqrstuvwxyz
306record_ref="refs/the-valley/changes/main/$change_id"
307# record_json <state> <seq> <active> <head…>: the record of the change,
308# owned by contributor, with one version per head.
309record_json() {
310 local state="$1" seq="$2" active="$3" head n=0 versions=""
311 shift 3
312 for head in "$@"; do
313 n=$((n + 1))
314 [ -z "$versions" ] || versions="$versions, "
315 versions="$versions{\"n\": $n, \"head\": \"git-sha1:$head\", \"title\": \"v$n\", \"branch\": \"topic\", \"uploader\": \"contributor\"}"
316 done
317 printf '{"schema": "valley-change-record/v1", "change_id": "%s", "target": "main", "owner": "contributor", "state": "%s", "seq": %s, "versions": [%s], "active": %s}\n' \
318 "$change_id" "$state" "$seq" "$versions" "$active"
319}
320# record_commit <json> <parent…>: a record commit holding the JSON.
321record_commit() {
322 local json="$1" blob tree parent
323 local -a parents=()
324 shift
325 for parent in "$@"; do parents+=(-p "$parent"); done
326 blob="$(printf '%s' "$json" | git hash-object -w --stdin)"
327 tree="$(printf '100644 blob %s\trecord.json\n' "$blob" | git mktree)"
328 git commit-tree "$tree" "${parents[@]}" -m 'a change record'
329}
330record_at() {
331 if [ "$(git -C "$TMPDIR/open.git" rev-parse "$record_ref")" != "$1" ]; then
332 echo "protect-e2e: the change record is not where $2 left it" >&2
333 exit 1
334 fi
335}
336h1="$(git commit-tree "HEAD^{tree}" -p HEAD -m 'the change, v1')"
337h2="$(git commit-tree "HEAD^{tree}" -p HEAD -m 'the change, v2')"
338
339created="$(record_commit "$(record_json ready 1 1 "$h1")" "$h1")"
340as contributor git push --quiet open "$created:$record_ref"
341record_at "$created" "the create"
342
343# unready: ready to draft. A draft is not made a draft again.
344drafted="$(record_commit "$(record_json draft 2 1 "$h1")" "$created")"
345refused unready.err "a principal neither owner nor holder unreadied a change" \
346 as integrator git push --quiet open "$drafted:$record_ref"
347grep -q "only the change's owner, contributor" unready.err
348as contributor git push --quiet open "$drafted:$record_ref"
349record_at "$drafted" "unready"
350again="$(record_commit "$(record_json draft 3 1 "$h1")" "$drafted")"
351refused redraft.err "a draft was made a draft again" \
352 as contributor git push --quiet open "$again:$record_ref"
353grep -q 'the change is draft already' redraft.err
354record_at "$drafted" "unready"
355
356# re-ready at the same version, by the operator, and unready once more.
357# It changes the state and the seq by one, and is the owner's or the
358# operator's to make.
359resumed="$(record_commit "$(record_json ready 3 1 "$h1")" "$drafted")"
360refused reready.err "a principal neither owner nor holder readied a draft" \
361 as integrator git push --quiet open "$resumed:$record_ref"
362grep -q "only the change's owner, contributor" reready.err
363stuck="$(record_commit "$(record_json ready 2 1 "$h1")" "$drafted")"
364refused reready-seq.err "a draft was readied with its seq left as it was" \
365 as contributor git push --quiet open "$stuck:$record_ref"
366grep -q 'its seq is 2' reready-seq.err
367record_at "$drafted" "unready"
368as requester git push --quiet open "$resumed:$record_ref"
369record_at "$resumed" "a re-ready at the same version"
370paused="$(record_commit "$(record_json draft 4 1 "$h1")" "$resumed")"
371as requester git push --quiet open "$paused:$record_ref"
372record_at "$paused" "the operator's unready"
373
374# re-ready at a new version, which needs a new head.
375same="$(record_commit "$(record_json ready 5 2 "$h1" "$h1")" "$paused" "$h1")"
376refused samehead.err "a draft was readied at a new version at its active head" \
377 as contributor git push --quiet open "$same:$record_ref"
378grep -q 'a new version needs a new head' samehead.err
379record_at "$paused" "the operator's unready"
380versioned="$(record_commit "$(record_json ready 5 2 "$h1" "$h2")" "$paused" "$h2")"
381as contributor git push --quiet open "$versioned:$record_ref"
382record_at "$versioned" "a re-ready at a new version"
383
384# withdraw, from a draft. A withdrawn change never comes back as a draft,
385# by its owner or by the operator.
386redrafted="$(record_commit "$(record_json draft 6 2 "$h1" "$h2")" "$versioned")"
387as contributor git push --quiet open "$redrafted:$record_ref"
388gone="$(record_commit "$(record_json withdrawn 7 2 "$h1" "$h2")" "$redrafted")"
389refused withdraw-draft.err "a principal neither owner nor holder withdrew a draft" \
390 as integrator git push --quiet open "$gone:$record_ref"
391grep -q "only the change's owner, contributor" withdraw-draft.err
392record_at "$redrafted" "an unready of the new version"
393as contributor git push --quiet open "$gone:$record_ref"
394record_at "$gone" "a withdrawal from a draft"
395back="$(record_commit "$(record_json draft 8 2 "$h1" "$h2")" "$gone")"
396for who in contributor requester; do
397 refused withdrawn-draft.err "$who made a withdrawn change a draft" \
398 as "$who" git push --quiet open "$back:$record_ref"
399 grep -q 'never as a draft' withdrawn-draft.err
400 record_at "$gone" "a withdrawal from a draft"
401done
402
403# re-ready a withdrawn change at the version it was withdrawn at, which
404# keeps the active version.
405earlier="$(record_commit "$(record_json ready 8 1 "$h1" "$h2")" "$gone")"
406refused revive-active.err "a withdrawn change was readied at another version" \
407 as contributor git push --quiet open "$earlier:$record_ref"
408grep -q 'changes active from 2 to 1' revive-active.err
409record_at "$gone" "a withdrawal from a draft"
410revived="$(record_commit "$(record_json ready 8 2 "$h1" "$h2")" "$gone")"
411as contributor git push --quiet open "$revived:$record_ref"
412record_at "$revived" "a re-ready from withdrawn at the same version"
413
414# ----------------------------------------------------------------------
415# Attestation refs: anyone may create one, if it is what its
416# name says, and it opens under the keys this host accepts.
417attest run --key "$TMPDIR/signer" --name signer.valley.invalid/attestations \
418 --command ok=true > run.out
419att="$(sed -n 's/^stored \(refs[^ ]*\) -> .*/\1/p' run.out)"
420digest="$(echo "$att" | cut -d/ -f4)"
421keyhash="$(echo "$att" | cut -d/ -f5)"
422as contributor git push --quiet guarded "$att:$att"
423has_ref guarded "$att"
424landed="$(git -C "$TMPDIR/guarded.git" rev-parse "$att")"
425
426# … and nobody may rewrite or drop one, writer or not.
427git update-ref "$att" "$(git rev-parse HEAD^{tree})"
428refused rewritten.err "an attestation ref was rewritten" \
429 as contributor git push --quiet --force guarded "$att:$att"
430grep -q 'create-only' rewritten.err
431grep -q "$att" rewritten.err
432refused rewritten-by-writer.err "a writer rewrote an attestation ref" \
433 as integrator git push --quiet --force guarded "$att:$att"
434grep -q 'create-only' rewritten-by-writer.err
435refused dropped.err "an attestation ref was deleted" \
436 as integrator git push --quiet --delete guarded "$att"
437grep -q 'create-only' dropped.err
438[ "$(git -C "$TMPDIR/guarded.git" rev-parse "$att")" = "$landed" ]
439git update-ref "$att" "$landed"
440
441# Occupation: a name in a create-only namespace is held for
442# good by whoever creates it first, so a name may only hold
443# what it says. Each of these would keep the real signer's
444# evidence out, and each is refused on a project that has
445# none yet.
446other_hash="$(attest key --key "$TMPDIR/other" --name other.valley.invalid/attestations | cut -d+ -f2)"
447occupy() {
448 local name="$1" target="$2" err="$3" why="$4"
449 refused "$err" "an attestation ref took a name it does not hold: $why" \
450 as contributor git push --quiet sealed "$target:refs/the-valley/attestations/$name"
451 lacks_ref sealed "refs/the-valley/attestations/$name"
452}
453# A ref named for the digest alone would block every ref
454# beneath it, and a name that is not hex is no signer's.
455occupy "$digest" "$landed" shape.err "the digest alone"
456grep -q '<digest>/<key hash>' shape.err
457occupy "$digest/$keyhash/extra" "$landed" deep.err "a segment too many"
458occupy "$digest/NOTAHASH" "$landed" nothex.err "a key hash that is not hex"
459# The real signer's note, under another signer's key hash.
460occupy "$digest/$other_hash" "$landed" squat.err "another signer's slot"
461grep -q "no signature under the key hash $other_hash" squat.err
462# The real signer's note, under another tree's digest.
463elsewhere="$(printf '%064d' 7)"
464occupy "$elsewhere/$keyhash" "$landed" subject.err "another tree's slot"
465grep -q 'is about the tree' subject.err
466# Something that is not a tree of notes at all.
467occupy "$elsewhere/$keyhash" HEAD commit.err "a commit"
468grep -q 'points at a commit' commit.err
469# The real signer's note with a line appended that no verifier
470# would read: the signature in it is still genuine, and the
471# note is still unusable, so it may not take the name either.
472git cat-file blob "$landed:ok/statement.note" > genuine.note
473{ cat genuine.note; printf '— x\t AAAAAAA=\n'; } > poisoned.note
474blob="$(git hash-object -w poisoned.note)"
475sub="$(printf '100644 blob %s\tstatement.note\n' "$blob" | git mktree)"
476poisoned="$(printf '040000 tree %s\tok\n' "$sub" | git mktree)"
477occupy "$digest/$keyhash" "$poisoned" poisoned.err "a poisoned note"
478grep -q 'does not open' poisoned.err
479# A tree naming one subtree over and over, which describes more
480# paths than any walk could visit, is refused at a bound.
481wide="$sub"
482for _ in 1 2; do
483 wide="$(for i in $(seq -w 1 64); do printf '040000 tree %s\tt%s\n' "$wide" "$i"; done | git mktree)"
484done
485occupy "$digest/$keyhash" "$wide" wide.err "a tree of shared subtrees"
486grep -q 'entries in all' wide.err
487# The ref the real evidence wants is still free, and takes it.
488as anonymous git push --quiet sealed "$att:$att"
489has_ref sealed "$att"
490
491# A host that names no keys accepts no attestation at all.
492refused nokeys.err "an attestation was accepted where no key is named" \
493 as anonymous git push --quiet open "$att:$att"
494grep -q 'names no keys it accepts evidence from' nokeys.err
495lacks_ref open "$att"
496
497# ----------------------------------------------------------------------
498# A hook a project composes after the policy runs once the
499# policy accepts, and can only refuse more: released's refuses
500# branches under frozen/.
501git branch frozen/one
502refused frozen.err "the composed hook did not run" \
503 as contributor git push --quiet released frozen/one
504grep -q 'released: refs/heads/frozen/one is frozen' frozen.err
505lacks_ref released refs/heads/frozen/one
506as contributor git push --quiet released idea/one
507has_ref released refs/heads/idea/one
508
509# ----------------------------------------------------------------------
510# A protected project's first main. No push may create it — sealed's main
511# has refused every one above — and the integrator lands only onto a main
512# that exists. So it is seeded on the host, by the git user, from a source
513# whose commit the operator has checked: fetched, compared with the commit
514# expected, and created only if no main exists yet. A local write is not a
515# push, so no hook is asked.
516expected="$(git rev-parse HEAD)"
517git -C "$TMPDIR/sealed.git" fetch --quiet --no-tags "$TMPDIR/work" HEAD
518[ "$(git -C "$TMPDIR/sealed.git" rev-parse FETCH_HEAD)" = "$expected" ]
519git -C "$TMPDIR/sealed.git" update-ref refs/heads/main "$expected" ""
520has_ref sealed refs/heads/main
521# The empty old value makes the seed a creation: over a main that exists,
522# it refuses.
523if git -C "$TMPDIR/sealed.git" update-ref refs/heads/main "$expected~1" "" 2> /dev/null; then
524 echo "protect-e2e: a seed overwrote a main that existed" >&2
525 exit 1
526fi
527[ "$(git -C "$TMPDIR/sealed.git" rev-parse main)" = "$expected" ]
528
529touch "$out"