From 9be1348cd9c096436255a7e64751a3623f6452d2 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 17 Nov 2026 16:37:54 +0000 Subject: [PATCH 01/11] feat: stacks, the container stack, and quay.io/crunchtools/sdlc The planner detects a repo's stacks from its files (pyproject.toml python, Containerfile/Dockerfile container, package.json node, a bootc base bootc, nothing docs) and runs each stack's checks plus the common ones. A repo may add or drop a stack in [tool.sdlc], never name a command; a Python repo that names neither key keeps the python stack alone, byte for byte. The container stack: containerfile-lint (hadolint) in push and ci, and container-build, container-smoke (a new host-up launcher mode: exits 0 or still running after N seconds) and container-scan (grype over the built image's rootfs via podman --mount type=image) in ci. sdlc.toml configures a repo without a pyproject.toml. container.yml copies the tested digest to quay.io/crunchtools/sdlc and promotes the same tags on both names. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01R7Y1jckWHhAgXg2WBwkWbb --- .github/workflows/container.yml | 46 +++- CHANGELOG.md | 33 +++ Containerfile | 25 +- README.md | 52 ++++ bin/sdlc | 55 +++- sdlc | 468 ++++++++++++++++++++++++++++++-- tests/contract.sh | 45 ++- tests/test_host_contract.sh | 41 +++ tests/test_plan.py | 209 +++++++++++++- 9 files changed, 920 insertions(+), 54 deletions(-) diff --git a/.github/workflows/container.yml b/.github/workflows/container.yml index 43f57c7..15f81d3 100644 --- a/.github/workflows/container.yml +++ b/.github/workflows/container.yml @@ -1,4 +1,5 @@ -# Build, test and publish quay.io/crunchtools/python-sdlc. +# Build, test and publish quay.io/crunchtools/python-sdlc, and the same digest +# as quay.io/crunchtools/sdlc, the fleet-wide name it is becoming. # # The shape is the Container Image profile's, and it is the same shape trentina # arrived at the hard way in #689: the build pushes ONLY the commit SHA tag, the @@ -54,6 +55,11 @@ permissions: {} env: QUAY: quay.io/crunchtools/python-sdlc + # The fleet-wide name for the same image: one digest, two repositories, the + # same tags on both, until every consumer has moved and python-sdlc retires. + # It is never built or pushed separately -- it is a COPY of the digest pushed + # above, so the two names cannot disagree about what they hold. + SDLC: quay.io/crunchtools/sdlc jobs: build: @@ -139,9 +145,15 @@ jobs: podman push "localhost/python-sdlc:$GITHUB_SHA" "$QUAY:$GITHUB_SHA" digest=$(skopeo inspect --format '{{.Digest}}' "docker://$QUAY:$GITHUB_SHA") [ -n "$digest" ] || { echo "the registry reported no digest" >&2; exit 1; } + # The fleet name gets the SAME manifest, copied rather than pushed a + # second time: a second podman push recompresses the layers and can + # produce a different digest, and then the two names hold two images. + skopeo copy --all --preserve-digests "docker://$QUAY@$digest" "docker://$SDLC:$GITHUB_SHA" + got=$(skopeo inspect --format '{{.Digest}}' "docker://$SDLC:$GITHUB_SHA") + [ "$got" = "$digest" ] || { echo "$SDLC:$GITHUB_SHA is $got, not $digest" >&2; exit 1; } echo "digest=$digest" >> "$GITHUB_OUTPUT" echo "ref=$QUAY@$digest" >> "$GITHUB_OUTPUT" - echo "pushed $QUAY@$digest" + echo "pushed $QUAY@$digest and $SDLC@$digest" # The tag path, in place of everything above. Nothing is built and nothing # is pushed: this reads the digest the main-branch run of this same commit @@ -262,22 +274,30 @@ jobs: # RT#1550): --preserve-digests refuses a copy that would change the # manifest, and the read-back fails the job if the tag resolves to # anything but the digest that was tested. + # + # Every tag moves on BOTH names, python-sdlc and sdlc, to the same + # digest, and the guard reads both: either one ahead of this commit + # refuses the whole promotion rather than leaving them split. want=${REF#*@} promote() { - skopeo copy --all --preserve-digests "docker://$REF" "docker://$QUAY:$1" - got=$(skopeo inspect --format '{{.Digest}}' "docker://$QUAY:$1") - [ "$got" = "$want" ] || { echo ":$1 resolves to $got, not the tested $want" >&2; exit 1; } + for repo in "$QUAY" "$SDLC"; do + skopeo copy --all --preserve-digests "docker://$REF" "docker://$repo:$1" + got=$(skopeo inspect --format '{{.Digest}}' "docker://$repo:$1") + [ "$got" = "$want" ] || { echo "$repo:$1 resolves to $got, not the tested $want" >&2; exit 1; } + done } if [ -n "$floating" ]; then - current=$(skopeo inspect --format \ - '{{ index .Labels "org.opencontainers.image.revision" }}' \ - "docker://$QUAY:$floating" 2>/dev/null || true) - if [ -n "${current:-}" ] && [ "$current" != "$GITHUB_SHA" ]; then - if git merge-base --is-ancestor "$GITHUB_SHA" "$current" 2>/dev/null; then - echo "refusing: :$floating is $current, which already contains $GITHUB_SHA" >&2 - exit 1 + for repo in "$QUAY" "$SDLC"; do + current=$(skopeo inspect --format \ + '{{ index .Labels "org.opencontainers.image.revision" }}' \ + "docker://$repo:$floating" 2>/dev/null || true) + if [ -n "${current:-}" ] && [ "$current" != "$GITHUB_SHA" ]; then + if git merge-base --is-ancestor "$GITHUB_SHA" "$current" 2>/dev/null; then + echo "refusing: $repo:$floating is $current, which already contains $GITHUB_SHA" >&2 + exit 1 + fi fi - fi + done promote "$floating" fi case "$GITHUB_REF" in diff --git a/CHANGELOG.md b/CHANGELOG.md index 4de0d94..bab077c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,39 @@ All notable changes to this project are documented here, following ## [Unreleased] +### Added + +- **Stacks.** One image for every kind of repo: the planner detects which + stacks a repo has from its files -- `pyproject.toml` is python, a root + `Containerfile` or `Dockerfile` is container, `package.json` is node, a + `bootc` base image is bootc, nothing is docs -- and runs each stack's checks + plus the ones every repo gets (Gourmand, Gatehouse, the constitution + validator, the hook check). `[tool.sdlc] stacks` adds a stack and + `skip-stacks` drops one; neither can name a command. `sdlc stacks` shows what + was detected and what runs. node, bootc and docs are named but not yet + checked, so a repo that has one is refused (exit 2) until it skips it, rather + than going green on checks that never ran. +- **A Python repo that names no stacks runs exactly what it ran before**, with + or without a Containerfile. Proved by planning every public consumer's + `commit`, `push`, `ci` and `tiers` with v0.7.4 and with this image and + diffing the output. +- **The container stack**: `containerfile-lint` (hadolint 2.14.0, in push and + ci) and, in ci only, `container-build`, `container-smoke` (the built image + either exits 0 or is still running after `seconds`, default 10, with no + network and no capabilities) and `container-scan` (grype 0.121.0 over the + built image's filesystem, failing on a fixable Critical by default; the + repo's `.grype.yaml` applies). +- `sdlc.toml`: the same `[tool.sdlc]` tables, for a repo with no + `pyproject.toml`. Refused beside one. +- The image is also published as `quay.io/crunchtools/sdlc`: the same digest + copied, never rebuilt, with the same tags as `python-sdlc`. + +### Fixed + +- `bin/sdlc`'s `run_host` kept its exit status and start time in `_rc` and + `_t0`, which `run_serial` also uses, so a host step could leak its own status + or shorten its check's recorded duration. It has its own names now. + ## [0.7.5] - 2026-11-17 ### Changed diff --git a/Containerfile b/Containerfile index 914fd4f..8cfd325 100644 --- a/Containerfile +++ b/Containerfile @@ -18,11 +18,16 @@ ARG GOURMAND_VERSION=1.0.0 ARG GATEHOUSE_VERSION=0.29.1 ARG UV_VERSION=0.13.7 +ARG HADOLINT_VERSION=2.14.0 +ARG GRYPE_VERSION=0.121.0 FROM quay.io/crunchtools/gourmand:${GOURMAND_VERSION} AS gourmand FROM quay.io/crunchtools/gatehouse:${GATEHOUSE_VERSION} AS gatehouse # Distroless, binary only -- the documented way to get a pinned uv. FROM ghcr.io/astral-sh/uv:${UV_VERSION} AS uv +# The container stack's two tools, each a static binary in a scratch image. +FROM ghcr.io/hadolint/hadolint:v${HADOLINT_VERSION} AS hadolint +FROM docker.io/anchore/grype:v${GRYPE_VERSION} AS grype FROM quay.io/hummingbird/python:3.14-builder @@ -30,6 +35,8 @@ FROM quay.io/hummingbird/python:3.14-builder ARG GOURMAND_VERSION=1.0.0 ARG GATEHOUSE_VERSION=0.29.1 ARG UV_VERSION=0.13.7 +ARG HADOLINT_VERSION=2.14.0 +ARG GRYPE_VERSION=0.121.0 ARG RUFF_VERSION=0.17.1 ARG SEMGREP_VERSION=1.177.0 ARG PIP_AUDIT_VERSION=2.9.0 @@ -40,9 +47,9 @@ ARG CODEQL_BUNDLE=codeql-bundle-v2.27.4 USER root LABEL maintainer="fatherlinux " -LABEL description="Every CrunchTools Python SDLC check in one image: ruff, mypy, pytest, Gourmand, Gatehouse, Semgrep, CodeQL, pip-audit and the constitution validator." +LABEL description="Every CrunchTools SDLC check in one image: ruff, mypy, pytest, Gourmand, Gatehouse, Semgrep, CodeQL, pip-audit, hadolint, grype and the constitution validator." LABEL org.opencontainers.image.source="https://github.com/crunchtools/python-sdlc" -LABEL org.opencontainers.image.description="Every CrunchTools Python SDLC check in one image." +LABEL org.opencontainers.image.description="Every CrunchTools SDLC check in one image, for Python and container repos." LABEL org.opencontainers.image.licenses="AGPL-3.0-or-later" LABEL version="0.7.5" @@ -55,6 +62,8 @@ LABEL com.crunchtools.sdlc.semgrep="${SEMGREP_VERSION}" LABEL com.crunchtools.sdlc.pip-audit="${PIP_AUDIT_VERSION}" LABEL com.crunchtools.sdlc.codeql="${CODEQL_BUNDLE}" LABEL com.crunchtools.sdlc.uv="${UV_VERSION}" +LABEL com.crunchtools.sdlc.hadolint="${HADOLINT_VERSION}" +LABEL com.crunchtools.sdlc.grype="${GRYPE_VERSION}" # The commit and build time. NOT optional, and the reason is a trap trentina hit # in #689: its promote job refuses a release unless it can read @@ -77,7 +86,9 @@ ENV SDLC_GOURMAND_VERSION=${GOURMAND_VERSION} \ SDLC_SEMGREP_VERSION=${SEMGREP_VERSION} \ SDLC_PIP_AUDIT_VERSION=${PIP_AUDIT_VERSION} \ SDLC_CODEQL_BUNDLE=${CODEQL_BUNDLE} \ - SDLC_UV_VERSION=${UV_VERSION} + SDLC_UV_VERSION=${UV_VERSION} \ + SDLC_HADOLINT_VERSION=${HADOLINT_VERSION} \ + SDLC_GRYPE_VERSION=${GRYPE_VERSION} # git-core, not git: no perl, no tk, and nothing here runs `git send-email`. # tar and zstd are for the CodeQL bundle below. @@ -133,6 +144,14 @@ RUN uv venv /opt/constitution-venv \ # so it is portable out of its image. Verified before relying on it. COPY --from=gourmand /usr/local/sbin/gourmand /usr/local/bin/gourmand +# The container stack. Both are static Go/Haskell binaries with no runtime, so +# they copy out of their scratch images as Gourmand does. hadolint lints a +# Containerfile; grype scans a built image's filesystem, which the launcher +# mounts read-only with `podman run --mount type=image`, so neither needs a +# container engine in here. +COPY --from=hadolint /bin/hadolint /usr/local/bin/hadolint +COPY --from=grype /grype /usr/local/bin/grype + # Gatehouse is a Python app in a self-contained venv. Copying it works ONLY # because that image and this one are both Hummingbird with python3.14 at the # same path -- its pyvenv.cfg records `home = /usr/sbin`. A different base would diff --git a/README.md b/README.md index e72a7c5..f8d623e 100644 --- a/README.md +++ b/README.md @@ -96,6 +96,53 @@ share. The constitution validator, Gourmand, Gatehouse, CodeQL and `ruff check .` stay repo-wide, and ruff's own config is hierarchical, so a nested `pyproject.toml` still applies to the files under it. +## Stacks + +One image serves every kind of repo. A **stack** is the set of checks that +only mean something for one kind of repo, and which stacks a repo has is read +off its files: + +| File at the root | Stack | Status | +|---|---|---| +| `pyproject.toml` | python | checked | +| `Containerfile` or `Dockerfile` | container | checked | +| `package.json` | node | not yet | +| a Containerfile whose `FROM` is a bootc image | bootc | not yet | +| none of these | docs | not yet | + +Gourmand, Gatehouse, the constitution validator and `hooks-installed` belong to +no stack and run everywhere. A repo can add or drop a stack, and nothing more: + +```toml +[tool.sdlc] +stacks = ["container"] +skip-stacks = ["node"] +``` + +**A Python repo that names neither key runs the python stack alone**, exactly +as python-sdlc always has, whatever else is in it. Turning the container stack +on under 36 pinned repos is its own decision, not a side effect of a bump. +`sdlc stacks` shows what was detected and what runs. + +A stack this image names but cannot check yet is refused with exit 2, not left +out: a node-only repo judged by the constitution alone would go green on checks +that never ran. `skip-stacks` is how a repo says it accepts that. + +A repo with no `pyproject.toml` writes the same `[tool.sdlc]` tables in +`sdlc.toml`. A repo with one configures sdlc there, and `sdlc.toml` beside it is +refused. + +### The container stack + +| Check | Command | What it does | +|---|---|---| +| `containerfile-lint` | push, ci | hadolint over the Containerfile; the repo's `.hadolint.yaml` applies, and without one the threshold is `warning` | +| `container-build` | ci | `podman build`, at the tag the next two read | +| `container-smoke` | ci | runs the built image with no network and no capabilities: exiting 0, or still running after `seconds`, passes | +| `container-scan` | ci | grype over the built image's filesystem, mounted read-only by podman; fails on a fixable Critical by default, and the repo's `.grype.yaml` applies | + +Build, smoke and scan are one lane, beside everything else. + ## What a repo gets with no config at all | Command | Runs | @@ -244,6 +291,10 @@ tests = ["tests/image/"] | `codeql` | `config` (default `.github/codeql/codeql-config.yml`), `baseline` (default `.sdlc/codeql-baseline.json`) | | `constitution` | `manifest` (default `.specify/memory/constitution.md`) | | `ruff-check`, `ruff-format`, `lint`, `mypy`, `gourmand`, `gatehouse`, `pip-audit`, `hooks-installed` | none | +| `containerfile-lint` | `containerfiles` (default: the root Containerfile or Dockerfile), `threshold` | +| `container-build` | the `image-build` kind's settings; `containerfile` defaults to the root one | +| `container-smoke` | `seconds` (default 10), `args`, `env`, `network` (default `false`), `image` | +| `container-scan` | `fail_on` (default `critical`), `only_fixed` (default `true`), `image` | ### Kinds, for a repo's own checks @@ -309,6 +360,7 @@ sees it. `sdlc codeql-baseline` writes it from the current findings. See | ruff, semgrep, pip-audit | this image | | Gourmand, Gatehouse | this image, copied from their official images | | CodeQL | this image (the Python-only bundle, 110 MB rather than 1.75 GB) | +| hadolint, grype | this image, copied from their official images | | constitution validator | the repo — cloned at the release its own manifest names | | mypy, pytest | the repo's `uv.lock`, because they load the project's own plugins | diff --git a/bin/sdlc b/bin/sdlc index 42bb5e8..21c6cc7 100755 --- a/bin/sdlc +++ b/bin/sdlc @@ -278,6 +278,7 @@ run_in_image() { -e PYTHONPYCACHEPREFIX=/tmp/pycache \ -e SEMGREP_SETTINGS_FILE=/tmp/semgrep.yml \ -e "SDLC_PROJECT=$PROJECT" \ + -e "SDLC_IMAGE=$IMAGE" \ -e "SDLC_JOBS=$GATE_CPUS" \ -e "SDLC_MEMORY=$GATE_MEMORY" \ -e "SDLC_DEFAULT_BRANCH=$(default_branch 2>/dev/null || :)" \ @@ -454,6 +455,8 @@ record_finish() { # worst status # image-ro | image-rw `sdlc ` in the image, the tree ro or rw # local a check this file answers itself # host a podman argv, words joined by the unit separator +# host-up seconds, then a host argv: a run still going when +# they are up passes (see run_host) # # A host line that does not start with `podman build` or `podman run` is # refused: the host contract is held here, by the launcher, not trusted to @@ -464,7 +467,12 @@ record_finish() { # worst status TAB=$(printf '\t') US=$(printf '\037') -run_host() { +run_host() { # argv [seconds] -> the step's verdict + # With seconds, the step is a `podman run` that may be a service: it is given + # that long under --timeout, and still running when it is up is a pass, as + # is exiting 0. podman reports its own timeout kill as 255, which a crashing + # program can also return, so the clock decides which one it was. + _up=${2:-} _ifs=$IFS IFS=$US # shellcheck disable=SC2086 # splitting on the separator is the point @@ -479,19 +487,40 @@ run_host() { --memory "$GATE_MEMORY" "$@" ;; run) set -- --events-backend=none run --log-driver=none \ --cap-drop=all --security-opt=no-new-privileges \ - --cpus "$GATE_CPUS" --memory "$GATE_MEMORY" --cpu-shares 256 "$@" ;; + --cpus "$GATE_CPUS" --memory "$GATE_MEMORY" --cpu-shares 256 \ + ${_up:+--timeout "$_up"} "$@" ;; *) die "a host step may only build or run; refused: podman $_sub" ;; esac - _rc=0 - podman "$@" &2 + return 2 ;; + esac + if [ "$_hrc" -eq 255 ] && [ $(( $(now_s) - _ht0 )) -ge "$_up" ]; then + printf 'still running after %ss: the image starts and stays up\n' "$_up" + return 0 + fi + printf '%s: the container exited %s within %ss of starting\n' "$ME" "$_hrc" "$_up" >&2 + return 1 + fi # A failed build is a finding: the code does not produce an image. A run # that podman could not start (125-127) never ran, so it is a 2, and so is a # pytest status above 1 (interrupted, usage error, internal error, no tests). - case "$_sub:$_rc" in + case "$_sub:$_hrc" in *:0) return 0 ;; build:*) return 1 ;; run:1) return 1 ;; - *) printf '%s: podman %s exited %s; nothing was judged\n' "$ME" "$_sub" "$_rc" >&2; return 2 ;; + *) printf '%s: podman %s exited %s; nothing was judged\n' "$ME" "$_sub" "$_hrc" >&2; return 2 ;; esac } @@ -520,6 +549,10 @@ _run_step() { *) die "the image asked for an unknown local check: $2" ;; esac ;; host) run_host "$2" ;; + host-up) + _secs=${2%%"$US"*} + case "$_secs" in ''|*[!0-9]*) die "a host-up step must start with a number of seconds" ;; esac + run_host "${2#*"$US"}" "$_secs" ;; *) die "the image asked for an unknown step mode: $1" ;; esac } @@ -951,7 +984,7 @@ case "${1:---help}" in STATE_HINT=$(git rev-parse --git-path sdlc 2>/dev/null) || STATE_HINT= [ -n "$STATE_HINT" ] || STATE_HINT='/sdlc' cat < | tiers | list | versions | codeql-baseline +usage: $ME commit | push | ci | | tiers | stacks | list | versions | codeql-baseline commit the pre-commit hook: seconds, offline, free push the pre-push hook: every verdict, no container builds @@ -961,12 +994,14 @@ usage: $ME commit | push | ci | | tiers | list | versions | codeql-basel -- one check, with arguments for the tool itself, as: $ME pytest -- tests/test_x.py::test_y -n0 tiers what each command runs in this repo + stacks which stacks this repo has: python, container, ... list the built-in checks and the kinds versions the exact tool versions the image carries codeql-baseline accept every current CodeQL finding into the baseline -Configure each command in pyproject.toml: [tool.sdlc.commit], -[tool.sdlc.push], [tool.sdlc.ci]. Skip one check once: SDLC_SKIP=name. +Configure each command in pyproject.toml, or in sdlc.toml in a repo without +one: [tool.sdlc.commit], [tool.sdlc.push], [tool.sdlc.ci]. Skip one check +once: SDLC_SKIP=name. Exit 2 means a check COULD NOT RUN and is never to be worked around. A push prints DEFERRED TO CI, and goes through, only when CodeQL itself broke and the repo's \`sdlc ci\` job will judge the same commit. @@ -987,7 +1022,7 @@ which is only what happened on this machine last time. Image: $IMAGE EOF ;; - tiers|list|versions) run_in_image ro nostdin "$IMAGE" "$1" ;; + tiers|stacks|list|versions) run_in_image ro nostdin "$IMAGE" "$1" ;; codeql-baseline) run_in_image rw nostdin "$IMAGE" "$1" ;; *) _target=$1 diff --git a/sdlc b/sdlc index 660beac..a17170b 100755 --- a/sdlc +++ b/sdlc @@ -63,6 +63,8 @@ BAKED = { "pip-audit": os.environ.get("SDLC_PIP_AUDIT_VERSION", "unknown"), "codeql": os.environ.get("SDLC_CODEQL_BUNDLE", "unknown"), "uv": os.environ.get("SDLC_UV_VERSION", "unknown"), + "hadolint": os.environ.get("SDLC_HADOLINT_VERSION", "unknown"), + "grype": os.environ.get("SDLC_GRYPE_VERSION", "unknown"), } @@ -73,6 +75,8 @@ VERSION_PROBE = { "pip-audit": ["pip-audit", "--version"], "codeql": ["codeql", "version", "--format=terse"], "uv": ["uv", "--version"], + "hadolint": ["hadolint", "--version"], + "grype": ["grype", "version"], # gatehouse has no --version flag; its usage line is all it offers. "gatehouse": None, } @@ -145,6 +149,31 @@ def pyproject() -> dict: cannot(f"cannot read {path}: {exc}") +# A repo with no pyproject.toml writes the same `[tool.sdlc]` tables in +# `sdlc.toml` instead, so moving a block between the two is copy and paste and +# every message that names `[tool.sdlc.ci]` names what the reader wrote. Only +# one of them, ever: a repo with a pyproject.toml configures sdlc there, because +# two files that can both say what CI runs are two answers to one question. +SDLC_TOML = "sdlc.toml" + + +def config_document() -> dict: + """Where `[tool.sdlc]` is read from: pyproject.toml, or sdlc.toml without one.""" + doc = pyproject() + path = REPO / SDLC_TOML + if not path.is_file(): + return doc + if (REPO / "pyproject.toml").is_file(): + cannot( + f"{SDLC_TOML} and pyproject.toml both exist; a repo with a pyproject.toml " + f"configures sdlc in its [tool.sdlc] tables, so remove {SDLC_TOML}" + ) + try: + return tomllib.loads(path.read_text(encoding="utf-8")) + except (OSError, tomllib.TOMLDecodeError) as exc: + cannot(f"cannot read {path}: {exc}") + + # ------------------------------------------------------------ the three commands # # One rule for all three config blocks, so what you read is what runs: @@ -184,6 +213,7 @@ DEFAULTS = { # (python-sdlc#48). First, because it is the cheapest thing here to fail. "push": [ "lint", + "containerfile-lint", "mypy", "pytest", "gourmand", @@ -195,6 +225,132 @@ DEFAULTS = { ], } +# What `sdlc ci` adds to push's checks by default. Builds go here and nowhere +# earlier: push never builds a container. +CI_DEFAULTS = ["container-build", "container-smoke", "container-scan"] + +# ------------------------------------------------------------------ the stacks +# +# One image, several kinds of repo. A STACK is the set of built-in checks that +# only mean something for one kind: ruff for Python, hadolint and a build for a +# Containerfile. The defaults above are every stack's checks in the one order +# they run in; a repo runs the ones whose stack it has, plus the checks no +# stack owns -- Gourmand, Gatehouse, the constitution validator and the hook +# check -- which apply to any repo. +# +# Which stacks a repo has is DETECTED from its files, and its config block may +# add to that or take away from it, never name a command: +# +# [tool.sdlc] +# stacks = ["container"] add a stack the files do not show +# skip-stacks = ["node"] drop one they do +# +# A Python repo whose config says neither keeps exactly the python stack, as +# every python-sdlc release before this one ran it. That is not detection +# failing, it is a promise: 36 repos pin this tool and gate merges on `sdlc +# ci`, and most of them carry a Containerfile. Turning the container stack on +# under them is a decision with its own release note, not a side effect of an +# image bump. `sdlc stacks` shows what was detected either way. + +STACK_OF = { + "ruff-check": "python", + "ruff-format": "python", + "lint": "python", + "mypy": "python", + "pytest": "python", + "semgrep": "python", + "pip-audit": "python", + "codeql": "python", + "containerfile-lint": "container", + "container-build": "container", + "container-smoke": "container", + "container-scan": "container", +} + +# Stacks this image has checks for, and stacks it can name but not yet run. +STACKS = ("python", "container") +PLANNED_STACKS = ("node", "bootc", "docs") + +# What a Python repo that says nothing about stacks gets: the pre-stacks tool. +COMPAT_STACKS = ("python",) + +CONTAINERFILES = ("Containerfile", "Dockerfile") + + +def containerfile() -> str | None: + """The root Containerfile or Dockerfile, whichever exists, Containerfile first.""" + return next((f for f in CONTAINERFILES if (REPO / f).is_file()), None) + + +def is_bootc(path: str) -> bool: + """Does this Containerfile build a bootable host rather than a container? + + A bootc image starts systemd as PID 1 and is not meant to run as an app, so + a smoke run of it judges nothing. It gets its own stack instead. + """ + try: + text = (REPO / path).read_text(encoding="utf-8", errors="replace") + except OSError as exc: + cannot(f"cannot read {path} to tell a container from a bootc image: {exc}") + return any( + line.split()[0].upper() == "FROM" and "bootc" in line.lower() + for line in text.splitlines() + if line.strip() + ) + + +def detect_stacks() -> list[str]: + """The stacks this repo's files show, in STACKS-then-planned order.""" + found = [] + if (REPO / "pyproject.toml").is_file(): + found.append("python") + cf = containerfile() + if cf and not is_bootc(cf): + found.append("container") + if (REPO / "package.json").is_file(): + found.append("node") + if cf and is_bootc(cf): + found.append("bootc") + return found or ["docs"] + + +def stack_names(value: object, where: str) -> list[str]: + out = names(value, where) + unknown = sorted(set(out) - set(STACKS) - set(PLANNED_STACKS)) + if unknown: + cannot( + f"{where} names {', '.join(unknown)}; the stacks are " + f"{', '.join(STACKS + PLANNED_STACKS)}" + ) + return out + + +def active_stacks(cfg: dict) -> tuple[str, ...]: + """The stacks this repo runs: detected, plus `stacks`, minus `skip-stacks`.""" + add = stack_names(cfg.get("stacks"), "[tool.sdlc] stacks") + drop = stack_names(cfg.get("skip-stacks"), "[tool.sdlc] skip-stacks") + if "stacks" not in cfg and "skip-stacks" not in cfg and (REPO / "pyproject.toml").is_file(): + return COMPAT_STACKS + out = [s for s in detect_stacks() if s not in drop] + out += [s for s in add if s not in out and s not in drop] + # A stack this image cannot run yet is refused rather than left out: a repo + # whose only stack is node, judged by nothing but the constitution, would + # report green for checks that never existed. + planned = [s for s in out if s in PLANNED_STACKS] + if planned: + cannot( + f"this repo has the {', '.join(planned)} stack(s), which this image cannot " + f"check yet. To run the rest without them, say so in its config: " + f"[tool.sdlc] skip-stacks = {json.dumps(planned)}" + ) + return tuple(out) + + +def in_stacks(check: str, stacks: tuple[str, ...]) -> bool: + owner = STACK_OF.get(check) + return owner is None or owner in stacks + + # CI runs push's checks except these two, which it gets from their own # workflows: Gatehouse's posts PR comments and must never be a blocking check, # and the constitution requires Gourmand's CI job to be the shared reusable one. @@ -210,23 +366,35 @@ LOCAL_CHECKS = ("hooks-installed", "gatehouse") WRITES_TREE = ("ruff-check", "ruff-format", "gourmand") # Words that are commands of this script, so no check may be named one. -RESERVED = (*COMMANDS, "plan", "tiers", "list", "versions", "pytest-deps", "codeql-baseline") +RESERVED = ( + *COMMANDS, + "plan", + "tiers", + "list", + "versions", + "stacks", + "pytest-deps", + "codeql-baseline", + "scan-rootfs", +) def sdlc_config() -> dict: """`[tool.sdlc]`, checked for shape before anything trusts it.""" - cfg = pyproject().get("tool", {}).get("sdlc", {}) or {} - stray = sorted(set(cfg) - set(COMMANDS) - {"project"}) + cfg = config_document().get("tool", {}).get("sdlc", {}) or {} + stray = sorted(set(cfg) - set(COMMANDS) - {"project", "stacks", "skip-stacks"}) if stray: cannot( f"[tool.sdlc] has {', '.join(stray)}; the only keys are " - f"[tool.sdlc.commit], [tool.sdlc.push], [tool.sdlc.ci] and `project`" + f"[tool.sdlc.commit], [tool.sdlc.push], [tool.sdlc.ci], `project`, " + f"`stacks` and `skip-stacks`" ) project_dir() # validated here, so a bad path is caught on the next commit + stacks = active_stacks(cfg) # The WHOLE config is checked whichever command runs, so a typo in the ci # block is found on the next commit rather than in CI. for command in COMMANDS: - for check in resolve(cfg, command): + for check in resolve(cfg, command, stacks): kind_of(cfg, check) return cfg @@ -256,12 +424,17 @@ def skipped() -> set[str]: return {s.strip() for s in os.environ.get("SDLC_SKIP", "").split(",") if s.strip()} -def resolve(cfg: dict, command: str) -> list[str]: - """The checks `command` runs in this repo, in order.""" +def resolve(cfg: dict, command: str, stacks: tuple[str, ...] = COMPAT_STACKS) -> list[str]: + """The checks `command` runs in this repo, in order. + + `stacks` defaults to the python stack alone, which is what every repo ran + before stacks existed; the planner passes `active_stacks(cfg)`. + """ if command == "ci": - base = [c for c in resolve(cfg, "push") if c not in CI_EXCLUDES] + base = [c for c in resolve(cfg, "push", stacks) if c not in CI_EXCLUDES] + base += [c for c in CI_DEFAULTS if in_stacks(c, stacks)] else: - base = list(DEFAULTS[command]) + base = [c for c in DEFAULTS[command] if in_stacks(c, stacks)] own = block(cfg, command) drop = set(names(own.get("skip"), f"[tool.sdlc.{command}] skip")) if command == "ci": @@ -296,7 +469,7 @@ def resolve(cfg: dict, command: str) -> list[str]: return out -def push_skips(cfg: dict) -> list[str]: +def push_skips(cfg: dict, stacks: tuple[str, ...] = COMPAT_STACKS) -> list[str]: """The push verdicts ci does not run because [tool.sdlc.push] skips them. That skip is the only way a repo takes a verdict out of ci, and a verdict @@ -304,12 +477,16 @@ def push_skips(cfg: dict) -> list[str]: whether the repo ran lint or opted out of it (python-sdlc#50). """ drop = set(names(block(cfg, "push").get("skip"), "[tool.sdlc.push] skip")) - ci = resolve(cfg, "ci") - return [c for c in DEFAULTS["push"] if c in drop and c not in CI_EXCLUDES and c not in ci] + ci = resolve(cfg, "ci", stacks) + return [ + c + for c in DEFAULTS["push"] + if c in drop and c not in CI_EXCLUDES and c not in ci and in_stacks(c, stacks) + ] -def push_skip_line(cfg: dict) -> str | None: - gone = push_skips(cfg) +def push_skip_line(cfg: dict, stacks: tuple[str, ...] = COMPAT_STACKS) -> str | None: + gone = push_skips(cfg, stacks) return f"sdlc ci: not run, skipped in [tool.sdlc.push]: {', '.join(gone)}" if gone else None @@ -363,6 +540,9 @@ def kind_of(cfg: dict, name: str) -> str | None: # local bin/sdlc's own check named # host is a podman argv, its words joined by # the ASCII unit separator (0x1f) +# host-up the same, after a first word that is a number of +# seconds: a `podman run` that is still running when +# they are up is a pass (container-smoke) # # bin/sdlc refuses a host line that does not start with `podman`, so the host # contract is held by the launcher rather than trusted to the config. It splits @@ -372,6 +552,8 @@ def kind_of(cfg: dict, name: str) -> str | None: # shell. UNIT_SEPARATOR = "\x1f" +# Modes whose payload is a separator-joined argv, already checked word by word. +HOST_MODES = ("host", "host-up") FORBIDDEN_IN_PLAN = ("\t", "\n", "\r", UNIT_SEPARATOR) @@ -389,6 +571,8 @@ def plan_lines(cfg: dict, check: str) -> list[tuple[str, str, str]]: kind = kind_of(cfg, check) if kind is not None: return KINDS[kind](check, check_settings(cfg, check)) + if check in HOST_CHECKS: + return HOST_CHECKS[check](cfg, check, check_settings(cfg, check)) if check in LOCAL_CHECKS: return [(check, "local", check)] return [(check, "image-rw" if check in WRITES_TREE else "image-ro", check)] @@ -463,14 +647,13 @@ def lane_of(cfg: dict, checks: list[str]) -> dict[str, str]: lanes: dict[str, str] = {} by_tag: dict[str, str] = {} for check in checks: - if kind_of(cfg, check) != "image-build": - continue - tag = str(check_settings(cfg, check).get("tag", default_image_tag())) - lanes[check] = by_tag.setdefault(tag, check) + tag = builds_image(cfg, check) + if tag is not None: + lanes[check] = by_tag.setdefault(tag, check) for check in checks: - if kind_of(cfg, check) != "pytest-in-image": + image = runs_image(cfg, check) + if image is None: continue - image = str(check_settings(cfg, check).get("image", default_image_tag())) # No build of that image in this command: the repo is testing something # already built, so it owes nothing to another lane and gets its own. lanes[check] = by_tag.get(image, check) @@ -480,14 +663,31 @@ def lane_of(cfg: dict, checks: list[str]) -> dict[str, str]: return {check: lanes.get(check, MAIN_LANE) for check in checks} +def builds_image(cfg: dict, check: str) -> str | None: + """The tag this check builds, or None when it builds nothing.""" + if check == "container-build" or kind_of(cfg, check) == "image-build": + return str(check_settings(cfg, check).get("tag", default_image_tag())) + return None + + +def runs_image(cfg: dict, check: str) -> str | None: + """The image this check runs or reads, or None when it uses none.""" + if check in ("container-smoke", "container-scan"): + return container_image(cfg, check_settings(cfg, check)) + if kind_of(cfg, check) == "pytest-in-image": + return str(check_settings(cfg, check).get("image", default_image_tag())) + return None + + def t_plan(target: str) -> int: cfg = sdlc_config() - checks = resolve(cfg, target) if target in COMMANDS else [target] + stacks = active_stacks(cfg) + checks = resolve(cfg, target, stacks) if target in COMMANDS else [target] for check in checks: if check not in BUILTINS and kind_of(cfg, check) is None: cannot(f"unknown check: {check}") # stderr, because stdout is the plan the launcher parses. - line = push_skip_line(cfg) if target == "ci" else None + line = push_skip_line(cfg, stacks) if target == "ci" else None if line: print(line, file=sys.stderr) lanes = lane_of(cfg, checks) @@ -495,7 +695,7 @@ def t_plan(target: str) -> int: lane = plain(lanes[check], "a lane name") for name, mode, payload in plan_lines(cfg, check): plain(name, "a check name") - if mode != "host": + if mode not in HOST_MODES: plain(payload, name) print(f"{name}\t{lane}\t{mode}\t{payload}") return CLEAN @@ -503,8 +703,9 @@ def t_plan(target: str) -> int: def t_tiers() -> int: cfg = sdlc_config() + stacks = active_stacks(cfg) for command in COMMANDS: - checks = resolve(cfg, command) + checks = resolve(cfg, command, stacks) lanes = lane_of(cfg, checks) parallel = len(set(lanes.values())) > 1 print(f"sdlc {command}:") @@ -516,7 +717,7 @@ def t_tiers() -> int: print(f" {check}{note}") if parallel: print(f" {len(set(lanes.values()))} lanes, run at the same time") - line = push_skip_line(cfg) if command == "ci" else None + line = push_skip_line(cfg, stacks) if command == "ci" else None if line: print(f" {line}") if skipped(): @@ -669,6 +870,197 @@ KINDS = { "pytest-in-image": kind_pytest_in_image, } + +# ---------------------------------------------------------- the container stack +# +# Three of its four checks are host steps, because they need podman and this +# container has none: build the repo's image, run it, and scan what was built. +# They are built-in CHECKS rather than kinds a repo writes, so a repo with a +# Containerfile gets them by having one. Their tables take the settings the +# matching kind takes. The fourth, `containerfile-lint`, reads the tree and runs +# here. + +HADOLINT_CONFIGS = (".hadolint.yaml", ".hadolint.yml") +HADOLINT_THRESHOLDS = ("error", "warning", "info", "style", "ignore", "none") +GRYPE_SEVERITIES = ("negligible", "low", "medium", "high", "critical") +SMOKE_SECONDS = 10 +SMOKE_SECONDS_MAX = 600 + + +def container_image(cfg: dict, settings: dict) -> str: + """What smoke and scan judge: their own `image`, or what container-build tags.""" + built = check_settings(cfg, "container-build").get("tag", default_image_tag()) + return str(settings.get("image", built)) + + +def check_container_build(cfg: dict, name: str, s: dict) -> list[tuple[str, str, str]]: + """`podman build` of the root Containerfile, at the tag smoke and scan read.""" + if "containerfile" not in s and containerfile() is None: + cannot( + f"{name}: there is no Containerfile or Dockerfile at the repo root; " + f"name one with containerfile in its [tool.sdlc.ci.{name}] table" + ) + return kind_image_build(name, {"containerfile": containerfile(), **s}) + + +def check_container_smoke(cfg: dict, name: str, s: dict) -> list[tuple[str, str, str]]: + """Start the built image and see that it either finishes cleanly or stays up. + + One rule covers both shapes the fleet builds. A CLI image runs and exits: + 0 is a pass, anything else is a finding. A service never exits: still + running when `seconds` is up is a pass. What fails is the image that dies on + start, which is the failure a green build hides -- a missing shared library, + an entrypoint that is not there, a config it cannot read. + + No network by default, and the launcher drops every capability, so an image + that needs either says so here rather than being judged on a host that + happens to provide it. + """ + seconds = s.get("seconds", SMOKE_SECONDS) + if ( + not isinstance(seconds, int) + or isinstance(seconds, bool) + or not 1 <= seconds <= SMOKE_SECONDS_MAX + ): + cannot(f"{name}: seconds must be a whole number from 1 to {SMOKE_SECONDS_MAX}") + argv = ["podman", "run", "--rm", "--pull=never"] + if not s.get("network", False): + argv += ["--network", "none"] + for key, value in table(s, "env").items(): + argv += ["-e", f"{key}={token(value)}"] + argv += [container_image(cfg, s), *names(s.get("args"), f"{name}.args")] + payload = UNIT_SEPARATOR.join([str(seconds), *(plain(a, name) for a in argv)]) + return [(name, "host-up", payload)] + + +def check_container_scan(cfg: dict, name: str, s: dict) -> list[tuple[str, str, str]]: + """Scan the built image's filesystem with grype, in this image. + + podman mounts the built image read-only at /scan -- no export, no daemon, + no registry -- and `sdlc scan-rootfs` scans that. The repo is mounted too, + read-only, so its own `.grype.yaml` ignore rules apply: what a repo has + accepted is the repo's to say, in a file a reviewer sees. + + The default fails on a Critical that has a fix, which is the line the + fleet's scheduled CVE check pages on. A finding with no fix is reported and + does not fail: nothing the repo can do in this pull request changes it. + """ + host = os.environ.get("SDLC_HOST_REPO") + volume = os.environ.get("SDLC_CACHE_VOLUME") + gate = os.environ.get("SDLC_IMAGE") + if not host or not volume or not gate: + cannot(f"{name}: run through bin/sdlc, which supplies the host paths and the image") + fail_on = str(s.get("fail_on", "critical")) + if fail_on not in GRYPE_SEVERITIES: + cannot(f"{name}: fail_on must be one of {', '.join(GRYPE_SEVERITIES)}") + only_fixed = s.get("only_fixed", True) + if not isinstance(only_fixed, bool): + cannot(f"{name}: only_fixed must be true or false") + image = container_image(cfg, s) + argv = [ + "podman", + "run", + "--rm", + "--pull=never", + "--mount", + f"type=image,source={image},destination=/scan,rw=false", + "-v", + f"{volume}:/cache", + "-v", + f"{host}:/repo:ro,z", + "-w", + "/repo", + "--tmpfs", + "/tmp:rw,exec,size=2g,mode=1777", + "-e", + "HOME=/tmp", + "-e", + f"SDLC_PROJECT={project_name()}", + "-e", + f"SDLC_SCAN_FAIL_ON={fail_on}", + "-e", + f"SDLC_SCAN_ONLY_FIXED={'1' if only_fixed else '0'}", + gate, + "scan-rootfs", + ] + return [host_step(name, argv)] + + +HOST_CHECKS = { + "container-build": check_container_build, + "container-smoke": check_container_smoke, + "container-scan": check_container_scan, +} + + +def t_containerfile_lint(s: dict) -> int: + """hadolint over the Containerfile, with the repo's own `.hadolint.yaml`. + + Without a config the threshold is `warning`: hadolint's own default fails + on style notes, and a gate that fails on taste is a gate people skip. A + repo with a config file sets its own threshold there, and `threshold` in + this check's table beats both. + """ + files = names(s.get("containerfiles"), "containerfile-lint.containerfiles") + if not files: + found = containerfile() + if found is None: + cannot("there is no Containerfile or Dockerfile at the repo root to lint") + files = [found] + files = [in_repo("containerfile-lint", "containerfiles", f) for f in files] + cmd = ["hadolint", "--no-color"] + threshold = s.get("threshold") + if threshold is None and not any((REPO / c).is_file() for c in HADOLINT_CONFIGS): + threshold = "warning" + if threshold is not None: + if threshold not in HADOLINT_THRESHOLDS: + cannot(f"containerfile-lint: threshold must be one of {', '.join(HADOLINT_THRESHOLDS)}") + cmd += ["--failure-threshold", str(threshold)] + step(f"hadolint {' '.join(files)}") + rc = run([*cmd, *files])[0] + # hadolint has two answers: 0 clean, 1 found something at the threshold. + # Anything else is the tool, not the Containerfile. + if rc not in (CLEAN, FOUND): + cannot(f"hadolint could not run (exit {rc})") + return rc + + +GRYPE_DB = CACHE / "grype" + + +def t_scan_rootfs() -> int: + """The in-image half of container-scan: grype over the image podman mounted. + + grype answers 0 clean, 2 above `--fail-on`, 1 when it could not scan -- + which is the reverse of this file's 1 and 2, so it is translated here and + nowhere else. Its vulnerability database lives in the cache volume and is + refreshed when stale; one lock keeps two gates from rewriting it at once. + """ + if not Path("/scan").is_dir(): + cannot("nothing is mounted at /scan; container-scan runs this, not a person") + fail_on = os.environ.get("SDLC_SCAN_FAIL_ON", "critical") + if fail_on not in GRYPE_SEVERITIES: + cannot(f"SDLC_SCAN_FAIL_ON must be one of {', '.join(GRYPE_SEVERITIES)}") + cmd = ["grype", "dir:/scan", "--name", project_name(), "--fail-on", fail_on, "-o", "table"] + if os.environ.get("SDLC_SCAN_ONLY_FIXED", "1") == "1": + cmd.append("--only-fixed") + env = dict(os.environ) + env.update({"GRYPE_DB_CACHE_DIR": str(GRYPE_DB), "GRYPE_CHECK_FOR_APP_UPDATE": "false"}) + GRYPE_DB.mkdir(parents=True, exist_ok=True) + step(f"grype, failing on a {'fixable ' if '--only-fixed' in cmd else ''}{fail_on} or worse") + with (GRYPE_DB / ".lock").open("w") as lock: + fcntl.flock(lock, fcntl.LOCK_EX) + rc = run(cmd, env=env)[0] + if rc == 0: + return CLEAN + if rc == 2: # noqa: PLR2004 -- grype's own "above the threshold" + return FOUND + cannot( + f"grype could not scan the image (exit {rc}). Its vulnerability database is " + f"downloaded on first use, so a refused network is the usual cause." + ) + + # pytest and the plugins an in-image run needs. Pure Python, so one install # serves a target image on any Python the lock admits. PYTEST_PACKAGES = ("pytest", "pytest-asyncio", "pytest-xdist", "pytest-timeout") @@ -1646,8 +2038,13 @@ BUILTINS = { "pip-audit": t_pip_audit, "constitution": t_constitution, "codeql": t_codeql, + "containerfile-lint": t_containerfile_lint, # Answered by bin/sdlc on the host; listed so the name is known here. "hooks-installed": None, + # Planned as host steps, which bin/sdlc runs; HOST_CHECKS builds them. + "container-build": None, + "container-smoke": None, + "container-scan": None, } @@ -1664,10 +2061,31 @@ def t_versions() -> int: return CLEAN +def t_stacks() -> int: + """What the repo's files show, what its config says, and what runs.""" + cfg = config_document().get("tool", {}).get("sdlc", {}) or {} + print(f"detected: {', '.join(detect_stacks())}") + for key in ("stacks", "skip-stacks"): + if key in cfg: + print(f"{key}: {', '.join(stack_names(cfg.get(key), f'[tool.sdlc] {key}'))}") + active = active_stacks(cfg) + print(f"runs: {', '.join(active) or '(no stack: the common checks only)'}") + if active == COMPAT_STACKS and "stacks" not in cfg and "skip-stacks" not in cfg: + print( + "\nA Python repo that names no stacks runs the python stack alone, as " + "python-sdlc always has.\nTo run what was detected, add " + "[tool.sdlc] stacks = [...] with the ones you want." + ) + print(f"\nthis image checks: {', '.join(STACKS)}; not yet: {', '.join(PLANNED_STACKS)}") + return CLEAN + + TOOL_COMMANDS = { "tiers": t_tiers, "list": t_list, "versions": t_versions, + "stacks": t_stacks, + "scan-rootfs": t_scan_rootfs, "pytest-deps": t_pytest_deps, "codeql-baseline": t_codeql_baseline, } diff --git a/tests/contract.sh b/tests/contract.sh index 370388e..aae102d 100755 --- a/tests/contract.sh +++ b/tests/contract.sh @@ -65,6 +65,8 @@ for probe in \ "gourmand:gourmand --version" \ "codeql:codeql version --format=terse" \ "uv:uv --version" \ + "hadolint:hadolint --version" \ + "grype:grype version" \ "git:git --version" \ "jq:jq --version" \ "curl:curl --version" \ @@ -106,6 +108,11 @@ check_version semgrep "$(inside sh -c 'printf %s "$SDLC_SEMGREP_VERSION"')" \ check_version pip-audit "$(inside sh -c 'printf %s "$SDLC_PIP_AUDIT_VERSION"')" \ "$(inside sh -c 'pip-audit --version' 2>&1)" +check_version hadolint "$(inside sh -c 'printf %s "$SDLC_HADOLINT_VERSION"')" \ + "$(inside sh -c 'hadolint --version' 2>&1)" +check_version grype "$(inside sh -c 'printf %s "$SDLC_GRYPE_VERSION"')" \ + "$(inside sh -c 'grype version' 2>&1)" + # codeql's bundle name is codeql-bundle-vX.Y.Z and the CLI reports X.Y.Z. _bundle=$(inside sh -c 'printf %s "$SDLC_CODEQL_BUNDLE"') check_version codeql "${_bundle#codeql-bundle-v}" \ @@ -429,6 +436,40 @@ else bad "bin/sdlc inside a container exited $rc (124 means it hung)" fi +# ------------------------------------------------------- the container stack +# +# A repo with a Containerfile and no pyproject.toml is the container stack and +# nothing else: no ruff, no pytest, and CI builds, smokes and scans the image. +# Planned on the real image because detection reads the mounted tree. +CFIX=$(mktemp -d) +printf 'FROM quay.io/hummingbird/python:3.14\nUSER 65532\n' > "$CFIX/Containerfile" +plan=$(podman run --rm -v "$CFIX:/repo:ro,z" -w /repo --entrypoint="" \ + -e SDLC_HOST_REPO=/host -e SDLC_CACHE_VOLUME=v -e SDLC_IMAGE=gate \ + "$IMAGE" sdlc plan ci 2>&1 | cut -f1 | awk '!s[$0]++' | tr '\n' ' ') +want="containerfile-lint constitution container-build container-smoke container-scan " +if [ "$plan" = "$want" ]; then + ok "a Containerfile-only repo plans the container stack in ci" +else + bad "sdlc plan ci on a Containerfile-only repo gave '$plan', want '$want'" +fi +if out=$(podman run --rm -v "$CFIX:/repo:ro,z" -w /repo --entrypoint="" \ + "$IMAGE" sdlc containerfile-lint 2>&1); then + ok "containerfile-lint runs clean on a read-only fixture Containerfile" +else + bad "containerfile-lint failed on a clean fixture Containerfile" + printf '%s\n' "$out" | sed 's/^/ /' | tail -8 +fi +printf 'FROM quay.io/hummingbird/python\nRUN cd /tmp && ls\n' > "$CFIX/Containerfile" +rc=0 +podman run --rm -v "$CFIX:/repo:ro,z" -w /repo --entrypoint="" \ + "$IMAGE" sdlc containerfile-lint >/dev/null 2>&1 || rc=$? +if [ "$rc" -eq 1 ]; then + ok "containerfile-lint finds an untagged FROM and a cd (exit 1)" +else + bad "containerfile-lint on a bad Containerfile exited $rc, want 1" +fi +rm -rf "$CFIX" + # --------------------------------------- `sdlc versions` matches the build pins # # The version table is the image's central promise: a consuming repo pins this @@ -444,7 +485,9 @@ for row in \ "gourmand:SDLC_GOURMAND_VERSION" \ "gatehouse:SDLC_GATEHOUSE_VERSION" \ "codeql:SDLC_CODEQL_BUNDLE" \ - "uv:SDLC_UV_VERSION" + "uv:SDLC_UV_VERSION" \ + "hadolint:SDLC_HADOLINT_VERSION" \ + "grype:SDLC_GRYPE_VERSION" do tool=${row%%:*} var=${row#*:} diff --git a/tests/test_host_contract.sh b/tests/test_host_contract.sh index bc1886f..48d48ba 100755 --- a/tests/test_host_contract.sh +++ b/tests/test_host_contract.sh @@ -118,6 +118,15 @@ case " \$* " in *" plan lanes-budget "*) printf 'p${TAB}one${TAB}host${TAB}podman${US}build${US}-t${US}localhost/x:sdlc${US}.\n' printf 'q${TAB}two${TAB}image-ro${TAB}quiet-ok\n' ;; + *" plan up-"*) + _secs=1; _what=\$_last + case "\$_last" in up-build) _what=build ;; up-noseconds) _secs=soon ;; esac + if [ "\$_what" = build ]; then + printf '%s${TAB}main${TAB}host-up${TAB}1${US}podman${US}build${US}-t${US}x${US}.\n' "\$_last" + else + printf '%s${TAB}main${TAB}host-up${TAB}%s${US}podman${US}run${US}--rm${US}localhost/x:sdlc${US}go-%s\n' \ + "\$_last" "\$_secs" "\$_last" + fi ;; *" plan "*) if [ -f "$SHIM/defer-last" ]; then printf 'codeql${TAB}main${TAB}image-ro${TAB}rc-three\n'; exit 0; fi if [ -f "$SHIM/defer" ]; then printf 'codeql${TAB}main${TAB}image-ro${TAB}rc-three\n'; fi @@ -131,6 +140,11 @@ case "\$_last" in printf 'START %s\n' "\$_last" >> "$SHIM/race.log" printf 'BEGIN %s\n' "\$_last"; sleep 1; printf 'END %s\n' "\$_last" printf 'DONE %s\n' "\$_last" >> "$SHIM/race.log" ;; + go-up-stays) sleep 1; exit 255 ;; + go-up-dies-255) exit 255 ;; + go-up-dies-3) exit 3 ;; + go-up-exits-0) exit 0 ;; + go-up-podman) exit 125 ;; rc-one) printf 'RAN rc-one\n'; exit 1 ;; rc-two) printf 'RAN rc-two\n'; exit 2 ;; rc-three) printf 'RAN rc-three\n'; exit 3 ;; @@ -188,6 +202,33 @@ else bad "a host step's word was evaluated as shell" fi +printf '\n== a run given time to stay up\n\n' + +# container-smoke's step: `host-up`, seconds and then a podman run. podman +# reports its own --timeout kill as 255, which a crashing program can also +# return, so the clock decides: 255 after the full time is a service that +# stayed up, 255 before it is a crash. +for case_ in up-stays:0 up-exits-0:0 up-dies-255:1 up-dies-3:1 up-podman:2 \ + up-build:2 up-noseconds:2; do + name=${case_%:*}; want=${case_#*:} + log_mark + PATH="$SHIM" sh bin/sdlc "$name" > "$RUN/out" 2>&1 && rc=0 || rc=$? + log_since + if [ "$rc" -eq "$want" ]; then + ok "host-up $name exits $want" + else + bad "host-up $name exited $rc, want $want: $(tail -3 "$RUN/out")" + fi +done +log_mark +PATH="$SHIM" sh bin/sdlc up-stays >/dev/null 2>&1 || true +log_since +if grep -q -- '--cap-drop=all .*--timeout 1 --rm localhost/x:sdlc go-up-stays' "$RUN/podman.log"; then + ok "host-up runs locked down, under --timeout" +else + bad "host-up did not pass --timeout with the lock-down: $(grep up-stays "$RUN/podman.log" | tail -1)" +fi + printf '\n== a check deferred to CI\n\n' # 3 is the image saying "could not run, and CI judges this commit". A push lets diff --git a/tests/test_plan.py b/tests/test_plan.py index c0c6ad7..18f73f4 100644 --- a/tests/test_plan.py +++ b/tests/test_plan.py @@ -68,12 +68,26 @@ def cannot_run(fn, *args) -> str | None: "commit defaults", S.resolve(empty, "commit") == ["ruff-check", "ruff-format", "gourmand", "hooks-installed"], ) -check("push defaults", S.resolve(empty, "push") == S.DEFAULTS["push"]) +# Written out rather than read from DEFAULTS: DEFAULTS holds every stack's +# checks, and this is the list a Python repo ran before stacks existed. +PYTHON_PUSH = [ + "lint", + "mypy", + "pytest", + "gourmand", + "gatehouse", + "semgrep", + "pip-audit", + "constitution", + "codeql", +] +check("push defaults", S.resolve(empty, "push") == PYTHON_PUSH, str(S.resolve(empty, "push"))) ci = S.resolve(empty, "ci") check("ci leaves out the reviewers", "gourmand" not in ci and "gatehouse" not in ci, str(ci)) check( "ci keeps the other push checks", - ci == [c for c in S.DEFAULTS["push"] if c not in S.CI_EXCLUDES], + ci == [c for c in PYTHON_PUSH if c not in S.CI_EXCLUDES], + str(ci), ) # python-sdlc#48: the commit tier's ruff steps fix and pass, so without a @@ -536,6 +550,197 @@ def stderr_and_stdout(fn, target: str | None, pyproject: str) -> tuple[str, str] ) +# ------------------------------------------------------------------ the stacks +# +# One image, several kinds of repo. Detection reads the tree; a config block may +# add or drop a stack but never name a command. And the promise that makes it +# safe to ship: a Python repo that says nothing about stacks plans EXACTLY what +# it planned before stacks existed, Containerfile or not. + + +def stacks_of(files: dict[str, str]) -> tuple[str, ...] | str: + """active_stacks() over a tree holding `files`, or the CannotRun message.""" + with tempfile.TemporaryDirectory() as root: + for name, text in files.items(): + Path(root, name).write_text(text) + S.REPO = Path(root) + try: + return S.active_stacks(S.config_document().get("tool", {}).get("sdlc", {}) or {}) + except S.CannotRunError as exc: + return str(exc) + + +PY = '[project]\nname = "fx"\nversion = "0.0.0"\n' +CF = "FROM quay.io/hummingbird/python:3.14\n" +check("a Python repo is the python stack", stacks_of({"pyproject.toml": PY}) == ("python",)) +check( + "a Python repo naming no stacks keeps python alone, Containerfile or not", + stacks_of({"pyproject.toml": PY, "Containerfile": CF, "package.json": "{}"}) == ("python",), +) +check( + "a Python repo that opts in gets what was detected and what it asked for", + stacks_of({"pyproject.toml": PY + '[tool.sdlc]\nstacks = ["container"]\n', "Containerfile": CF}) + == ("python", "container"), +) +check( + "a Containerfile alone is the container stack", + stacks_of({"Containerfile": CF}) == ("container",), +) +check("a Dockerfile counts too", stacks_of({"Dockerfile": CF}) == ("container",)) +msg = stacks_of({"Containerfile": "FROM quay.io/fedora/fedora-bootc:42\n"}) +check("a bootc Containerfile is not the container stack", "bootc" in str(msg), str(msg)) +msg = stacks_of({"package.json": "{}"}) +check("a stack this image cannot run yet is refused, not skipped", "node" in str(msg), str(msg)) +check( + "skip-stacks accepts running without it", + stacks_of({"package.json": "{}", "sdlc.toml": '[tool.sdlc]\nskip-stacks = ["node"]\n'}) == (), +) +msg = stacks_of({"README.md": "x"}) +check("a repo with nothing detected is docs, which is not here yet", "docs" in str(msg), str(msg)) +msg = stacks_of({"Containerfile": CF, "sdlc.toml": '[tool.sdlc]\nstacks = ["rust"]\n'}) +check("an unknown stack is refused", "rust" in str(msg), str(msg)) +msg = stacks_of({"pyproject.toml": PY, "sdlc.toml": "[tool.sdlc]\n"}) +check("sdlc.toml beside a pyproject.toml is refused", "both exist" in str(msg), str(msg)) + +check( + "the python stack plans push exactly as before stacks", + S.resolve(empty, "push", ("python",)) == PYTHON_PUSH, +) +check( + "the default stacks argument is the python stack", + S.resolve(empty, "ci") == S.resolve(empty, "ci", ("python",)), +) +check( + "the container stack alone pushes hadolint and the common checks", + S.resolve(empty, "push", ("container",)) + == ["containerfile-lint", "gourmand", "gatehouse", "constitution"], +) +check( + "the container stack builds, smokes and scans in ci, after push's checks", + S.resolve(empty, "ci", ("container",)) + == [ + "containerfile-lint", + "constitution", + "container-build", + "container-smoke", + "container-scan", + ], +) +check( + "commit has nothing from the container stack", + S.resolve(empty, "commit", ("container",)) == ["gourmand", "hooks-installed"], +) +msg = cannot_run(S.resolve, {"ci": {"skip": ["containerfile-lint"]}}, "ci", ("container",)) +check("ci may not skip hadolint, a push verdict", msg is not None, str(msg)) +check( + "ci may skip the scan, which only ci runs", + "container-scan" not in S.resolve({"ci": {"skip": ["container-scan"]}}, "ci", ("container",)), +) + + +def container_plan(target: str, sdlc_toml: str = "", **env) -> list[list[str]]: + with tempfile.TemporaryDirectory() as root: + Path(root, "Containerfile").write_text(CF) + if sdlc_toml: + Path(root, "sdlc.toml").write_text(sdlc_toml) + S.REPO = Path(root) + saved = dict(os.environ) + os.environ.update( + { + "SDLC_HOST_REPO": root, + "SDLC_CACHE_VOLUME": "testvol", + "SDLC_PROJECT": "fx", + "SDLC_IMAGE": "quay.io/crunchtools/sdlc:9.9.9", + **env, + } + ) + buf = io.StringIO() + try: + with contextlib.redirect_stdout(buf): + S.t_plan(target) + finally: + os.environ.clear() + os.environ.update(saved) + return [line.split("\t") for line in buf.getvalue().splitlines()] + + +rows = container_plan("ci") +by_check = {r[0]: r for r in rows} +check("every container plan line has four fields", all(len(r) == PLAN_FIELDS for r in rows)) +check( + "build, smoke and scan are one lane, off the main lane", + len({by_check[c][1] for c in S.CI_DEFAULTS}) == 1 + and by_check["container-build"][1] != S.MAIN_LANE, + str([(r[0], r[1]) for r in rows]), +) +build = by_check["container-build"][3].split(S.UNIT_SEPARATOR) +check( + "container-build builds the root Containerfile at the default tag", + build[:4] == ["podman", "build", "-f", "Containerfile"] and "localhost/fx:sdlc" in build, + str(build), +) +smoke = by_check["container-smoke"] +words = smoke[3].split(S.UNIT_SEPARATOR) +check( + "container-smoke is host-up, its first word the seconds", + smoke[2] == "host-up" and words[0] == str(S.SMOKE_SECONDS) and words[1:3] == ["podman", "run"], + str(smoke), +) +check("container-smoke has no network by default", "--network" in words and "none" in words) +check("container-smoke never pulls what it judges", "--pull=never" in words, str(words)) +scan = by_check["container-scan"][3].split(S.UNIT_SEPARATOR) +check( + "container-scan mounts the built image and runs this image's scan-rootfs", + "type=image,source=localhost/fx:sdlc,destination=/scan,rw=false" in scan + and scan[-2:] == ["quay.io/crunchtools/sdlc:9.9.9", "scan-rootfs"], + str(scan), +) +check("container-scan fails on a fixable critical by default", "SDLC_SCAN_FAIL_ON=critical" in scan) +rows = container_plan( + "ci", + '[tool.sdlc.ci.container-build]\ntag = "localhost/other:t"\n' + '[tool.sdlc.ci.container-smoke]\nseconds = 3\nargs = ["--version"]\nnetwork = true\n', +) +by_check = {r[0]: r for r in rows} +words = by_check["container-smoke"][3].split(S.UNIT_SEPARATOR) +check( + "smoke and scan follow container-build's tag", + "localhost/other:t" in words + and "type=image,source=localhost/other:t,destination=/scan,rw=false" + in by_check["container-scan"][3].split(S.UNIT_SEPARATOR), + str(words), +) +check( + "smoke takes its seconds, args and network from its table", + words[0] == "3" and words[-1] == "--version" and "none" not in words, + str(words), +) +for bad_cfg, why in ( + ("seconds = 0", "seconds"), + ("seconds = true", "seconds"), + ('seconds = "10"', "seconds"), +): + with tempfile.TemporaryDirectory(): + try: + container_plan("ci", f"[tool.sdlc.ci.container-smoke]\n{bad_cfg}\n") + msg = None + except S.CannotRunError as exc: + msg = str(exc) + check(f"smoke refuses {bad_cfg}", msg is not None and why in msg, str(msg)) +try: + container_plan("ci", '[tool.sdlc.ci.container-scan]\nfail_on = "severe"\n') + msg = None +except S.CannotRunError as exc: + msg = str(exc) +check("scan refuses an unknown severity", msg is not None and "fail_on" in msg, str(msg)) +try: + container_plan("ci", '[tool.sdlc.ci.container-build]\nkind = "image-build"\n') + msg = None +except S.CannotRunError as exc: + msg = str(exc) +check("a built-in container check cannot set a kind", msg is not None and "built in" in msg) + + # ------------------------------------------------ arguments for a single check # # bin/sdlc joins the words after `--` with the unit separator and exports From 0323fcad8ac033f9fc193e5e6103938c0846feff Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 17 Nov 2026 16:51:35 +0000 Subject: [PATCH 02/11] fix: scan-rootfs and sdlc.toml report a 2, never a traceback An unwritable grype cache and an sdlc.toml that is not UTF-8 escaped as uncaught exceptions. Both are now cannot-run, and scan-rootfs's exit translation (grype 0/2/1 -> 0/1/2) is tested against a fake grype. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01R7Y1jckWHhAgXg2WBwkWbb --- sdlc | 29 ++++++++++++----- tests/test_plan.py | 78 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 99 insertions(+), 8 deletions(-) diff --git a/sdlc b/sdlc index a17170b..16c4a2c 100755 --- a/sdlc +++ b/sdlc @@ -170,7 +170,7 @@ def config_document() -> dict: ) try: return tomllib.loads(path.read_text(encoding="utf-8")) - except (OSError, tomllib.TOMLDecodeError) as exc: + except (OSError, UnicodeDecodeError, tomllib.TOMLDecodeError) as exc: cannot(f"cannot read {path}: {exc}") @@ -1026,6 +1026,7 @@ def t_containerfile_lint(s: dict) -> int: GRYPE_DB = CACHE / "grype" +SCAN_ROOT = Path("/scan") def t_scan_rootfs() -> int: @@ -1036,21 +1037,33 @@ def t_scan_rootfs() -> int: nowhere else. Its vulnerability database lives in the cache volume and is refreshed when stale; one lock keeps two gates from rewriting it at once. """ - if not Path("/scan").is_dir(): + if not SCAN_ROOT.is_dir(): cannot("nothing is mounted at /scan; container-scan runs this, not a person") fail_on = os.environ.get("SDLC_SCAN_FAIL_ON", "critical") if fail_on not in GRYPE_SEVERITIES: - cannot(f"SDLC_SCAN_FAIL_ON must be one of {', '.join(GRYPE_SEVERITIES)}") - cmd = ["grype", "dir:/scan", "--name", project_name(), "--fail-on", fail_on, "-o", "table"] + cannot(f"SDLC_SCAN_FAIL_ON is {fail_on!r}; it must be one of {', '.join(GRYPE_SEVERITIES)}") + cmd = [ + "grype", + f"dir:{SCAN_ROOT}", + "--name", + project_name(), + "--fail-on", + fail_on, + "-o", + "table", + ] if os.environ.get("SDLC_SCAN_ONLY_FIXED", "1") == "1": cmd.append("--only-fixed") env = dict(os.environ) env.update({"GRYPE_DB_CACHE_DIR": str(GRYPE_DB), "GRYPE_CHECK_FOR_APP_UPDATE": "false"}) - GRYPE_DB.mkdir(parents=True, exist_ok=True) step(f"grype, failing on a {'fixable ' if '--only-fixed' in cmd else ''}{fail_on} or worse") - with (GRYPE_DB / ".lock").open("w") as lock: - fcntl.flock(lock, fcntl.LOCK_EX) - rc = run(cmd, env=env)[0] + try: + GRYPE_DB.mkdir(parents=True, exist_ok=True) + with (GRYPE_DB / ".lock").open("w") as lock: + fcntl.flock(lock, fcntl.LOCK_EX) + rc = run(cmd, env=env)[0] + except OSError as exc: + cannot(f"cannot use grype's database cache at {GRYPE_DB}: {exc}") if rc == 0: return CLEAN if rc == 2: # noqa: PLR2004 -- grype's own "above the threshold" diff --git a/tests/test_plan.py b/tests/test_plan.py index 18f73f4..c3776ac 100644 --- a/tests/test_plan.py +++ b/tests/test_plan.py @@ -741,6 +741,84 @@ def container_plan(target: str, sdlc_toml: str = "", **env) -> list[list[str]]: check("a built-in container check cannot set a kind", msg is not None and "built in" in msg) +# -------------------------------------------------------------- scan-rootfs +# +# grype's exit codes are the reverse of this file's: 2 is a finding and 1 is +# "could not scan". scan-rootfs translates them, and a broken cache is a 2, not +# a traceback. A fake grype stands in, recording its argv. + +FAKE_GRYPE = """#!/bin/sh +printf '%s\\n' "$@" > "$FAKE_DIR/argv" +exit "$(cat "$FAKE_DIR/rc")" +""" + + +def scan_with(grype_rc: int, *, broken_cache: bool = False, **env) -> tuple[int | str, list[str]]: + with tempfile.TemporaryDirectory() as root: + bindir = Path(root, "bin") + bindir.mkdir() + fake = bindir / "grype" + fake.write_text(FAKE_GRYPE) + fake.chmod(0o755) + Path(root, "rc").write_text(str(grype_rc)) + Path(root, "scan").mkdir() + saved = (dict(os.environ), S.SCAN_ROOT, S.GRYPE_DB, S.REPO) + os.environ.update({"PATH": f"{bindir}:{os.environ['PATH']}", "FAKE_DIR": root, **env}) + S.SCAN_ROOT = Path(root, "scan") + S.REPO = Path(root) + if broken_cache: + Path(root, "cache").write_text("a file where a directory goes") + S.GRYPE_DB = Path(root, "cache", "grype") + try: + with contextlib.redirect_stdout(io.StringIO()): + out: int | str = S.t_scan_rootfs() + except S.CannotRunError as exc: + out = str(exc) + finally: + os.environ.clear() + os.environ.update(saved[0]) + S.SCAN_ROOT, S.GRYPE_DB, S.REPO = saved[1:] + argv_file = Path(root, "argv") + argv = argv_file.read_text().split("\n") if argv_file.is_file() else [] + return out, argv + + +out, argv = scan_with(0) +check("scan-rootfs: grype 0 is clean", out == S.CLEAN, str(out)) +check( + "scan-rootfs fails on a fixable critical by default", + "--fail-on" in argv + and argv[argv.index("--fail-on") + 1] == "critical" + and "--only-fixed" in argv, + str(argv), +) +out, _ = scan_with(2) +check("scan-rootfs: grype 2, above the threshold, is a finding", out == S.FOUND, str(out)) +out, _ = scan_with(1) +check("scan-rootfs: grype 1 could not scan, which is a 2", isinstance(out, str), str(out)) +out, argv = scan_with(0, SDLC_SCAN_FAIL_ON="high", SDLC_SCAN_ONLY_FIXED="0") +check( + "scan-rootfs takes its threshold and drops --only-fixed when told", + argv[argv.index("--fail-on") + 1] == "high" and "--only-fixed" not in argv, + str(argv), +) +out, _ = scan_with(0, SDLC_SCAN_FAIL_ON="severe") +check("scan-rootfs refuses an unknown severity", isinstance(out, str) and "severe" in out, str(out)) +out, _ = scan_with(0, broken_cache=True) +check( + "scan-rootfs: a cache it cannot write is a 2, not a traceback", + isinstance(out, str) and "database cache" in out, + str(out), +) + +with tempfile.TemporaryDirectory() as root: + Path(root, "Containerfile").write_text(CF) + Path(root, "sdlc.toml").write_bytes(b"\xff\xfe not utf-8") + S.REPO = Path(root) + msg = cannot_run(S.config_document) +check("an sdlc.toml that is not UTF-8 is a 2, not a traceback", msg is not None, str(msg)) + + # ------------------------------------------------ arguments for a single check # # bin/sdlc joins the words after `--` with the unit separator and exports From 50416d536f9a79c0cc2a3a27846aee5ea7717e1e Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 17 Nov 2026 17:14:08 +0000 Subject: [PATCH 03/11] test: the container stack end to end in CI tests/test_container_stack.sh runs the real launcher and podman against the freshly built image: a Containerfile-only repo's `sdlc ci` builds, smokes and scans a service image (exit 0), and an image that dies on start fails container-smoke (exit 1). It proves the `--mount type=image` scan path, which the planner's argv tests cannot. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01R7Y1jckWHhAgXg2WBwkWbb --- .github/workflows/container.yml | 8 ++++- .pre-commit-config.yaml | 2 +- tests/test_container_stack.sh | 57 +++++++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+), 2 deletions(-) create mode 100755 tests/test_container_stack.sh diff --git a/.github/workflows/container.yml b/.github/workflows/container.yml index 15f81d3..a3cf78c 100644 --- a/.github/workflows/container.yml +++ b/.github/workflows/container.yml @@ -88,7 +88,7 @@ jobs: set -eu podman run --rm -v "$PWD:/mnt:ro,z" -w /mnt \ docker.io/koalaman/shellcheck:v0.11.0 bin/sdlc tests/contract.sh \ - tests/test_host_contract.sh scripts/release-version.sh + tests/test_host_contract.sh tests/test_container_stack.sh scripts/release-version.sh tests/test_host_contract.sh tests/test_release_version.py @@ -130,6 +130,12 @@ jobs: "localhost/python-sdlc:$GITHUB_SHA" "$t" done + # The container stack through the real launcher and a real podman: the + # image mount grype scans is something only podman can prove. + - name: Container stack end to end + if: env.IS_TAG != 'true' + run: tests/test_container_stack.sh "localhost/python-sdlc:$GITHUB_SHA" + - name: Log in to Quay if: github.event_name != 'pull_request' env: diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 503604c..1264c78 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -35,7 +35,7 @@ repos: hooks: - id: shellcheck name: shellcheck (bin/sdlc, the tests and scripts) - entry: podman --events-backend=none run --rm --log-driver=none -v .:/mnt:ro,z -w /mnt docker.io/koalaman/shellcheck:v0.11.0 bin/sdlc tests/contract.sh tests/test_host_contract.sh scripts/release-version.sh + entry: podman --events-backend=none run --rm --log-driver=none -v .:/mnt:ro,z -w /mnt docker.io/koalaman/shellcheck:v0.11.0 bin/sdlc tests/contract.sh tests/test_host_contract.sh tests/test_container_stack.sh scripts/release-version.sh language: system pass_filenames: false always_run: true diff --git a/tests/test_container_stack.sh b/tests/test_container_stack.sh new file mode 100755 index 0000000..2adc655 --- /dev/null +++ b/tests/test_container_stack.sh @@ -0,0 +1,57 @@ +#!/bin/sh +# The container stack end to end, through the real launcher and a real podman: +# a Containerfile-only repo's `sdlc ci` builds the image, smoke-runs it and +# scans it with grype through `podman run --mount type=image`. Then an image +# that dies on start must fail the smoke run. Planned argv is unit-tested in +# test_plan.py; this is the part only podman can prove. +# +# Usage: tests/test_container_stack.sh + +set -eu + +IMAGE=${1:?usage: tests/test_container_stack.sh } +HERE=$(CDPATH='' cd -- "$(dirname -- "$0")/.." && pwd) +FAILED=0 +ok() { printf 'ok %s\n' "$*"; } +bad() { printf 'FAIL %s\n' "$*"; FAILED=$((FAILED + 1)); } + +WORK=$(mktemp -d) +trap 'rm -rf "$WORK"' EXIT + +fixture() { # name entrypoint-json + mkdir -p "$WORK/$1" + printf 'FROM quay.io/hummingbird/python:3.14\nUSER 65532\nENTRYPOINT %s\n' "$2" \ + > "$WORK/$1/Containerfile" + # The fixture has no constitution manifest to validate. + printf '[tool.sdlc.push]\nskip = ["constitution"]\n' > "$WORK/$1/sdlc.toml" + git -C "$WORK/$1" init -q + git -C "$WORK/$1" add -A + git -C "$WORK/$1" -c user.email=t@t -c user.name=t commit -qm fixture +} + +fixture serves '["python3", "-m", "http.server", "8080"]' +rc=0 +(cd "$WORK/serves" && SDLC_IMAGE=$IMAGE "$HERE/bin/sdlc" ci) > "$WORK/serves.out" 2>&1 || rc=$? +if [ "$rc" -eq 0 ] \ + && grep -q 'container-smoke passed' "$WORK/serves.out" \ + && grep -q 'container-scan passed' "$WORK/serves.out"; then + ok "sdlc ci builds, smokes and scans a service image (exit 0)" +else + bad "sdlc ci on a service image exited $rc" + tail -30 "$WORK/serves.out" | sed 's/^/ /' +fi + +fixture crashes '["python3", "-c", "import missing_module"]' +(cd "$WORK/crashes" && SDLC_IMAGE=$IMAGE "$HERE/bin/sdlc" container-build) >/dev/null 2>&1 || : +rc=0 +(cd "$WORK/crashes" && SDLC_IMAGE=$IMAGE "$HERE/bin/sdlc" container-smoke) > "$WORK/crashes.out" 2>&1 || rc=$? +if [ "$rc" -eq 1 ]; then + ok "an image that dies on start fails container-smoke (exit 1)" +else + bad "container-smoke on a crashing image exited $rc, want 1" + tail -10 "$WORK/crashes.out" | sed 's/^/ /' +fi + +printf '\n' +[ "$FAILED" -eq 0 ] && { printf 'container stack PASSED\n'; exit 0; } +printf 'container stack FAILED: %s check(s)\n' "$FAILED"; exit 1 From 36a89f9bf22393f2798d9fa2f9a0849c3411f3ee Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 17 Nov 2026 17:25:03 +0000 Subject: [PATCH 04/11] fix: scan-rootfs guards only the cache, not grype's launch The cache handler wrapped run() too, so a grype that could not start would have been reported as a cache failure. run() already reports that itself. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01R7Y1jckWHhAgXg2WBwkWbb --- sdlc | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/sdlc b/sdlc index 16c4a2c..1d94f68 100755 --- a/sdlc +++ b/sdlc @@ -1057,13 +1057,16 @@ def t_scan_rootfs() -> int: env = dict(os.environ) env.update({"GRYPE_DB_CACHE_DIR": str(GRYPE_DB), "GRYPE_CHECK_FOR_APP_UPDATE": "false"}) step(f"grype, failing on a {'fixable ' if '--only-fixed' in cmd else ''}{fail_on} or worse") + # Only the cache is guarded here. A grype that cannot start is run()'s to + # report, in its own words. try: GRYPE_DB.mkdir(parents=True, exist_ok=True) - with (GRYPE_DB / ".lock").open("w") as lock: - fcntl.flock(lock, fcntl.LOCK_EX) - rc = run(cmd, env=env)[0] + lock = (GRYPE_DB / ".lock").open("w") + fcntl.flock(lock, fcntl.LOCK_EX) except OSError as exc: cannot(f"cannot use grype's database cache at {GRYPE_DB}: {exc}") + with lock: + rc = run(cmd, env=env)[0] if rc == 0: return CLEAN if rc == 2: # noqa: PLR2004 -- grype's own "above the threshold" From 9e982f10bd23657d4f77a134529e0d18e1c2966c Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 17 Nov 2026 17:36:15 +0000 Subject: [PATCH 05/11] test: the crash case must build, and fail on its own entrypoint A failed build or a missing image also makes container-smoke exit non-zero, so the regression test now requires the build to succeed and the smoke failure to carry the entrypoint's own error. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01R7Y1jckWHhAgXg2WBwkWbb --- tests/test_container_stack.sh | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/tests/test_container_stack.sh b/tests/test_container_stack.sh index 2adc655..3850f69 100755 --- a/tests/test_container_stack.sh +++ b/tests/test_container_stack.sh @@ -42,14 +42,24 @@ else fi fixture crashes '["python3", "-c", "import missing_module"]' -(cd "$WORK/crashes" && SDLC_IMAGE=$IMAGE "$HERE/bin/sdlc" container-build) >/dev/null 2>&1 || : +# The build has to succeed, and the failure has to be the entrypoint's: a +# missing image is also a non-zero smoke run, and must not pass for this one. rc=0 -(cd "$WORK/crashes" && SDLC_IMAGE=$IMAGE "$HERE/bin/sdlc" container-smoke) > "$WORK/crashes.out" 2>&1 || rc=$? -if [ "$rc" -eq 1 ]; then - ok "an image that dies on start fails container-smoke (exit 1)" +(cd "$WORK/crashes" && SDLC_IMAGE=$IMAGE "$HERE/bin/sdlc" container-build) > "$WORK/build.out" 2>&1 || rc=$? +if [ "$rc" -ne 0 ]; then + bad "the crashing fixture did not build (exit $rc), so its smoke run proves nothing" + tail -10 "$WORK/build.out" | sed 's/^/ /' else - bad "container-smoke on a crashing image exited $rc, want 1" - tail -10 "$WORK/crashes.out" | sed 's/^/ /' + rc=0 + (cd "$WORK/crashes" && SDLC_IMAGE=$IMAGE "$HERE/bin/sdlc" container-smoke) > "$WORK/crashes.out" 2>&1 || rc=$? + if [ "$rc" -eq 1 ] \ + && grep -q "No module named 'missing_module'" "$WORK/crashes.out" \ + && grep -q 'the container exited 1 within' "$WORK/crashes.out"; then + ok "an image that dies on start fails container-smoke (exit 1, its own error)" + else + bad "container-smoke on a crashing image exited $rc, want 1 from the entrypoint" + tail -10 "$WORK/crashes.out" | sed 's/^/ /' + fi fi printf '\n' From 08ed876a8be597e762fd6a23d518fd0932411b1b Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 17 Nov 2026 18:05:25 +0000 Subject: [PATCH 06/11] feat: run a pull request's image before a release CI publishes each pull request's tested image as quay.io/crunchtools/sdlc:pr-, and :beta when the PR has the `beta` label; never latest, main or a version tag. The launcher runs one through SDLC_IMAGE (a fleet tag or any reference) or [tool.sdlc] image = "" (fleet tags only), prints which image it ran on every invocation that is not the pinned release, and pulls a registry tag when newer. container-scan names the gate image as `@sdlc-image`, which run_host replaces, instead of the launcher passing SDLC_IMAGE into every container: with no override the launcher's podman argv is byte-identical to main's. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01R7Y1jckWHhAgXg2WBwkWbb --- .github/workflows/container.yml | 35 +++++++++++++ CHANGELOG.md | 7 +++ README.md | 27 ++++++++++ bin/sdlc | 78 ++++++++++++++++++++++++++-- sdlc | 47 ++++++++++++++--- tests/test_host_contract.sh | 90 +++++++++++++++++++++++++++++++++ tests/test_plan.py | 41 ++++++++++++++- 7 files changed, 313 insertions(+), 12 deletions(-) diff --git a/.github/workflows/container.yml b/.github/workflows/container.yml index a3cf78c..ce16b5e 100644 --- a/.github/workflows/container.yml +++ b/.github/workflows/container.yml @@ -23,7 +23,9 @@ on: push: branches: [main] tags: ["v*"] + # `labeled` too, so adding the `beta` label publishes `:beta` without a push. pull_request: + types: [opened, synchronize, reopened, labeled] # Weekly, to pick up base-image errata. MANDATORY for this profile, and so is # bypassing the layer cache on it -- a cached `dnf install` layer is reused # while the Containerfile and base digest are unchanged, so a cached rebuild @@ -136,6 +138,39 @@ jobs: if: env.IS_TAG != 'true' run: tests/test_container_stack.sh "localhost/python-sdlc:$GITHUB_SHA" + # The inner loop: a pull request's tested image, runnable before any + # release carries it, with SDLC_IMAGE=pr- or [tool.sdlc] image. It + # goes to the fleet name only, as `:pr-`, and as `:beta` when the PR + # carries the `beta` label -- whichever labelled PR pushed last holds it. + # + # These two tags and no others. `latest`, `main` and the version tags + # belong to `promote`, which never runs on a pull request; nothing here + # can reach them. A fork's pull request has no secrets and Dependabot's + # has its own, so both are skipped rather than failed. + - name: Publish the pull request's image + if: >- + github.event_name == 'pull_request' + && github.event.pull_request.head.repo.full_name == github.repository + && github.event.pull_request.user.login != 'dependabot[bot]' + env: + QUAY_USERNAME: ${{ secrets.QUAY_USERNAME }} + QUAY_PASSWORD: ${{ secrets.QUAY_PASSWORD }} + PR: ${{ github.event.pull_request.number }} + BETA: ${{ contains(github.event.pull_request.labels.*.name, 'beta') }} + run: | + set -eu + podman login -u "$QUAY_USERNAME" -p "$QUAY_PASSWORD" quay.io + podman push "localhost/python-sdlc:$GITHUB_SHA" "$SDLC:pr-$PR" + digest=$(skopeo inspect --format '{{.Digest}}' "docker://$SDLC:pr-$PR") + [ -n "$digest" ] || { echo "the registry reported no digest" >&2; exit 1; } + echo "published $SDLC:pr-$PR ($digest)" + if [ "$BETA" = true ]; then + skopeo copy --all --preserve-digests "docker://$SDLC@$digest" "docker://$SDLC:beta" + got=$(skopeo inspect --format '{{.Digest}}' "docker://$SDLC:beta") + [ "$got" = "$digest" ] || { echo "$SDLC:beta is $got, not $digest" >&2; exit 1; } + echo "published $SDLC:beta ($digest)" + fi + - name: Log in to Quay if: github.event_name != 'pull_request' env: diff --git a/CHANGELOG.md b/CHANGELOG.md index bab077c..0546203 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -30,6 +30,13 @@ All notable changes to this project are documented here, following repo's `.grype.yaml` applies). - `sdlc.toml`: the same `[tool.sdlc]` tables, for a repo with no `pyproject.toml`. Refused beside one. +- **A pull request's image, before a release.** Every push to a pull request + here publishes `quay.io/crunchtools/sdlc:pr-`, and `:beta` when it has + the `beta` label; never `latest`, `main` or a version tag. `SDLC_IMAGE` + (a fleet tag such as `beta`, or any image reference) or + `[tool.sdlc] image = ""` runs one. Every run on anything but the pinned + release prints which image it is and what chose it, and a registry tag is + pulled when newer. With neither set, nothing changes. - The image is also published as `quay.io/crunchtools/sdlc`: the same digest copied, never rebuilt, with the same tags as `python-sdlc`. diff --git a/README.md b/README.md index f8d623e..8f65f3f 100644 --- a/README.md +++ b/README.md @@ -330,6 +330,33 @@ Values in `build_args`, `labels` and `env` may be `@project.version`, There is no shell kind. A repo that needs something these cannot express gets a new kind here, so the config stays something a person can read. +## Running a pull request's image + +A pull request's image can be run before any release carries it. Every push to +a pull request in this repo publishes the tested image as +`quay.io/crunchtools/sdlc:pr-`, and as `:beta` too when the pull request +has the `beta` label. Whichever labelled pull request pushed last holds +`:beta`. Only a release moves `latest`, `main` or a version tag. + +To run one, first matching wins: + +| How | Scope | Value | +|---|---|---| +| `SDLC_IMAGE=pr-59 git push` | this run, this machine | a tag of `quay.io/crunchtools/sdlc`, or a full image reference | +| `[tool.sdlc] image = "beta"` | every run of this repo, CI included | a tag of `quay.io/crunchtools/sdlc` only, written on its own line under `[tool.sdlc]` | + +A run on anything but the pinned release says so at the top of every command, +naming the image and what chose it. A registry tag is pulled when newer, so +`:beta` is the current `:beta`, not whatever was cached. A repo's config can +only name a tag of the fleet image, never an arbitrary image to judge itself +with. Take `image` out before merging: CI would run it too. + +**Nothing built on a laptop or a seat is ever pushed.** `sdlc` builds images +only to test them (`container-build` tags `localhost/:sdlc`) and has no +push step. Run a local build of this image with +`SDLC_IMAGE=localhost/python-sdlc:dev`. Only this repo's GitHub Actions +publish to quay.io. + ## Skipping a check `SDLC_SKIP=gatehouse git push` skips one check, once, for a false positive. The diff --git a/bin/sdlc b/bin/sdlc index 21c6cc7..5eca00b 100755 --- a/bin/sdlc +++ b/bin/sdlc @@ -44,9 +44,11 @@ step() { printf '\n== %s\n' "$*"; } IMAGE_TAG=$(tr -d ' \t\n\r' < "$HOOKS_DIR/IMAGE_TAG") [ -n "$IMAGE_TAG" ] || die "IMAGE_TAG is empty" # Read from the cloned hooks repo, so the `rev:` a repo pins fixes the launcher -# and the image together and they cannot disagree. Overridable for development -# of this repo itself, not for consumers. -IMAGE=${SDLC_IMAGE:-quay.io/crunchtools/python-sdlc:$IMAGE_TAG} +# and the image together and they cannot disagree. An override -- below, once +# the repo is known -- replaces it for the inner loop, and says so every run. +DEFAULT_IMAGE=quay.io/crunchtools/python-sdlc:$IMAGE_TAG +# Where CI publishes a pull request's tested image, as `:pr-` and `:beta`. +FLEET_IMAGE=quay.io/crunchtools/sdlc # --------------------------------------------------------------- preconditions @@ -65,6 +67,65 @@ REPO=$(git rev-parse --show-toplevel 2>/dev/null) \ cd "$REPO" PROJECT=$(basename "$REPO") +# ------------------------------------------------------------- an image override +# +# The inner loop: run a pull request's image, `:pr-`, or the one labelled +# `beta`, before any release carries it. Two ways in, the first winning: +# +# SDLC_IMAGE= | this seat, this run. A bare tag means a tag of +# quay.io/crunchtools/sdlc, so SDLC_IMAGE=beta. +# [tool.sdlc] image = "" the repo, every run. A TAG of the fleet image +# and nothing else: a repo's config must not be +# able to name whatever image judges it. +# +# The image is the judge, so a run that is not on the pinned release says so on +# every invocation, loudly. And a floating tag is pulled when newer: `missing`, +# podman's default, is how a two-week-old reviewer once judged trentina. +CONFIG_FILE=pyproject.toml +[ -f "$CONFIG_FILE" ] || CONFIG_FILE=sdlc.toml +config_image() { # the `image` key of [tool.sdlc], or nothing + [ -f "$CONFIG_FILE" ] || return 0 + awk ' + /^[ \t]*\[/ { s = ($0 ~ /^[ \t]*\[tool\.sdlc\][ \t]*(#.*)?$/); next } + s && /^[ \t]*image[ \t]*=/ { + v = $0; sub(/^[^=]*=[ \t]*/, "", v) + if (v ~ /^"[^"]*"/) { sub(/^"/, "", v); sub(/".*$/, "", v) } + print v; exit + }' "$CONFIG_FILE" +} +fleet_tag() { # value where -> the fleet image at that tag, or die + case "$1" in + ''|*[!A-Za-z0-9._-]*|[.-]*) die "$2 must be a tag of $FLEET_IMAGE such as beta or pr-59; got '$1'" ;; + esac + printf '%s:%s' "$FLEET_IMAGE" "$1" +} +SDLC_CONFIG_IMAGE=$(config_image) +export SDLC_CONFIG_IMAGE +IMAGE=$DEFAULT_IMAGE +IMAGE_FROM= +if [ -n "${SDLC_IMAGE:-}" ]; then + case "$SDLC_IMAGE" in + */*) IMAGE=$SDLC_IMAGE ;; + *) IMAGE=$(fleet_tag "$SDLC_IMAGE" SDLC_IMAGE) ;; + esac + IMAGE_FROM=SDLC_IMAGE +elif [ -n "$SDLC_CONFIG_IMAGE" ]; then + IMAGE=$(fleet_tag "$SDLC_CONFIG_IMAGE" "[tool.sdlc] image in $CONFIG_FILE") + IMAGE_FROM="[tool.sdlc] image in $CONFIG_FILE" +fi +PULL_NEWER= +if [ "$IMAGE" != "$DEFAULT_IMAGE" ]; then + case "$IMAGE" in localhost/*) ;; *) PULL_NEWER=yes ;; esac + { + printf '%s: ************************************************************\n' "$ME" + printf '%s: NOT THE PINNED RELEASE. This run is judged by\n' "$ME" + printf '%s: %s\n' "$ME" "$IMAGE" + printf '%s: set by %s, in place of\n' "$ME" "$IMAGE_FROM" + printf '%s: %s\n' "$ME" "$DEFAULT_IMAGE" + printf '%s: ************************************************************\n' "$ME" + } >&2 +fi + # ---------------------------------------------------------- the resource budget # # Ported from gates.sh:55-80. A gate must not take the machine away from the @@ -268,6 +329,7 @@ run_in_image() { # never hit it. set -- \ --events-backend=none run --rm --log-driver=none \ + ${PULL_NEWER:+--pull=newer} \ --cap-drop=all --security-opt=no-new-privileges \ --cpus "$GATE_CPUS" --memory "$GATE_MEMORY" --cpu-shares 256 \ -v "$REPO:/repo:$_mode,z" -w /repo \ @@ -278,7 +340,7 @@ run_in_image() { -e PYTHONPYCACHEPREFIX=/tmp/pycache \ -e SEMGREP_SETTINGS_FILE=/tmp/semgrep.yml \ -e "SDLC_PROJECT=$PROJECT" \ - -e "SDLC_IMAGE=$IMAGE" \ + ${SDLC_CONFIG_IMAGE:+-e SDLC_CONFIG_IMAGE} \ -e "SDLC_JOBS=$GATE_CPUS" \ -e "SDLC_MEMORY=$GATE_MEMORY" \ -e "SDLC_DEFAULT_BRANCH=$(default_branch 2>/dev/null || :)" \ @@ -481,6 +543,14 @@ run_host() { # argv [seconds] -> the step's verdict [ "${1:-}" = podman ] || die "a host step may only run podman; refused: ${1:-}" _sub=${2:-} shift 2 + # `@sdlc-image` is this gate's own image, which only this script knows: a + # check that runs the gate image as a host step (container-scan) names it so. + # Replaced as a whole word only, after the split, so it is never shell. + for _w in "$@"; do + shift + [ "$_w" = @sdlc-image ] && _w=$IMAGE + set -- "$@" "$_w" + done # The budget is applied here so it is defined once, whatever the step. case "$_sub" in build) set -- build --cpu-period 100000 --cpu-quota "$(( GATE_CPUS * 100000 ))" \ diff --git a/sdlc b/sdlc index 1d94f68..9dcbce5 100755 --- a/sdlc +++ b/sdlc @@ -382,13 +382,14 @@ RESERVED = ( def sdlc_config() -> dict: """`[tool.sdlc]`, checked for shape before anything trusts it.""" cfg = config_document().get("tool", {}).get("sdlc", {}) or {} - stray = sorted(set(cfg) - set(COMMANDS) - {"project", "stacks", "skip-stacks"}) + stray = sorted(set(cfg) - set(COMMANDS) - {"project", "stacks", "skip-stacks", "image"}) if stray: cannot( f"[tool.sdlc] has {', '.join(stray)}; the only keys are " f"[tool.sdlc.commit], [tool.sdlc.push], [tool.sdlc.ci], `project`, " - f"`stacks` and `skip-stacks`" + f"`stacks`, `skip-stacks` and `image`" ) + config_image(cfg) project_dir() # validated here, so a bad path is caught on the next commit stacks = active_stacks(cfg) # The WHOLE config is checked whichever command runs, so a typo in the ci @@ -399,6 +400,36 @@ def sdlc_config() -> dict: return cfg +# `[tool.sdlc] image = "beta"` runs a tag of the fleet image instead of the +# pinned release. bin/sdlc chooses the image before this script exists, so it +# reads the key itself -- with awk, as one `image = ""` line under +# [tool.sdlc] -- and passes what it read as SDLC_CONFIG_IMAGE. TOML has other +# ways to write the same key, and a launcher that missed one would run the +# pinned release while the repo believed it ran beta, so the two readings must +# agree or nothing runs. +IMAGE_TAG_RE = re.compile(r"[A-Za-z0-9_][A-Za-z0-9._-]{0,127}") + + +def config_image(cfg: dict) -> None: + if "image" not in cfg: + seen = os.environ.get("SDLC_CONFIG_IMAGE") + if "SDLC_HOST_REPO" in os.environ and seen: + cannot(f"bin/sdlc read [tool.sdlc] image = {seen!r} where TOML has none") + return + tag = cfg["image"] + if not isinstance(tag, str) or not IMAGE_TAG_RE.fullmatch(tag): + cannot( + "[tool.sdlc] image must be a tag of quay.io/crunchtools/sdlc, " + f"such as beta or pr-59; got {tag!r}" + ) + if "SDLC_HOST_REPO" in os.environ and os.environ.get("SDLC_CONFIG_IMAGE") != tag: + cannot( + f"[tool.sdlc] image is {tag!r} but bin/sdlc could not read it, so it " + f"would have run the pinned release. Write it on its own line directly " + f'under [tool.sdlc]: image = "{tag}"' + ) + + def block(cfg: dict, command: str) -> dict: value = cfg.get(command, {}) if not isinstance(value, dict): @@ -933,6 +964,11 @@ def check_container_smoke(cfg: dict, name: str, s: dict) -> list[tuple[str, str, return [(name, "host-up", payload)] +# The gate's own image in a host argv. bin/sdlc replaces this word with the +# image it is running, so a plan never has to be told which one that is. +GATE_IMAGE = "@sdlc-image" + + def check_container_scan(cfg: dict, name: str, s: dict) -> list[tuple[str, str, str]]: """Scan the built image's filesystem with grype, in this image. @@ -947,9 +983,8 @@ def check_container_scan(cfg: dict, name: str, s: dict) -> list[tuple[str, str, """ host = os.environ.get("SDLC_HOST_REPO") volume = os.environ.get("SDLC_CACHE_VOLUME") - gate = os.environ.get("SDLC_IMAGE") - if not host or not volume or not gate: - cannot(f"{name}: run through bin/sdlc, which supplies the host paths and the image") + if not host or not volume: + cannot(f"{name}: run through bin/sdlc, which supplies the host paths") fail_on = str(s.get("fail_on", "critical")) if fail_on not in GRYPE_SEVERITIES: cannot(f"{name}: fail_on must be one of {', '.join(GRYPE_SEVERITIES)}") @@ -980,7 +1015,7 @@ def check_container_scan(cfg: dict, name: str, s: dict) -> list[tuple[str, str, f"SDLC_SCAN_FAIL_ON={fail_on}", "-e", f"SDLC_SCAN_ONLY_FIXED={'1' if only_fixed else '0'}", - gate, + GATE_IMAGE, "scan-rootfs", ] return [host_step(name, argv)] diff --git a/tests/test_host_contract.sh b/tests/test_host_contract.sh index 48d48ba..6bc9fcc 100755 --- a/tests/test_host_contract.sh +++ b/tests/test_host_contract.sh @@ -118,6 +118,8 @@ case " \$* " in *" plan lanes-budget "*) printf 'p${TAB}one${TAB}host${TAB}podman${US}build${US}-t${US}localhost/x:sdlc${US}.\n' printf 'q${TAB}two${TAB}image-ro${TAB}quiet-ok\n' ;; + *" plan gate-word "*) + printf 'gate-word${TAB}main${TAB}host${TAB}podman${US}run${US}--rm${US}@sdlc-image${US}scan-rootfs\n' ;; *" plan up-"*) _secs=1; _what=\$_last case "\$_last" in up-build) _what=build ;; up-noseconds) _secs=soon ;; esac @@ -663,6 +665,94 @@ else bad "a tier forwarded SDLC_ARGS from the environment" fi +printf '\n== an image other than the pinned release\n\n' + +# The inner loop runs a pull request's image. With no override, nothing about +# the argv may change: no pull flag, the pinned image. With one, the image is +# named on every run and a floating tag is pulled when newer. +pinned="quay.io/crunchtools/python-sdlc:$(tr -d ' \t\n\r' < IMAGE_TAG)" +last_run() { grep ' run --rm' "$RUN/podman.log" | tail -1; } + +log_mark +PATH="$SHIM" sh bin/sdlc tiers > "$RUN/out" 2>&1 || true +log_since +if last_run | grep -q -- " $pinned tiers\$" && ! last_run | grep -q -- '--pull' \ + && ! grep -q 'NOT THE PINNED RELEASE' "$RUN/out"; then + ok "no override: the pinned image, no pull flag, no banner" +else + bad "no override changed the run: $(last_run)" +fi + +log_mark +SDLC_IMAGE=beta PATH="$SHIM" sh bin/sdlc tiers > "$RUN/out" 2>&1 || true +log_since +if last_run | grep -q -- '--pull=newer .* quay.io/crunchtools/sdlc:beta tiers$' \ + && grep -q 'NOT THE PINNED RELEASE' "$RUN/out" && grep -q 'set by SDLC_IMAGE' "$RUN/out"; then + ok "SDLC_IMAGE=beta runs quay.io/crunchtools/sdlc:beta, pulled when newer, and says so" +else + bad "SDLC_IMAGE=beta: $(last_run) / $(head -3 "$RUN/out")" +fi + +log_mark +SDLC_IMAGE=localhost/python-sdlc:dev PATH="$SHIM" sh bin/sdlc tiers > "$RUN/out" 2>&1 || true +log_since +if last_run | grep -q -- ' localhost/python-sdlc:dev tiers$' && ! last_run | grep -q -- '--pull'; then + ok "a local image is run as built, never pulled" +else + bad "SDLC_IMAGE=localhost/...: $(last_run)" +fi + +rc=0 +SDLC_IMAGE='beta;touch' PATH="$SHIM" sh bin/sdlc tiers > "$RUN/out" 2>&1 || rc=$? +if [ "$rc" -eq 2 ]; then + ok "a bare SDLC_IMAGE that is not a tag is refused (exit 2)" +else + bad "SDLC_IMAGE='beta;touch' exited $rc" +fi + +CREPO=$(mktemp -d) +git -C "$CREPO" init -q +printf '[project]\nname = "x"\n\n[tool.sdlc]\nimage = "pr-59"\n' > "$CREPO/pyproject.toml" +log_mark +(cd "$CREPO" && PATH="$SHIM" sh "$OLDPWD/bin/sdlc" tiers) > "$RUN/out" 2>&1 || true +log_since +if last_run | grep -q -- 'SDLC_CONFIG_IMAGE .*quay.io/crunchtools/sdlc:pr-59 tiers$' \ + && grep -q 'set by \[tool.sdlc\] image in pyproject.toml' "$RUN/out"; then + ok "[tool.sdlc] image = \"pr-59\" runs that tag and tells the image what it read" +else + bad "[tool.sdlc] image: $(last_run) / $(head -4 "$RUN/out")" +fi +log_mark +(cd "$CREPO" && SDLC_IMAGE=beta PATH="$SHIM" sh "$OLDPWD/bin/sdlc" tiers) > "$RUN/out" 2>&1 || true +log_since +if last_run | grep -q -- 'quay.io/crunchtools/sdlc:beta tiers$'; then + ok "SDLC_IMAGE beats [tool.sdlc] image" +else + bad "SDLC_IMAGE did not beat the repo's image: $(last_run)" +fi +printf '[tool.sdlc]\nimage = "../../evil"\n' > "$CREPO/sdlc.toml" +rm "$CREPO/pyproject.toml" +rc=0 +(cd "$CREPO" && PATH="$SHIM" sh "$OLDPWD/bin/sdlc" tiers) > "$RUN/out" 2>&1 || rc=$? +if [ "$rc" -eq 2 ] && grep -q 'must be a tag of' "$RUN/out"; then + ok "a repo's image that is not a plain tag is refused (exit 2)" +else + bad "[tool.sdlc] image = ../../evil exited $rc" +fi +rm -rf "$CREPO" + +# container-scan runs the gate's own image as a host step, and only the +# launcher knows which image that is: the plan names it `@sdlc-image`. +log_mark +SDLC_IMAGE=beta PATH="$SHIM" sh bin/sdlc gate-word >/dev/null 2>&1 || true +log_since +if grep -q -- '--cap-drop=all .* quay.io/crunchtools/sdlc:beta scan-rootfs$' "$RUN/podman.log" \ + && ! grep -q '@sdlc-image' "$RUN/podman.log"; then + ok "a host step's @sdlc-image is the image this run is on" +else + bad "@sdlc-image was not replaced: $(grep scan-rootfs "$RUN/podman.log" | tail -1)" +fi + printf '\n== what the containers are given\n\n' # The worktree is the developer's, so its __pycache__ is the laptop's. Every diff --git a/tests/test_plan.py b/tests/test_plan.py index c3776ac..452107f 100644 --- a/tests/test_plan.py +++ b/tests/test_plan.py @@ -650,7 +650,6 @@ def container_plan(target: str, sdlc_toml: str = "", **env) -> list[list[str]]: "SDLC_HOST_REPO": root, "SDLC_CACHE_VOLUME": "testvol", "SDLC_PROJECT": "fx", - "SDLC_IMAGE": "quay.io/crunchtools/sdlc:9.9.9", **env, } ) @@ -692,7 +691,7 @@ def container_plan(target: str, sdlc_toml: str = "", **env) -> list[list[str]]: check( "container-scan mounts the built image and runs this image's scan-rootfs", "type=image,source=localhost/fx:sdlc,destination=/scan,rw=false" in scan - and scan[-2:] == ["quay.io/crunchtools/sdlc:9.9.9", "scan-rootfs"], + and scan[-2:] == [S.GATE_IMAGE, "scan-rootfs"], str(scan), ) check("container-scan fails on a fixable critical by default", "SDLC_SCAN_FAIL_ON=critical" in scan) @@ -741,6 +740,44 @@ def container_plan(target: str, sdlc_toml: str = "", **env) -> list[list[str]]: check("a built-in container check cannot set a kind", msg is not None and "built in" in msg) +# ------------------------------------------------------------- [tool.sdlc] image +# +# bin/sdlc reads the key with awk before this script runs. If TOML and awk +# disagree, the repo believes it runs one image and the launcher ran another. + + +def image_error(pyproject: str, launcher_read: str | None) -> str | None: + saved = dict(os.environ) + os.environ["SDLC_HOST_REPO"] = "/host" + os.environ.pop("SDLC_CONFIG_IMAGE", None) + if launcher_read is not None: + os.environ["SDLC_CONFIG_IMAGE"] = launcher_read + try: + return config_error(pyproject) + finally: + os.environ.clear() + os.environ.update(saved) + + +check( + "image: the launcher and TOML agreeing is fine", + image_error('[tool.sdlc]\nimage = "beta"\n', "beta") is None, +) +msg = image_error('tool.sdlc.image = "beta"\n', None) +check( + "image: a form the launcher could not read is refused", + msg is not None and "could not read" in msg, + str(msg), +) +msg = image_error('[tool.sdlc]\nimage = "a/b:c"\n', "a/b:c") +check("image: only a tag of the fleet image", msg is not None and "must be a tag" in msg, str(msg)) +msg = image_error("[tool.sdlc]\nimage = 3\n", "3") +check("image: a number is not a tag", msg is not None and "must be a tag" in msg, str(msg)) +msg = image_error("[tool.ruff]\n", "beta") +check("image: a launcher reading one that TOML lacks is refused", msg is not None, str(msg)) +check("image: none set anywhere is fine", image_error("[tool.ruff]\n", None) is None) + + # -------------------------------------------------------------- scan-rootfs # # grype's exit codes are the reverse of this file's: 2 is a finding and 1 is From eccf58d15c05b33ac843df1e3b41a1fbac3d9228 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 17 Nov 2026 18:16:40 +0000 Subject: [PATCH 07/11] fix(ci): contract-test the pushed :pr- digest before :beta moves Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01R7Y1jckWHhAgXg2WBwkWbb --- .github/workflows/container.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.github/workflows/container.yml b/.github/workflows/container.yml index ce16b5e..0d1f388 100644 --- a/.github/workflows/container.yml +++ b/.github/workflows/container.yml @@ -164,6 +164,10 @@ jobs: digest=$(skopeo inspect --format '{{.Digest}}' "docker://$SDLC:pr-$PR") [ -n "$digest" ] || { echo "the registry reported no digest" >&2; exit 1; } echo "published $SDLC:pr-$PR ($digest)" + # What the registry holds is tested, not just what was built here, + # as the profile requires of every pushed digest -- and before + # `:beta`, which other repos run, can move onto it. + tests/contract.sh "$SDLC@$digest" if [ "$BETA" = true ]; then skopeo copy --all --preserve-digests "docker://$SDLC@$digest" "docker://$SDLC:beta" got=$(skopeo inspect --format '{{.Digest}}' "docker://$SDLC:beta") From 06bba339a8340a9bc2aabe50e94764684dcd4240 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 17 Nov 2026 18:45:15 +0000 Subject: [PATCH 08/11] fix: review a202 -- staging repo, CI refuses the image key, smoke by state - [tool.sdlc] image is refused (exit 2) under CI, GITHUB_ACTIONS and `sdlc ci`: a pull request must not choose the image that judges it, and only the launcher can hold that line. - A pull request's image goes to quay.io/crunchtools/sdlc-pr, from a separate publish-pr job that runs none of the PR's code, with its own robot (QUAY_PR_USERNAME / QUAY_PR_PASSWORD, environment sdlc-pr). Pushed to a scratch tag, verified against the tested image ID, then tagged; a re-run of an older run never moves :pr- or :beta back (run-id label). Bare pr- and beta resolve to sdlc-pr. - container-smoke starts detached and reads podman's State (Running, ExitCode, OOMKilled) instead of a clock; 126/127, an entrypoint that cannot start, is a finding, and only 125 is a 2. - hadolint, grype and shellcheck images pinned by digest; actions pinned by commit SHA. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01R7Y1jckWHhAgXg2WBwkWbb --- .github/workflows/container.yml | 149 +++++++++++++++++++++-------- .github/workflows/release.yml | 4 +- .github/workflows/update-tools.yml | 4 +- .pre-commit-config.yaml | 2 +- CHANGELOG.md | 29 +++--- Containerfile | 10 +- README.md | 37 +++---- bin/sdlc | 119 ++++++++++++++++------- sdlc | 8 +- tests/test_container_stack.sh | 18 ++++ tests/test_host_contract.sh | 94 +++++++++++++----- 11 files changed, 337 insertions(+), 137 deletions(-) diff --git a/.github/workflows/container.yml b/.github/workflows/container.yml index 0d1f388..e6dee8f 100644 --- a/.github/workflows/container.yml +++ b/.github/workflows/container.yml @@ -80,7 +80,7 @@ jobs: digest: ${{ steps.push.outputs.digest || steps.resolve.outputs.digest }} ref: ${{ steps.push.outputs.ref || steps.resolve.outputs.ref }} steps: - - uses: actions/checkout@v7 + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false @@ -89,7 +89,7 @@ jobs: run: | set -eu podman run --rm -v "$PWD:/mnt:ro,z" -w /mnt \ - docker.io/koalaman/shellcheck:v0.11.0 bin/sdlc tests/contract.sh \ + docker.io/koalaman/shellcheck:v0.11.0@sha256:61862eba1fcf09a484ebcc6feea46f1782532571a34ed51fedf90dd25f925a8d bin/sdlc tests/contract.sh \ tests/test_host_contract.sh tests/test_container_stack.sh scripts/release-version.sh tests/test_host_contract.sh tests/test_release_version.py @@ -109,6 +109,7 @@ jobs: # shellcheck disable=SC2086 podman build $args -f Containerfile \ --build-arg "REVISION=$GITHUB_SHA" \ + --build-arg "RUN_ID=$GITHUB_RUN_ID" \ --build-arg "CREATED=$(date -u +%Y-%m-%dT%H:%M:%SZ)" \ -t "localhost/python-sdlc:$GITHUB_SHA" . @@ -138,42 +139,25 @@ jobs: if: env.IS_TAG != 'true' run: tests/test_container_stack.sh "localhost/python-sdlc:$GITHUB_SHA" - # The inner loop: a pull request's tested image, runnable before any - # release carries it, with SDLC_IMAGE=pr- or [tool.sdlc] image. It - # goes to the fleet name only, as `:pr-`, and as `:beta` when the PR - # carries the `beta` label -- whichever labelled PR pushed last holds it. - # - # These two tags and no others. `latest`, `main` and the version tags - # belong to `promote`, which never runs on a pull request; nothing here - # can reach them. A fork's pull request has no secrets and Dependabot's - # has its own, so both are skipped rather than failed. - - name: Publish the pull request's image + # The inner loop's image leaves this job as a file, never as a push: + # this job has run the pull request's own code, so it holds no registry + # credential. `publish-pr` pushes it, running none of that code. + - name: Save the image for publish-pr if: >- github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name == github.repository && github.event.pull_request.user.login != 'dependabot[bot]' - env: - QUAY_USERNAME: ${{ secrets.QUAY_USERNAME }} - QUAY_PASSWORD: ${{ secrets.QUAY_PASSWORD }} - PR: ${{ github.event.pull_request.number }} - BETA: ${{ contains(github.event.pull_request.labels.*.name, 'beta') }} - run: | - set -eu - podman login -u "$QUAY_USERNAME" -p "$QUAY_PASSWORD" quay.io - podman push "localhost/python-sdlc:$GITHUB_SHA" "$SDLC:pr-$PR" - digest=$(skopeo inspect --format '{{.Digest}}' "docker://$SDLC:pr-$PR") - [ -n "$digest" ] || { echo "the registry reported no digest" >&2; exit 1; } - echo "published $SDLC:pr-$PR ($digest)" - # What the registry holds is tested, not just what was built here, - # as the profile requires of every pushed digest -- and before - # `:beta`, which other repos run, can move onto it. - tests/contract.sh "$SDLC@$digest" - if [ "$BETA" = true ]; then - skopeo copy --all --preserve-digests "docker://$SDLC@$digest" "docker://$SDLC:beta" - got=$(skopeo inspect --format '{{.Digest}}' "docker://$SDLC:beta") - [ "$got" = "$digest" ] || { echo "$SDLC:beta is $got, not $digest" >&2; exit 1; } - echo "published $SDLC:beta ($digest)" - fi + run: podman save -o "$RUNNER_TEMP/sdlc-pr.tar" "localhost/python-sdlc:$GITHUB_SHA" + - name: Upload the image for publish-pr + if: >- + github.event_name == 'pull_request' + && github.event.pull_request.head.repo.full_name == github.repository + && github.event.pull_request.user.login != 'dependabot[bot]' + uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 + with: + name: sdlc-pr-image + path: ${{ runner.temp }}/sdlc-pr.tar + retention-days: 1 - name: Log in to Quay if: github.event_name != 'pull_request' @@ -230,6 +214,95 @@ jobs: echo "ref=$QUAY@$digest" >> "$GITHUB_OUTPUT" echo "promoting the already-built $QUAY@$digest" + # The inner loop: a pull request's tested image, runnable before any release + # carries it, with SDLC_IMAGE=pr- or SDLC_IMAGE=beta on a developer's + # machine. It goes to a STAGING repository, quay.io/crunchtools/sdlc-pr, as + # `:pr-`, and as `:beta` when the PR carries the `beta` label. + # + # A job of its own, because `build` ran the pull request's code, and code + # can leave things behind for later steps of its job (GITHUB_PATH, a podman + # or skopeo of its own). This job checks out nothing and runs nothing from + # the PR: it loads the image file `build` saved and pushes it. Its robot can + # write sdlc-pr and nothing else, so `latest`, `main` and the version tags of + # the real images are out of its reach, not merely out of its script. + # + # The pushed bytes are checked against the image `build` tested by config + # digest -- which is the image ID -- since running the contract test here + # would mean running the PR's code beside the credential. It goes to a + # scratch tag first and only a verified digest is tagged. A re-run of an + # older run is refused rather than moving a tag backwards: each image carries + # the run id that built it, and a re-run keeps its run's id. + publish-pr: + name: Publish the pull request's image + needs: build + if: >- + github.event_name == 'pull_request' + && github.event.pull_request.head.repo.full_name == github.repository + && github.event.pull_request.user.login != 'dependabot[bot]' + runs-on: ubuntu-latest + environment: sdlc-pr + permissions: + actions: read + env: + STAGE: quay.io/crunchtools/sdlc-pr + steps: + - uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2 + with: + name: sdlc-pr-image + path: ${{ runner.temp }} + + - name: Push, verify, then tag + env: + QUAY_PR_USERNAME: ${{ secrets.QUAY_PR_USERNAME }} + QUAY_PR_PASSWORD: ${{ secrets.QUAY_PR_PASSWORD }} + PR: ${{ github.event.pull_request.number }} + BETA: ${{ contains(github.event.pull_request.labels.*.name, 'beta') }} + run: | + set -eu + # Until the staging robot exists the image simply is not published. + # A warning rather than a failure: this is a convenience, not a gate. + if [ -z "$QUAY_PR_USERNAME" ] || [ -z "$QUAY_PR_PASSWORD" ]; then + echo "::warning::QUAY_PR_USERNAME / QUAY_PR_PASSWORD are not set for the sdlc-pr environment; $STAGE:pr-$PR was not published" + exit 0 + fi + ref="localhost/python-sdlc:$GITHUB_SHA" + podman load -q -i "$RUNNER_TEMP/sdlc-pr.tar" + id=$(podman image inspect --format '{{.Id}}' "$ref") + built_by=$(podman image inspect --format '{{ index .Labels "com.crunchtools.sdlc.run-id" }}' "$ref") + [ "$built_by" = "$GITHUB_RUN_ID" ] \ + || { echo "$ref was built by run $built_by, not this run ($GITHUB_RUN_ID)" >&2; exit 1; } + podman login -u "$QUAY_PR_USERNAME" -p "$QUAY_PR_PASSWORD" quay.io + + # Never backwards: a tag built by a later run stays where it is. + guard() { + current=$(skopeo inspect --format '{{ index .Labels "com.crunchtools.sdlc.run-id" }}' \ + "docker://$STAGE:$1" 2>/dev/null || true) + case "$current" in + ''|*[!0-9]*) return 0 ;; + esac + if [ "$current" -gt "$GITHUB_RUN_ID" ]; then + echo "refusing: $STAGE:$1 was built by run $current, later than this run ($GITHUB_RUN_ID)" >&2 + exit 1 + fi + } + + scratch="pr-$PR-candidate" + podman push "$ref" "$STAGE:$scratch" + digest=$(skopeo inspect --format '{{.Digest}}' "docker://$STAGE:$scratch") + config=$(skopeo inspect --raw "docker://$STAGE@$digest" | jq -r .config.digest) + [ "$config" = "sha256:$id" ] \ + || { echo "$STAGE@$digest holds config $config, not the tested image sha256:$id" >&2; exit 1; } + + tag() { + guard "$1" + skopeo copy --all --preserve-digests "docker://$STAGE@$digest" "docker://$STAGE:$1" + got=$(skopeo inspect --format '{{.Digest}}' "docker://$STAGE:$1") + [ "$got" = "$digest" ] || { echo "$STAGE:$1 is $got, not $digest" >&2; exit 1; } + echo "published $STAGE:$1 ($digest)" + } + tag "pr-$PR" + if [ "$BETA" = true ]; then tag beta; fi + # Re-runs the contract test against what is ACTUALLY on the registry, not # against the local build. The profile requires this on every trigger: a # rebuild changes packages without a commit, and a digest nobody tested is a @@ -242,7 +315,7 @@ jobs: permissions: contents: read steps: - - uses: actions/checkout@v7 + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - name: Contract test against the pushed digest @@ -268,7 +341,7 @@ jobs: steps: # Full history: the backwards-move guard below asks git whether the # currently published commit already contains this one. - - uses: actions/checkout@v7 + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false fetch-depth: 0 @@ -376,13 +449,13 @@ jobs: group: bump-consumers-python-sdlc cancel-in-progress: false steps: - - uses: actions/checkout@v7 + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - name: Mint CI token id: ci-token - uses: actions/create-github-app-token@v3 + uses: actions/create-github-app-token@64ab729a6294000f5aa4a4bc0b73d1f32bc45510 # v3.3.0 with: client-id: ${{ vars.CRUNCHTOOLS_CI_CLIENT_ID }} private-key: ${{ secrets.CRUNCHTOOLS_CI_APP_KEY }} diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 6aade4f..db3d4e5 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -25,7 +25,7 @@ jobs: permissions: contents: read steps: - - uses: actions/checkout@v7 + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - name: Is there an untagged version? id: version @@ -41,7 +41,7 @@ jobs: - name: Mint CI token if: steps.version.outputs.tag != '' id: ci-token - uses: actions/create-github-app-token@v3 + uses: actions/create-github-app-token@64ab729a6294000f5aa4a4bc0b73d1f32bc45510 # v3.3.0 with: client-id: ${{ vars.CRUNCHTOOLS_CI_CLIENT_ID }} private-key: ${{ secrets.CRUNCHTOOLS_CI_APP_KEY }} diff --git a/.github/workflows/update-tools.yml b/.github/workflows/update-tools.yml index f095cf9..56b136f 100644 --- a/.github/workflows/update-tools.yml +++ b/.github/workflows/update-tools.yml @@ -37,13 +37,13 @@ jobs: # merge itself. - name: Mint CI token id: ci-token - uses: actions/create-github-app-token@v3 + uses: actions/create-github-app-token@64ab729a6294000f5aa4a4bc0b73d1f32bc45510 # v3.3.0 with: client-id: ${{ vars.CRUNCHTOOLS_CI_CLIENT_ID }} private-key: ${{ secrets.CRUNCHTOOLS_CI_APP_KEY }} owner: crunchtools - - uses: actions/checkout@v7 + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: token: ${{ steps.ci-token.outputs.token }} diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 1264c78..7af1e6e 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -35,7 +35,7 @@ repos: hooks: - id: shellcheck name: shellcheck (bin/sdlc, the tests and scripts) - entry: podman --events-backend=none run --rm --log-driver=none -v .:/mnt:ro,z -w /mnt docker.io/koalaman/shellcheck:v0.11.0 bin/sdlc tests/contract.sh tests/test_host_contract.sh tests/test_container_stack.sh scripts/release-version.sh + entry: podman --events-backend=none run --rm --log-driver=none -v .:/mnt:ro,z -w /mnt docker.io/koalaman/shellcheck:v0.11.0@sha256:61862eba1fcf09a484ebcc6feea46f1782532571a34ed51fedf90dd25f925a8d bin/sdlc tests/contract.sh tests/test_host_contract.sh tests/test_container_stack.sh scripts/release-version.sh language: system pass_filenames: false always_run: true diff --git a/CHANGELOG.md b/CHANGELOG.md index 0546203..a14b36c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,27 +24,34 @@ All notable changes to this project are documented here, following diffing the output. - **The container stack**: `containerfile-lint` (hadolint 2.14.0, in push and ci) and, in ci only, `container-build`, `container-smoke` (the built image - either exits 0 or is still running after `seconds`, default 10, with no - network and no capabilities) and `container-scan` (grype 0.121.0 over the + starts detached with no network and no capabilities, and podman's report of + its state decides: still running after `seconds`, default 10, or exited 0, + passes; an entrypoint that cannot start fails) and `container-scan` (grype 0.121.0 over the built image's filesystem, failing on a fixable Critical by default; the repo's `.grype.yaml` applies). - `sdlc.toml`: the same `[tool.sdlc]` tables, for a repo with no `pyproject.toml`. Refused beside one. - **A pull request's image, before a release.** Every push to a pull request - here publishes `quay.io/crunchtools/sdlc:pr-`, and `:beta` when it has - the `beta` label; never `latest`, `main` or a version tag. `SDLC_IMAGE` - (a fleet tag such as `beta`, or any image reference) or - `[tool.sdlc] image = ""` runs one. Every run on anything but the pinned - release prints which image it is and what chose it, and a registry tag is - pulled when newer. With neither set, nothing changes. + here publishes its tested image to the staging repository + `quay.io/crunchtools/sdlc-pr` as `:pr-`, and `:beta` when it has the + `beta` label. A separate `publish-pr` job does this: it runs none of the + pull request's code, and its robot can write only `sdlc-pr`. A digest is + verified against the tested image before it is tagged, and a re-run of an + older run never moves a tag backwards. On a developer's machine, + `SDLC_IMAGE` or `[tool.sdlc] image = ""` runs one. CI and `sdlc ci` + refuse the repo key (exit 2). Every run on anything but the pinned release + prints which image it is and what chose it, and a registry tag is pulled + when newer. With neither set, nothing changes. - The image is also published as `quay.io/crunchtools/sdlc`: the same digest copied, never rebuilt, with the same tags as `python-sdlc`. ### Fixed -- `bin/sdlc`'s `run_host` kept its exit status and start time in `_rc` and - `_t0`, which `run_serial` also uses, so a host step could leak its own status - or shorten its check's recorded duration. It has its own names now. +- `bin/sdlc`'s `run_host` kept its exit status in `_rc`, which `run_serial` + also uses, so a host step could leak its own status. It has its own name now. +- The hadolint, grype and shellcheck images are pinned by digest, and every + `actions/*` step in `container.yml`, `release.yml` and `update-tools.yml` + by commit SHA. ## [0.7.5] - 2026-11-17 diff --git a/Containerfile b/Containerfile index 8cfd325..b1f9d17 100644 --- a/Containerfile +++ b/Containerfile @@ -26,8 +26,10 @@ FROM quay.io/crunchtools/gatehouse:${GATEHOUSE_VERSION} AS gatehouse # Distroless, binary only -- the documented way to get a pinned uv. FROM ghcr.io/astral-sh/uv:${UV_VERSION} AS uv # The container stack's two tools, each a static binary in a scratch image. -FROM ghcr.io/hadolint/hadolint:v${HADOLINT_VERSION} AS hadolint -FROM docker.io/anchore/grype:v${GRYPE_VERSION} AS grype +# Pinned by digest as well as tag: the digest is what is fetched, the tag says +# which release it is, and the contract test asserts the two agree. +FROM ghcr.io/hadolint/hadolint:v${HADOLINT_VERSION}@sha256:27086352fd5e1907ea2b934eb1023f217c5ae087992eb59fde121dce9c9ff21e AS hadolint +FROM docker.io/anchore/grype:v${GRYPE_VERSION}@sha256:621d56d1d68106b787022c81546f7e0a36202a25d1239f0c20195fab8c16f7ea AS grype FROM quay.io/hummingbird/python:3.14-builder @@ -76,6 +78,10 @@ ARG REVISION=unknown ARG CREATED=unknown LABEL org.opencontainers.image.revision="${REVISION}" LABEL org.opencontainers.image.created="${CREATED}" +# The Actions run that built it. A pull request's staging tags compare it to +# refuse a re-run of an older run moving them backwards. +ARG RUN_ID=unknown +LABEL com.crunchtools.sdlc.run-id="${RUN_ID}" # The same values the labels carry, visible to the running script so `sdlc # versions` reports what is actually installed rather than a second list diff --git a/README.md b/README.md index 8f65f3f..5be5ce1 100644 --- a/README.md +++ b/README.md @@ -138,7 +138,7 @@ refused. |---|---|---| | `containerfile-lint` | push, ci | hadolint over the Containerfile; the repo's `.hadolint.yaml` applies, and without one the threshold is `warning` | | `container-build` | ci | `podman build`, at the tag the next two read | -| `container-smoke` | ci | runs the built image with no network and no capabilities: exiting 0, or still running after `seconds`, passes | +| `container-smoke` | ci | starts the built image detached, with no network and no capabilities, and reads its state from podman: still running after `seconds`, or exited 0, passes; any other exit, the OOM killer, or an entrypoint that cannot start (126/127) fails | | `container-scan` | ci | grype over the built image's filesystem, mounted read-only by podman; fails on a fixable Critical by default, and the repo's `.grype.yaml` applies | Build, smoke and scan are one lane, beside everything else. @@ -333,23 +333,28 @@ new kind here, so the config stays something a person can read. ## Running a pull request's image A pull request's image can be run before any release carries it. Every push to -a pull request in this repo publishes the tested image as -`quay.io/crunchtools/sdlc:pr-`, and as `:beta` too when the pull request -has the `beta` label. Whichever labelled pull request pushed last holds -`:beta`. Only a release moves `latest`, `main` or a version tag. +a pull request in this repo publishes its tested image to a **staging** +repository, `quay.io/crunchtools/sdlc-pr`, as `:pr-`, and as `:beta` too +when the pull request has the `beta` label. Whichever labelled pull request ran +last holds `:beta`, and a re-run of an older run never moves either tag back. +The robot that publishes them can write `sdlc-pr` and nothing else, so only a +release moves `latest`, `main` or a version tag of the real images. -To run one, first matching wins: +To run one on your machine, first matching wins: -| How | Scope | Value | -|---|---|---| -| `SDLC_IMAGE=pr-59 git push` | this run, this machine | a tag of `quay.io/crunchtools/sdlc`, or a full image reference | -| `[tool.sdlc] image = "beta"` | every run of this repo, CI included | a tag of `quay.io/crunchtools/sdlc` only, written on its own line under `[tool.sdlc]` | - -A run on anything but the pinned release says so at the top of every command, -naming the image and what chose it. A registry tag is pulled when newer, so -`:beta` is the current `:beta`, not whatever was cached. A repo's config can -only name a tag of the fleet image, never an arbitrary image to judge itself -with. Take `image` out before merging: CI would run it too. +| How | Value | +|---|---| +| `SDLC_IMAGE=pr-59 git push` | `beta` or `pr-` is that tag of `sdlc-pr`; any other bare tag is a release of `quay.io/crunchtools/sdlc`; or a full image reference | +| `[tool.sdlc] image = "beta"` | the same bare tags, written on its own line under `[tool.sdlc]` | + +Every command on anything but the pinned release prints which image it is and +what chose it. A registry tag is pulled when newer, so `:beta` is the current +`:beta`, not whatever was cached. + +**CI never honours `[tool.sdlc] image`.** Under `CI` or `GITHUB_ACTIONS`, and +for `sdlc ci` anywhere, the key is refused with exit 2. A pull request must not +choose the image that judges it, and only the launcher can enforce that: the +planner runs inside whichever image was chosen. **Nothing built on a laptop or a seat is ever pushed.** `sdlc` builds images only to test them (`container-build` tags `localhost/:sdlc`) and has no diff --git a/bin/sdlc b/bin/sdlc index 5eca00b..424c153 100755 --- a/bin/sdlc +++ b/bin/sdlc @@ -47,8 +47,11 @@ IMAGE_TAG=$(tr -d ' \t\n\r' < "$HOOKS_DIR/IMAGE_TAG") # and the image together and they cannot disagree. An override -- below, once # the repo is known -- replaces it for the inner loop, and says so every run. DEFAULT_IMAGE=quay.io/crunchtools/python-sdlc:$IMAGE_TAG -# Where CI publishes a pull request's tested image, as `:pr-` and `:beta`. +# Releases are published as the fleet image. A pull request's tested image goes +# to a staging repository instead, as `:pr-` and `:beta`, written by a robot +# that can write nothing else. FLEET_IMAGE=quay.io/crunchtools/sdlc +STAGING_IMAGE=quay.io/crunchtools/sdlc-pr # --------------------------------------------------------------- preconditions @@ -72,11 +75,14 @@ PROJECT=$(basename "$REPO") # The inner loop: run a pull request's image, `:pr-`, or the one labelled # `beta`, before any release carries it. Two ways in, the first winning: # -# SDLC_IMAGE= | this seat, this run. A bare tag means a tag of -# quay.io/crunchtools/sdlc, so SDLC_IMAGE=beta. -# [tool.sdlc] image = "" the repo, every run. A TAG of the fleet image -# and nothing else: a repo's config must not be -# able to name whatever image judges it. +# SDLC_IMAGE= | this seat, this run. A bare `beta` or `pr-` +# is that tag of quay.io/crunchtools/sdlc-pr; any +# other bare tag is a release of the fleet image. +# [tool.sdlc] image = "" the repo, on a seat. The same bare tags and +# nothing else, and REFUSED in CI and by `sdlc +# ci`: a pull request must not choose the image +# that judges it. The planner cannot enforce +# that, because it runs inside the chosen image. # # The image is the judge, so a run that is not on the pinned release says so on # every invocation, loudly. And a floating tag is pulled when newer: `missing`, @@ -93,14 +99,20 @@ config_image() { # the `image` key of [tool.sdlc], or nothing print v; exit }' "$CONFIG_FILE" } -fleet_tag() { # value where -> the fleet image at that tag, or die +fleet_tag() { # value where -> the image that tag names, or die case "$1" in - ''|*[!A-Za-z0-9._-]*|[.-]*) die "$2 must be a tag of $FLEET_IMAGE such as beta or pr-59; got '$1'" ;; + ''|*[!A-Za-z0-9._-]*|[.-]*) die "$2 must be a tag such as beta, pr-59 or 0.8.0; got '$1'" ;; + beta|pr-[0-9]*) printf '%s:%s' "$STAGING_IMAGE" "$1" ;; + *) printf '%s:%s' "$FLEET_IMAGE" "$1" ;; esac - printf '%s:%s' "$FLEET_IMAGE" "$1" } SDLC_CONFIG_IMAGE=$(config_image) export SDLC_CONFIG_IMAGE +if [ -n "$SDLC_CONFIG_IMAGE" ]; then + if [ -n "${CI:-}" ] || [ -n "${GITHUB_ACTIONS:-}" ] || [ "${1:-}" = ci ]; then + die "[tool.sdlc] image is for a developer's machine. CI and 'sdlc ci' run the pinned release, so a pull request cannot choose the image that judges it. Remove it from $CONFIG_FILE" + fi +fi IMAGE=$DEFAULT_IMAGE IMAGE_FROM= if [ -n "${SDLC_IMAGE:-}" ]; then @@ -529,11 +541,62 @@ record_finish() { # worst status TAB=$(printf '\t') US=$(printf '\037') +run_up() { # seconds run-args -> the smoke verdict + # A `podman run` that may be a service. It starts detached, and its STATE is + # asked once a second for up to : still running at the end passes, + # and so does exiting 0; any other exit, or the OOM killer, is a finding. + # Asked of podman, never inferred from a clock or from podman run's own + # status, because 255 is both podman's timeout kill and a program's crash, + # and a clock started before `podman run` counts creation as uptime. + # + # Not --rm: the state is read after the container stops. Removed here, by + # name, however it ended. A k8s-file log so its output can be shown, which + # --log-driver=none would discard. + _upsecs=$1; shift + _cname=sdlc-smoke-$$-$(now_s) + _uprc=0 + podman --events-backend=none run -d --name "$_cname" --log-driver=k8s-file \ + --cap-drop=all --security-opt=no-new-privileges \ + --cpus "$GATE_CPUS" --memory "$GATE_MEMORY" --cpu-shares 256 \ + "$@" /dev/null || _uprc=$? + case "$_uprc" in + 0) ;; + # 125 is podman itself: the container never existed, nothing was judged. + 125) podman rm -f "$_cname" >/dev/null 2>&1 || : + printf '%s: podman could not create the container (exit 125); nothing was judged\n' "$ME" >&2 + return 2 ;; + # 126/127 is the image: an entrypoint that is missing or cannot execute, + # which is exactly what a smoke run exists to catch. + *) podman logs "$_cname" 2>&1 || : + podman rm -f "$_cname" >/dev/null 2>&1 || : + printf '%s: the image could not start its entrypoint (exit %s)\n' "$ME" "$_uprc" >&2 + return 1 ;; + esac + _upi=0 + while [ "$_upi" -lt "$_upsecs" ]; do + _uprun=$(podman inspect --format '{{.State.Running}}' "$_cname" 2>/dev/null) || _uprun=gone + [ "$_uprun" = true ] || break + sleep 1 + _upi=$((_upi + 1)) + done + _upstate=$(podman inspect --format '{{.State.Running}} {{.State.ExitCode}} {{.State.OOMKilled}}' \ + "$_cname" 2>/dev/null) || _upstate= + podman logs "$_cname" 2>&1 || : + podman rm -f "$_cname" >/dev/null 2>&1 || : + case "$_upstate" in + 'true '*) printf 'still running after %ss: the image starts and stays up\n' "$_upsecs"; return 0 ;; + *' true') printf '%s: the container was killed for memory\n' "$ME" >&2; return 1 ;; + 'false 0 '*) return 0 ;; + 'false '*) printf '%s: the container exited %s within %ss of starting\n' "$ME" \ + "$(printf '%s' "$_upstate" | cut -d' ' -f2)" "$_upsecs" >&2; return 1 ;; + *) printf '%s: podman could not report the container state; nothing was judged\n' "$ME" >&2 + return 2 ;; + esac +} + run_host() { # argv [seconds] -> the step's verdict - # With seconds, the step is a `podman run` that may be a service: it is given - # that long under --timeout, and still running when it is up is a pass, as - # is exiting 0. podman reports its own timeout kill as 255, which a crashing - # program can also return, so the clock decides which one it was. + # With seconds, the step is a `podman run` that may be a service: run_up + # judges it by its state. _up=${2:-} _ifs=$IFS IFS=$US @@ -551,38 +614,24 @@ run_host() { # argv [seconds] -> the step's verdict [ "$_w" = @sdlc-image ] && _w=$IMAGE set -- "$@" "$_w" done + if [ -n "$_up" ]; then + [ "$_sub" = run ] || die "only a podman run can be given a time to stay up" + run_up "$_up" "$@" + return + fi # The budget is applied here so it is defined once, whatever the step. case "$_sub" in build) set -- build --cpu-period 100000 --cpu-quota "$(( GATE_CPUS * 100000 ))" \ --memory "$GATE_MEMORY" "$@" ;; run) set -- --events-backend=none run --log-driver=none \ --cap-drop=all --security-opt=no-new-privileges \ - --cpus "$GATE_CPUS" --memory "$GATE_MEMORY" --cpu-shares 256 \ - ${_up:+--timeout "$_up"} "$@" ;; + --cpus "$GATE_CPUS" --memory "$GATE_MEMORY" --cpu-shares 256 "$@" ;; *) die "a host step may only build or run; refused: podman $_sub" ;; esac - [ -z "$_up" ] || [ "$_sub" = run ] || die "only a podman run can be given a time to stay up" - _ht0=$(now_s) - # Not `_rc` or `_t0`: shell variables are global, and run_serial reads its - # own after this returns. A service's 255 that this turns into a pass would - # survive into the caller as the step's failure, and a later start time would - # shorten the check's recorded duration. + # Not `_rc`: shell variables are global, and run_serial reads its own after + # this returns, so a status set here would survive into the caller. _hrc=0 podman "$@" &2 - return 2 ;; - esac - if [ "$_hrc" -eq 255 ] && [ $(( $(now_s) - _ht0 )) -ge "$_up" ]; then - printf 'still running after %ss: the image starts and stays up\n' "$_up" - return 0 - fi - printf '%s: the container exited %s within %ss of starting\n' "$ME" "$_hrc" "$_up" >&2 - return 1 - fi # A failed build is a finding: the code does not produce an image. A run # that podman could not start (125-127) never ran, so it is a 2, and so is a # pytest status above 1 (interrupted, usage error, internal error, no tests). diff --git a/sdlc b/sdlc index 9dcbce5..9214ecf 100755 --- a/sdlc +++ b/sdlc @@ -418,10 +418,7 @@ def config_image(cfg: dict) -> None: return tag = cfg["image"] if not isinstance(tag, str) or not IMAGE_TAG_RE.fullmatch(tag): - cannot( - "[tool.sdlc] image must be a tag of quay.io/crunchtools/sdlc, " - f"such as beta or pr-59; got {tag!r}" - ) + cannot(f"[tool.sdlc] image must be a tag such as beta, pr-59 or 0.8.0; got {tag!r}") if "SDLC_HOST_REPO" in os.environ and os.environ.get("SDLC_CONFIG_IMAGE") != tag: cannot( f"[tool.sdlc] image is {tag!r} but bin/sdlc could not read it, so it " @@ -954,7 +951,8 @@ def check_container_smoke(cfg: dict, name: str, s: dict) -> list[tuple[str, str, or not 1 <= seconds <= SMOKE_SECONDS_MAX ): cannot(f"{name}: seconds must be a whole number from 1 to {SMOKE_SECONDS_MAX}") - argv = ["podman", "run", "--rm", "--pull=never"] + # No --rm: bin/sdlc reads the stopped container's state, then removes it. + argv = ["podman", "run", "--pull=never"] if not s.get("network", False): argv += ["--network", "none"] for key, value in table(s, "env").items(): diff --git a/tests/test_container_stack.sh b/tests/test_container_stack.sh index 3850f69..ce20b3f 100755 --- a/tests/test_container_stack.sh +++ b/tests/test_container_stack.sh @@ -62,6 +62,24 @@ else fi fi +# An entrypoint that does not exist: podman's own 127. That is the image's +# defect -- exactly what a smoke run is for -- so a finding, not a 2. +fixture noentry '["/no/such/entrypoint"]' +rc=0 +(cd "$WORK/noentry" && SDLC_IMAGE=$IMAGE "$HERE/bin/sdlc" container-build) > "$WORK/build.out" 2>&1 || rc=$? +if [ "$rc" -ne 0 ]; then + bad "the missing-entrypoint fixture did not build (exit $rc)" +else + rc=0 + (cd "$WORK/noentry" && SDLC_IMAGE=$IMAGE "$HERE/bin/sdlc" container-smoke) > "$WORK/noentry.out" 2>&1 || rc=$? + if [ "$rc" -eq 1 ] && grep -q 'could not start its entrypoint' "$WORK/noentry.out"; then + ok "a missing entrypoint fails container-smoke (exit 1)" + else + bad "container-smoke on a missing entrypoint exited $rc, want 1" + tail -10 "$WORK/noentry.out" | sed 's/^/ /' + fi +fi + printf '\n' [ "$FAILED" -eq 0 ] && { printf 'container stack PASSED\n'; exit 0; } printf 'container stack FAILED: %s check(s)\n' "$FAILED"; exit 1 diff --git a/tests/test_host_contract.sh b/tests/test_host_contract.sh index 6bc9fcc..5ce3712 100755 --- a/tests/test_host_contract.sh +++ b/tests/test_host_contract.sh @@ -81,6 +81,27 @@ cat > "$SHIM/podman" <> "$LOG" for _last in "\$@"; do :; done +# container-smoke: a detached run records which case it is, and inspect +# answers that case's state as: Running ExitCode OOMKilled. +case " \$* " in + *" inspect "*) + _st=\$(cat "$SHIM/upcase" 2>/dev/null) + case "\$_st" in + go-up-stays) _full='true 0 false' ;; + go-up-exits-0) _full='false 0 false' ;; + go-up-dies-255) _full='false 255 false' ;; + go-up-dies-3) _full='false 3 false' ;; + go-up-oom) _full='false 137 true' ;; + *) exit 125 ;; + esac + case " \$* " in + *ExitCode*) printf '%s\n' "\$_full" ;; + *) printf '%s\n' "\${_full%% *}" ;; + esac + exit 0 ;; + *" logs "*) printf 'CONTAINER LOG\n'; exit 0 ;; + *" rm -f "*) exit 0 ;; +esac case " \$* " in *" plan gatehouse-direct "*) printf 'gatehouse${TAB}main${TAB}local${TAB}gatehouse\n' ;; @@ -126,7 +147,7 @@ case " \$* " in if [ "\$_what" = build ]; then printf '%s${TAB}main${TAB}host-up${TAB}1${US}podman${US}build${US}-t${US}x${US}.\n' "\$_last" else - printf '%s${TAB}main${TAB}host-up${TAB}%s${US}podman${US}run${US}--rm${US}localhost/x:sdlc${US}go-%s\n' \ + printf '%s${TAB}main${TAB}host-up${TAB}%s${US}podman${US}run${US}localhost/x:sdlc${US}go-%s\n' \ "\$_last" "\$_secs" "\$_last" fi ;; *" plan "*) @@ -142,11 +163,9 @@ case "\$_last" in printf 'START %s\n' "\$_last" >> "$SHIM/race.log" printf 'BEGIN %s\n' "\$_last"; sleep 1; printf 'END %s\n' "\$_last" printf 'DONE %s\n' "\$_last" >> "$SHIM/race.log" ;; - go-up-stays) sleep 1; exit 255 ;; - go-up-dies-255) exit 255 ;; - go-up-dies-3) exit 3 ;; - go-up-exits-0) exit 0 ;; - go-up-podman) exit 125 ;; + go-up-*) + printf '%s\n' "\$_last" > "$SHIM/upcase" + case "\$_last" in go-up-podman) exit 125 ;; go-up-127) exit 127 ;; esac ;; rc-one) printf 'RAN rc-one\n'; exit 1 ;; rc-two) printf 'RAN rc-two\n'; exit 2 ;; rc-three) printf 'RAN rc-three\n'; exit 3 ;; @@ -206,12 +225,13 @@ fi printf '\n== a run given time to stay up\n\n' -# container-smoke's step: `host-up`, seconds and then a podman run. podman -# reports its own --timeout kill as 255, which a crashing program can also -# return, so the clock decides: 255 after the full time is a service that -# stayed up, 255 before it is a crash. -for case_ in up-stays:0 up-exits-0:0 up-dies-255:1 up-dies-3:1 up-podman:2 \ - up-build:2 up-noseconds:2; do +# container-smoke's step: `host-up`, seconds and then a podman run. The +# container starts detached and its STATE decides: still running, or exited 0, +# passes; any other exit, the OOM killer, or an entrypoint that cannot start +# (126/127) is a finding; podman failing to create it (125) is a 2. A 255 is +# just another exit -- no clock is consulted. +for case_ in up-stays:0 up-exits-0:0 up-dies-255:1 up-dies-3:1 up-oom:1 up-127:1 \ + up-podman:2 up-build:2 up-noseconds:2; do name=${case_%:*}; want=${case_#*:} log_mark PATH="$SHIM" sh bin/sdlc "$name" > "$RUN/out" 2>&1 && rc=0 || rc=$? @@ -225,10 +245,11 @@ done log_mark PATH="$SHIM" sh bin/sdlc up-stays >/dev/null 2>&1 || true log_since -if grep -q -- '--cap-drop=all .*--timeout 1 --rm localhost/x:sdlc go-up-stays' "$RUN/podman.log"; then - ok "host-up runs locked down, under --timeout" +if grep -q -- 'run -d --name sdlc-smoke-[0-9-]* --log-driver=k8s-file --cap-drop=all --security-opt=no-new-privileges .* localhost/x:sdlc go-up-stays$' "$RUN/podman.log" \ + && grep -q -- '^rm -f sdlc-smoke-' "$RUN/podman.log"; then + ok "host-up runs detached and locked down, and removes the container" else - bad "host-up did not pass --timeout with the lock-down: $(grep up-stays "$RUN/podman.log" | tail -1)" + bad "host-up's run or cleanup is wrong: $(grep -E 'up-stays|rm -f' "$RUN/podman.log" | tail -2)" fi printf '\n== a check deferred to CI\n\n' @@ -686,9 +707,9 @@ fi log_mark SDLC_IMAGE=beta PATH="$SHIM" sh bin/sdlc tiers > "$RUN/out" 2>&1 || true log_since -if last_run | grep -q -- '--pull=newer .* quay.io/crunchtools/sdlc:beta tiers$' \ +if last_run | grep -q -- '--pull=newer .* quay.io/crunchtools/sdlc-pr:beta tiers$' \ && grep -q 'NOT THE PINNED RELEASE' "$RUN/out" && grep -q 'set by SDLC_IMAGE' "$RUN/out"; then - ok "SDLC_IMAGE=beta runs quay.io/crunchtools/sdlc:beta, pulled when newer, and says so" + ok "SDLC_IMAGE=beta runs the staging quay.io/crunchtools/sdlc-pr:beta, pulled when newer, and says so" else bad "SDLC_IMAGE=beta: $(last_run) / $(head -3 "$RUN/out")" fi @@ -714,30 +735,53 @@ CREPO=$(mktemp -d) git -C "$CREPO" init -q printf '[project]\nname = "x"\n\n[tool.sdlc]\nimage = "pr-59"\n' > "$CREPO/pyproject.toml" log_mark -(cd "$CREPO" && PATH="$SHIM" sh "$OLDPWD/bin/sdlc" tiers) > "$RUN/out" 2>&1 || true +(cd "$CREPO" && CI='' GITHUB_ACTIONS='' PATH="$SHIM" sh "$OLDPWD/bin/sdlc" tiers) > "$RUN/out" 2>&1 || true log_since -if last_run | grep -q -- 'SDLC_CONFIG_IMAGE .*quay.io/crunchtools/sdlc:pr-59 tiers$' \ +if last_run | grep -q -- 'SDLC_CONFIG_IMAGE .*quay.io/crunchtools/sdlc-pr:pr-59 tiers$' \ && grep -q 'set by \[tool.sdlc\] image in pyproject.toml' "$RUN/out"; then ok "[tool.sdlc] image = \"pr-59\" runs that tag and tells the image what it read" else bad "[tool.sdlc] image: $(last_run) / $(head -4 "$RUN/out")" fi log_mark -(cd "$CREPO" && SDLC_IMAGE=beta PATH="$SHIM" sh "$OLDPWD/bin/sdlc" tiers) > "$RUN/out" 2>&1 || true +(cd "$CREPO" && CI='' GITHUB_ACTIONS='' SDLC_IMAGE=beta PATH="$SHIM" sh "$OLDPWD/bin/sdlc" tiers) > "$RUN/out" 2>&1 || true log_since -if last_run | grep -q -- 'quay.io/crunchtools/sdlc:beta tiers$'; then +if last_run | grep -q -- 'quay.io/crunchtools/sdlc-pr:beta tiers$'; then ok "SDLC_IMAGE beats [tool.sdlc] image" else bad "SDLC_IMAGE did not beat the repo's image: $(last_run)" fi +# The image is the judge, so a pull request must not pick it: CI and `sdlc ci` +# refuse the repo's key outright. The planner cannot hold this line, because +# it runs inside whichever image was chosen. +for how in 'CI=true' 'GITHUB_ACTIONS=true' 'ci'; do + rc=0 + case "$how" in + ci) (cd "$CREPO" && CI='' GITHUB_ACTIONS='' PATH="$SHIM" sh "$OLDPWD/bin/sdlc" ci) > "$RUN/out" 2>&1 || rc=$? ;; + *) (cd "$CREPO" && CI='' GITHUB_ACTIONS='' env "$how" PATH="$SHIM" sh "$OLDPWD/bin/sdlc" tiers) > "$RUN/out" 2>&1 || rc=$? ;; + esac + if [ "$rc" -eq 2 ] && grep -q 'cannot choose the image that judges it' "$RUN/out"; then + ok "[tool.sdlc] image is refused under $how (exit 2)" + else + bad "[tool.sdlc] image under $how exited $rc" + fi +done +log_mark +(cd "$CREPO" && CI='' GITHUB_ACTIONS='' SDLC_IMAGE=0.8.0 PATH="$SHIM" sh "$OLDPWD/bin/sdlc" tiers) > "$RUN/out" 2>&1 || true +log_since +if last_run | grep -q -- 'quay.io/crunchtools/sdlc:0.8.0 tiers$'; then + ok "a bare release tag is the fleet image, not staging" +else + bad "SDLC_IMAGE=0.8.0: $(last_run)" +fi printf '[tool.sdlc]\nimage = "../../evil"\n' > "$CREPO/sdlc.toml" rm "$CREPO/pyproject.toml" rc=0 -(cd "$CREPO" && PATH="$SHIM" sh "$OLDPWD/bin/sdlc" tiers) > "$RUN/out" 2>&1 || rc=$? -if [ "$rc" -eq 2 ] && grep -q 'must be a tag of' "$RUN/out"; then +(cd "$CREPO" && CI='' GITHUB_ACTIONS='' PATH="$SHIM" sh "$OLDPWD/bin/sdlc" tiers) > "$RUN/out" 2>&1 || rc=$? +if [ "$rc" -eq 2 ] && grep -q 'must be a tag such as' "$RUN/out"; then ok "a repo's image that is not a plain tag is refused (exit 2)" else - bad "[tool.sdlc] image = ../../evil exited $rc" + bad "[tool.sdlc] image = ../../evil exited $rc: $(tail -2 "$RUN/out")" fi rm -rf "$CREPO" @@ -746,7 +790,7 @@ rm -rf "$CREPO" log_mark SDLC_IMAGE=beta PATH="$SHIM" sh bin/sdlc gate-word >/dev/null 2>&1 || true log_since -if grep -q -- '--cap-drop=all .* quay.io/crunchtools/sdlc:beta scan-rootfs$' "$RUN/podman.log" \ +if grep -q -- '--cap-drop=all .* quay.io/crunchtools/sdlc-pr:beta scan-rootfs$' "$RUN/podman.log" \ && ! grep -q '@sdlc-image' "$RUN/podman.log"; then ok "a host step's @sdlc-image is the image this run is on" else From daa02d5380024c0ad1ff404325fec500ad051e60 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 17 Nov 2026 19:05:00 +0000 Subject: [PATCH 09/11] refactor: no registry from pull requests; :main is the pre-release image Pull requests publish nothing: the publish-pr job, the sdlc-pr staging resolution, the QUAY_PR_* secrets and the run-id label are gone, and the pull_request trigger is back to its defaults. What has merged and not been released is quay.io/crunchtools/sdlc:main, which promote already moves on every push to main -- the digest verify-digest tested at :, read back after the copy, never onto an older commit. SDLC_IMAGE=main runs it. A release still moves :latest and the version tags. Seats iterate on local builds, which are never pushed. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01R7Y1jckWHhAgXg2WBwkWbb --- .github/workflows/container.yml | 112 -------------------------------- CHANGELOG.md | 20 +++--- Containerfile | 4 -- README.md | 32 +++++---- bin/sdlc | 20 +++--- sdlc | 6 +- tests/test_host_contract.sh | 34 ++++------ tests/test_plan.py | 6 +- 8 files changed, 51 insertions(+), 183 deletions(-) diff --git a/.github/workflows/container.yml b/.github/workflows/container.yml index e6dee8f..9270ff3 100644 --- a/.github/workflows/container.yml +++ b/.github/workflows/container.yml @@ -23,9 +23,7 @@ on: push: branches: [main] tags: ["v*"] - # `labeled` too, so adding the `beta` label publishes `:beta` without a push. pull_request: - types: [opened, synchronize, reopened, labeled] # Weekly, to pick up base-image errata. MANDATORY for this profile, and so is # bypassing the layer cache on it -- a cached `dnf install` layer is reused # while the Containerfile and base digest are unchanged, so a cached rebuild @@ -109,7 +107,6 @@ jobs: # shellcheck disable=SC2086 podman build $args -f Containerfile \ --build-arg "REVISION=$GITHUB_SHA" \ - --build-arg "RUN_ID=$GITHUB_RUN_ID" \ --build-arg "CREATED=$(date -u +%Y-%m-%dT%H:%M:%SZ)" \ -t "localhost/python-sdlc:$GITHUB_SHA" . @@ -139,26 +136,6 @@ jobs: if: env.IS_TAG != 'true' run: tests/test_container_stack.sh "localhost/python-sdlc:$GITHUB_SHA" - # The inner loop's image leaves this job as a file, never as a push: - # this job has run the pull request's own code, so it holds no registry - # credential. `publish-pr` pushes it, running none of that code. - - name: Save the image for publish-pr - if: >- - github.event_name == 'pull_request' - && github.event.pull_request.head.repo.full_name == github.repository - && github.event.pull_request.user.login != 'dependabot[bot]' - run: podman save -o "$RUNNER_TEMP/sdlc-pr.tar" "localhost/python-sdlc:$GITHUB_SHA" - - name: Upload the image for publish-pr - if: >- - github.event_name == 'pull_request' - && github.event.pull_request.head.repo.full_name == github.repository - && github.event.pull_request.user.login != 'dependabot[bot]' - uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 - with: - name: sdlc-pr-image - path: ${{ runner.temp }}/sdlc-pr.tar - retention-days: 1 - - name: Log in to Quay if: github.event_name != 'pull_request' env: @@ -214,95 +191,6 @@ jobs: echo "ref=$QUAY@$digest" >> "$GITHUB_OUTPUT" echo "promoting the already-built $QUAY@$digest" - # The inner loop: a pull request's tested image, runnable before any release - # carries it, with SDLC_IMAGE=pr- or SDLC_IMAGE=beta on a developer's - # machine. It goes to a STAGING repository, quay.io/crunchtools/sdlc-pr, as - # `:pr-`, and as `:beta` when the PR carries the `beta` label. - # - # A job of its own, because `build` ran the pull request's code, and code - # can leave things behind for later steps of its job (GITHUB_PATH, a podman - # or skopeo of its own). This job checks out nothing and runs nothing from - # the PR: it loads the image file `build` saved and pushes it. Its robot can - # write sdlc-pr and nothing else, so `latest`, `main` and the version tags of - # the real images are out of its reach, not merely out of its script. - # - # The pushed bytes are checked against the image `build` tested by config - # digest -- which is the image ID -- since running the contract test here - # would mean running the PR's code beside the credential. It goes to a - # scratch tag first and only a verified digest is tagged. A re-run of an - # older run is refused rather than moving a tag backwards: each image carries - # the run id that built it, and a re-run keeps its run's id. - publish-pr: - name: Publish the pull request's image - needs: build - if: >- - github.event_name == 'pull_request' - && github.event.pull_request.head.repo.full_name == github.repository - && github.event.pull_request.user.login != 'dependabot[bot]' - runs-on: ubuntu-latest - environment: sdlc-pr - permissions: - actions: read - env: - STAGE: quay.io/crunchtools/sdlc-pr - steps: - - uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2 - with: - name: sdlc-pr-image - path: ${{ runner.temp }} - - - name: Push, verify, then tag - env: - QUAY_PR_USERNAME: ${{ secrets.QUAY_PR_USERNAME }} - QUAY_PR_PASSWORD: ${{ secrets.QUAY_PR_PASSWORD }} - PR: ${{ github.event.pull_request.number }} - BETA: ${{ contains(github.event.pull_request.labels.*.name, 'beta') }} - run: | - set -eu - # Until the staging robot exists the image simply is not published. - # A warning rather than a failure: this is a convenience, not a gate. - if [ -z "$QUAY_PR_USERNAME" ] || [ -z "$QUAY_PR_PASSWORD" ]; then - echo "::warning::QUAY_PR_USERNAME / QUAY_PR_PASSWORD are not set for the sdlc-pr environment; $STAGE:pr-$PR was not published" - exit 0 - fi - ref="localhost/python-sdlc:$GITHUB_SHA" - podman load -q -i "$RUNNER_TEMP/sdlc-pr.tar" - id=$(podman image inspect --format '{{.Id}}' "$ref") - built_by=$(podman image inspect --format '{{ index .Labels "com.crunchtools.sdlc.run-id" }}' "$ref") - [ "$built_by" = "$GITHUB_RUN_ID" ] \ - || { echo "$ref was built by run $built_by, not this run ($GITHUB_RUN_ID)" >&2; exit 1; } - podman login -u "$QUAY_PR_USERNAME" -p "$QUAY_PR_PASSWORD" quay.io - - # Never backwards: a tag built by a later run stays where it is. - guard() { - current=$(skopeo inspect --format '{{ index .Labels "com.crunchtools.sdlc.run-id" }}' \ - "docker://$STAGE:$1" 2>/dev/null || true) - case "$current" in - ''|*[!0-9]*) return 0 ;; - esac - if [ "$current" -gt "$GITHUB_RUN_ID" ]; then - echo "refusing: $STAGE:$1 was built by run $current, later than this run ($GITHUB_RUN_ID)" >&2 - exit 1 - fi - } - - scratch="pr-$PR-candidate" - podman push "$ref" "$STAGE:$scratch" - digest=$(skopeo inspect --format '{{.Digest}}' "docker://$STAGE:$scratch") - config=$(skopeo inspect --raw "docker://$STAGE@$digest" | jq -r .config.digest) - [ "$config" = "sha256:$id" ] \ - || { echo "$STAGE@$digest holds config $config, not the tested image sha256:$id" >&2; exit 1; } - - tag() { - guard "$1" - skopeo copy --all --preserve-digests "docker://$STAGE@$digest" "docker://$STAGE:$1" - got=$(skopeo inspect --format '{{.Digest}}' "docker://$STAGE:$1") - [ "$got" = "$digest" ] || { echo "$STAGE:$1 is $got, not $digest" >&2; exit 1; } - echo "published $STAGE:$1 ($digest)" - } - tag "pr-$PR" - if [ "$BETA" = true ]; then tag beta; fi - # Re-runs the contract test against what is ACTUALLY on the registry, not # against the local build. The profile requires this on every trigger: a # rebuild changes packages without a commit, and a digest nobody tested is a diff --git a/CHANGELOG.md b/CHANGELOG.md index a14b36c..bdf232a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -31,17 +31,15 @@ All notable changes to this project are documented here, following repo's `.grype.yaml` applies). - `sdlc.toml`: the same `[tool.sdlc]` tables, for a repo with no `pyproject.toml`. Refused beside one. -- **A pull request's image, before a release.** Every push to a pull request - here publishes its tested image to the staging repository - `quay.io/crunchtools/sdlc-pr` as `:pr-`, and `:beta` when it has the - `beta` label. A separate `publish-pr` job does this: it runs none of the - pull request's code, and its robot can write only `sdlc-pr`. A digest is - verified against the tested image before it is tagged, and a re-run of an - older run never moves a tag backwards. On a developer's machine, - `SDLC_IMAGE` or `[tool.sdlc] image = ""` runs one. CI and `sdlc ci` - refuse the repo key (exit 2). Every run on anything but the pinned release - prints which image it is and what chose it, and a registry tag is pulled - when newer. With neither set, nothing changes. +- **Main before a release, as `:main`.** Every push to main publishes its + tested image as `quay.io/crunchtools/sdlc:main`: the digest the contract + test ran against, read back after the copy, never moved onto an older + commit. No pull request run touches a registry. On a developer's machine, + `SDLC_IMAGE=main` (or any tag or image reference) or + `[tool.sdlc] image = ""` runs it; CI and `sdlc ci` refuse the repo key + (exit 2). Every run on anything but the pinned release prints which image + it is and what chose it, and a registry tag is pulled when newer. With + neither set, nothing changes. - The image is also published as `quay.io/crunchtools/sdlc`: the same digest copied, never rebuilt, with the same tags as `python-sdlc`. diff --git a/Containerfile b/Containerfile index b1f9d17..b5f6ff7 100644 --- a/Containerfile +++ b/Containerfile @@ -78,10 +78,6 @@ ARG REVISION=unknown ARG CREATED=unknown LABEL org.opencontainers.image.revision="${REVISION}" LABEL org.opencontainers.image.created="${CREATED}" -# The Actions run that built it. A pull request's staging tags compare it to -# refuse a re-run of an older run moving them backwards. -ARG RUN_ID=unknown -LABEL com.crunchtools.sdlc.run-id="${RUN_ID}" # The same values the labels carry, visible to the running script so `sdlc # versions` reports what is actually installed rather than a second list diff --git a/README.md b/README.md index 5be5ce1..d955767 100644 --- a/README.md +++ b/README.md @@ -330,37 +330,35 @@ Values in `build_args`, `labels` and `env` may be `@project.version`, There is no shell kind. A repo that needs something these cannot express gets a new kind here, so the config stays something a person can read. -## Running a pull request's image +## Running main before a release -A pull request's image can be run before any release carries it. Every push to -a pull request in this repo publishes its tested image to a **staging** -repository, `quay.io/crunchtools/sdlc-pr`, as `:pr-`, and as `:beta` too -when the pull request has the `beta` label. Whichever labelled pull request ran -last holds `:beta`, and a re-run of an older run never moves either tag back. -The robot that publishes them can write `sdlc-pr` and nothing else, so only a -release moves `latest`, `main` or a version tag of the real images. +Every push to main publishes its tested image as `quay.io/crunchtools/sdlc:main`, +the same name git and Gourmand use: what has merged and not yet been released. +It is the digest the contract test ran against at `:`, read back after the +copy, and it never moves back onto an older commit. Only a release moves `latest` and the version tags, and no +pull request run touches a registry at all. The release is the gate. To run one on your machine, first matching wins: | How | Value | |---|---| -| `SDLC_IMAGE=pr-59 git push` | `beta` or `pr-` is that tag of `sdlc-pr`; any other bare tag is a release of `quay.io/crunchtools/sdlc`; or a full image reference | -| `[tool.sdlc] image = "beta"` | the same bare tags, written on its own line under `[tool.sdlc]` | +| `SDLC_IMAGE=main git push` | a bare tag of `quay.io/crunchtools/sdlc` (`main`, or a release such as `0.8.0`), or a full image reference | +| `[tool.sdlc] image = "main"` | a bare tag only, written on its own line under `[tool.sdlc]` | Every command on anything but the pinned release prints which image it is and -what chose it. A registry tag is pulled when newer, so `:beta` is the current -`:beta`, not whatever was cached. +what chose it. A registry tag is pulled when newer, so `:main` is the current +`:main`, not whatever was cached. **CI never honours `[tool.sdlc] image`.** Under `CI` or `GITHUB_ACTIONS`, and for `sdlc ci` anywhere, the key is refused with exit 2. A pull request must not choose the image that judges it, and only the launcher can enforce that: the planner runs inside whichever image was chosen. -**Nothing built on a laptop or a seat is ever pushed.** `sdlc` builds images -only to test them (`container-build` tags `localhost/:sdlc`) and has no -push step. Run a local build of this image with -`SDLC_IMAGE=localhost/python-sdlc:dev`. Only this repo's GitHub Actions -publish to quay.io. +**Nothing built on a laptop or a seat is ever pushed.** To try a change to +this image before it merges, build it locally (`podman build -t +localhost/python-sdlc:dev .`) and run it with +`SDLC_IMAGE=localhost/python-sdlc:dev`. `sdlc` has no push step, and only this +repo's GitHub Actions on main publish to quay.io. ## Skipping a check diff --git a/bin/sdlc b/bin/sdlc index 424c153..4e68a98 100755 --- a/bin/sdlc +++ b/bin/sdlc @@ -47,11 +47,9 @@ IMAGE_TAG=$(tr -d ' \t\n\r' < "$HOOKS_DIR/IMAGE_TAG") # and the image together and they cannot disagree. An override -- below, once # the repo is known -- replaces it for the inner loop, and says so every run. DEFAULT_IMAGE=quay.io/crunchtools/python-sdlc:$IMAGE_TAG -# Releases are published as the fleet image. A pull request's tested image goes -# to a staging repository instead, as `:pr-` and `:beta`, written by a robot -# that can write nothing else. +# The fleet image: releases at their version tags, and `:main`, which is what +# has merged to main and not yet been released. FLEET_IMAGE=quay.io/crunchtools/sdlc -STAGING_IMAGE=quay.io/crunchtools/sdlc-pr # --------------------------------------------------------------- preconditions @@ -72,12 +70,11 @@ PROJECT=$(basename "$REPO") # ------------------------------------------------------------- an image override # -# The inner loop: run a pull request's image, `:pr-`, or the one labelled -# `beta`, before any release carries it. Two ways in, the first winning: +# The inner loop: run what has merged to main before a release carries it, +# `:main`, or a seat's own local build. Two ways in, the first winning: # -# SDLC_IMAGE= | this seat, this run. A bare `beta` or `pr-` -# is that tag of quay.io/crunchtools/sdlc-pr; any -# other bare tag is a release of the fleet image. +# SDLC_IMAGE= | this seat, this run. A bare tag is a tag of +# quay.io/crunchtools/sdlc: main, or a release. # [tool.sdlc] image = "" the repo, on a seat. The same bare tags and # nothing else, and REFUSED in CI and by `sdlc # ci`: a pull request must not choose the image @@ -101,10 +98,9 @@ config_image() { # the `image` key of [tool.sdlc], or nothing } fleet_tag() { # value where -> the image that tag names, or die case "$1" in - ''|*[!A-Za-z0-9._-]*|[.-]*) die "$2 must be a tag such as beta, pr-59 or 0.8.0; got '$1'" ;; - beta|pr-[0-9]*) printf '%s:%s' "$STAGING_IMAGE" "$1" ;; - *) printf '%s:%s' "$FLEET_IMAGE" "$1" ;; + ''|*[!A-Za-z0-9._-]*|[.-]*) die "$2 must be a tag such as main or 0.8.0; got '$1'" ;; esac + printf '%s:%s' "$FLEET_IMAGE" "$1" } SDLC_CONFIG_IMAGE=$(config_image) export SDLC_CONFIG_IMAGE diff --git a/sdlc b/sdlc index 9214ecf..2dc160b 100755 --- a/sdlc +++ b/sdlc @@ -400,12 +400,12 @@ def sdlc_config() -> dict: return cfg -# `[tool.sdlc] image = "beta"` runs a tag of the fleet image instead of the +# `[tool.sdlc] image = "main"` runs a tag of the fleet image instead of the # pinned release. bin/sdlc chooses the image before this script exists, so it # reads the key itself -- with awk, as one `image = ""` line under # [tool.sdlc] -- and passes what it read as SDLC_CONFIG_IMAGE. TOML has other # ways to write the same key, and a launcher that missed one would run the -# pinned release while the repo believed it ran beta, so the two readings must +# pinned release while the repo believed it ran main, so the two readings must # agree or nothing runs. IMAGE_TAG_RE = re.compile(r"[A-Za-z0-9_][A-Za-z0-9._-]{0,127}") @@ -418,7 +418,7 @@ def config_image(cfg: dict) -> None: return tag = cfg["image"] if not isinstance(tag, str) or not IMAGE_TAG_RE.fullmatch(tag): - cannot(f"[tool.sdlc] image must be a tag such as beta, pr-59 or 0.8.0; got {tag!r}") + cannot(f"[tool.sdlc] image must be a tag such as main or 0.8.0; got {tag!r}") if "SDLC_HOST_REPO" in os.environ and os.environ.get("SDLC_CONFIG_IMAGE") != tag: cannot( f"[tool.sdlc] image is {tag!r} but bin/sdlc could not read it, so it " diff --git a/tests/test_host_contract.sh b/tests/test_host_contract.sh index 5ce3712..c8b4108 100755 --- a/tests/test_host_contract.sh +++ b/tests/test_host_contract.sh @@ -705,13 +705,13 @@ else fi log_mark -SDLC_IMAGE=beta PATH="$SHIM" sh bin/sdlc tiers > "$RUN/out" 2>&1 || true +SDLC_IMAGE=main PATH="$SHIM" sh bin/sdlc tiers > "$RUN/out" 2>&1 || true log_since -if last_run | grep -q -- '--pull=newer .* quay.io/crunchtools/sdlc-pr:beta tiers$' \ +if last_run | grep -q -- '--pull=newer .* quay.io/crunchtools/sdlc:main tiers$' \ && grep -q 'NOT THE PINNED RELEASE' "$RUN/out" && grep -q 'set by SDLC_IMAGE' "$RUN/out"; then - ok "SDLC_IMAGE=beta runs the staging quay.io/crunchtools/sdlc-pr:beta, pulled when newer, and says so" + ok "SDLC_IMAGE=main runs quay.io/crunchtools/sdlc:main, pulled when newer, and says so" else - bad "SDLC_IMAGE=beta: $(last_run) / $(head -3 "$RUN/out")" + bad "SDLC_IMAGE=main: $(last_run) / $(head -3 "$RUN/out")" fi log_mark @@ -724,29 +724,29 @@ else fi rc=0 -SDLC_IMAGE='beta;touch' PATH="$SHIM" sh bin/sdlc tiers > "$RUN/out" 2>&1 || rc=$? +SDLC_IMAGE='main;touch' PATH="$SHIM" sh bin/sdlc tiers > "$RUN/out" 2>&1 || rc=$? if [ "$rc" -eq 2 ]; then ok "a bare SDLC_IMAGE that is not a tag is refused (exit 2)" else - bad "SDLC_IMAGE='beta;touch' exited $rc" + bad "SDLC_IMAGE='main;touch' exited $rc" fi CREPO=$(mktemp -d) git -C "$CREPO" init -q -printf '[project]\nname = "x"\n\n[tool.sdlc]\nimage = "pr-59"\n' > "$CREPO/pyproject.toml" +printf '[project]\nname = "x"\n\n[tool.sdlc]\nimage = "0.8.0"\n' > "$CREPO/pyproject.toml" log_mark (cd "$CREPO" && CI='' GITHUB_ACTIONS='' PATH="$SHIM" sh "$OLDPWD/bin/sdlc" tiers) > "$RUN/out" 2>&1 || true log_since -if last_run | grep -q -- 'SDLC_CONFIG_IMAGE .*quay.io/crunchtools/sdlc-pr:pr-59 tiers$' \ +if last_run | grep -q -- 'SDLC_CONFIG_IMAGE .*quay.io/crunchtools/sdlc:0.8.0 tiers$' \ && grep -q 'set by \[tool.sdlc\] image in pyproject.toml' "$RUN/out"; then - ok "[tool.sdlc] image = \"pr-59\" runs that tag and tells the image what it read" + ok "[tool.sdlc] image = \"0.8.0\" runs that tag and tells the image what it read" else bad "[tool.sdlc] image: $(last_run) / $(head -4 "$RUN/out")" fi log_mark -(cd "$CREPO" && CI='' GITHUB_ACTIONS='' SDLC_IMAGE=beta PATH="$SHIM" sh "$OLDPWD/bin/sdlc" tiers) > "$RUN/out" 2>&1 || true +(cd "$CREPO" && CI='' GITHUB_ACTIONS='' SDLC_IMAGE=main PATH="$SHIM" sh "$OLDPWD/bin/sdlc" tiers) > "$RUN/out" 2>&1 || true log_since -if last_run | grep -q -- 'quay.io/crunchtools/sdlc-pr:beta tiers$'; then +if last_run | grep -q -- 'quay.io/crunchtools/sdlc:main tiers$'; then ok "SDLC_IMAGE beats [tool.sdlc] image" else bad "SDLC_IMAGE did not beat the repo's image: $(last_run)" @@ -766,14 +766,6 @@ for how in 'CI=true' 'GITHUB_ACTIONS=true' 'ci'; do bad "[tool.sdlc] image under $how exited $rc" fi done -log_mark -(cd "$CREPO" && CI='' GITHUB_ACTIONS='' SDLC_IMAGE=0.8.0 PATH="$SHIM" sh "$OLDPWD/bin/sdlc" tiers) > "$RUN/out" 2>&1 || true -log_since -if last_run | grep -q -- 'quay.io/crunchtools/sdlc:0.8.0 tiers$'; then - ok "a bare release tag is the fleet image, not staging" -else - bad "SDLC_IMAGE=0.8.0: $(last_run)" -fi printf '[tool.sdlc]\nimage = "../../evil"\n' > "$CREPO/sdlc.toml" rm "$CREPO/pyproject.toml" rc=0 @@ -788,9 +780,9 @@ rm -rf "$CREPO" # container-scan runs the gate's own image as a host step, and only the # launcher knows which image that is: the plan names it `@sdlc-image`. log_mark -SDLC_IMAGE=beta PATH="$SHIM" sh bin/sdlc gate-word >/dev/null 2>&1 || true +SDLC_IMAGE=main PATH="$SHIM" sh bin/sdlc gate-word >/dev/null 2>&1 || true log_since -if grep -q -- '--cap-drop=all .* quay.io/crunchtools/sdlc-pr:beta scan-rootfs$' "$RUN/podman.log" \ +if grep -q -- '--cap-drop=all .* quay.io/crunchtools/sdlc:main scan-rootfs$' "$RUN/podman.log" \ && ! grep -q '@sdlc-image' "$RUN/podman.log"; then ok "a host step's @sdlc-image is the image this run is on" else diff --git a/tests/test_plan.py b/tests/test_plan.py index 452107f..8dbfa45 100644 --- a/tests/test_plan.py +++ b/tests/test_plan.py @@ -761,9 +761,9 @@ def image_error(pyproject: str, launcher_read: str | None) -> str | None: check( "image: the launcher and TOML agreeing is fine", - image_error('[tool.sdlc]\nimage = "beta"\n', "beta") is None, + image_error('[tool.sdlc]\nimage = "main"\n', "main") is None, ) -msg = image_error('tool.sdlc.image = "beta"\n', None) +msg = image_error('tool.sdlc.image = "main"\n', None) check( "image: a form the launcher could not read is refused", msg is not None and "could not read" in msg, @@ -773,7 +773,7 @@ def image_error(pyproject: str, launcher_read: str | None) -> str | None: check("image: only a tag of the fleet image", msg is not None and "must be a tag" in msg, str(msg)) msg = image_error("[tool.sdlc]\nimage = 3\n", "3") check("image: a number is not a tag", msg is not None and "must be a tag" in msg, str(msg)) -msg = image_error("[tool.ruff]\n", "beta") +msg = image_error("[tool.ruff]\n", "main") check("image: a launcher reading one that TOML lacks is refused", msg is not None, str(msg)) check("image: none set anywhere is fine", image_error("[tool.ruff]\n", None) is None) From d820756e0731b7e375d3f9a1d18f9d5c39849791 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 17 Nov 2026 19:20:03 +0000 Subject: [PATCH 10/11] fix(security): no podman option injection through image references Review a203. A repo-set image or tag reached the host's podman argv unchecked, so `image = "--privileged"` with `args = ["-v", "/:/host"]` made `sdlc ci` run a privileged, root-bound container on whoever ran it. - The planner refuses any image or tag that is not an image reference (one strict regex, every kind and built-in check, pytest-in-image's pre-existing hole included); a repo path starting with `-` likewise. - Smoke and scan put the image after `--`; bin/sdlc re-checks that word on the host for every run, and a host-up run without `--` is refused. - The comma that could rewrite `--mount type=image,source=...` is refused by the same check. - The promote guard reads a tag's revision fail-closed: only "manifest unknown" means absent; auth or network errors stop the job. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01R7Y1jckWHhAgXg2WBwkWbb --- .github/workflows/container.yml | 21 +++++++-- CHANGELOG.md | 12 +++++ bin/sdlc | 25 ++++++++++- sdlc | 49 ++++++++++++++++++--- tests/test_host_contract.sh | 28 ++++++++++-- tests/test_plan.py | 78 +++++++++++++++++++++++++++++++++ 6 files changed, 201 insertions(+), 12 deletions(-) diff --git a/.github/workflows/container.yml b/.github/workflows/container.yml index 9270ff3..7bada9b 100644 --- a/.github/workflows/container.yml +++ b/.github/workflows/container.yml @@ -292,11 +292,26 @@ jobs: [ "$got" = "$want" ] || { echo "$repo:$1 resolves to $got, not the tested $want" >&2; exit 1; } done } + # The published revision of a tag, or nothing when the tag does not + # exist yet. Anything else -- auth, network, a registry error -- + # stops the job: a guard that cannot read what it guards would + # otherwise wave the move through. + revision_of() { + if out=$(skopeo inspect --format \ + '{{ index .Labels "org.opencontainers.image.revision" }}' \ + "docker://$1" 2>"$RUNNER_TEMP/inspect.err"); then + printf '%s' "$out" + elif grep -qi 'manifest unknown' "$RUNNER_TEMP/inspect.err"; then + : + else + echo "cannot read $1 to guard it:" >&2 + cat "$RUNNER_TEMP/inspect.err" >&2 + exit 1 + fi + } if [ -n "$floating" ]; then for repo in "$QUAY" "$SDLC"; do - current=$(skopeo inspect --format \ - '{{ index .Labels "org.opencontainers.image.revision" }}' \ - "docker://$repo:$floating" 2>/dev/null || true) + current=$(revision_of "$repo:$floating") if [ -n "${current:-}" ] && [ "$current" != "$GITHUB_SHA" ]; then if git merge-base --is-ancestor "$GITHUB_SHA" "$current" 2>/dev/null; then echo "refusing: $repo:$floating is $current, which already contains $GITHUB_SHA" >&2 diff --git a/CHANGELOG.md b/CHANGELOG.md index bdf232a..6bcab29 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -43,6 +43,18 @@ All notable changes to this project are documented here, following - The image is also published as `quay.io/crunchtools/sdlc`: the same digest copied, never rebuilt, with the same tags as `python-sdlc`. +### Security + +- **A repo's config cannot inject podman options.** Every image and tag a + check uses -- `container-build`'s tag, smoke's and scan's image, and the + `image-build` and `pytest-in-image` kinds (the last two since they were + added) -- must be an image reference: nothing starting with `-`, no comma, + no whitespace. Otherwise the planner exits 2. The smoke and scan runs put + the image after `--`, and `bin/sdlc` checks that word again on the host. A + repo path that begins with `-` is refused the same way. +- The promote guard fails closed: it treats only "manifest unknown" as an + absent tag, and any other failure to read a tag's revision stops the job. + ### Fixed - `bin/sdlc`'s `run_host` kept its exit status in `_rc`, which `run_serial` diff --git a/bin/sdlc b/bin/sdlc index 4e68a98..f2cd005 100755 --- a/bin/sdlc +++ b/bin/sdlc @@ -537,6 +537,26 @@ record_finish() { # worst status TAB=$(printf '\t') US=$(printf '\037') +check_image_word() { # run-args -> dies unless the word after `--` is an image + # The planner validates every image reference; this holds the same line on + # the host, because the host is what a bad one would harm. Only the word + # after a `--` is judged: before it podman reads options, after the image it + # passes arguments to the container. + _ciafter= + for _ciw in "$@"; do + if [ "$_ciafter" = yes ]; then + case "$_ciw" in + ''|-*|*[!A-Za-z0-9._/:@-]*) + die "a host step names '$_ciw' as its image, which is not an image reference" ;; + esac + return 0 + fi + [ "$_ciw" = -- ] && _ciafter=yes + done + [ "$_ciafter" = yes ] && die "a host step ends with '--' and names no image" + return 0 +} + run_up() { # seconds run-args -> the smoke verdict # A `podman run` that may be a service. It starts detached, and its STATE is # asked once a second for up to : still running at the end passes, @@ -549,6 +569,8 @@ run_up() { # seconds run-args -> the smoke verdict # name, however it ended. A k8s-file log so its output can be shown, which # --log-driver=none would discard. _upsecs=$1; shift + case " $* " in *" -- "*) ;; *) die "a run given time to stay up must name its image after '--'" ;; esac + check_image_word "$@" _cname=sdlc-smoke-$$-$(now_s) _uprc=0 podman --events-backend=none run -d --name "$_cname" --log-driver=k8s-file \ @@ -619,7 +641,8 @@ run_host() { # argv [seconds] -> the step's verdict case "$_sub" in build) set -- build --cpu-period 100000 --cpu-quota "$(( GATE_CPUS * 100000 ))" \ --memory "$GATE_MEMORY" "$@" ;; - run) set -- --events-backend=none run --log-driver=none \ + run) check_image_word "$@" + set -- --events-backend=none run --log-driver=none \ --cap-drop=all --security-opt=no-new-privileges \ --cpus "$GATE_CPUS" --memory "$GATE_MEMORY" --cpu-shares 256 "$@" ;; *) die "a host step may only build or run; refused: podman $_sub" ;; diff --git a/sdlc b/sdlc index 2dc160b..c887b52 100755 --- a/sdlc +++ b/sdlc @@ -797,6 +797,35 @@ def token(value: object) -> str: cannot(f"unknown value {text}; known: @project.version, @git.sha, @now") +# An image reference, and nothing that could be read as anything else. These +# values are repo-controlled and land in a HOST podman argv: one beginning with +# `-` is an option (`--privileged`), one with a comma rewrites the +# `--mount type=image,source=...` it is spliced into, and whitespace or a +# newline is two words. So every image and tag any check uses -- built in or a +# kind, set or defaulted -- must match this, and bin/sdlc checks again. +# +# The distribution grammar, narrowed: an optional registry host and port, a +# lowercase path, an optional tag and an optional sha256 digest. +_IMAGE_PATH = r"[a-z0-9]+(?:(?:[._]|__|-+)[a-z0-9]+)*" +IMAGE_REF_RE = re.compile( + r"(?:[A-Za-z0-9](?:[A-Za-z0-9.-]*[A-Za-z0-9])?(?::[0-9]+)?/)?" + rf"{_IMAGE_PATH}(?:/{_IMAGE_PATH})*" + r"(?::[A-Za-z0-9_][A-Za-z0-9._-]{0,127})?" + r"(?:@sha256:[0-9a-f]{64})?" +) + + +def image_ref(name: str, key: str, value: object) -> str: + """`value` as an image reference, or a refusal.""" + text = str(value) + if not isinstance(value, str) or not IMAGE_REF_RE.fullmatch(text): + cannot( + f"{name}: {key} {text!r} is not an image reference such as " + f"localhost/name:tag or quay.io/org/name:1.0" + ) + return text + + def in_repo(name: str, key: str, value: object) -> str: """A repo-relative path that stays inside the repo, or a refusal. @@ -811,7 +840,12 @@ def in_repo(name: str, key: str, value: object) -> str: cannot(f"{name}: {key} {text!r} is outside the repository") if not target.exists(): cannot(f"{name}: {key} {text!r} does not exist") - return str(target.relative_to(REPO.resolve())) or "." + rel = str(target.relative_to(REPO.resolve())) or "." + # A path is a positional word in a podman argv, so one podman would read as + # an option is refused like any other path it should not be given. + if rel.startswith("-"): + cannot(f"{name}: {key} {text!r} begins with '-', which podman would read as an option") + return rel def table(settings: dict, key: str) -> dict: @@ -834,7 +868,7 @@ def kind_image_build(name: str, settings: dict) -> list[tuple[str, str, str]]: argv += ["--build-arg", f"{key}={token(value)}"] for key, value in table(settings, "labels").items(): argv += ["--label", f"{key}={token(value)}"] - argv += ["-t", str(settings.get("tag", default_image_tag())), context] + argv += ["-t", image_ref(name, "tag", settings.get("tag", default_image_tag())), context] return [host_step(name, argv)] @@ -886,7 +920,7 @@ def kind_pytest_in_image(name: str, settings: dict) -> list[tuple[str, str, str] argv += ["-v", f"{host}/{rel}:/repo/{rel}:ro,z"] for key, value in table(settings, "env").items(): argv += ["-e", f"{key}={token(value)}"] - argv += [str(settings.get("image", default_image_tag()))] + argv += [image_ref(name, "image", settings.get("image", default_image_tag()))] argv += ["-m", "pytest", "-p", "no:cacheprovider"] argv += names(settings.get("args", ["-q", "-rfE"]), f"{name}.args") argv += tests @@ -918,7 +952,9 @@ SMOKE_SECONDS_MAX = 600 def container_image(cfg: dict, settings: dict) -> str: """What smoke and scan judge: their own `image`, or what container-build tags.""" built = check_settings(cfg, "container-build").get("tag", default_image_tag()) - return str(settings.get("image", built)) + if "image" in settings: + return image_ref("container-smoke/scan", "image", settings["image"]) + return image_ref("container-build", "tag", built) def check_container_build(cfg: dict, name: str, s: dict) -> list[tuple[str, str, str]]: @@ -957,7 +993,9 @@ def check_container_smoke(cfg: dict, name: str, s: dict) -> list[tuple[str, str, argv += ["--network", "none"] for key, value in table(s, "env").items(): argv += ["-e", f"{key}={token(value)}"] - argv += [container_image(cfg, s), *names(s.get("args"), f"{name}.args")] + # `--` before the image: nothing after it is a podman option, whatever it + # says, and bin/sdlc checks the word that follows it. + argv += ["--", container_image(cfg, s), *names(s.get("args"), f"{name}.args")] payload = UNIT_SEPARATOR.join([str(seconds), *(plain(a, name) for a in argv)]) return [(name, "host-up", payload)] @@ -1013,6 +1051,7 @@ def check_container_scan(cfg: dict, name: str, s: dict) -> list[tuple[str, str, f"SDLC_SCAN_FAIL_ON={fail_on}", "-e", f"SDLC_SCAN_ONLY_FIXED={'1' if only_fixed else '0'}", + "--", GATE_IMAGE, "scan-rootfs", ] diff --git a/tests/test_host_contract.sh b/tests/test_host_contract.sh index c8b4108..f408da8 100755 --- a/tests/test_host_contract.sh +++ b/tests/test_host_contract.sh @@ -140,14 +140,22 @@ case " \$* " in printf 'p${TAB}one${TAB}host${TAB}podman${US}build${US}-t${US}localhost/x:sdlc${US}.\n' printf 'q${TAB}two${TAB}image-ro${TAB}quiet-ok\n' ;; *" plan gate-word "*) - printf 'gate-word${TAB}main${TAB}host${TAB}podman${US}run${US}--rm${US}@sdlc-image${US}scan-rootfs\n' ;; + printf 'gate-word${TAB}main${TAB}host${TAB}podman${US}run${US}--rm${US}--${US}@sdlc-image${US}scan-rootfs\n' ;; + *" plan inject-up "*) + printf 'inject-up${TAB}main${TAB}host-up${TAB}5${US}podman${US}run${US}--${US}--privileged${US}-v${US}/:/host\n' ;; + *" plan inject-run "*) + printf 'inject-run${TAB}main${TAB}host${TAB}podman${US}run${US}--rm${US}--${US}-x\n' ;; + *" plan inject-comma "*) + printf 'inject-comma${TAB}main${TAB}host${TAB}podman${US}run${US}--rm${US}--${US}a,b\n' ;; + *" plan inject-nodd "*) + printf 'inject-nodd${TAB}main${TAB}host-up${TAB}5${US}podman${US}run${US}--privileged${US}img\n' ;; *" plan up-"*) _secs=1; _what=\$_last case "\$_last" in up-build) _what=build ;; up-noseconds) _secs=soon ;; esac if [ "\$_what" = build ]; then printf '%s${TAB}main${TAB}host-up${TAB}1${US}podman${US}build${US}-t${US}x${US}.\n' "\$_last" else - printf '%s${TAB}main${TAB}host-up${TAB}%s${US}podman${US}run${US}localhost/x:sdlc${US}go-%s\n' \ + printf '%s${TAB}main${TAB}host-up${TAB}%s${US}podman${US}run${US}--${US}localhost/x:sdlc${US}go-%s\n' \ "\$_last" "\$_secs" "\$_last" fi ;; *" plan "*) @@ -245,13 +253,27 @@ done log_mark PATH="$SHIM" sh bin/sdlc up-stays >/dev/null 2>&1 || true log_since -if grep -q -- 'run -d --name sdlc-smoke-[0-9-]* --log-driver=k8s-file --cap-drop=all --security-opt=no-new-privileges .* localhost/x:sdlc go-up-stays$' "$RUN/podman.log" \ +if grep -q -- 'run -d --name sdlc-smoke-[0-9-]* --log-driver=k8s-file --cap-drop=all --security-opt=no-new-privileges .* -- localhost/x:sdlc go-up-stays$' "$RUN/podman.log" \ && grep -q -- '^rm -f sdlc-smoke-' "$RUN/podman.log"; then ok "host-up runs detached and locked down, and removes the container" else bad "host-up's run or cleanup is wrong: $(grep -E 'up-stays|rm -f' "$RUN/podman.log" | tail -2)" fi +# A repo's config must never reach the host's podman as an option. The planner +# refuses such an image; the launcher refuses it again, here, on the host. +for case_ in inject-up inject-run inject-comma inject-nodd; do + log_mark + rc=0 + PATH="$SHIM" sh bin/sdlc "$case_" > "$RUN/out" 2>&1 || rc=$? + log_since + if [ "$rc" -eq 2 ] && ! grep -q -- '--privileged\|a,b\| -x$' "$RUN/podman.log"; then + ok "$case_: an image word that is not an image is refused (exit 2) and never run" + else + bad "$case_ exited $rc: $(grep -E 'privileged|a,b| -x$' "$RUN/podman.log" | tail -1)" + fi +done + printf '\n== a check deferred to CI\n\n' # 3 is the image saying "could not run, and CI judges this commit". A push lets diff --git a/tests/test_plan.py b/tests/test_plan.py index 8dbfa45..56bbf36 100644 --- a/tests/test_plan.py +++ b/tests/test_plan.py @@ -740,6 +740,84 @@ def container_plan(target: str, sdlc_toml: str = "", **env) -> list[list[str]]: check("a built-in container check cannot set a kind", msg is not None and "built in" in msg) +# ------------------------------------------------------- image reference injection +# +# An image or tag is repo-controlled and lands in a HOST podman argv. Anything +# podman would read as an option, a comma that would rewrite +# `--mount type=image,source=...`, or whitespace that would split into two +# words is refused, wherever it is set: container-build's tag, smoke's and +# scan's image, and the image-build and pytest-in-image kinds. + +INJECTIONS = ["-x", "--privileged", "a,b", "a b", "a\nb", "Upper/Case"] + + +def refused(fn) -> str | None: + try: + fn() + except S.CannotRunError as exc: + return str(exc) + return None + + +for bad in INJECTIONS: + value = json.dumps(bad) + for table, key in ( + ("container-build", "tag"), + ("container-smoke", "image"), + ("container-scan", "image"), + ): + msg = refused( + lambda t=table, k=key, v=value: container_plan("ci", f"[tool.sdlc.ci.{t}]\n{k} = {v}\n") + ) + check( + f"{table} {key} {bad!r} is refused", + msg is not None and "image reference" in msg, + str(msg), + ) + msg = refused( + lambda v=value: plan_of( + "ci", + '[project]\nname = "fx"\nversion = "0"\n[tool.sdlc.ci]\nchecks = ["t"]\n' + f'[tool.sdlc.ci.t]\nkind = "pytest-in-image"\ntests = ["tests/"]\nimage = {v}\n', + ) + ) + check( + f"pytest-in-image image {bad!r} is refused", + msg is not None and "image reference" in msg, + str(msg), + ) + msg = refused( + lambda v=value: plan_of( + "ci", + '[project]\nname = "fx"\nversion = "0"\n[tool.sdlc.ci]\nchecks = ["b"]\n' + f'[tool.sdlc.ci.b]\nkind = "image-build"\ntag = {v}\n', + ) + ) + check( + f"image-build tag {bad!r} is refused", + msg is not None and "image reference" in msg, + str(msg), + ) + +for good in ( + "localhost/fx:sdlc", + "quay.io/crunchtools/sdlc:main", + "registry.example.com:5000/a/b_c-d:1.0", + "quay.io/x/y@sha256:" + "0" * 64, +): + check(f"{good} is an image reference", S.IMAGE_REF_RE.fullmatch(good) is not None) + +rows = container_plan("ci") +smoke_words = next(r[3] for r in rows if r[0] == "container-smoke").split(S.UNIT_SEPARATOR) +check( + "smoke puts the image after --, so nothing after it is a podman option", + smoke_words[smoke_words.index("--") + 1] == "localhost/fx:sdlc", + str(smoke_words), +) +scan_words = next(r[3] for r in rows if r[0] == "container-scan").split(S.UNIT_SEPARATOR) +check("scan puts the gate image after --", scan_words[-3:] == ["--", S.GATE_IMAGE, "scan-rootfs"]) + + # ------------------------------------------------------------- [tool.sdlc] image # # bin/sdlc reads the key with awk before this script runs. If TOML and awk From aa57a90f73a583788508441c84bb2ec49d94ef6d Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 17 Nov 2026 19:22:50 +0000 Subject: [PATCH 11/11] test: the promote guard's tag read, as a script with its own test scripts/revision-of.sh replaces the inline revision_of in container.yml. tests/test_revision_of.sh runs it against a fake skopeo: a readable tag prints its revision, "manifest unknown" is an absent tag, and an auth or network failure exits non-zero, so the guard fails closed. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01R7Y1jckWHhAgXg2WBwkWbb --- .github/workflows/container.yml | 25 +++++------------- .pre-commit-config.yaml | 2 +- scripts/revision-of.sh | 27 +++++++++++++++++++ tests/test_revision_of.sh | 46 +++++++++++++++++++++++++++++++++ 4 files changed, 80 insertions(+), 20 deletions(-) create mode 100755 scripts/revision-of.sh create mode 100755 tests/test_revision_of.sh diff --git a/.github/workflows/container.yml b/.github/workflows/container.yml index 7bada9b..5be1bd3 100644 --- a/.github/workflows/container.yml +++ b/.github/workflows/container.yml @@ -88,8 +88,10 @@ jobs: set -eu podman run --rm -v "$PWD:/mnt:ro,z" -w /mnt \ docker.io/koalaman/shellcheck:v0.11.0@sha256:61862eba1fcf09a484ebcc6feea46f1782532571a34ed51fedf90dd25f925a8d bin/sdlc tests/contract.sh \ - tests/test_host_contract.sh tests/test_container_stack.sh scripts/release-version.sh + tests/test_host_contract.sh tests/test_container_stack.sh scripts/release-version.sh \ + scripts/revision-of.sh tests/test_revision_of.sh tests/test_host_contract.sh + tests/test_revision_of.sh tests/test_release_version.py - name: Build @@ -292,26 +294,11 @@ jobs: [ "$got" = "$want" ] || { echo "$repo:$1 resolves to $got, not the tested $want" >&2; exit 1; } done } - # The published revision of a tag, or nothing when the tag does not - # exist yet. Anything else -- auth, network, a registry error -- - # stops the job: a guard that cannot read what it guards would - # otherwise wave the move through. - revision_of() { - if out=$(skopeo inspect --format \ - '{{ index .Labels "org.opencontainers.image.revision" }}' \ - "docker://$1" 2>"$RUNNER_TEMP/inspect.err"); then - printf '%s' "$out" - elif grep -qi 'manifest unknown' "$RUNNER_TEMP/inspect.err"; then - : - else - echo "cannot read $1 to guard it:" >&2 - cat "$RUNNER_TEMP/inspect.err" >&2 - exit 1 - fi - } if [ -n "$floating" ]; then for repo in "$QUAY" "$SDLC"; do - current=$(revision_of "$repo:$floating") + # Fails closed: only an absent tag reads as empty + # (scripts/revision-of.sh, tests/test_revision_of.sh). + current=$(scripts/revision-of.sh "$repo:$floating") if [ -n "${current:-}" ] && [ "$current" != "$GITHUB_SHA" ]; then if git merge-base --is-ancestor "$GITHUB_SHA" "$current" 2>/dev/null; then echo "refusing: $repo:$floating is $current, which already contains $GITHUB_SHA" >&2 diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 7af1e6e..ab8b1a1 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -35,7 +35,7 @@ repos: hooks: - id: shellcheck name: shellcheck (bin/sdlc, the tests and scripts) - entry: podman --events-backend=none run --rm --log-driver=none -v .:/mnt:ro,z -w /mnt docker.io/koalaman/shellcheck:v0.11.0@sha256:61862eba1fcf09a484ebcc6feea46f1782532571a34ed51fedf90dd25f925a8d bin/sdlc tests/contract.sh tests/test_host_contract.sh tests/test_container_stack.sh scripts/release-version.sh + entry: podman --events-backend=none run --rm --log-driver=none -v .:/mnt:ro,z -w /mnt docker.io/koalaman/shellcheck:v0.11.0@sha256:61862eba1fcf09a484ebcc6feea46f1782532571a34ed51fedf90dd25f925a8d bin/sdlc tests/contract.sh tests/test_host_contract.sh tests/test_container_stack.sh scripts/release-version.sh scripts/revision-of.sh tests/test_revision_of.sh language: system pass_filenames: false always_run: true diff --git a/scripts/revision-of.sh b/scripts/revision-of.sh new file mode 100755 index 0000000..5769051 --- /dev/null +++ b/scripts/revision-of.sh @@ -0,0 +1,27 @@ +#!/bin/sh +# Print the revision label of a published image tag, or nothing when the tag +# does not exist yet. Used by container.yml's promote guard, which refuses to +# move a floating tag onto a commit the tag already contains. +# +# Only "manifest unknown" means absent. Any other failure to read the tag -- +# auth, network, a registry error -- exits non-zero: a guard that cannot read +# what it guards would otherwise wave the move through. +# +# Usage: scripts/revision-of.sh +set -eu + +ref=${1:?usage: scripts/revision-of.sh } +err=$(mktemp) +trap 'rm -f "$err"' EXIT + +if out=$(skopeo inspect --format '{{ index .Labels "org.opencontainers.image.revision" }}' \ + "docker://$ref" 2>"$err"); then + printf '%s\n' "$out" + exit 0 +fi +if grep -qi 'manifest unknown' "$err"; then + exit 0 +fi +printf 'cannot read %s to guard it:\n' "$ref" >&2 +cat "$err" >&2 +exit 1 diff --git a/tests/test_revision_of.sh b/tests/test_revision_of.sh new file mode 100755 index 0000000..6039f20 --- /dev/null +++ b/tests/test_revision_of.sh @@ -0,0 +1,46 @@ +#!/bin/sh +# scripts/revision-of.sh, against a fake skopeo: a readable tag prints its +# revision, "manifest unknown" prints nothing and succeeds, and every other +# failure -- auth, network -- exits non-zero, so the promote guard fails closed. +# +# Usage: tests/test_revision_of.sh + +set -eu + +cd "$(CDPATH='' cd -- "$(dirname -- "$0")/.." && pwd)" +FAILED=0 +ok() { printf 'ok %s\n' "$*"; } +bad() { printf 'FAIL %s\n' "$*"; FAILED=$((FAILED + 1)); } + +BIN=$(mktemp -d) +trap 'rm -rf "$BIN"' EXIT +cat > "$BIN/skopeo" <<'SHIM' +#!/bin/sh +case "$FAKE_SKOPEO" in + ok) printf 'abc123\n' ;; + missing) echo 'Error: reading manifest main in quay.io/x/y: manifest unknown' >&2; exit 1 ;; + auth) echo 'Error: unauthorized: authentication required' >&2; exit 1 ;; + network) echo 'Error: pinging container registry quay.io: dial tcp: i/o timeout' >&2; exit 1 ;; +esac +SHIM +chmod +x "$BIN/skopeo" + +run() { FAKE_SKOPEO=$1 PATH="$BIN:$PATH" scripts/revision-of.sh quay.io/x/y:main; } + +rc=0; out=$(run ok 2>/dev/null) || rc=$? +if [ "$rc" -eq 0 ] && [ "$out" = abc123 ]; then ok "a readable tag prints its revision" +else bad "readable tag: rc=$rc out='$out'"; fi + +rc=0; out=$(run missing 2>/dev/null) || rc=$? +if [ "$rc" -eq 0 ] && [ -z "$out" ]; then ok "manifest unknown is an absent tag: nothing, exit 0" +else bad "missing tag: rc=$rc out='$out'"; fi + +for failure in auth network; do + rc=0; run "$failure" >/dev/null 2>&1 || rc=$? + if [ "$rc" -ne 0 ]; then ok "a $failure failure stops the guard (exit $rc)" + else bad "a $failure failure was read as an absent tag"; fi +done + +printf '\n' +[ "$FAILED" -eq 0 ] && { printf 'revision-of PASSED\n'; exit 0; } +printf 'revision-of FAILED: %s check(s)\n' "$FAILED"; exit 1