gunk-dev/the-valley

protect-e2e.sh

sourceef0e6a83672emain's published tip history
10.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
9cd "$TMPDIR" || exit 1
10
11# A bare repo wired exactly as valley-init wires it, with
12# the real store path followed from the rendered init script.
13serve() {
14 local hook
15 hook="$(grep -o "/nix/store/[^ ]*-valley-protect-$1" "$initScriptPath" | head -n1)"
16 test -x "$hook"
17 git init --quiet --bare "$1.git"
18 ln -s "$hook" "$1.git/hooks/pre-receive"
19}
20serve sealed
21serve guarded
22serve released
23
24# Pushing as a principal is pushing with the tag on; pushing
25# with an untagged key is pushing with it off.
26as() {
27 local who="$1"
28 shift
29 if [ "$who" = anonymous ]; then
30 env -u VALLEY_PRINCIPAL "$@"
31 else
32 VALLEY_PRINCIPAL="$who" "$@"
33 fi
34}
35refs() { git -C "$TMPDIR/$1.git" for-each-ref --format='%(refname)'; }
36has_ref() {
37 if ! refs "$1" | grep -qx "$2"; then
38 echo "protect-e2e: $1 has no $2" >&2
39 refs "$1" >&2
40 exit 1
41 fi
42}
43lacks_ref() {
44 if refs "$1" | grep -qx "$2"; then
45 echo "protect-e2e: $1 kept $2 through a rejected push" >&2
46 exit 1
47 fi
48}
49
50git init --quiet work
51cd work || exit 1
52git commit --quiet --allow-empty -m one
53git remote add sealed "$TMPDIR/sealed.git"
54git remote add guarded "$TMPDIR/guarded.git"
55git remote add released "$TMPDIR/released.git"
56
57# A non-writer cannot create a protected ref, and the
58# rejection says who was pushing, what they were writing,
59# and who may.
60if as contributor git push --quiet guarded main 2> denied.err; then
61 echo "protect-e2e: a non-writer wrote a protected ref" >&2
62 exit 1
63fi
64grep -q 'contributor' denied.err
65grep -q 'refs/heads/main' denied.err
66grep -q 'writers: integrator' denied.err
67lacks_ref guarded refs/heads/main
68
69# An untagged key is nobody, and nobody is not a writer.
70if as anonymous git push --quiet guarded main 2> untagged.err; then
71 echo "protect-e2e: an untagged key wrote a protected ref" >&2
72 exit 1
73fi
74grep -q '<untagged key>' untagged.err
75lacks_ref guarded refs/heads/main
76
77# Topic branches are open to the same non-writer, including a
78# branch some other project's declaration protects.
79git branch idea/one
80git tag v1
81git branch release/1.0
82as contributor git push --quiet guarded idea/one release/1.0
83has_ref guarded refs/heads/idea/one
84has_ref guarded refs/heads/release/1.0
85
86# Every other namespace is closed, and a refusal names the
87# namespace and what would open it. Tags first: nothing in
88# guarded's protection opens one.
89if as contributor git push --quiet guarded v1 2> tag.err; then
90 echo "protect-e2e: a tag was pushed that no allow entry opens" >&2
91 exit 1
92fi
93grep -q 'refs/tags/ is closed to pushes' tag.err
94grep -q 'no allow entry' tag.err
95lacks_ref guarded refs/tags/v1
96
97# Notes, and the valley's own namespaces that only the
98# integrator writes on the host: an outcome is the
99# controller's record of a verdict, and a pushed one would be
100# a forged verdict.
101git notes add -m "a note" HEAD
102if as contributor git push --quiet guarded refs/notes/commits 2> notes.err; then
103 echo "protect-e2e: a notes ref was pushed" >&2
104 exit 1
105fi
106grep -q 'refs/notes/ is closed to pushes' notes.err
107lacks_ref guarded refs/notes/commits
108outcome=refs/the-valley/integration-outcomes/main/idea
109if as contributor git push --quiet guarded "HEAD:$outcome" 2> outcome.err; then
110 echo "protect-e2e: an integration outcome was pushed" >&2
111 exit 1
112fi
113grep -q 'refs/the-valley/integration-outcomes/ is closed to pushes' outcome.err
114lacks_ref guarded "$outcome"
115
116# Replacement refs are closed to every push, a declared
117# writer's included: one makes an object stand in for another
118# wherever git looks it up, which would change what every
119# reader of the repository sees.
120git commit --quiet --allow-empty -m decoy
121replaced="refs/replace/$(git rev-parse HEAD~1)"
122git update-ref "$replaced" HEAD
123for who in contributor integrator; do
124 if as "$who" git push --quiet guarded "$replaced:$replaced" 2> replace.err; then
125 echo "protect-e2e: $who pushed a replacement ref" >&2
126 exit 1
127 fi
128 grep -q 'refs/replace/ is closed to every push' replace.err
129 lacks_ref guarded "$replaced"
130done
131git update-ref -d "$replaced"
132git reset --quiet --hard HEAD~1
133
134# Integration requests take writes only from a holder of the
135# request grant. Pushing to a topic branch does not make a
136# principal one, and neither does being a declared writer.
137request=refs/the-valley/integration-requests/main/idea
138for who in contributor integrator anonymous; do
139 if as "$who" git push --quiet guarded "idea/one:$request" 2> request.err; then
140 echo "protect-e2e: $who filed a request without the request grant" >&2
141 exit 1
142 fi
143 grep -q 'refs/the-valley/integration-requests/' request.err
144 grep -q 'request grant' request.err
145 lacks_ref guarded "$request"
146done
147
148# The holder files one, replaces it with a head that does not
149# descend from the first, and withdraws it.
150git commit --quiet --allow-empty -m "first ask"
151as requester git push --quiet guarded "HEAD:$request"
152has_ref guarded "$request"
153git reset --quiet --hard HEAD~1
154git commit --quiet --allow-empty -m "second ask"
155as requester git push --quiet --force guarded "HEAD:$request"
156[ "$(git -C "$TMPDIR/guarded.git" rev-parse "$request")" = "$(git rev-parse HEAD)" ]
157as requester git push --quiet --delete guarded "$request"
158lacks_ref guarded "$request"
159git reset --quiet --hard HEAD~1
160
161# The same ref is protected where a declaration says so: the
162# glob in released's set matches it.
163if as contributor git push --quiet released release/1.0 2> glob.err; then
164 echo "protect-e2e: a glob in the protected set did not match" >&2
165 exit 1
166fi
167grep -q 'refs/heads/release/1.0' glob.err
168lacks_ref released refs/heads/release/1.0
169
170# The writer creates it, and updates it.
171as integrator git push --quiet guarded main
172has_ref guarded refs/heads/main
173git commit --quiet --allow-empty -m two
174as integrator git push --quiet guarded main
175second="$(git rev-parse HEAD)"
176[ "$(git -C "$TMPDIR/guarded.git" rev-parse main)" = "$second" ]
177
178# A delete is a write.
179if as contributor git push --quiet --delete guarded main 2> deleted.err; then
180 echo "protect-e2e: a non-writer deleted a protected ref" >&2
181 exit 1
182fi
183grep -q 'refs/heads/main' deleted.err
184has_ref guarded refs/heads/main
185
186# pre-receive answers for the whole push: an open ref
187# travelling with a protected one lands only if the
188# protected one does.
189git commit --quiet --allow-empty -m three
190git branch idea/two
191if as contributor git push --quiet --atomic guarded idea/two main 2> atomic.err; then
192 echo "protect-e2e: a protected ref rode in on an atomic push" >&2
193 exit 1
194fi
195grep -q 'refs/heads/main' atomic.err
196lacks_ref guarded refs/heads/idea/two
197[ "$(git -C "$TMPDIR/guarded.git" rev-parse main)" = "$second" ]
198
199# Attestation refs: anyone may create one …
200att="refs/the-valley/attestations/$(printf '%064d' 0)/key0"
201git update-ref "$att" HEAD~1
202as contributor git push --quiet guarded "$att:$att"
203has_ref guarded "$att"
204landed="$(git -C "$TMPDIR/guarded.git" rev-parse "$att")"
205
206# … and nobody may rewrite or drop one, writer or not.
207git update-ref "$att" HEAD
208if as contributor git push --quiet --force guarded "$att:$att" 2> rewritten.err; then
209 echo "protect-e2e: an attestation ref was rewritten" >&2
210 exit 1
211fi
212grep -q 'create-only' rewritten.err
213grep -q "$att" rewritten.err
214if as integrator git push --quiet --force guarded "$att:$att" 2> rewritten-by-writer.err; then
215 echo "protect-e2e: a writer rewrote an attestation ref" >&2
216 exit 1
217fi
218grep -q 'create-only' rewritten-by-writer.err
219if as integrator git push --quiet --delete guarded "$att" 2> dropped.err; then
220 echo "protect-e2e: an attestation ref was deleted" >&2
221 exit 1
222fi
223grep -q 'create-only' dropped.err
224[ "$(git -C "$TMPDIR/guarded.git" rev-parse "$att")" = "$landed" ]
225
226# A second attestation beside it is still just a creation.
227att2="refs/the-valley/attestations/$(printf '%064d' 0)/key1"
228git update-ref "$att2" HEAD
229as contributor git push --quiet guarded "$att2:$att2"
230has_ref guarded "$att2"
231
232# The norm, on a project that declares no writer at all: the
233# protected ref takes no push from anyone. Even the principal
234# that writes guarded's main is refused here, and told how a
235# change lands instead of being told who may.
236if as integrator git push --quiet sealed main 2> sealed.err; then
237 echo "protect-e2e: a protected ref with no declared writer took a push" >&2
238 exit 1
239fi
240grep -q 'integrator' sealed.err
241grep -q 'no writer is declared' sealed.err
242grep -q 'integration request' sealed.err
243lacks_ref sealed refs/heads/main
244
245# What the declaration does not name is decided there as
246# anywhere: the wall closes one ref, not the project, and the
247# namespaces the allowlist leaves out stay closed.
248as contributor git push --quiet sealed idea/one
249has_ref sealed refs/heads/idea/one
250if as contributor git push --quiet sealed v1 2> sealed-tag.err; then
251 echo "protect-e2e: a tag was pushed to a project that opens none" >&2
252 exit 1
253fi
254lacks_ref sealed refs/tags/v1
255
256# An allow entry opens what it names to whom it names, and
257# nothing more. released opens release tags to the integrator
258# principal: that principal pushes one, nobody else does, and
259# a tag outside the pattern stays closed to it too.
260git tag release/1.0
261tag=refs/tags/release/1.0
262if as contributor git push --quiet released "$tag:$tag" 2> opened.err; then
263 echo "protect-e2e: an allow entry opened a ref to a principal it does not name" >&2
264 exit 1
265fi
266grep -q 'opens this ref to integrator only' opened.err
267lacks_ref released "$tag"
268as integrator git push --quiet released "$tag:$tag"
269has_ref released "$tag"
270if as integrator git push --quiet released v1 2> unopened.err; then
271 echo "protect-e2e: an allow entry opened a ref outside its pattern" >&2
272 exit 1
273fi
274grep -q 'no allow entry' unopened.err
275lacks_ref released refs/tags/v1
276
277# And the attestation namespace is unchanged by any of it —
278# create-only, for everyone, with or without a writers list.
279as contributor git push --quiet sealed "$att:$att"
280has_ref sealed "$att"
281git update-ref "$att" HEAD~1
282if as integrator git push --quiet --force sealed "$att:$att" 2> sealed-att.err; then
283 echo "protect-e2e: an attestation ref was rewritten on a writer-less project" >&2
284 exit 1
285fi
286grep -q 'create-only' sealed-att.err
287
288touch "$out"