gunk-dev/the-valley

module-eval.sh

sourcebe2de7158769main's published tip history
13.2 KiB
1# shellcheck shell=bash
2# Paths and fixtures come from the derivation environment.
3# shellcheck disable=SC2154
4# The repo init list and mirror hook must be generated from
5# the CUE export, and the git user's sshd Match block must
6# be terminated so it cannot scope later config.
7grep -q "the-valley" "$initScriptPath"
8grep -q "valley-mirrors" "$initScriptPath"
9grep -q "Match All" "$sshdConfigPath"
10# The git user's keys come from the declared file and nothing
11# else: not ~/.ssh/authorized_keys, not a command. Every key it
12# accepts therefore carries a tag this module wrote.
13grep -qx ' AuthorizedKeysFile /etc/ssh/authorized_keys.d/%u' "$sshdConfigPath"
14grep -qx ' AuthorizedKeysCommand none' "$sshdConfigPath"
15# sshd names the authenticated key to the session, and the git
16# user's shell derives the principal from it.
17grep -qx ' ExposeAuthInfo yes' "$sshdConfigPath"
18
19# The mirror publishes main and tags only — a heads glob
20# would publish every topic branch awaiting review — with
21# --prune for tag deletions and a sweep that unpublishes
22# any other head (--prune cannot: it ignores non-glob
23# refspecs). Never --mirror: it also deletes remote-only
24# refs, and GitHub's read-only refs/pull/* fails every such
25# push. Follow the hook chain from the init script to the
26# rendered push script and pin the invocation there.
27mirrorHook="$(grep -o '/nix/store/[^ ]*-valley-mirrors-[^ ]*' "$initScriptPath" | head -n1)"
28mirrorPush="$(grep -o '/nix/store/[^ ]*-valley-mirror-push-[^ ]*' "$mirrorHook" | head -n1)"
29grep -q -- 'push --prune' "$mirrorPush"
30grep -qF -- '+refs/heads/main:refs/heads/main' "$mirrorPush"
31grep -qF -- '+refs/tags/*:refs/tags/*' "$mirrorPush"
32grep -qF -- 'ls-remote --heads' "$mirrorPush"
33# shellcheck disable=SC2016 # the text pinned is the script's, not ours
34grep -qF -- 'push "$url" --delete' "$mirrorPush"
35if grep -qF -- '+refs/heads/*:refs/heads/*' "$mirrorPush"; then
36 echo "module-eval: mirror push regressed to publishing every head" >&2
37 exit 1
38fi
39if grep -q -- '--mirror' "$mirrorPush"; then
40 echo "module-eval: mirror push regressed to --mirror" >&2
41 exit 1
42fi
43# Deletions must never reach a namespace the mirror owns.
44if grep -q 'refs/pull' "$mirrorPush"; then
45 echo "module-eval: mirror push must not name refs/pull/*" >&2
46 exit 1
47fi
48
49# The rendered restic units must back up the data directory
50# to the consumer-supplied repository with the declared
51# retention over a pinned host key, on the declared cadence.
52grep -q "RESTIC_REPOSITORY_FILE=/run/agenix/valley-restic-repo" "$resticServicePath"
53grep -q -- "--keep-daily 7 --keep-weekly 4 --keep-monthly 6" "$resticServicePath"
54grep -q "UserKnownHostsFile=/var/lib/valley-backup/known_hosts" "$resticServicePath"
55grep -q "BatchMode=yes" "$resticServicePath"
56grep -q "OnCalendar=03:30" "$resticTimerPath"
57
58# The backup paths render as a --files-from list: follow
59# ExecStartPre to that list and pin the data directory.
60preStart="$(sed -n 's/^ExecStartPre=//p' "$resticServicePath" | head -n1)"
61staticPaths="$(grep -o '/nix/store/[^ ]*-staticPaths' "$preStart" | head -n1)"
62grep -qx "/srv/git" "$staticPaths"
63
64# A key's principal rides on its own authorized_keys entry
65# — that is the only thing that tells one pusher from
66# another — and a key written as a plain string is passed
67# through untouched.
68grep -q '^environment="VALLEY_PRINCIPAL=integrator" ssh-ed25519 ' "$protectedKeysPath"
69grep -q '^ssh-ed25519 .* valley-check$' "$protectedKeysPath"
70
71# The hook goes on every project the host serves, protected
72# or not: a protection block only adds protected refs to the
73# push policy, and "open" declares none.
74for name in sealed guarded released open; do
75 grep -q "valley-protect-$name" "$protectedInitPath"
76done
77
78# And what it enforces is what the declaration says, down
79# to the file the hook reads: released names a pattern beside
80# main and grants release tags, guarded takes the schema's
81# default set, open protects nothing. Follow the hook chain
82# from the init script to each script, and from each script to
83# its push policy, which is the declaration's protected refs,
84# writers and grants exported to JSON.
85guardedHook="$(grep -o '/nix/store/[^ ]*-valley-protect-guarded' "$protectedInitPath" | head -n1)"
86releasedHook="$(grep -o '/nix/store/[^ ]*-valley-protect-released' "$protectedInitPath" | head -n1)"
87sealedHook="$(grep -o '/nix/store/[^ ]*-valley-protect-sealed' "$protectedInitPath" | head -n1)"
88openHook="$(grep -o '/nix/store/[^ ]*-valley-protect-open' "$protectedInitPath" | head -n1)"
89policy() { grep -o '/nix/store/[^ ]*-valley-push-policy-[^ ]*\.json' "$1" | head -n1; }
90grep -qF -- '"refs":["refs/heads/main"]' "$(policy "$guardedHook")"
91grep -qF -- '"writers":["integrator"]' "$(policy "$guardedHook")"
92grep -qF -- '"refs":["refs/heads/main","refs/heads/release/*","refs/the-valley/integration-requests/*"]' "$(policy "$releasedHook")"
93grep -qF -- '"grants":{"release-tags":{"refs":["refs/tags/release/*"],"writers":["integrator"]}}' "$(policy "$releasedHook")"
94# The norm: a protected ref with no writer declared, and
95# nothing granted. Both render empty rather than missing.
96grep -qF -- '"writers":[]' "$(policy "$sealedHook")"
97grep -qF -- '"grants":{}' "$(policy "$sealedHook")"
98# A project with no protection block protects nothing, and
99# is held to the rest of the policy all the same.
100grep -qF -- '{"grants":{},"refs":[],"writers":[]}' "$(policy "$openHook")"
101
102# A project that composes a hook after the policy hands it to
103# valleyhook to run, and only that project does.
104grep -q -- '--then /nix/store/[^ ]*-valley-check-released-pre-receive' "$releasedHook"
105if grep -q -- '--then' "$guardedHook"; then
106 echo "module-eval: a project that composes no hook was handed one" >&2
107 exit 1
108fi
109
110# A hook the module did not write, or a hooks path that sends
111# git elsewhere, fails init rather than being left in place:
112# either would run instead of the policy.
113grep -qF -- 'conflicts=1' "$protectedInitPath"
114grep -qF -- 'config --get core.hooksPath' "$protectedInitPath"
115grep -qF -- 'cmp -s "$phook" "$composed"' "$protectedInitPath"
116
117# Pushes pause while the host is not converged. The record the
118# git user's shell checks is removed before anything else init
119# does, written only after the refusal above, and names the
120# configuration the shell was rendered with.
121record=/srv/git/.valley-converged
122first="$(grep -n -- "rm -f $record" "$protectedInitPath" | head -n1 | cut -d: -f1)"
123refusal="$(grep -n -- 'refusing to report this host converged' "$protectedInitPath" | head -n1 | cut -d: -f1)"
124last="$(grep -n -- "mv -f $record.tmp $record" "$protectedInitPath" | head -n1 | cut -d: -f1)"
125if [ -z "$first" ] || [ -z "$refusal" ] || [ -z "$last" ] ||
126 [ "$first" -ge "$refusal" ] || [ "$refusal" -ge "$last" ]; then
127 echo "module-eval: init must remove the converged record first and write it only after the refusal" >&2
128 exit 1
129fi
130id="$(awk -v r="$record.tmp" '$1 == "printf" && $NF == r { print $3 }' "$protectedInitPath")"
131[ -n "$id" ] || { echo "module-eval: init writes no configuration id into the record" >&2; exit 1; }
132grep -qF -- "--converged $record" "$protectedShell"
133grep -qF -- "--expect $id" "$protectedShell"
134grep -qF -- '--hold /srv/git/.valley-hold' "$protectedShell"
135if grep -q -- '.valley-hold' "$protectedInitPath"; then
136 echo "module-eval: init must never touch the operator's hold" >&2
137 exit 1
138fi
139grep -qF -- '--authorized-keys /etc/ssh/authorized_keys.d/git' "$protectedShell"
140grep -qF -- 'valleyhook shell' "$protectedShell"
141
142# The rules are valleyhook's, and the script is only how git
143# reaches it: it hands over the principal sshd tagged the
144# push with, and nothing it decides itself.
145grep -qF -- 'valleyhook pre-receive' "$guardedHook"
146# shellcheck disable=SC2016 # the text pinned is the script's, not ours
147grep -qF -- '--principal="${VALLEY_PRINCIPAL:-}"' "$guardedHook"
148if grep -qE '^ *(case|for|while|if) ' "$guardedHook"; then
149 echo "module-eval: the rendered hook decides something itself — the policy belongs to valleyhook" >&2
150 exit 1
151fi
152
153# The grants it reads. A grant declared by hand renders into
154# a file in the format the compiler writes, and a host that
155# compiles no registry reads that file alone.
156grants="$(grep -o '/nix/store/[^ ]*-valley-grants' "$guardedHook" | head -n1)"
157grep -qx 'request requester' "$grants"
158if grep -q '/var/lib/valley-identity' "$guardedHook"; then
159 echo "module-eval: a host that compiles no registry reads compiled grants" >&2
160 exit 1
161fi
162# A host that compiles one reads the compiled grants beside
163# the declared ones, which a compiled file can only add to.
164identityHook="$(grep -o '/nix/store/[^ ]*-valley-protect-guarded' "$identityInitPath" | head -n1)"
165grep -qF -- '--grants /var/lib/valley-identity/grants' "$identityHook"
166grep -q -- '-valley-grants ' "$identityHook"
167
168# Replacement refs a push wrote before the hook refused them
169# are reported on every activation, in every repository.
170grep -qF -- 'for-each-ref' "$protectedInitPath"
171grep -qF -- 'refs/replace/' "$protectedInitPath"
172
173# A controller is given the repository it serves and the
174# identity it acts under, and nothing about policy: it reads
175# the project layer from the target tip and the instance
176# layer from its own side.
177grep -q -- '--repo /srv/git/%i.git' "$integratorUnitPath"
178grep -q -- '--key /run/agenix/valley-integrator-key' "$integratorUnitPath"
179grep -q -- '--known-signers /var/lib/valley-instance/known_signers' "$integratorUnitPath"
180# The floor's source, not a copy of it: the repository whose
181# integrated tip carries it. It is named, and for every
182# controller here but sealed's it is not the repository the
183# controller serves.
184grep -q -- '--instance-repo /srv/git/sealed.git' "$integratorUnitPath"
185if grep -q -- '--project-policy' "$integratorUnitPath"; then
186 echo "module-eval: the module must not tell a controller where the project's policy is" >&2
187 exit 1
188fi
189# Nor where the floor is inside that repository. Both directories are the
190# integrator's defaults — policy/instance and policy/project — so a host
191# declaration that says nothing about policy gets the convention.
192if grep -q -- '--instance-policy' "$integratorUnitPath"; then
193 echo "module-eval: the module must not tell a controller where the floor is in the instance repository" >&2
194 exit 1
195fi
196
197# It does not own the repositories, so git needs an ownership
198# exception, and the rendered unit is where it has to appear:
199# GIT_CONFIG_* is command scope, which is the only scope besides
200# system and global that safe.directory is read from, and the
201# environment reaches every git the controller drives. One
202# repository, this instance's.
203# A second exception for the instance repository, which carries the
204# group's floor and is read by every controller — including the ones
205# that serve some other project.
206grep -qF -- 'Environment="GIT_CONFIG_COUNT=2"' "$integratorUnitPath"
207grep -qF -- 'Environment="GIT_CONFIG_KEY_0=safe.directory"' "$integratorUnitPath"
208grep -qF -- 'Environment="GIT_CONFIG_VALUE_0=/srv/git/%i.git"' "$integratorUnitPath"
209grep -qF -- 'Environment="GIT_CONFIG_KEY_1=safe.directory"' "$integratorUnitPath"
210grep -qF -- 'Environment="GIT_CONFIG_VALUE_1=/srv/git/sealed.git"' "$integratorUnitPath"
211
212# It writes refs as itself, so the repositories it serves are
213# group-shared — and only those. The sharing block names each
214# repo it touches; the repo-creation loop above it names none.
215grep -qxF -- "repo=/srv/git/guarded.git" "$integratorInitPath"
216grep -qxF -- "repo=/srv/git/released.git" "$integratorInitPath"
217grep -qF -- 'config core.sharedRepository group' "$integratorInitPath"
218# The instance repository declares protection, so a controller serves
219# it and it is shared like every other served repository.
220grep -qxF -- "repo=/srv/git/sealed.git" "$integratorInitPath"
221# "open" is protected by nobody, so no controller serves it and nothing
222# shares it.
223if grep -qxF -- "repo=/srv/git/open.git" "$integratorInitPath"; then
224 echo "module-eval: a project nobody protected was made group-writable" >&2
225 exit 1
226fi
227
228# git creates $GIT_DIR/worktrees on the first `worktree add`, owned by
229# whoever got there first. init has to get there first, because the
230# sweep below it runs as the git user and cannot chmod what a
231# controller owns. Both the creation and its order are pinned: made
232# after the repo-creation loop, before the recursive chmod that would
233# otherwise be the first thing to meet it.
234mkdir_line="$(grep -nF -- 'mkdir -p "$repo/worktrees"' "$integratorInitPath" | head -n1 | cut -d: -f1)"
235chmod_line="$(grep -nF -- 'chmod -R g+rwX "$repo"' "$integratorInitPath" | head -n1 | cut -d: -f1)"
236if [ -z "$mkdir_line" ]; then
237 echo "module-eval: valley-init must create each served repository's worktrees root — a controller that creates it first owns a directory the git user cannot chmod" >&2
238 exit 1
239fi
240if [ -z "$chmod_line" ] || [ "$mkdir_line" -ge "$chmod_line" ]; then
241 echo "module-eval: the worktrees root must be created before the share sweep reaches it" >&2
242 exit 1
243fi
244grep -qF -- 'chmod g+rwxs "$repo/worktrees"' "$integratorInitPath"
245
246# A chmod the git user is not allowed to make must not fail valley-init:
247# every valley unit requires it, so a failure there costs the host its
248# git service rather than one repository's group share.
249grep -qF -- 'share chmod -R g+rwX "$repo"' "$integratorInitPath"
250if ! grep -qF -- 'echo "valley-init:' "$integratorInitPath"; then
251 echo "module-eval: a share the git user cannot apply must warn, not fail — valley-init failing takes every valley unit with it" >&2
252 exit 1
253fi
254touch "$out"