| 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 guarded |
| 21 | serve released |
| 22 | |
| 23 | # Pushing as a principal is pushing with the tag on; pushing |
| 24 | # with an untagged key is pushing with it off. |
| 25 | as() { |
| 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 | } |
| 34 | refs() { git -C "$TMPDIR/$1.git" for-each-ref --format='%(refname)'; } |
| 35 | has_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 | } |
| 42 | lacks_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 | |
| 49 | git init --quiet work |
| 50 | cd work || exit 1 |
| 51 | git commit --quiet --allow-empty -m one |
| 52 | git remote add guarded "$TMPDIR/guarded.git" |
| 53 | git 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. |
| 58 | if 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 |
| 61 | fi |
| 62 | grep -q 'contributor' denied.err |
| 63 | grep -q 'refs/heads/main' denied.err |
| 64 | grep -q 'writers: integrator' denied.err |
| 65 | lacks_ref guarded refs/heads/main |
| 66 | |
| 67 | # An untagged key is nobody, and nobody is not a writer. |
| 68 | if 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 |
| 71 | fi |
| 72 | grep -q '<untagged key>' untagged.err |
| 73 | lacks_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. |
| 77 | git branch idea/one |
| 78 | git tag v1 |
| 79 | git branch release/1.0 |
| 80 | as contributor git push --quiet guarded idea/one v1 release/1.0 |
| 81 | has_ref guarded refs/heads/idea/one |
| 82 | has_ref guarded refs/tags/v1 |
| 83 | has_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. |
| 87 | if 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 |
| 90 | fi |
| 91 | grep -q 'refs/heads/release/1.0' glob.err |
| 92 | lacks_ref released refs/heads/release/1.0 |
| 93 | |
| 94 | # The writer creates it, and updates it. |
| 95 | as integrator git push --quiet guarded main |
| 96 | has_ref guarded refs/heads/main |
| 97 | git commit --quiet --allow-empty -m two |
| 98 | as integrator git push --quiet guarded main |
| 99 | second="$(git rev-parse HEAD)" |
| 100 | [ "$(git -C "$TMPDIR/guarded.git" rev-parse main)" = "$second" ] |
| 101 | |
| 102 | # A delete is a write. |
| 103 | if 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 |
| 106 | fi |
| 107 | grep -q 'refs/heads/main' deleted.err |
| 108 | has_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. |
| 113 | git commit --quiet --allow-empty -m three |
| 114 | git branch idea/two |
| 115 | if 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 |
| 118 | fi |
| 119 | grep -q 'refs/heads/main' atomic.err |
| 120 | lacks_ref guarded refs/heads/idea/two |
| 121 | [ "$(git -C "$TMPDIR/guarded.git" rev-parse main)" = "$second" ] |
| 122 | |
| 123 | # Attestation refs: anyone may create one … |
| 124 | att="refs/the-valley/attestations/$(printf '%064d' 0)/key0" |
| 125 | git update-ref "$att" HEAD~1 |
| 126 | as contributor git push --quiet guarded "$att:$att" |
| 127 | has_ref guarded "$att" |
| 128 | landed="$(git -C "$TMPDIR/guarded.git" rev-parse "$att")" |
| 129 | |
| 130 | # … and nobody may rewrite or drop one, writer or not. |
| 131 | git update-ref "$att" HEAD |
| 132 | if 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 |
| 135 | fi |
| 136 | grep -q 'create-only' rewritten.err |
| 137 | grep -q "$att" rewritten.err |
| 138 | if 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 |
| 141 | fi |
| 142 | grep -q 'create-only' rewritten-by-writer.err |
| 143 | if 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 |
| 146 | fi |
| 147 | grep -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. |
| 151 | att2="refs/the-valley/attestations/$(printf '%064d' 0)/key1" |
| 152 | git update-ref "$att2" HEAD |
| 153 | as contributor git push --quiet guarded "$att2:$att2" |
| 154 | has_ref guarded "$att2" |
| 155 | |
| 156 | touch "$out" |