From 8e7ac6a659fe73042c5ce956ec2d46713727bb45 Mon Sep 17 00:00:00 2001 From: Pavlo Ratushnyi Date: Sat, 14 Nov 2026 13:51:33 +0100 Subject: [PATCH] LEAS-2795 minio: gates, image check and release workflow --- .gitea/workflows/release.yml | 233 +++++++++++++++++++ vezha/check-image.sh | 138 ++++++++++++ vezha/gates.sh | 426 +++++++++++++++++++++++++++++++++++ 3 files changed, 797 insertions(+) create mode 100644 .gitea/workflows/release.yml create mode 100644 vezha/check-image.sh create mode 100644 vezha/gates.sh diff --git a/.gitea/workflows/release.yml b/.gitea/workflows/release.yml new file mode 100644 index 000000000..706bd2399 --- /dev/null +++ b/.gitea/workflows/release.yml @@ -0,0 +1,233 @@ +name: release + +on: + push: + branches: ['**'] + workflow_dispatch: + +concurrency: + group: release-${{ github.ref }} + cancel-in-progress: false + +jobs: + release: + runs-on: ubuntu-latest + timeout-minutes: 90 + permissions: + contents: read + env: + KIND: minio + BIN: minio + PKG: minio + REGISTRY: git.vezha.io + steps: + - name: AC1 checkout the recipe + uses: https://git.vezha.io/actions/checkout@v4 + with: + persist-credentials: false + + - name: AC2 pins are complete and well formed + id: pins + run: sh vezha/gates.sh pins "$KIND" | tee -a "$GITHUB_ENV" | tee -a "$GITHUB_OUTPUT" + + - name: AC3 no unpinned reference and workflow lint + run: sh vezha/gates.sh refs "$KIND" + + - name: tools docker cli and jq + run: | + command -v docker >/dev/null 2>&1 || { apt-get update -qq; DEBIAN_FRONTEND=noninteractive apt-get install -y -qq docker.io >/dev/null; } + command -v jq >/dev/null 2>&1 || { apt-get update -qq; DEBIAN_FRONTEND=noninteractive apt-get install -y -qq jq >/dev/null; } + docker version --format 'client {{.Client.Version}} server {{.Server.Version}}' + jq --version + + - name: AC1 provenance of the import, anonymous + run: sh vezha/gates.sh provenance-remote + + - name: AC1 checkout the pinned upstream source + uses: https://git.vezha.io/actions/checkout@v4 + with: + ref: ${{ steps.pins.outputs.UPSTREAM_COMMIT }} + path: src + fetch-depth: 1 + persist-credentials: false + + - name: AC1 source tree is at the pinned commit + run: sh vezha/gates.sh provenance-src + + - name: AC7 source offer URLs answer anonymously + run: sh vezha/gates.sh source-urls + + - name: negative controls, every gate must refuse its planted input + run: sh vezha/gates.sh self-test + + - name: names and build arguments + run: | + TAG=$(sh vezha/gates.sh make-tag) + { echo "TAG=$TAG"; echo "IMG=$REGISTRY/para/$PKG:$TAG"; echo "LOCAL=vezha-build-$KIND:$GITHUB_RUN_ID"; } >> "$GITHUB_ENV" + mkdir -p out + sh vezha/gates.sh pins "$KIND" | sed 's/^/--build-arg=/' > out/build-args + echo "tag $TAG" + + - name: AC2 pins stage proves the source tree and the module + run: | + mapfile -t ARGS < out/build-args + docker build --target pins -f vezha/Dockerfile "${ARGS[@]}" -t "$LOCAL-pins" . + + - name: AC4 build stage and binary hash A + run: | + mapfile -t ARGS < out/build-args + docker build --target build -f vezha/Dockerfile "${ARGS[@]}" -t "$LOCAL-build" . + cid=$(docker create "$LOCAL-build") + docker cp "$cid:/out/$BIN" "out/$BIN.a" + docker cp "$cid:/out/$BIN.sha256" out/sha.a + docker rm "$cid" > /dev/null + A=$(sha256sum "out/$BIN.a" | cut -d' ' -f1) + if [ "$A" != "$(cat out/sha.a)" ]; then + echo "REFUSED build: the extracted binary does not match the build stage's sha file" + exit 1 + fi + echo "BINARY_SHA256=$A" >> "$GITHUB_ENV" + echo "binary sha256 A: $A" + + - name: AC6 final image and content check + run: | + mapfile -t ARGS < out/build-args + sh vezha/gates.sh render-source vezha/SOURCE.tmpl vezha/SOURCE + docker build -f vezha/Dockerfile "${ARGS[@]}" "--build-arg=RECIPE_COMMIT=$GITHUB_SHA" "--build-arg=BINARY_SHA256=$BINARY_SHA256" -t "$IMG" . + RECIPE_COMMIT="$GITHUB_SHA" sh vezha/check-image.sh "$KIND" "$IMG" + + - name: AC4 version output carries the pinned release and commit + run: | + out=$(docker run --rm --label "io.vezha.smoke=$GITHUB_RUN_ID" "$IMG" --version) + echo "$out" + case "$out" in + *"$UPSTREAM_RELEASE"*"commit-id=$UPSTREAM_COMMIT"*) echo "version output names $UPSTREAM_RELEASE and $UPSTREAM_COMMIT" ;; + *) echo "REFUSED version: --version does not name $UPSTREAM_RELEASE and commit-id=$UPSTREAM_COMMIT"; exit 1 ;; + esac + + - name: AC5 reproducible, second no-cache build of the build stage + run: | + mapfile -t ARGS < out/build-args + docker build --no-cache --target build -f vezha/Dockerfile "${ARGS[@]}" -t "$LOCAL-build2" . + cid=$(docker create "$LOCAL-build2") + docker cp "$cid:/out/$BIN" "out/$BIN.b" + docker rm "$cid" > /dev/null + B=$(sha256sum "out/$BIN.b" | cut -d' ' -f1) + if [ "$B" != "$BINARY_SHA256" ]; then + echo "REFUSED reproducible: second build has sha256 $B, first has $BINARY_SHA256" + exit 1 + fi + echo "both builds have sha256 $B" + + - name: AC7 source offer in the image resolves anonymously + run: sh vezha/gates.sh source-offer "$IMG" "$KIND" "$UPSTREAM_RELEASE" + + - name: AC4 the mc inside the image is the pinned mc release + run: | + out=$(docker run --rm --entrypoint /usr/bin/mc --label "io.vezha.smoke=$GITHUB_RUN_ID" "$IMG" --version) + echo "$out" + case "$out" in + *RELEASE.2025-08-13T08-35-41Z*) echo "mc --version names RELEASE.2025-08-13T08-35-41Z" ;; + *) echo "REFUSED version: /usr/bin/mc --version does not name RELEASE.2025-08-13T08-35-41Z"; exit 1 ;; + esac + + - name: AC8 smoke of the built server with the mc image + run: HEALTH=yes sh vezha/smoke.sh smoke "$IMG" "$MC_IMAGE" + + - name: AC9 volume continuity from the stopgap server to the built server + run: sh vezha/smoke.sh continuity git.vezha.io/para/minio@sha256:a1a8bd4ac40ad7881a245bab97323e18f971e4d4cba2c2007ec1bedd21cbaba2 "$IMG" "$MC_IMAGE" + + - name: AC10 preflight, write-once tag and the stopgap tags before + run: | + sh vezha/gates.sh preflight "$KIND" "$GITHUB_REF" "${MC_IMAGE:-}" + sh vezha/gates.sh tag "$PKG" "$TAG" + sh vezha/gates.sh stopgap | tee out/stopgap.before + + - name: publish tools, pinned syft and oras installed before any login + if: github.ref == 'refs/heads/vezha' + run: | + curl -fsSLO https://github.com/anchore/syft/releases/download/v1.50.0/syft_1.50.0_linux_amd64.tar.gz + echo "bf7b29ff57f06da30918266a0e1c2885a8f99784798d1bdb1628886aa015d788 syft_1.50.0_linux_amd64.tar.gz" | sha256sum -c - + tar -xzf syft_1.50.0_linux_amd64.tar.gz syft + install -m 0755 syft /usr/local/bin/syft + curl -fsSLO https://github.com/oras-project/oras/releases/download/v1.3.3/oras_1.3.3_linux_amd64.tar.gz + echo "9ce999f8d2de03fc03968b29d743077a58783e545e5eaa53917ca177352d0e59 oras_1.3.3_linux_amd64.tar.gz" | sha256sum -c - + tar -xzf oras_1.3.3_linux_amd64.tar.gz oras + install -m 0755 oras /usr/local/bin/oras + syft version + oras version + + - name: publish SBOM generated before the image + if: github.ref == 'refs/heads/vezha' + run: | + syft "docker:$IMG" -o "spdx-json=out/sbom.spdx.json" + test -s out/sbom.spdx.json + + - name: publish login to the registry + if: github.ref == 'refs/heads/vezha' + env: + REGISTRY_USER: ${{ secrets.REGISTRY_USER || github.actor }} + REGISTRY_TOKEN: ${{ secrets.REGISTRY_TOKEN }} + run: | + sh vezha/gates.sh login-guard + echo "$REGISTRY_TOKEN" | docker login "$REGISTRY" -u "$REGISTRY_USER" --password-stdin + + - name: publish SBOM artifact pushed before the image + if: github.ref == 'refs/heads/vezha' + run: cd out && oras push "$REGISTRY/para/$PKG-sbom:$TAG" --artifact-type application/spdx+json sbom.spdx.json:application/spdx+json + + - name: publish the image and cross-check its digest + if: github.ref == 'refs/heads/vezha' + run: | + docker push "$IMG" | tee out/push.log + D=$(sed -n 's/.*digest: \(sha256:[0-9a-f]\{64\}\).*/\1/p' out/push.log | tail -1) + if [ -z "$D" ]; then + echo "REFUSED push: no digest in the push output" + exit 1 + fi + R=$(sh vezha/gates.sh digest "$PKG" "$TAG") + if [ "$R" != "$D" ]; then + echo "REFUSED push: docker push printed $D but the registry serves $R for $TAG" + exit 1 + fi + echo "DIGEST=$D" >> "$GITHUB_ENV" + + - name: publish AC11 digest proof, pull by digest and compare the binary + if: github.ref == 'refs/heads/vezha' + run: | + REF="$REGISTRY/para/$PKG@$DIGEST" + docker rmi "$IMG" > /dev/null + sh vezha/gates.sh prove "$REF" "$BIN" "$BINARY_SHA256" + docker run --rm --label "io.vezha.smoke=$GITHUB_RUN_ID" "$REF" --version + oras pull "$REGISTRY/para/$PKG-sbom:$TAG" -o out/sbom-back + grep -q '"spdxVersion"' out/sbom-back/sbom.spdx.json + echo "the SBOM was pulled back and is SPDX JSON" + + - name: publish AC11 the PUBLISHED line + if: github.ref == 'refs/heads/vezha' + run: | + LINE="PUBLISHED $REGISTRY/para/$PKG@$DIGEST tag=$TAG binary-sha256=$BINARY_SHA256" + echo "$LINE" | grep -Eq '^PUBLISHED git\.vezha\.io/para/(minio|minio-mc)@sha256:[0-9a-f]{64} tag=[a-z0-9._-]+ binary-sha256=[0-9a-f]{64}$' || { echo "REFUSED published: malformed line"; exit 1; } + echo "$LINE" + echo "$LINE" > out/published.txt + cd out && oras push "$REGISTRY/para/$PKG-published:$TAG" published.txt:text/plain + + - name: publish AC10 stopgap tags unchanged after the run + if: github.ref == 'refs/heads/vezha' + run: | + sh vezha/gates.sh stopgap > out/stopgap.after + diff out/stopgap.before out/stopgap.after + echo "the stopgap tags resolve to the same digests as before the run" + + - name: publish AC7 source offer of the pulled digest resolves anonymously + if: github.ref == 'refs/heads/vezha' + run: sh vezha/gates.sh source-offer "$REGISTRY/para/$PKG@$DIGEST" "$KIND" "$UPSTREAM_RELEASE" + + - name: cleanup of this run's containers, networks, volumes and images + if: always() + run: | + for c in $(docker ps -aq --filter "label=io.vezha.smoke=$GITHUB_RUN_ID"); do docker rm -f "$c" > /dev/null 2>&1 || true; done + for n in $(docker network ls -q --filter "label=io.vezha.smoke=$GITHUB_RUN_ID"); do docker network rm "$n" > /dev/null 2>&1 || true; done + for v in $(docker volume ls -q --filter "label=io.vezha.smoke=$GITHUB_RUN_ID"); do docker volume rm "$v" > /dev/null 2>&1 || true; done + for i in "${LOCAL:-none}-pins" "${LOCAL:-none}-build" "${LOCAL:-none}-build2" "${IMG:-none}" "${REGISTRY}/para/${PKG}@${DIGEST:-none}"; do docker rmi "$i" > /dev/null 2>&1 || true; done + docker logout "$REGISTRY" > /dev/null 2>&1 || true diff --git a/vezha/check-image.sh b/vezha/check-image.sh new file mode 100644 index 000000000..8ea1c7ed0 --- /dev/null +++ b/vezha/check-image.sh @@ -0,0 +1,138 @@ +#!/bin/sh +set -eu + +kind=${1:?usage: check-image.sh } +image=${2:?usage: check-image.sh } +REGISTRY=git.vezha.io +OWNER=para +STOPGAP_MC_DIGEST=sha256:eb4ea9884b77704230e2423e9004d2fa738dc272876b9cc41a297d29443b8780 + +fail() { + printf 'REFUSED image: %s\n' "$1" >&2 + exit 1 +} + +for v in UPSTREAM_RELEASE UPSTREAM_COMMIT RECIPE_COMMIT BINARY_SHA256 GO_BUILDER MODULE_VERSION MODULE_SUM; do + eval "val=\${$v:-}" + [ -n "$val" ] || fail "environment variable $v is empty" +done + +tmp=$(mktemp -d) +cid="" +cleanup() { + if [ -n "$cid" ]; then + docker rm -f "$cid" > /dev/null 2>&1 || true + fi + rm -rf "$tmp" +} +trap cleanup EXIT + +cid=$(docker create "$image") +docker export "$cid" > "$tmp/fs.tar" +tar -tvf "$tmp/fs.tar" | sed 's| link to .*||; s| -> .*||' > "$tmp/list.txt" +docker inspect "$cid" > "$tmp/inspect.json" + +mode_of() { + awk -v p="$1" '{ n = $NF; sub(/^\.\//, "", n); sub(/\/$/, "", n); if (n == p) { print $1; exit } }' "$tmp/list.txt" +} + +need_file() { + m=$(mode_of "$1") + [ -n "$m" ] || fail "$image has no /$1" + echo "image: /$1 present ($m)" +} + +member() { + tar -xOf "$tmp/fs.tar" "$1" 2> /dev/null || tar -xOf "$tmp/fs.tar" "./$1" +} + +cfg() { + jq -r "$1" "$tmp/inspect.json" +} + +bin=$kind +need_file "usr/bin/$bin" +need_file etc/ssl/certs/ca-certificates.crt +need_file licenses/LICENSE +need_file licenses/CREDITS +need_file licenses/SOURCE +case $kind in + mc) + need_file bin/sh + ;; + minio) + need_file usr/bin/mc + need_file etc/passwd + need_file etc/group + need_file licenses/mc/LICENSE + need_file licenses/mc/CREDITS + if [ "${MC_IMAGE:-}" != "$REGISTRY/$OWNER/minio-mc@$STOPGAP_MC_DIGEST" ]; then + need_file licenses/mc/SOURCE + fi + [ "$(mode_of tmp)" = drwxrwxrwt ] || fail "/tmp of $image is not mode 1777" + ;; +esac + +want_label() { + got=$(cfg ".[0].Config.Labels[\"$1\"] // \"\"") + [ "$got" = "$2" ] || fail "label $1 is '$got', expected '$2'" + echo "image: label $1 = $got" +} +want_label org.opencontainers.image.source "https://$REGISTRY/$OWNER/$kind" +want_label org.opencontainers.image.revision "$UPSTREAM_COMMIT" +want_label org.opencontainers.image.version "$UPSTREAM_RELEASE" +want_label org.opencontainers.image.licenses AGPL-3.0 +want_label io.vezha.recipe "$RECIPE_COMMIT" +want_label io.vezha.binary.sha256 "$BINARY_SHA256" + +want_cfg() { + got=$(cfg "$1") + [ "$got" = "$2" ] || fail "$3 is '$got', expected '$2'" + echo "image: $3 = $got" +} +user=$(cfg '.[0].Config.User // ""') +case $user in + "" | root | 0) echo "image: runs as root" ;; + *) fail "the image runs as user '$user', expected root" ;; +esac +case $kind in + mc) + want_cfg '.[0].Config.Entrypoint | tostring' '["mc"]' Entrypoint + want_cfg '.[0].Config.Cmd | tostring' 'null' Cmd + want_cfg '.[0].Config.Healthcheck | tostring' 'null' Healthcheck + ;; + minio) + want_cfg '.[0].Config.Entrypoint | tostring' '["/usr/bin/minio"]' Entrypoint + want_cfg '.[0].Config.Cmd | tostring' '["server","/data"]' Cmd + want_cfg '.[0].Config.Volumes | keys | tostring' '["/data"]' Volumes + want_cfg '.[0].Config.ExposedPorts | keys | tostring' '["9000/tcp"]' ExposedPorts + want_cfg '.[0].Config.Healthcheck.Test | tostring' '["CMD","/usr/bin/mc","ready","local"]' Healthcheck + want_cfg '[.[0].Config.Env[] | select(. == "MC_CONFIG_DIR=/tmp/.mc")] | length' 1 MC_CONFIG_DIR + want_cfg '[.[0].Config.Env[] | select(. == "PATH=/usr/bin")] | length' 1 PATH + ;; +esac + +got=$(member "usr/bin/$bin" | sha256sum | cut -d' ' -f1) +[ "$got" = "$BINARY_SHA256" ] || fail "/usr/bin/$bin has sha256 $got, expected $BINARY_SHA256" +echo "image: /usr/bin/$bin sha256 $got" + +member licenses/LICENSE | cmp -s - src/LICENSE || fail "/licenses/LICENSE differs from src/LICENSE" +member licenses/CREDITS | cmp -s - src/CREDITS || fail "/licenses/CREDITS differs from src/CREDITS" +member licenses/SOURCE > "$tmp/SOURCE" +for needle in \ + "https://github.com/minio/$kind" \ + "$UPSTREAM_RELEASE" \ + "$UPSTREAM_COMMIT" \ + "https://$REGISTRY/$OWNER/$kind/src/tag/$UPSTREAM_RELEASE" \ + "https://$REGISTRY/$OWNER/$kind/archive/$UPSTREAM_RELEASE.tar.gz" \ + "https://$REGISTRY/$OWNER/$kind/src/commit/$RECIPE_COMMIT" \ + "$GO_BUILDER" \ + "$MODULE_VERSION" \ + "$MODULE_SUM" \ + "$BINARY_SHA256"; do + grep -qF "$needle" "$tmp/SOURCE" || fail "/licenses/SOURCE does not name $needle" +done +echo "image: /licenses/SOURCE names the upstream, the tag, the recipe, the builder, the module and the binary" + +[ -f .dockerignore ] || fail "no .dockerignore at the repository root" +echo "image: all checks passed for $image" diff --git a/vezha/gates.sh b/vezha/gates.sh new file mode 100644 index 000000000..b1f072dd1 --- /dev/null +++ b/vezha/gates.sh @@ -0,0 +1,426 @@ +#!/bin/sh +set -eu + +REGISTRY=git.vezha.io +OWNER=para +STOPGAP_SERVER_TAG=release.2025-09-07t16-13-09z +STOPGAP_SERVER_DIGEST=sha256:a1a8bd4ac40ad7881a245bab97323e18f971e4d4cba2c2007ec1bedd21cbaba2 +STOPGAP_MC_TAG=release.2025-08-13t08-35-41z +STOPGAP_MC_DIGEST=sha256:eb4ea9884b77704230e2423e9004d2fa738dc272876b9cc41a297d29443b8780 +TAG_RE='^release\.[0-9]{4}-[0-9]{2}-[0-9]{2}t[0-9]{2}-[0-9]{2}-[0-9]{2}z-[0-9a-f]{12}-r[0-9a-f]{8}$' +ACCEPT='application/vnd.oci.image.index.v1+json, application/vnd.oci.image.manifest.v1+json, application/vnd.docker.distribution.manifest.v2+json, application/vnd.docker.distribution.manifest.list.v2+json' +GUARD="if: github.ref == 'refs/heads/vezha'" +SELF=$0 + +refuse() { + printf 'REFUSED %s: %s\n' "$1" "$2" >&2 + exit 1 +} + +pin_value() { + sed -n "s/^$1=//p" "$2" +} + +anon_head() { + token=$(curl -fsS "https://$REGISTRY/v2/token?service=container_registry&scope=repository:$OWNER/$1:pull" | jq -r .token) || return 1 + curl -sS -o /dev/null -D - -H "Authorization: Bearer $token" -H "Accept: $ACCEPT" "https://$REGISTRY/v2/$OWNER/$1/manifests/$2" \ + | tr -d '\r' | awk '/^HTTP/ { s = $2 } tolower($1) == "docker-content-digest:" { d = $2 } END { print s, d }' +} + +fetch_ok() { + i=0 + code=none + while [ "$i" -lt 3 ]; do + code=$(curl -sS -o /dev/null -L -m 900 -w '%{http_code}' "$1" || true) + if [ "$code" = 200 ]; then + return 0 + fi + i=$((i + 1)) + sleep 5 + done + echo "$1 answered $code" >&2 + return 1 +} + +cmd_pins() { + kind=$1 + file=${2:-vezha/PINS} + case $kind in + mc) last=BASE_IMAGE ;; + minio) last=MC_IMAGE ;; + *) refuse pins "unknown kind $kind" ;; + esac + keys="UPSTREAM_RELEASE UPSTREAM_COMMIT MODULE MODULE_VERSION MODULE_SUM GO_BUILDER $last" + [ -f "$file" ] || refuse pins "$file is missing" + n=0 + while IFS= read -r line || [ -n "$line" ]; do + n=$((n + 1)) + if printf '%s' "$line" | grep -q '[[:space:]"'"'"']'; then + refuse pins "line $n contains a space or a quote" + fi + printf '%s' "$line" | grep -Eq '^[A-Z_]+=' || refuse pins "line $n is not KEY=VALUE" + k=${line%%=*} + known=0 + for x in $keys; do + if [ "$x" = "$k" ]; then + known=1 + fi + done + [ "$known" = 1 ] || refuse pins "unknown key $k" + done < "$file" + for k in $keys; do + c=$(grep -c "^$k=" "$file" || true) + [ "$c" -ge 1 ] || refuse pins "missing key $k" + [ "$c" -le 1 ] || refuse pins "duplicate key $k" + [ -n "$(pin_value "$k" "$file")" ] || refuse pins "empty value for key $k" + done + shape() { + printf '%s' "$(pin_value "$1" "$file")" | grep -Eq "$2" || refuse pins "key $1 has an unexpected form" + } + shape UPSTREAM_RELEASE '^RELEASE\.[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}-[0-9]{2}-[0-9]{2}Z$' + shape UPSTREAM_COMMIT '^[0-9a-f]{40}$' + shape MODULE "^github\\.com/minio/$kind\$" + shape MODULE_VERSION '^v0\.0\.0-[0-9]{14}-[0-9a-f]{12}$' + shape MODULE_SUM '^h1:[A-Za-z0-9+/]{43}=$' + for k in $keys; do + printf '%s=%s\n' "$k" "$(pin_value "$k" "$file")" + done +} + +wf_lint() { + f=$1 + if grep -q 'pull_request' "$f"; then + refuse refs "the workflow has a pull_request trigger" + fi + if grep -n 'latest' "$f" | grep -v 'ubuntu-latest'; then + refuse refs "the workflow mentions latest (lines above)" + fi + grep -q '^ contents: read$' "$f" || refuse refs "permissions contents: read is missing" + awk -v g="$GUARD" ' + function flush() { + if (name == "") return + if (secret) secretsteps++ + if ((risky || secret) && !guarded) { + printf "REFUSED refs: step \"%s\" can push or read a secret and has no vezha-ref guard\n", name > "/dev/stderr" + bad = 1 + } + } + /^ - name: / { flush(); name = $0; sub(/^ - name: /, "", name); risky = 0; secret = 0; guarded = 0 } + /docker login|docker push|oras push/ { risky = 1 } + /secrets\./ { secret = 1 } + index($0, g) { guarded = 1 } + END { + flush() + if (secretsteps != 1) { + printf "REFUSED refs: secrets. appears in %d steps, expected exactly 1\n", secretsteps > "/dev/stderr" + bad = 1 + } + exit bad + } + ' "$f" || exit 1 +} + +cmd_wf_lint() { + wf_lint "${1:-.gitea/workflows/release.yml}" +} + +cmd_refs() { + kind=$1 + df=vezha/Dockerfile + pins=vezha/PINS + case $kind in + mc) last=BASE_IMAGE ;; + minio) last=MC_IMAGE ;; + *) refuse refs "unknown kind $kind" ;; + esac + if grep -nE '^ARG[[:space:]]+[A-Za-z_][A-Za-z0-9_]*=' "$df"; then + refuse refs "a Dockerfile ARG has a default (lines above)" + fi + for k in GO_BUILDER $last; do + printf '%s' "$(pin_value "$k" "$pins")" | grep -Eq '^[^@[:space:]]+@sha256:[0-9a-f]{64}$' || refuse refs "$k is not pinned by @sha256 digest" + done + if [ "$kind" = minio ]; then + printf '%s' "$(pin_value MC_IMAGE "$pins")" | grep -Eq "^$REGISTRY/$OWNER/minio-mc@sha256:[0-9a-f]{64}\$" || refuse refs "MC_IMAGE is not $REGISTRY/$OWNER/minio-mc@sha256:" + fi + stages="" + while IFS= read -r line; do + case $line in + FROM\ *) ;; + *) continue ;; + esac + # shellcheck disable=SC2086 + set -- $line + shift + case $1 in --*) refuse refs "FROM option $1 is not allowed" ;; esac + img=$1 + case $img in + scratch) ;; + '${GO_BUILDER}' | '${BASE_IMAGE}' | '${MC_IMAGE}') + var=${img#\$\{} + var=${var%\}} + [ "$var" = GO_BUILDER ] || [ "$var" = "$last" ] || refuse refs "FROM uses $var which is not a pinned image key of this repo" + ;; + *@sha256:*) + printf '%s' "$img" | grep -Eq '@sha256:[0-9a-f]{64}$' || refuse refs "FROM $img has a malformed digest" + ;; + *) + found=0 + for s in $stages; do + if [ "$s" = "$img" ]; then + found=1 + fi + done + [ "$found" = 1 ] || refuse refs "FROM $img is not pinned by digest" + ;; + esac + if [ "${2:-}" = AS ]; then + stages="$stages $3" + fi + done < "$df" + for f in $(grep -oE '^COPY[[:space:]]+--from=[^[:space:]]+' "$df" | sed 's/.*--from=//'); do + case $f in + *'$'*) refuse refs "COPY --from=$f takes a variable" ;; + esac + found=0 + for s in $stages; do + if [ "$s" = "$f" ]; then + found=1 + fi + done + [ "$found" = 1 ] || refuse refs "COPY --from=$f is not a stage of this Dockerfile" + done + wf_lint .gitea/workflows/release.yml +} + +cmd_provenance_remote() { + : "${GITHUB_REPOSITORY:?}" "${UPSTREAM_RELEASE:?}" "${UPSTREAM_COMMIT:?}" + body=$(curl -fsS "https://$REGISTRY/api/v1/repos/$GITHUB_REPOSITORY") || refuse provenance "anonymous GET of the repository failed" + [ "$(printf '%s' "$body" | jq -r .private)" = false ] || refuse provenance "the repository is not public" + [ "$(printf '%s' "$body" | jq -r .has_actions)" = true ] || refuse provenance "the repository has no actions" + [ "$(printf '%s' "$body" | jq -r .default_branch)" = vezha ] || refuse provenance "the default branch is not vezha" + got=$(GIT_TERMINAL_PROMPT=0 git ls-remote "https://$REGISTRY/$GITHUB_REPOSITORY" "refs/tags/${UPSTREAM_RELEASE}^{}" | cut -f1) + [ "$got" = "$UPSTREAM_COMMIT" ] || refuse provenance "tag $UPSTREAM_RELEASE resolves to '$got' but PINS says UPSTREAM_COMMIT=$UPSTREAM_COMMIT" + echo "provenance: tag $UPSTREAM_RELEASE resolves to $got" +} + +cmd_provenance_src() { + : "${UPSTREAM_COMMIT:?}" + head=$(git -C src rev-parse HEAD) + [ "$head" = "$UPSTREAM_COMMIT" ] || refuse provenance "src HEAD is $head but PINS says UPSTREAM_COMMIT=$UPSTREAM_COMMIT" + echo "provenance: src HEAD is $head" +} + +cmd_source_urls() { + : "${GITHUB_REPOSITORY:?}" "${UPSTREAM_RELEASE:?}" + for u in "https://$REGISTRY/$GITHUB_REPOSITORY/src/tag/$UPSTREAM_RELEASE" "https://$REGISTRY/$GITHUB_REPOSITORY/archive/$UPSTREAM_RELEASE.tar.gz"; do + fetch_ok "$u" || refuse source-urls "anonymous fetch of $u failed" + echo "source-urls: $u -> 200" + done +} + +cmd_make_tag() { + : "${UPSTREAM_RELEASE:?}" "${UPSTREAM_COMMIT:?}" "${GITHUB_SHA:?}" + lower=$(printf '%s' "$UPSTREAM_RELEASE" | tr 'A-Z' 'a-z') + printf '%s-%s-r%s\n' "$lower" "$(printf '%s' "$UPSTREAM_COMMIT" | cut -c1-12)" "$(printf '%s' "$GITHUB_SHA" | cut -c1-8)" +} + +cmd_tag() { + pkg=$1 + tag=$2 + case $tag in + "$STOPGAP_SERVER_TAG" | "$STOPGAP_MC_TAG") refuse tag "$tag is a stopgap tag and is never written" ;; + esac + printf '%s' "$tag" | grep -Eq "$TAG_RE" || refuse tag "$tag does not have the immutable tag form" + r=$(anon_head "$pkg" "$tag") || refuse tag "the anonymous registry token request failed" + case ${r%% *} in + 404) echo "tag: $pkg:$tag does not exist yet" ;; + 200) refuse tag "$pkg:$tag already exists, tags are write-once" ;; + *) refuse tag "the registry answered ${r%% *} for $pkg:$tag" ;; + esac +} + +cmd_digest() { + r=$(anon_head "$1" "$2") || refuse digest "the anonymous registry token request failed" + [ "${r%% *}" = 200 ] || refuse digest "the registry answered ${r%% *} for $1:$2" + printf '%s\n' "${r#* }" +} + +cmd_stopgap() { + s=$(anon_head minio "$STOPGAP_SERVER_TAG") || refuse stopgap "the anonymous registry token request failed" + m=$(anon_head minio-mc "$STOPGAP_MC_TAG") || refuse stopgap "the anonymous registry token request failed" + [ "${s#* }" = "$STOPGAP_SERVER_DIGEST" ] || refuse stopgap "minio:$STOPGAP_SERVER_TAG resolves to '${s#* }'" + [ "${m#* }" = "$STOPGAP_MC_DIGEST" ] || refuse stopgap "minio-mc:$STOPGAP_MC_TAG resolves to '${m#* }'" + echo "minio:$STOPGAP_SERVER_TAG ${s#* }" + echo "minio-mc:$STOPGAP_MC_TAG ${m#* }" +} + +cmd_preflight() { + kind=$1 + ref=$2 + mc_image=${3:-} + [ "$kind" = minio ] || return 0 + [ "$ref" = refs/heads/vezha ] || return 0 + printf '%s' "$mc_image" | grep -Eq "^$REGISTRY/$OWNER/minio-mc@sha256:[0-9a-f]{64}\$" || refuse preflight "MC_IMAGE '$mc_image' is not $REGISTRY/$OWNER/minio-mc@sha256:" + [ "$mc_image" != "$REGISTRY/$OWNER/minio-mc@$STOPGAP_MC_DIGEST" ] || refuse preflight "MC_IMAGE is the stopgap digest, which is never published from vezha" +} + +cmd_login_guard() { + [ -n "${REGISTRY_TOKEN:-}" ] || { + echo "::error::REGISTRY_TOKEN secret is not set. Create a Gitea PAT with write:package scope and add it under repo Settings, Actions, Secrets." + refuse login-guard "REGISTRY_TOKEN is not set" + } +} + +cmd_put_denied() { + [ "$1" -ne 0 ] || refuse put-denied "the read-only user's PUT succeeded" + grep -Eqi 'access denied|insufficient permissions' "$2" || refuse put-denied "the PUT failed but not with Access Denied" +} + +cmd_prove() { + ref=$1 + bin=$2 + want=$3 + docker pull "$ref" > /dev/null || refuse prove "pull of $ref failed" + cid=$(docker create "$ref") || refuse prove "docker create of $ref failed" + tmp=$(mktemp -d) + if ! docker cp "$cid:/usr/bin/$bin" "$tmp/$bin" > /dev/null; then + docker rm -f "$cid" > /dev/null + refuse prove "no /usr/bin/$bin in $ref" + fi + label=$(docker inspect -f '{{index .Config.Labels "io.vezha.binary.sha256"}}' "$cid") + docker rm -f "$cid" > /dev/null + got=$(sha256sum "$tmp/$bin" | cut -d' ' -f1) + rm -rf "$tmp" + [ "$got" = "$want" ] || refuse prove "/usr/bin/$bin in $ref has sha256 $got, expected $want" + [ "$label" = "$want" ] || refuse prove "label io.vezha.binary.sha256 of $ref is '$label', expected $want" + echo "prove: $ref carries /usr/bin/$bin with sha256 $got" +} + +cmd_render_source() { + tmpl=$1 + out=$2 + sed \ + -e "s|@UPSTREAM_RELEASE@|$UPSTREAM_RELEASE|g" \ + -e "s|@UPSTREAM_COMMIT@|$UPSTREAM_COMMIT|g" \ + -e "s|@RECIPE_COMMIT@|$GITHUB_SHA|g" \ + -e "s|@GO_BUILDER@|$GO_BUILDER|g" \ + -e "s|@MODULE@|$MODULE|g" \ + -e "s|@MODULE_VERSION@|$MODULE_VERSION|g" \ + -e "s|@MODULE_SUM@|$MODULE_SUM|g" \ + -e "s|@BINARY_SHA256@|$BINARY_SHA256|g" \ + -e "s|@BASE_IMAGE@|${BASE_IMAGE:-none}|g" \ + -e "s|@MC_IMAGE@|${MC_IMAGE:-none}|g" \ + "$tmpl" > "$out" + if grep -n '@[A-Z_0-9]*@' "$out"; then + refuse render-source "an unrendered placeholder remains (lines above)" + fi +} + +cmd_source_offer() { + image=$1 + repo=$2 + release=$3 + cid=$(docker create "$image") || refuse source-offer "docker create of $image failed" + tmp=$(mktemp -d) + if ! docker cp "$cid:/licenses" "$tmp/licenses" > /dev/null; then + docker rm -f "$cid" > /dev/null + refuse source-offer "$image has no /licenses" + fi + docker rm -f "$cid" > /dev/null + tagurl="https://$REGISTRY/$OWNER/$repo/src/tag/$release" + grep -qF "$tagurl" "$tmp/licenses/SOURCE" || refuse source-offer "/licenses/SOURCE does not name $tagurl" + urls=$(grep -rohE "https://$REGISTRY/[^[:space:])>\"']+" "$tmp/licenses" | sort -u) + [ -n "$urls" ] || refuse source-offer "no $REGISTRY URL in /licenses" + for u in $urls; do + fetch_ok "$u" || refuse source-offer "anonymous fetch of $u failed" + echo "source-offer: $u -> 200" + done + rm -rf "$tmp" +} + +control() { + name=$1 + gate=$2 + shift 2 + if out=$("$@" 2>&1); then + echo "CONTROL FAILED: $name was accepted" >&2 + exit 1 + fi + printf '%s' "$out" | grep -q "REFUSED $gate:" || { + echo "CONTROL FAILED: $name was refused without the REFUSED $gate message: $out" >&2 + exit 1 + } + echo "control ok: $name refused" +} + +accept() { + name=$1 + shift + "$@" > /dev/null 2>&1 || { + echo "CONTROL FAILED: $name was refused" >&2 + exit 1 + } + echo "control ok: $name accepted" +} + +cmd_self_test() { + tmp=$(mktemp -d) + mc_stop="$REGISTRY/$OWNER/minio-mc@$STOPGAP_MC_DIGEST" + control "stopgap server tag" tag sh "$SELF" tag minio "$STOPGAP_SERVER_TAG" + control "stopgap mc tag" tag sh "$SELF" tag minio-mc "$STOPGAP_MC_TAG" + control "moving tag" tag sh "$SELF" tag minio-mc latest + control "stopgap digest as MC_IMAGE on vezha" preflight sh "$SELF" preflight minio refs/heads/vezha "$mc_stop" + control "tag form MC_IMAGE on vezha" preflight sh "$SELF" preflight minio refs/heads/vezha "$REGISTRY/$OWNER/minio-mc:$STOPGAP_MC_TAG" + control "empty MC_IMAGE on vezha" preflight sh "$SELF" preflight minio refs/heads/vezha "" + accept "stopgap MC_IMAGE off vezha" sh "$SELF" preflight minio refs/heads/dev/x "$mc_stop" + control "pull-by-digest of a different manifest" prove sh "$SELF" prove "$mc_stop" mc 0000000000000000000000000000000000000000000000000000000000000000 + control "empty REGISTRY_TOKEN" login-guard env REGISTRY_TOKEN= sh "$SELF" login-guard + accept "set REGISTRY_TOKEN" env REGISTRY_TOKEN=present sh "$SELF" login-guard + printf 'mc: Unable to put. Access Denied.\n' > "$tmp/denied.txt" + printf 'done\n' > "$tmp/ok.txt" + control "allowed PUT" put-denied sh "$SELF" put-denied 0 "$tmp/ok.txt" + control "PUT that failed for another reason" put-denied sh "$SELF" put-denied 1 "$tmp/ok.txt" + accept "denied PUT" sh "$SELF" put-denied 1 "$tmp/denied.txt" + printf 'mc: Unable to write to one or more targets. Insufficient permissions to access this path.\n' > "$tmp/insufficient.txt" + accept "PUT refused for insufficient permissions" sh "$SELF" put-denied 1 "$tmp/insufficient.txt" + if [ -n "$(pin_value BASE_IMAGE vezha/PINS)" ]; then + kind=mc + last=BASE_IMAGE + else + kind=minio + last=MC_IMAGE + fi + for k in UPSTREAM_RELEASE UPSTREAM_COMMIT MODULE MODULE_VERSION MODULE_SUM GO_BUILDER $last; do + grep -v "^$k=" vezha/PINS > "$tmp/pins.missing" || true + sed "s|^$k=.*|$k=|" vezha/PINS > "$tmp/pins.empty" + control "PINS without $k" pins sh "$SELF" pins "$kind" "$tmp/pins.missing" + control "PINS with empty $k" pins sh "$SELF" pins "$kind" "$tmp/pins.empty" + done + printf 'on:\n pull_request:\njobs:\n' > "$tmp/wf.pr" + control "workflow with a pull_request trigger" refs sh "$SELF" wf-lint "$tmp/wf.pr" + rm -rf "$tmp" + echo "self-test: every control behaved" +} + +cmd=${1:?usage: gates.sh [args]} +shift +case $cmd in + pins) cmd_pins "$@" ;; + refs) cmd_refs "$@" ;; + wf-lint) cmd_wf_lint "$@" ;; + provenance-remote) cmd_provenance_remote ;; + provenance-src) cmd_provenance_src ;; + source-urls) cmd_source_urls ;; + make-tag) cmd_make_tag ;; + tag) cmd_tag "$@" ;; + digest) cmd_digest "$@" ;; + stopgap) cmd_stopgap ;; + preflight) cmd_preflight "$@" ;; + login-guard) cmd_login_guard ;; + put-denied) cmd_put_denied "$@" ;; + prove) cmd_prove "$@" ;; + render-source) cmd_render_source "$@" ;; + source-offer) cmd_source_offer "$@" ;; + self-test) cmd_self_test ;; + *) refuse usage "unknown command $cmd" ;; +esac