From 748e4bc5e6c87d283e5fac9ea8238e9d2d6b079b Mon Sep 17 00:00:00 2001 From: pingqiu Date: Mon, 30 Nov 2026 06:31:25 -0800 Subject: [PATCH] lab tag: the wiring check pins an executed refusal; every read is checked The refusal must be an executed line, RUN bash scripts/refuse-lab-binary.sh, after the recipe's last go build and naming every binary the recipe builds (go build ... -o /out/NAME); a comment no longer counts. Each recipe and the publish workflow is read once with a checked read, so a read error fails instead of reading as no match; failures go to stderr. The controls now start from copies of the real recipes and publish workflow: the guard deleted with its comment kept (both recipes), commented out, a built binary left out, a new binary not refused, the refusal before the build, and an unreadable recipe (Linux), each refused for its own reason. Co-Authored-By: Claude Opus 5.5 --- .../.github/scripts/lab-tag-check-test.sh | 47 ++++++++++++------- .../.github/scripts/lab-tag-check.sh | 37 ++++++++++----- 2 files changed, 56 insertions(+), 28 deletions(-) diff --git a/seaweed-block/.github/scripts/lab-tag-check-test.sh b/seaweed-block/.github/scripts/lab-tag-check-test.sh index 18ba9a599d..c919d8e201 100644 --- a/seaweed-block/.github/scripts/lab-tag-check-test.sh +++ b/seaweed-block/.github/scripts/lab-tag-check-test.sh @@ -1,39 +1,54 @@ #!/usr/bin/env bash # Controls for lab-tag-check.sh and scripts/refuse-lab-binary.sh. Each case -# builds a small git repository with one contamination and expects a refusal; -# the clean repository passes. The refusal script is driven with stand-in -# binaries. Exit 0 when every control holds. +# builds a small git repository from the real image recipes and publish +# workflow with one contamination, and expects a refusal; the unchanged copy +# passes. The refusal script is driven with stand-in binaries. Exit 0 when +# every control holds. set -euo pipefail here=$(cd "$(dirname "$0")" && pwd) +src=$(cd "$here/../.." && pwd) check="$here/lab-tag-check.sh" -refuse="$here/../../scripts/refuse-lab-binary.sh" +refuse="$src/scripts/refuse-lab-binary.sh" t=$(mktemp -d); trap 'rm -rf -- "${t:?}"' EXIT tag=swblock_lab_bigvol +sw=deploy/k8s/g15b/Dockerfile.sw-block +csi=deploy/k8s/g15b/Dockerfile.blockcsi +pub=.github/workflows/publish-alpha-images.yml n=0 gadd() { git -C "$r" -c core.autocrlf=false -c core.safecrlf=false add -A; } -repo() { # a clean repository in $t/r +repo() { # the real recipes and publish workflow, plus one allowed file, in $t/r n=$((n+1)); r="$t/r$n" - mkdir -p "$r/deploy/k8s" "$r/.github/workflows" "$r/core/geometry" "$r/core/x" - printf 'RUN go build -o /out/b ./cmd/b\nRUN bash scripts/refuse-lab-binary.sh /out/b\n' > "$r/deploy/k8s/Dockerfile.b" - printf 'steps:\n - with:\n file: deploy/k8s/Dockerfile.b\n' > "$r/.github/workflows/publish-alpha-images.yml" + mkdir -p "$r/deploy/k8s/g15b" "$r/.github/workflows" "$r/core/geometry" "$r/core/x" + tr -d '\r' < "$src/$sw" > "$r/$sw" + tr -d '\r' < "$src/$csi" > "$r/$csi" + tr -d '\r' < "$src/$pub" > "$r/$pub" printf '//go:build %s\npackage geometry\n' "$tag" > "$r/core/geometry/volume_size_lab.go" printf 'package x\n' > "$r/core/x/x.go" git -C "$r" init -q && gadd } +edit() { # edit FILE SED: change one file of the current repository + sed -i "$2" "$r/$1" && gadd +} expect() { # expect pass|fail name local want=$1 name=$2 rc=0 bash "$check" "$r" >/dev/null 2>&1 || rc=$? - if [ "$want" = pass ] && [ $rc -ne 0 ]; then echo "FAIL: $name: refused"; exit 1; fi + if [ "$want" = pass ] && [ $rc -ne 0 ]; then echo "FAIL: $name: refused"; bash "$check" "$r" || true; exit 1; fi if [ "$want" = fail ] && [ $rc -eq 0 ]; then echo "FAIL: $name: passed"; exit 1; fi } -repo; expect pass "clean repository" +repo; expect pass "the real recipes, unchanged" repo; printf '// %s\npackage x\n' "$tag" > "$r/core/x/x.go"; gadd; expect fail "the tag outside the allowlist" -repo; printf 'RUN go build -tags %s -o /out/b ./cmd/b\nRUN bash scripts/refuse-lab-binary.sh /out/b\n' "$tag" > "$r/deploy/k8s/Dockerfile.b"; gadd; expect fail "the tag in a Dockerfile" -repo; printf 'RUN go build -o /out/b ./cmd/b\n' > "$r/deploy/k8s/Dockerfile.b"; gadd; expect fail "a Dockerfile without the refusal" -repo; printf 'RUN bash scripts/refuse-lab-binary.sh /out/b\nRUN go build -o /out/b ./cmd/b\n' > "$r/deploy/k8s/Dockerfile.b"; gadd; expect fail "the refusal before the build" -repo; printf 'steps:\n - run: go build ./...\n - with:\n file: deploy/k8s/Dockerfile.b\n' > "$r/.github/workflows/publish-alpha-images.yml"; gadd; expect fail "a go build in the publish workflow" -repo; printf 'RUN go build -o /out/b ./cmd/b\n' > "$r/Dockerfile.root"; printf 'steps:\n - with:\n file: Dockerfile.root\n' > "$r/.github/workflows/publish-alpha-images.yml"; gadd; expect fail "publishing from a Dockerfile outside deploy/" -repo; rm -f "$r/.github/workflows/publish-alpha-images.yml"; gadd; expect fail "no readable publish workflow" +repo; edit "$sw" "s#go build -o /out/blockvolume#go build -tags $tag -o /out/blockvolume#"; expect fail "the tag in a real recipe's build" +repo; edit "$sw" '/^RUN bash scripts\/refuse-lab-binary.sh/d'; expect fail "the real recipe's RUN guard deleted, its comment kept" +repo; edit "$csi" '/^RUN bash scripts\/refuse-lab-binary.sh/d'; expect fail "the CSI recipe's RUN guard deleted, its comment kept" +repo; edit "$sw" 's#^RUN bash scripts/refuse-lab-binary.sh#\# RUN bash scripts/refuse-lab-binary.sh#'; expect fail "the RUN guard commented out" +repo; edit "$sw" 's#^\(RUN bash scripts/refuse-lab-binary.sh.*\) /out/sw-block#\1#'; expect fail "a built binary left out of the refusal" +repo; edit "$sw" 's#/out/blockvolume ./cmd/blockvolume#/out/blockvolume ./cmd/blockvolume \&\& \\n CGO_ENABLED=0 go build -o /out/extra ./cmd/extra#'; expect fail "a new binary built and not refused" +repo; guard=$(grep '^RUN bash scripts/refuse-lab-binary.sh' "$r/$sw"); edit "$sw" '/^RUN bash scripts\/refuse-lab-binary.sh/d'; sed -i "s#^RUN CGO_ENABLED=0 go build#$guard\nRUN CGO_ENABLED=0 go build#" "$r/$sw"; gadd; expect fail "the refusal before the build" +repo; printf ' - run: go build ./...\n' >> "$r/$pub"; gadd; expect fail "a go build in the publish workflow" +repo; edit "$pub" "s#file: $csi#file: Dockerfile.root#"; printf 'RUN go build -o /out/b ./cmd/b\n' > "$r/Dockerfile.root"; gadd; expect fail "publishing from a Dockerfile outside deploy/" +repo; rm -f "$r/$pub"; gadd; expect fail "no readable publish workflow" +repo; ln -s missing-target "$r/deploy/k8s/g15b/Dockerfile.dangling" 2>/dev/null || true +if [ -L "$r/deploy/k8s/g15b/Dockerfile.dangling" ]; then gadd; expect fail "an image Dockerfile that cannot be read"; else echo "note: no symlinks here; the unreadable-recipe control runs on Linux"; fi r="$t/missing"; expect fail "a missing repository" r="$t/notgit"; mkdir -p "$r"; expect fail "a directory the scan cannot read as a repository" diff --git a/seaweed-block/.github/scripts/lab-tag-check.sh b/seaweed-block/.github/scripts/lab-tag-check.sh index 4dc0b9b035..e07cd4e1cd 100644 --- a/seaweed-block/.github/scripts/lab-tag-check.sh +++ b/seaweed-block/.github/scripts/lab-tag-check.sh @@ -26,7 +26,7 @@ core/geometry/volume_size_release_test.go docs/design/invariants.md internal/buildinfo/buildinfo.go scripts/refuse-lab-binary.sh' -fail() { echo "lab-tag: $*"; exit 1; } +fail() { echo "lab-tag: $*" >&2; exit 1; } [ -d "$root" ] || fail "no repository at $root" set +e hits=$(git -C "$root" grep -l -- "$tag") @@ -44,21 +44,34 @@ while IFS= read -r f; do done <<< "$hits" [ -z "$bad" ] || fail "$tag named outside the allowlist:$bad" -dockerfiles=$(cd "$root" && find deploy -type f -name 'Dockerfile*' | sort) || fail "cannot list deploy/" +# read FILE: its whole content, once; a read that fails is a failure, never +# "no match". +read_file() { local c; c=$(cat -- "$root/$1") || fail "cannot read $1"; printf '%s\n' "$c"; } +dockerfiles=$(cd "$root" && find deploy \( -type f -o -type l \) -name 'Dockerfile*' | sort) || fail "cannot list deploy/" [ -n "$dockerfiles" ] || fail "no image Dockerfile under deploy/" for d in $dockerfiles; do - [ -r "$root/$d" ] || fail "cannot read $d" - last=$(grep -n 'go build' "$root/$d" | tail -1 | cut -d: -f1 || true) - [ -n "$last" ] || continue - refuse=$(grep -n 'refuse-lab-binary.sh' "$root/$d" | tail -1 | cut -d: -f1 || true) - [ -n "$refuse" ] && [ "$refuse" -gt "$last" ] || fail "$d builds a binary without refusing a lab build after it (scripts/refuse-lab-binary.sh)" + body=$(read_file "$d") || exit 1 + # The binaries it builds (go build ... -o /out/NAME), and the line of its + # last build. A recipe that builds nothing needs no refusal. + built=$(printf '%s\n' "$body" | grep -oE 'go build[^&\]*-o /out/[A-Za-z0-9_.-]+' | grep -oE '/out/[A-Za-z0-9_.-]+$' | sort -u) || true + [ -n "$built" ] || continue + last=$(printf '%s\n' "$body" | grep -nE 'go build' | tail -1 | cut -d: -f1) + # The refusal must be an executed RUN line (not a comment) after the last + # build, naming every binary the recipe builds. + refuse=$(printf '%s\n' "$body" | grep -nE '^RUN bash scripts/refuse-lab-binary\.sh( |$)' | tail -1) || fail "$d builds ${built//$'\n'/ } without a RUN of scripts/refuse-lab-binary.sh" + at=${refuse%%:*} + [ "$at" -gt "$last" ] || fail "$d runs the refusal before its last build" + for b in $built; do + case " ${refuse#*:} " in *" $b "*) ;; *) fail "$d does not refuse $b, which it builds" ;; esac + done done -pub="$root/.github/workflows/publish-alpha-images.yml" -[ -r "$pub" ] || fail "cannot read the publish workflow" -! grep -q 'go build' "$pub" || fail "the publish workflow runs its own go build" -files=$(grep -E '^[[:space:]]*file:' "$pub" | sed -E 's/^[[:space:]]*file:[[:space:]]*//') || fail "the publish workflow names no Dockerfile" +pub=.github/workflows/publish-alpha-images.yml +pubbody=$(read_file "$pub") || exit 1 +case "$pubbody" in *"go build"*) fail "the publish workflow runs its own go build" ;; esac +files=$(printf '%s\n' "$pubbody" | grep -E '^[[:space:]]*file:' | sed -E 's/^[[:space:]]*file:[[:space:]]*//') || fail "the publish workflow names no Dockerfile" for f in $files; do printf '%s\n' "$dockerfiles" | grep -qxF -- "$f" || fail "the publish workflow builds from $f, not an image Dockerfile under deploy/" - grep -q 'refuse-lab-binary.sh' "$root/$f" || fail "the publish workflow builds from $f, which does not refuse a lab build" + fbody=$(read_file "$f") || exit 1 + printf '%s\n' "$fbody" | grep -qE '^RUN bash scripts/refuse-lab-binary\.sh ' || fail "the publish workflow builds from $f, which does not run the refusal" done echo "lab-tag: clean"