gunk-dev/the-valley

module-eval.sh

source88696b8bfad2main's published tip history
7.1 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"
89grep -q -- '--instance /var/lib/valley-instance/policy' "$integratorUnitPath"
90if grep -q -- '--project-policy' "$integratorUnitPath"; then
91 echo "module-eval: the module must not tell a controller where the project's policy is" >&2
92 exit 1
93fi
94
95# It does not own the repositories, so git needs an ownership
96# exception, and the rendered unit is where it has to appear:
97# GIT_CONFIG_* is command scope, which is the only scope besides
98# system and global that safe.directory is read from, and the
99# environment reaches every git the controller drives. One
100# repository, this instance's.
101grep -qF -- 'Environment="GIT_CONFIG_COUNT=1"' "$integratorUnitPath"
102grep -qF -- 'Environment="GIT_CONFIG_KEY_0=safe.directory"' "$integratorUnitPath"
103grep -qF -- 'Environment="GIT_CONFIG_VALUE_0=/srv/git/%i.git"' "$integratorUnitPath"
104
105# It writes refs as itself, so the repositories it serves are
106# group-shared — and only those. The sharing block names each
107# repo it touches; the repo-creation loop above it names none.
108grep -qxF -- "repo=/srv/git/guarded.git" "$integratorInitPath"
109grep -qxF -- "repo=/srv/git/released.git" "$integratorInitPath"
110grep -qF -- 'config core.sharedRepository group' "$integratorInitPath"
111if grep -qxF -- "repo=/srv/git/open.git" "$integratorInitPath"; then
112 echo "module-eval: a project nobody protected was made group-shared" >&2
113 exit 1
114fi
115
116# git creates $GIT_DIR/worktrees on the first `worktree add`, owned by
117# whoever got there first. init has to get there first, because the
118# sweep below it runs as the git user and cannot chmod what a
119# controller owns. Both the creation and its order are pinned: made
120# after the repo-creation loop, before the recursive chmod that would
121# otherwise be the first thing to meet it.
122mkdir_line="$(grep -nF -- 'mkdir -p "$repo/worktrees"' "$integratorInitPath" | head -n1 | cut -d: -f1)"
123chmod_line="$(grep -nF -- 'chmod -R g+rwX "$repo"' "$integratorInitPath" | head -n1 | cut -d: -f1)"
124if [ -z "$mkdir_line" ]; then
125 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
126 exit 1
127fi
128if [ -z "$chmod_line" ] || [ "$mkdir_line" -ge "$chmod_line" ]; then
129 echo "module-eval: the worktrees root must be created before the share sweep reaches it" >&2
130 exit 1
131fi
132grep -qF -- 'chmod g+rwxs "$repo/worktrees"' "$integratorInitPath"
133
134# A chmod the git user is not allowed to make must not fail valley-init:
135# every valley unit requires it, so a failure there costs the host its
136# git service rather than one repository's group share.
137grep -qF -- 'share chmod -R g+rwX "$repo"' "$integratorInitPath"
138if ! grep -qF -- 'echo "valley-init:' "$integratorInitPath"; then
139 echo "module-eval: a share the git user cannot apply must warn, not fail — valley-init failing takes every valley unit with it" >&2
140 exit 1
141fi
142touch "$out"