| 1 | # shellcheck shell=bash |
| 2 | # Paths and fixtures come from the derivation environment. |
| 3 | # shellcheck disable=SC2154 |
| 4 | export HOME="$TMPDIR" |
| 5 | export GIT_CONFIG_NOSYSTEM=1 |
| 6 | git config --global user.name valley-check |
| 7 | git config --global user.email valley-check@localhost |
| 8 | git config --global init.defaultBranch main |
| 9 | cd "$TMPDIR" || exit 1 |
| 10 | |
| 11 | # A bare instance repository, holding one registry at its tip — what a host |
| 12 | # serves and what the compiler reads. |
| 13 | serve() { |
| 14 | local name="$1" source="$2" |
| 15 | rm -rf work "$name.git" |
| 16 | git init --quiet --bare "$name.git" |
| 17 | git init --quiet work |
| 18 | mkdir -p work/identity |
| 19 | cp "$source"/*.cue work/identity/ |
| 20 | chmod u+w work/identity/*.cue |
| 21 | git -C work add -A |
| 22 | git -C work commit --quiet -m "$name" |
| 23 | git -C work push --quiet "$TMPDIR/$name.git" main |
| 24 | } |
| 25 | |
| 26 | compile() { |
| 27 | identity compile --repo "$TMPDIR/instance.git" \ |
| 28 | --known-signers "$TMPDIR/signers" --authorized-keys "$TMPDIR/keys" \ |
| 29 | --grants "$TMPDIR/grants" --approval-signers "$TMPDIR/approval-signers" \ |
| 30 | --approval-signers-hardware "$TMPDIR/approval-signers-hardware" "$@" |
| 31 | } |
| 32 | |
| 33 | # The worked registry compiles to exactly the artifacts checked in beside |
| 34 | # it. This is the whole claim of the compiler, and the artifacts it replaces |
| 35 | # were written by hand — so the comparison is byte for byte. |
| 36 | serve instance "$registry" |
| 37 | compile --now 2026-08-09 2> first.log || exit 1 |
| 38 | diff -u "$compiled/known-signers" "$TMPDIR/signers" || exit 1 |
| 39 | diff -u "$compiled/authorized_keys" "$TMPDIR/keys" || exit 1 |
| 40 | diff -u "$compiled/grants" "$TMPDIR/grants" || exit 1 |
| 41 | # The approval files too: every key of the one human holding the approve |
| 42 | # grant, and no hardware key, because no key in this registry carries an |
| 43 | # attestation. |
| 44 | diff -u "$compiled/approval-signers" "$TMPDIR/approval-signers" || exit 1 |
| 45 | diff -u "$compiled/approval-signers-hardware" "$TMPDIR/approval-signers-hardware" || exit 1 |
| 46 | |
| 47 | # A replacement ref planted in the instance repository changes nothing the |
| 48 | # compiler reads. refs/replace/<blob> makes git read another object wherever |
| 49 | # it looks that blob up, so a reader that leaves replacement refs on would |
| 50 | # compile a registry nobody landed — here, one granting a key push and |
| 51 | # request — while main stays exactly where it was. |
| 52 | ssh-keygen -q -t ed25519 -N "" -C intruder -f "$TMPDIR/intruder" |
| 53 | cp "$registry/registry.cue" forged.cue |
| 54 | chmod u+w forged.cue |
| 55 | { |
| 56 | echo |
| 57 | echo 'principals: "intruder": {' |
| 58 | echo ' kind: "human"' |
| 59 | echo ' keys: [{' |
| 60 | echo ' class: "ssh-ed25519"' |
| 61 | echo ' bound: "hardware"' |
| 62 | printf '\t\tpublic: "%s"\n' "$(cut -d' ' -f1,2 < "$TMPDIR/intruder.pub")" |
| 63 | echo ' }]' |
| 64 | echo ' grants: {' |
| 65 | echo ' push: boundary: "valley-host-push"' |
| 66 | echo ' request: boundary: "valley-host-request"' |
| 67 | echo ' }' |
| 68 | echo '}' |
| 69 | } >> forged.cue |
| 70 | landed="$(git -C instance.git rev-parse main:identity/registry.cue)" |
| 71 | forged="$(git -C instance.git hash-object -w "$TMPDIR/forged.cue")" |
| 72 | git -C instance.git update-ref "refs/replace/$landed" "$forged" |
| 73 | if ! git -C instance.git show main:identity/registry.cue | grep intruder > /dev/null; then |
| 74 | echo "identity-e2e: the planted replacement did not take, so the check below proves nothing" >&2 |
| 75 | exit 1 |
| 76 | fi |
| 77 | compile --now 2026-08-09 || exit 1 |
| 78 | diff -u "$compiled/known-signers" "$TMPDIR/signers" || exit 1 |
| 79 | diff -u "$compiled/authorized_keys" "$TMPDIR/keys" || exit 1 |
| 80 | diff -u "$compiled/grants" "$TMPDIR/grants" || exit 1 |
| 81 | if grep -q intruder "$TMPDIR/keys" "$TMPDIR/grants"; then |
| 82 | echo "identity-e2e: a replacement ref put a principal nobody landed into the compiled artifacts" >&2 |
| 83 | exit 1 |
| 84 | fi |
| 85 | # The same holds when the caller's environment asks for replacement refs. |
| 86 | GIT_CONFIG_PARAMETERS="'core.usereplacerefs'='true'" compile --now 2026-08-09 || exit 1 |
| 87 | diff -u "$compiled/grants" "$TMPDIR/grants" || exit 1 |
| 88 | git -C instance.git update-ref -d "refs/replace/$landed" |
| 89 | |
| 90 | # A push landing while the compiler reads cannot mix two registries. The |
| 91 | # compiler resolves the ref to a commit once and reads every document from |
| 92 | # that commit. Here a git standing in front of the real one moves main to a |
| 93 | # commit holding the forged registry the moment the compiler has resolved |
| 94 | # it. A compiler reading through the ref would compile the forged registry |
| 95 | # and name the first commit in its journal line. The approval signers files |
| 96 | # come from the resolved commit too: the forged registry's intruder is a |
| 97 | # human holding request, so it would add an approval key. The compiler runs |
| 98 | # unwrapped, because its wrapper puts its own git first on PATH. |
| 99 | resolved="$(git -C instance.git rev-parse main)" |
| 100 | cp forged.cue work/identity/registry.cue |
| 101 | git -C work commit --quiet -am forged |
| 102 | moved="$(git -C work rev-parse HEAD)" |
| 103 | git -C work push --quiet "$TMPDIR/instance.git" "$moved:refs/heads/forged" |
| 104 | realgit="$(command -v git)" |
| 105 | mkdir -p racing |
| 106 | cat > racing/git <<RACING |
| 107 | #!/bin/sh |
| 108 | "$realgit" "\$@" || exit |
| 109 | case " \$* " in |
| 110 | *" show-ref --verify "*) "$realgit" -C "$TMPDIR/instance.git" update-ref refs/heads/main "$moved" ;; |
| 111 | esac |
| 112 | RACING |
| 113 | chmod +x racing/git |
| 114 | PATH="$TMPDIR/racing:$PATH" "$identityUnwrapped" compile --repo "$TMPDIR/instance.git" \ |
| 115 | --schema "$identitySchema" --known-signers "$TMPDIR/signers" \ |
| 116 | --authorized-keys "$TMPDIR/keys" --grants "$TMPDIR/grants" \ |
| 117 | --approval-signers "$TMPDIR/approval-signers" \ |
| 118 | --approval-signers-hardware "$TMPDIR/approval-signers-hardware" --now 2026-08-09 2> raced.log || { |
| 119 | cat raced.log >&2 |
| 120 | exit 1 |
| 121 | } |
| 122 | if [ "$(git -C instance.git rev-parse main)" != "$moved" ]; then |
| 123 | echo "identity-e2e: main did not move mid-read, so the check below proves nothing" >&2 |
| 124 | exit 1 |
| 125 | fi |
| 126 | grep -qF "at $resolved:" raced.log || { |
| 127 | echo "identity-e2e: the compilation did not name the commit it resolved" >&2 |
| 128 | cat raced.log >&2 |
| 129 | exit 1 |
| 130 | } |
| 131 | diff -u "$compiled/known-signers" "$TMPDIR/signers" || exit 1 |
| 132 | diff -u "$compiled/authorized_keys" "$TMPDIR/keys" || exit 1 |
| 133 | diff -u "$compiled/grants" "$TMPDIR/grants" || exit 1 |
| 134 | diff -u "$compiled/approval-signers" "$TMPDIR/approval-signers" || exit 1 |
| 135 | diff -u "$compiled/approval-signers-hardware" "$TMPDIR/approval-signers-hardware" || exit 1 |
| 136 | # The next compilation resolves main where it now is, and compiles the |
| 137 | # forged registry: the commit the race moved main to compiles differently. |
| 138 | compile --now 2026-08-09 || exit 1 |
| 139 | if ! grep -q intruder "$TMPDIR/keys" || cmp -s "$compiled/approval-signers" "$TMPDIR/approval-signers"; then |
| 140 | echo "identity-e2e: the commit main moved to compiles the same, so the race check proves nothing" >&2 |
| 141 | exit 1 |
| 142 | fi |
| 143 | git -C instance.git update-ref refs/heads/main "$resolved" |
| 144 | compile --now 2026-08-09 || exit 1 |
| 145 | diff -u "$compiled/authorized_keys" "$TMPDIR/keys" || exit 1 |
| 146 | |
| 147 | # The registry is read at its ref by that exact name. For a missing |
| 148 | # refs/heads/registry, git rev-parse would go on to read |
| 149 | # refs/heads/refs/heads/registry, a branch any pusher can make, here holding |
| 150 | # the forged registry. The compiler finds no refs/heads/registry, and the |
| 151 | # compilation fails, leaving the artifacts as they were. A ref that is not a |
| 152 | # full name is refused before anything is read. |
| 153 | git -C instance.git update-ref refs/heads/refs/heads/registry "$moved" |
| 154 | if compile --ref refs/heads/registry --now 2026-08-09 2> missing.log; then |
| 155 | echo "identity-e2e: the compiler read the missing refs/heads/registry through another ref" >&2 |
| 156 | exit 1 |
| 157 | fi |
| 158 | grep -qF "has no refs/heads/registry" missing.log || { |
| 159 | echo "identity-e2e: the compilation with refs/heads/registry missing did not say so" >&2 |
| 160 | cat missing.log >&2 |
| 161 | exit 1 |
| 162 | } |
| 163 | diff -u "$compiled/authorized_keys" "$TMPDIR/keys" || exit 1 |
| 164 | diff -u "$compiled/grants" "$TMPDIR/grants" || exit 1 |
| 165 | if compile --ref main --now 2026-08-09 2> short.log || ! grep -qF -- "--ref main is not a full ref name" short.log; then |
| 166 | echo "identity-e2e: the compiler took a ref that is not a full name" >&2 |
| 167 | cat short.log >&2 |
| 168 | exit 1 |
| 169 | fi |
| 170 | git -C instance.git update-ref -d refs/heads/refs/heads/registry |
| 171 | |
| 172 | # What the hook does with the compiled grants. A bare repository is wired |
| 173 | # with the pre-receive hook a host compiling this registry renders, |
| 174 | # followed from its init script; the one edit is the directory of the |
| 175 | # compiled files, which a host keeps under /var/lib and this sandbox cannot. |
| 176 | # Pushing as a principal is pushing with the tag on, the substitution for |
| 177 | # sshd the hook checks make everywhere. |
| 178 | rendered="$(grep -o "/nix/store/[^ ]*-valley-protect-guarded" "$identityInitScriptPath" | head -n1)" |
| 179 | if ! grep -qF -- "--grants /var/lib/valley-identity/grants" "$rendered"; then |
| 180 | echo "identity-e2e: the hook of a host compiling the registry does not read the compiled grants" >&2 |
| 181 | cat "$rendered" >&2 |
| 182 | exit 1 |
| 183 | fi |
| 184 | sed -e "s|/var/lib/valley-identity/grants|$TMPDIR/grants|" \ |
| 185 | -e "s|/var/lib/valley-identity/known-signers|$TMPDIR/signers|" "$rendered" > hook |
| 186 | grep -qF -- "--known-signers $TMPDIR/signers" hook || { |
| 187 | echo "identity-e2e: the hook of a host compiling the registry does not check attestations against the compiled keys" >&2 |
| 188 | exit 1 |
| 189 | } |
| 190 | chmod +x hook |
| 191 | git init --quiet --bare guarded.git |
| 192 | ln -s "$TMPDIR/hook" guarded.git/hooks/pre-receive |
| 193 | request=refs/the-valley/integration-requests/main/topic |
| 194 | if VALLEY_PRINCIPAL=agent-vm git -C work push --quiet "$TMPDIR/guarded.git" "main:$request" 2> refused.err; then |
| 195 | echo "identity-e2e: a principal the registry grants push and not request filed a request" >&2 |
| 196 | exit 1 |
| 197 | fi |
| 198 | grep -q 'approve grant (also accepted as request)' refused.err |
| 199 | VALLEY_PRINCIPAL=operator git -C work push --quiet "$TMPDIR/guarded.git" "main:$request" |
| 200 | git -C guarded.git rev-parse --verify --quiet "$request" > /dev/null || { |
| 201 | echo "identity-e2e: the principal the registry grants request could not file one" >&2 |
| 202 | exit 1 |
| 203 | } |
| 204 | VALLEY_PRINCIPAL=operator git -C work push --quiet "$TMPDIR/guarded.git" --delete "$request" |
| 205 | |
| 206 | # The approve grant goes by two names, approve and request, and a registry |
| 207 | # may use either. A registry whose hook boundary is of kind approve, and one |
| 208 | # granting operator at a boundary of each kind, compile exactly the artifacts |
| 209 | # the worked registry does, with operator written once. |
| 210 | mkdir -p approve-registry both-registry |
| 211 | sed 's/kind: "request"/kind: "approve"/' "$registry/registry.cue" > approve-registry/registry.cue |
| 212 | sed -e 's/^\t"valley-host-request": kind: "request"$/&\n\t"valley-host-approve": kind: "approve"/' \ |
| 213 | -e 's/^\t\t\trequest: boundary: "valley-host-request"$/&\n\t\t\tapprove: boundary: "valley-host-approve"/' \ |
| 214 | "$registry/registry.cue" > both-registry/registry.cue |
| 215 | if grep -q 'kind: "request"' approve-registry/registry.cue || |
| 216 | [ "$(grep -c 'valley-host-approve' both-registry/registry.cue)" != 2 ]; then |
| 217 | echo "identity-e2e: the registries naming the grant approve were not written, so the checks below prove nothing" >&2 |
| 218 | exit 1 |
| 219 | fi |
| 220 | # hook_as <principal> <git push arguments…>: a push through the rendered |
| 221 | # hook, as the principal. |
| 222 | hook_as() { |
| 223 | local who="$1" |
| 224 | shift |
| 225 | VALLEY_PRINCIPAL="$who" git -C work push --quiet "$TMPDIR/guarded.git" "$@" |
| 226 | } |
| 227 | for variant in approve-registry both-registry; do |
| 228 | serve instance "$variant" |
| 229 | compile --now 2026-08-09 || exit 1 |
| 230 | diff -u "$compiled/known-signers" "$TMPDIR/signers" || exit 1 |
| 231 | diff -u "$compiled/authorized_keys" "$TMPDIR/keys" || exit 1 |
| 232 | diff -u "$compiled/grants" "$TMPDIR/grants" || exit 1 |
| 233 | # And through the hook: operator files and deletes a request, and |
| 234 | # agent-vm, holding push alone, does neither. |
| 235 | if hook_as agent-vm "main:$request" 2> refused.err; then |
| 236 | echo "identity-e2e: under $variant, a principal holding push alone filed a request" >&2 |
| 237 | exit 1 |
| 238 | fi |
| 239 | grep -q 'approve grant (also accepted as request)' refused.err |
| 240 | hook_as operator "main:$request" |
| 241 | if hook_as agent-vm --delete "$request" 2> refused.err; then |
| 242 | echo "identity-e2e: under $variant, a principal holding push alone deleted a request" >&2 |
| 243 | exit 1 |
| 244 | fi |
| 245 | hook_as operator --delete "$request" |
| 246 | if git -C guarded.git rev-parse --verify --quiet "$request" > /dev/null; then |
| 247 | echo "identity-e2e: under $variant, the approve holder could not delete its request" >&2 |
| 248 | exit 1 |
| 249 | fi |
| 250 | done |
| 251 | |
| 252 | # The hook reads a grants file naming the grant approve exactly as one |
| 253 | # naming it request. approver is named under approve, requester under |
| 254 | # request, both under each, and agent-vm under neither. A principal |
| 255 | # named approve is in the file too, holding only push, a grant the hook |
| 256 | # does not check: a principal's name is not a grant. Each holder files a |
| 257 | # request, replaces it with a head that does not descend from it, and |
| 258 | # deletes it. agent-vm and approve can do none of that in between. |
| 259 | cat > "$TMPDIR/grants" << 'EOF' |
| 260 | approve approver |
| 261 | request requester |
| 262 | approve both |
| 263 | request both |
| 264 | push approve |
| 265 | EOF |
| 266 | for holder in approver requester both; do |
| 267 | hook_as "$holder" "main:$request" |
| 268 | git -C work commit --quiet --allow-empty -m "replaced by $holder" |
| 269 | replacement="$(git -C work rev-parse HEAD)" |
| 270 | git -C work reset --quiet --hard HEAD~1 |
| 271 | hook_as "$holder" --force "$replacement:$request" |
| 272 | [ "$(git -C guarded.git rev-parse "$request")" = "$replacement" ] || { |
| 273 | echo "identity-e2e: $holder could not replace its request" >&2 |
| 274 | exit 1 |
| 275 | } |
| 276 | for refused in "main:$request" "--delete $request"; do |
| 277 | for lacking in agent-vm approve; do |
| 278 | # shellcheck disable=SC2086 # the push arguments are two words, split on purpose |
| 279 | if hook_as "$lacking" --force $refused 2> refused.err; then |
| 280 | echo "identity-e2e: $lacking, holding neither name of the approve grant, wrote $holder's request" >&2 |
| 281 | exit 1 |
| 282 | fi |
| 283 | grep -q 'approve grant (also accepted as request)' refused.err |
| 284 | done |
| 285 | done |
| 286 | hook_as "$holder" --delete "$request" |
| 287 | if git -C guarded.git rev-parse --verify --quiet "$request" > /dev/null; then |
| 288 | echo "identity-e2e: $holder could not delete its request" >&2 |
| 289 | exit 1 |
| 290 | fi |
| 291 | done |
| 292 | |
| 293 | # Change records, through the same hook and the same grants file. operator |
| 294 | # owns the change and holds nothing in this file. A holder of the approve |
| 295 | # grant under either name, or both, may update another principal's change |
| 296 | # record, here by withdrawing the change, and may delete the record. |
| 297 | # agent-vm and approve may do neither, and each refusal leaves the |
| 298 | # record where it was. The record is created afresh for each holder. |
| 299 | owned_ref=refs/the-valley/changes/main/klmnopqrstuvwxyzklmnopqrstuvwxyz |
| 300 | # owned_record <state> <seq> <parent>: a record commit of operator's change, |
| 301 | # at one version whose head is owned_head. |
| 302 | owned_record() { |
| 303 | local blob tree |
| 304 | blob="$(printf '{"schema": "valley-change-record/v1", "change_id": "%s", "target": "main", "owner": "operator", "state": "%s", "seq": %s, "versions": [{"n": 1, "head": "git-sha1:%s", "title": "the change", "branch": "topic", "uploader": "operator"}], "active": 1}\n' \ |
| 305 | "${owned_ref##*/}" "$1" "$2" "$owned_head" | git -C work hash-object -w --stdin)" |
| 306 | tree="$(printf '100644 blob %s\trecord.json\n' "$blob" | git -C work mktree)" |
| 307 | git -C work commit-tree "$tree" -p "$3" -m "a change record" |
| 308 | } |
| 309 | # owned_at <commit> <what>: the record is at the commit, or gone for "". |
| 310 | owned_at() { |
| 311 | if [ "$(git -C guarded.git rev-parse --verify --quiet "$owned_ref")" != "$1" ]; then |
| 312 | echo "identity-e2e: $2" >&2 |
| 313 | exit 1 |
| 314 | fi |
| 315 | } |
| 316 | # owned_refused <principal> <commit>: the principal may neither move the |
| 317 | # record to the commit nor delete it, for holding neither name of the |
| 318 | # approve grant. |
| 319 | owned_refused() { |
| 320 | local who="$1" before |
| 321 | before="$(git -C guarded.git rev-parse "$owned_ref")" |
| 322 | if hook_as "$who" "$2:$owned_ref" 2> refused.err; then |
| 323 | echo "identity-e2e: $who, holding neither name of the approve grant, updated another principal's change record" >&2 |
| 324 | exit 1 |
| 325 | fi |
| 326 | grep -q "only the change's owner, operator, or a principal holding the approve grant (also accepted as request) may update its record" refused.err |
| 327 | if hook_as "$who" --delete "$owned_ref" 2> refused.err; then |
| 328 | echo "identity-e2e: $who, holding neither name of the approve grant, deleted another principal's change record" >&2 |
| 329 | exit 1 |
| 330 | fi |
| 331 | grep -q "only a principal holding the approve grant (also accepted as request) may delete a change record, and $who holds none" refused.err |
| 332 | owned_at "$before" "a refused write by $who moved the change record" |
| 333 | } |
| 334 | # owned_commits: operator's change, and the two records of it pushed below, |
| 335 | # written into work. filed creates the change, and retired withdraws it. |
| 336 | # serve replaces work, so they are written again after it. |
| 337 | owned_commits() { |
| 338 | owned_head="$(git -C work commit-tree "main^{tree}" -p main -m "operator's change")" |
| 339 | filed="$(owned_record ready 1 "$owned_head")" |
| 340 | retired="$(owned_record withdrawn 2 "$filed")" |
| 341 | } |
| 342 | owned_commits |
| 343 | for holder in approver requester both; do |
| 344 | hook_as operator "$filed:$owned_ref" |
| 345 | owned_at "$filed" "operator could not create a change record" |
| 346 | owned_refused agent-vm "$retired" |
| 347 | owned_refused approve "$retired" |
| 348 | hook_as "$holder" "$retired:$owned_ref" |
| 349 | owned_at "$retired" "$holder could not withdraw another principal's change" |
| 350 | hook_as "$holder" --delete "$owned_ref" |
| 351 | owned_at "" "$holder could not delete another principal's change record" |
| 352 | done |
| 353 | |
| 354 | # A grant's name is not its kind. A registry granting agent-vm a grant |
| 355 | # named approve at a boundary of another kind compiles exactly the grants |
| 356 | # the worked registry does, so the hook refuses agent-vm the approve |
| 357 | # grant's writes. |
| 358 | for boundary in valley-host-push registry; do |
| 359 | mkdir -p "mislabelled-$boundary" |
| 360 | sed "s/^\t\tgrants: push: boundary: \"valley-host-push\"$/\t\tgrants: {push: boundary: \"valley-host-push\", approve: boundary: \"$boundary\"}/" \ |
| 361 | "$registry/registry.cue" > "mislabelled-$boundary/registry.cue" |
| 362 | if ! grep -q "approve: boundary: \"$boundary\"" "mislabelled-$boundary/registry.cue"; then |
| 363 | echo "identity-e2e: the registry granting approve at $boundary was not written, so the check below proves nothing" >&2 |
| 364 | exit 1 |
| 365 | fi |
| 366 | serve instance "mislabelled-$boundary" |
| 367 | compile --now 2026-08-09 || exit 1 |
| 368 | diff -u "$compiled/grants" "$TMPDIR/grants" || exit 1 |
| 369 | owned_commits |
| 370 | if hook_as agent-vm "main:$request" 2> refused.err; then |
| 371 | echo "identity-e2e: a grant named approve at $boundary let its holder file a request" >&2 |
| 372 | exit 1 |
| 373 | fi |
| 374 | grep -q 'approve grant (also accepted as request)' refused.err |
| 375 | hook_as operator "$filed:$owned_ref" |
| 376 | owned_refused agent-vm "$retired" |
| 377 | hook_as operator --delete "$owned_ref" |
| 378 | owned_at "" "operator could not delete its own change record, holding the approve grant" |
| 379 | done |
| 380 | |
| 381 | # The worked registry again, for what follows. |
| 382 | serve instance "$registry" |
| 383 | compile --now 2026-08-09 || exit 1 |
| 384 | |
| 385 | # Level-triggered: compiling again converges on the same bytes and touches |
| 386 | # nothing. |
| 387 | compile --now 2026-08-09 2> second.log || exit 1 |
| 388 | if ! grep -q "unchanged" second.log; then |
| 389 | echo "identity-e2e: a second compilation of an unchanged registry rewrote its artifacts" >&2 |
| 390 | cat second.log >&2 |
| 391 | exit 1 |
| 392 | fi |
| 393 | |
| 394 | # The registry is read from the integrated tip and from nowhere else, which |
| 395 | # is the whole of the forcing: an edit on a branch governs nothing. |
| 396 | git -C work checkout --quiet -b topic |
| 397 | cp "$expired"/*.cue work/identity/registry.cue |
| 398 | git -C work commit --quiet -am topic |
| 399 | git -C work push --quiet "$TMPDIR/instance.git" topic |
| 400 | compile --now 2026-08-09 || exit 1 |
| 401 | diff -u "$compiled/known-signers" "$TMPDIR/signers" || exit 1 |
| 402 | |
| 403 | # The compiled verifier line is the one attest writes. The published keys |
| 404 | # above pin the format against a fixed vector; this pins it against the |
| 405 | # implementation that reads it, over a key generated here. |
| 406 | ssh-keygen -q -t ed25519 -N "" -C valley-check -f "$TMPDIR/scratch" |
| 407 | mkdir -p scratch-registry |
| 408 | { |
| 409 | echo 'package identity' |
| 410 | echo 'boundaries: "registry": kind: "registry"' |
| 411 | echo 'genesis: "someone"' |
| 412 | echo 'principals: "someone": {' |
| 413 | echo ' kind: "human"' |
| 414 | echo ' keys: [{' |
| 415 | echo ' class: "ssh-ed25519"' |
| 416 | echo ' bound: "hardware"' |
| 417 | printf '\t\tpublic: "%s"\n' "$(cut -d' ' -f1,2 < "$TMPDIR/scratch.pub")" |
| 418 | echo ' signs: "someone/attestations"' |
| 419 | echo ' }]' |
| 420 | echo ' grants: govern: boundary: "registry"' |
| 421 | echo '}' |
| 422 | } > scratch-registry/registry.cue |
| 423 | serve instance scratch-registry |
| 424 | compile --now 2026-08-09 || exit 1 |
| 425 | attest key --key "$TMPDIR/scratch" --name "someone/attestations" > expected-line |
| 426 | if ! grep -qxFf expected-line "$TMPDIR/signers"; then |
| 427 | echo "identity-e2e: the compiled verifier key is not the line attest writes" >&2 |
| 428 | cat expected-line "$TMPDIR/signers" >&2 |
| 429 | exit 1 |
| 430 | fi |
| 431 | |
| 432 | # An entry past its expiry leaves both artifacts, says so, and costs the |
| 433 | # rest of the registry nothing. The day before, it is still there — the |
| 434 | # declared day is the first day the entry no longer counts. |
| 435 | serve instance "$expired" |
| 436 | compile --now 2026-09-30 || exit 1 |
| 437 | grep -q "agent-vm/attestations" "$TMPDIR/signers" || { |
| 438 | echo "identity-e2e: an unexpired entry was omitted" >&2 |
| 439 | exit 1 |
| 440 | } |
| 441 | grep -qx "request agent-vm" "$TMPDIR/grants" || { |
| 442 | echo "identity-e2e: an unexpired holder of request was omitted from the grants" >&2 |
| 443 | exit 1 |
| 444 | } |
| 445 | compile --now 2026-11-01 2> expired.log || exit 1 |
| 446 | if grep -q "agent-vm" "$TMPDIR/signers" "$TMPDIR/keys" "$TMPDIR/grants"; then |
| 447 | echo "identity-e2e: an expired entry stayed in the compiled artifacts" >&2 |
| 448 | cat "$TMPDIR/signers" "$TMPDIR/keys" "$TMPDIR/grants" >&2 |
| 449 | exit 1 |
| 450 | fi |
| 451 | if ! grep -q "agent-vm expired 2026-10-01" expired.log; then |
| 452 | echo "identity-e2e: an expired entry was dropped without saying so" >&2 |
| 453 | cat expired.log >&2 |
| 454 | exit 1 |
| 455 | fi |
| 456 | grep -q "operator" "$TMPDIR/keys" || { |
| 457 | echo "identity-e2e: one expired entry took the rest of the registry with it" >&2 |
| 458 | exit 1 |
| 459 | } |
| 460 | |
| 461 | # The fail-safe. An invalid registry must cost the compilation and nothing |
| 462 | # else: the last good artifacts are what the gates keep reading, because a |
| 463 | # compiler bug that emptied them would lock the git user out of the host. |
| 464 | cp "$TMPDIR/signers" last-good-signers |
| 465 | cp "$TMPDIR/keys" last-good-keys |
| 466 | cp "$TMPDIR/grants" last-good-grants |
| 467 | cp "$TMPDIR/approval-signers" last-good-approval |
| 468 | cp "$TMPDIR/approval-signers-hardware" last-good-hardware |
| 469 | mkdir -p invalid-registry |
| 470 | cp "$invalid" invalid-registry/registry.cue |
| 471 | serve instance invalid-registry |
| 472 | if compile --now 2026-08-09 2> refused.log; then |
| 473 | echo "identity-e2e: an invalid registry compiled" >&2 |
| 474 | exit 1 |
| 475 | fi |
| 476 | if ! grep -q "externalGovernance" refused.log; then |
| 477 | echo "identity-e2e: the refusal did not name the field that failed" >&2 |
| 478 | cat refused.log >&2 |
| 479 | exit 1 |
| 480 | fi |
| 481 | diff -u last-good-signers "$TMPDIR/signers" || exit 1 |
| 482 | diff -u last-good-keys "$TMPDIR/keys" || exit 1 |
| 483 | diff -u last-good-grants "$TMPDIR/grants" || exit 1 |
| 484 | diff -u last-good-approval "$TMPDIR/approval-signers" || exit 1 |
| 485 | diff -u last-good-hardware "$TMPDIR/approval-signers-hardware" || exit 1 |
| 486 | |
| 487 | # The registry's fields are read spelled exactly as the schema spells them. |
| 488 | # encoding/json matches a key to a field without regard to case, and cue |
| 489 | # keeps keys that differ in case apart. A registry may reopen a schema |
| 490 | # definition and add a field to it, so it can hold a grant under a field |
| 491 | # spelled another way beside an empty grants, or a second public key beside |
| 492 | # the one a reviewer reads. A key that appears twice in one object is read |
| 493 | # the same way, and cue never exports one, so a stand-in for cue adds a |
| 494 | # second principals to the export. Each is refused, naming the key, and the |
| 495 | # last good artifacts stand. |
| 496 | ssh-keygen -q -t ed25519 -N "" -C decoy -f "$TMPDIR/decoy" |
| 497 | intruder_key="$(cut -d' ' -f1,2 < "$TMPDIR/intruder.pub")" |
| 498 | decoy_key="$(cut -d' ' -f1,2 < "$TMPDIR/decoy.pub")" |
| 499 | mkdir -p grants-spelled public-spelled |
| 500 | for spelled in grants-spelled public-spelled; do |
| 501 | cp "$registry/registry.cue" "$spelled/registry.cue" |
| 502 | chmod u+w "$spelled/registry.cue" |
| 503 | done |
| 504 | cat >> grants-spelled/registry.cue <<SPELLED |
| 505 | |
| 506 | #Principal: Grants?: _ |
| 507 | principals: "intruder": { |
| 508 | kind: "human" |
| 509 | keys: [{class: "ssh-ed25519", bound: "hardware", public: "$intruder_key"}] |
| 510 | grants: {} |
| 511 | Grants: push: boundary: "valley-host-push" |
| 512 | } |
| 513 | SPELLED |
| 514 | cat >> public-spelled/registry.cue <<SPELLED |
| 515 | |
| 516 | #Key: Public?: _ |
| 517 | principals: "intruder": { |
| 518 | kind: "human" |
| 519 | keys: [{class: "ssh-ed25519", bound: "hardware", public: "$decoy_key", Public: "$intruder_key"}] |
| 520 | grants: push: boundary: "valley-host-push" |
| 521 | } |
| 522 | SPELLED |
| 523 | realcue="$(command -v cue)" |
| 524 | mkdir -p doubling |
| 525 | cat > doubling/cue <<DOUBLING |
| 526 | #!/bin/sh |
| 527 | [ "\$1" = export ] || exec "$realcue" "\$@" |
| 528 | "$realcue" "\$@" | sed '\$d' |
| 529 | cat "$TMPDIR/doubled.json" |
| 530 | DOUBLING |
| 531 | chmod +x doubling/cue |
| 532 | printf ', "principals": {"intruder": {"kind": "human", "keys": [{"class": "ssh-ed25519", "bound": "hardware", "public": "%s"}], "grants": {"push": {"boundary": "valley-host-push"}}, "external": null}}}\n' \ |
| 533 | "$intruder_key" > doubled.json |
| 534 | if [ "$(PATH="$TMPDIR/doubling:$PATH" cue export "$identitySchema" "$registry"/*.cue | grep -c '"principals"')" != 2 ]; then |
| 535 | echo "identity-e2e: the stand-in for cue did not export principals twice, so the check below proves nothing" >&2 |
| 536 | exit 1 |
| 537 | fi |
| 538 | # misread <what> <says> <command...>: the command fails to compile the |
| 539 | # registry, says what it says of the key, and the last good artifacts stand. |
| 540 | misread() { |
| 541 | local what="$1" says="$2" |
| 542 | shift 2 |
| 543 | if "$@" 2> misread.err; then |
| 544 | echo "identity-e2e: $what compiled" >&2 |
| 545 | exit 1 |
| 546 | fi |
| 547 | if ! grep -qF "$says" misread.err; then |
| 548 | echo "identity-e2e: the refusal of $what did not say: $says" >&2 |
| 549 | cat misread.err >&2 |
| 550 | exit 1 |
| 551 | fi |
| 552 | diff -u last-good-signers "$TMPDIR/signers" || exit 1 |
| 553 | diff -u last-good-keys "$TMPDIR/keys" || exit 1 |
| 554 | diff -u last-good-grants "$TMPDIR/grants" || exit 1 |
| 555 | diff -u last-good-approval "$TMPDIR/approval-signers" || exit 1 |
| 556 | diff -u last-good-hardware "$TMPDIR/approval-signers-hardware" || exit 1 |
| 557 | } |
| 558 | serve instance grants-spelled |
| 559 | misread "a grant under Grants beside an empty grants" \ |
| 560 | 'principals["intruder"] holds the key "Grants", which is its field "grants" spelled another way' \ |
| 561 | compile --now 2026-08-09 |
| 562 | serve instance public-spelled |
| 563 | misread "a key's Public beside its public" \ |
| 564 | 'principals["intruder"].keys[0] holds the key "Public", which is its field "public" spelled another way' \ |
| 565 | compile --now 2026-08-09 |
| 566 | serve instance "$registry" |
| 567 | misread "an export holding principals twice" 'the document holds the key "principals" twice' \ |
| 568 | env PATH="$TMPDIR/doubling:$PATH" "$identityUnwrapped" compile --repo "$TMPDIR/instance.git" \ |
| 569 | --schema "$identitySchema" --known-signers "$TMPDIR/signers" \ |
| 570 | --authorized-keys "$TMPDIR/keys" --grants "$TMPDIR/grants" \ |
| 571 | --approval-signers "$TMPDIR/approval-signers" \ |
| 572 | --approval-signers-hardware "$TMPDIR/approval-signers-hardware" --now 2026-08-09 |
| 573 | |
| 574 | # Governance the clock orphans. This registry is valid as a document — the |
| 575 | # genesis entry governs — and the only entry governing carries an expiry, so |
| 576 | # whether it compiles depends on the day. The schema cannot see this; the |
| 577 | # compiler can, and refuses the whole render rather than write artifacts |
| 578 | # nobody could ever change again. |
| 579 | mkdir -p orphan-registry |
| 580 | { |
| 581 | echo 'package identity' |
| 582 | echo 'boundaries: {' |
| 583 | echo ' "push": kind: "git-push"' |
| 584 | echo ' "registry": kind: "registry"' |
| 585 | echo '}' |
| 586 | echo 'genesis: "founder"' |
| 587 | echo 'principals: {' |
| 588 | echo ' "founder": {' |
| 589 | echo ' kind: "human"' |
| 590 | echo ' keys: [{' |
| 591 | echo ' class: "ssh-ed25519"' |
| 592 | echo ' bound: "hardware"' |
| 593 | echo ' public: "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIMsWYyDSCNBwb6MnvQ7sfu++kZold+0HuP5leY4IiKnV"' |
| 594 | echo ' signs: "founder"' |
| 595 | echo ' }]' |
| 596 | echo ' grants: {' |
| 597 | echo ' push: boundary: "push"' |
| 598 | echo ' govern: boundary: "registry"' |
| 599 | echo ' }' |
| 600 | echo ' expires: "2026-10-01"' |
| 601 | echo ' }' |
| 602 | echo ' "runner": {' |
| 603 | echo ' kind: "machine"' |
| 604 | echo ' keys: [{' |
| 605 | echo ' class: "ssh-ed25519"' |
| 606 | echo ' bound: "host"' |
| 607 | echo ' public: "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIO0AEJMcSrXCOYKlyVrdVWG07RF2SIM/8gaI/8oDEMRI"' |
| 608 | echo ' }]' |
| 609 | echo ' grants: push: boundary: "push"' |
| 610 | echo ' expires: "2027-01-01"' |
| 611 | echo ' }' |
| 612 | echo '}' |
| 613 | } > orphan-registry/registry.cue |
| 614 | |
| 615 | # The day before: an ordinary compilation. |
| 616 | serve instance orphan-registry |
| 617 | compile --now 2026-09-30 || exit 1 |
| 618 | grep -q "founder" "$TMPDIR/signers" || { |
| 619 | echo "identity-e2e: the governing entry was omitted while still unexpired" >&2 |
| 620 | exit 1 |
| 621 | } |
| 622 | cp "$TMPDIR/signers" last-good-signers |
| 623 | cp "$TMPDIR/keys" last-good-keys |
| 624 | |
| 625 | # The day it expires, and a day well past it: the same document, refused. The |
| 626 | # entry still pushing does not save it — a grant at another boundary is not |
| 627 | # governance. |
| 628 | for pinned in 2026-10-01 2026-12-01; do |
| 629 | if compile --now "$pinned" 2> orphaned.log; then |
| 630 | echo "identity-e2e: a registry nobody governs on $pinned compiled" >&2 |
| 631 | exit 1 |
| 632 | fi |
| 633 | if ! grep -q "governanceOrphaned" orphaned.log; then |
| 634 | echo "identity-e2e: the refusal on $pinned did not name governanceOrphaned" >&2 |
| 635 | cat orphaned.log >&2 |
| 636 | exit 1 |
| 637 | fi |
| 638 | diff -u last-good-signers "$TMPDIR/signers" || exit 1 |
| 639 | diff -u last-good-keys "$TMPDIR/keys" || exit 1 |
| 640 | done |
| 641 | |
| 642 | # A hardware approval key, from a real YubiKey. The registry is written |
| 643 | # here from the test vectors in identity/testdata: the-valley operator's |
| 644 | # own approval key and the certificates its YubiKey wrote, which are the |
| 645 | # only chain at hand that verifies to Yubico Attestation Root 1. No |
| 646 | # example enrolls them. The registry enrolls that key under test-vector, |
| 647 | # beside the example laptop key. Both approve. Only the YubiKey's is a |
| 648 | # hardware approval key: its attestation verifies to the root the |
| 649 | # compiler carries, names the entry's public key, and says every |
| 650 | # signature needs a touch. The compiler reports what the attestation |
| 651 | # says, for whoever reviews the enrollment. |
| 652 | yubikey="$(cat "$testdata/valley-approve.pub")" |
| 653 | laptop="ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIDgoIskk25f2EYhLkPeJniiairVGv46CCYt2CUZ8EoxH" |
| 654 | # attested_registry <dir> <public> <slot pem> <f9 pem>: a registry whose |
| 655 | # one principal, test-vector, governs and approves with the attested key |
| 656 | # and the laptop key. |
| 657 | attested_registry() { |
| 658 | mkdir -p "$1" |
| 659 | { |
| 660 | echo 'package identity' |
| 661 | echo 'boundaries: {' |
| 662 | echo ' "push": kind: "git-push"' |
| 663 | echo ' "registry": kind: "registry"' |
| 664 | echo ' "approve": kind: "approve"' |
| 665 | echo '}' |
| 666 | echo 'genesis: "test-vector"' |
| 667 | echo 'principals: "test-vector": {' |
| 668 | echo ' kind: "human"' |
| 669 | echo ' keys: [{' |
| 670 | echo ' class: "ssh-ed25519"' |
| 671 | echo ' bound: "hardware"' |
| 672 | printf '\t\tpublic: "%s"\n' "$2" |
| 673 | echo ' attestation: {' |
| 674 | echo ' slot: """' |
| 675 | sed 's/^/\t\t\t\t/' "$3" |
| 676 | echo ' """' |
| 677 | echo ' f9: """' |
| 678 | sed 's/^/\t\t\t\t/' "$4" |
| 679 | echo ' """' |
| 680 | echo ' }' |
| 681 | echo ' }, {' |
| 682 | echo ' class: "ssh-ed25519"' |
| 683 | echo ' bound: "host"' |
| 684 | printf '\t\tpublic: "%s"\n' "$laptop" |
| 685 | echo ' }]' |
| 686 | echo ' grants: {push: boundary: "push", approve: boundary: "approve", govern: boundary: "registry"}' |
| 687 | echo '}' |
| 688 | } > "$1/registry.cue" |
| 689 | } |
| 690 | attested_registry device-registry "$yubikey" "$testdata/valley-approve.attest.pem" "$testdata/yubico-f9.pem" |
| 691 | serve instance device-registry |
| 692 | compile --now 2026-10-22 2> attested.log || exit 1 |
| 693 | diff -u "$testdata/compiled/approval-signers" "$TMPDIR/approval-signers" || exit 1 |
| 694 | diff -u "$testdata/compiled/approval-signers-hardware" "$TMPDIR/approval-signers-hardware" || exit 1 |
| 695 | grep -qxF "identity: test-vector: key 0 is on $(cat "$testdata/valley-approve.report"); it approves as a hardware key" attested.log || { |
| 696 | echo "identity-e2e: the compiler did not report the attestation it verified" >&2 |
| 697 | cat attested.log >&2 |
| 698 | exit 1 |
| 699 | } |
| 700 | cp "$TMPDIR/approval-signers" last-good-approval |
| 701 | cp "$TMPDIR/approval-signers-hardware" last-good-hardware |
| 702 | |
| 703 | # Evidence that does not verify fails the compilation, and the last good |
| 704 | # approval files stand: the key is never compiled as an ordinary key |
| 705 | # instead. The cases: |
| 706 | # - wrong-key: the attestation offered for the laptop's key; |
| 707 | # - altered-slot: the slot certificate with one character of its |
| 708 | # signature changed, the first of its last base64 line; |
| 709 | # - the example registry: its attestation chains to the test-only root |
| 710 | # "the-valley example attestation root, not Yubico", so an entry copied |
| 711 | # from it compiles only with the enrolling device's own certificates. |
| 712 | attested_registry wrong-key "$laptop" "$testdata/valley-approve.attest.pem" "$testdata/yubico-f9.pem" |
| 713 | last="$(($(grep -n -- '-----END' "$testdata/valley-approve.attest.pem" | cut -d: -f1) - 1))" |
| 714 | awk -v n="$last" 'NR == n { $0 = (substr($0, 1, 1) == "A" ? "B" : "A") substr($0, 2) } { print }' \ |
| 715 | "$testdata/valley-approve.attest.pem" > altered-slot.pem |
| 716 | if cmp -s altered-slot.pem "$testdata/valley-approve.attest.pem"; then |
| 717 | echo "identity-e2e: the altered slot certificate was not written, so the check below proves nothing" >&2 |
| 718 | exit 1 |
| 719 | fi |
| 720 | attested_registry altered-slot "$yubikey" altered-slot.pem "$testdata/yubico-f9.pem" |
| 721 | # The example's certificates are the ones the unit tests verify under the |
| 722 | # test-only root, so its refusal here is the root's alone. |
| 723 | for which in slot f9; do |
| 724 | sed -n "/^\t\t\t\t\t$which: \"\"\"\$/,/\"\"\"\$/p" "$attested/registry.cue" | sed '1d;$d' | tr -d '\t' > "example-$which.pem" |
| 725 | diff -u "$testdata/example-$which.pem" "example-$which.pem" || { |
| 726 | echo "identity-e2e: the example's $which certificate is not the one the unit tests verify" >&2 |
| 727 | exit 1 |
| 728 | } |
| 729 | done |
| 730 | # refused <registry dir> <key> <reason>: the registry fails to compile, |
| 731 | # naming the key and the reason, and the last good approval files stand. |
| 732 | refused() { |
| 733 | serve instance "$1" |
| 734 | if compile --now 2026-10-22 2> refused-attestation.err; then |
| 735 | echo "identity-e2e: a registry whose attestation does not verify ($1) compiled" >&2 |
| 736 | exit 1 |
| 737 | fi |
| 738 | if ! grep -qF "$2: " refused-attestation.err || ! grep -qF "$3" refused-attestation.err; then |
| 739 | echo "identity-e2e: the refusal of $1 did not name \"$2\" and say \"$3\"" >&2 |
| 740 | cat refused-attestation.err >&2 |
| 741 | exit 1 |
| 742 | fi |
| 743 | diff -u last-good-approval "$TMPDIR/approval-signers" || exit 1 |
| 744 | diff -u last-good-hardware "$TMPDIR/approval-signers-hardware" || exit 1 |
| 745 | } |
| 746 | refused wrong-key "test-vector: key 0" "attests a different key" |
| 747 | refused altered-slot "test-vector: key 0" "does not verify to Yubico Attestation Root 1" |
| 748 | refused "$attested" "example-operator: key 0" \ |
| 749 | "does not verify to Yubico Attestation Root 1: x509: certificate signed by unknown authority" |
| 750 | |
| 751 | # The approval files are written only where the caller names them, so a |
| 752 | # host that does not ask for them gets neither. |
| 753 | rm "$TMPDIR/approval-signers" "$TMPDIR/approval-signers-hardware" |
| 754 | serve instance device-registry |
| 755 | identity compile --repo "$TMPDIR/instance.git" --known-signers "$TMPDIR/signers" \ |
| 756 | --authorized-keys "$TMPDIR/keys" --grants "$TMPDIR/grants" --now 2026-10-22 || exit 1 |
| 757 | if [ -e "$TMPDIR/approval-signers" ] || [ -e "$TMPDIR/approval-signers-hardware" ]; then |
| 758 | echo "identity-e2e: a compilation that named no approval file wrote one" >&2 |
| 759 | exit 1 |
| 760 | fi |
| 761 | |
| 762 | # A key the host declares by hand under one tag, and the registry |
| 763 | # authorizes as another principal, is refused: sshd reads the declared |
| 764 | # entry first, so the registry's principal would never be the one pushing. |
| 765 | serve instance "$registry" |
| 766 | compile --now 2026-08-09 || exit 1 |
| 767 | cp "$TMPDIR/keys" last-good-keys |
| 768 | operator_key="$(grep -m1 -o 'ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIMsWYyDSCNBwb6MnvQ7sfu++kZold+0HuP5leY4IiKnV' "$registry/registry.cue")" |
| 769 | for declared in "$operator_key operator@laptop" "environment=\"VALLEY_PRINCIPAL=someone\" $operator_key"; do |
| 770 | printf '%s\n' "$declared" > declared-keys |
| 771 | if compile --now 2026-08-09 --declared-keys declared-keys 2> declared.err; then |
| 772 | echo "identity-e2e: a registry key declared by hand under another tag compiled" >&2 |
| 773 | exit 1 |
| 774 | fi |
| 775 | grep -q 'declared on this host' declared.err || { |
| 776 | echo "identity-e2e: the refusal did not name the declared entry" >&2 |
| 777 | cat declared.err >&2 |
| 778 | exit 1 |
| 779 | } |
| 780 | diff -u last-good-keys "$TMPDIR/keys" || exit 1 |
| 781 | done |
| 782 | printf 'environment="VALLEY_PRINCIPAL=operator" %s\n' "$operator_key" > declared-keys |
| 783 | compile --now 2026-08-09 --declared-keys declared-keys || exit 1 |
| 784 | |
| 785 | # Attestation names. Under a registry with two signers, the hook checks |
| 786 | # each pushed attestation's signature against the compiled keys, so a name |
| 787 | # in the create-only namespace can only be taken by the evidence it is |
| 788 | # named for. The pusher is never the question: evidence is relayed. |
| 789 | for who in victim attacker stranger; do |
| 790 | ssh-keygen -q -t ed25519 -N "" -C "$who" -f "$TMPDIR/$who" |
| 791 | done |
| 792 | mkdir -p signers-registry |
| 793 | { |
| 794 | echo 'package identity' |
| 795 | echo 'boundaries: {' |
| 796 | echo ' "push": kind: "git-push"' |
| 797 | echo ' "registry": kind: "registry"' |
| 798 | echo '}' |
| 799 | echo 'genesis: "victim"' |
| 800 | for who in victim attacker; do |
| 801 | printf 'principals: "%s": {\n' "$who" |
| 802 | echo ' kind: "human"' |
| 803 | echo ' keys: [{' |
| 804 | echo ' class: "ssh-ed25519"' |
| 805 | echo ' bound: "hardware"' |
| 806 | printf '\t\tpublic: "%s"\n' "$(cut -d' ' -f1,2 < "$TMPDIR/$who.pub")" |
| 807 | printf '\t\tsigns: "%s/attestations"\n' "$who" |
| 808 | echo ' }]' |
| 809 | if [ "$who" = victim ]; then |
| 810 | echo ' grants: {push: boundary: "push", govern: boundary: "registry"}' |
| 811 | else |
| 812 | echo ' grants: push: boundary: "push"' |
| 813 | fi |
| 814 | echo '}' |
| 815 | done |
| 816 | } > signers-registry/registry.cue |
| 817 | serve instance signers-registry |
| 818 | compile --now 2026-08-09 || exit 1 |
| 819 | |
| 820 | git init --quiet --bare attested.git |
| 821 | ln -s "$TMPDIR/hook" attested.git/hooks/pre-receive |
| 822 | git init --quiet occ |
| 823 | git -C occ commit --quiet --allow-empty -m "a tree to attest" |
| 824 | attestation() { |
| 825 | attest run --repo occ --key "$TMPDIR/$1" --name "$1/attestations" --command ok=true | |
| 826 | sed -n 's/^stored \(refs[^ ]*\) -> .*/\1/p' |
| 827 | } |
| 828 | mine="$(attestation attacker)" |
| 829 | theirs="$(attestation victim)" |
| 830 | unknown="$(attestation stranger)" |
| 831 | digest="$(echo "$mine" | cut -d/ -f4)" |
| 832 | victim_hash="$(echo "$theirs" | cut -d/ -f5)" |
| 833 | squat="refs/the-valley/attestations/$digest/$victim_hash" |
| 834 | push_as() { VALLEY_PRINCIPAL="$1" git -C occ push --quiet "$TMPDIR/attested.git" "$2"; } |
| 835 | must_refuse() { |
| 836 | if push_as attacker "$1" 2> occupied.err; then |
| 837 | echo "identity-e2e: $2 took an attestation name" >&2 |
| 838 | exit 1 |
| 839 | fi |
| 840 | grep -qF -- "$3" occupied.err || { |
| 841 | echo "identity-e2e: the refusal of $2 did not say \"$3\"" >&2 |
| 842 | cat occupied.err >&2 |
| 843 | exit 1 |
| 844 | } |
| 845 | } |
| 846 | # The attacker's own evidence, under its own name: accepted. |
| 847 | push_as attacker "$mine:$mine" |
| 848 | # The attacker's note, filed under the victim's key hash. |
| 849 | must_refuse "$mine:$squat" "the attacker's note under the victim's name" \ |
| 850 | "no signature under the key hash $victim_hash by a key this host accepts" |
| 851 | # A note whose signature line claims the victim's key and carries the |
| 852 | # attacker's signature. |
| 853 | git -C occ cat-file blob "$mine:ok/statement.note" > mine.note |
| 854 | sig="$(sed -n 's/^— attacker\/attestations //p' mine.note)" |
| 855 | forged_sig="$( { printf "$(echo "$victim_hash" | sed 's/../\\x&/g')"; printf '%s' "$sig" | base64 -d | tail -c 64; } | base64 -w0)" |
| 856 | sed "s|^— attacker/attestations .*|— victim/attestations $forged_sig|" mine.note > forged.note |
| 857 | blob="$(git -C occ hash-object -w "$TMPDIR/forged.note")" |
| 858 | sub="$(printf '100644 blob %s\tstatement.note\n' "$blob" | git -C occ mktree)" |
| 859 | top="$(printf '040000 tree %s\tok\n' "$sub" | git -C occ mktree)" |
| 860 | must_refuse "$top:$squat" "a forged signature" "does not check out" |
| 861 | # A key the registry does not hold, under its own name. |
| 862 | must_refuse "$unknown:$unknown" "an unknown signer's note" "does not open" |
| 863 | # The victim's genuine note, with a line appended that no verifier reads. |
| 864 | # Its signature still verifies, and the note is still one no verifier |
| 865 | # opens, so it may not take the victim's name. |
| 866 | git -C occ cat-file blob "$theirs:ok/statement.note" > theirs.note |
| 867 | { cat theirs.note; printf '— x\t AAAAAAA=\n'; } > poisoned.note |
| 868 | blob="$(git -C occ hash-object -w "$TMPDIR/poisoned.note")" |
| 869 | sub="$(printf '100644 blob %s\tstatement.note\n' "$blob" | git -C occ mktree)" |
| 870 | top="$(printf '040000 tree %s\tok\n' "$sub" | git -C occ mktree)" |
| 871 | must_refuse "$top:$theirs" "a poisoned relay of the victim's note" "does not open" |
| 872 | # And the victim's own evidence, relayed by the attacker, takes the name |
| 873 | # that none of the above could. |
| 874 | push_as attacker "$theirs:$theirs" |
| 875 | git -C attested.git rev-parse --verify --quiet "$theirs" > /dev/null || { |
| 876 | echo "identity-e2e: relayed evidence could not take its own name" >&2 |
| 877 | exit 1 |
| 878 | } |
| 879 | |
| 880 | # The restack grant. A principal holding a grant at a boundary of kind |
| 881 | # restack is compiled into the grants as "restack <principal>", and the |
| 882 | # hook a host renders then lets it add a version to another principal's |
| 883 | # ready change and write that change's restack report. The same write from |
| 884 | # a principal the registry grants push alone is refused. |
| 885 | ssh-keygen -q -t ed25519 -N "" -C restacker -f "$TMPDIR/restacker" |
| 886 | mkdir -p restack-registry |
| 887 | { |
| 888 | echo 'package identity' |
| 889 | echo 'boundaries: {' |
| 890 | echo ' "push": kind: "git-push"' |
| 891 | echo ' "registry": kind: "registry"' |
| 892 | echo ' "restack": kind: "restack"' |
| 893 | echo '}' |
| 894 | echo 'genesis: "author"' |
| 895 | echo 'principals: "author": {' |
| 896 | echo ' kind: "human"' |
| 897 | echo ' keys: [{' |
| 898 | echo ' class: "ssh-ed25519"' |
| 899 | echo ' bound: "hardware"' |
| 900 | printf '\t\tpublic: "%s"\n' "$(cut -d' ' -f1,2 < "$TMPDIR/victim.pub")" |
| 901 | echo ' }]' |
| 902 | echo ' grants: {push: boundary: "push", govern: boundary: "registry"}' |
| 903 | echo '}' |
| 904 | echo 'principals: "valley-restacker": {' |
| 905 | echo ' kind: "service"' |
| 906 | echo ' keys: [{' |
| 907 | echo ' class: "ssh-ed25519"' |
| 908 | echo ' bound: "host"' |
| 909 | printf '\t\tpublic: "%s"\n' "$(cut -d' ' -f1,2 < "$TMPDIR/restacker.pub")" |
| 910 | echo ' }]' |
| 911 | echo ' grants: {push: boundary: "push", restack: boundary: "restack"}' |
| 912 | echo ' expires: "2027-01-01"' |
| 913 | echo '}' |
| 914 | } > restack-registry/registry.cue |
| 915 | serve instance restack-registry |
| 916 | compile --now 2026-08-09 || exit 1 |
| 917 | if [ "$(grep -v '^#' "$TMPDIR/grants")" != "restack valley-restacker" ]; then |
| 918 | echo "identity-e2e: the restack grant did not compile into the one line it holds" >&2 |
| 919 | cat "$TMPDIR/grants" >&2 |
| 920 | exit 1 |
| 921 | fi |
| 922 | |
| 923 | git init --quiet --bare restacked.git |
| 924 | ln -s "$TMPDIR/hook" restacked.git/hooks/pre-receive |
| 925 | git init --quiet rs |
| 926 | git -C rs commit --quiet --allow-empty -m "the change" |
| 927 | h1="$(git -C rs rev-parse HEAD)" |
| 928 | h2="$(git -C rs commit-tree "HEAD^{tree}" -p HEAD -m "the change, restacked")" |
| 929 | change_id=nxvpwlkmqtsrzyoukklnpmqrstuvwxyz |
| 930 | version() { printf '{"n": %s, "head": "git-sha1:%s", "title": "the change", "branch": "topic", "uploader": "%s"}' "$@"; } |
| 931 | # one_file <file> <json> <parent…>: a commit whose tree holds the JSON as |
| 932 | # the file. |
| 933 | one_file() { |
| 934 | local file="$1" json="$2" blob tree parent |
| 935 | local -a parents=() |
| 936 | shift 2 |
| 937 | for parent in "$@"; do parents+=(-p "$parent"); done |
| 938 | blob="$(printf '%s' "$json" | git -C rs hash-object -w --stdin)" |
| 939 | tree="$(printf '100644 blob %s\t%s\n' "$blob" "$file" | git -C rs mktree)" |
| 940 | git -C rs commit-tree "$tree" "${parents[@]}" -m "$file" |
| 941 | } |
| 942 | rpush() { VALLEY_PRINCIPAL="$1" git -C rs push --quiet "$TMPDIR/restacked.git" "$2"; } |
| 943 | record_ref="refs/the-valley/changes/main/$change_id" |
| 944 | created="$(one_file record.json "{\"schema\": \"valley-change-record/v1\", \"change_id\": \"$change_id\", \"target\": \"main\", \"owner\": \"author\", \"state\": \"ready\", \"seq\": 1, \"versions\": [$(version 1 "$h1" author)], \"active\": 1}" "$h1")" |
| 945 | rpush author "$created:$record_ref" |
| 946 | appended="$(one_file record.json "{\"schema\": \"valley-change-record/v1\", \"change_id\": \"$change_id\", \"target\": \"main\", \"owner\": \"author\", \"state\": \"ready\", \"seq\": 2, \"versions\": [$(version 1 "$h1" author), $(version 2 "$h2" valley-restacker)], \"active\": 2}" "$created" "$h2")" |
| 947 | if rpush agent-vm "$appended:$record_ref" 2> restack.err; then |
| 948 | echo "identity-e2e: a principal the registry does not grant restack added a version to another's change" >&2 |
| 949 | exit 1 |
| 950 | fi |
| 951 | grep -q "only the change's owner, author" restack.err |
| 952 | rpush valley-restacker "$appended:$record_ref" |
| 953 | [ "$(git -C restacked.git rev-parse "$record_ref")" = "$appended" ] || { |
| 954 | echo "identity-e2e: the principal the registry grants restack could not add a version" >&2 |
| 955 | exit 1 |
| 956 | } |
| 957 | report_ref="refs/the-valley/restack/main/$change_id" |
| 958 | report="$(one_file report.json "{\"schema\": \"valley-restack-report/v1\", \"change_id\": \"$change_id\", \"target\": \"main\", \"target_tip\": \"git-sha1:$h1\", \"operation\": \"op-1\", \"result\": \"complete\", \"layers\": [{\"change_id\": \"$change_id\", \"record\": \"git-sha1:$created\", \"version\": 1, \"head\": \"git-sha1:$h1\", \"branch\": \"topic\", \"branch_tip\": \"git-sha1:$h1\", \"published\": {\"head\": \"git-sha1:$h2\", \"version\": 2, \"record\": \"git-sha1:$appended\"}}], \"failing_layer\": null, \"conflict_paths\": [], \"blocking_requests\": [], \"refusal\": null}")" |
| 959 | if rpush author "$report:$report_ref" 2> report.err; then |
| 960 | echo "identity-e2e: a principal the registry does not grant restack wrote a restack report" >&2 |
| 961 | exit 1 |
| 962 | fi |
| 963 | grep -q 'restack grant' report.err |
| 964 | rpush valley-restacker "$report:$report_ref" |
| 965 | [ "$(git -C restacked.git rev-parse "$report_ref")" = "$report" ] || { |
| 966 | echo "identity-e2e: the principal the registry grants restack could not write a restack report" >&2 |
| 967 | exit 1 |
| 968 | } |
| 969 | |
| 970 | touch "$out" |