gunk-dev/the-valley

init-e2e.sh

source8ea96b8e18d0main's published tip history
10.7 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 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
44for name in guarded released open; do
45 git init --quiet --bare "$data/$name.git"
46done
47git clone --quiet "$data/guarded.git" work
48git -C work commit --quiet --allow-empty -m one
49git -C work push --quiet origin main
50before="$(git -C work rev-parse HEAD)"
51
52composed="$(grep -o -- '--then /nix/store/[^ ]*' "$(declared released)" | cut -d' ' -f2)"
53[ -n "$composed" ] || fail "released's managed hook names no composed hook"
54cp "$composed" "$(hook released)"
55chmod +x "$(hook released)"
56ln -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.
61decoy="$(git -C "$data/guarded.git" commit-tree "$before^{tree}" -m decoy)"
62git -C "$data/guarded.git" update-ref "refs/replace/$before" "$decoy"
63
64bash init.sh 2> init.err || fail "the rendered init script failed over existing repositories: $(cat init.err)"
65grep -q "guarded.git holds replacement refs" init.err ||
66 fail "a replacement ref already in a repository was not reported"
67grep -q "refs/replace/$before" init.err ||
68 fail "the report did not name the replacement ref"
69git -C "$data/guarded.git" rev-parse --verify --quiet "refs/replace/$before" > /dev/null ||
70 fail "init deleted a replacement ref instead of reporting it"
71git -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.
76is_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.
82is_declared_hook released
83grep -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.
89is_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.
93conflict() {
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}
102exit_zero="$(printf '#!/bin/sh\nexit 0\n')"
103
104# A hook nobody declared, which would run instead of the policy.
105rm "$(hook guarded)"
106printf '%s\n' "$exit_zero" > "$(hook guarded)"
107chmod +x "$(hook guarded)"
108ln -sfn "$(declared guarded)" "$(hook open)"
109conflict "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"
111is_declared_hook open
112rm "$(hook guarded)"
113
114# A hand-written hook where a project composes one, that is not the one it
115# composes.
116rm "$(hook released)"
117printf '%s\n' "$exit_zero" > "$(hook released)"
118chmod +x "$(hook released)"
119conflict "a hook that differs from the declared composition" "released: $(hook released) is a pre-receive hook this module did not write"
120rm "$(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.
125git -C "$data/guarded.git" config core.hooksPath "$TMPDIR/elsewhere"
126conflict "a repository's core.hooksPath" "guarded: core.hooksPath is set for $data/guarded.git (to '$TMPDIR/elsewhere')"
127git -C "$data/guarded.git" config --unset core.hooksPath
128printf '[core]\n\thooksPath = %s\n' "$TMPDIR/included" > "$TMPDIR/hooks.inc"
129git -C "$data/guarded.git" config include.path "$TMPDIR/hooks.inc"
130conflict "an included core.hooksPath" "guarded: core.hooksPath is set for $data/guarded.git (to '$TMPDIR/included')"
131git -C "$data/guarded.git" config --unset include.path
132git config --global core.hooksPath "$TMPDIR/global"
133conflict "the git user's core.hooksPath" "core.hooksPath is set for $data/guarded.git (to '$TMPDIR/global')"
134git 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.
137git -C "$data/guarded.git" config core.hooksPath ""
138conflict "an empty core.hooksPath" "core.hooksPath is set for $data/guarded.git (to '')"
139git -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.
148bash init.sh || fail "the rendered init script failed once every conflict was gone"
149# Converged, and the record names this configuration.
150id="$(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"
153bash init.sh || fail "the rendered init script failed on a second run"
154for name in guarded released open; do
155 is_declared_hook "$name"
156done
157ln -sfn "$(declared released)" "$(hook guarded)"
158bash init.sh || fail "the rendered init script failed over a drifted hook"
159is_declared_hook guarded
160# The declared hooks run a stand-in for valleyhook, so no push is made
161# through them here. protect-e2e runs this init script with the real
162# valleyhook and pushes through the hook it installs.
163
164# A host without an instance repository publishes no project list.
165for name in sealed guarded released open; do
166 [ ! -e "$data/$name.git" ] || [ -z "$(git -C "$data/$name.git" for-each-ref refs/the-valley/projects)" ] ||
167 fail "$name carries a project list, on a host with no instance repository"
168done
169
170# The same declaration with the controllers switched on. A controller
171# writes the worktree metadata git keeps under $GIT_DIR/worktrees, and git
172# creates that directory on the first `worktree add` as whichever user got
173# there first. init has to get there first: the share sweep runs as the git
174# user, and a root a controller owns is one the git user cannot chmod.
175sed "s|/srv/git|$data|g" "$integratorInitScriptPath" > init-integrator.sh
176bash init-integrator.sh 2> share.err ||
177 fail "the rendered init script failed with controllers enabled"
178for name in guarded released; do
179 root="$data/$name.git/worktrees"
180 [ -d "$root" ] || fail "$name has no worktrees root for a controller to write into"
181 perms="$(stat -c %A "$root")"
182 # Group-writable is asserted; setgid is not. This sandbox refuses to set
183 # it — see below — and what the module asks for is pinned by module-eval
184 # over the rendered script instead.
185 [ "${perms:4:2}" = rw ] ||
186 fail "$name's worktrees root is $perms — a controller cannot write into it"
187done
188[ ! -e "$data/open.git/worktrees" ] ||
189 fail "a project no controller serves was given a worktrees root"
190
191# With an instance repository, init publishes the host's project list in
192# it: a blob at refs/the-valley/projects naming exactly the enabled git
193# projects, sorted, one per line. valley changes --all discovers the
194# host's projects by it.
195[ "$(git -C "$data/sealed.git" cat-file -t refs/the-valley/projects)" = blob ] ||
196 fail "the instance repository publishes no project list blob"
197printf '%s\n' guarded open released sealed > projects.want
198git -C "$data/sealed.git" cat-file blob refs/the-valley/projects > projects.got
199cmp -s projects.want projects.got ||
200 fail "the project list is not exactly the enabled git projects: $(cat projects.got)"
201for name in guarded released open; do
202 [ -z "$(git -C "$data/$name.git" for-each-ref refs/the-valley/projects)" ] ||
203 fail "$name carries a project list, and only the instance repository may"
204done
205
206# A share the git user is not allowed to apply must warn and leave init
207# standing. Every valley unit requires valley-init, so a failure here costs
208# the host its git service over one repository's permissions, and a
209# degraded share costs the integrator the paths it cannot write. The exit
210# status above is the assertion; this pairs a refusal with its warning
211# wherever one happened, rather than requiring one. As it stands the
212# sandbox refuses the setgid bit, so the path is exercised on every run —
213# but the check does not depend on that staying true.
214if grep -q 'Operation not permitted' share.err; then
215 grep -q 'valley-init:.*does not own' share.err ||
216 fail "a refused chmod went through without a warning"
217fi
218
219touch "$out"