gunk-dev/the-valley

protect-e2e.sh

source0a11b2de4864main's published tip history
6.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# Everything else is wide open to the same non-writer: topic
78# branches, tags, and refs no declaration protects.
79git branch idea/one
80git tag v1
81git branch release/1.0
82as contributor git push --quiet guarded idea/one v1 release/1.0
83has_ref guarded refs/heads/idea/one
84has_ref guarded refs/tags/v1
85has_ref guarded refs/heads/release/1.0
86
87# The same ref is protected where a declaration says so: the
88# glob in released's set matches it.
89if as contributor git push --quiet released release/1.0 2> glob.err; then
90 echo "protect-e2e: a glob in the protected set did not match" >&2
91 exit 1
92fi
93grep -q 'refs/heads/release/1.0' glob.err
94lacks_ref released refs/heads/release/1.0
95
96# The writer creates it, and updates it.
97as integrator git push --quiet guarded main
98has_ref guarded refs/heads/main
99git commit --quiet --allow-empty -m two
100as integrator git push --quiet guarded main
101second="$(git rev-parse HEAD)"
102[ "$(git -C "$TMPDIR/guarded.git" rev-parse main)" = "$second" ]
103
104# A delete is a write.
105if as contributor git push --quiet --delete guarded main 2> deleted.err; then
106 echo "protect-e2e: a non-writer deleted a protected ref" >&2
107 exit 1
108fi
109grep -q 'refs/heads/main' deleted.err
110has_ref guarded refs/heads/main
111
112# pre-receive answers for the whole push: an open ref
113# travelling with a protected one lands only if the
114# protected one does.
115git commit --quiet --allow-empty -m three
116git branch idea/two
117if as contributor git push --quiet --atomic guarded idea/two main 2> atomic.err; then
118 echo "protect-e2e: a protected ref rode in on an atomic push" >&2
119 exit 1
120fi
121grep -q 'refs/heads/main' atomic.err
122lacks_ref guarded refs/heads/idea/two
123[ "$(git -C "$TMPDIR/guarded.git" rev-parse main)" = "$second" ]
124
125# Attestation refs: anyone may create one …
126att="refs/the-valley/attestations/$(printf '%064d' 0)/key0"
127git update-ref "$att" HEAD~1
128as contributor git push --quiet guarded "$att:$att"
129has_ref guarded "$att"
130landed="$(git -C "$TMPDIR/guarded.git" rev-parse "$att")"
131
132# … and nobody may rewrite or drop one, writer or not.
133git update-ref "$att" HEAD
134if as contributor git push --quiet --force guarded "$att:$att" 2> rewritten.err; then
135 echo "protect-e2e: an attestation ref was rewritten" >&2
136 exit 1
137fi
138grep -q 'create-only' rewritten.err
139grep -q "$att" rewritten.err
140if as integrator git push --quiet --force guarded "$att:$att" 2> rewritten-by-writer.err; then
141 echo "protect-e2e: a writer rewrote an attestation ref" >&2
142 exit 1
143fi
144grep -q 'create-only' rewritten-by-writer.err
145if as integrator git push --quiet --delete guarded "$att" 2> dropped.err; then
146 echo "protect-e2e: an attestation ref was deleted" >&2
147 exit 1
148fi
149grep -q 'create-only' dropped.err
150[ "$(git -C "$TMPDIR/guarded.git" rev-parse "$att")" = "$landed" ]
151
152# A second attestation beside it is still just a creation.
153att2="refs/the-valley/attestations/$(printf '%064d' 0)/key1"
154git update-ref "$att2" HEAD
155as contributor git push --quiet guarded "$att2:$att2"
156has_ref guarded "$att2"
157
158# The norm, on a project that declares no writer at all: the
159# protected ref takes no push from anyone. Even the principal
160# that writes guarded's main is refused here, and told how a
161# change lands instead of being told who may.
162if as integrator git push --quiet sealed main 2> sealed.err; then
163 echo "protect-e2e: a protected ref with no declared writer took a push" >&2
164 exit 1
165fi
166grep -q 'integrator' sealed.err
167grep -q 'no writer is declared' sealed.err
168grep -q 'integration request' sealed.err
169lacks_ref sealed refs/heads/main
170
171# What the declaration does not name is open there as
172# anywhere: the wall closes one ref, not the project.
173as contributor git push --quiet sealed idea/one v1
174has_ref sealed refs/heads/idea/one
175has_ref sealed refs/tags/v1
176
177# And the attestation namespace is unchanged by any of it —
178# create-only, for everyone, with or without a writers list.
179as contributor git push --quiet sealed "$att:$att"
180has_ref sealed "$att"
181git update-ref "$att" HEAD~1
182if as integrator git push --quiet --force sealed "$att:$att" 2> sealed-att.err; then
183 echo "protect-e2e: an attestation ref was rewritten on a writer-less project" >&2
184 exit 1
185fi
186grep -q 'create-only' sealed-att.err
187
188touch "$out"