| 1 | # shellcheck shell=bash |
| 2 | # Paths and fixtures come from the derivation environment. |
| 3 | # shellcheck disable=SC2154 |
| 4 | export HOME="$TMPDIR" |
| 5 | export GIT_CONFIG_NOSYSTEM=1 |
| 6 | git config --global user.name valley-check |
| 7 | git config --global user.email valley-check@localhost |
| 8 | git config --global init.defaultBranch main |
| 9 | cd "$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. |
| 13 | serve() { |
| 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 | } |
| 20 | serve sealed |
| 21 | serve guarded |
| 22 | serve released |
| 23 | |
| 24 | # Pushing as a principal is pushing with the tag on; pushing |
| 25 | # with an untagged key is pushing with it off. |
| 26 | as() { |
| 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 | } |
| 35 | refs() { git -C "$TMPDIR/$1.git" for-each-ref --format='%(refname)'; } |
| 36 | has_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 | } |
| 43 | lacks_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 | |
| 50 | git init --quiet work |
| 51 | cd work || exit 1 |
| 52 | git commit --quiet --allow-empty -m one |
| 53 | git remote add sealed "$TMPDIR/sealed.git" |
| 54 | git remote add guarded "$TMPDIR/guarded.git" |
| 55 | git 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. |
| 60 | if 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 |
| 63 | fi |
| 64 | grep -q 'contributor' denied.err |
| 65 | grep -q 'refs/heads/main' denied.err |
| 66 | grep -q 'writers: integrator' denied.err |
| 67 | lacks_ref guarded refs/heads/main |
| 68 | |
| 69 | # An untagged key is nobody, and nobody is not a writer. |
| 70 | if 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 |
| 73 | fi |
| 74 | grep -q '<untagged key>' untagged.err |
| 75 | lacks_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. |
| 79 | git branch idea/one |
| 80 | git tag v1 |
| 81 | git branch release/1.0 |
| 82 | as contributor git push --quiet guarded idea/one release/1.0 |
| 83 | has_ref guarded refs/heads/idea/one |
| 84 | has_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. |
| 89 | if 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 |
| 92 | fi |
| 93 | grep -q 'refs/tags/ is closed to pushes' tag.err |
| 94 | grep -q 'no allow entry' tag.err |
| 95 | lacks_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. |
| 101 | git notes add -m "a note" HEAD |
| 102 | if 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 |
| 105 | fi |
| 106 | grep -q 'refs/notes/ is closed to pushes' notes.err |
| 107 | lacks_ref guarded refs/notes/commits |
| 108 | outcome=refs/the-valley/integration-outcomes/main/idea |
| 109 | if 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 |
| 112 | fi |
| 113 | grep -q 'refs/the-valley/integration-outcomes/ is closed to pushes' outcome.err |
| 114 | lacks_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. |
| 120 | git commit --quiet --allow-empty -m decoy |
| 121 | replaced="refs/replace/$(git rev-parse HEAD~1)" |
| 122 | git update-ref "$replaced" HEAD |
| 123 | for 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" |
| 130 | done |
| 131 | git update-ref -d "$replaced" |
| 132 | git 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. |
| 137 | request=refs/the-valley/integration-requests/main/idea |
| 138 | for 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" |
| 146 | done |
| 147 | |
| 148 | # The holder files one, replaces it with a head that does not |
| 149 | # descend from the first, and withdraws it. |
| 150 | git commit --quiet --allow-empty -m "first ask" |
| 151 | as requester git push --quiet guarded "HEAD:$request" |
| 152 | has_ref guarded "$request" |
| 153 | git reset --quiet --hard HEAD~1 |
| 154 | git commit --quiet --allow-empty -m "second ask" |
| 155 | as requester git push --quiet --force guarded "HEAD:$request" |
| 156 | [ "$(git -C "$TMPDIR/guarded.git" rev-parse "$request")" = "$(git rev-parse HEAD)" ] |
| 157 | as requester git push --quiet --delete guarded "$request" |
| 158 | lacks_ref guarded "$request" |
| 159 | git 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. |
| 163 | if 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 |
| 166 | fi |
| 167 | grep -q 'refs/heads/release/1.0' glob.err |
| 168 | lacks_ref released refs/heads/release/1.0 |
| 169 | |
| 170 | # The writer creates it, and updates it. |
| 171 | as integrator git push --quiet guarded main |
| 172 | has_ref guarded refs/heads/main |
| 173 | git commit --quiet --allow-empty -m two |
| 174 | as integrator git push --quiet guarded main |
| 175 | second="$(git rev-parse HEAD)" |
| 176 | [ "$(git -C "$TMPDIR/guarded.git" rev-parse main)" = "$second" ] |
| 177 | |
| 178 | # A delete is a write. |
| 179 | if 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 |
| 182 | fi |
| 183 | grep -q 'refs/heads/main' deleted.err |
| 184 | has_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. |
| 189 | git commit --quiet --allow-empty -m three |
| 190 | git branch idea/two |
| 191 | if 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 |
| 194 | fi |
| 195 | grep -q 'refs/heads/main' atomic.err |
| 196 | lacks_ref guarded refs/heads/idea/two |
| 197 | [ "$(git -C "$TMPDIR/guarded.git" rev-parse main)" = "$second" ] |
| 198 | |
| 199 | # Attestation refs: anyone may create one … |
| 200 | att="refs/the-valley/attestations/$(printf '%064d' 0)/key0" |
| 201 | git update-ref "$att" HEAD~1 |
| 202 | as contributor git push --quiet guarded "$att:$att" |
| 203 | has_ref guarded "$att" |
| 204 | landed="$(git -C "$TMPDIR/guarded.git" rev-parse "$att")" |
| 205 | |
| 206 | # … and nobody may rewrite or drop one, writer or not. |
| 207 | git update-ref "$att" HEAD |
| 208 | if 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 |
| 211 | fi |
| 212 | grep -q 'create-only' rewritten.err |
| 213 | grep -q "$att" rewritten.err |
| 214 | if 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 |
| 217 | fi |
| 218 | grep -q 'create-only' rewritten-by-writer.err |
| 219 | if 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 |
| 222 | fi |
| 223 | grep -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. |
| 227 | att2="refs/the-valley/attestations/$(printf '%064d' 0)/key1" |
| 228 | git update-ref "$att2" HEAD |
| 229 | as contributor git push --quiet guarded "$att2:$att2" |
| 230 | has_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. |
| 236 | if 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 |
| 239 | fi |
| 240 | grep -q 'integrator' sealed.err |
| 241 | grep -q 'no writer is declared' sealed.err |
| 242 | grep -q 'integration request' sealed.err |
| 243 | lacks_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. |
| 248 | as contributor git push --quiet sealed idea/one |
| 249 | has_ref sealed refs/heads/idea/one |
| 250 | if 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 |
| 253 | fi |
| 254 | lacks_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. |
| 260 | git tag release/1.0 |
| 261 | tag=refs/tags/release/1.0 |
| 262 | if 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 |
| 265 | fi |
| 266 | grep -q 'opens this ref to integrator only' opened.err |
| 267 | lacks_ref released "$tag" |
| 268 | as integrator git push --quiet released "$tag:$tag" |
| 269 | has_ref released "$tag" |
| 270 | if 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 |
| 273 | fi |
| 274 | grep -q 'no allow entry' unopened.err |
| 275 | lacks_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. |
| 279 | as contributor git push --quiet sealed "$att:$att" |
| 280 | has_ref sealed "$att" |
| 281 | git update-ref "$att" HEAD~1 |
| 282 | if 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 |
| 285 | fi |
| 286 | grep -q 'create-only' sealed-att.err |
| 287 | |
| 288 | touch "$out" |