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