| 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. |
| 7 | grep -q "the-valley" "$initScriptPath" |
| 8 | grep -q "valley-mirrors" "$initScriptPath" |
| 9 | grep -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. |
| 19 | mirrorHook="$(grep -o '/nix/store/[^ ]*-valley-mirrors-[^ ]*' "$initScriptPath" | head -n1)" |
| 20 | mirrorPush="$(grep -o '/nix/store/[^ ]*-valley-mirror-push-[^ ]*' "$mirrorHook" | head -n1)" |
| 21 | grep -q -- 'push --prune' "$mirrorPush" |
| 22 | grep -qF -- '+refs/heads/main:refs/heads/main' "$mirrorPush" |
| 23 | grep -qF -- '+refs/tags/*:refs/tags/*' "$mirrorPush" |
| 24 | grep -qF -- 'ls-remote --heads' "$mirrorPush" |
| 25 | # shellcheck disable=SC2016 # the text pinned is the script's, not ours |
| 26 | grep -qF -- 'push "$url" --delete' "$mirrorPush" |
| 27 | if grep -qF -- '+refs/heads/*:refs/heads/*' "$mirrorPush"; then |
| 28 | echo "module-eval: mirror push regressed to publishing every head" >&2 |
| 29 | exit 1 |
| 30 | fi |
| 31 | if grep -q -- '--mirror' "$mirrorPush"; then |
| 32 | echo "module-eval: mirror push regressed to --mirror" >&2 |
| 33 | exit 1 |
| 34 | fi |
| 35 | # Deletions must never reach a namespace the mirror owns. |
| 36 | if grep -q 'refs/pull' "$mirrorPush"; then |
| 37 | echo "module-eval: mirror push must not name refs/pull/*" >&2 |
| 38 | exit 1 |
| 39 | fi |
| 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. |
| 44 | grep -q "RESTIC_REPOSITORY_FILE=/run/agenix/valley-restic-repo" "$resticServicePath" |
| 45 | grep -q -- "--keep-daily 7 --keep-weekly 4 --keep-monthly 6" "$resticServicePath" |
| 46 | grep -q "UserKnownHostsFile=/var/lib/valley-backup/known_hosts" "$resticServicePath" |
| 47 | grep -q "BatchMode=yes" "$resticServicePath" |
| 48 | grep -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. |
| 52 | preStart="$(sed -n 's/^ExecStartPre=//p' "$resticServicePath" | head -n1)" |
| 53 | staticPaths="$(grep -o '/nix/store/[^ ]*-staticPaths' "$preStart" | head -n1)" |
| 54 | grep -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. |
| 60 | grep -q '^environment="VALLEY_PRINCIPAL=integrator" ssh-ed25519 ' "$protectedKeysPath" |
| 61 | grep -q '^ssh-ed25519 .* valley-check$' "$protectedKeysPath" |
| 62 | |
| 63 | # The hook goes on the projects whose declaration has a |
| 64 | # protection block, and nowhere else. |
| 65 | grep -q "valley-protect-guarded" "$protectedInitPath" |
| 66 | grep -q "valley-protect-released" "$protectedInitPath" |
| 67 | if grep -q "valley-protect-open" "$protectedInitPath"; then |
| 68 | echo "module-eval: a project nobody protected was wired with the hook" >&2 |
| 69 | exit 1 |
| 70 | fi |
| 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. |
| 76 | guardedHook="$(grep -o '/nix/store/[^ ]*-valley-protect-guarded' "$protectedInitPath" | head -n1)" |
| 77 | releasedHook="$(grep -o '/nix/store/[^ ]*-valley-protect-released' "$protectedInitPath" | head -n1)" |
| 78 | grep -qF -- 'protected=( refs/heads/main )' "$guardedHook" |
| 79 | grep -qF -- "protected=( refs/heads/main 'refs/heads/release/*' )" "$releasedHook" |
| 80 | grep -qF -- 'writers=( integrator )' "$guardedHook" |
| 81 | touch "$out" |