| 1 | # shellcheck shell=bash |
| 2 | # Paths and fixtures come from the derivation environment. |
| 3 | # shellcheck disable=SC2154 |
| 4 | export HOME="$TMPDIR" |
| 5 | export XDG_STATE_HOME="$TMPDIR/state" |
| 6 | export GIT_CONFIG_NOSYSTEM=1 |
| 7 | # What this check writes down, kept out of the repository under test: an |
| 8 | # answer file or a captured stream sitting in the working tree would be |
| 9 | # committed by the next `git add -A` and change the tree being attested. |
| 10 | w="$TMPDIR/scratch" |
| 11 | mkdir -p "$w" |
| 12 | git config --global user.name valley-check |
| 13 | git config --global user.email valley-check@localhost |
| 14 | git config --global init.defaultBranch main |
| 15 | git config --global advice.detachedHead false |
| 16 | |
| 17 | # The operator's signing key. Provisioning one is deployment, so the verb |
| 18 | # takes it as a parameter and this check supplies a throwaway. |
| 19 | ssh-keygen -q -t ed25519 -N "" -C valley-check -f "$TMPDIR/host" |
| 20 | export VALLEY_ATTEST_KEY="$TMPDIR/host" |
| 21 | export VALLEY_ATTEST_NAME=laddie.valley.invalid/attestations |
| 22 | keyhash="$(attest key --key "$TMPDIR/host" --name "$VALLEY_ATTEST_NAME" | cut -d+ -f2)" |
| 23 | |
| 24 | origin="$TMPDIR/project.git" |
| 25 | git init --quiet --bare "$origin" |
| 26 | |
| 27 | # Every push the bare repo receives, as its hook sees it. The claim [a]sk |
| 28 | # makes is that ONE push carries the evidence and the request together, and |
| 29 | # that claim is only observable from the receiving side. |
| 30 | pushlog="$TMPDIR/pushlog" |
| 31 | cat > "$origin/hooks/pre-receive" <<EOF |
| 32 | #!/bin/sh |
| 33 | echo '--- push' >> $pushlog |
| 34 | cat >> $pushlog |
| 35 | EOF |
| 36 | chmod +x "$origin/hooks/pre-receive" |
| 37 | |
| 38 | # ---------------------------------------------------------------------- |
| 39 | # The project, with the two policy layers where the deriver looks without |
| 40 | # being told. Both checks use the command runner: the nix runner cannot be |
| 41 | # exercised from inside a nix build, since the sandbox has no daemon to |
| 42 | # call, and what is under test here is the sequencing rather than the |
| 43 | # runner. |
| 44 | repo="$TMPDIR/project" |
| 45 | git init --quiet "$repo" |
| 46 | cd "$repo" || exit 1 |
| 47 | git remote add origin "$origin" |
| 48 | |
| 49 | mkdir -p docs src schema examples/policy/instance examples/policy/project |
| 50 | cp "$schemaFile" schema/verification.cue |
| 51 | echo "the readme" > docs/readme.md |
| 52 | cat > examples/policy/instance/floor.cue <<'EOF' |
| 53 | package verification |
| 54 | |
| 55 | floor: { |
| 56 | checks: "tree-ok": { |
| 57 | runner: "command" |
| 58 | command: "test -f docs/readme.md" |
| 59 | } |
| 60 | classes: prose: { |
| 61 | paths: "docs/**": true |
| 62 | requires: "tree-ok": true |
| 63 | } |
| 64 | unclassified: "tree-ok": true |
| 65 | } |
| 66 | EOF |
| 67 | cat > examples/policy/project/policy.cue <<'EOF' |
| 68 | package verification |
| 69 | |
| 70 | project: { |
| 71 | checks: "no-secrets": { |
| 72 | runner: "command" |
| 73 | command: "! grep -rqF SECRET src" |
| 74 | } |
| 75 | classes: source: { |
| 76 | paths: "src/**": true |
| 77 | requires: "no-secrets": true |
| 78 | } |
| 79 | } |
| 80 | EOF |
| 81 | git add -A |
| 82 | git commit --quiet -m base |
| 83 | git push --quiet -u origin main |
| 84 | first="$(git rev-parse main)" |
| 85 | |
| 86 | # ---------------------------------------------------------------------- |
| 87 | # 1. A branch whose required check passes: the evidence and the request |
| 88 | # reach origin in one push, and main does not move. |
| 89 | git checkout --quiet -b topic/one |
| 90 | echo "a better readme" > docs/readme.md |
| 91 | git commit --quiet -am "improve the readme" |
| 92 | git push --quiet origin topic/one |
| 93 | digest="$(attest digest --rev topic/one | cut -d: -f2)" |
| 94 | |
| 95 | : > "$pushlog" |
| 96 | printf 'a\n' > "$w/answers" |
| 97 | valley review topic/one < "$w/answers" > "$w/ask.out" 2> "$w/ask.err" |
| 98 | |
| 99 | grep -q '^checking topic/one at .*: tree-ok$' "$w/ask.out" |
| 100 | grep -qE '^check tree-ok +command passed$' "$w/ask.out" |
| 101 | grep -qx 'requested topic/one -> refs/heads/main' "$w/ask.out" |
| 102 | grep -qx " request refs/the-valley/integration-requests/main/topic-one -> $(git rev-parse --short topic/one)" "$w/ask.out" |
| 103 | grep -qx " evidence refs/the-valley/attestations/$digest/$keyhash" "$w/ask.out" |
| 104 | grep -qx ' checks tree-ok' "$w/ask.out" |
| 105 | grep -q 'main is untouched' "$w/ask.out" |
| 106 | |
| 107 | # What origin holds now. The attestation is keyed by the digest of the |
| 108 | # BRANCH's tree, which is what says the checks ran over the right tree and |
| 109 | # not over the operator's working copy. |
| 110 | git -C "$origin" for-each-ref --format='%(refname)' refs/the-valley > "$w/published.txt" |
| 111 | grep -qx "refs/the-valley/integration-requests/main/topic-one" "$w/published.txt" |
| 112 | grep -qx "refs/the-valley/attestations/$digest/$keyhash" "$w/published.txt" |
| 113 | if [ "$(git -C "$origin" rev-parse refs/the-valley/integration-requests/main/topic-one)" \ |
| 114 | != "$(git rev-parse topic/one)" ]; then |
| 115 | echo "valley-request: the request ref does not name the branch's head" >&2 |
| 116 | exit 1 |
| 117 | fi |
| 118 | if [ "$(git -C "$origin" rev-parse refs/heads/main)" != "$first" ]; then |
| 119 | echo "valley-request: [a]sk moved main" >&2 |
| 120 | exit 1 |
| 121 | fi |
| 122 | |
| 123 | # One push carried both. This is the friction the verb exists to remove: |
| 124 | # `attest run --push` publishes the branch and the evidence and knows |
| 125 | # nothing about a request ref, so doing this by hand is two beats. |
| 126 | pushes="$(grep -c '^--- push' "$pushlog" || true)" |
| 127 | if [ "$pushes" != 1 ]; then |
| 128 | echo "valley-request: the evidence and the request took $pushes pushes, not 1" >&2 |
| 129 | cat "$pushlog" >&2 |
| 130 | exit 1 |
| 131 | fi |
| 132 | grep -q "refs/the-valley/attestations/$digest/$keyhash" "$pushlog" |
| 133 | grep -q 'refs/the-valley/integration-requests/main/topic-one' "$pushlog" |
| 134 | |
| 135 | # The note origin holds is the one the operator signed, over that tree. |
| 136 | git cat-file -p "refs/the-valley/attestations/$digest/$keyhash:tree-ok/statement.note" \ |
| 137 | > "$w/statement.note" |
| 138 | attest key --key "$TMPDIR/host" --name "$VALLEY_ATTEST_NAME" > "$w/known_keys" |
| 139 | attest verify --note "$w/statement.note" --repo "$repo" --rev topic/one \ |
| 140 | --known-keys "$w/known_keys" --signer "$VALLEY_ATTEST_NAME" > "$w/verify.out" |
| 141 | grep -q 'matches the recorded subject digest' "$w/verify.out" |
| 142 | |
| 143 | # ---------------------------------------------------------------------- |
| 144 | # 2. main moves on, so the next branch is stale. [a]sk is offered there |
| 145 | # too โ the integrator applies a delta to the tip rather than |
| 146 | # fast-forwarding โ and a branch whose check fails publishes nothing |
| 147 | # and leaves the review loop usable. |
| 148 | git checkout --quiet main |
| 149 | echo "a note" > docs/notes.md |
| 150 | git add -A |
| 151 | git commit --quiet -m "a landing that moves main" |
| 152 | git push --quiet origin main |
| 153 | moved="$(git rev-parse main)" |
| 154 | |
| 155 | git checkout --quiet -b topic/two "$first" |
| 156 | printf 'SECRET\n' > src/leak.txt |
| 157 | git add -A |
| 158 | git commit --quiet -m "a leak" |
| 159 | git push --quiet origin topic/two |
| 160 | |
| 161 | : > "$pushlog" |
| 162 | printf 'a\ns\n' > "$w/answers" |
| 163 | valley review topic/two < "$w/answers" > "$w/fail.out" 2> "$w/fail.err" |
| 164 | |
| 165 | grep -q 'does not fast-forward from main: \[a\]sk, \[b\]ase onto main, \[r\]eject, \[s\]kip?' "$w/fail.out" |
| 166 | grep -q '^checking topic/two at .*: no-secrets$' "$w/fail.out" |
| 167 | grep -qE '^check no-secrets +command failed$' "$w/fail.out" |
| 168 | grep -q 'the checks did not pass; nothing published, main untouched' "$w/fail.err" |
| 169 | # The prompt is printed without a newline, so what follows a refusal shares |
| 170 | # its line โ the answer to it is not anchored. |
| 171 | grep -q 'skipped topic/two; nothing done' "$w/fail.out" |
| 172 | if [ -s "$pushlog" ]; then |
| 173 | echo "valley-request: a failing check pushed something" >&2 |
| 174 | cat "$pushlog" >&2 |
| 175 | exit 1 |
| 176 | fi |
| 177 | git -C "$origin" for-each-ref --format='%(refname)' refs/the-valley > "$w/after.txt" |
| 178 | if grep -q 'topic-two' "$w/after.txt"; then |
| 179 | echo "valley-request: a failing check left a ref on origin" >&2 |
| 180 | cat "$w/after.txt" >&2 |
| 181 | exit 1 |
| 182 | fi |
| 183 | |
| 184 | # ---------------------------------------------------------------------- |
| 185 | # 3. No signing key: refused before anything is built, and the prompt |
| 186 | # still comes back. The host signs, and there is no unsigned mode. |
| 187 | : > "$pushlog" |
| 188 | printf 'a\ns\n' > "$w/answers" |
| 189 | env -u VALLEY_ATTEST_KEY valley review topic/two < "$w/answers" > "$w/nokey.out" 2> "$w/nokey.err" |
| 190 | grep -q 'no signing key: set VALLEY_ATTEST_KEY' "$w/nokey.err" |
| 191 | grep -q 'skipped topic/two; nothing done' "$w/nokey.out" |
| 192 | if [ -s "$pushlog" ]; then |
| 193 | echo "valley-request: a run with no signing key pushed something" >&2 |
| 194 | exit 1 |
| 195 | fi |
| 196 | |
| 197 | # ---------------------------------------------------------------------- |
| 198 | # 4. [i]ntegrate is untouched: both paths coexist until the writers change |
| 199 | # lands, and neither verb knows which one the operator may use. |
| 200 | git checkout --quiet main |
| 201 | git checkout --quiet -b topic/three |
| 202 | echo "another readme" > docs/readme.md |
| 203 | git commit --quiet -am "another readme" |
| 204 | git push --quiet origin topic/three |
| 205 | |
| 206 | printf 'i\n' > "$w/answers" |
| 207 | valley review topic/three < "$w/answers" > "$w/integrate.out" 2>&1 |
| 208 | grep -q '^topic/three: \[a\]sk, \[i\]ntegrate, \[r\]eject, \[s\]kip? ' "$w/integrate.out" |
| 209 | grep -q 'integrated topic/three: main is now' "$w/integrate.out" |
| 210 | if [ "$(git -C "$origin" rev-parse refs/heads/main)" = "$moved" ]; then |
| 211 | echo "valley-request: [i]ntegrate did not move main" >&2 |
| 212 | exit 1 |
| 213 | fi |
| 214 | |
| 215 | # The request filed in 1 is still pending: nothing here consumes it, which |
| 216 | # is the integrator's own step. |
| 217 | git -C "$origin" rev-parse --verify --quiet \ |
| 218 | refs/the-valley/integration-requests/main/topic-one > /dev/null |
| 219 | |
| 220 | # No throwaway worktree outlived any of it: only the checkout is left. |
| 221 | worktrees="$(git worktree list --porcelain | grep -c '^worktree ' || true)" |
| 222 | if [ "$worktrees" != 1 ]; then |
| 223 | echo "valley-request: a throwaway worktree was left behind" >&2 |
| 224 | git worktree list >&2 |
| 225 | exit 1 |
| 226 | fi |
| 227 | |
| 228 | echo "every scenario held" |
| 229 | { |
| 230 | cat "$w/ask.out" |
| 231 | printf '\n--- the scratch origin, after all of it ---\n' |
| 232 | git -C "$origin" for-each-ref --format='%(objectname) %(refname)' |
| 233 | } > "$out" |