| 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 | git config --global advice.detachedHead false |
| 10 | cd "$TMPDIR" || exit 1 |
| 11 | |
| 12 | # The keys this host accepts evidence from, as a controller would: the |
| 13 | # signer's, and nobody else's. |
| 14 | ssh-keygen -q -t ed25519 -N "" -C signer -f "$TMPDIR/signer" |
| 15 | ssh-keygen -q -t ed25519 -N "" -C other -f "$TMPDIR/other" |
| 16 | attest key --key "$TMPDIR/signer" --name signer.valley.invalid/attestations > "$TMPDIR/known_signers" |
| 17 | |
| 18 | # A bare repo wired as valley-init wires it, with the hook followed from |
| 19 | # the rendered init script. Every project the host serves gets the hook, |
| 20 | # protected or not. The one edit is where the verifier keys are, which a |
| 21 | # host keeps under /var/lib and this sandbox cannot; open's points at no |
| 22 | # file at all, as a host that names no keys would. |
| 23 | serve() { |
| 24 | local hook keys="$2" |
| 25 | hook="$(grep -o "/nix/store/[^ ]*-valley-protect-$1" "$initScriptPath" | head -n1)" |
| 26 | test -x "$hook" |
| 27 | grep -qF -- '--known-signers /var/lib/valley-instance/known_signers' "$hook" |
| 28 | git init --quiet --bare "$1.git" |
| 29 | sed "s|/var/lib/valley-instance/known_signers|$keys|" "$hook" > "$1.git/hooks/pre-receive" |
| 30 | chmod +x "$1.git/hooks/pre-receive" |
| 31 | } |
| 32 | serve sealed "$TMPDIR/known_signers" |
| 33 | serve guarded "$TMPDIR/known_signers" |
| 34 | serve released "$TMPDIR/known_signers" |
| 35 | serve open "$TMPDIR/no_such_keys" |
| 36 | |
| 37 | # Pushing as a principal is pushing with the tag on; pushing |
| 38 | # with an untagged key is pushing with it off. That sshd sets |
| 39 | # the tag from a key's entry, and nothing else does, is what |
| 40 | # ssh-e2e checks against a real sshd. |
| 41 | as() { |
| 42 | local who="$1" |
| 43 | shift |
| 44 | if [ "$who" = anonymous ]; then |
| 45 | env -u VALLEY_PRINCIPAL "$@" |
| 46 | else |
| 47 | VALLEY_PRINCIPAL="$who" "$@" |
| 48 | fi |
| 49 | } |
| 50 | refs() { git -C "$TMPDIR/$1.git" for-each-ref --format='%(refname)'; } |
| 51 | has_ref() { |
| 52 | if ! refs "$1" | grep -qx "$2"; then |
| 53 | echo "protect-e2e: $1 has no $2" >&2 |
| 54 | refs "$1" >&2 |
| 55 | exit 1 |
| 56 | fi |
| 57 | } |
| 58 | lacks_ref() { |
| 59 | if refs "$1" | grep -qx "$2"; then |
| 60 | echo "protect-e2e: $1 kept $2 through a rejected push" >&2 |
| 61 | exit 1 |
| 62 | fi |
| 63 | } |
| 64 | # refused <file> <what> <command…> — the push must fail. |
| 65 | refused() { |
| 66 | local err="$1" what="$2" |
| 67 | shift 2 |
| 68 | if "$@" 2> "$err"; then |
| 69 | echo "protect-e2e: $what" >&2 |
| 70 | exit 1 |
| 71 | fi |
| 72 | } |
| 73 | |
| 74 | git init --quiet work |
| 75 | cd work || exit 1 |
| 76 | git commit --quiet --allow-empty -m one |
| 77 | for name in sealed guarded released open; do |
| 78 | git remote add "$name" "$TMPDIR/$name.git" |
| 79 | done |
| 80 | |
| 81 | # ---------------------------------------------------------------------- |
| 82 | # Protected refs. |
| 83 | |
| 84 | # A non-writer cannot create a protected ref, and the |
| 85 | # rejection says who was pushing, what they were writing, |
| 86 | # and who may. |
| 87 | refused denied.err "a non-writer wrote a protected ref" \ |
| 88 | as contributor git push --quiet guarded main |
| 89 | grep -q 'contributor' denied.err |
| 90 | grep -q 'refs/heads/main' denied.err |
| 91 | grep -q 'writers: integrator' denied.err |
| 92 | lacks_ref guarded refs/heads/main |
| 93 | |
| 94 | # An untagged key is nobody, and nobody is not a writer. |
| 95 | refused untagged.err "an untagged key wrote a protected ref" \ |
| 96 | as anonymous git push --quiet guarded main |
| 97 | grep -q '<untagged key>' untagged.err |
| 98 | lacks_ref guarded refs/heads/main |
| 99 | |
| 100 | # Topic branches are open to the same non-writer, including a |
| 101 | # branch some other project's declaration protects. |
| 102 | git branch idea/one |
| 103 | git tag v1 |
| 104 | git branch release/1.0 |
| 105 | as contributor git push --quiet guarded idea/one release/1.0 |
| 106 | has_ref guarded refs/heads/idea/one |
| 107 | has_ref guarded refs/heads/release/1.0 |
| 108 | |
| 109 | # The same ref is protected where a declaration says so: the |
| 110 | # glob in released's set matches it. |
| 111 | refused glob.err "a glob in the protected set did not match" \ |
| 112 | as contributor git push --quiet released release/1.0 |
| 113 | grep -q 'refs/heads/release/1.0' glob.err |
| 114 | lacks_ref released refs/heads/release/1.0 |
| 115 | |
| 116 | # The writer creates it, and updates it. |
| 117 | as integrator git push --quiet guarded main |
| 118 | has_ref guarded refs/heads/main |
| 119 | git commit --quiet --allow-empty -m two |
| 120 | as integrator git push --quiet guarded main |
| 121 | second="$(git rev-parse HEAD)" |
| 122 | [ "$(git -C "$TMPDIR/guarded.git" rev-parse main)" = "$second" ] |
| 123 | |
| 124 | # A delete is a write. |
| 125 | refused deleted.err "a non-writer deleted a protected ref" \ |
| 126 | as contributor git push --quiet --delete guarded main |
| 127 | grep -q 'refs/heads/main' deleted.err |
| 128 | has_ref guarded refs/heads/main |
| 129 | |
| 130 | # pre-receive answers for the whole push: an open ref |
| 131 | # travelling with a protected one lands only if the |
| 132 | # protected one does, atomic push or not. |
| 133 | git commit --quiet --allow-empty -m three |
| 134 | git branch idea/two |
| 135 | for mode in --atomic --no-atomic; do |
| 136 | refused mixed.err "a protected ref rode in on a $mode push" \ |
| 137 | as contributor git push --quiet "$mode" guarded idea/two main |
| 138 | grep -q 'refs/heads/main' mixed.err |
| 139 | lacks_ref guarded refs/heads/idea/two |
| 140 | [ "$(git -C "$TMPDIR/guarded.git" rev-parse main)" = "$second" ] |
| 141 | done |
| 142 | |
| 143 | # The norm, on a project that declares no writer at all: the |
| 144 | # protected ref takes no push from anyone. Even the principal |
| 145 | # that writes guarded's main is refused here, and told how a |
| 146 | # change lands instead of being told who may. |
| 147 | refused sealed.err "a protected ref with no declared writer took a push" \ |
| 148 | as integrator git push --quiet sealed main |
| 149 | grep -q 'integrator' sealed.err |
| 150 | grep -q 'no writer is declared' sealed.err |
| 151 | grep -q 'integration request' sealed.err |
| 152 | lacks_ref sealed refs/heads/main |
| 153 | |
| 154 | # A project with no protection block protects nothing: its |
| 155 | # main is a branch like any other. Everything else the policy |
| 156 | # says still holds there, as the sections below show. |
| 157 | as contributor git push --quiet open main |
| 158 | has_ref open refs/heads/main |
| 159 | |
| 160 | # ---------------------------------------------------------------------- |
| 161 | # A symbolic ref takes no push. One cannot be pushed, but one |
| 162 | # can exist on the server, and git would write the ref it |
| 163 | # points at: here, a topic-branch name standing for the |
| 164 | # protected main. Its writer is refused too. |
| 165 | git -C "$TMPDIR/guarded.git" symbolic-ref refs/heads/alias refs/heads/main |
| 166 | for who in contributor integrator; do |
| 167 | refused symref.err "$who wrote through a symbolic ref" \ |
| 168 | as "$who" git push --quiet --force guarded HEAD:refs/heads/alias |
| 169 | grep -q 'symbolic ref' symref.err |
| 170 | [ "$(git -C "$TMPDIR/guarded.git" rev-parse main)" = "$second" ] |
| 171 | done |
| 172 | git -C "$TMPDIR/guarded.git" symbolic-ref --delete refs/heads/alias |
| 173 | |
| 174 | # git also reads a loose ref holding "ref:" followed by a tab, or by |
| 175 | # nothing, as a symbolic ref, and a filesystem symlink too. Each is refused |
| 176 | # the same way, to main's own writer, who could otherwise write main |
| 177 | # through it. |
| 178 | heads="$TMPDIR/guarded.git/refs/heads" |
| 179 | mkdir -p "$heads" |
| 180 | printf 'ref:\trefs/heads/main\n' > "$heads/tabbed" |
| 181 | printf 'ref:refs/heads/main\n' > "$heads/tight" |
| 182 | ln -s refs/heads/main "$heads/linked" |
| 183 | for alias in tabbed tight linked; do |
| 184 | refused symref.err "integrator wrote through the symbolic ref $alias" \ |
| 185 | as integrator git push --quiet --force guarded "HEAD:refs/heads/$alias" |
| 186 | grep -q 'symbolic ref' symref.err |
| 187 | [ "$(git -C "$TMPDIR/guarded.git" rev-parse main)" = "$second" ] |
| 188 | done |
| 189 | rm "$heads/tabbed" "$heads/tight" "$heads/linked" |
| 190 | |
| 191 | # ---------------------------------------------------------------------- |
| 192 | # Namespaces no push may write, on a protected project and an |
| 193 | # unprotected one, by the protected ref's own writer. |
| 194 | |
| 195 | # Tags open only through a named grant, and guarded has none. |
| 196 | refused tag.err "a tag was pushed that no grant opens" \ |
| 197 | as integrator git push --quiet guarded v1 |
| 198 | grep -q 'refs/tags/ opens only through a named grant' tag.err |
| 199 | grep -q 'no grant of guarded' tag.err |
| 200 | lacks_ref guarded refs/tags/v1 |
| 201 | |
| 202 | # Notes, replacement refs and the valley's own namespace, outcome and |
| 203 | # landing records among it, are closed outright, wherever they are pushed |
| 204 | # and by whom. |
| 205 | git notes add -m "a note" HEAD |
| 206 | git commit --quiet --allow-empty -m decoy |
| 207 | replaced="refs/replace/$(git rev-parse HEAD~1)" |
| 208 | git update-ref "$replaced" HEAD |
| 209 | outcome=refs/the-valley/integration-outcomes/main/idea |
| 210 | landing="refs/the-valley/integration-landings/main/$(git rev-parse HEAD)" |
| 211 | for project in guarded open; do |
| 212 | for who in contributor integrator; do |
| 213 | refused notes.err "$who pushed a notes ref to $project" \ |
| 214 | as "$who" git push --quiet "$project" refs/notes/commits |
| 215 | grep -q 'refs/notes/ is closed to every push' notes.err |
| 216 | refused replace.err "$who pushed a replacement ref to $project" \ |
| 217 | as "$who" git push --quiet "$project" "$replaced:$replaced" |
| 218 | grep -q 'refs/replace/ is closed to every push' replace.err |
| 219 | refused outcome.err "$who pushed an integration outcome to $project" \ |
| 220 | as "$who" git push --quiet "$project" "HEAD:$outcome" |
| 221 | grep -q 'refs/the-valley/integration-outcomes/ is written only by the machinery' outcome.err |
| 222 | refused landing.err "$who pushed a landing record to $project" \ |
| 223 | as "$who" git push --quiet "$project" "HEAD:$landing" |
| 224 | grep -q 'refs/the-valley/integration-landings/ is written only by the machinery' landing.err |
| 225 | done |
| 226 | lacks_ref "$project" refs/notes/commits |
| 227 | lacks_ref "$project" "$replaced" |
| 228 | lacks_ref "$project" "$outcome" |
| 229 | lacks_ref "$project" "$landing" |
| 230 | done |
| 231 | git update-ref -d "$replaced" |
| 232 | git reset --quiet --hard HEAD~1 |
| 233 | |
| 234 | # A grant opens what it names to whom it names, and nothing |
| 235 | # more. released grants release tags to the integrator |
| 236 | # principal: that principal pushes one, nobody else does, and |
| 237 | # a tag outside the pattern stays closed to it too. |
| 238 | git tag release/1.0 |
| 239 | tag=refs/tags/release/1.0 |
| 240 | refused opened.err "a grant opened a ref to a principal it does not name" \ |
| 241 | as contributor git push --quiet released "$tag:$tag" |
| 242 | grep -q 'the grant release-tags of released opens this ref to integrator only' opened.err |
| 243 | lacks_ref released "$tag" |
| 244 | as integrator git push --quiet released "$tag:$tag" |
| 245 | has_ref released "$tag" |
| 246 | refused unopened.err "a grant opened a ref outside its pattern" \ |
| 247 | as integrator git push --quiet released v1 |
| 248 | grep -q 'no grant of released' unopened.err |
| 249 | lacks_ref released refs/tags/v1 |
| 250 | |
| 251 | # refs/the-valley/flow/data opens only through a named grant, matched |
| 252 | # exactly. Without a grant, released refuses it with the named-grant |
| 253 | # wording rather than the machinery wording. |
| 254 | flow_ref=refs/the-valley/flow/data |
| 255 | refused flow-ungranted.err "refs/the-valley/flow/data opened without a grant" \ |
| 256 | as integrator git push --quiet released "HEAD:$flow_ref" |
| 257 | grep -q 'refs/the-valley/flow/ opens only through a named grant' flow-ungranted.err |
| 258 | grep -q 'no grant of released opens this ref' flow-ungranted.err |
| 259 | lacks_ref released "$flow_ref" |
| 260 | |
| 261 | # With a grant naming refs/the-valley/flow/data, the granted principal |
| 262 | # creates, force-updates and deletes it through the real hook, while an |
| 263 | # ungranted principal is refused with the named-grant wording and |
| 264 | # neighbouring refs stay refused as host machinery. |
| 265 | printf '{"refs":[],"writers":[],"grants":{"flow":{"refs":["%s"],"writers":["agent"]}}}\n' \ |
| 266 | "$flow_ref" > "$TMPDIR/flow-policy.json" |
| 267 | git init --quiet --bare "$TMPDIR/flow.git" |
| 268 | sed -e 's|--project open|--project flow|' \ |
| 269 | -e "s|--policy /nix/store/[^ ]*-valley-push-policy-open\.json|--policy $TMPDIR/flow-policy.json|" \ |
| 270 | "$TMPDIR/open.git/hooks/pre-receive" > "$TMPDIR/flow.git/hooks/pre-receive" |
| 271 | grep -qF -- "--policy $TMPDIR/flow-policy.json" "$TMPDIR/flow.git/hooks/pre-receive" |
| 272 | chmod +x "$TMPDIR/flow.git/hooks/pre-receive" |
| 273 | git remote add flow "$TMPDIR/flow.git" |
| 274 | |
| 275 | refused flow-other.err "an ungranted principal wrote refs/the-valley/flow/data" \ |
| 276 | as contributor git push --quiet flow "HEAD:$flow_ref" |
| 277 | grep -q 'refs/the-valley/flow/ opens only through a named grant' flow-other.err |
| 278 | grep -q 'the grant flow of flow opens this ref to agent only' flow-other.err |
| 279 | lacks_ref flow "$flow_ref" |
| 280 | |
| 281 | as agent git push --quiet flow "HEAD:$flow_ref" |
| 282 | has_ref flow "$flow_ref" |
| 283 | as agent git push --quiet --force flow "HEAD~1:$flow_ref" |
| 284 | [ "$(git -C "$TMPDIR/flow.git" rev-parse "$flow_ref")" = "$(git rev-parse HEAD~1)" ] |
| 285 | as agent git push --quiet --delete flow "$flow_ref" |
| 286 | lacks_ref flow "$flow_ref" |
| 287 | |
| 288 | for neighbour in refs/the-valley/flow/data/x refs/the-valley/flow/data2; do |
| 289 | refused flow-neighbour.err "a neighbour of refs/the-valley/flow/data ($neighbour) opened" \ |
| 290 | as agent git push --quiet flow "HEAD:$neighbour" |
| 291 | grep -q 'refs/the-valley/flow/ is written only by the machinery on the host' flow-neighbour.err |
| 292 | lacks_ref flow "$neighbour" |
| 293 | done |
| 294 | |
| 295 | # ---------------------------------------------------------------------- |
| 296 | # A ref named like another ref takes no create and no update. When a ref |
| 297 | # is missing, git reads, fetches and pushes to refs/, refs/tags/, |
| 298 | # refs/heads/ or refs/remotes/ followed by its name instead, so a branch, |
| 299 | # tag or other ref named that way could stand in for a ref the valley |
| 300 | # reads. Each shape is refused to an open project's contributor and to |
| 301 | # the writer of guarded's main, and no ref is made. |
| 302 | read_exact() { git -C "$TMPDIR/$1.git" show-ref --verify --hash "$2"; } |
| 303 | lookalikes=( |
| 304 | refs/heads/refs/the-valley/knots |
| 305 | refs/heads/refs/heads/main |
| 306 | refs/tags/refs/heads/main |
| 307 | refs/refs/the-valley/signers |
| 308 | refs/remotes/refs/heads/main |
| 309 | ) |
| 310 | before="$(git rev-parse HEAD)" |
| 311 | git commit --quiet --allow-empty -m lookalike |
| 312 | for pair in open:contributor guarded:integrator; do |
| 313 | project="${pair%%:*}" who="${pair#*:}" |
| 314 | for ref in "${lookalikes[@]}"; do |
| 315 | refused lookalike.err "$who created $ref on $project" \ |
| 316 | as "$who" git push --quiet "$project" "HEAD:$ref" |
| 317 | grep -qF "valley: $who may not create $ref — no push may create or update a ref named" lookalike.err |
| 318 | lacks_ref "$project" "$ref" |
| 319 | done |
| 320 | done |
| 321 | |
| 322 | # Names that hold refs anywhere else are ordinary: branches pushed by a |
| 323 | # contributor, and a release tag pushed under released's grant. |
| 324 | for branch in fix-refs a/refs/b a/refs/heads/main; do |
| 325 | as contributor git push --quiet open "HEAD:refs/heads/$branch" |
| 326 | has_ref open "refs/heads/$branch" |
| 327 | done |
| 328 | tag=refs/tags/release/refs/heads/main |
| 329 | git update-ref "$tag" HEAD |
| 330 | as integrator git push --quiet released "$tag:$tag" |
| 331 | has_ref released "$tag" |
| 332 | |
| 333 | # A host may hold such refs already, made before the hook refused them; |
| 334 | # they are written here on the host. A push updates none of them, and |
| 335 | # each is left where it was. |
| 336 | for ref in "${lookalikes[@]}"; do |
| 337 | git -C "$TMPDIR/open.git" update-ref "$ref" "$before" |
| 338 | done |
| 339 | for ref in "${lookalikes[@]}"; do |
| 340 | refused lookalike.err "contributor updated $ref" \ |
| 341 | as contributor git push --quiet --force open "HEAD:$ref" |
| 342 | grep -qF "valley: contributor may not update $ref — no push may create or update a ref named" lookalike.err |
| 343 | [ "$(read_exact open "$ref")" = "$before" ] |
| 344 | done |
| 345 | |
| 346 | # The one-off report docs/guide.md gives lists each of them, and none of |
| 347 | # the ordinary names. |
| 348 | git -C "$TMPDIR/open.git" for-each-ref --format='%(refname)' \ |
| 349 | refs/refs/ refs/heads/refs/ refs/tags/refs/ refs/remotes/refs/ > reported.txt |
| 350 | printf '%s\n' "${lookalikes[@]}" | sort > expected.txt |
| 351 | if ! sort reported.txt | cmp -s - expected.txt; then |
| 352 | echo "protect-e2e: the report of refs named like other refs is wrong" >&2 |
| 353 | cat reported.txt >&2 |
| 354 | exit 1 |
| 355 | fi |
| 356 | |
| 357 | # A delete is decided by the namespace's own rule: a contributor deletes |
| 358 | # the branches. No grant of open opens the others, and the operator |
| 359 | # deletes those on the host. |
| 360 | as contributor git push --quiet --delete open refs/heads/refs/the-valley/knots refs/heads/refs/heads/main |
| 361 | lacks_ref open refs/heads/refs/the-valley/knots |
| 362 | lacks_ref open refs/heads/refs/heads/main |
| 363 | refused lookalike.err "contributor deleted a tag no grant opens" \ |
| 364 | as contributor git push --quiet --delete open refs/tags/refs/heads/main |
| 365 | grep -q 'refs/tags/ opens only through a named grant' lookalike.err |
| 366 | for ref in refs/tags/refs/heads/main refs/refs/the-valley/signers refs/remotes/refs/heads/main; do |
| 367 | git -C "$TMPDIR/open.git" update-ref -d "$ref" |
| 368 | done |
| 369 | for branch in fix-refs a/refs/b a/refs/heads/main; do |
| 370 | as contributor git push --quiet --delete open "refs/heads/$branch" |
| 371 | done |
| 372 | git update-ref -d "$tag" |
| 373 | git reset --quiet --hard "$before" |
| 374 | |
| 375 | # ---------------------------------------------------------------------- |
| 376 | # Integration requests take writes only from a holder of the |
| 377 | # approve grant. Pushing topic branches does not make a |
| 378 | # principal one, and neither does being a declared writer. |
| 379 | request=refs/the-valley/integration-requests/main/idea |
| 380 | for who in contributor integrator anonymous; do |
| 381 | refused request.err "$who filed a request without the approve grant" \ |
| 382 | as "$who" git push --quiet guarded "idea/one:$request" |
| 383 | grep -q 'refs/the-valley/integration-requests/' request.err |
| 384 | grep -q 'approve grant (also accepted as request)' request.err |
| 385 | lacks_ref guarded "$request" |
| 386 | done |
| 387 | |
| 388 | # Each holder files one, replaces it with a head that does not |
| 389 | # descend from the first, and withdraws it: requester, declared |
| 390 | # under both names of the approve grant, and approver, declared |
| 391 | # under approve alone. In between, a principal without the grant |
| 392 | # can neither replace nor withdraw what the holder filed. |
| 393 | for holder in requester approver; do |
| 394 | git commit --quiet --allow-empty -m "first request" |
| 395 | as "$holder" git push --quiet guarded "HEAD:$request" |
| 396 | filed="$(git rev-parse HEAD)" |
| 397 | git reset --quiet --hard HEAD~1 |
| 398 | git commit --quiet --allow-empty -m "second request" |
| 399 | refused replaced.err "a principal without the grant replaced $holder's request" \ |
| 400 | as contributor git push --quiet --force guarded "HEAD:$request" |
| 401 | grep -q 'approve grant (also accepted as request)' replaced.err |
| 402 | refused withdrawn.err "a principal without the grant withdrew $holder's request" \ |
| 403 | as contributor git push --quiet --delete guarded "$request" |
| 404 | grep -q 'approve grant (also accepted as request)' withdrawn.err |
| 405 | [ "$(git -C "$TMPDIR/guarded.git" rev-parse "$request")" = "$filed" ] |
| 406 | as "$holder" git push --quiet --force guarded "HEAD:$request" |
| 407 | [ "$(git -C "$TMPDIR/guarded.git" rev-parse "$request")" = "$(git rev-parse HEAD)" ] |
| 408 | as "$holder" git push --quiet --delete guarded "$request" |
| 409 | lacks_ref guarded "$request" |
| 410 | git reset --quiet --hard HEAD~1 |
| 411 | done |
| 412 | |
| 413 | # Where protection covers the request namespace too, each rule |
| 414 | # still applies and neither stands in for the other: released's |
| 415 | # writer lacks the grant, and the holder of the grant is not |
| 416 | # released's writer. |
| 417 | refused overlap-writer.err "a writer filed a request without the grant" \ |
| 418 | as integrator git push --quiet released "idea/one:$request" |
| 419 | grep -q 'approve grant (also accepted as request)' overlap-writer.err |
| 420 | refused overlap-holder.err "an approve holder wrote a protected request ref" \ |
| 421 | as requester git push --quiet released "idea/one:$request" |
| 422 | grep -q 'a protected ref of released' overlap-holder.err |
| 423 | lacks_ref released "$request" |
| 424 | |
| 425 | # The grant is the same on a project that declares no |
| 426 | # protection at all. |
| 427 | refused open-request.err "a request was filed on an unprotected project without the grant" \ |
| 428 | as contributor git push --quiet open "idea/one:$request" |
| 429 | as requester git push --quiet open "idea/one:$request" |
| 430 | has_ref open "$request" |
| 431 | |
| 432 | # ---------------------------------------------------------------------- |
| 433 | # A change record moves only by the rows of the hook's table |
| 434 | # (valleyhook/record.go), here the rows that change its state. Each is |
| 435 | # pushed as the change's owner or the holder of the approve grant, and |
| 436 | # each refusal leaves the record where it was. A principal that is |
| 437 | # neither may not move it at all. |
| 438 | change_id=nxvpwlkmqtsrzyoukklnpmqrstuvwxyz |
| 439 | record_ref="refs/the-valley/changes/main/$change_id" |
| 440 | # record_json <state> <seq> <active> <head…>: the record of the change, |
| 441 | # owned by contributor, with one version per head. |
| 442 | record_json() { |
| 443 | local state="$1" seq="$2" active="$3" head n=0 versions="" |
| 444 | shift 3 |
| 445 | for head in "$@"; do |
| 446 | n=$((n + 1)) |
| 447 | [ -z "$versions" ] || versions="$versions, " |
| 448 | versions="$versions{\"n\": $n, \"head\": \"git-sha1:$head\", \"title\": \"v$n\", \"branch\": \"topic\", \"uploader\": \"contributor\"}" |
| 449 | done |
| 450 | printf '{"schema": "valley-change-record/v1", "change_id": "%s", "target": "main", "owner": "contributor", "state": "%s", "seq": %s, "versions": [%s], "active": %s}\n' \ |
| 451 | "$change_id" "$state" "$seq" "$versions" "$active" |
| 452 | } |
| 453 | # record_commit <json> <parent…>: a record commit holding the JSON. |
| 454 | record_commit() { |
| 455 | local json="$1" blob tree parent |
| 456 | local -a parents=() |
| 457 | shift |
| 458 | for parent in "$@"; do parents+=(-p "$parent"); done |
| 459 | blob="$(printf '%s' "$json" | git hash-object -w --stdin)" |
| 460 | tree="$(printf '100644 blob %s\trecord.json\n' "$blob" | git mktree)" |
| 461 | git commit-tree "$tree" "${parents[@]}" -m 'a change record' |
| 462 | } |
| 463 | record_at() { |
| 464 | if [ "$(git -C "$TMPDIR/open.git" rev-parse "$record_ref")" != "$1" ]; then |
| 465 | echo "protect-e2e: the change record is not where $2 left it" >&2 |
| 466 | exit 1 |
| 467 | fi |
| 468 | } |
| 469 | h1="$(git commit-tree "HEAD^{tree}" -p HEAD -m 'the change, v1')" |
| 470 | h2="$(git commit-tree "HEAD^{tree}" -p HEAD -m 'the change, v2')" |
| 471 | |
| 472 | created="$(record_commit "$(record_json ready 1 1 "$h1")" "$h1")" |
| 473 | as contributor git push --quiet open "$created:$record_ref" |
| 474 | record_at "$created" "the create" |
| 475 | |
| 476 | # unready: ready to draft. A draft is not made a draft again. |
| 477 | drafted="$(record_commit "$(record_json draft 2 1 "$h1")" "$created")" |
| 478 | refused unready.err "a principal neither owner nor holder unreadied a change" \ |
| 479 | as integrator git push --quiet open "$drafted:$record_ref" |
| 480 | grep -q "only the change's owner, contributor" unready.err |
| 481 | as contributor git push --quiet open "$drafted:$record_ref" |
| 482 | record_at "$drafted" "unready" |
| 483 | again="$(record_commit "$(record_json draft 3 1 "$h1")" "$drafted")" |
| 484 | refused redraft.err "a draft was made a draft again" \ |
| 485 | as contributor git push --quiet open "$again:$record_ref" |
| 486 | grep -q 'the change is draft already' redraft.err |
| 487 | record_at "$drafted" "unready" |
| 488 | |
| 489 | # re-ready at the same version, by the operator, and unready once more. |
| 490 | # It changes the state and the seq by one, and is the owner's or the |
| 491 | # operator's to make. |
| 492 | resumed="$(record_commit "$(record_json ready 3 1 "$h1")" "$drafted")" |
| 493 | refused reready.err "a principal neither owner nor holder readied a draft" \ |
| 494 | as integrator git push --quiet open "$resumed:$record_ref" |
| 495 | grep -q "only the change's owner, contributor" reready.err |
| 496 | stuck="$(record_commit "$(record_json ready 2 1 "$h1")" "$drafted")" |
| 497 | refused reready-seq.err "a draft was readied with its seq left as it was" \ |
| 498 | as contributor git push --quiet open "$stuck:$record_ref" |
| 499 | grep -q 'its seq is 2' reready-seq.err |
| 500 | record_at "$drafted" "unready" |
| 501 | as requester git push --quiet open "$resumed:$record_ref" |
| 502 | record_at "$resumed" "a re-ready at the same version" |
| 503 | paused="$(record_commit "$(record_json draft 4 1 "$h1")" "$resumed")" |
| 504 | as requester git push --quiet open "$paused:$record_ref" |
| 505 | record_at "$paused" "the operator's unready" |
| 506 | |
| 507 | # re-ready at a new version, which needs a new head. |
| 508 | same="$(record_commit "$(record_json ready 5 2 "$h1" "$h1")" "$paused" "$h1")" |
| 509 | refused samehead.err "a draft was readied at a new version at its active head" \ |
| 510 | as contributor git push --quiet open "$same:$record_ref" |
| 511 | grep -q 'a new version needs a new head' samehead.err |
| 512 | record_at "$paused" "the operator's unready" |
| 513 | versioned="$(record_commit "$(record_json ready 5 2 "$h1" "$h2")" "$paused" "$h2")" |
| 514 | as contributor git push --quiet open "$versioned:$record_ref" |
| 515 | record_at "$versioned" "a re-ready at a new version" |
| 516 | |
| 517 | # withdraw, from a draft. A withdrawn change never comes back as a draft, |
| 518 | # by its owner or by the operator. |
| 519 | redrafted="$(record_commit "$(record_json draft 6 2 "$h1" "$h2")" "$versioned")" |
| 520 | as contributor git push --quiet open "$redrafted:$record_ref" |
| 521 | gone="$(record_commit "$(record_json withdrawn 7 2 "$h1" "$h2")" "$redrafted")" |
| 522 | refused withdraw-draft.err "a principal neither owner nor holder withdrew a draft" \ |
| 523 | as integrator git push --quiet open "$gone:$record_ref" |
| 524 | grep -q "only the change's owner, contributor" withdraw-draft.err |
| 525 | record_at "$redrafted" "an unready of the new version" |
| 526 | as contributor git push --quiet open "$gone:$record_ref" |
| 527 | record_at "$gone" "a withdrawal from a draft" |
| 528 | back="$(record_commit "$(record_json draft 8 2 "$h1" "$h2")" "$gone")" |
| 529 | for who in contributor requester; do |
| 530 | refused withdrawn-draft.err "$who made a withdrawn change a draft" \ |
| 531 | as "$who" git push --quiet open "$back:$record_ref" |
| 532 | grep -q 'never as a draft' withdrawn-draft.err |
| 533 | record_at "$gone" "a withdrawal from a draft" |
| 534 | done |
| 535 | |
| 536 | # re-ready a withdrawn change at the version it was withdrawn at, which |
| 537 | # keeps the active version. |
| 538 | earlier="$(record_commit "$(record_json ready 8 1 "$h1" "$h2")" "$gone")" |
| 539 | refused revive-active.err "a withdrawn change was readied at another version" \ |
| 540 | as contributor git push --quiet open "$earlier:$record_ref" |
| 541 | grep -q 'changes active from 2 to 1' revive-active.err |
| 542 | record_at "$gone" "a withdrawal from a draft" |
| 543 | revived="$(record_commit "$(record_json ready 8 2 "$h1" "$h2")" "$gone")" |
| 544 | as contributor git push --quiet open "$revived:$record_ref" |
| 545 | record_at "$revived" "a re-ready from withdrawn at the same version" |
| 546 | |
| 547 | # approver, declared under the approve grant's new name alone, moves |
| 548 | # another's record as requester does: it withdraws the change, and deletes |
| 549 | # the record, which the change's owner may not. |
| 550 | refused owner-delete.err "the owner deleted a change record" \ |
| 551 | as contributor git push --quiet --delete open "$record_ref" |
| 552 | grep -q 'only a principal holding the approve grant (also accepted as request) may delete' owner-delete.err |
| 553 | record_at "$revived" "a refused delete" |
| 554 | retired="$(record_commit "$(record_json withdrawn 9 2 "$h1" "$h2")" "$revived")" |
| 555 | as approver git push --quiet open "$retired:$record_ref" |
| 556 | record_at "$retired" "a withdrawal under the approve grant" |
| 557 | as approver git push --quiet --delete open "$record_ref" |
| 558 | lacks_ref open "$record_ref" |
| 559 | |
| 560 | # ---------------------------------------------------------------------- |
| 561 | # The restack grant: the host restacker's one record write. restacker holds |
| 562 | # it alone, and requester holds it beside the approve grant. Neither owns |
| 563 | # the change, which contributor does. A holder may add exactly one version |
| 564 | # to the change while it is ready, on the active version's branch and under |
| 565 | # its title, naming itself as the uploader. Every other write the grant |
| 566 | # would have to authorise is refused, and leaves the record where it was. |
| 567 | restack_id=pqrstuvwxyzklmnopqrstuvwxyzklmno |
| 568 | restack_ref="refs/the-valley/changes/main/$restack_id" |
| 569 | # restack_json <state> <seq> <active> <version…>: the record of the change, |
| 570 | # owned by contributor. Each version is "<head>|<uploader>|<title>|<branch>". |
| 571 | restack_json() { |
| 572 | local state="$1" seq="$2" active="$3" spec head who title branch n=0 versions="" |
| 573 | shift 3 |
| 574 | for spec in "$@"; do |
| 575 | IFS='|' read -r head who title branch <<< "$spec" |
| 576 | n=$((n + 1)) |
| 577 | [ -z "$versions" ] || versions="$versions, " |
| 578 | versions="$versions{\"n\": $n, \"head\": \"git-sha1:$head\", \"title\": \"$title\", \"branch\": \"$branch\", \"uploader\": \"$who\"}" |
| 579 | done |
| 580 | printf '{"schema": "valley-change-record/v1", "change_id": "%s", "target": "main", "owner": "contributor", "state": "%s", "seq": %s, "versions": [%s], "active": %s}\n' \ |
| 581 | "$restack_id" "$state" "$seq" "$versions" "$active" |
| 582 | } |
| 583 | restack_at() { |
| 584 | if [ "$(git -C "$TMPDIR/open.git" rev-parse "$restack_ref")" != "$1" ]; then |
| 585 | echo "protect-e2e: the restacked change's record is not where $2 left it" >&2 |
| 586 | exit 1 |
| 587 | fi |
| 588 | } |
| 589 | # restack_refused <file> <what> <commit> [<who>]: a push of the record |
| 590 | # commit is refused, and the record stays where it was. |
| 591 | restack_refused() { |
| 592 | local before |
| 593 | before="$(git -C "$TMPDIR/open.git" rev-parse "$restack_ref")" |
| 594 | refused "$1" "$2" as "${4:-restacker}" git push --quiet open "$3:$restack_ref" |
| 595 | restack_at "$before" "a refused push: $2" |
| 596 | } |
| 597 | r1="$(git commit-tree "HEAD^{tree}" -p HEAD -m 'the restacked change, v1')" |
| 598 | r2="$(git commit-tree "HEAD^{tree}" -p HEAD -m 'the restacked change, rebased')" |
| 599 | r3="$(git commit-tree "HEAD^{tree}" -p HEAD -m 'the restacked change, rebased again')" |
| 600 | v1="$r1|contributor|the change|topic" |
| 601 | at_r2="$r2|restacker|the change|topic" |
| 602 | |
| 603 | rcreated="$(record_commit "$(restack_json ready 1 1 "$v1")" "$r1")" |
| 604 | as contributor git push --quiet open "$rcreated:$restack_ref" |
| 605 | restack_at "$rcreated" "the create" |
| 606 | |
| 607 | # A principal without the grant may not add the version, and neither may a |
| 608 | # key that names no principal. |
| 609 | appended="$(record_commit "$(restack_json ready 2 2 "$v1" "$at_r2")" "$rcreated" "$r2")" |
| 610 | for who in integrator anonymous; do |
| 611 | restack_refused nogrant.err "$who added a version to another's change without the restack grant" "$appended" "$who" |
| 612 | done |
| 613 | grep -q 'a key that names no principal' nogrant.err |
| 614 | refused nogrant.err "integrator added a version without the grant" \ |
| 615 | as integrator git push --quiet open "$appended:$restack_ref" |
| 616 | grep -q "only the change's owner, contributor, or a principal holding the approve grant (also accepted as request)" nogrant.err |
| 617 | |
| 618 | # Each way to write more than the one version is refused. |
| 619 | two="$(record_commit "$(restack_json ready 2 3 "$v1" "$at_r2" "$r3|restacker|the change|topic")" "$rcreated" "$r3")" |
| 620 | restack_refused two.err "a holder added two versions" "$two" |
| 621 | grep -q 'a new version adds one version to the 1 before it, and this record holds 3' two.err |
| 622 | rewritten="$(record_commit "$(restack_json ready 2 2 "$r1|contributor|rewritten|topic" "$at_r2")" "$rcreated" "$r2")" |
| 623 | restack_refused rewritten.err "a holder changed an earlier version" "$rewritten" |
| 624 | grep -q 'it changes version 1' rewritten.err |
| 625 | owned="$(record_commit "$(restack_json ready 2 2 "$v1" "$at_r2" | sed 's/"owner": "contributor"/"owner": "restacker"/')" "$rcreated" "$r2")" |
| 626 | restack_refused owned.err "a holder changed the owner" "$owned" |
| 627 | grep -q 'changes the owner from contributor to restacker' owned.err |
| 628 | retargeted="$(record_commit "$(restack_json ready 2 2 "$v1" "$at_r2" | sed 's/"target": "main"/"target": "release"/')" "$rcreated" "$r2")" |
| 629 | restack_refused retargeted.err "a holder changed the target" "$retargeted" |
| 630 | grep -q 'names the target release' retargeted.err |
| 631 | retitled="$(record_commit "$(restack_json ready 2 2 "$v1" "$r2|restacker|another title|topic")" "$rcreated" "$r2")" |
| 632 | restack_refused retitled.err "a holder changed the title" "$retitled" |
| 633 | grep -q 'is titled "another title"' retitled.err |
| 634 | moved="$(record_commit "$(restack_json ready 2 2 "$v1" "$r2|restacker|the change|elsewhere")" "$rcreated" "$r2")" |
| 635 | restack_refused moved.err "a holder moved the change to another branch" "$moved" |
| 636 | grep -q 'names the branch "elsewhere"' moved.err |
| 637 | forged="$(record_commit "$(restack_json ready 2 2 "$v1" "$r2|contributor|the change|topic")" "$rcreated" "$r2")" |
| 638 | restack_refused forged.err "a holder forged the owner as the uploader" "$forged" |
| 639 | grep -q 'names contributor as its uploader, and restacker is pushing it' forged.err |
| 640 | for state in draft withdrawn; do |
| 641 | stated="$(record_commit "$(restack_json "$state" 2 1 "$v1")" "$rcreated")" |
| 642 | restack_refused stated.err "a holder moved a ready change to $state" "$stated" |
| 643 | grep -q 'the restack grant changes no state' stated.err |
| 644 | done |
| 645 | |
| 646 | # The one version is added. |
| 647 | as restacker git push --quiet open "$appended:$restack_ref" |
| 648 | restack_at "$appended" "a version added under the restack grant" |
| 649 | |
| 650 | # Not to a draft, nor to a withdrawn change. The owner's moves between |
| 651 | # states are as they were. |
| 652 | paused="$(record_commit "$(restack_json draft 3 2 "$v1" "$at_r2")" "$appended")" |
| 653 | as contributor git push --quiet open "$paused:$restack_ref" |
| 654 | on_draft="$(record_commit "$(restack_json ready 4 3 "$v1" "$at_r2" "$r3|restacker|the change|topic")" "$paused" "$r3")" |
| 655 | restack_refused draft.err "a holder added a version to a draft" "$on_draft" |
| 656 | grep -q 'the change is draft, and the restack grant adds a version only to a change that is ready' draft.err |
| 657 | gone_r="$(record_commit "$(restack_json withdrawn 4 2 "$v1" "$at_r2")" "$paused")" |
| 658 | as contributor git push --quiet open "$gone_r:$restack_ref" |
| 659 | on_withdrawn="$(record_commit "$(restack_json ready 5 3 "$v1" "$at_r2" "$r3|restacker|the change|topic")" "$gone_r" "$r3")" |
| 660 | restack_refused withdrawn.err "a holder added a version to a withdrawn change" "$on_withdrawn" |
| 661 | grep -q 'the change is withdrawn, and the restack grant adds a version only to a change that is ready' withdrawn.err |
| 662 | back_r="$(record_commit "$(restack_json ready 5 2 "$v1" "$at_r2")" "$gone_r")" |
| 663 | as contributor git push --quiet open "$back_r:$restack_ref" |
| 664 | restack_at "$back_r" "the owner's re-ready" |
| 665 | |
| 666 | # Deleting stays the approve grant's alone. |
| 667 | refused rdelete.err "a holder of restack alone deleted a record" \ |
| 668 | as restacker git push --quiet --delete open "$restack_ref" |
| 669 | grep -q 'only a principal holding the approve grant (also accepted as request) may delete' rdelete.err |
| 670 | restack_at "$back_r" "a refused delete" |
| 671 | |
| 672 | # Holding restack beside approve leaves approve's powers as they were: |
| 673 | # requester unreadies the change, which restack alone may not. |
| 674 | held="$(record_commit "$(restack_json draft 6 2 "$v1" "$at_r2")" "$back_r")" |
| 675 | as requester git push --quiet open "$held:$restack_ref" |
| 676 | restack_at "$held" "an unready under the approve grant" |
| 677 | |
| 678 | # A holder keeps a named principal's own powers: it creates its own |
| 679 | # record, and unreadies it. |
| 680 | own_id=lmnopqrstuvwxyzklmnopqrstuvwxyzk |
| 681 | own_ref="refs/the-valley/changes/main/$own_id" |
| 682 | own_json() { |
| 683 | printf '{"schema": "valley-change-record/v1", "change_id": "%s", "target": "main", "owner": "restacker", "state": "%s", "seq": %s, "versions": [{"n": 1, "head": "git-sha1:%s", "title": "its own", "branch": "own", "uploader": "restacker"}], "active": 1}\n' \ |
| 684 | "$own_id" "$1" "$2" "$r1" |
| 685 | } |
| 686 | own="$(record_commit "$(own_json ready 1)" "$r1")" |
| 687 | as restacker git push --quiet open "$own:$own_ref" |
| 688 | own_draft="$(record_commit "$(own_json draft 2)" "$own")" |
| 689 | as restacker git push --quiet open "$own_draft:$own_ref" |
| 690 | [ "$(git -C "$TMPDIR/open.git" rev-parse "$own_ref")" = "$own_draft" ] |
| 691 | |
| 692 | # The grant opens nothing on integration requests: restacker files none, |
| 693 | # and neither replaces nor withdraws the one requester filed. |
| 694 | refused rrequest.err "a holder of restack alone filed a request" \ |
| 695 | as restacker git push --quiet open "$r2:refs/the-valley/integration-requests/main/c-$restack_id" |
| 696 | grep -q 'approve grant (also accepted as request)' rrequest.err |
| 697 | lacks_ref open "refs/the-valley/integration-requests/main/c-$restack_id" |
| 698 | filed_request="$(git -C "$TMPDIR/open.git" rev-parse "$request")" |
| 699 | refused rreplace.err "a holder of restack alone replaced a request" \ |
| 700 | as restacker git push --quiet --force open "$r2:$request" |
| 701 | grep -q 'approve grant (also accepted as request)' rreplace.err |
| 702 | refused rwithdraw.err "a holder of restack alone withdrew a request" \ |
| 703 | as restacker git push --quiet --delete open "$request" |
| 704 | grep -q 'approve grant (also accepted as request)' rwithdraw.err |
| 705 | [ "$(git -C "$TMPDIR/open.git" rev-parse "$request")" = "$filed_request" ] |
| 706 | |
| 707 | # ---------------------------------------------------------------------- |
| 708 | # Restack reports, at refs/the-valley/restack/main/<change-id>: a holder of |
| 709 | # the restack grant creates one, and extends it with a report whose one |
| 710 | # parent is the one before. Nothing else writes one, and nothing replaces |
| 711 | # its history or deletes it. |
| 712 | report_ref="refs/the-valley/restack/main/$restack_id" |
| 713 | # report_json [<result>]: a report about the restacked change, observed at |
| 714 | # its record and head now, with its branch at that head. |
| 715 | report_json() { |
| 716 | printf '{"schema": "valley-restack-report/v1", "change_id": "%s", "target": "main", "target_tip": "git-sha1:%s", "operation": "op-1", "result": "%s", "layers": [{"change_id": "%s", "record": "git-sha1:%s", "version": 2, "head": "git-sha1:%s", "branch": "topic", "branch_tip": "git-sha1:%s", "published": null}], "failing_layer": "%s", "conflict_paths": ["readme.txt"], "blocking_requests": [], "refusal": null}\n' \ |
| 717 | "$restack_id" "$(git -C "$TMPDIR/open.git" rev-parse main)" "${1:-conflict}" "$restack_id" "$held" "$r2" "$r2" "$restack_id" |
| 718 | } |
| 719 | # report_commit <json> [<file>] [<parent…>]: a commit holding the JSON as |
| 720 | # the one file given, report.json by default. |
| 721 | report_commit() { |
| 722 | local json="$1" file="${2:-report.json}" blob tree parent |
| 723 | local -a parents=() |
| 724 | shift |
| 725 | [ "$#" = 0 ] || shift |
| 726 | for parent in "$@"; do parents+=(-p "$parent"); done |
| 727 | blob="$(printf '%s' "$json" | git hash-object -w --stdin)" |
| 728 | tree="$(printf '100644 blob %s\t%s\n' "$blob" "$file" | git mktree)" |
| 729 | git commit-tree "$tree" "${parents[@]}" -m 'a restack report' |
| 730 | } |
| 731 | report_at() { |
| 732 | if [ "$(git -C "$TMPDIR/open.git" rev-parse "$report_ref")" != "$1" ]; then |
| 733 | echo "protect-e2e: the restack report is not where $2 left it" >&2 |
| 734 | exit 1 |
| 735 | fi |
| 736 | } |
| 737 | # report_refused <file> <what> <commit> [<who>] [<ref>]: the push is |
| 738 | # refused, and the report stays where it was. |
| 739 | report_refused() { |
| 740 | local before |
| 741 | before="$(git -C "$TMPDIR/open.git" rev-parse --verify --quiet "$report_ref" || true)" |
| 742 | refused "$1" "$2" as "${4:-restacker}" git push --quiet --force open "$3:${5:-$report_ref}" |
| 743 | if [ -n "$before" ]; then report_at "$before" "a refused push: $2"; else lacks_ref open "$report_ref"; fi |
| 744 | } |
| 745 | report="$(report_json)" |
| 746 | |
| 747 | # Who. |
| 748 | first_report="$(report_commit "$report")" |
| 749 | for who in contributor integrator anonymous; do |
| 750 | report_refused report-who.err "$who wrote a restack report without the grant" "$first_report" "$who" |
| 751 | grep -q 'refs/the-valley/restack/ takes writes only from a principal holding the restack grant' report-who.err |
| 752 | done |
| 753 | |
| 754 | # The ref: main alone, and a well-formed change ID that names a change the |
| 755 | # repository records. |
| 756 | report_refused report-target.err "a report for another target was written" "$first_report" restacker \ |
| 757 | "refs/the-valley/restack/release/$restack_id" |
| 758 | grep -q 'a restack report.s ref is refs/the-valley/restack/main/<change ID>' report-target.err |
| 759 | lacks_ref open "refs/the-valley/restack/release/$restack_id" |
| 760 | report_refused report-id.err "a report under a malformed change ID was written" "$first_report" restacker \ |
| 761 | "refs/the-valley/restack/main/${restack_id:0:31}" |
| 762 | grep -q '32 letters from k to z' report-id.err |
| 763 | unknown_id=zyxwvutsrqponmlkzyxwvutsrqponmlk |
| 764 | report_refused report-unknown.err "a report about a change nobody records was written" \ |
| 765 | "$(report_commit "${report//$restack_id/$unknown_id}")" restacker "refs/the-valley/restack/main/$unknown_id" |
| 766 | grep -q "holds no record of the change $unknown_id" report-unknown.err |
| 767 | |
| 768 | # The first report has no parent. |
| 769 | report_refused report-parent.err "a first report with a parent was written" \ |
| 770 | "$(report_commit "$report" report.json "$r1")" |
| 771 | grep -q 'first restack report has no parent' report-parent.err |
| 772 | as restacker git push --quiet open "$first_report:$report_ref" |
| 773 | report_at "$first_report" "the first report" |
| 774 | |
| 775 | # The report itself. |
| 776 | report_refused report-file.err "a report with a second file was written" \ |
| 777 | "$(git commit-tree "$(printf '100644 blob %s\treport.json\n100644 blob %s\textra\n' \ |
| 778 | "$(printf '%s' "$report" | git hash-object -w --stdin)" "$(printf x | git hash-object -w --stdin)" | git mktree)" \ |
| 779 | -p "$first_report" -m 'a restack report')" |
| 780 | grep -q "a restack report's tree holds report.json alone" report-file.err |
| 781 | report_refused report-name.err "a report under another name was written" \ |
| 782 | "$(report_commit "$report" record.json "$first_report")" |
| 783 | grep -q 'holds "record.json"' report-name.err |
| 784 | report_refused report-big.err "an oversized report was written" \ |
| 785 | "$(report_commit "$report$(head -c 65536 /dev/zero | tr '\0' ' ')" report.json "$first_report")" |
| 786 | grep -q 'more than the 65536' report-big.err |
| 787 | report_refused report-unknown-field.err "a report with an unknown field was written" \ |
| 788 | "$(report_commit "$(printf '%s' "$report" | sed 's/"refusal": null/"refusal": null, "note": "x"/')" report.json "$first_report")" |
| 789 | grep -q 'which the schema does not have' report-unknown-field.err |
| 790 | report_refused report-malformed.err "a report that is no JSON was written" \ |
| 791 | "$(report_commit "conflict in readme.txt" report.json "$first_report")" |
| 792 | grep -q 'its report.json does not read' report-malformed.err |
| 793 | report_refused report-other.err "a report about another change was written" \ |
| 794 | "$(report_commit "$(report_json | sed "s/\"change_id\": \"$restack_id\", \"target\"/\"change_id\": \"$own_id\", \"target\"/")" report.json "$first_report")" |
| 795 | grep -q "no layer names the change $own_id" report-other.err |
| 796 | |
| 797 | # History: a later report extends the one before, and nothing replaces the |
| 798 | # history or deletes it. |
| 799 | report_refused report-restart.err "a report that starts the history over was written" \ |
| 800 | "$(report_commit "$(report_json blocked | sed 's/"failing_layer": "[a-z]*"/"failing_layer": null/')")" |
| 801 | grep -q 'as its one parent, so a push only adds to the history of reports' report-restart.err |
| 802 | report_refused report-elsewhere.err "a report on another parent was written" \ |
| 803 | "$(report_commit "$report" report.json "$r1")" |
| 804 | grep -q 'as its one parent' report-elsewhere.err |
| 805 | second_report="$(report_commit "$report" report.json "$first_report")" |
| 806 | as restacker git push --quiet open "$second_report:$report_ref" |
| 807 | report_at "$second_report" "the report that extends the first" |
| 808 | refused report-delete.err "a restack report was deleted" \ |
| 809 | as restacker git push --quiet --delete open "$report_ref" |
| 810 | grep -q 'no push deletes one' report-delete.err |
| 811 | report_at "$second_report" "a refused delete" |
| 812 | |
| 813 | # A principal holding both grants writes a report too. This one is a |
| 814 | # block: it saw the branch missing, and names the request filed above at |
| 815 | # the head it read. |
| 816 | blocked_report="$(report_json blocked | sed \ |
| 817 | -e 's/"failing_layer": "[a-z]*"/"failing_layer": null/' \ |
| 818 | -e 's/"branch_tip": "[^"]*"/"branch_tip": null/' \ |
| 819 | -e "s|\"blocking_requests\": \[\]|\"blocking_requests\": [{\"ref\": \"$request\", \"tip\": \"git-sha1:$filed_request\"}]|")" |
| 820 | grep -q '"branch_tip": null' <<< "$blocked_report" |
| 821 | grep -q "\"blocking_requests\": \[{\"ref\": \"$request\"" <<< "$blocked_report" |
| 822 | third_report="$(report_commit "$blocked_report" report.json "$second_report")" |
| 823 | as requester git push --quiet open "$third_report:$report_ref" |
| 824 | report_at "$third_report" "a report by a holder of both grants" |
| 825 | |
| 826 | # ---------------------------------------------------------------------- |
| 827 | # Attestation refs: anyone may create one, if it is what its |
| 828 | # name says, and it opens under the keys this host accepts. |
| 829 | attest run --key "$TMPDIR/signer" --name signer.valley.invalid/attestations \ |
| 830 | --command ok=true > run.out |
| 831 | att="$(sed -n 's/^stored \(refs[^ ]*\) -> .*/\1/p' run.out)" |
| 832 | digest="$(echo "$att" | cut -d/ -f4)" |
| 833 | keyhash="$(echo "$att" | cut -d/ -f5)" |
| 834 | as contributor git push --quiet guarded "$att:$att" |
| 835 | has_ref guarded "$att" |
| 836 | landed="$(git -C "$TMPDIR/guarded.git" rev-parse "$att")" |
| 837 | |
| 838 | # … and nobody may rewrite or drop one, writer or not. An update |
| 839 | # only adds notes. |
| 840 | git update-ref "$att" "$(git rev-parse HEAD^{tree})" |
| 841 | refused rewritten.err "an attestation ref was rewritten" \ |
| 842 | as contributor git push --quiet --force guarded "$att:$att" |
| 843 | grep -q "may not update $att" rewritten.err |
| 844 | refused rewritten-by-writer.err "a writer rewrote an attestation ref" \ |
| 845 | as integrator git push --quiet --force guarded "$att:$att" |
| 846 | grep -q "may not update $att" rewritten-by-writer.err |
| 847 | refused dropped.err "an attestation ref was deleted" \ |
| 848 | as integrator git push --quiet --delete guarded "$att" |
| 849 | grep -q 'published evidence is never removed' dropped.err |
| 850 | [ "$(git -C "$TMPDIR/guarded.git" rev-parse "$att")" = "$landed" ] |
| 851 | git update-ref "$att" "$landed" |
| 852 | |
| 853 | # Occupation: a name in the attestation namespace is held for |
| 854 | # good by whoever creates it first, so a name may only hold |
| 855 | # what it says. Each of these would keep the real signer's |
| 856 | # evidence out, and each is refused on a project that has |
| 857 | # none yet. |
| 858 | other_hash="$(attest key --key "$TMPDIR/other" --name other.valley.invalid/attestations | cut -d+ -f2)" |
| 859 | occupy() { |
| 860 | local name="$1" target="$2" err="$3" why="$4" |
| 861 | refused "$err" "an attestation ref took a name it does not hold: $why" \ |
| 862 | as contributor git push --quiet sealed "$target:refs/the-valley/attestations/$name" |
| 863 | lacks_ref sealed "refs/the-valley/attestations/$name" |
| 864 | } |
| 865 | # A ref named for the digest alone would block every ref |
| 866 | # beneath it, and a name that is not hex is no signer's. |
| 867 | occupy "$digest" "$landed" shape.err "the digest alone" |
| 868 | grep -q '<digest>/<key hash>' shape.err |
| 869 | occupy "$digest/$keyhash/extra" "$landed" deep.err "a segment too many" |
| 870 | occupy "$digest/NOTAHASH" "$landed" nothex.err "a key hash that is not hex" |
| 871 | # The real signer's note, under another signer's key hash. |
| 872 | occupy "$digest/$other_hash" "$landed" squat.err "another signer's slot" |
| 873 | grep -q "no signature under the key hash $other_hash" squat.err |
| 874 | # The real signer's note, under another tree's digest. |
| 875 | elsewhere="$(printf '%064d' 7)" |
| 876 | occupy "$elsewhere/$keyhash" "$landed" subject.err "another tree's slot" |
| 877 | grep -q 'is about the tree' subject.err |
| 878 | # Something that is not a tree of notes at all. |
| 879 | occupy "$elsewhere/$keyhash" HEAD commit.err "a commit" |
| 880 | grep -q 'points at a commit' commit.err |
| 881 | # The real signer's note with a line appended that no verifier |
| 882 | # would read: the signature in it is still genuine, and the |
| 883 | # note is still unusable, so it may not take the name either. |
| 884 | git cat-file blob "$landed:ok/statement.note" > genuine.note |
| 885 | { cat genuine.note; printf '— x\t AAAAAAA=\n'; } > poisoned.note |
| 886 | blob="$(git hash-object -w poisoned.note)" |
| 887 | sub="$(printf '100644 blob %s\tstatement.note\n' "$blob" | git mktree)" |
| 888 | poisoned="$(printf '040000 tree %s\tok\n' "$sub" | git mktree)" |
| 889 | occupy "$digest/$keyhash" "$poisoned" poisoned.err "a poisoned note" |
| 890 | grep -q 'does not open' poisoned.err |
| 891 | # A tree naming one subtree over and over, which describes more |
| 892 | # paths than any walk could visit, is refused at a bound. |
| 893 | wide="$sub" |
| 894 | for _ in 1 2; do |
| 895 | wide="$(for i in $(seq -w 1 64); do printf '040000 tree %s\tt%s\n' "$wide" "$i"; done | git mktree)" |
| 896 | done |
| 897 | occupy "$digest/$keyhash" "$wide" wide.err "a tree of shared subtrees" |
| 898 | grep -q 'entries in all' wide.err |
| 899 | # The genuine note, at the root of a tree written by hand under the name |
| 900 | # ok/statement.note, a name with a slash in it, which git never writes and |
| 901 | # mktree refuses to. The tree is written byte by byte: the mode, the name, |
| 902 | # a NUL, and the note's object ID in binary. |
| 903 | flat_file="$TMPDIR/flat-tree" |
| 904 | printf '100644 %s\0' ok/statement.note > "$flat_file" |
| 905 | # shellcheck disable=SC2059 # the format is the object ID as \x escapes |
| 906 | printf "$(git rev-parse "$landed:ok/statement.note" | sed 's/../\\x&/g')" >> "$flat_file" |
| 907 | flat="$(git hash-object -t tree --literally -w "$flat_file")" |
| 908 | occupy "$digest/$keyhash" "$flat" flat.err "a note named with a slash" |
| 909 | grep -q 'it holds an entry named "ok/statement.note", and git writes no name with a slash' flat.err |
| 910 | # The ref the real evidence wants is still free, and takes it. |
| 911 | as anonymous git push --quiet sealed "$att:$att" |
| 912 | has_ref sealed "$att" |
| 913 | |
| 914 | # A host that names no keys accepts no attestation at all. |
| 915 | refused nokeys.err "an attestation was accepted where no key is named" \ |
| 916 | as anonymous git push --quiet open "$att:$att" |
| 917 | grep -q 'names no keys it accepts evidence from' nokeys.err |
| 918 | lacks_ref open "$att" |
| 919 | |
| 920 | # ---------------------------------------------------------------------- |
| 921 | # A hook a project composes after the policy runs once the |
| 922 | # policy accepts, and can only refuse more: released's refuses |
| 923 | # branches under frozen/. |
| 924 | git branch frozen/one |
| 925 | refused frozen.err "the composed hook did not run" \ |
| 926 | as contributor git push --quiet released frozen/one |
| 927 | grep -q 'released: refs/heads/frozen/one is frozen' frozen.err |
| 928 | lacks_ref released refs/heads/frozen/one |
| 929 | as contributor git push --quiet released idea/one |
| 930 | has_ref released refs/heads/idea/one |
| 931 | |
| 932 | # ---------------------------------------------------------------------- |
| 933 | # A protected project's first main. No push may create it — sealed's main |
| 934 | # has refused every one above — and the integrator lands only onto a main |
| 935 | # that exists. So it is seeded on the host, by the git user, from a source |
| 936 | # whose commit the operator has checked: fetched, compared with the commit |
| 937 | # expected, and created only if no main exists yet. A local write is not a |
| 938 | # push, so no hook is asked. |
| 939 | expected="$(git rev-parse HEAD)" |
| 940 | git -C "$TMPDIR/sealed.git" fetch --quiet --no-tags "$TMPDIR/work" HEAD |
| 941 | [ "$(git -C "$TMPDIR/sealed.git" rev-parse FETCH_HEAD)" = "$expected" ] |
| 942 | git -C "$TMPDIR/sealed.git" update-ref refs/heads/main "$expected" "" |
| 943 | has_ref sealed refs/heads/main |
| 944 | # The empty old value makes the seed a creation: over a main that exists, |
| 945 | # it refuses. |
| 946 | if git -C "$TMPDIR/sealed.git" update-ref refs/heads/main "$expected~1" "" 2> /dev/null; then |
| 947 | echo "protect-e2e: a seed overwrote a main that existed" >&2 |
| 948 | exit 1 |
| 949 | fi |
| 950 | [ "$(git -C "$TMPDIR/sealed.git" rev-parse main)" = "$expected" ] |
| 951 | |
| 952 | # ---------------------------------------------------------------------- |
| 953 | # The hook valley-init converges on is live. init-e2e holds the rendered |
| 954 | # init script to installing each project's declared hook over repositories |
| 955 | # that already exist, over a stand-in for valleyhook. Here the same script |
| 956 | # wires a repository that already has history, with the real valleyhook, |
| 957 | # and the hook it installed is pushed to. Relocating the data directory is |
| 958 | # the only edit to the script: the sandbox cannot write /srv. |
| 959 | data="$TMPDIR/host/srv/git" |
| 960 | mkdir -p "$data" |
| 961 | sed "s|/srv/git|$data|g" "$initScriptPath" > "$TMPDIR/init.sh" |
| 962 | git init --quiet --bare "$data/guarded.git" |
| 963 | git clone --quiet "$data/guarded.git" "$TMPDIR/existing" |
| 964 | git -C "$TMPDIR/existing" commit --quiet --allow-empty -m one |
| 965 | git -C "$TMPDIR/existing" push --quiet origin main |
| 966 | before="$(git -C "$TMPDIR/existing" rev-parse HEAD)" |
| 967 | bash "$TMPDIR/init.sh" |
| 968 | git -C "$TMPDIR/existing" commit --quiet --allow-empty -m two |
| 969 | refused existing.err "an untagged key wrote a protected ref of a pre-existing repository" \ |
| 970 | as anonymous git -C "$TMPDIR/existing" push --quiet origin main |
| 971 | grep -q '<untagged key>' existing.err |
| 972 | grep -q 'refs/heads/main' existing.err |
| 973 | if [ "$(git -C "$data/guarded.git" rev-parse main)" != "$before" ]; then |
| 974 | echo "protect-e2e: guarded kept a ref through a rejected push" >&2 |
| 975 | exit 1 |
| 976 | fi |
| 977 | as integrator git -C "$TMPDIR/existing" push --quiet origin main |
| 978 | if [ "$(git -C "$data/guarded.git" rev-parse main)" != "$(git -C "$TMPDIR/existing" rev-parse HEAD)" ]; then |
| 979 | echo "protect-e2e: a declared writer could not write a protected ref" >&2 |
| 980 | exit 1 |
| 981 | fi |
| 982 | |
| 983 | touch "$out" |