gunk-dev/the-valley

protect-e2e.sh

source818813f26b40main's published tip history
14.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# A bare repo wired exactly as valley-init wires it, with
13# the real store path followed from the rendered init script.
14# Every project the host serves gets the hook, protected or not.
15serve() {
16 local hook
17 hook="$(grep -o "/nix/store/[^ ]*-valley-protect-$1" "$initScriptPath" | head -n1)"
18 test -x "$hook"
19 git init --quiet --bare "$1.git"
20 ln -s "$hook" "$1.git/hooks/pre-receive"
21}
22serve sealed
23serve guarded
24serve released
25serve open
26
27# Pushing as a principal is pushing with the tag on; pushing
28# with an untagged key is pushing with it off. That sshd sets
29# the tag from a key's entry, and nothing else does, is what
30# ssh-e2e checks against a real sshd.
31as() {
32 local who="$1"
33 shift
34 if [ "$who" = anonymous ]; then
35 env -u VALLEY_PRINCIPAL "$@"
36 else
37 VALLEY_PRINCIPAL="$who" "$@"
38 fi
39}
40refs() { git -C "$TMPDIR/$1.git" for-each-ref --format='%(refname)'; }
41has_ref() {
42 if ! refs "$1" | grep -qx "$2"; then
43 echo "protect-e2e: $1 has no $2" >&2
44 refs "$1" >&2
45 exit 1
46 fi
47}
48lacks_ref() {
49 if refs "$1" | grep -qx "$2"; then
50 echo "protect-e2e: $1 kept $2 through a rejected push" >&2
51 exit 1
52 fi
53}
54# refused <file> <what> <command…> — the push must fail.
55refused() {
56 local err="$1" what="$2"
57 shift 2
58 if "$@" 2> "$err"; then
59 echo "protect-e2e: $what" >&2
60 exit 1
61 fi
62}
63
64git init --quiet work
65cd work || exit 1
66git commit --quiet --allow-empty -m one
67for name in sealed guarded released open; do
68 git remote add "$name" "$TMPDIR/$name.git"
69done
70
71# ----------------------------------------------------------------------
72# Protected refs.
73
74# A non-writer cannot create a protected ref, and the
75# rejection says who was pushing, what they were writing,
76# and who may.
77refused denied.err "a non-writer wrote a protected ref" \
78 as contributor git push --quiet guarded main
79grep -q 'contributor' denied.err
80grep -q 'refs/heads/main' denied.err
81grep -q 'writers: integrator' denied.err
82lacks_ref guarded refs/heads/main
83
84# An untagged key is nobody, and nobody is not a writer.
85refused untagged.err "an untagged key wrote a protected ref" \
86 as anonymous git push --quiet guarded main
87grep -q '<untagged key>' untagged.err
88lacks_ref guarded refs/heads/main
89
90# Topic branches are open to the same non-writer, including a
91# branch some other project's declaration protects.
92git branch idea/one
93git tag v1
94git branch release/1.0
95as contributor git push --quiet guarded idea/one release/1.0
96has_ref guarded refs/heads/idea/one
97has_ref guarded refs/heads/release/1.0
98
99# The same ref is protected where a declaration says so: the
100# glob in released's set matches it.
101refused glob.err "a glob in the protected set did not match" \
102 as contributor git push --quiet released release/1.0
103grep -q 'refs/heads/release/1.0' glob.err
104lacks_ref released refs/heads/release/1.0
105
106# The writer creates it, and updates it.
107as integrator git push --quiet guarded main
108has_ref guarded refs/heads/main
109git commit --quiet --allow-empty -m two
110as integrator git push --quiet guarded main
111second="$(git rev-parse HEAD)"
112[ "$(git -C "$TMPDIR/guarded.git" rev-parse main)" = "$second" ]
113
114# A delete is a write.
115refused deleted.err "a non-writer deleted a protected ref" \
116 as contributor git push --quiet --delete guarded main
117grep -q 'refs/heads/main' deleted.err
118has_ref guarded refs/heads/main
119
120# pre-receive answers for the whole push: an open ref
121# travelling with a protected one lands only if the
122# protected one does, atomic push or not.
123git commit --quiet --allow-empty -m three
124git branch idea/two
125for mode in --atomic --no-atomic; do
126 refused mixed.err "a protected ref rode in on a $mode push" \
127 as contributor git push --quiet "$mode" guarded idea/two main
128 grep -q 'refs/heads/main' mixed.err
129 lacks_ref guarded refs/heads/idea/two
130 [ "$(git -C "$TMPDIR/guarded.git" rev-parse main)" = "$second" ]
131done
132
133# The norm, on a project that declares no writer at all: the
134# protected ref takes no push from anyone. Even the principal
135# that writes guarded's main is refused here, and told how a
136# change lands instead of being told who may.
137refused sealed.err "a protected ref with no declared writer took a push" \
138 as integrator git push --quiet sealed main
139grep -q 'integrator' sealed.err
140grep -q 'no writer is declared' sealed.err
141grep -q 'integration request' sealed.err
142lacks_ref sealed refs/heads/main
143
144# A project with no protection block protects nothing: its
145# main is a branch like any other. Everything else the policy
146# says still holds there, as the sections below show.
147as contributor git push --quiet open main
148has_ref open refs/heads/main
149
150# ----------------------------------------------------------------------
151# A symbolic ref takes no push. One cannot be pushed, but one
152# can exist on the server, and git would write the ref it
153# points at: here, a topic-branch name standing for the
154# protected main. Its writer is refused too.
155git -C "$TMPDIR/guarded.git" symbolic-ref refs/heads/alias refs/heads/main
156for who in contributor integrator; do
157 refused symref.err "$who wrote through a symbolic ref" \
158 as "$who" git push --quiet --force guarded HEAD:refs/heads/alias
159 grep -q 'symbolic ref' symref.err
160 [ "$(git -C "$TMPDIR/guarded.git" rev-parse main)" = "$second" ]
161done
162git -C "$TMPDIR/guarded.git" symbolic-ref --delete refs/heads/alias
163
164# ----------------------------------------------------------------------
165# Namespaces no push may write, on a protected project and an
166# unprotected one, by the protected ref's own writer.
167
168# Tags open only through a named grant, and guarded has none.
169refused tag.err "a tag was pushed that no grant opens" \
170 as integrator git push --quiet guarded v1
171grep -q 'refs/tags/ opens only through a named grant' tag.err
172grep -q 'no grant of guarded' tag.err
173lacks_ref guarded refs/tags/v1
174
175# Notes, replacement refs and the valley's own namespace are
176# closed outright, wherever they are pushed and by whom.
177git notes add -m "a note" HEAD
178git commit --quiet --allow-empty -m decoy
179replaced="refs/replace/$(git rev-parse HEAD~1)"
180git update-ref "$replaced" HEAD
181outcome=refs/the-valley/integration-outcomes/main/idea
182for project in guarded open; do
183 for who in contributor integrator; do
184 refused notes.err "$who pushed a notes ref to $project" \
185 as "$who" git push --quiet "$project" refs/notes/commits
186 grep -q 'refs/notes/ is closed to every push' notes.err
187 refused replace.err "$who pushed a replacement ref to $project" \
188 as "$who" git push --quiet "$project" "$replaced:$replaced"
189 grep -q 'refs/replace/ is closed to every push' replace.err
190 refused outcome.err "$who pushed an integration outcome to $project" \
191 as "$who" git push --quiet "$project" "HEAD:$outcome"
192 grep -q 'refs/the-valley/integration-outcomes/ is written only by the machinery' outcome.err
193 done
194 lacks_ref "$project" refs/notes/commits
195 lacks_ref "$project" "$replaced"
196 lacks_ref "$project" "$outcome"
197done
198git update-ref -d "$replaced"
199git reset --quiet --hard HEAD~1
200
201# A grant opens what it names to whom it names, and nothing
202# more. released grants release tags to the integrator
203# principal: that principal pushes one, nobody else does, and
204# a tag outside the pattern stays closed to it too.
205git tag release/1.0
206tag=refs/tags/release/1.0
207refused opened.err "a grant opened a ref to a principal it does not name" \
208 as contributor git push --quiet released "$tag:$tag"
209grep -q 'the grant release-tags of released opens this ref to integrator only' opened.err
210lacks_ref released "$tag"
211as integrator git push --quiet released "$tag:$tag"
212has_ref released "$tag"
213refused unopened.err "a grant opened a ref outside its pattern" \
214 as integrator git push --quiet released v1
215grep -q 'no grant of released' unopened.err
216lacks_ref released refs/tags/v1
217
218# ----------------------------------------------------------------------
219# Integration requests take writes only from a holder of the
220# request grant. Pushing topic branches does not make a
221# principal one, and neither does being a declared writer.
222request=refs/the-valley/integration-requests/main/idea
223for who in contributor integrator anonymous; do
224 refused request.err "$who filed a request without the request grant" \
225 as "$who" git push --quiet guarded "idea/one:$request"
226 grep -q 'refs/the-valley/integration-requests/' request.err
227 grep -q 'request grant' request.err
228 lacks_ref guarded "$request"
229done
230
231# The holder files one, replaces it with a head that does not
232# descend from the first, and withdraws it. In between, a
233# principal without the grant can neither replace nor
234# withdraw what the holder filed.
235git commit --quiet --allow-empty -m "first ask"
236as requester git push --quiet guarded "HEAD:$request"
237filed="$(git rev-parse HEAD)"
238git reset --quiet --hard HEAD~1
239git commit --quiet --allow-empty -m "second ask"
240refused replaced.err "a principal without the grant replaced a request" \
241 as contributor git push --quiet --force guarded "HEAD:$request"
242grep -q 'request grant' replaced.err
243refused withdrawn.err "a principal without the grant withdrew a request" \
244 as contributor git push --quiet --delete guarded "$request"
245grep -q 'request grant' withdrawn.err
246[ "$(git -C "$TMPDIR/guarded.git" rev-parse "$request")" = "$filed" ]
247as requester git push --quiet --force guarded "HEAD:$request"
248[ "$(git -C "$TMPDIR/guarded.git" rev-parse "$request")" = "$(git rev-parse HEAD)" ]
249as requester git push --quiet --delete guarded "$request"
250lacks_ref guarded "$request"
251git reset --quiet --hard HEAD~1
252
253# Where protection covers the request namespace too, each rule
254# still applies and neither stands in for the other: released's
255# writer lacks the grant, and the holder of the grant is not
256# released's writer.
257refused overlap-writer.err "a writer filed a request without the grant" \
258 as integrator git push --quiet released "idea/one:$request"
259grep -q 'request grant' overlap-writer.err
260refused overlap-holder.err "a request holder wrote a protected request ref" \
261 as requester git push --quiet released "idea/one:$request"
262grep -q 'a protected ref of released' overlap-holder.err
263lacks_ref released "$request"
264
265# The grant is the same on a project that declares no
266# protection at all.
267refused open-request.err "a request was filed on an unprotected project without the grant" \
268 as contributor git push --quiet open "idea/one:$request"
269as requester git push --quiet open "idea/one:$request"
270has_ref open "$request"
271
272# ----------------------------------------------------------------------
273# Attestation refs: anyone may create one, if it is what its
274# name says. This host names no verifier keys, so what a note
275# claims about its signer is checked and its signature is not
276# (identity-e2e checks the signature against compiled keys).
277ssh-keygen -q -t ed25519 -N "" -C signer -f "$TMPDIR/signer"
278ssh-keygen -q -t ed25519 -N "" -C other -f "$TMPDIR/other"
279attest run --key "$TMPDIR/signer" --name signer.valley.invalid/attestations \
280 --command ok=true > run.out
281att="$(sed -n 's/^stored \(refs[^ ]*\) -> .*/\1/p' run.out)"
282digest="$(echo "$att" | cut -d/ -f4)"
283keyhash="$(echo "$att" | cut -d/ -f5)"
284as contributor git push --quiet guarded "$att:$att"
285has_ref guarded "$att"
286landed="$(git -C "$TMPDIR/guarded.git" rev-parse "$att")"
287
288# … and nobody may rewrite or drop one, writer or not.
289git update-ref "$att" "$(git rev-parse HEAD^{tree})"
290refused rewritten.err "an attestation ref was rewritten" \
291 as contributor git push --quiet --force guarded "$att:$att"
292grep -q 'create-only' rewritten.err
293grep -q "$att" rewritten.err
294refused rewritten-by-writer.err "a writer rewrote an attestation ref" \
295 as integrator git push --quiet --force guarded "$att:$att"
296grep -q 'create-only' rewritten-by-writer.err
297refused dropped.err "an attestation ref was deleted" \
298 as integrator git push --quiet --delete guarded "$att"
299grep -q 'create-only' dropped.err
300[ "$(git -C "$TMPDIR/guarded.git" rev-parse "$att")" = "$landed" ]
301git update-ref "$att" "$landed"
302
303# Occupation: a name in a create-only namespace is held for
304# good by whoever creates it first, so a name may only hold
305# what it says. Each of these would keep the real signer's
306# evidence out, and each is refused on a project that has
307# none yet.
308other_hash="$(attest key --key "$TMPDIR/other" --name other.valley.invalid/attestations | cut -d+ -f2)"
309occupy() {
310 local name="$1" target="$2" err="$3" why="$4"
311 refused "$err" "an attestation ref took a name it does not hold: $why" \
312 as contributor git push --quiet sealed "$target:refs/the-valley/attestations/$name"
313 lacks_ref sealed "refs/the-valley/attestations/$name"
314}
315# A ref named for the digest alone would block every ref
316# beneath it, and a name that is not hex is no signer's.
317occupy "$digest" "$landed" shape.err "the digest alone"
318grep -q '<digest>/<key hash>' shape.err
319occupy "$digest/$keyhash/extra" "$landed" deep.err "a segment too many"
320occupy "$digest/NOTAHASH" "$landed" nothex.err "a key hash that is not hex"
321# The real signer's note, under another signer's key hash.
322occupy "$digest/$other_hash" "$landed" squat.err "another signer's slot"
323grep -q "no signature under the key hash $other_hash" squat.err
324# The real signer's note, under another tree's digest.
325elsewhere="$(printf '%064d' 7)"
326occupy "$elsewhere/$keyhash" "$landed" subject.err "another tree's slot"
327grep -q 'is about the tree' subject.err
328# Something that is not a tree of notes at all.
329occupy "$elsewhere/$keyhash" HEAD commit.err "a commit"
330grep -q 'points at a commit' commit.err
331# The ref the real evidence wants is still free, and takes it.
332as anonymous git push --quiet sealed "$att:$att"
333has_ref sealed "$att"
334
335# ----------------------------------------------------------------------
336# A hook a project composes after the policy runs once the
337# policy accepts, and can only refuse more: released's refuses
338# branches under frozen/.
339git branch frozen/one
340refused frozen.err "the composed hook did not run" \
341 as contributor git push --quiet released frozen/one
342grep -q 'released: refs/heads/frozen/one is frozen' frozen.err
343lacks_ref released refs/heads/frozen/one
344as contributor git push --quiet released idea/one
345has_ref released refs/heads/idea/one
346
347touch "$out"