gunk-dev/the-valley

module-eval.sh

source6545b7fdec50main's published tip history
8.8 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
11# The mirror publishes main and tags only — a heads glob
12# would publish every topic branch awaiting review — with
13# --prune for tag deletions and a sweep that unpublishes
14# any other head (--prune cannot: it ignores non-glob
15# refspecs). Never --mirror: it also deletes remote-only
16# refs, and GitHub's read-only refs/pull/* fails every such
17# push. Follow the hook chain from the init script to the
18# rendered push script and pin the invocation there.
19mirrorHook="$(grep -o '/nix/store/[^ ]*-valley-mirrors-[^ ]*' "$initScriptPath" | head -n1)"
20mirrorPush="$(grep -o '/nix/store/[^ ]*-valley-mirror-push-[^ ]*' "$mirrorHook" | head -n1)"
21grep -q -- 'push --prune' "$mirrorPush"
22grep -qF -- '+refs/heads/main:refs/heads/main' "$mirrorPush"
23grep -qF -- '+refs/tags/*:refs/tags/*' "$mirrorPush"
24grep -qF -- 'ls-remote --heads' "$mirrorPush"
25# shellcheck disable=SC2016 # the text pinned is the script's, not ours
26grep -qF -- 'push "$url" --delete' "$mirrorPush"
27if grep -qF -- '+refs/heads/*:refs/heads/*' "$mirrorPush"; then
28 echo "module-eval: mirror push regressed to publishing every head" >&2
29 exit 1
30fi
31if grep -q -- '--mirror' "$mirrorPush"; then
32 echo "module-eval: mirror push regressed to --mirror" >&2
33 exit 1
34fi
35# Deletions must never reach a namespace the mirror owns.
36if grep -q 'refs/pull' "$mirrorPush"; then
37 echo "module-eval: mirror push must not name refs/pull/*" >&2
38 exit 1
39fi
40
41# The rendered restic units must back up the data directory
42# to the consumer-supplied repository with the declared
43# retention over a pinned host key, on the declared cadence.
44grep -q "RESTIC_REPOSITORY_FILE=/run/agenix/valley-restic-repo" "$resticServicePath"
45grep -q -- "--keep-daily 7 --keep-weekly 4 --keep-monthly 6" "$resticServicePath"
46grep -q "UserKnownHostsFile=/var/lib/valley-backup/known_hosts" "$resticServicePath"
47grep -q "BatchMode=yes" "$resticServicePath"
48grep -q "OnCalendar=03:30" "$resticTimerPath"
49
50# The backup paths render as a --files-from list: follow
51# ExecStartPre to that list and pin the data directory.
52preStart="$(sed -n 's/^ExecStartPre=//p' "$resticServicePath" | head -n1)"
53staticPaths="$(grep -o '/nix/store/[^ ]*-staticPaths' "$preStart" | head -n1)"
54grep -qx "/srv/git" "$staticPaths"
55
56# A key's principal rides on its own authorized_keys entry
57# — that is the only thing that tells one pusher from
58# another — and a key written as a plain string is passed
59# through untouched.
60grep -q '^environment="VALLEY_PRINCIPAL=integrator" ssh-ed25519 ' "$protectedKeysPath"
61grep -q '^ssh-ed25519 .* valley-check$' "$protectedKeysPath"
62
63# The hook goes on the projects whose declaration has a
64# protection block, and nowhere else.
65grep -q "valley-protect-guarded" "$protectedInitPath"
66grep -q "valley-protect-released" "$protectedInitPath"
67if grep -q "valley-protect-open" "$protectedInitPath"; then
68 echo "module-eval: a project nobody protected was wired with the hook" >&2
69 exit 1
70fi
71
72# And what it enforces is what the declaration says, down
73# to the rendered script: released names a pattern beside
74# main, guarded takes the schema's default set. Follow the
75# hook chain from the init script to each script.
76guardedHook="$(grep -o '/nix/store/[^ ]*-valley-protect-guarded' "$protectedInitPath" | head -n1)"
77releasedHook="$(grep -o '/nix/store/[^ ]*-valley-protect-released' "$protectedInitPath" | head -n1)"
78grep -qF -- 'protected=( refs/heads/main )' "$guardedHook"
79grep -qF -- "protected=( refs/heads/main 'refs/heads/release/*' )" "$releasedHook"
80grep -qF -- 'writers=( integrator )' "$guardedHook"
81
82# A controller is given the repository it serves and the
83# identity it acts under, and nothing about policy: it reads
84# the project layer from the target tip and the instance
85# layer from its own side.
86grep -q -- '--repo /srv/git/%i.git' "$integratorUnitPath"
87grep -q -- '--key /run/agenix/valley-integrator-key' "$integratorUnitPath"
88grep -q -- '--known-signers /var/lib/valley-instance/known_signers' "$integratorUnitPath"
89# The floor's source, not a copy of it: the repository whose
90# integrated tip carries it. It is named, and it is not the
91# repository the controller serves — every controller here
92# reads the floor out of "open", which no controller serves.
93grep -q -- '--instance-repo /srv/git/open.git' "$integratorUnitPath"
94if grep -q -- '--project-policy' "$integratorUnitPath"; then
95 echo "module-eval: the module must not tell a controller where the project's policy is" >&2
96 exit 1
97fi
98# Nor where the floor is inside that repository. Both directories are the
99# integrator's defaults — policy/instance and policy/project — so a host
100# declaration that says nothing about policy gets the convention.
101if grep -q -- '--instance-policy' "$integratorUnitPath"; then
102 echo "module-eval: the module must not tell a controller where the floor is in the instance repository" >&2
103 exit 1
104fi
105
106# It does not own the repositories, so git needs an ownership
107# exception, and the rendered unit is where it has to appear:
108# GIT_CONFIG_* is command scope, which is the only scope besides
109# system and global that safe.directory is read from, and the
110# environment reaches every git the controller drives. One
111# repository, this instance's.
112# A second exception for the instance repository, which carries the
113# group's floor and is read by every controller — including the ones
114# that serve some other project.
115grep -qF -- 'Environment="GIT_CONFIG_COUNT=2"' "$integratorUnitPath"
116grep -qF -- 'Environment="GIT_CONFIG_KEY_0=safe.directory"' "$integratorUnitPath"
117grep -qF -- 'Environment="GIT_CONFIG_VALUE_0=/srv/git/%i.git"' "$integratorUnitPath"
118grep -qF -- 'Environment="GIT_CONFIG_KEY_1=safe.directory"' "$integratorUnitPath"
119grep -qF -- 'Environment="GIT_CONFIG_VALUE_1=/srv/git/open.git"' "$integratorUnitPath"
120
121# It writes refs as itself, so the repositories it serves are
122# group-shared — and only those. The sharing block names each
123# repo it touches; the repo-creation loop above it names none.
124grep -qxF -- "repo=/srv/git/guarded.git" "$integratorInitPath"
125grep -qxF -- "repo=/srv/git/released.git" "$integratorInitPath"
126grep -qF -- 'config core.sharedRepository group' "$integratorInitPath"
127# "open" is protected by nobody, so no controller serves it — and on
128# this host it is the instance repository, which every controller reads
129# the floor from. Read is the whole of that grant: no group write, and
130# no core.sharedRepository, which is a statement about who writes.
131open_block="$(awk '/^repo=/ { inside = ($0 == "repo=/srv/git/open.git") } inside' "$integratorInitPath")"
132if [ -z "$open_block" ]; then
133 echo "module-eval: the instance repository must be readable by the git group even where no controller serves it" >&2
134 exit 1
135fi
136if ! printf '%s\n' "$open_block" | grep -qF -- 'chmod -R g+rX "$repo"'; then
137 echo "module-eval: the instance repository must be made group-readable" >&2
138 exit 1
139fi
140if printf '%s\n' "$open_block" | grep -qE 'g\+rwX|sharedRepository'; then
141 echo "module-eval: a project nobody protected was made group-writable — the floor is read, never written" >&2
142 exit 1
143fi
144
145# git creates $GIT_DIR/worktrees on the first `worktree add`, owned by
146# whoever got there first. init has to get there first, because the
147# sweep below it runs as the git user and cannot chmod what a
148# controller owns. Both the creation and its order are pinned: made
149# after the repo-creation loop, before the recursive chmod that would
150# otherwise be the first thing to meet it.
151mkdir_line="$(grep -nF -- 'mkdir -p "$repo/worktrees"' "$integratorInitPath" | head -n1 | cut -d: -f1)"
152chmod_line="$(grep -nF -- 'chmod -R g+rwX "$repo"' "$integratorInitPath" | head -n1 | cut -d: -f1)"
153if [ -z "$mkdir_line" ]; then
154 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
155 exit 1
156fi
157if [ -z "$chmod_line" ] || [ "$mkdir_line" -ge "$chmod_line" ]; then
158 echo "module-eval: the worktrees root must be created before the share sweep reaches it" >&2
159 exit 1
160fi
161grep -qF -- 'chmod g+rwxs "$repo/worktrees"' "$integratorInitPath"
162
163# A chmod the git user is not allowed to make must not fail valley-init:
164# every valley unit requires it, so a failure there costs the host its
165# git service rather than one repository's group share.
166grep -qF -- 'share chmod -R g+rwX "$repo"' "$integratorInitPath"
167if ! grep -qF -- 'echo "valley-init:' "$integratorInitPath"; then
168 echo "module-eval: a share the git user cannot apply must warn, not fail — valley-init failing takes every valley unit with it" >&2
169 exit 1
170fi
171touch "$out"