| 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 | # Everything else is wide open to the same non-writer: topic |
| 78 | # branches, tags, and refs no 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 v1 release/1.0 |
| 83 | has_ref guarded refs/heads/idea/one |
| 84 | has_ref guarded refs/tags/v1 |
| 85 | has_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. |
| 89 | if 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 |
| 92 | fi |
| 93 | grep -q 'refs/heads/release/1.0' glob.err |
| 94 | lacks_ref released refs/heads/release/1.0 |
| 95 | |
| 96 | # The writer creates it, and updates it. |
| 97 | as integrator git push --quiet guarded main |
| 98 | has_ref guarded refs/heads/main |
| 99 | git commit --quiet --allow-empty -m two |
| 100 | as integrator git push --quiet guarded main |
| 101 | second="$(git rev-parse HEAD)" |
| 102 | [ "$(git -C "$TMPDIR/guarded.git" rev-parse main)" = "$second" ] |
| 103 | |
| 104 | # A delete is a write. |
| 105 | if 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 |
| 108 | fi |
| 109 | grep -q 'refs/heads/main' deleted.err |
| 110 | has_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. |
| 115 | git commit --quiet --allow-empty -m three |
| 116 | git branch idea/two |
| 117 | if 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 |
| 120 | fi |
| 121 | grep -q 'refs/heads/main' atomic.err |
| 122 | lacks_ref guarded refs/heads/idea/two |
| 123 | [ "$(git -C "$TMPDIR/guarded.git" rev-parse main)" = "$second" ] |
| 124 | |
| 125 | # Attestation refs: anyone may create one … |
| 126 | att="refs/the-valley/attestations/$(printf '%064d' 0)/key0" |
| 127 | git update-ref "$att" HEAD~1 |
| 128 | as contributor git push --quiet guarded "$att:$att" |
| 129 | has_ref guarded "$att" |
| 130 | landed="$(git -C "$TMPDIR/guarded.git" rev-parse "$att")" |
| 131 | |
| 132 | # … and nobody may rewrite or drop one, writer or not. |
| 133 | git update-ref "$att" HEAD |
| 134 | if 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 |
| 137 | fi |
| 138 | grep -q 'create-only' rewritten.err |
| 139 | grep -q "$att" rewritten.err |
| 140 | if 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 |
| 143 | fi |
| 144 | grep -q 'create-only' rewritten-by-writer.err |
| 145 | if 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 |
| 148 | fi |
| 149 | grep -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. |
| 153 | att2="refs/the-valley/attestations/$(printf '%064d' 0)/key1" |
| 154 | git update-ref "$att2" HEAD |
| 155 | as contributor git push --quiet guarded "$att2:$att2" |
| 156 | has_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. |
| 162 | if 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 |
| 165 | fi |
| 166 | grep -q 'integrator' sealed.err |
| 167 | grep -q 'no writer is declared' sealed.err |
| 168 | grep -q 'integration request' sealed.err |
| 169 | lacks_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. |
| 173 | as contributor git push --quiet sealed idea/one v1 |
| 174 | has_ref sealed refs/heads/idea/one |
| 175 | has_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. |
| 179 | as contributor git push --quiet sealed "$att:$att" |
| 180 | has_ref sealed "$att" |
| 181 | git update-ref "$att" HEAD~1 |
| 182 | if 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 |
| 185 | fi |
| 186 | grep -q 'create-only' sealed-att.err |
| 187 | |
| 188 | touch "$out" |