gunk-dev/the-valley

init-e2e.sh

sourceef0e6a83672emain's published tip history
6.8 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
55# A replacement ref written before any hook refused one. The hook cannot
56# remove what is already there, so init reports it on every activation and
57# leaves it in place: what it is evidence of is the operator's to read.
58decoy="$(git -C "$data/guarded.git" commit-tree "$before^{tree}" -m decoy)"
59git -C "$data/guarded.git" update-ref "refs/replace/$before" "$decoy"
60
61bash init.sh 2> init.err || fail "the rendered init script failed over existing repositories"
62grep -q "guarded.git holds replacement refs" init.err ||
63 fail "a replacement ref already in a repository was not reported"
64grep -q "refs/replace/$before" init.err ||
65 fail "the report did not name the replacement ref"
66git -C "$data/guarded.git" rev-parse --verify --quiet "refs/replace/$before" > /dev/null ||
67 fail "init deleted a replacement ref instead of reporting it"
68git -C "$data/guarded.git" update-ref -d "refs/replace/$before"
69
70# The declared hook is installed where there was none, and the history
71# that was already there is untouched — an existing repository is wired,
72# never re-initialized.
73is_declared_hook guarded
74[ "$(git -C "$data/guarded.git" rev-parse main)" = "$before" ] ||
75 fail "guarded lost the history it already had"
76
77# A hook the module did not write is not the module's to replace, even
78# where the declaration protects the project.
79[ ! -L "$(hook released)" ] || fail "a hand-written hook was replaced by the managed one"
80[ "$(cat "$(hook released)")" = "$(printf '#!/bin/sh\nexit 0\n')" ] ||
81 fail "a hand-written hook was rewritten"
82
83# A project that declares no protection ends with no managed hook,
84# whatever it was carrying before.
85[ ! -e "$(hook open)" ] || fail "open kept a managed hook it no longer declares"
86
87# Running it again changes nothing, and running it over a hook that has
88# drifted to some other store path re-points it: the script is level
89# triggered, so what a repository carries now does not decide what it
90# ends up with.
91bash init.sh || fail "the rendered init script failed on a second run"
92is_declared_hook guarded
93ln -sfn "$(declared released)" "$(hook guarded)"
94rm -f "$(hook released)"
95bash init.sh || fail "the rendered init script failed over a drifted hook"
96is_declared_hook guarded
97is_declared_hook released
98
99# The converged hook is live. Pushing as a principal is pushing with the
100# tag on; pushing with an untagged key is pushing with it off — the same
101# substitution for sshd that protect-e2e makes.
102git -C work commit --quiet --allow-empty -m two
103if env -u VALLEY_PRINCIPAL git -C work push --quiet origin main 2> denied.err; then
104 fail "an untagged key wrote a protected ref of a pre-existing repository"
105fi
106grep -q '<untagged key>' denied.err
107grep -q 'refs/heads/main' denied.err
108[ "$(git -C "$data/guarded.git" rev-parse main)" = "$before" ] ||
109 fail "guarded kept a ref through a rejected push"
110VALLEY_PRINCIPAL=integrator git -C work push --quiet origin main
111[ "$(git -C "$data/guarded.git" rev-parse main)" = "$(git -C work rev-parse HEAD)" ] ||
112 fail "a declared writer could not write a protected ref"
113
114# The same declaration with the controllers switched on. A controller
115# writes the worktree metadata git keeps under $GIT_DIR/worktrees, and git
116# creates that directory on the first `worktree add` as whichever user got
117# there first. init has to get there first: the share sweep runs as the git
118# user, and a root a controller owns is one the git user cannot chmod.
119sed "s|/srv/git|$data|g" "$integratorInitScriptPath" > init-integrator.sh
120bash init-integrator.sh 2> share.err ||
121 fail "the rendered init script failed with controllers enabled"
122for name in guarded released; do
123 root="$data/$name.git/worktrees"
124 [ -d "$root" ] || fail "$name has no worktrees root for a controller to write into"
125 perms="$(stat -c %A "$root")"
126 # Group-writable is asserted; setgid is not. This sandbox refuses to set
127 # it — see below — and what the module asks for is pinned by module-eval
128 # over the rendered script instead.
129 [ "${perms:4:2}" = rw ] ||
130 fail "$name's worktrees root is $perms — a controller cannot write into it"
131done
132[ ! -e "$data/open.git/worktrees" ] ||
133 fail "a project no controller serves was given a worktrees root"
134
135# A share the git user is not allowed to apply must warn and leave init
136# standing. Every valley unit requires valley-init, so a failure here costs
137# the host its git service over one repository's permissions, and a
138# degraded share costs the integrator the paths it cannot write. The exit
139# status above is the assertion; this pairs a refusal with its warning
140# wherever one happened, rather than requiring one. As it stands the
141# sandbox refuses the setgid bit, so the path is exercised on every run —
142# but the check does not depend on that staying true.
143if grep -q 'Operation not permitted' share.err; then
144 grep -q 'valley-init:.*does not own' share.err ||
145 fail "a refused chmod went through without a warning"
146fi
147
148touch "$out"