gunk-dev/the-valley

protect-e2e.sh

sourcee4ada5512f03main's published tip history
5.0 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 guarded
21serve released
22
23# Pushing as a principal is pushing with the tag on; pushing
24# with an untagged key is pushing with it off.
25as() {
26 local who="$1"
27 shift
28 if [ "$who" = anonymous ]; then
29 env -u VALLEY_PRINCIPAL "$@"
30 else
31 VALLEY_PRINCIPAL="$who" "$@"
32 fi
33}
34refs() { git -C "$TMPDIR/$1.git" for-each-ref --format='%(refname)'; }
35has_ref() {
36 if ! refs "$1" | grep -qx "$2"; then
37 echo "protect-e2e: $1 has no $2" >&2
38 refs "$1" >&2
39 exit 1
40 fi
41}
42lacks_ref() {
43 if refs "$1" | grep -qx "$2"; then
44 echo "protect-e2e: $1 kept $2 through a rejected push" >&2
45 exit 1
46 fi
47}
48
49git init --quiet work
50cd work || exit 1
51git commit --quiet --allow-empty -m one
52git remote add guarded "$TMPDIR/guarded.git"
53git remote add released "$TMPDIR/released.git"
54
55# A non-writer cannot create a protected ref, and the
56# rejection says who was pushing, what they were writing,
57# and who may.
58if as contributor git push --quiet guarded main 2> denied.err; then
59 echo "protect-e2e: a non-writer wrote a protected ref" >&2
60 exit 1
61fi
62grep -q 'contributor' denied.err
63grep -q 'refs/heads/main' denied.err
64grep -q 'writers: integrator' denied.err
65lacks_ref guarded refs/heads/main
66
67# An untagged key is nobody, and nobody is not a writer.
68if as anonymous git push --quiet guarded main 2> untagged.err; then
69 echo "protect-e2e: an untagged key wrote a protected ref" >&2
70 exit 1
71fi
72grep -q '<untagged key>' untagged.err
73lacks_ref guarded refs/heads/main
74
75# Everything else is wide open to the same non-writer: topic
76# branches, tags, and refs no declaration protects.
77git branch idea/one
78git tag v1
79git branch release/1.0
80as contributor git push --quiet guarded idea/one v1 release/1.0
81has_ref guarded refs/heads/idea/one
82has_ref guarded refs/tags/v1
83has_ref guarded refs/heads/release/1.0
84
85# The same ref is protected where a declaration says so: the
86# glob in released's set matches it.
87if as contributor git push --quiet released release/1.0 2> glob.err; then
88 echo "protect-e2e: a glob in the protected set did not match" >&2
89 exit 1
90fi
91grep -q 'refs/heads/release/1.0' glob.err
92lacks_ref released refs/heads/release/1.0
93
94# The writer creates it, and updates it.
95as integrator git push --quiet guarded main
96has_ref guarded refs/heads/main
97git commit --quiet --allow-empty -m two
98as integrator git push --quiet guarded main
99second="$(git rev-parse HEAD)"
100[ "$(git -C "$TMPDIR/guarded.git" rev-parse main)" = "$second" ]
101
102# A delete is a write.
103if as contributor git push --quiet --delete guarded main 2> deleted.err; then
104 echo "protect-e2e: a non-writer deleted a protected ref" >&2
105 exit 1
106fi
107grep -q 'refs/heads/main' deleted.err
108has_ref guarded refs/heads/main
109
110# pre-receive answers for the whole push: an open ref
111# travelling with a protected one lands only if the
112# protected one does.
113git commit --quiet --allow-empty -m three
114git branch idea/two
115if as contributor git push --quiet --atomic guarded idea/two main 2> atomic.err; then
116 echo "protect-e2e: a protected ref rode in on an atomic push" >&2
117 exit 1
118fi
119grep -q 'refs/heads/main' atomic.err
120lacks_ref guarded refs/heads/idea/two
121[ "$(git -C "$TMPDIR/guarded.git" rev-parse main)" = "$second" ]
122
123# Attestation refs: anyone may create one …
124att="refs/the-valley/attestations/$(printf '%064d' 0)/key0"
125git update-ref "$att" HEAD~1
126as contributor git push --quiet guarded "$att:$att"
127has_ref guarded "$att"
128landed="$(git -C "$TMPDIR/guarded.git" rev-parse "$att")"
129
130# … and nobody may rewrite or drop one, writer or not.
131git update-ref "$att" HEAD
132if as contributor git push --quiet --force guarded "$att:$att" 2> rewritten.err; then
133 echo "protect-e2e: an attestation ref was rewritten" >&2
134 exit 1
135fi
136grep -q 'create-only' rewritten.err
137grep -q "$att" rewritten.err
138if as integrator git push --quiet --force guarded "$att:$att" 2> rewritten-by-writer.err; then
139 echo "protect-e2e: a writer rewrote an attestation ref" >&2
140 exit 1
141fi
142grep -q 'create-only' rewritten-by-writer.err
143if as integrator git push --quiet --delete guarded "$att" 2> dropped.err; then
144 echo "protect-e2e: an attestation ref was deleted" >&2
145 exit 1
146fi
147grep -q 'create-only' dropped.err
148[ "$(git -C "$TMPDIR/guarded.git" rev-parse "$att")" = "$landed" ]
149
150# A second attestation beside it is still just a creation.
151att2="refs/the-valley/attestations/$(printf '%064d' 0)/key1"
152git update-ref "$att2" HEAD
153as contributor git push --quiet guarded "$att2:$att2"
154has_ref guarded "$att2"
155
156touch "$out"