diff --git a/.github/workflows/container.yml b/.github/workflows/container.yml index 43f57c7..5be1bd3 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: @@ -72,7 +78,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 @@ -81,9 +87,11 @@ 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 \ - tests/test_host_contract.sh scripts/release-version.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 \ + 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 @@ -124,6 +132,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: @@ -139,9 +153,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 @@ -185,7 +205,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 @@ -211,7 +231,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 @@ -262,22 +282,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 + # 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 + exit 1 + fi fi - fi + done promote "$floating" fi case "$GITHUB_REF" in @@ -311,13 +339,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 503604c..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 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@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/CHANGELOG.md b/CHANGELOG.md index 4de0d94..6bcab29 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,63 @@ 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 + 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. +- **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`. + +### 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` + 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 ### Changed diff --git a/Containerfile b/Containerfile index 914fd4f..b5f6ff7 100644 --- a/Containerfile +++ b/Containerfile @@ -18,11 +18,18 @@ 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. +# 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 @@ -30,6 +37,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 +49,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 +64,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 +88,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 +146,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..d955767 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 | 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. + ## 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 @@ -279,6 +330,36 @@ 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 main before a release + +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=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 `: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.** 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 `SDLC_SKIP=gatehouse git push` skips one check, once, for a false positive. The @@ -309,6 +390,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..f2cd005 100755 --- a/bin/sdlc +++ b/bin/sdlc @@ -44,9 +44,12 @@ 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 +# 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 # --------------------------------------------------------------- preconditions @@ -65,6 +68,72 @@ REPO=$(git rev-parse --show-toplevel 2>/dev/null) \ cd "$REPO" PROJECT=$(basename "$REPO") +# ------------------------------------------------------------- an image override +# +# 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 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 +# 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`, +# 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 image that tag names, or die + case "$1" in + ''|*[!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 +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 + 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 +337,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,6 +348,7 @@ run_in_image() { -e PYTHONPYCACHEPREFIX=/tmp/pycache \ -e SEMGREP_SETTINGS_FILE=/tmp/semgrep.yml \ -e "SDLC_PROJECT=$PROJECT" \ + ${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 || :)" \ @@ -454,6 +525,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 +537,85 @@ record_finish() { # worst status TAB=$(printf '\t') US=$(printf '\037') -run_host() { +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, + # 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 + 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 \ + --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: run_up + # judges it by its state. + _up=${2:-} _ifs=$IFS IFS=$US # shellcheck disable=SC2086 # splitting on the separator is the point @@ -473,25 +624,41 @@ run_host() { [ "${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 + 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 \ + 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" ;; esac - _rc=0 - podman "$@" &2; return 2 ;; + *) printf '%s: podman %s exited %s; nothing was judged\n' "$ME" "$_sub" "$_hrc" >&2; return 2 ;; esac } @@ -520,6 +687,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 +1122,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 +1132,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 +1160,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/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/sdlc b/sdlc index 660beac..c887b52 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, UnicodeDecodeError, 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,27 +366,67 @@ 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", "image"}) 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`, `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 # 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 +# `[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 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}") + + +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(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 " + 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): @@ -256,12 +452,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 +497,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 +505,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 +568,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 +580,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 +599,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 +675,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 +691,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 +723,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 +731,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 +745,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(): @@ -568,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. @@ -582,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: @@ -605,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)] @@ -657,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 @@ -669,6 +932,223 @@ 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()) + 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]]: + """`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}") + # 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(): + argv += ["-e", f"{key}={token(value)}"] + # `--` 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)] + + +# 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. + + 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") + 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)}") + 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_IMAGE, + "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" +SCAN_ROOT = Path("/scan") + + +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 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 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"}) + 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) + 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" + 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 +2126,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 +2149,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_container_stack.sh b/tests/test_container_stack.sh new file mode 100755 index 0000000..ce20b3f --- /dev/null +++ b/tests/test_container_stack.sh @@ -0,0 +1,85 @@ +#!/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"]' +# 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-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 + 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 + +# 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 bc1886f..f408da8 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' ;; @@ -118,6 +139,25 @@ 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}--${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}--${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 +171,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-*) + 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 ;; @@ -188,6 +231,49 @@ 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. 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=$? + 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 -- '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 @@ -622,6 +708,109 @@ 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=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:main tiers$' \ + && grep -q 'NOT THE PINNED RELEASE' "$RUN/out" && grep -q 'set by SDLC_IMAGE' "$RUN/out"; then + ok "SDLC_IMAGE=main runs quay.io/crunchtools/sdlc:main, pulled when newer, and says so" +else + bad "SDLC_IMAGE=main: $(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='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='main;touch' exited $rc" +fi + +CREPO=$(mktemp -d) +git -C "$CREPO" init -q +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:0.8.0 tiers$' \ + && grep -q 'set by \[tool.sdlc\] image in pyproject.toml' "$RUN/out"; then + 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=main PATH="$SHIM" sh "$OLDPWD/bin/sdlc" tiers) > "$RUN/out" 2>&1 || true +log_since +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)" +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 +printf '[tool.sdlc]\nimage = "../../evil"\n' > "$CREPO/sdlc.toml" +rm "$CREPO/pyproject.toml" +rc=0 +(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: $(tail -2 "$RUN/out")" +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=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: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 + 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 c0c6ad7..56bbf36 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,390 @@ 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", + **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:] == [S.GATE_IMAGE, "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) + + +# ------------------------------------------------------- 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 +# 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 = "main"\n', "main") is 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, + 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", "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) + + +# -------------------------------------------------------------- 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 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