| 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 the module is deployed over repositories that |
| 38 | # predate it. |
| 39 | # |
| 40 | # guarded real history, no hooks at all — nothing to converge from |
| 41 | # released a hand-written pre-receive identical to the hook released |
| 42 | # declares it composes after the push policy |
| 43 | # open a stale managed hook: a store symlink to another hook |
| 44 | for name in guarded released open; do |
| 45 | git init --quiet --bare "$data/$name.git" |
| 46 | done |
| 47 | git clone --quiet "$data/guarded.git" work |
| 48 | git -C work commit --quiet --allow-empty -m one |
| 49 | git -C work push --quiet origin main |
| 50 | before="$(git -C work rev-parse HEAD)" |
| 51 | |
| 52 | composed="$(grep -o -- '--then /nix/store/[^ ]*' "$(declared released)" | cut -d' ' -f2)" |
| 53 | [ -n "$composed" ] || fail "released's managed hook names no composed hook" |
| 54 | cp "$composed" "$(hook released)" |
| 55 | chmod +x "$(hook released)" |
| 56 | ln -s "$(declared guarded)" "$(hook open)" |
| 57 | |
| 58 | # A replacement ref written before any hook refused one. The hook cannot |
| 59 | # remove what is already there, so init reports it on every activation and |
| 60 | # leaves it in place: what it is evidence of is the operator's to read. |
| 61 | decoy="$(git -C "$data/guarded.git" commit-tree "$before^{tree}" -m decoy)" |
| 62 | git -C "$data/guarded.git" update-ref "refs/replace/$before" "$decoy" |
| 63 | |
| 64 | bash init.sh 2> init.err || fail "the rendered init script failed over existing repositories: $(cat init.err)" |
| 65 | grep -q "guarded.git holds replacement refs" init.err || |
| 66 | fail "a replacement ref already in a repository was not reported" |
| 67 | grep -q "refs/replace/$before" init.err || |
| 68 | fail "the report did not name the replacement ref" |
| 69 | git -C "$data/guarded.git" rev-parse --verify --quiet "refs/replace/$before" > /dev/null || |
| 70 | fail "init deleted a replacement ref instead of reporting it" |
| 71 | git -C "$data/guarded.git" update-ref -d "refs/replace/$before" |
| 72 | |
| 73 | # The declared hook is installed where there was none, and the history |
| 74 | # that was already there is untouched — an existing repository is wired, |
| 75 | # never re-initialized. |
| 76 | is_declared_hook guarded |
| 77 | [ "$(git -C "$data/guarded.git" rev-parse main)" = "$before" ] || |
| 78 | fail "guarded lost the history it already had" |
| 79 | |
| 80 | # A hand-written hook that is byte for byte the one released composes is |
| 81 | # the one case init replaces: the managed hook runs it after the policy. |
| 82 | is_declared_hook released |
| 83 | grep -q "is the one services.valley.extraPreReceive.released composes" init.err || |
| 84 | fail "a composed hook was replaced without saying so" |
| 85 | |
| 86 | # A project that declares no protection gets the managed hook all the |
| 87 | # same — the push policy is every project's — re-pointed from the stale |
| 88 | # store path it carried. |
| 89 | is_declared_hook open |
| 90 | |
| 91 | # Each conflict fails init. It says which repository and why, leaves what |
| 92 | # it found in place, and still converges every repository it can. |
| 93 | conflict() { |
| 94 | local what="$1" says="$2" |
| 95 | if bash init.sh 2> conflict.err; then |
| 96 | fail "init reported the host converged over $what" |
| 97 | fi |
| 98 | grep -qF -- "$says" conflict.err || fail "the refusal over $what did not say \"$says\": $(cat conflict.err)" |
| 99 | grep -q 'refusing to report this host converged' conflict.err || |
| 100 | fail "init failed over $what without saying the host is not converged" |
| 101 | } |
| 102 | exit_zero="$(printf '#!/bin/sh\nexit 0\n')" |
| 103 | |
| 104 | # A hook nobody declared, which would run instead of the policy. |
| 105 | rm "$(hook guarded)" |
| 106 | printf '%s\n' "$exit_zero" > "$(hook guarded)" |
| 107 | chmod +x "$(hook guarded)" |
| 108 | ln -sfn "$(declared guarded)" "$(hook open)" |
| 109 | conflict "a hand-written hook" "guarded: $(hook guarded) is a pre-receive hook this module did not write" |
| 110 | [ "$(cat "$(hook guarded)")" = "$exit_zero" ] || fail "init rewrote a hook it refused" |
| 111 | is_declared_hook open |
| 112 | rm "$(hook guarded)" |
| 113 | |
| 114 | # A hand-written hook where a project composes one, that is not the one it |
| 115 | # composes. |
| 116 | rm "$(hook released)" |
| 117 | printf '%s\n' "$exit_zero" > "$(hook released)" |
| 118 | chmod +x "$(hook released)" |
| 119 | conflict "a hook that differs from the declared composition" "released: $(hook released) is a pre-receive hook this module did not write" |
| 120 | rm "$(hook released)" |
| 121 | |
| 122 | # A hooks path, from the repository's config, from a file it includes, and |
| 123 | # from the git user's own config: each sends git to look for hooks |
| 124 | # somewhere the managed hook is not. |
| 125 | git -C "$data/guarded.git" config core.hooksPath "$TMPDIR/elsewhere" |
| 126 | conflict "a repository's core.hooksPath" "guarded: core.hooksPath is set for $data/guarded.git (to '$TMPDIR/elsewhere')" |
| 127 | git -C "$data/guarded.git" config --unset core.hooksPath |
| 128 | printf '[core]\n\thooksPath = %s\n' "$TMPDIR/included" > "$TMPDIR/hooks.inc" |
| 129 | git -C "$data/guarded.git" config include.path "$TMPDIR/hooks.inc" |
| 130 | conflict "an included core.hooksPath" "guarded: core.hooksPath is set for $data/guarded.git (to '$TMPDIR/included')" |
| 131 | git -C "$data/guarded.git" config --unset include.path |
| 132 | git config --global core.hooksPath "$TMPDIR/global" |
| 133 | conflict "the git user's core.hooksPath" "core.hooksPath is set for $data/guarded.git (to '$TMPDIR/global')" |
| 134 | git config --global --unset core.hooksPath |
| 135 | # An empty value is a value: git then looks for hooks relative to where it |
| 136 | # runs, which is not where the managed hook is. |
| 137 | git -C "$data/guarded.git" config core.hooksPath "" |
| 138 | conflict "an empty core.hooksPath" "core.hooksPath is set for $data/guarded.git (to '')" |
| 139 | git -C "$data/guarded.git" config --unset core.hooksPath |
| 140 | |
| 141 | # While init is failing, the record the git user's shell lets pushes |
| 142 | # through on is gone: a failed convergence leaves no write path open. |
| 143 | [ ! -e "$data/.valley-converged" ] || fail "a failed init left the converged record in place" |
| 144 | |
| 145 | # With every conflict gone, init converges, and running it again changes |
| 146 | # nothing: the script is level triggered, so what a repository carries now |
| 147 | # does not decide what it ends up with. |
| 148 | bash init.sh || fail "the rendered init script failed once every conflict was gone" |
| 149 | # Converged, and the record names this configuration. |
| 150 | id="$(awk -v r="$data/.valley-converged.tmp" '$1 == "printf" && $NF == r { print $3 }' init.sh)" |
| 151 | [ -n "$id" ] && [ "$(cat "$data/.valley-converged")" = "$id" ] || |
| 152 | fail "a converged init did not record the configuration it converged on" |
| 153 | bash init.sh || fail "the rendered init script failed on a second run" |
| 154 | for name in guarded released open; do |
| 155 | is_declared_hook "$name" |
| 156 | done |
| 157 | ln -sfn "$(declared released)" "$(hook guarded)" |
| 158 | bash init.sh || fail "the rendered init script failed over a drifted hook" |
| 159 | is_declared_hook guarded |
| 160 | |
| 161 | # The converged hook is live. Pushing as a principal is pushing with the |
| 162 | # tag on; pushing with an untagged key is pushing with it off — the same |
| 163 | # substitution for sshd that protect-e2e makes. |
| 164 | git -C work commit --quiet --allow-empty -m two |
| 165 | if env -u VALLEY_PRINCIPAL git -C work push --quiet origin main 2> denied.err; then |
| 166 | fail "an untagged key wrote a protected ref of a pre-existing repository" |
| 167 | fi |
| 168 | grep -q '<untagged key>' denied.err |
| 169 | grep -q 'refs/heads/main' denied.err |
| 170 | [ "$(git -C "$data/guarded.git" rev-parse main)" = "$before" ] || |
| 171 | fail "guarded kept a ref through a rejected push" |
| 172 | VALLEY_PRINCIPAL=integrator git -C work push --quiet origin main |
| 173 | [ "$(git -C "$data/guarded.git" rev-parse main)" = "$(git -C work rev-parse HEAD)" ] || |
| 174 | fail "a declared writer could not write a protected ref" |
| 175 | |
| 176 | # A host without an instance repository publishes no project list. |
| 177 | for name in sealed guarded released open; do |
| 178 | [ ! -e "$data/$name.git" ] || [ -z "$(git -C "$data/$name.git" for-each-ref refs/the-valley/projects)" ] || |
| 179 | fail "$name carries a project list, on a host with no instance repository" |
| 180 | done |
| 181 | |
| 182 | # The same declaration with the controllers switched on. A controller |
| 183 | # writes the worktree metadata git keeps under $GIT_DIR/worktrees, and git |
| 184 | # creates that directory on the first `worktree add` as whichever user got |
| 185 | # there first. init has to get there first: the share sweep runs as the git |
| 186 | # user, and a root a controller owns is one the git user cannot chmod. |
| 187 | sed "s|/srv/git|$data|g" "$integratorInitScriptPath" > init-integrator.sh |
| 188 | bash init-integrator.sh 2> share.err || |
| 189 | fail "the rendered init script failed with controllers enabled" |
| 190 | for name in guarded released; do |
| 191 | root="$data/$name.git/worktrees" |
| 192 | [ -d "$root" ] || fail "$name has no worktrees root for a controller to write into" |
| 193 | perms="$(stat -c %A "$root")" |
| 194 | # Group-writable is asserted; setgid is not. This sandbox refuses to set |
| 195 | # it — see below — and what the module asks for is pinned by module-eval |
| 196 | # over the rendered script instead. |
| 197 | [ "${perms:4:2}" = rw ] || |
| 198 | fail "$name's worktrees root is $perms — a controller cannot write into it" |
| 199 | done |
| 200 | [ ! -e "$data/open.git/worktrees" ] || |
| 201 | fail "a project no controller serves was given a worktrees root" |
| 202 | |
| 203 | # With an instance repository, init publishes the host's project list in |
| 204 | # it: a blob at refs/the-valley/projects naming exactly the enabled git |
| 205 | # projects, sorted, one per line. valley changes --all discovers the |
| 206 | # host's projects by it. |
| 207 | [ "$(git -C "$data/sealed.git" cat-file -t refs/the-valley/projects)" = blob ] || |
| 208 | fail "the instance repository publishes no project list blob" |
| 209 | printf '%s\n' guarded open released sealed > projects.want |
| 210 | git -C "$data/sealed.git" cat-file blob refs/the-valley/projects > projects.got |
| 211 | cmp -s projects.want projects.got || |
| 212 | fail "the project list is not exactly the enabled git projects: $(cat projects.got)" |
| 213 | for name in guarded released open; do |
| 214 | [ -z "$(git -C "$data/$name.git" for-each-ref refs/the-valley/projects)" ] || |
| 215 | fail "$name carries a project list, and only the instance repository may" |
| 216 | done |
| 217 | |
| 218 | # A share the git user is not allowed to apply must warn and leave init |
| 219 | # standing. Every valley unit requires valley-init, so a failure here costs |
| 220 | # the host its git service over one repository's permissions, and a |
| 221 | # degraded share costs the integrator the paths it cannot write. The exit |
| 222 | # status above is the assertion; this pairs a refusal with its warning |
| 223 | # wherever one happened, rather than requiring one. As it stands the |
| 224 | # sandbox refuses the setgid bit, so the path is exercised on every run — |
| 225 | # but the check does not depend on that staying true. |
| 226 | if grep -q 'Operation not permitted' share.err; then |
| 227 | grep -q 'valley-init:.*does not own' share.err || |
| 228 | fail "a refused chmod went through without a warning" |
| 229 | fi |
| 230 | |
| 231 | touch "$out" |