| 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 | # The rendered valley-init script, run as the host runs it. Relocating |
| 12 | # the data directory is the only edit — the sandbox cannot write /srv — |
| 13 | # and it leaves every store path and every decision the script makes |
| 14 | # exactly as shipped. |
| 15 | data="$TMPDIR/srv/git" |
| 16 | sed "s|/srv/git|$data|g" "$initScriptPath" > init.sh |
| 17 | mkdir -p "$data" |
| 18 | |
| 19 | fail() { |
| 20 | echo "init-e2e: $1" >&2 |
| 21 | exit 1 |
| 22 | } |
| 23 | hook() { echo "$data/$1.git/hooks/pre-receive"; } |
| 24 | # The store path the declaration says a project's hook must be. |
| 25 | declared() { |
| 26 | grep -o "/nix/store/[^ ]*-valley-protect-$1" "$initScriptPath" | head -n1 |
| 27 | } |
| 28 | is_declared_hook() { |
| 29 | local link |
| 30 | link="$(readlink "$(hook "$1")")" || fail "$1 has no pre-receive symlink" |
| 31 | [ "$link" = "$(declared "$1")" ] || fail "$1 points at $link, not its declared hook" |
| 32 | [ -x "$(hook "$1")" ] || fail "$1 has a pre-receive that is not executable" |
| 33 | } |
| 34 | |
| 35 | # Three bare repositories that already exist, each in a state the module |
| 36 | # has to converge from. None of them was created by valley-init: this is |
| 37 | # the path a host takes when protection is declared over repositories |
| 38 | # that predate the declaration. |
| 39 | # |
| 40 | # guarded real history, no hooks at all — nothing to converge from |
| 41 | # released a hand-written pre-receive, which is not the module's |
| 42 | # open a stale managed hook, for protection no longer declared |
| 43 | for name in guarded released open; do |
| 44 | git init --quiet --bare "$data/$name.git" |
| 45 | done |
| 46 | git clone --quiet "$data/guarded.git" work |
| 47 | git -C work commit --quiet --allow-empty -m one |
| 48 | git -C work push --quiet origin main |
| 49 | before="$(git -C work rev-parse HEAD)" |
| 50 | |
| 51 | printf '#!/bin/sh\nexit 0\n' > "$(hook released)" |
| 52 | chmod +x "$(hook released)" |
| 53 | ln -s "$(declared guarded)" "$(hook open)" |
| 54 | |
| 55 | bash init.sh || fail "the rendered init script failed over existing repositories" |
| 56 | |
| 57 | # The declared hook is installed where there was none, and the history |
| 58 | # that was already there is untouched — an existing repository is wired, |
| 59 | # never re-initialized. |
| 60 | is_declared_hook guarded |
| 61 | [ "$(git -C "$data/guarded.git" rev-parse main)" = "$before" ] || |
| 62 | fail "guarded lost the history it already had" |
| 63 | |
| 64 | # A hook the module did not write is not the module's to replace, even |
| 65 | # where the declaration protects the project. |
| 66 | [ ! -L "$(hook released)" ] || fail "a hand-written hook was replaced by the managed one" |
| 67 | [ "$(cat "$(hook released)")" = "$(printf '#!/bin/sh\nexit 0\n')" ] || |
| 68 | fail "a hand-written hook was rewritten" |
| 69 | |
| 70 | # A project that declares no protection ends with no managed hook, |
| 71 | # whatever it was carrying before. |
| 72 | [ ! -e "$(hook open)" ] || fail "open kept a managed hook it no longer declares" |
| 73 | |
| 74 | # Running it again changes nothing, and running it over a hook that has |
| 75 | # drifted to some other store path re-points it: the script is level |
| 76 | # triggered, so what a repository carries now does not decide what it |
| 77 | # ends up with. |
| 78 | bash init.sh || fail "the rendered init script failed on a second run" |
| 79 | is_declared_hook guarded |
| 80 | ln -sfn "$(declared released)" "$(hook guarded)" |
| 81 | rm -f "$(hook released)" |
| 82 | bash init.sh || fail "the rendered init script failed over a drifted hook" |
| 83 | is_declared_hook guarded |
| 84 | is_declared_hook released |
| 85 | |
| 86 | # The converged hook is live. Pushing as a principal is pushing with the |
| 87 | # tag on; pushing with an untagged key is pushing with it off — the same |
| 88 | # substitution for sshd that protect-e2e makes. |
| 89 | git -C work commit --quiet --allow-empty -m two |
| 90 | if env -u VALLEY_PRINCIPAL git -C work push --quiet origin main 2> denied.err; then |
| 91 | fail "an untagged key wrote a protected ref of a pre-existing repository" |
| 92 | fi |
| 93 | grep -q '<untagged key>' denied.err |
| 94 | grep -q 'refs/heads/main' denied.err |
| 95 | [ "$(git -C "$data/guarded.git" rev-parse main)" = "$before" ] || |
| 96 | fail "guarded kept a ref through a rejected push" |
| 97 | VALLEY_PRINCIPAL=integrator git -C work push --quiet origin main |
| 98 | [ "$(git -C "$data/guarded.git" rev-parse main)" = "$(git -C work rev-parse HEAD)" ] || |
| 99 | fail "a declared writer could not write a protected ref" |
| 100 | |
| 101 | touch "$out" |