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