gunk-dev/the-valley

init-e2e.sh

source2cf4bc34a6a0main's published tip history
6.0 KiB
1# shellcheck shell=bash
2# Paths and fixtures come from the derivation environment.
3# shellcheck disable=SC2154
4export HOME="$TMPDIR"
5export GIT_CONFIG_NOSYSTEM=1
6git config --global user.name valley-check
7git config --global user.email valley-check@localhost
8git config --global init.defaultBranch main
9cd "$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.
15data="$TMPDIR/srv/git"
16sed "s|/srv/git|$data|g" "$initScriptPath" > init.sh
17mkdir -p "$data"
18
19fail() {
20 echo "init-e2e: $1" >&2
21 exit 1
22}
23hook() { echo "$data/$1.git/hooks/pre-receive"; }
24# The store path the declaration says a project's hook must be.
25declared() {
26 grep -o "/nix/store/[^ ]*-valley-protect-$1" "$initScriptPath" | head -n1
27}
28is_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
43for name in guarded released open; do
44 git init --quiet --bare "$data/$name.git"
45done
46git clone --quiet "$data/guarded.git" work
47git -C work commit --quiet --allow-empty -m one
48git -C work push --quiet origin main
49before="$(git -C work rev-parse HEAD)"
50
51printf '#!/bin/sh\nexit 0\n' > "$(hook released)"
52chmod +x "$(hook released)"
53ln -s "$(declared guarded)" "$(hook open)"
54
55bash 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.
60is_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.
78bash init.sh || fail "the rendered init script failed on a second run"
79is_declared_hook guarded
80ln -sfn "$(declared released)" "$(hook guarded)"
81rm -f "$(hook released)"
82bash init.sh || fail "the rendered init script failed over a drifted hook"
83is_declared_hook guarded
84is_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.
89git -C work commit --quiet --allow-empty -m two
90if 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"
92fi
93grep -q '<untagged key>' denied.err
94grep -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"
97VALLEY_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# The same declaration with the controllers switched on. A controller
102# writes the worktree metadata git keeps under $GIT_DIR/worktrees, and git
103# creates that directory on the first `worktree add` as whichever user got
104# there first. init has to get there first: the share sweep runs as the git
105# user, and a root a controller owns is one the git user cannot chmod.
106sed "s|/srv/git|$data|g" "$integratorInitScriptPath" > init-integrator.sh
107bash init-integrator.sh 2> share.err ||
108 fail "the rendered init script failed with controllers enabled"
109for name in guarded released; do
110 root="$data/$name.git/worktrees"
111 [ -d "$root" ] || fail "$name has no worktrees root for a controller to write into"
112 perms="$(stat -c %A "$root")"
113 # Group-writable is asserted; setgid is not. This sandbox refuses to set
114 # it — see below — and what the module asks for is pinned by module-eval
115 # over the rendered script instead.
116 [ "${perms:4:2}" = rw ] ||
117 fail "$name's worktrees root is $perms — a controller cannot write into it"
118done
119[ ! -e "$data/open.git/worktrees" ] ||
120 fail "a project no controller serves was given a worktrees root"
121
122# A share the git user is not allowed to apply must warn and leave init
123# standing. Every valley unit requires valley-init, so a failure here costs
124# the host its git service over one repository's permissions, and a
125# degraded share costs the integrator the paths it cannot write. The exit
126# status above is the assertion; this pairs a refusal with its warning
127# wherever one happened, rather than requiring one. As it stands the
128# sandbox refuses the setgid bit, so the path is exercised on every run —
129# but the check does not depend on that staying true.
130if grep -q 'Operation not permitted' share.err; then
131 grep -q 'valley-init:.*does not own' share.err ||
132 fail "a refused chmod went through without a warning"
133fi
134
135touch "$out"