| 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-sealed" "$protectedInitPath" |
| 66 | grep -q "valley-protect-guarded" "$protectedInitPath" |
| 67 | grep -q "valley-protect-released" "$protectedInitPath" |
| 68 | if grep -q "valley-protect-open" "$protectedInitPath"; then |
| 69 | echo "module-eval: a project nobody protected was wired with the hook" >&2 |
| 70 | exit 1 |
| 71 | fi |
| 72 | |
| 73 | # And what it enforces is what the declaration says, down |
| 74 | # to the file the hook reads: released names a pattern beside |
| 75 | # main and opens release tags, guarded takes the schema's |
| 76 | # default set. Follow the hook chain from the init script to |
| 77 | # each script, and from each script to its protection, which |
| 78 | # is the declaration's own block exported to JSON. |
| 79 | guardedHook="$(grep -o '/nix/store/[^ ]*-valley-protect-guarded' "$protectedInitPath" | head -n1)" |
| 80 | releasedHook="$(grep -o '/nix/store/[^ ]*-valley-protect-released' "$protectedInitPath" | head -n1)" |
| 81 | sealedHook="$(grep -o '/nix/store/[^ ]*-valley-protect-sealed' "$protectedInitPath" | head -n1)" |
| 82 | protection() { grep -o '/nix/store/[^ ]*-valley-protection-[^ ]*\.json' "$1" | head -n1; } |
| 83 | grep -qF -- '"refs":["refs/heads/main"]' "$(protection "$guardedHook")" |
| 84 | grep -qF -- '"writers":["integrator"]' "$(protection "$guardedHook")" |
| 85 | grep -qF -- '"refs":["refs/heads/main","refs/heads/release/*"]' "$(protection "$releasedHook")" |
| 86 | grep -qF -- '"allow":[{"refs":["refs/tags/release/*"],"writers":["integrator"]}]' "$(protection "$releasedHook")" |
| 87 | # The norm: a protected ref with no writer declared, and |
| 88 | # nothing opened. Both lists render empty rather than missing. |
| 89 | grep -qF -- '"writers":[]' "$(protection "$sealedHook")" |
| 90 | grep -qF -- '"allow":[]' "$(protection "$sealedHook")" |
| 91 | |
| 92 | # The rules are valleyhook's, and the script is only how git |
| 93 | # reaches it: it hands over the principal sshd tagged the |
| 94 | # push with, and nothing it decides itself. |
| 95 | grep -qF -- 'valleyhook pre-receive' "$guardedHook" |
| 96 | # shellcheck disable=SC2016 # the text pinned is the script's, not ours |
| 97 | grep -qF -- '--principal="${VALLEY_PRINCIPAL:-}"' "$guardedHook" |
| 98 | if grep -qE '^ *(case|for|while|if) ' "$guardedHook"; then |
| 99 | echo "module-eval: the rendered hook decides something itself — the policy belongs to valleyhook" >&2 |
| 100 | exit 1 |
| 101 | fi |
| 102 | |
| 103 | # The grants it reads. A grant declared by hand renders into |
| 104 | # a file in the format the compiler writes, and a host that |
| 105 | # compiles no registry reads that file alone. |
| 106 | grants="$(grep -o '/nix/store/[^ ]*-valley-grants' "$guardedHook" | head -n1)" |
| 107 | grep -qx 'request requester' "$grants" |
| 108 | if grep -q '/var/lib/valley-identity' "$guardedHook"; then |
| 109 | echo "module-eval: a host that compiles no registry reads compiled grants" >&2 |
| 110 | exit 1 |
| 111 | fi |
| 112 | # A host that compiles one reads the compiled grants beside |
| 113 | # the declared ones, which a compiled file can only add to. |
| 114 | identityHook="$(grep -o '/nix/store/[^ ]*-valley-protect-guarded' "$identityInitPath" | head -n1)" |
| 115 | grep -qF -- '--grants /var/lib/valley-identity/grants' "$identityHook" |
| 116 | grep -q -- '-valley-grants ' "$identityHook" |
| 117 | |
| 118 | # Replacement refs a push wrote before the hook refused them |
| 119 | # are reported on every activation, in every repository. |
| 120 | grep -qF -- 'for-each-ref' "$protectedInitPath" |
| 121 | grep -qF -- 'refs/replace/' "$protectedInitPath" |
| 122 | |
| 123 | # A controller is given the repository it serves and the |
| 124 | # identity it acts under, and nothing about policy: it reads |
| 125 | # the project layer from the target tip and the instance |
| 126 | # layer from its own side. |
| 127 | grep -q -- '--repo /srv/git/%i.git' "$integratorUnitPath" |
| 128 | grep -q -- '--key /run/agenix/valley-integrator-key' "$integratorUnitPath" |
| 129 | grep -q -- '--known-signers /var/lib/valley-instance/known_signers' "$integratorUnitPath" |
| 130 | # The floor's source, not a copy of it: the repository whose |
| 131 | # integrated tip carries it. It is named, and it is not the |
| 132 | # repository the controller serves — every controller here |
| 133 | # reads the floor out of "open", which no controller serves. |
| 134 | grep -q -- '--instance-repo /srv/git/open.git' "$integratorUnitPath" |
| 135 | if grep -q -- '--project-policy' "$integratorUnitPath"; then |
| 136 | echo "module-eval: the module must not tell a controller where the project's policy is" >&2 |
| 137 | exit 1 |
| 138 | fi |
| 139 | # Nor where the floor is inside that repository. Both directories are the |
| 140 | # integrator's defaults — policy/instance and policy/project — so a host |
| 141 | # declaration that says nothing about policy gets the convention. |
| 142 | if grep -q -- '--instance-policy' "$integratorUnitPath"; then |
| 143 | echo "module-eval: the module must not tell a controller where the floor is in the instance repository" >&2 |
| 144 | exit 1 |
| 145 | fi |
| 146 | |
| 147 | # It does not own the repositories, so git needs an ownership |
| 148 | # exception, and the rendered unit is where it has to appear: |
| 149 | # GIT_CONFIG_* is command scope, which is the only scope besides |
| 150 | # system and global that safe.directory is read from, and the |
| 151 | # environment reaches every git the controller drives. One |
| 152 | # repository, this instance's. |
| 153 | # A second exception for the instance repository, which carries the |
| 154 | # group's floor and is read by every controller — including the ones |
| 155 | # that serve some other project. |
| 156 | grep -qF -- 'Environment="GIT_CONFIG_COUNT=2"' "$integratorUnitPath" |
| 157 | grep -qF -- 'Environment="GIT_CONFIG_KEY_0=safe.directory"' "$integratorUnitPath" |
| 158 | grep -qF -- 'Environment="GIT_CONFIG_VALUE_0=/srv/git/%i.git"' "$integratorUnitPath" |
| 159 | grep -qF -- 'Environment="GIT_CONFIG_KEY_1=safe.directory"' "$integratorUnitPath" |
| 160 | grep -qF -- 'Environment="GIT_CONFIG_VALUE_1=/srv/git/open.git"' "$integratorUnitPath" |
| 161 | |
| 162 | # It writes refs as itself, so the repositories it serves are |
| 163 | # group-shared — and only those. The sharing block names each |
| 164 | # repo it touches; the repo-creation loop above it names none. |
| 165 | grep -qxF -- "repo=/srv/git/guarded.git" "$integratorInitPath" |
| 166 | grep -qxF -- "repo=/srv/git/released.git" "$integratorInitPath" |
| 167 | grep -qF -- 'config core.sharedRepository group' "$integratorInitPath" |
| 168 | # "open" is protected by nobody, so no controller serves it — and on |
| 169 | # this host it is the instance repository, which every controller reads |
| 170 | # the floor from. Read is the whole of that grant: no group write, and |
| 171 | # no core.sharedRepository, which is a statement about who writes. |
| 172 | open_block="$(awk '/^repo=/ { inside = ($0 == "repo=/srv/git/open.git") } inside' "$integratorInitPath")" |
| 173 | if [ -z "$open_block" ]; then |
| 174 | echo "module-eval: the instance repository must be readable by the git group even where no controller serves it" >&2 |
| 175 | exit 1 |
| 176 | fi |
| 177 | if ! printf '%s\n' "$open_block" | grep -qF -- 'chmod -R g+rX "$repo"'; then |
| 178 | echo "module-eval: the instance repository must be made group-readable" >&2 |
| 179 | exit 1 |
| 180 | fi |
| 181 | if printf '%s\n' "$open_block" | grep -qE 'g\+rwX|sharedRepository'; then |
| 182 | echo "module-eval: a project nobody protected was made group-writable — the floor is read, never written" >&2 |
| 183 | exit 1 |
| 184 | fi |
| 185 | |
| 186 | # git creates $GIT_DIR/worktrees on the first `worktree add`, owned by |
| 187 | # whoever got there first. init has to get there first, because the |
| 188 | # sweep below it runs as the git user and cannot chmod what a |
| 189 | # controller owns. Both the creation and its order are pinned: made |
| 190 | # after the repo-creation loop, before the recursive chmod that would |
| 191 | # otherwise be the first thing to meet it. |
| 192 | mkdir_line="$(grep -nF -- 'mkdir -p "$repo/worktrees"' "$integratorInitPath" | head -n1 | cut -d: -f1)" |
| 193 | chmod_line="$(grep -nF -- 'chmod -R g+rwX "$repo"' "$integratorInitPath" | head -n1 | cut -d: -f1)" |
| 194 | if [ -z "$mkdir_line" ]; then |
| 195 | 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 |
| 196 | exit 1 |
| 197 | fi |
| 198 | if [ -z "$chmod_line" ] || [ "$mkdir_line" -ge "$chmod_line" ]; then |
| 199 | echo "module-eval: the worktrees root must be created before the share sweep reaches it" >&2 |
| 200 | exit 1 |
| 201 | fi |
| 202 | grep -qF -- 'chmod g+rwxs "$repo/worktrees"' "$integratorInitPath" |
| 203 | |
| 204 | # A chmod the git user is not allowed to make must not fail valley-init: |
| 205 | # every valley unit requires it, so a failure there costs the host its |
| 206 | # git service rather than one repository's group share. |
| 207 | grep -qF -- 'share chmod -R g+rwX "$repo"' "$integratorInitPath" |
| 208 | if ! grep -qF -- 'echo "valley-init:' "$integratorInitPath"; then |
| 209 | echo "module-eval: a share the git user cannot apply must warn, not fail — valley-init failing takes every valley unit with it" >&2 |
| 210 | exit 1 |
| 211 | fi |
| 212 | touch "$out" |