#!/usr/bin/env bash # One file says what each gate *is*, so "what CI runs" and "what an operator ran # before pushing" stop being two different claims. CI calls one gate per step, so # the job log still names the gate that failed; `all` runs them in order and # stops at the first failure. # # scripts/ci-gates.sh restore|build|unit|integration|schema-script|smoke # scripts/ci-gates.sh image|scratch-image|compose-smoke|scratch-smoke|all # scripts/ci-gates.sh console (needs a real Chromium; not part of `all`) # scripts/ci-gates.sh readme-counts scripts/ (compares the README's # assertion count with what that gate printed # in this run; needs GATE_TOTALS_DIR) # scripts/ci-gates.sh lint (the workflow and shell lint CI runs. Not in # `all` for the console gate's reason: a machine # that has neither the pinned tools nor a # supported platform exits 2 — "cannot run" — # which is a different answer from "clean".) # # Not here on purpose: scripts/geo-db-freshness.sh. It asks the vendor what the # newest geo database is, so it is the one gate that talks to the internet, and # putting it behind `all` would make the offline claim below false. It has its own # scheduled job (.github/workflows/geo-freshness.yml). # # OPENWAF_ADMIN_TOKEN defaults below to the same throwaway CI uses; set it to # override. Nothing here talks to anything outside the run. set -euo pipefail cd "$(dirname "$0")/.." # The compose gate needs an admin token for the stack it starts. It is not a # secret — that stack exists only inside this run and is torn down on exit — but # requiring it meant `ci-gates.sh all` stopped at the last gate for anyone who # forgot to export one, which made the one-command entry point a lie. Same value # CI uses, overridable by the caller. export OPENWAF_ADMIN_TOKEN="${OPENWAF_ADMIN_TOKEN:-ci-only-throwaway}" banner() { printf '\n\033[1m=== gate: %s ===\033[0m\n' "$1"; } restore() { banner restore; dotnet restore dotnet/OpenWaf.slnx; } # TreatWarningsAsErrors is set in Directory.Build.props, so this is also the # warning gate. build() { banner build; dotnet build dotnet/OpenWaf.slnx -c Release --no-restore; } unit() { banner unit; dotnet test dotnet/OpenWaf.Tests.Unit/OpenWaf.Tests.Unit.csproj -c Release --no-build; } integration() { banner integration; dotnet test dotnet/OpenWaf.Tests.Integration/OpenWaf.Tests.Integration.csproj -c Release --no-build; } smoke() { banner smoke; ./scripts/smoke.sh; } # The DDL the shipped binary carries (Persistence/Schema/schema.sqlite.sql) is # generated from the EF model, not written by hand. This gate regenerates it and # refuses the run when the committed copy is not what the current model produces, # which is what keeps "the image creates the schema the model describes" a # checked claim. It exists because the NativeAOT build cannot ask EF to build a # model at run time at all, so for that build the file *is* the schema step. schema_script() { banner schema-script local committed=dotnet/OpenWaf.Infrastructure/Persistence/Schema/schema.sqlite.sql local regenerated regenerated="$(mktemp)" # --tool-manifest: the manifest lives in dotnet/, and `dotnet tool restore` # from the repo root finds no manifest, prints "No tools were restored." and # still exits 0 — measured on the runner at 7c5e1ef, where the gate then died # on "Run \"dotnet tool restore\" to make the \"dotnet-ef\" command available." # A successful restore is therefore checked by asking the tool for its version, # not by trusting the restore's exit code. if ! dotnet tool restore --tool-manifest dotnet/dotnet-tools.json >/dev/null 2>&1 \ || ! (cd dotnet && dotnet ef --version) >/dev/null 2>&1; then printf 'the pinned dotnet-ef tool is not available: cannot check the schema script\n' >&2 return 2 fi # OPENWAF_MIGRATE_PROVIDER=sqlite: the design-time factory defaults to postgres, # and a postgres DDL checked against a sqlite file would fail for the wrong # reason. if ! (cd dotnet && OPENWAF_MIGRATE_PROVIDER=sqlite dotnet ef dbcontext script \ -c OpenWafDbContext -p OpenWaf.Infrastructure -s OpenWaf.Infrastructure \ -o "$regenerated" >/dev/null); then printf 'dotnet ef dbcontext script could not run: cannot check the schema script\n' >&2 return 2 fi if cmp -s "$committed" "$regenerated"; then printf ' ok the committed schema script is what the current model generates (%s bytes)\n' \ "$(wc -c < "$committed" | tr -d ' ')" else printf ' FAIL the committed schema script is not what the current model generates\n' >&2 diff -u "$committed" "$regenerated" | head -40 >&2 return 1 fi } # The plain build the README shows, plus its size: the compose path would hide a # Dockerfile regression that this step catches. image() { banner image; docker build -t openwaf:ci .; docker image ls openwaf:ci --format '{{.Repository}}:{{.Tag}} {{.Size}}'; } compose_smoke() { banner compose-smoke; ./scripts/compose-smoke.sh; } # The same program on a base of nothing at all: scratch, plus the shared objects # it was linked against, plus the CA bundle. Its size is printed next to the # chiseled one's because the difference between the two is the claim being made. scratch_image() { banner scratch-image docker build -f Dockerfile.scratch -t openwaf-scratch:ci . # One argument per `docker image ls` — two is a usage error, exit 1. The # chiseled line prints nothing in a job that never built it (the scratch job # is its own job), which is why it is a separate call rather than a failure. docker image ls --format 'scratch {{.Repository}}:{{.Tag}} {{.Size}}' openwaf-scratch:ci docker image ls --format 'chiseled {{.Repository}}:{{.Tag}} {{.Size}}' openwaf:ci } # The production smoke, unchanged, against the scratch image. COMPOSE_FILE is # appended to rather than replaced, so a caller that already names the CI # overlay (the workflow does) keeps it and gains only the dockerfile swap. scratch_smoke() { banner scratch-smoke COMPOSE_FILE="${COMPOSE_FILE:-docker-compose.yml}:docker-compose.scratch.yml" ./scripts/compose-smoke.sh } # The browser gate over the operator page. It is a gate, so it lives here too and # CI calls it by the same name. It is NOT part of `all`: it needs a real # Chromium, and a machine without one exits 2 ("cannot run"), which is a # different answer from 1 ("the page is wrong") and would otherwise make the # one-command entry point unrunnable. Run it explicitly before pushing a change # to the console. console() { banner console; ./scripts/console-smoke.sh; } # Checksum helper: sha256sum on Linux, shasum on a macOS host, and a refusal to # check nothing when neither is present. sha256_verify() { if command -v sha256sum >/dev/null 2>&1; then printf '%s %s\n' "$2" "$1" | sha256sum -c - elif command -v shasum >/dev/null 2>&1; then printf '%s %s\n' "$2" "$1" | shasum -a 256 -c - else printf 'no sha256 tool on this host: refusing to download an unverified binary\n' >&2 return 2 fi } # The lint CI runs. It lives here because until this stage existed the workflow # files and the smoke scripts were checked by exactly one caller — a CI job — and # `ci-gates.sh all` could be green on a tree CI would refuse (OPEN-44). The # versions and checksums are pinned here rather than in the workflow, and the CI # job now calls this function, so the two callers cannot drift into linting # different versions of the same gate. # # A tool already on PATH is used as it is, and the version it prints is the # version that did the linting — that is the number the README quotes. Otherwise # the pinned release is downloaded, which is supported on linux x86_64 and arm64. # Anywhere else this says so and exits 2: a lint stage that silently passes on a # machine where it never ran is the defect OPEN-44 is about. lint() { banner lint local workdir; workdir=$(mktemp -d) if command -v actionlint >/dev/null 2>&1; then actionlint --version | head -1 actionlint -no-color .github/workflows/*.yml elif [ "$(uname -s)" = Linux ]; then local version=1.7.12 local sha=8aca8db96f1b94770f1b0d72b6dddcb1ebb8123cb3712530b08cc387b349a3d8 local arch case "$(uname -m)" in x86_64) arch=amd64 ;; aarch64) arch=arm64 ;; *) printf 'actionlint: unsupported architecture %s — cannot run (exit 2), not a pass\n' "$(uname -m)" >&2; return 2 ;; esac curl -fsSL -o "$workdir/actionlint.tar.gz" \ "https://github.com/rhysd/actionlint/releases/download/v${version}/actionlint_${version}_linux_${arch}.tar.gz" sha256_verify "$workdir/actionlint.tar.gz" "$sha" tar xzf "$workdir/actionlint.tar.gz" -C "$workdir" actionlint "$workdir/actionlint" --version | head -1 "$workdir/actionlint" -no-color .github/workflows/*.yml else printf 'actionlint is not installed and this is not Linux: the lint gate cannot run (exit 2), which is not a pass\n' >&2 return 2 fi if command -v shellcheck >/dev/null 2>&1; then shellcheck --version | sed -n 's/^version: //p' shellcheck -s bash scripts/*.sh elif [ "$(uname -s)" = Linux ]; then local sv=0.11.0 local sv_x86_64=8c3be12b05d5c177a04c29e3c78ce89ac86f1595681cab149b65b97c4e227198 local sv_aarch64=12b331c1d2db6b9eb13cfca64306b1b157a86eb69db83023e261eaa7e7c14588 local sarch ssha case "$(uname -m)" in x86_64) sarch=x86_64; ssha=$sv_x86_64 ;; aarch64) sarch=aarch64; ssha=$sv_aarch64 ;; *) printf 'shellcheck: unsupported architecture %s — cannot run (exit 2), not a pass\n' "$(uname -m)" >&2; return 2 ;; esac curl -fsSL -o "$workdir/shellcheck.tar.xz" \ "https://github.com/koalaman/shellcheck/releases/download/v${sv}/shellcheck-v${sv}.linux.${sarch}.tar.xz" sha256_verify "$workdir/shellcheck.tar.xz" "$ssha" tar xJf "$workdir/shellcheck.tar.xz" -C "$workdir" "$workdir/shellcheck-v${sv}/shellcheck" --version | sed -n 's/^version: //p' "$workdir/shellcheck-v${sv}/shellcheck" -s bash scripts/*.sh else printf 'shellcheck is not installed and this is not Linux: the lint gate cannot run (exit 2), which is not a pass\n' >&2 return 2 fi } # The README names an assertion count per gate. Those numbers were copied by # hand out of a log, and they drift: OPEN-46 measured the README claiming # scripts/compose-smoke.sh had 229 assertions while that gate was printing 356. # A number nobody checks is not a measurement, so this gate compares what the # README claims with what the gate recorded in this run. Each gate writes its own # total into $GATE_TOTALS_DIR when that variable is set — CI sets it once per # job, `all` sets it for a local run. With nothing to compare this gate exits 2 # ("cannot verify"), because a check that reports a pass because it had no input # is the defect this is about. readme_counts() { local gate="${1:-}" banner "readme-counts $gate" if [ -z "$gate" ]; then printf 'usage: %s readme-counts scripts/\n' "$0" >&2 return 2 fi if [ ! -f README.md ]; then printf 'no README.md to read: cannot verify (exit 2), not a pass\n' >&2 return 2 fi local claimed claimed=$(awk -v g="$gate" '$1==g && $3=="assertions" { print $2; exit }' README.md) if [ -z "$claimed" ]; then printf 'README.md names no assertion count for %s: nothing to verify (exit 2)\n' "$gate" >&2 return 2 fi if [ -z "${GATE_TOTALS_DIR:-}" ]; then printf 'GATE_TOTALS_DIR is not set, so %s recorded no total this run: cannot verify (exit 2)\n' "$gate" >&2 return 2 fi local measured_name measured_name=$(printf '%s' "$gate" | tr '/' '_') local measured_file="$GATE_TOTALS_DIR/$measured_name.txt" if [ ! -f "$measured_file" ]; then printf '%s did not record a total in %s: cannot verify (exit 2)\n' "$gate" "$GATE_TOTALS_DIR" >&2 return 2 fi local measured failed read -r _ measured failed < "$measured_file" printf 'README claims %s assertions for %s; the gate printed %s (%s failed)\n' \ "$claimed" "$gate" "$measured" "$failed" if [ "$claimed" != "$measured" ]; then printf 'the README is stale by %s assertions. Either the gate changed and the README did not, or the README was never measured.\n' \ "$((measured - claimed))" >&2 return 1 fi } case "${1:-all}" in restore) restore ;; build) build ;; unit) unit ;; integration) integration ;; smoke) smoke ;; schema-script) schema_script ;; image) image ;; scratch-image) scratch_image ;; compose-smoke) compose_smoke ;; scratch-smoke) scratch_smoke ;; console) console ;; lint) lint ;; readme-counts) readme_counts "${2:-}" ;; all) # Set here rather than requiring the caller: the totals only mean something # if the gates that produced them and the check that reads them ran in the # same invocation. export GATE_TOTALS_DIR="${GATE_TOTALS_DIR:-$(mktemp -d)}" restore; build; unit; integration; schema-script; smoke; image; compose_smoke # The scratch image and the same 379 assertions against it. It is in `all` # because it is the shipped shape, not an experiment: the only difference a # caller can see is what is under the program. scratch_image; scratch_smoke # The two gates `all` actually ran. console is not among them, so its README # number is verified by the console job (or by an operator who ran it and # then calls readme-counts with the same GATE_TOTALS_DIR). readme_counts scripts/smoke.sh readme_counts scripts/compose-smoke.sh printf '\nall gates passed\n' printf 'NOT included above: console (needs a real Chromium; exits 2 without one),\n' printf 'and lint (needs actionlint and shellcheck; exits 2 without them).\n' printf 'If the console changed, run: %s console\n' "$0" printf 'If a workflow or script changed, run: %s lint\n' "$0" ;; *) printf 'usage: %s restore|build|unit|integration|schema-script|smoke|image|compose-smoke|console|lint|readme-counts|all\n' "$0" >&2 exit 2 ;; esac