diff --git a/.github/agent-guides/validation.md b/.github/agent-guides/validation.md index 71b79dd6..5ff74671 100644 --- a/.github/agent-guides/validation.md +++ b/.github/agent-guides/validation.md @@ -67,6 +67,22 @@ required CI. Legacy recovery must refuse prebuilt-marked evidence even when the new mode input is omitted. Caller/verifier adoption remains separate; #684 owns the finite rollout gate review and retirement. +**Caller-signed publication (`actions/publish-app`):** The composite signs as the +calling workflow, so its admission is the boundary. Before any registry contact it +requires a `refs/tags/v` ref, the caller's own workflow ref at that tag, the +tagged commit checked out at the workspace root without a persisted credential, and +that commit on the explicitly fetched default branch; unknown ancestry refuses. +`deploy-path` and `app-manifest` are plain names without links, and `deploy-path` +holds only regular files equal to the tagged source. It keeps the occupied-version +guard before the first write and twice more, signs by digest, and verifies both +digests against `https://github.com/$GITHUB_WORKFLOW_REF` before exposing version or +stable `latest`. No credential reaches any argv: flux uses the docker login and the +action's own version check gives curl private config files (the retiring workflows' +shared guard and `--creds` stay unchanged). The prebuilt mode reuses +`prepare-prebuilt-webapp.sh`. Keep `.github/tests/test-publish-app-action.sh`, its +composite simulation, argv record and mutation controls in required CI. Recovery is +not carried yet. + Normal publication's public CI evidence reads remain credential-free. The standalone verifier additionally supports explicit `PREBUILT_CI_AUTH_MODE=job-token` for isolated GitHub-hosted CI, with an explicitly supplied `PREBUILT_CI_READ_TOKEN`. diff --git a/.github/scripts/ci-selection/inventory.json b/.github/scripts/ci-selection/inventory.json index 08badffe..e938eefc 100644 --- a/.github/scripts/ci-selection/inventory.json +++ b/.github/scripts/ci-selection/inventory.json @@ -6,7 +6,7 @@ "test-enable-auto-merge-queue", "test-free-disk-space", "test-free-disk-space-keep", "test-govulncheck-allowlist-honored", "test-govulncheck-strict-blocks", "test-govulncheck-timeout-retry-live", "test-lint-blocks", "test-login-to-ghcr", "test-login-to-ghcr-empty-token", - "test-prepare-fixes", "test-retry-script", "test-run-dotnet-tests", "test-run-dotnet-tests-blocks", + "test-prepare-fixes", "test-publish-app", "test-retry-script", "test-run-dotnet-tests", "test-run-dotnet-tests-blocks", "test-run-dotnet-tests-coverage-inline-lockstep", "test-run-dotnet-tests-gate-lockstep", "test-run-dotnet-tests-missing-working-directory", "test-run-dotnet-tests-preserves-source-mapping", "test-setup-go-toolchain", @@ -46,7 +46,7 @@ "test-enable-auto-merge-on-pr": {"if": "github.event_name != 'merge_group' && !startsWith(github.event.head_commit.message, 'chore(main): release ') && startsWith(github.event_name, 'pull_request') && (\n github.event.pull_request.user.login == 'dependabot[bot]' ||\n github.event.pull_request.user.login == 'renovate[bot]' ||\n github.event.pull_request.user.login == 'botantler[bot]' ||\n github.event.pull_request.user.login == 'devantler'\n)"}, "test-zizmor": {"if": "${{ github.event_name == 'push' && !startsWith(github.event.head_commit.message, 'chore(main): release ') }}"}, "test-run-dotnet-tests-workflow-authenticated": {"if": "${{\n github.event_name != 'merge_group' &&\n !startsWith(github.event.head_commit.message, 'chore(main): release ') &&\n github.event_name == 'pull_request' &&\n github.event.pull_request.head.repo.full_name == github.repository &&\n github.event.pull_request.user.login == 'devantler'\n}}"}, - "test-publish-app": {"if": "${{ github.event_name == 'push' && !startsWith(github.event.head_commit.message, 'chore(main): release ') }}"}, + "test-publish-app-workflow": {"if": "${{ github.event_name == 'push' && !startsWith(github.event.head_commit.message, 'chore(main): release ') }}"}, "test-publish-app-caller-pin": {"if": "${{ github.event_name == 'push' && !startsWith(github.event.head_commit.message, 'chore(main): release ') }}"}, "test-publish-caller-pin-live": {"if": "${{ github.event_name == 'push' && !startsWith(github.event.head_commit.message, 'chore(main): release ') }}"}, "test-publish-manifests": {"if": "${{ github.event_name == 'push' && !startsWith(github.event.head_commit.message, 'chore(main): release ') }}"}, @@ -75,6 +75,7 @@ "test-free-disk-space-keep": ["actions/free-disk-space/"], "test-login-to-ghcr": ["actions/login-to-ghcr/"], "test-login-to-ghcr-empty-token": ["actions/login-to-ghcr/"], + "test-publish-app": ["actions/publish-app/"], "test-aggregate-job-checks-empty": ["actions/aggregate-job-checks/"], "test-aggregate-job-checks-success": ["actions/aggregate-job-checks/"], "test-aggregate-job-checks-failure": ["actions/aggregate-job-checks/"], @@ -118,7 +119,7 @@ "test-publish-release-recovery-disabled": [], "test-create-release-config": [], "test-release-please-semantics": [], - "test-publish-app": [], + "test-publish-app-workflow": [], "test-publish-app-caller-pin": [], "test-publish-caller-pin-live": [], "test-publish-manifests": [], diff --git a/.github/scripts/prepare-prebuilt-webapp.sh b/.github/scripts/prepare-prebuilt-webapp.sh index 081eb169..265afcfa 100644 --- a/.github/scripts/prepare-prebuilt-webapp.sh +++ b/.github/scripts/prepare-prebuilt-webapp.sh @@ -36,16 +36,30 @@ esac [[ -n "${GITHUB_OUTPUT:-}" ]] || fail 'missing workflow output file' path="${DEPLOY_PATH#./}" [[ -n "$path" && "$path" != /* && "$path" != */ && "$path" != *//* ]] || fail 'require a relative deployment directory' +# The WebApp manifest is named relative to the deployment directory; empty keeps webapp.yaml. +manifest="${APP_MANIFEST:-webapp.yaml}" +[[ "$manifest" != *$'\n'* && "$manifest" != *$'\r'* && "$manifest" != /* && "$manifest" != */ && "$manifest" != *//* ]] || + fail 'require a relative app manifest inside the deployment directory' +# Walk below the cursor one component at a time; every directory must be a regular source +# directory and no component may be `.`, `..`, hidden or unusual. +walk() { + local component components + IFS=/ read -r -a components <<< "$1" + for component in "${components[@]}"; do + [[ "$component" =~ ^[A-Za-z0-9_-][A-Za-z0-9_.-]*$ && "$component" != . && "$component" != .. ]] || fail "unsafe $2" + cursor="$cursor/$component" + [[ -d "$cursor" && ! -L "$cursor" ]] || fail 'deployment directories must be regular source directories' + done +} cursor=. -IFS=/ read -r -a components <<< "$path" -for component in "${components[@]}"; do - [[ "$component" =~ ^[A-Za-z0-9_-][A-Za-z0-9_.-]*$ && "$component" != . && "$component" != .. ]] || fail 'unsafe deployment directory' - cursor="$cursor/$component" - [[ -d "$cursor" && ! -L "$cursor" ]] || fail 'deployment directories must be regular source directories' -done -file="$cursor/webapp.yaml" -[[ -f "$file" && ! -L "$file" ]] || fail 'require a regular webapp.yaml' -unsafe="$(find "$cursor" ! -type f ! -type d -print -quit)" || fail 'cannot inspect the complete deployment directory' +walk "$path" 'deployment directory' +deploy_dir="$cursor" +manifest_name="${manifest##*/}" +[[ "$manifest_name" =~ ^[A-Za-z0-9_-][A-Za-z0-9_.-]*$ ]] || fail 'unsafe app manifest path' +[[ "$manifest" == "$manifest_name" ]] || walk "${manifest%/*}" 'app manifest path' +file="$cursor/$manifest_name" +[[ -f "$file" && ! -L "$file" ]] || fail "require a regular $manifest" +unsafe="$(find "$deploy_dir" ! -type f ! -type d -print -quit)" || fail 'cannot inspect the complete deployment directory' [[ -z "$unsafe" ]] || fail 'deployment artifacts cannot contain linked or special files' work="$(mktemp -d)" trap 'rm -rf "$work"' EXIT diff --git a/.github/tests/test-ci-catalogue-scope.sh b/.github/tests/test-ci-catalogue-scope.sh index 792b34e9..dec3d4bc 100644 --- a/.github/tests/test-ci-catalogue-scope.sh +++ b/.github/tests/test-ci-catalogue-scope.sh @@ -248,7 +248,7 @@ jq -e 'index("test-validate-go-postgresql-fixture") != null' <<<"${selected}" >/ jq -e 'index("test-go-cache-native-observer") != null' <<<"${selected}" >/dev/null || fail 'native Go cache observer left the independent coverage floor' jq -e 'index("test-go-workflow-guards") != null' <<<"${selected}" >/dev/null || fail 'grouped Go guards left the independent coverage floor' jq -en --argjson selected "${selected}" --slurpfile inventory "${repo_root}/.github/scripts/ci-selection/inventory.json" ' - ($inventory[0].catalogue_optional | length == 37) and + ($inventory[0].catalogue_optional | length == 38) and all($selected[]; . as $id | ($inventory[0].catalogue_optional | index($id)) == null) and ($selected | index("test-approve-pr-credential-boundary") != null) and ($selected | index("test-validate-go-project-apply-signed-fixes") != null) diff --git a/.github/tests/test-ci-selection.sh b/.github/tests/test-ci-selection.sh index 0a9ead2e..da027ae6 100644 --- a/.github/tests/test-ci-selection.sh +++ b/.github/tests/test-ci-selection.sh @@ -80,7 +80,7 @@ EVENT_NAME=push RUN_CATALOGUE=true CATALOGUE_SCOPE=true GITHUB_OUTPUT="$work/out go -C "$root/.github/scripts/ci-selection" run . "$root" "$root/.github/scripts/ci-selection/inventory.json" "$work/workflow.json" selected="$(sed -n 's/^selected=//p' "$work/output")" # The skill action executions moved with their standalone repositories; all remaining executions stay selected. -jq -e 'length == 61 and index("test-lint-workflow") == null and +jq -e 'length == 62 and index("test-lint-workflow") == null and index("test-lint-workflow-manual-fixes") == null' <<< "$selected" >/dev/null jq -e 'index("test-validate-go-postgresql-fixture") != null' <<<"$selected" >/dev/null jq -e 'index("test-go-cache-native-observer") != null' <<<"$selected" >/dev/null @@ -280,7 +280,7 @@ jq -e --argjson a "$action_only" --argjson r "$readme_readers" '. == ($a + $r | { echo 'FAIL: README with a recognized action lost a reader or an owner' >&2; exit 1; } for kinds in 'agents' 'readme agents' 'readme unknown' 'action agents unknown' 'readme-renamed'; do # shellcheck disable=SC2086 # Each kinds word is one fixture change. - jq -e 'length == 61' <<<"$(select_docs $kinds)" >/dev/null || + jq -e 'length == 62' <<<"$(select_docs $kinds)" >/dev/null || { echo "FAIL: root-document change '$kinds' narrowed full coverage" >&2; exit 1; } done for kinds in 'readme' 'action readme agents'; do diff --git a/.github/tests/test-prebuilt-webapp.sh b/.github/tests/test-prebuilt-webapp.sh index e1f85efc..91785d8e 100644 --- a/.github/tests/test-prebuilt-webapp.sh +++ b/.github/tests/test-prebuilt-webapp.sh @@ -354,4 +354,78 @@ reset_fixture ln -s "$work/run.json" "$work/deploy/unrelated.yaml" if invoke none > "$work/log" 2>&1; then fail 'accepted a linked file in the packaged deployment directory'; fi [[ ! -s "$work/output" && ! -s "$work/reads" ]] || fail 'linked deployment file reached verification' +rm "$work/deploy/unrelated.yaml" + +# app-manifest names the WebApp manifest inside the deployment directory; omitted, webapp.yaml is +# used as above, even beside a base/ copy. +reset_split_fixture() { # — the healthy WebApp moved to deploy/ + rm -rf "$work/deploy" "$work/outside" "$work/outside.yaml" "$work/webapp.yaml" + mkdir -p "$work/deploy" + reset_fixture + mkdir -p "$(dirname "$work/deploy/$1")" + [[ "$1" == webapp.yaml ]] || mv "$work/deploy/webapp.yaml" "$work/deploy/$1" + cp "$work/deploy/$1" "$work/before.yaml" + printf 'resources: [prod]\n' > "$work/deploy/kustomization.yaml" +} +pinned_only() { # — the selected image is pinned and every other document value is kept + yq -o=json -I=0 '.' "$1" | jq -s -e --arg image "ghcr.io/devantler-tech/app@$image_digest" ' + . == [ + {apiVersion:"v1",kind:"ConfigMap",metadata:{name:"unrelated"},data:{literal:"keep me"}}, + {apiVersion:"kro.run/v1alpha1",kind:"WebApp",metadata:{name:"app",namespace:"app"}, + spec:{name:"app",image:$image,host:"example.invalid"}} + ]' > /dev/null +} +reset_split_fixture webapp.yaml +mkdir -p "$work/deploy/base" +cp "$work/before.yaml" "$work/deploy/base/webapp.yaml" +invoke none > "$work/log" 2>&1 || fail "flat WebApp beside base/ refused: $(cat "$work/log")" +pinned_only "$work/deploy/webapp.yaml" || fail 'default did not pin the flat webapp.yaml' +cmp -s "$work/before.yaml" "$work/deploy/base/webapp.yaml" || fail 'default pinned a base/ manifest nobody named' +for manifest in base/webapp.yaml overlays/base/webapp.yaml app.yaml; do + reset_split_fixture "$manifest" + invoke none APP_MANIFEST="$manifest" > "$work/log" 2>&1 || fail "healthy WebApp at $manifest refused: $(cat "$work/log")" + pinned_only "$work/deploy/$manifest" || fail "did not pin only the selected image in $manifest" + [[ "$manifest" == app.yaml || ! -e "$work/deploy/webapp.yaml" ]] || fail "$manifest created a flat webapp.yaml" + grep -qx "digest=$image_digest" "$work/output" || fail "$manifest omitted the verified image output" +done +# Unsafe, linked or missing app-manifest values are refused before any external observation and +# change no file. A valid WebApp sits wherever each path would lead. +for case in \ + '../webapp.yaml|unsafe app manifest path' \ + '/webapp.yaml|require a relative app manifest inside the deployment directory' \ + 'base/./webapp.yaml|unsafe app manifest path' \ + 'base/../webapp.yaml|unsafe app manifest path' \ + 'base//webapp.yaml|require a relative app manifest inside the deployment directory' \ + 'base/|require a relative app manifest inside the deployment directory' \ + '.hidden/webapp.yaml|unsafe app manifest path' \ + "base/webapp.yaml"$'\n'"x|require a relative app manifest inside the deployment directory" \ + "base/webapp.yaml"$'\r'"|require a relative app manifest inside the deployment directory" \ + 'linked-dir|deployment directories must be regular source directories' \ + 'linked-file|require a regular base/webapp.yaml' \ + 'missing|require a regular base/webapp.yaml' \ + 'linked-sibling|deployment artifacts cannot contain linked or special files'; do + manifest="${case%%|*}" + expected="${case#*|}" + reset_split_fixture base/webapp.yaml + cp "$work/before.yaml" "$work/deploy/webapp.yaml" + cp "$work/before.yaml" "$work/webapp.yaml" + mkdir -p "$work/deploy/.hidden" + cp "$work/before.yaml" "$work/deploy/.hidden/webapp.yaml" + case "$manifest" in + linked-dir) mv "$work/deploy/base" "$work/outside" && ln -s ../outside "$work/deploy/base" ;; + linked-file) mv "$work/deploy/base/webapp.yaml" "$work/outside.yaml" && + ln -s ../../outside.yaml "$work/deploy/base/webapp.yaml" ;; + missing) rm "$work/deploy/base/webapp.yaml" ;; + # The whole packaged directory is scanned, not only the named manifest's directory. + linked-sibling) mkdir -p "$work/deploy/prod" && ln -s ../../run.json "$work/deploy/prod/linked.yaml" ;; + esac + case "$manifest" in linked-dir | linked-file | missing | linked-sibling) manifest=base/webapp.yaml ;; esac + find "$work" -type f -name '*webapp.yaml' -exec shasum {} + | sort > "$work/sums-before" + [[ -s "$work/sums-before" ]] || fail 'the refusal fixture has no manifests to compare' + if invoke none APP_MANIFEST="$manifest" > "$work/log" 2>&1; then fail "accepted app-manifest: $manifest"; fi + [[ ! -s "$work/output" && ! -s "$work/reads" ]] || fail "app-manifest $manifest reached verification" + grep -qF "$expected" "$work/log" || fail "app-manifest $manifest refused for the wrong reason: $(grep '::error::' "$work/log")" + find "$work" -type f -name '*webapp.yaml' -exec shasum {} + | sort | cmp -s - "$work/sums-before" || + fail "refused app-manifest $manifest changed manifests" +done echo 'prebuilt-webapp: real YAML pinning and source/CI/provenance/partial-observation refusals pass' diff --git a/.github/tests/test-publish-app-action.sh b/.github/tests/test-publish-app-action.sh new file mode 100644 index 00000000..5ce25549 --- /dev/null +++ b/.github/tests/test-publish-app-action.sh @@ -0,0 +1,1139 @@ +#!/usr/bin/env bash +# Pins #925: actions/publish-app publishes a release signed AS THE CALLING WORKFLOW, and only from +# a complete v tag whose commit is on the default branch. +# +# - Every refusal of the release tag, the signer identity, the checkout (including a persisted +# credential), the default-branch ancestry (including when it cannot be determined), the +# inputs, deploy-path (links, special files, anything that differs from the tagged commit) and +# app-manifest happens before ANY registry contact: no login, no read, no write. +# - An occupied version is refused before the first registry write. +# - Both digests are signed and then verified against https://github.com/ +# before the version (and, for stable releases, latest) is exposed; any verification, +# copy or late-occupation failure exposes nothing. +# - The token never appears in any program's arguments: every program a step runs is wrapped to +# record its argv, and no recorded argv may contain the token or its encoded header. +# +# The composite runs here step by step exactly as action.yaml declares it: each `run:` step with +# its own resolved `env:`, in a clean environment, under bash -eo pipefail; each `uses:` step must +# be a SHA-pinned action this simulation models (the image build is recorded as a registry write). +# Stub flux, cosign, docker and curl record every registry and API call; the default-branch fetch +# runs real git against a local server repository. Any expression, condition, step key or action +# the simulation does not model fails the test instead of being ignored. +# +# Mutation controls at the end remove each guard from a copy of the action and require the matching +# case to stop holding, so a guard that silently stops biting fails this test. + +# shellcheck disable=SC2016 # `${{ }}`, `$J` and friends are GitHub and jq syntax, not shell. +set -euo pipefail + +fail() { + echo "FAIL: $*" >&2 + exit 1 +} + +root="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)" +action_dir="$root/actions/publish-app" +[[ -f "$action_dir/action.yaml" ]] || fail 'run from the repository root' +scratch="$(mktemp -d)" +trap 'rm -rf "$scratch"' EXIT + +repo=devantler-tech/app +token=stub-not-a-secret +token_header="AUTHORIZATION: basic $(printf 'x-access-token:%s' "$token" | base64 | tr -d '\n')" +image_digest="sha256:$(printf 'b%.0s' $(seq 1 64))" +artifact_digest="sha256:$(printf 'a%.0s' $(seq 1 64))" +other_digest="sha256:$(printf 'c%.0s' $(seq 1 64))" +public_digest="$(printf 'd%.0s' $(seq 1 64))" + +# Fixture repositories never read the developer's Git configuration (signing, URL rewrites). +export GIT_CONFIG_NOSYSTEM=1 GIT_CONFIG_GLOBAL=/dev/null +export GIT_AUTHOR_NAME=fixture GIT_AUTHOR_EMAIL=fixture@example.invalid +export GIT_COMMITTER_NAME=fixture GIT_COMMITTER_EMAIL=fixture@example.invalid + +# ---- stubs and the tools a step may use ------------------------------------------------------ + +mkdir -p "$scratch/bin" "$scratch/tools" "$scratch/home" "$scratch/fixture" +cat >"$scratch/bin/flux" <<'EOF' +#!/usr/bin/env bash +set -euo pipefail +printf 'flux %s\n' "$*" >>"$ARGV_LOG" +printf 'flux %s\n' "$*" >>"$CALLS" +if [[ "$1 $2" == "push artifact" ]]; then + for arg in "$@"; do + [[ "$arg" == --path=* ]] || continue + # Record exactly what was packaged: every file of the pushed directory with its content. + (cd "${arg#--path=}" && find . -type f | sort | while IFS= read -r f; do printf '== %s\n' "$f"; cat "$f"; done) >"$STATE/pushed-tree" + done + if [[ "$FAULT" == no-artifact-digest ]]; then printf '{}\n'; else printf '{"digest":"%s"}\n' "$STUB_ARTIFACT_DIGEST"; fi +fi +EOF +cat >"$scratch/bin/cosign" <<'EOF' +#!/usr/bin/env bash +set -euo pipefail +printf 'cosign %s\n' "$*" >>"$ARGV_LOG" +case "$1" in + sign) printf 'cosign %s\n' "$*" >>"$CALLS" ;; + verify) + # The prebuilt verifier reading the original main-CI signature (not a publication check). + if [[ "$*" == *'--certificate-identity https://github.com/devantler-tech/app/.github/workflows/ci.yaml@refs/heads/main '* ]]; then + printf 'read original-signature\n' >>"$CALLS" + cat "$FIXTURE/signature.json" + exit 0 + fi + printf 'cosign %s\n' "$*" >>"$CALLS" + ref="${*: -1}" + if [[ "$FAULT" == verify-image && "$ref" != */manifests@* ]] || + [[ "$FAULT" == verify-artifact && "$ref" == */manifests@* ]]; then + echo 'stub: no signature matches the identity' >&2 + exit 1 + fi + : >"$STATE/verified" + ;; + verify-attestation) + printf 'read original-provenance\n' >>"$CALLS" + jq -nc --arg payload "$(base64 <"$FIXTURE/statement.json" | tr -d '\n')" '{payload:$payload}' + ;; + *) echo "unmodelled cosign $1" >&2; exit 1 ;; +esac +EOF +cat >"$scratch/bin/docker" <<'EOF' +#!/usr/bin/env bash +set -euo pipefail +printf 'docker %s\n' "$*" >>"$ARGV_LOG" +[[ "$1 $2 $3" == "buildx imagetools create" ]] || { echo "unmodelled docker $*" >&2; exit 1; } +printf 'docker %s\n' "$*" >>"$CALLS" +for arg in "$@"; do + if [[ "$arg" == --metadata-file=* ]]; then + copied="$STUB_IMAGE_DIGEST" + [[ "$FAULT" != copy-mismatch ]] || copied="$STUB_OTHER_DIGEST" + printf '{"containerimage.descriptor":{"digest":"%s"}}\n' "$copied" >"${arg#*=}" + fi +done +EOF +cat >"$scratch/bin/curl" <<'EOF' +#!/usr/bin/env bash +set -euo pipefail +printf 'curl %s\n' "$*" >>"$ARGV_LOG" +url="${*: -1}" +output='' headers='' config='' user='' status=false +[[ "${1:-}" == -q ]] || { echo 'curl must ignore ambient .curlrc files' >&2; exit 90; } +while [[ $# -gt 0 ]]; do + case "$1" in + --output) output="$2"; shift 2 ;; + --dump-header) headers="$2"; shift 2 ;; + --config) config="$2"; shift 2 ;; + # Accepted so that the argv record, not this stub, is what catches a credential argument. + --user) user="$2"; shift 2 ;; + --write-out) status=true; shift 2 ;; + --header | -H | --data-urlencode | --proto | --connect-timeout | --max-time | --max-filesize) shift 2 ;; + -q | --fail | --silent | --show-error | --tlsv1.2 | --get) shift ;; + https://*) shift ;; + *) echo "unmodelled curl argument $1" >&2; exit 91 ;; + esac +done +case "$url" in + https://api.github.com/repos/*/actions/runs/*) + printf 'read %s\n' "$url" >>"$CALLS" + [[ -z "$headers" ]] || printf 'HTTP/2 200\r\n\r\n' >"$headers" + cat "$FIXTURE/run.json" >"$output" + if "$status"; then printf 200; fi + ;; + https://ghcr.io/token) + printf 'read %s\n' "$url" >>"$CALLS" + # The registry login comes from a private config file (or, if a mutation puts it back, argv). + credential="$user" + if [[ -n "$config" ]]; then + [[ -n "$(find "$config" -type f -perm 0600)" ]] || { echo 'login config is not private' >&2; exit 92; } + credential="$(sed -n 's/^user = "\(.*\)"$/\1/p' "$config")" + fi + [[ "$credential" == "bot:$STUB_TOKEN" ]] || { echo 'wrong registry login' >&2; exit 93; } + printf '{"token":"synthetic-bearer"}' >"$output" + printf 200 + ;; + https://ghcr.io/v2/*/manifests/*) + printf 'read %s\n' "$url" >>"$CALLS" + [[ -n "$config" ]] && grep -qx 'header = "Authorization: Bearer synthetic-bearer"' "$config" || + { echo 'missing bearer config' >&2; exit 94; } + existing=false + case "$FAULT" in + registry-existing) existing=true ;; + registry-image-only) [[ "$url" == */manifests/manifests/* ]] || existing=true ;; + registry-manifests-only) [[ "$url" != */manifests/manifests/* ]] || existing=true ;; + # The version appears in the registry after the image build, or after verification. + registry-raced-after-build) [[ ! -e "$STATE/built" ]] || existing=true ;; + registry-raced) [[ ! -e "$STATE/verified" ]] || existing=true ;; + esac + if "$existing"; then + printf '{}' >"$output" + printf 200 + else + printf '{"errors":[{"code":"MANIFEST_UNKNOWN"}]}' >"$output" + printf 404 + fi + ;; + *) echo "unmodelled curl $url" >&2; exit 1 ;; +esac +EOF +cat >"$scratch/bin/git" <<'EOF' +#!/usr/bin/env bash +set -euo pipefail +printf 'git %s\n' "$*" >>"$ARGV_LOG" +if [[ "$FAULT" == merge-base-error && "${1:-}" == merge-base ]]; then + echo 'stub: git merge-base could not walk the history' >&2 + exit 128 +fi +if [[ "${1:-}" == fetch ]]; then + # The Authorization headers this fetch sends, as git resolves the list: an empty value resets it. + { "$REAL_GIT" config --get-all "http.$GITHUB_SERVER_URL/.extraheader" || true; } | + awk 'length($0) == 0 { n = 0; next } { h[++n] = $0 } END { for (i = 1; i <= n; i++) print h[i] }' >"$STATE/fetch-headers" +fi +exec "$REAL_GIT" "$@" +EOF +chmod +x "$scratch/bin/"* +bash_path="$(command -v bash)" +real_git="$(command -v git)" +# Every other program a step may run is a thin wrapper that records its argv first. A step that +# reaches for anything else (oras, crane, gh, ...) fails instead of acting unseen. +for tool in awk base64 basename bash cat cmp cp cut date dirname find grep head jq mkdir mktemp mv rm sed sleep sort tail tr wc yq; do + tool_path="$(command -v "$tool")" || fail "this test needs $tool on PATH" + printf '#!%s\nprintf "%%s %%s\\n" %q "$*" >>"$ARGV_LOG"\nexec %q "$@"\n' "$bash_path" "$tool" "$tool_path" >"$scratch/tools/$tool" + chmod +x "$scratch/tools/$tool" +done + +# ---- the release repository and its default branch ------------------------------------------- + +deployment() { # — a Deployment manifest + printf '%s\n' 'apiVersion: apps/v1' 'kind: Deployment' 'metadata:' ' name: app' 'spec:' ' template:' \ + ' spec:' ' containers:' " - name: $1" ' image: ghcr.io/devantler-tech/app:placeholder' \ + " - name: $2" ' image: ghcr.io/devantler-tech/sidecar:1.0.0' +} +src="$scratch/src" +git init --quiet --initial-branch=main "$src" +mkdir -p "$src/deploy" "$src/flat-beside-base/base" "$src/split/base" "$src/nested/overlays/base" "$src/renamed" \ + "$src/k8s/base" "$src/ambiguous" "$src/missing-app" "$src/unparsable" "$src/linked" +deployment app sidecar >"$src/deploy/deployment.yaml" +cat >"$src/deploy/webapp.yaml" <<'EOF' +apiVersion: v1 +kind: ConfigMap +metadata: + name: unrelated +data: + literal: keep me +--- +apiVersion: kro.run/v1alpha1 +kind: WebApp +metadata: + name: app + namespace: app +spec: + name: app + image: ghcr.io/devantler-tech/app:old + host: example.invalid +EOF +printf 'resources: [deployment.yaml]\n' >"$src/deploy/kustomization.yaml" +# Committed layouts: each directory is a deploy-path some case publishes from the tagged commit. +for layout in flat-beside-base flat-beside-base/base split/base nested/overlays/base k8s/base linked; do + deployment app sidecar >"$src/$layout/deployment.yaml" +done +deployment app sidecar >"$src/renamed/app.yaml" +printf 'resources: [base]\n' | tee "$src/split/kustomization.yaml" "$src/k8s/kustomization.yaml" >/dev/null +deployment app app >"$src/ambiguous/deployment.yaml" +deployment web sidecar >"$src/missing-app/deployment.yaml" +printf 'spec: [\n' >"$src/unparsable/deployment.yaml" +ln -s ../deploy/kustomization.yaml "$src/linked/kustomization.yaml" +printf '*.local\n' >"$src/.gitignore" +git -C "$src" add -A +git -C "$src" commit --quiet -m initial +printf 'second\n' >"$src/README.md" +git -C "$src" add README.md +git -C "$src" commit --quiet -m 'release commit' +released="$(git -C "$src" rev-parse HEAD)" +git -C "$src" switch --quiet -c feature +printf 'unreviewed\n' >"$src/FEATURE.md" +git -C "$src" add FEATURE.md +git -C "$src" commit --quiet -m 'not on main' +feature="$(git -C "$src" rev-parse HEAD)" +git -C "$src" switch --quiet main +printf 'later\n' >>"$src/README.md" +git -C "$src" commit --quiet -am 'main moves on' +tip="$(git -C "$src" rev-parse HEAD)" + +server="$scratch/server" +mkdir -p "$server/devantler-tech" "$server/Devantler-Tech" "$server/empty/devantler-tech" "$server/blob/devantler-tech" +git clone --quiet --bare "$src" "$server/devantler-tech/app.git" +git -C "$server/devantler-tech/app.git" symbolic-ref HEAD refs/heads/main +git -C "$server/devantler-tech/app.git" config uploadpack.allowAnySHA1InWant true +# Other spellings of the same repository (a case-insensitive filesystem already resolves the first). +[[ -e "$server/Devantler-Tech/App.git" ]] || ln -s ../devantler-tech/app.git "$server/Devantler-Tech/App.git" +ln -s app.git "$server/devantler-tech/.github.git" +# A repository without a default branch, and one whose HEAD names a blob instead of a commit. +git init --quiet --bare "$server/empty/devantler-tech/app.git" +git clone --quiet --bare "$src" "$server/blob/devantler-tech/app.git" +blob="$(printf 'not a commit\n' | git -C "$server/blob/devantler-tech/app.git" hash-object -w --stdin)" +git -C "$server/blob/devantler-tech/app.git" update-ref refs/tags/not-a-commit "$blob" +git -C "$server/blob/devantler-tech/app.git" symbolic-ref HEAD refs/tags/not-a-commit + +# One shallow checkout per commit, as actions/checkout leaves it, copied fresh for every case. +for commit in "$released" "$feature" "$tip"; do + git init --quiet "$scratch/template-$commit" + git -C "$scratch/template-$commit" fetch --quiet --depth=1 --no-tags "file://$server/devantler-tech/app.git" "$commit" + git -C "$scratch/template-$commit" checkout --quiet --detach FETCH_HEAD +done + +# Prebuilt-webapp evidence for the released commit (see test-prebuilt-webapp.sh for its refusals). +jq -n --arg sha "$released" '{ + id:321,run_attempt:1,status:"completed",conclusion:"success",event:"push", + head_sha:$sha,head_branch:"main",path:".github/workflows/ci.yaml", + repository:{full_name:"devantler-tech/app",private:false},head_repository:{full_name:"devantler-tech/app",private:false} +}' >"$scratch/fixture/run.json" +jq -n --arg digest "$image_digest" --arg sha "$released" --arg public "$public_digest" '[{ + critical:{identity:{"docker-reference":("ghcr.io/devantler-tech/app@"+$digest)},image:{"docker-manifest-digest":$digest}}, + optional:{} +}]' >"$scratch/fixture/signature.json" +jq -n --arg sha "$released" --arg digest "${image_digest#sha256:}" --arg public "$public_digest" '{ + predicateType:"https://slsa.dev/provenance/v0.2", + subject:[{name:"ghcr.io/devantler-tech/app",digest:{sha256:$digest}}], + predicate:{builder:{id:"https://github.com/devantler-tech/app/.github/workflows/ci.yaml@refs/heads/main"}, + buildType:"https://devantler.tech/build-types/combined-runtime-container/v1", + invocation:{configSource:{uri:"git+https://github.com/devantler-tech/app",digest:{sha1:$sha},entryPoint:".github/workflows/ci.yaml"}, + parameters:{ciRunId:"321",ciRunAttempt:"1",publicDigest:$public,publicProfile:"production-template"}}, + metadata:{buildInvocationId:"https://github.com/devantler-tech/app/actions/runs/321"}, + materials:[{uri:"git+https://github.com/devantler-tech/app",digest:{sha1:$sha}}]} +}' >"$scratch/fixture/statement.json" + +# ---- the composite simulation ---------------------------------------------------------------- + +calls="$scratch/calls" # every registry/API contact, in order +log="$scratch/log" # the output of every step that ran +state="$scratch/state" # stub markers (built, verified) +inputs="$scratch/inputs" # one file per input the caller passes, holding its raw value +outputs="$scratch/outputs" # /, from GITHUB_OUTPUT +argv_log="$scratch/argv" # every program a step ran, with its arguments +argv_all="$scratch/argv-all" # every argv of every unmutated case + +# setup_case — a fresh workspace at and the default healthy caller. +setup_case() { + ws="$scratch/ws" + rm -rf "$ws" "$scratch/outer" "$inputs" "$outputs" "$state" "$scratch/runner-temp" "$scratch/action-outputs" + mkdir -p "$inputs" "$outputs" "$state" "$scratch/runner-temp" "$scratch/action-outputs" + : >"$calls" + : >"$log" + : >"$argv_log" + cp -R "$scratch/template-$1" "$ws" + sim_sha="$1" + sim_ref=refs/tags/v1.2.3 + sim_workflow_ref='' + sim_repository="$repo" + sim_server="file://$server" + sim_git_global=/dev/null + FAULT=none + set_input app-name app +} +set_input() { printf '%s' "$2" >"$inputs/$1"; } +unset_input() { rm -f "$inputs/$1"; } + +# input_value — the caller's value (even empty) or the declared default. +input_value() { + [[ "$(jq --arg n "$1" '.inputs | has($n)' "$sim_json")" == true ]] || { + echo "undeclared input $1" >&2 + return 1 + } + if [[ -f "$inputs/$1" ]]; then + cat "$inputs/$1" + return 0 + fi + local default + default="$(jq -r --arg n "$1" '.inputs[$n].default // ""' "$sim_json")" + if [[ "$default" == '${{ github.token }}' ]]; then printf '%s' "$token"; else printf '%s' "$default"; fi +} + +resolve_expr() { # — what GitHub substitutes for ${{ }} + local id name + case "$1" in + github.actor) printf bot ;; + inputs.*) input_value "${1#inputs.}" ;; + steps.*.outputs.*) + id="${1#steps.}" + id="${id%%.outputs.*}" + name="${1##*.outputs.}" + if [[ -f "$outputs/$id/$name" ]]; then cat "$outputs/$id/$name"; fi + ;; + *) + echo "unmodelled expression: $1" >&2 + return 1 + ;; + esac +} + +substitute() { # — every ${{ expression }} resolved + local rest="$1" out='' expr value + while [[ "$rest" == *'${{'* ]]; do + out+="${rest%%'${{'*}" + rest="${rest#*'${{'}" + [[ "$rest" == *'}}'* ]] || return 1 + expr="${rest%%'}}'*}" + rest="${rest#*'}}'}" + expr="${expr#"${expr%%[![:space:]]*}"}" + expr="${expr%"${expr##*[![:space:]]}"}" + value="$(resolve_expr "$expr" && printf x)" || return 1 + out+="${value%x}" + done + printf '%s' "$out$rest" +} + +step_json() { jq -c --argjson i "$1" '.runs.steps[$i]' "$sim_json"; } + +# run_action — runs the composite in $ws; 0 when every step succeeded. The argv of +# every unmutated run is kept for the suite-wide no-credential check. +run_action() { + local status=0 + run_steps "$@" || status=$? + [[ -n "${mutating:-}" ]] || cat "$argv_log" >>"$argv_all" + return "$status" +} +run_steps() { + local dir="$1" count i step name id cond uses key value status out_file line + local -a env_args step_env + sim_json="$scratch/action.json" + yq -o=json '.' "$dir/action.yaml" >"$sim_json" + [[ "$(jq -r '.runs.using' "$sim_json")" == composite ]] || { echo 'not a composite action' >>"$log"; return 1; } + [[ -n "$sim_workflow_ref" ]] || sim_workflow_ref="$sim_repository/.github/workflows/release.yaml@$sim_ref" + env_args=( + "PATH=$scratch/bin:$scratch/tools" "HOME=$scratch/home" + GIT_CONFIG_NOSYSTEM=1 "GIT_CONFIG_GLOBAL=$sim_git_global" RETRY_BASE_DELAY=0 + "ARGV_LOG=$argv_log" "REAL_GIT=$real_git" "STUB_TOKEN=$token" + "GITHUB_REF=$sim_ref" "GITHUB_SHA=$sim_sha" "GITHUB_REPOSITORY=$sim_repository" + "GITHUB_WORKFLOW_REF=$sim_workflow_ref" "GITHUB_SERVER_URL=$sim_server" + GITHUB_RUN_ID=123 GITHUB_RUN_ATTEMPT=2 GITHUB_ACTOR=bot + "GITHUB_ACTION_PATH=$dir" "RUNNER_TEMP=$scratch/runner-temp" + "CALLS=$calls" "STATE=$state" "FAULT=$FAULT" "FIXTURE=$scratch/fixture" + "STUB_ARTIFACT_DIGEST=$artifact_digest" "STUB_IMAGE_DIGEST=$image_digest" "STUB_OTHER_DIGEST=$other_digest" + ) + count="$(jq '.runs.steps | length' "$sim_json")" + for ((i = 0; i < count; i++)); do + step="$(step_json "$i")" + name="$(jq -r '.name' <<<"$step")" + id="$(jq -r '.id // ""' <<<"$step")" + if ! jq -e 'keys - ["name","id","if","shell","env","run","uses","with"] == []' <<<"$step" >/dev/null; then + echo "unmodelled step key in $name" >>"$log" + return 1 + fi + cond="$(jq -r '.if // ""' <<<"$step")" + if [[ -n "$cond" ]]; then + if [[ "$cond" =~ ^\$\{\{\ inputs\.mode\ ==\ \'([a-z-]+)\'\ \}\}$ ]]; then + [[ "$(input_value mode)" == "${BASH_REMATCH[1]}" ]] || continue + else + echo "unmodelled condition in $name: $cond" >>"$log" + return 1 + fi + fi + if [[ "$(jq -r 'has("run")' <<<"$step")" == true ]]; then + [[ "$(jq -r '.shell // ""' <<<"$step")" == bash ]] || { echo "unmodelled shell in $name" >>"$log"; return 1; } + step_env=() + while IFS= read -r key; do + [[ -n "$key" ]] || continue + value="$(substitute "$(jq -r --arg k "$key" '.env[$k]' <<<"$step")" && printf x)" || { + echo "FAILED to resolve $key in $name" >>"$log" + return 1 + } + step_env+=("$key=${value%x}") + done < <(jq -r '.env // {} | keys[]' <<<"$step") + out_file="$scratch/runner-temp/output-$i" + : >"$out_file" + status=0 + (cd "$ws" && /usr/bin/env -i "${env_args[@]}" "${step_env[@]}" "GITHUB_OUTPUT=$out_file" \ + "$bash_path" --noprofile --norc -eo pipefail -c "$(jq -r '.run' <<<"$step")") >>"$log" 2>&1 || status=$? + if [[ "$status" != 0 ]]; then + echo "FAILED at $name" >>"$log" + return 1 + fi + if [[ -n "$id" ]]; then + mkdir -p "$outputs/$id" + while IFS= read -r line; do + [[ "$line" == *=* && "$line" != *'<<'* ]] || { echo "unmodelled output line in $name" >>"$log"; return 1; } + printf '%s' "${line#*=}" >"$outputs/$id/${line%%=*}" + done <"$out_file" + fi + continue + fi + uses="$(jq -r '.uses' <<<"$step")" + [[ "$uses" =~ ^([a-z0-9/._-]+)@[0-9a-f]{40}$ ]] || { echo "unpinned action in $name" >>"$log"; return 1; } + case "${BASH_REMATCH[1]}" in + fluxcd/flux2/action | sigstore/cosign-installer) ;; + docker/login-action) + [[ "$(substitute "$(jq -r '.with.password' <<<"$step")")" == "$token" && + "$(substitute "$(jq -r '.with.username' <<<"$step")")" == bot ]] || { echo 'login lost its credential' >>"$log"; return 1; } + printf 'login %s\n' "$(jq -r '.with.registry' <<<"$step")" >>"$calls" + ;; + docker/metadata-action) + mkdir -p "$outputs/$id" + printf 'org.opencontainers.image.source=%s' "$(substitute "$(jq -r '.with.images' <<<"$step")")" >"$outputs/$id/labels" + ;; + docker/build-push-action) + [[ "$(jq -r '.with.push' <<<"$step")" == true && "$(jq -r '.with.context' <<<"$step")" == . ]] || + { echo 'unmodelled build' >>"$log"; return 1; } + printf 'image push %s labels=%s\n' "$(substitute "$(jq -r '.with.tags' <<<"$step")")" \ + "$(substitute "$(jq -r '.with.labels' <<<"$step")")" >>"$calls" + : >"$state/built" + mkdir -p "$outputs/$id" + printf '%s' "$image_digest" >"$outputs/$id/digest" + ;; + *) + echo "unmodelled action ${BASH_REMATCH[1]}" >>"$log" + return 1 + ;; + esac + done + while IFS= read -r key; do + substitute "$(jq -r --arg k "$key" '.outputs[$k].value' "$sim_json")" >"$scratch/action-outputs/$key" + done < <(jq -r '.outputs // {} | keys[]' "$sim_json") +} + +# ---- what a case must show -------------------------------------------------------------------- + +writes() { grep -E '^(image push|flux push|flux tag|cosign sign|docker buildx imagetools create)' "$calls" || true; } +exposed() { grep -E '^flux tag |^docker buildx imagetools create' "$calls" | grep -v ':verified-' || true; } +snapshot() { (cd "$ws" && find . -path ./.git -prune -o -type f -print | sort | while IFS= read -r f; do printf '== %s\n' "$f"; cat "$f"; done) >"$scratch/snapshot-$1"; } +action_output() { cat "$scratch/action-outputs/$1"; } + +# admission_refusal — refused by admission, before any registry contact, +# for , with the workspace untouched. +admission_refusal() { + snapshot before + if run_action "$1"; then return 1; fi + snapshot after + [[ ! -s "$calls" ]] || return 1 + grep -qF 'FAILED at 🔒 Admit the release tag, inputs and manifests' "$log" || return 1 + grep -qF -- "$2" "$log" || return 1 + cmp -s "$scratch/snapshot-before" "$scratch/snapshot-after" +} + +# no_credential_in_argv — no program received the token, or the header that encodes it, as an +# argument; flux in particular gets no --creds and reads the docker login instead. +no_credential_in_argv() { + [[ -s "$argv_log" ]] || return 1 + ! grep -qF -- "$token" "$argv_log" || return 1 + ! grep -qF -- "${token_header#AUTHORIZATION: basic }" "$argv_log" || return 1 + ! grep -qE -- "^flux .*--creds" "$argv_log" +} + +# published [latest] — a complete publication of . +published() { + run_action "$1" || return 1 + no_credential_in_argv || return 1 + [[ "$(action_output version)" == "$2" ]] || return 1 + [[ "$(action_output manifests-digest)" == "$artifact_digest" ]] || return 1 + local pointers + pointers="$(exposed)" + grep -qE "^flux tag .* --tag $2( |$)" <<<"$pointers" || return 1 + if [[ "${3:-}" == latest ]]; then + grep -qE '^flux tag .* --tag latest( |$)' <<<"$pointers" || return 1 + else + ! grep -qE -- '--tag latest( |$)|:latest ' <<<"$pointers" || return 1 + fi +} + +# verified_before_exposed — both signatures verified against the caller's identity at its tag, +# and nothing selectable written before the last verification. +verified_before_exposed() { + local identity="https://github.com/$sim_workflow_ref" last_verify first_expose + [[ "$(grep -cE "^cosign verify --certificate-identity $identity --certificate-oidc-issuer https://token.actions.githubusercontent.com " "$calls")" == "$1" ]] || return 1 + [[ "$(grep -c '^cosign verify' "$calls")" == "$1" ]] || return 1 + last_verify="$(grep -n '^cosign verify' "$calls" | tail -n 1 | cut -d: -f1)" + first_expose="$(grep -nE '^flux tag |^docker buildx imagetools create' "$calls" | grep -v ':verified-' | head -n 1 | cut -d: -f1)" + [[ -n "$last_verify" && -n "$first_expose" && "$last_verify" -lt "$first_expose" ]] +} + +# nothing_exposed — a failure after staging leaves no version or latest pointer behind. +nothing_exposed() { + run_action "$1" && return 1 + [[ -z "$(exposed)" ]] || return 1 + [[ -s "$calls" ]] +} + +# ---- 1. the release ref and the signer identity ----------------------------------------------- + +case_ref() { # + setup_case "$released" + sim_ref="$2" + admission_refusal "$1" "$3" +} +for ref in refs/heads/main refs/pull/7/merge refs/heads/v1.2.3 v1.2.3; do + case_ref "$action_dir" "$ref" 'must run on a release tag' || fail "accepted or misjudged non-tag ref $ref: $(cat "$log")" +done +long_pre="$(printf 'a%.0s' $(seq 1 130))" +long_build="$(printf 'b%.0s' $(seq 1 251))" +for tag in v1.2.3garbage v1.2.3.4 v1.2 v1 1.2.3 V1.2.3 v01.2.3 v1.02.3 v1.2.03 v1.2.3- v1.2.3+ v1.2.3-01 \ + v1.2.3-rc..1 v1.2.3-rc.1+ v1.2.3+build..1 v1.2.3_build 'v1.2.3 ' ' v1.2.3' v1.2.3-ünicode; do + case_ref "$action_dir" "refs/tags/$tag" 'complete semantic version' || fail "accepted or misjudged tag '$tag': $(cat "$log")" +done +for tag in v9007199254740992.0.0 v1.0.18446744073709551616 v1.2.3-rc.1000000000000000 "v1.2.3+$long_build"; do + case_ref "$action_dir" "refs/tags/$tag" 'at most 256 characters with numbers of at most 15 digits' || + fail "accepted an unreadable version $tag: $(cat "$log")" +done +case_ref "$action_dir" "refs/tags/v1.2.3-$long_pre" 'longer than the 128 characters' || fail 'accepted an over-long OCI version' +echo 'ok only a complete v tag ref is admitted, before any registry contact' + +for entry in "v0.0.0|0.0.0|latest" "v10.20.30|10.20.30|latest" "v1.2.3-rc.1|1.2.3-rc.1|" \ + "v1.0.0-alpha.beta.1|1.0.0-alpha.beta.1|" "v1.0.0-x-y-z.--|1.0.0-x-y-z.--|" "v1.2.3+build.5|1.2.3|latest" \ + "v1.2.3-rc.1+build.5|1.2.3-rc.1|" "v999999999999999.0.0|999999999999999.0.0|latest"; do + IFS='|' read -r tag version latest <<<"$entry" + setup_case "$released" + sim_ref="refs/tags/$tag" + published "$action_dir" "$version" "$latest" || fail "did not publish $tag as $version: $(cat "$log")" + grep -qF -- "-a devantler.source-ref=$tag " "$calls" || fail "$tag: the signature does not claim the tag" +done +echo 'ok SemVer tags publish their normalized version; prereleases never move latest' + +case_identity() { # + setup_case "$released" + sim_workflow_ref="$2" + admission_refusal "$1" "$3" +} +belong='the calling workflow must belong to devantler-tech/app and run on refs/tags/v1.2.3' +for workflow_ref in \ + "devantler-tech/other/.github/workflows/release.yaml@refs/tags/v1.2.3|$belong" \ + "devantler-tech/app/.github/workflows/release.yaml@refs/heads/main|$belong" \ + "devantler-tech/app/.github/workflows/release.yaml@refs/tags/v1.2.4|$belong" \ + "devantler-tech/.github/.github/workflows/publish-app.yaml@refs/tags/v1.2.3|$belong" \ + "devantler-tech/app/.github/workflows/sub/release.yaml@refs/tags/v1.2.3|a file directly in .github/workflows" \ + "devantler-tech/app/.github/workflows/.hidden.yaml@refs/tags/v1.2.3|a file directly in .github/workflows"; do + case_identity "$action_dir" "${workflow_ref%%|*}" "${workflow_ref#*|}" || + fail "accepted or misjudged signer identity ${workflow_ref%%|*}: $(cat "$log")" +done +echo "ok the signer identity must be the caller's own workflow at the release tag" + +# ---- 2. the checkout and the default branch --------------------------------------------------- + +case_head_moved() { + setup_case "$released" + sim_sha="$tip" + admission_refusal "$1" "the checked-out commit is not the tagged commit $tip" +} +case_head_moved "$action_dir" || fail "accepted a checkout of another commit: $(cat "$log")" +setup_case "$released" +rm -rf "$ws/.git" +admission_refusal "$action_dir" 'check out the tagged commit' || fail "accepted a workspace without a checkout: $(cat "$log")" +# A checkout below the workspace root: HEAD resolves, but the workspace is not the repository. +case_outer() { + setup_case "$released" + mv "$ws" "$scratch/outer" + ws="$scratch/outer/deploy" + admission_refusal "$1" 'at the workspace root (actions/checkout without a path)' +} +case_outer "$action_dir" || fail "accepted a workspace inside a checkout: $(cat "$log")" + +# actions/checkout persists its token unless told not to, either in the local config or in a file +# the local config includes, for one host or (with no URL) for every host. Either way the checkout +# (and so the image build context) carries it. +case_persisted() { # + setup_case "$released" + if [[ "$2" == direct ]]; then + git -C "$ws" config --local http.https://github.com/.extraheader 'AUTHORIZATION: basic cGVyc2lzdGVk' + elif [[ "$2" == any-host ]]; then + git -C "$ws" config --local http.extraheader 'AUTHORIZATION: basic cGVyc2lzdGVk' + else + printf '[http "https://github.com/"]\n\textraheader = AUTHORIZATION: basic cGVyc2lzdGVk\n' >"$scratch/persisted.gitconfig" + git -C "$ws" config --local "includeIf.gitdir:$(cd "$ws/.git" && pwd -P).path" "$scratch/persisted.gitconfig" + fi + admission_refusal "$1" 'the checkout persisted a git credential; check out with persist-credentials: false' +} +for style in direct any-host included; do + case_persisted "$action_dir" "$style" || fail "accepted a checkout with a $style persisted credential: $(cat "$log")" +done + +case_not_on_default() { + setup_case "$feature" + admission_refusal "$1" "points to $feature, which is not on the default branch" +} +case_not_on_default "$action_dir" || fail "accepted a tag on a commit outside the default branch: $(cat "$log")" + +case_undeterminable() { # — the default branch cannot be fetched from + setup_case "$released" + sim_server="$2" + admission_refusal "$1" 'could not fetch the default branch' +} +case_undeterminable "$action_dir" "file://$scratch/nowhere" || fail "accepted an unreachable default branch: $(cat "$log")" +case_undeterminable "$action_dir" "file://$server/empty" || fail "accepted a repository without a default branch: $(cat "$log")" +case_no_commit() { # the remote HEAD names a blob, so the fetch succeeds without a commit + setup_case "$released" + sim_server="file://$server/blob" + admission_refusal "$1" 'no default branch commit was fetched' +} +case_no_commit "$action_dir" || fail "accepted a default branch that is not a commit: $(cat "$log")" +case_ancestry_error() { # git cannot answer the ancestry question (exit 128) + setup_case "$released" + FAULT=merge-base-error + admission_refusal "$1" 'is on the default branch (git exit 128)' +} +case_ancestry_error "$action_dir" || fail "accepted an unanswered ancestry check: $(cat "$log")" + +# The tag on an older default-branch commit (main moved on) and on the tip both publish, from a +# shallow checkout; the fetch carries the token only in its environment. +setup_case "$released" +published "$action_dir" 1.2.3 latest || fail "refused a tag behind the default branch tip: $(cat "$log")" +setup_case "$tip" +published "$action_dir" 1.2.3 latest || fail "refused a tag at the default branch tip: $(cat "$log")" +! grep -rqF "$token" "$ws/.git/config" || fail 'the default-branch fetch persisted the token' + +# An inherited header for the same host (here from the global config) is reset, so the fetch sends +# exactly one Authorization header: the token's. +case_one_header() { + setup_case "$released" + printf '[http "%s/"]\n\textraheader = AUTHORIZATION: basic aW5oZXJpdGVk\n' "$sim_server" >"$scratch/global.gitconfig" + sim_git_global="$scratch/global.gitconfig" + published "$1" 1.2.3 latest || return 1 + [[ "$(cat "$state/fetch-headers")" == "$token_header" ]] +} +case_one_header "$action_dir" || fail "the default-branch fetch did not send exactly the token header: $(cat "$state/fetch-headers" "$log")" +echo 'ok only the tagged, credential-free checkout of a default-branch commit is admitted; unknown ancestry refuses' + +# ---- 3. inputs, deploy-path and app-manifest --------------------------------------------------- + +case_inputs() { # ... (a name without = is removed) + local dir="$1" reason="$2" setting + shift 2 + setup_case "$released" + for setting in "$@"; do + if [[ "$setting" == *=* ]]; then set_input "${setting%%=*}" "${setting#*=}"; else unset_input "$setting"; fi + done + admission_refusal "$dir" "$reason" +} +dns='app-name must be a DNS label' +prebuilt_ok=(mode=prebuilt-webapp "prebuilt-image-digest=$image_digest" "prebuilt-public-digest=$public_digest" + prebuilt-ci-run-id=321 prebuilt-ci-run-attempt=1) +case_inputs "$action_dir" 'mode must be deployment, prebuilt-webapp or manifests' mode=Deployment || fail 'accepted an unknown mode' +case_inputs "$action_dir" 'mode must be deployment, prebuilt-webapp or manifests' mode= || fail 'accepted an empty mode' +case_inputs "$action_dir" "$dns" app-name || fail 'accepted a missing app-name' +case_inputs "$action_dir" "$dns" app-name=App || fail 'accepted an invalid app-name' +case_inputs "$action_dir" "$dns" 'app-name=a$(id)' || fail 'accepted an unsafe app-name' +case_inputs "$action_dir" 'missing registry credentials' github-token= || fail 'accepted an empty github-token' +case_inputs "$action_dir" 'oci-name applies only to manifests mode' oci-name=devantler-tech/app || fail 'accepted oci-name for an app' +case_inputs "$action_dir" 'prebuilt-* inputs apply only to prebuilt-webapp mode' prebuilt-ci-run-id=321 || + fail 'accepted prebuilt evidence in deployment mode' +case_inputs "$action_dir" 'manifests mode takes no app-name' mode=manifests || fail 'accepted app-name in manifests mode' +case_inputs "$action_dir" 'manifests mode takes no app-name' mode=manifests app-name app-manifest=deployment.yaml || + fail 'accepted app-manifest in manifests mode' +case_inputs "$action_dir" 'oci-name must stay in the devantler-tech namespace' mode=manifests app-name oci-name=someone-else/config || + fail 'accepted an oci-name outside the caller namespace' +case_inputs "$action_dir" 'not a valid registry path' mode=manifests app-name oci-name=devantler-tech/.github || + fail 'accepted an invalid oci-name' +for setting in prebuilt-image-digest=latest prebuilt-public-digest=wrong prebuilt-ci-run-id=0 prebuilt-ci-run-attempt=x; do + case_inputs "$action_dir" 'prebuilt-' "${prebuilt_ok[@]}" "$setting" || fail "accepted prebuilt input $setting" +done +setup_case "$released" +sim_repository=devantler-tech/.github +admission_refusal "$action_dir" 'not a valid registry path' || fail "accepted an image name that is not a registry path: $(cat "$log")" +echo 'ok mode, app-name, oci-name, prebuilt evidence and credentials are checked before any registry contact' + +plain_deploy='deploy-path must be a relative directory of plain names' +for deploy in /deploy ../deploy deploy/../deploy ./deploy/ . '' .hidden 'deploy/./x' $'deploy\n' $'deploy\r'; do + case_inputs "$action_dir" 'deploy-path' "deploy-path=$deploy" || fail "accepted deploy-path '$deploy': $(cat "$log")" + grep -qF -- "$plain_deploy" "$log" || grep -qF 'deploy-path must not contain a line break' "$log" || + fail "deploy-path '$deploy' refused for the wrong reason: $(grep '::error::' "$log")" +done +case_deploy_link() { + setup_case "$released" + mv "$ws/deploy" "$ws/outside" + ln -s outside "$ws/deploy" + admission_refusal "$1" 'deploy-path passes through a link' +} +case_deploy_link "$action_dir" || fail "accepted a linked deploy-path: $(cat "$log")" +case_inputs "$action_dir" 'is not a directory' deploy-path=./nowhere || fail 'accepted a missing deploy-path' + +# app-manifest: the #922 cases, ported. A valid manifest sits wherever each path would lead, so +# only the input checks can refuse it. +plain_manifest='app-manifest must be a relative path of plain names inside deploy-path' +case_manifest() { # + local dir="$1" mode="$2" manifest="$3" reason="$4" file=deployment.yaml + [[ "$mode" == deployment ]] || file=webapp.yaml + setup_case "$released" + if [[ "$mode" != deployment ]]; then + local setting + for setting in "${prebuilt_ok[@]}"; do set_input "${setting%%=*}" "${setting#*=}"; done + fi + mkdir -p "$ws/deploy/base" "$ws/deploy/.hidden" + cp "$ws/deploy/$file" "$ws/deploy/base/$file" + cp "$ws/deploy/$file" "$ws/deploy/.hidden/$file" + cp "$ws/deploy/$file" "$ws/$file" + case "$manifest" in + linked-dir) mv "$ws/deploy/base" "$ws/outside" && ln -s ../outside "$ws/deploy/base" ;; + linked-file) mv "$ws/deploy/base/$file" "$ws/outside.yaml" && ln -s ../../outside.yaml "$ws/deploy/base/$file" ;; + missing) rm "$ws/deploy/base/$file" ;; + esac + case "$manifest" in linked-dir | linked-file | missing) manifest="base/$file" ;; esac + set_input app-manifest "$manifest" + admission_refusal "$dir" "$reason" +} +for mode in deployment prebuilt-webapp; do + file=deployment.yaml + [[ "$mode" == deployment ]] || file=webapp.yaml + for entry in "../$file|$plain_manifest" "/$file|$plain_manifest" "base/./$file|$plain_manifest" \ + "base/../$file|$plain_manifest" "base//$file|$plain_manifest" "base/|$plain_manifest" \ + ".hidden/$file|$plain_manifest" "base/$file"$'\n'"::error::injected|$plain_manifest" \ + "base/$file"$'\r'"|$plain_manifest" $'\n'"base/$file|$plain_manifest" \ + "linked-dir|app-manifest passes through a link" "linked-file|app-manifest passes through a link" \ + "missing|app manifest ./deploy/base/$file not found"; do + manifest="${entry%%|*}" + case_manifest "$action_dir" "$mode" "$manifest" "${entry#*|}" || + fail "$mode accepted or misjudged app-manifest '$manifest': $(grep '::error::' "$log" || cat "$log")" + ! grep -qF 'injected' "$log" || fail 'an unchecked app-manifest reached the log' + done +done + +case_containers() { # + setup_case "$released" + set_input deploy-path "./$2" + admission_refusal "$1" "$3" +} +case_containers "$action_dir" ambiguous "expected exactly one container named 'app'" || fail "accepted two app containers: $(cat "$log")" +case_containers "$action_dir" missing-app "expected exactly one container named 'app'" || + fail "accepted a Deployment without the app container: $(cat "$log")" +case_containers "$action_dir" unparsable 'cannot read ./unparsable/deployment.yaml as a Deployment' || + fail "accepted an unparsable manifest: $(cat "$log")" + +# Everything under deploy-path is published: a committed link, or a special file, is refused in +# every mode, as the prebuilt verifier already refuses them. +linked_reason='deploy-path must hold only regular files and directories (no links or special files)' +case_linked_contents() { # + setup_case "$released" + set_input deploy-path ./linked + if [[ "$2" == manifests ]]; then set_input mode manifests; unset_input app-name; fi + admission_refusal "$1" "$linked_reason" +} +for mode in deployment manifests; do + case_linked_contents "$action_dir" "$mode" || fail "$mode accepted a link inside deploy-path: $(cat "$log")" +done +setup_case "$released" +mkfifo "$ws/deploy/pipe" +admission_refusal "$action_dir" "$linked_reason" || fail "accepted a special file inside deploy-path: $(cat "$log")" + +# The signed manifests are the tagged source: a changed, removed or added file in deploy-path +# (an ignored one included) is refused; changes elsewhere in the workspace are not. +case_source() { # + setup_case "$released" + case "$2" in + modified) printf 'patched: true\n' >>"$ws/deploy/kustomization.yaml" ;; + staged) printf 'patched: true\n' >>"$ws/deploy/kustomization.yaml" && git -C "$ws" add deploy/kustomization.yaml ;; + removed) rm "$ws/deploy/webapp.yaml" ;; + untracked) printf 'kind: Secret\n' >"$ws/deploy/extra.yaml" ;; + ignored) printf 'kind: Secret\n' >"$ws/deploy/values.local" ;; + esac + admission_refusal "$1" "$3" +} +differs='deploy-path deploy differs from the tagged commit' +untracked='deploy-path deploy holds files that are not in the tagged commit' +for entry in "modified|$differs" "staged|$differs" "removed|$differs" "untracked|$untracked" "ignored|$untracked"; do + case_source "$action_dir" "${entry%%|*}" "${entry#*|}" || fail "accepted a ${entry%%|*} file in deploy-path: $(cat "$log")" +done +setup_case "$released" +printf 'built\n' >>"$ws/README.md" +mkdir -p "$ws/dist" +printf 'built\n' >"$ws/dist/out.txt" +published "$action_dir" 1.2.3 latest || fail "refused build outputs outside deploy-path: $(cat "$log")" +echo 'ok deploy-path, app-manifest and the app container are refused before any registry contact' + +# ---- 4. an occupied version --------------------------------------------------------------------- + +case_occupied() { # [mode] + setup_case "$released" + FAULT="$2" + if [[ "${3:-}" == manifests ]]; then set_input mode manifests; unset_input app-name; fi + run_action "$1" && return 1 + grep -qF 'FAILED at 🔒 Refuse an occupied release version' "$log" || return 1 + grep -qF 'already exists' "$log" || return 1 + [[ -z "$(writes)" ]] +} +for fault in registry-existing registry-image-only registry-manifests-only; do + case_occupied "$action_dir" "$fault" || fail "did not refuse $fault before the first write: $(cat "$calls")" +done +case_occupied "$action_dir" registry-manifests-only manifests || fail 'manifests mode did not refuse an occupied version' +case_raced_after_build() { + setup_case "$released" + FAULT=registry-raced-after-build + run_action "$1" && return 1 + grep -qF 'already exists' "$log" || return 1 + [[ "$(writes)" == "image push "* && "$(writes | wc -l | tr -d ' ')" == 1 ]] +} +case_raced_after_build "$action_dir" || fail "staged or signed after the version appeared: $(cat "$calls")" +echo 'ok an occupied version is refused before the first registry write, and again before staging' + +# ---- 5. healthy publication in every mode ------------------------------------------------------ + +case_deployment() { # — the complete, ordered deployment-mode publication + local image=ghcr.io/devantler-tech/app artifact=ghcr.io/devantler-tech/app/manifests + setup_case "$released" + published "$1" 1.2.3 latest || return 1 + verified_before_exposed 2 || return 1 + # Exact calls, except that a trailing " *" leaves the claims (checked below) to a prefix match. + local -a expected=( + "login ghcr.io" + "image push $image:staging-123-2 labels=org.opencontainers.image.source=$image" + "flux push artifact oci://$artifact:staging-123-2 --path=./deploy --source=file://$server/$repo --revision=v1.2.3@sha1:$released --output json" + "cosign sign --yes *" + "cosign sign --yes *" + "cosign verify *" + "cosign verify *" + "docker buildx imagetools create --prefer-index=false --tag $image:verified-123-2 --metadata-file=$scratch/runner-temp/publish-app-copy.json $image@$image_digest" + "docker buildx imagetools create --prefer-index=false --tag $image:1.2.3 $image@$image_digest" + "flux tag artifact oci://$artifact@$artifact_digest --tag 1.2.3" + "docker buildx imagetools create --prefer-index=false --tag $image:latest $image@$image_digest" + "flux tag artifact oci://$artifact@$artifact_digest --tag latest" + ) + local actual i=0 line + actual="$(grep -v '^read ' "$calls")" + [[ "$(wc -l <<<"$actual" | tr -d ' ')" == "${#expected[@]}" ]] || return 1 + while IFS= read -r line; do + if [[ "${expected[$i]}" == *" *" ]]; then + [[ "$line" == "${expected[$i]%\*}"* ]] || return 1 + else + [[ "$line" == "${expected[$i]}" ]] || return 1 + fi + i=$((i + 1)) + done <<<"$actual" + # Three authenticated absence checks over both repositories: before the build, before staging + # and before exposure. + [[ "$(grep -c "^read https://ghcr.io/v2/devantler-tech/app/manifests/1.2.3$" "$calls")" == 3 ]] || return 1 + [[ "$(grep -c "^read https://ghcr.io/v2/devantler-tech/app/manifests/manifests/1.2.3$" "$calls")" == 3 ]] || return 1 + # Image and artifact are signed by digest, with the publication claims. + local claim + for claim in "repository=$repo" "source-sha=$released" "source-ref=v1.2.3" "version=1.2.3" "run-id=123" \ + "run-attempt=2" "workflow-ref=$repo/.github/workflows/release.yaml@refs/tags/v1.2.3" \ + "image-digest=$image_digest" "manifests-digest=$artifact_digest"; do + [[ "$(grep -cF -- "-a devantler.$claim " "$calls")" == 4 ]] || return 1 + done + grep -qE "^cosign sign .* $image@$image_digest$" "$calls" || return 1 + grep -qE "^cosign sign .* $artifact@$artifact_digest$" "$calls" || return 1 + # The pushed tree is the whole deploy path with only the app container pinned. + grep -qxF '== ./kustomization.yaml' "$state/pushed-tree" || return 1 + grep -qxF '== ./webapp.yaml' "$state/pushed-tree" || return 1 + [[ "$(yq '.spec.template.spec.containers[] | select(.name == "app") | .image' "$ws/deploy/deployment.yaml")" == "$image@$image_digest" ]] || return 1 + [[ "$(yq '.spec.template.spec.containers[] | select(.name == "sidecar") | .image' "$ws/deploy/deployment.yaml")" == ghcr.io/devantler-tech/sidecar:1.0.0 ]] || return 1 + grep -qF "image: $image@$image_digest" "$state/pushed-tree" || return 1 + [[ "$(action_output image-digest)" == "$image_digest" ]] +} +case_deployment "$action_dir" || fail "deployment publication changed: $(cat "$calls") $(cat "$log")" +echo 'ok deployment mode builds to staging, pins, stages, signs, verifies as the caller and only then exposes' + +# Each committed layout: deploy-path | app-manifest | the file that must be pinned. +for entry in "flat-beside-base||deployment.yaml" "split|base/deployment.yaml|base/deployment.yaml" \ + "nested|overlays/base/deployment.yaml|overlays/base/deployment.yaml" "renamed|app.yaml|app.yaml" \ + "k8s|base/deployment.yaml|base/deployment.yaml"; do + IFS='|' read -r deploy manifest at <<<"$entry" + setup_case "$released" + set_input deploy-path "./$deploy" + [[ -z "$manifest" ]] || set_input app-manifest "$manifest" + published "$action_dir" 1.2.3 latest || fail "refused app-manifest '$manifest' in ./$deploy: $(cat "$log")" + pinned="$(yq '.spec.template.spec.containers[] | select(.name == "app") | .image' "$ws/$deploy/$at")" + [[ "$pinned" == "ghcr.io/devantler-tech/app@$image_digest" ]] || fail "did not pin ./$deploy/$at: $pinned" + if [[ -z "$manifest" ]]; then + [[ "$(yq '.spec.template.spec.containers[0].image' "$ws/$deploy/base/deployment.yaml")" == ghcr.io/devantler-tech/app:placeholder ]] || + fail 'the default pinned a base/ manifest nobody named' + fi + grep -qF -- "--path=./$deploy " "$calls" || fail "did not push the whole ./$deploy directory" +done +echo 'ok app-manifest names the pinned manifest inside deploy-path; the whole deploy-path is published' + +setup_case "$released" +sim_repository=Devantler-Tech/App +published "$action_dir" 1.2.3 latest || fail "refused a mixed-case repository: $(cat "$log")" +grep -qF 'image push ghcr.io/devantler-tech/app:staging-123-2 ' "$calls" || fail 'the image name is not lowercase' +grep -qF -- '--certificate-identity https://github.com/Devantler-Tech/App/.github/workflows/release.yaml@refs/tags/v1.2.3 ' "$calls" || + fail 'the verified identity is not the exact calling workflow ref' + +case_prebuilt() { + local image=ghcr.io/devantler-tech/app setting + setup_case "$released" + for setting in "${prebuilt_ok[@]}"; do set_input "${setting%%=*}" "${setting#*=}"; done + published "$1" 1.2.3 latest || return 1 + verified_before_exposed 2 || return 1 + ! grep -q '^image push' "$calls" || return 1 + # The original main-CI signature and provenance are read before the first registry write. + local first_write + first_write="$(grep -nE '^(flux push|cosign sign)' "$calls" | head -n 1 | cut -d: -f1)" + [[ "$(grep -n '^read original-provenance' "$calls" | cut -d: -f1)" -lt "$first_write" ]] || return 1 + grep -qE "^cosign sign .*-a devantler.public-digest=$public_digest .* $image@$image_digest$" "$calls" || return 1 + grep -qF -- '-a devantler.image-producer=https://github.com/devantler-tech/app/.github/workflows/ci.yaml@refs/heads/main ' "$calls" || return 1 + [[ "$(yq 'select(.kind == "WebApp") | .spec.image' "$ws/deploy/webapp.yaml")" == "$image@$image_digest" ]] || return 1 + [[ "$(yq '.spec.template.spec.containers[0].image' "$ws/deploy/deployment.yaml")" == "$image:placeholder" ]] || return 1 + [[ "$(action_output image-digest)" == "$image_digest" ]] +} +case_prebuilt "$action_dir" || fail "prebuilt-webapp publication changed: $(cat "$calls") $(cat "$log")" +echo 'ok prebuilt-webapp mode verifies the main-CI image, pins the WebApp and re-signs it as the caller' + +case_manifests() { # + setup_case "$tip" + set_input mode manifests + unset_input app-name + [[ -z "$2" ]] || set_input oci-name "$2" + published "$1" 1.2.3 latest || return 1 + verified_before_exposed 1 || return 1 + ! grep -qE '^(image push|docker)' "$calls" || return 1 + [[ "$(grep -c '^cosign sign' "$calls")" == 1 ]] || return 1 + grep -qE "^cosign sign .* $3@$artifact_digest$" "$calls" || return 1 + ! grep -qF 'devantler.image-digest' "$calls" || return 1 + [[ "$(grep -c "^read https://ghcr.io/v2/${3#ghcr.io/}/manifests/1.2.3$" "$calls")" == 3 ]] || return 1 + [[ -z "$(action_output image-digest)" ]] || return 1 + [[ "$(yq '.spec.template.spec.containers[0].image' "$ws/deploy/deployment.yaml")" == ghcr.io/devantler-tech/app:placeholder ]] +} +case_manifests "$action_dir" '' ghcr.io/devantler-tech/app/manifests || fail "manifests mode changed: $(cat "$calls") $(cat "$log")" +case_manifests "$action_dir" Devantler-Tech/GitHub-Config ghcr.io/devantler-tech/github-config/manifests || + fail "manifests mode ignored oci-name: $(cat "$calls") $(cat "$log")" +echo 'ok manifests mode publishes only the signed manifests, under oci-name when given' + +# ---- 6. nothing is exposed unless both signatures verify ---------------------------------------- + +case_fault() { # [mode] + setup_case "$released" + FAULT="$2" + if [[ "${4:-}" == manifests ]]; then set_input mode manifests; unset_input app-name; fi + nothing_exposed "$1" || return 1 + grep -qF -- "$3" "$log" +} +case_fault "$action_dir" verify-image 'does not verify as https://github.com/devantler-tech/app/.github/workflows/release.yaml@refs/tags/v1.2.3' || + fail "exposed an image whose signature does not verify: $(cat "$calls")" +case_fault "$action_dir" verify-artifact 'does not verify' || fail "exposed manifests whose signature does not verify: $(cat "$calls")" +case_fault "$action_dir" verify-artifact 'does not verify' manifests || fail "manifests mode exposed an unverified artifact: $(cat "$calls")" +case_fault "$action_dir" registry-raced 'already exists' || fail "exposed a version that appeared after verification: $(cat "$calls")" +case_fault "$action_dir" copy-mismatch 'the image copy changed the verified digest' || fail "exposed a changed image copy: $(cat "$calls")" +case_fault "$action_dir" no-artifact-digest 'did not report the pushed artifact digest' || fail 'signed without an artifact digest' +grep -q '^cosign' "$calls" && fail 'signed or verified without an artifact digest' +echo 'ok a failed verification, copy or late version exposes nothing' + +# Across every unmutated run above, no program a step ran received the token or its encoded header. +for program in curl git flux cosign; do + grep -q "^$program " "$argv_all" || fail "the argv record never saw $program" +done +grep -q '^git fetch ' "$argv_all" || fail 'the argv record never saw the default-branch fetch' +! grep -qF -- "$token" "$argv_all" || fail "a program received the token as an argument: $(grep -F -- "$token" "$argv_all" | head -n 3)" +! grep -qF -- "${token_header#AUTHORIZATION: basic }" "$argv_all" || fail 'a program received the encoded token header as an argument' +echo 'ok no program any step ran received the token or its encoded header as an argument' + +# ---- 7. mutation controls: every guard must matter ------------------------------------------------ + +mutating=true +case_prerelease() { + setup_case "$released" + sim_ref=refs/tags/v1.2.3-rc.1 + published "$1" 1.2.3-rc.1 +} +case_prerelease "$action_dir" || fail "a prerelease moved latest: $(cat "$calls")" + +# copy_action — a copy of the catalogue with one action file edited, in +# $copy_dir. The copy resolves the shared scripts exactly as the published action does. +copy_action() { + copy_dir="$(mktemp -d "$scratch/mutant.XXXXXX")" + mkdir -p "$copy_dir/actions" + cp -R "$action_dir" "$copy_dir/actions/publish-app" + ln -s "$root/.github" "$copy_dir/.github" + ln -s "$root/.scripts" "$copy_dir/.scripts" + sed "$2" "$action_dir/$1" >"$copy_dir/actions/publish-app/$1" + ! cmp -s "$action_dir/$1" "$copy_dir/actions/publish-app/$1" || fail "edit did not apply to $1: $2" + copy_dir="$copy_dir/actions/publish-app" +} + +# Control: copies made this way, with a no-op edit to each file, still hold every case below, so a +# mutant fails only because of its own edit. +for edit in 'admit.sh|s/^set -euo pipefail$/set -euo pipefail # control/' \ + 'publish.sh|s/^set -euo pipefail$/set -euo pipefail # control/' \ + 'require-unpublished.sh|s/^set -euo pipefail$/set -euo pipefail # control/' \ + 'action.yaml|s/^author: devantler-tech$/author: devantler-tech # control/'; do + copy_action "${edit%%|*}" "${edit#*|}" + if ! { case_ref "$copy_dir" refs/heads/main 'must run on a release tag' && + case_ref "$copy_dir" refs/tags/v1.2.3garbage 'complete semantic version' && + case_ref "$copy_dir" refs/tags/v9007199254740992.0.0 'numbers of at most 15 digits' && + case_identity "$copy_dir" "devantler-tech/app/.github/workflows/release.yaml@refs/heads/main" "$belong" && + case_head_moved "$copy_dir" && case_outer "$copy_dir" && case_persisted "$copy_dir" direct && + case_persisted "$copy_dir" any-host && + case_not_on_default "$copy_dir" && case_undeterminable "$copy_dir" "file://$scratch/nowhere" && + case_no_commit "$copy_dir" && case_ancestry_error "$copy_dir" && case_one_header "$copy_dir" && + case_deploy_link "$copy_dir" && case_linked_contents "$copy_dir" deployment && + case_source "$copy_dir" modified "$differs" && case_source "$copy_dir" untracked "$untracked" && + case_manifest "$copy_dir" deployment linked-file 'app-manifest passes through a link' && + case_manifest "$copy_dir" deployment ../deployment.yaml "$plain_manifest" && + case_containers "$copy_dir" ambiguous "expected exactly one container named 'app'" && + case_occupied "$copy_dir" registry-existing && case_raced_after_build "$copy_dir" && + case_fault "$copy_dir" registry-raced 'already exists' && case_fault "$copy_dir" verify-image 'does not verify' && + case_fault "$copy_dir" verify-artifact 'does not verify' && case_fault "$copy_dir" copy-mismatch 'changed the verified digest' && + case_fault "$copy_dir" verify-artifact 'does not verify' manifests && + case_deployment "$copy_dir" && case_manifests "$copy_dir" '' ghcr.io/devantler-tech/app/manifests && + case_prerelease "$copy_dir"; }; then + fail "an unmutated copy (${edit%%|*}) no longer holds: $(cat "$log")" + fi +done +echo 'ok unmutated copies hold every mutation case' + +# must_catch [case args...] +# Applies one portable sed edit to a fresh copy of the action and requires the case to stop holding. +must_catch() { + local description="$1" case_fn="$4" + copy_action "$2" "$3" + shift 4 + if "$case_fn" "$copy_dir" "$@" >/dev/null 2>&1; then fail "mutation not caught: $description"; fi + echo "ok caught: $description" +} + +must_catch 'branch refs admitted' admit.sh 's/\[\[ "\$ref" == refs\/tags\/\* \]\] || fail/[[ "$ref" == refs\/tags\/* ]] || true/' \ + case_ref refs/heads/main 'must run on a release tag' +must_catch 'non-semver tags admitted' admit.sh 's/\[\[ "\$tag" =~ \$semver \]\] || fail/true || fail/' \ + case_ref refs/tags/v1.2.3garbage 'complete semantic version' +must_catch 'unreadable version numbers admitted' admit.sh 's/ || "\$numbers" =~ \$too_long//' \ + case_ref refs/tags/v9007199254740992.0.0 'numbers of at most 15 digits' +must_catch 'a foreign signer identity admitted' admit.sh 's/^check_signer_identity$/:/' \ + case_identity "devantler-tech/app/.github/workflows/release.yaml@refs/heads/main" "$belong" +must_catch 'a different checkout admitted' admit.sh 's/^check_checkout$/:/' case_head_moved +must_catch 'a checkout below the workspace root admitted' admit.sh 's/\[\[ "\$(git rev-parse --show-toplevel 2>\/dev\/null)" -ef \. \]\] ||/true ||/' \ + case_outer +must_catch 'a persisted checkout credential admitted' admit.sh "s/ 0) fail 'the checkout persisted/ 0) : 'the checkout persisted/" \ + case_persisted direct +must_catch 'a header for every host passing the persisted-credential check' admit.sh \ + 's/\^http\\\.(\.\*\\\.)?extraheader\$/^http\\\..*\\\.extraheader$/' case_persisted any-host +must_catch 'tags outside the default branch admitted' admit.sh 's/ 1) fail "tag/ 1) : "tag/' case_not_on_default +must_catch 'the default-branch check removed' admit.sh 's/^check_default_branch$/:/' case_not_on_default +must_catch 'an undeterminable default branch admitted' admit.sh 's/fail "cannot determine/: "cannot determine/g' \ + case_undeterminable "file://$scratch/nowhere" +must_catch 'a fetched default branch without a commit passed' admit.sh \ + 's/ fail "cannot determine whether \$GITHUB_SHA is on the default branch: no default branch commit was fetched"/ : no commit/' \ + case_no_commit +must_catch 'an unanswered ancestry check passed' admit.sh \ + 's/ \*) fail "cannot determine whether \$GITHUB_SHA is on the default branch (git exit \$status)" ;;/ *) ;;/' \ + case_ancestry_error +must_catch 'the inherited fetch header not reset' admit.sh \ + '/GIT_CONFIG_VALUE_0=.. \\$/s/_0=/_9=/g; s/GIT_CONFIG_KEY_1=/GIT_CONFIG_KEY_0=/; s/GIT_CONFIG_VALUE_1=/GIT_CONFIG_VALUE_0=/; s/GIT_CONFIG_COUNT=2/GIT_CONFIG_COUNT=1/' \ + case_one_header +must_catch 'a linked deploy-path admitted' admit.sh 's/\[\[ ! -L "\$cursor" \]\] || fail/true || fail/' case_deploy_link +must_catch 'a linked app-manifest admitted' admit.sh 's/\[\[ ! -L "\$cursor" \]\] || fail/true || fail/' \ + case_manifest deployment linked-file 'app-manifest passes through a link' +must_catch 'links inside deploy-path admitted' admit.sh 's/^check_deploy_contents$/:/' case_linked_contents deployment +must_catch 'links inside a manifests-only deploy-path admitted' admit.sh 's/^check_deploy_contents$/:/' case_linked_contents manifests +must_catch 'a changed deploy-path admitted' admit.sh 's/ 1) fail "deploy-path/ 1) : "deploy-path/' case_source modified "$differs" +must_catch 'untracked deploy-path files admitted' admit.sh 's/\[\[ -z "\$untracked" \]\] || fail/true || fail/' \ + case_source untracked "$untracked" +must_catch 'an unsafe app-manifest admitted' admit.sh 's/^ \[\[ "\$manifest" =~ .* \]\] ||$/ true ||/' \ + case_manifest deployment ../deployment.yaml "$plain_manifest" +must_catch 'an ambiguous app container admitted' admit.sh 's/\[\[ "\$matches" == 1 \]\] || fail/true || fail/' \ + case_containers ambiguous "expected exactly one container named 'app'" +must_catch 'the pre-build version check removed' action.yaml \ + '/- name: 🔒 Refuse an occupied release version/,/run: bash "\$GITHUB_ACTION_PATH\/require-unpublished.sh"/d' \ + case_occupied registry-existing +must_catch 'the pre-staging version check removed' publish.sh 's/^require_unpublished before-staging$/:/' case_raced_after_build +must_catch 'the pre-exposure version check removed' publish.sh 's/^require_unpublished before-exposure$/:/' \ + case_fault registry-raced 'already exists' +must_catch 'verification removed' publish.sh 's/^verify$/:/' case_fault verify-image 'does not verify' +must_catch 'verification moved after exposure' publish.sh 's/^verify$/:/; s/^expose$/expose; verify/' \ + case_fault verify-artifact 'does not verify' +must_catch 'a foreign verification identity' publish.sh \ + 's|^identity=.*|identity="https://github.com/devantler-tech/.github/.github/workflows/publish-app.yaml@refs/heads/main"|' case_deployment +must_catch 'the image copy check removed' publish.sh 's/^check_copy$/:/' case_fault copy-mismatch 'changed the verified digest' +must_catch 'the digest pin removed' publish.sh 's/|| pin_image$/|| :/' case_deployment +must_catch 'prereleases moving latest' publish.sh 's/ || "\$VERSION" != \*-\* \]\]/ || true ]]/' case_prerelease +must_catch 'manifests mode exposing an unverified artifact' publish.sh 's/^verify$/:/' \ + case_fault verify-artifact 'does not verify' manifests +must_catch 'the token back in the flux push arguments' publish.sh \ + 's/^ --output json)"$/ --creds "$GITHUB_ACTOR:$GH_TOKEN" --output json)"/' case_deployment +must_catch 'the token back in the flux tag arguments' publish.sh \ + 's/ --tag "\$pointer"$/ --tag "$pointer" --creds "$GITHUB_ACTOR:$GH_TOKEN"/' case_manifests '' ghcr.io/devantler-tech/app/manifests +must_catch 'the token back in the curl arguments' require-unpublished.sh \ + 's/--config "\$work\/login.curlrc"/--user "$GITHUB_ACTOR:$GH_TOKEN"/' case_deployment + +echo 'PASS: publish-app admits only default-branch release tags, refuses before any registry contact, keeps the token off every command line and exposes only verified releases' diff --git a/.github/tests/test-publish-contract-job.sh b/.github/tests/test-publish-contract-job.sh index e038e767..89941227 100644 --- a/.github/tests/test-publish-contract-job.sh +++ b/.github/tests/test-publish-contract-job.sh @@ -14,7 +14,7 @@ cat >"$work/check.jq" <<'JQ' "test-manifests-recovery", "test-app-recovery", "test-unpublished-version-native", "test-app-signed-promotion", "test-prebuilt-webapp", - "test-publish-manifests-promotion"] as $tests | + "test-publish-manifests-promotion", "test-publish-app-action"] as $tests | ($j != null) and ($j.needs == null) and ($j.if == "${{ github.event_name != 'merge_group' && !startsWith(github.event.head_commit.message, 'chore(main): release ') }}") and @@ -47,6 +47,7 @@ if ! check "$work/ci.json"; then fi for mutation in \ '.jobs["test-publish-contracts"].steps |= map(select(.run != "bash .github/tests/test-prebuilt-webapp.sh"))' \ + '.jobs["test-publish-contracts"].steps |= map(select(.run != "bash .github/tests/test-publish-app-action.sh"))' \ 'del(.jobs["test-publish-contracts"].steps[2])' \ '.jobs["test-publish-contracts"].steps[2].if = "false"' \ '.jobs["test-publish-contracts"].steps[2]."continue-on-error" = true' \ diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index a7801ff7..66d73f40 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -1767,6 +1767,61 @@ jobs: exit 1 fi + # ── publish-app (native refusal: a non-tag ref never reaches the registry) ── + test-publish-app: + timeout-minutes: 5 + needs: [select-ci-tests] + if: "${{ github.event_name != 'merge_group' && !startsWith(github.event.head_commit.message, 'chore(main): release ') && contains(fromJSON(needs.select-ci-tests.outputs.selected), 'test-publish-app') }}" + runs-on: ubuntu-latest + permissions: + contents: read + steps: + - name: 🔒 Harden Runner + uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + with: + egress-policy: audit + + - name: 📑 Checkout + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + + # CI runs on a branch or pull request ref, which admission must refuse before any registry + # contact. The job holds no package or OIDC grant, so nothing could be published anyway. + - name: 🧪 Run publish-app on a non-tag ref (expected to refuse) + id: publish + continue-on-error: true + uses: $/actions/publish-app + with: + app-name: app + github-token: not-a-credential + + - name: ✅ Verify the action refused without resolving a release + shell: bash + env: + OUTCOME: ${{ steps.publish.outcome }} + VERSION: ${{ steps.publish.outputs.version }} + run: | + if [[ "$OUTCOME" != "failure" || -n "$VERSION" ]]; then + echo "::error::Expected publish-app to refuse a non-tag ref, got outcome '$OUTCOME' and version '$VERSION'" + exit 1 + fi + + - name: 🔎 Verify the refusal names the runner's real non-tag ref + shell: bash + env: + MODE: deployment + APP_NAME: app + DEPLOY_PATH: ./deploy + GH_TOKEN: not-a-credential + run: | + if bash actions/publish-app/admit.sh >"$RUNNER_TEMP/admit.log" 2>&1; then + echo "::error::admission accepted $GITHUB_REF" + exit 1 + fi + cat "$RUNNER_TEMP/admit.log" + grep -qF 'publish-app must run on a release tag' "$RUNNER_TEMP/admit.log" + # ── stale issues (real upstream action, loopback mutations only) ── test-triage-stale-issues: timeout-minutes: 5 @@ -2305,6 +2360,10 @@ jobs: shell: bash run: bash .github/tests/test-publish-manifests-promotion.sh + - name: 🔏 Check publish-app signs as the caller and refuses before any registry write + shell: bash + run: bash .github/tests/test-publish-app-action.sh + lint-ci-coverage-parity: timeout-minutes: 10 if: "${{ github.event_name != 'merge_group' && !startsWith(github.event.head_commit.message, 'chore(main): release ') }}" @@ -3199,7 +3258,7 @@ jobs: run: bash .github/tests/test-check-release-pr-lifecycle.sh - test-publish-app: + test-publish-app-workflow: if: "${{ github.event_name == 'push' && !startsWith(github.event.head_commit.message, 'chore(main): release ') }}" name: "[Test] Publish App - Dry Run" uses: $/.github/workflows/publish-app.yaml @@ -5211,6 +5270,7 @@ jobs: - test-publish-release-recovery - test-publish-release-recovery-disabled - test-release-please-semantics + - test-publish-app-workflow - test-publish-app - test-publish-app-caller-pin - test-publish-manifests @@ -5330,6 +5390,7 @@ jobs: ${{ needs.test-publish-release-recovery.result }} ${{ needs.test-publish-release-recovery-disabled.result }} ${{ needs.test-release-please-semantics.result }} + ${{ needs.test-publish-app-workflow.result }} ${{ needs.test-publish-app.result }} ${{ needs.test-publish-app-caller-pin.result }} ${{ needs.test-publish-manifests.result }} diff --git a/README.md b/README.md index ab7f3632..7b70ec1a 100644 --- a/README.md +++ b/README.md @@ -98,6 +98,7 @@ declaration. These are steps inside a caller-owned job, not required workflows. | [enable-auto-merge-on-pr](actions/enable-auto-merge-on-pr/README.md) | Enable auto-merge on a pull request | [action.yaml](actions/enable-auto-merge-on-pr/action.yaml) | | [free-disk-space](actions/free-disk-space/README.md) | Reclaim runner disk by removing large preinstalled toolchains | [action.yaml](actions/free-disk-space/action.yaml) | | [login-to-ghcr](actions/login-to-ghcr/README.md) | Login to GitHub Container Registry | [action.yaml](actions/login-to-ghcr/action.yaml) | +| [publish-app](actions/publish-app/README.md) | Publish a release signed as the calling workflow on its release tag; refuses tags not on the default branch | [action.yaml](actions/publish-app/action.yaml) | | [run-dotnet-tests](actions/run-dotnet-tests/README.md) | Test .NET solution or project with coverage | [action.yaml](actions/run-dotnet-tests/action.yaml) | | [setup-go-toolchain](actions/setup-go-toolchain/README.md) | Setup Go with private module discovery | [action.yaml](actions/setup-go-toolchain/action.yaml) | | [setup-ksail-cli](actions/setup-ksail-cli/README.md) | Install KSail CLI via Homebrew | [action.yaml](actions/setup-ksail-cli/action.yaml) | @@ -615,6 +616,8 @@ jobs:
Purpose, interface and usage +> **Successor:** the [publish-app action](actions/publish-app/README.md) publishes the same image and manifests from the product's own release workflow, signed as that workflow on its release tag. This workflow is unchanged until every consumer has moved. + [.github/workflows/publish-app.yaml](.github/workflows/publish-app.yaml) builds and publishes a containerized app and its Kubernetes manifests to GHCR as cosign-signed OCI artifacts. Normal publication first requires authenticated evidence that the normalized version is absent from both repositories. The default mode builds under a run-specific staging tag, pins the produced image digest into the deployment manifest, stages the manifests artifact, and signs both produced digests. After both signatures succeed and an image copy preserves the signed digest, another authenticated absence check precedes promotion of the version and source-SHA image tags and manifests version. Stable releases then move both `latest` tags from those exact digests; prereleases preserve stable `latest`. Signing failures leave version and latest tags unchanged. Opt-in signed promotion below also verifies signature identity and signed publication claims before exposing release tags. #### Usage diff --git a/actions/publish-app/README.md b/actions/publish-app/README.md new file mode 100644 index 00000000..657c3681 --- /dev/null +++ b/actions/publish-app/README.md @@ -0,0 +1,148 @@ +# Publish App + +Publish a product release to GHCR from the product's own release workflow. The image and the +manifests artifact are keylessly signed **as the calling workflow on its release tag**, so the +signature names the product's own repository and tag: + +```text +https://github.com///.github/workflows/@refs/tags/vX.Y.Z +``` + +The platform can then trust each tenant's own repository on release tags instead of approving +every revision of a shared publisher. + +## What it does + +1. **Admits the release, before any registry contact.** It refuses unless: + - the run's ref is `refs/tags/v` (SemVer 2.0.0; a pre-release is allowed, build + metadata is dropped from the published version, and every number has at most 15 digits); + - the calling workflow ref is this repository's own workflow file on that exact tag; + - the tagged commit is checked out at the workspace root, without a persisted git credential + (`persist-credentials: false`; the checkout is also the image build context); + - the tagged commit is an ancestor of the repository's default branch, which it fetches + explicitly. When that cannot be established (fetch failure, no default-branch commit, a git + error), it refuses; + - the inputs, `deploy-path` and `app-manifest` are valid (see below), and in `deployment` mode + the app manifest holds exactly one container named `app-name`; + - `deploy-path` holds only regular files and directories (no links or special files) and is + exactly the tagged source: no changed, removed or untracked (ignored included) file in it. + Build outputs elsewhere in the workspace are fine. +2. **Refuses an occupied release version** in the image and manifests repositories, from an + authenticated, definitive registry read, before the first write. +3. **Builds or verifies the image.** + - `deployment`: builds the repository root's `Dockerfile` with `docker/build-push-action` and + pushes it to an unselectable `staging--` tag. + - `prebuilt-webapp`: verifies the caller's own public main-CI image signature and SLSA + provenance (`ci.yaml@refs/heads/main`) and the CI run evidence, then pins that image into the + WebApp. No image is built. + - `manifests`: no image. +4. **Pins and stages.** In `deployment` mode it pins `image@digest` into the app manifest, then + pushes the **whole** `deploy-path` as the manifests artifact to a staging tag. +5. **Signs and verifies by digest.** It signs the image and the artifact (with publication claims + such as `devantler.source-sha`, `devantler.version` and `devantler.workflow-ref`), then runs + `cosign verify` on both against the calling workflow's identity and the GitHub OIDC issuer. +6. **Exposes only verified releases.** After an image copy keeps the signed digest and the + version is still unpublished, it tags the version, and `latest` for non-pre-releases. A failed + verification exposes nothing. + +No credential is ever passed on a command line. The default-branch fetch gets the token through +its environment, flux and cosign use the registry login that `docker/login-action` writes, and the +version check gives curl its login and bearer token in private config files. + +## Inputs + +| Name | Description | Required | Default | +|------|-------------|----------|---------| +| `mode` | `deployment`, `prebuilt-webapp` or `manifests` | No | `deployment` | +| `app-name` | Container name in the Deployment, or the WebApp name, namespace and spec name in `prebuilt-webapp` mode. A DNS label. Must be empty in `manifests` mode | In `deployment` and `prebuilt-webapp` modes | `""` | +| `deploy-path` | Relative directory of plain names, published whole as the manifests artifact | No | `./deploy` | +| `app-manifest` | Path, relative to `deploy-path`, of the manifest holding the app. Empty means `deployment.yaml` (`webapp.yaml` in `prebuilt-webapp` mode). Must be empty in `manifests` mode | No | `""` | +| `oci-name` | `manifests` mode only: `/` to publish under, in the caller's own namespace, for a repository whose name is not a valid OCI path (for example `.github`) | No | `""` | +| `prebuilt-image-digest` | `prebuilt-webapp` mode only: the original main-CI image `sha256:` digest | In `prebuilt-webapp` mode | `""` | +| `prebuilt-public-digest` | `prebuilt-webapp` mode only: the public artifact SHA-256 recorded by the image producer | In `prebuilt-webapp` mode | `""` | +| `prebuilt-ci-run-id` | `prebuilt-webapp` mode only: the successful public main-push CI run ID | In `prebuilt-webapp` mode | `""` | +| `prebuilt-ci-run-attempt` | `prebuilt-webapp` mode only: that run's successful, still current attempt | In `prebuilt-webapp` mode | `""` | +| `github-token` | Token for the default-branch fetch, registry reads and GHCR writes | No | `${{ github.token }}` | + +`deploy-path` and `app-manifest` must be plain names joined by `/` (letters, digits, `.`, `_` +and `-`; no leading `/`, no empty, `.`, `..` or hidden component, no line break), and no +directory on the way, nor the manifest itself, may be a link. A refused value is not echoed. + +## Outputs + +| Name | Description | +|------|-------------| +| `version` | The published version: the tag without its `v` and build metadata | +| `image-digest` | The signed image digest (empty in `manifests` mode) | +| `manifests-digest` | The signed manifests artifact digest | + +## Usage + +The calling job must grant `contents: read`, `packages: write` and `id-token: write`, check out +the tagged commit at the workspace root with `persist-credentials: false` (required), and call the action **directly in the product's release +workflow**. Called from inside a reusable workflow, the certificate would name that workflow +instead, and verification would refuse to expose the release. Serialize publishers of one +repository with a job concurrency group that never cancels. + +### Build and publish an app + +```yaml +on: + push: + tags: + - "v*" + +permissions: {} + +jobs: + publish: + runs-on: ubuntu-latest + timeout-minutes: 60 + concurrency: + group: publish-${{ github.repository }} + cancel-in-progress: false + permissions: + contents: read # checkout and the default-branch fetch + packages: write # push the image and the manifests artifact + id-token: write # keyless cosign signing as this workflow + steps: + - uses: step-security/harden-runner@ # vX.Y.Z + with: + egress-policy: audit + - uses: actions/checkout@ # vX.Y.Z + with: + persist-credentials: false # required: publish-app refuses a persisted credential + - uses: devantler-tech/.github/actions/publish-app@ # vX.Y.Z + with: + app-name: my-app + app-manifest: base/deployment.yaml # optional; default deployment.yaml +``` + +### Publish a prebuilt WebApp + +```yaml + - uses: devantler-tech/.github/actions/publish-app@ # vX.Y.Z + with: + mode: prebuilt-webapp + app-name: my-site + prebuilt-image-digest: sha256: + prebuilt-public-digest: + prebuilt-ci-run-id: "" + prebuilt-ci-run-attempt: "" +``` + +### Publish manifests only + +```yaml + - uses: devantler-tech/.github/actions/publish-app@ # vX.Y.Z + with: + mode: manifests + oci-name: devantler-tech/github-config # optional +``` + +## Not covered + +- **Recovery** of a release whose version tags were lost after signing is not part of this action + yet; the reusable workflows keep their recovery modes until a follow-up carries it over. +- The reusable `publish-app.yaml` and `publish-manifests.yaml` workflows are unchanged and keep + their shared-publisher identity until every consumer has moved. diff --git a/actions/publish-app/action.yaml b/actions/publish-app/action.yaml new file mode 100644 index 00000000..df09355e --- /dev/null +++ b/actions/publish-app/action.yaml @@ -0,0 +1,154 @@ +name: Publish App +description: Publish a product release to GHCR, keylessly signed as the calling workflow on its release tag, and expose its version only after both signatures verify. +author: devantler-tech + +inputs: + mode: + description: "What to publish: `deployment` builds the image and pins it into a Deployment, `prebuilt-webapp` verifies the caller's main-CI image and pins it into a WebApp, `manifests` publishes only the manifests." + required: false + default: deployment + app-name: + description: "Container name in the Deployment, or the WebApp metadata name, namespace and spec name in prebuilt-webapp mode. Must be empty in manifests mode." + required: false + default: "" + deploy-path: + description: "Relative directory of Kubernetes manifests, published whole as the signed manifests artifact." + required: false + default: ./deploy + app-manifest: + description: "Path, relative to deploy-path, of the manifest holding the app. Empty means deployment.yaml (webapp.yaml in prebuilt-webapp mode). Must be empty in manifests mode." + required: false + default: "" + oci-name: + description: "Manifests mode only: OCI name (`/`, in the caller's own namespace) to publish under instead of the repository name, for repositories whose name is not a valid OCI path." + required: false + default: "" + prebuilt-image-digest: + description: "Prebuilt-webapp mode only: the original main-CI image sha256 digest." + required: false + default: "" + prebuilt-public-digest: + description: "Prebuilt-webapp mode only: the public artifact SHA-256 recorded by the original image producer." + required: false + default: "" + prebuilt-ci-run-id: + description: "Prebuilt-webapp mode only: the successful public main-push CI run ID that built the image." + required: false + default: "" + prebuilt-ci-run-attempt: + description: "Prebuilt-webapp mode only: that CI run's successful attempt, which must still be current." + required: false + default: "" + github-token: + description: "Token for the default-branch fetch, registry reads and GHCR writes. Needs contents read and packages write." + required: false + default: ${{ github.token }} + +outputs: + version: + description: "The published version: the tag without its `v` and build metadata." + value: ${{ steps.admit.outputs.version }} + image-digest: + description: "The signed image digest (empty in manifests mode)." + value: ${{ steps.publish.outputs.image-digest }} + manifests-digest: + description: "The signed manifests artifact digest." + value: ${{ steps.publish.outputs.manifests-digest }} + +runs: + using: composite + steps: + # Every refusal happens here, before login and before any registry read or write. + - name: 🔒 Admit the release tag, inputs and manifests + id: admit + shell: bash + env: + MODE: ${{ inputs.mode }} + APP_NAME: ${{ inputs.app-name }} + DEPLOY_PATH: ${{ inputs.deploy-path }} + APP_MANIFEST: ${{ inputs.app-manifest }} + OCI_NAME: ${{ inputs.oci-name }} + PREBUILT_IMAGE_DIGEST: ${{ inputs.prebuilt-image-digest }} + PREBUILT_PUBLIC_DIGEST: ${{ inputs.prebuilt-public-digest }} + PREBUILT_CI_RUN_ID: ${{ inputs.prebuilt-ci-run-id }} + PREBUILT_CI_RUN_ATTEMPT: ${{ inputs.prebuilt-ci-run-attempt }} + GH_TOKEN: ${{ inputs.github-token }} + run: bash "$GITHUB_ACTION_PATH/admit.sh" + + - name: ⚙️ Setup Flux CLI + uses: fluxcd/flux2/action@3fa784edd773d56630bb9f62645e08f70c2cff0a # v2.10.0 + + - name: ✍️ Setup Cosign + uses: sigstore/cosign-installer@756fe2d284c7292c4e9b62c8a28f1e5629c59532 # v4.1.3 + + - name: 🔐 Log in to registry + uses: docker/login-action@8aa5831fb8d48d8f76594c7ddde61ae58019f550 # v4.8.0 + with: + registry: ghcr.io + username: ${{ github.actor }} + password: ${{ inputs.github-token }} + + # Verifies the caller's own main-CI image and provenance, then pins the WebApp. No registry write. + - name: 🔎 Verify the original image and pin the WebApp + id: prebuilt + if: ${{ inputs.mode == 'prebuilt-webapp' }} + shell: bash + env: + APP_NAME: ${{ inputs.app-name }} + DEPLOY_PATH: ${{ inputs.deploy-path }} + APP_MANIFEST: ${{ inputs.app-manifest }} + PREBUILT_IMAGE_DIGEST: ${{ inputs.prebuilt-image-digest }} + PREBUILT_PUBLIC_DIGEST: ${{ inputs.prebuilt-public-digest }} + PREBUILT_CI_RUN_ID: ${{ inputs.prebuilt-ci-run-id }} + PREBUILT_CI_RUN_ATTEMPT: ${{ inputs.prebuilt-ci-run-attempt }} + run: REPOSITORY="$GITHUB_REPOSITORY" SOURCE_SHA="$GITHUB_SHA" bash "$GITHUB_ACTION_PATH/../../.github/scripts/prepare-prebuilt-webapp.sh" + + - name: 🔒 Refuse an occupied release version + shell: bash + env: + VERSION: ${{ steps.admit.outputs.version }} + IMAGE: ${{ steps.admit.outputs.image }} + ARTIFACT: ${{ steps.admit.outputs.artifact }} + GH_TOKEN: ${{ inputs.github-token }} + run: bash "$GITHUB_ACTION_PATH/require-unpublished.sh" + + - name: 🏷️ Derive image labels + id: meta + if: ${{ inputs.mode == 'deployment' }} + uses: docker/metadata-action@dc802804100637a589fabce1cb79ff13a1411302 # v6.2.0 + with: + images: ${{ steps.admit.outputs.image }} + flavor: latest=false + tags: | + type=semver,pattern={{version}} + type=sha + + # The image goes to an unselectable, run-specific staging tag; only verified digests are exposed. + - name: 🐳 Build & push image + id: build + if: ${{ inputs.mode == 'deployment' }} + uses: docker/build-push-action@c3c9e263c25d99ce0380d002d59b67737d91b0dc # v7.4.0 + with: + context: . + push: true + tags: ${{ steps.admit.outputs.staging-image }} + labels: ${{ steps.meta.outputs.labels }} + + - name: 📦 Sign, verify and expose the release + id: publish + shell: bash + env: + MODE: ${{ inputs.mode }} + APP_NAME: ${{ inputs.app-name }} + VERSION: ${{ steps.admit.outputs.version }} + IMAGE: ${{ steps.admit.outputs.image }} + ARTIFACT: ${{ steps.admit.outputs.artifact }} + DEPLOY_PATH: ${{ steps.admit.outputs.deploy-path }} + MANIFEST: ${{ steps.admit.outputs.manifest }} + BUILT_DIGEST: ${{ steps.build.outputs.digest }} + PREBUILT_DIGEST: ${{ steps.prebuilt.outputs.digest }} + PUBLIC_DIGEST: ${{ steps.prebuilt.outputs.public-digest }} + PREBUILT_CI_RUN_ID: ${{ inputs.prebuilt-ci-run-id }} + PREBUILT_CI_RUN_ATTEMPT: ${{ inputs.prebuilt-ci-run-attempt }} + GH_TOKEN: ${{ inputs.github-token }} + run: bash "$GITHUB_ACTION_PATH/publish.sh" diff --git a/actions/publish-app/admit.sh b/actions/publish-app/admit.sh new file mode 100644 index 00000000..5a3fc8d0 --- /dev/null +++ b/actions/publish-app/admit.sh @@ -0,0 +1,227 @@ +#!/usr/bin/env bash +# Admit one release before anything reaches the registry. +# +# The keyless signature this action makes names the calling workflow at its ref, so only a +# complete semantic-version tag whose commit is already on the default branch may publish. Every +# input and manifest check also runs here, before login, so a refused release leaves the registry +# untouched. Nothing is echoed back until it has passed its own check. +set +x +set -euo pipefail +umask 077 +export LC_ALL=C + +fail() { + echo "::error::publish-app refused the release: $*" >&2 + exit 1 +} + +catalogue="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)" +name='[A-Za-z0-9_-][A-Za-z0-9_.-]*' + +check_inputs() { + case "${MODE:-}" in + deployment | prebuilt-webapp | manifests) ;; + *) fail "mode must be deployment, prebuilt-webapp or manifests" ;; + esac + [[ -n "${GITHUB_OUTPUT:-}" ]] || fail 'missing step output file' + [[ "${GITHUB_RUN_ID:-}" =~ ^[1-9][0-9]{0,19}$ && "${GITHUB_RUN_ATTEMPT:-}" =~ ^[1-9][0-9]{0,5}$ ]] || + fail 'require a numeric workflow run ID and attempt' + [[ "${GITHUB_REPOSITORY:-}" =~ ^[A-Za-z0-9]([A-Za-z0-9-]{0,37}[A-Za-z0-9])?/[A-Za-z0-9_.-]{1,100}$ ]] || + fail 'invalid calling repository' + [[ "${GITHUB_SHA:-}" =~ ^[0-9a-f]{40}$ ]] || fail 'require the tagged commit SHA' + [[ -n "${GITHUB_ACTOR:-}" && -n "${GH_TOKEN:-}" ]] || fail 'missing registry credentials (github-token)' + local prebuilt="${PREBUILT_IMAGE_DIGEST:-}${PREBUILT_PUBLIC_DIGEST:-}${PREBUILT_CI_RUN_ID:-}${PREBUILT_CI_RUN_ATTEMPT:-}" + if [[ "$MODE" == manifests ]]; then + [[ -z "${APP_NAME:-}" && -z "${APP_MANIFEST:-}" && -z "$prebuilt" ]] || + fail 'manifests mode takes no app-name, app-manifest or prebuilt-* input' + else + [[ "${APP_NAME:-}" =~ ^[a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?$ ]] || + fail 'app-name must be a DNS label (the container name, or the WebApp name in prebuilt mode)' + [[ -z "${OCI_NAME:-}" ]] || fail 'oci-name applies only to manifests mode' + fi + if [[ "$MODE" == prebuilt-webapp ]]; then + [[ "${PREBUILT_IMAGE_DIGEST:-}" =~ ^sha256:[0-9a-f]{64}$ ]] || fail 'prebuilt-image-digest must be a sha256 digest' + [[ "${PREBUILT_PUBLIC_DIGEST:-}" =~ ^[0-9a-f]{64}$ ]] || fail 'prebuilt-public-digest must be a SHA-256 hex digest' + [[ "${PREBUILT_CI_RUN_ID:-}" =~ ^[1-9][0-9]{0,14}$ && "${PREBUILT_CI_RUN_ATTEMPT:-}" =~ ^[1-9][0-9]{0,5}$ ]] || + fail 'prebuilt-ci-run-id and prebuilt-ci-run-attempt must be positive numbers' + elif [[ -n "$prebuilt" ]]; then + fail 'prebuilt-* inputs apply only to prebuilt-webapp mode' + fi +} + +# The ref must be refs/tags/v. Keep this identical to the tag step of the reusable +# publish workflows: Flux and docker/metadata-action must both be able to read and order it. +check_release_tag() { + local ref="${GITHUB_REF:-}" num pre build semver numbers too_long + [[ "$ref" == refs/tags/* ]] || fail 'publish-app must run on a release tag (refs/tags/v), not a branch or pull request ref' + tag="${ref#refs/tags/}" + num='(0|[1-9][0-9]*)' + pre='(0|[1-9][0-9]*|[0-9]*[A-Za-z-][0-9A-Za-z-]*)' + build='[0-9A-Za-z-]+' + semver="^v${num}\.${num}\.${num}(-${pre}(\.${pre})*)?(\+${build}(\.${build})*)?$" + [[ "$tag" =~ $semver ]] || fail 'the release tag must be a complete semantic version vMAJOR.MINOR.PATCH[-PRERELEASE][+BUILD]' + # An OCI tag cannot carry `+`, and SemVer ignores build metadata when ordering versions. + version="${tag#v}" + version="${version%%+*}" + numbers=".${version%%-*}." + case "$version" in *-*) numbers="${numbers}${version#*-}." ;; esac + too_long='\.[0-9]{16,}\.' + if [[ "${#tag}" -gt 256 || "$numbers" =~ $too_long ]]; then + fail "tag $tag must be at most 256 characters with numbers of at most 15 digits" + fi + [[ "${#version}" -le 128 ]] || fail "version $version is longer than the 128 characters an OCI tag allows" +} + +# The certificate identity is the calling workflow at this tag. Require the workflow ref to name +# this repository and this exact tag, so the identity verified later is the caller's own. +check_signer_identity() { + local ref="${GITHUB_WORKFLOW_REF:-}" prefix="${GITHUB_REPOSITORY}/.github/workflows/" file + [[ "$ref" != *$'\n'* && "$ref" != *$'\r'* ]] || fail 'invalid calling workflow ref' + [[ "$ref" == "$prefix"* && "$ref" == *"@$GITHUB_REF" ]] || + fail "the calling workflow must belong to $GITHUB_REPOSITORY and run on $GITHUB_REF" + file="${ref#"$prefix"}" + file="${file%"@$GITHUB_REF"}" + [[ "$file" =~ ^${name}$ ]] || fail 'the calling workflow must be a file directly in .github/workflows' +} + +check_checkout() { + local head status=0 + head="$(git rev-parse --verify --quiet 'HEAD^{commit}')" || + fail 'check out the tagged commit (actions/checkout) at the workspace root before publish-app' + [[ "$(git rev-parse --show-toplevel 2>/dev/null)" -ef . ]] || + fail 'check out the tagged commit at the workspace root (actions/checkout without a path)' + [[ "$head" == "$GITHUB_SHA" ]] || fail "the checked-out commit is not the tagged commit $GITHUB_SHA" + # A credential persisted by actions/checkout (its default) stays in the checkout, which is also the + # image build context. Require persist-credentials: false. + # Also a header without a URL (http.extraheader), which git sends to every host. + git config --local --includes --get-regexp '^http\.(.*\.)?extraheader$' >/dev/null || status=$? + case "$status" in + 0) fail 'the checkout persisted a git credential; check out with persist-credentials: false' ;; + 1) ;; + *) fail 'cannot inspect the checkout configuration' ;; + esac +} + +# The tagged commit must already be on the default branch. Fetch the remote's HEAD (its default +# branch) explicitly; anything that cannot be established refuses the release. +check_default_branch() { + local url fetch_head tip status=0 retry + url="${GITHUB_SERVER_URL:-https://github.com}/${GITHUB_REPOSITORY}.git" + retry="$catalogue/.scripts/retry.sh" + fetch_head="$(git rev-parse --git-path FETCH_HEAD)" || fail 'cannot inspect the checkout' + rm -f -- "$fetch_head" + # The token travels in the environment for this one command, never on the command line. + # The empty first value resets any inherited header list, so exactly one Authorization header is sent. + if ! GIT_TERMINAL_PROMPT=0 GIT_CONFIG_COUNT=2 \ + GIT_CONFIG_KEY_0="http.${GITHUB_SERVER_URL:-https://github.com}/.extraheader" GIT_CONFIG_VALUE_0='' \ + GIT_CONFIG_KEY_1="http.${GITHUB_SERVER_URL:-https://github.com}/.extraheader" \ + GIT_CONFIG_VALUE_1="AUTHORIZATION: basic $(printf 'x-access-token:%s' "$GH_TOKEN" | base64 | tr -d '\n')" \ + bash "$retry" git fetch --quiet --no-tags --no-recurse-submodules "$url" HEAD; then + fail "cannot determine whether $GITHUB_SHA is on the default branch: could not fetch the default branch" + fi + tip="$(git rev-parse --verify --quiet 'FETCH_HEAD^{commit}')" || + fail "cannot determine whether $GITHUB_SHA is on the default branch: no default branch commit was fetched" + git merge-base --is-ancestor "$GITHUB_SHA" "$tip" || status=$? + case "$status" in + 0) ;; + 1) fail "tag $tag points to $GITHUB_SHA, which is not on the default branch; tag a commit from the default branch" ;; + *) fail "cannot determine whether $GITHUB_SHA is on the default branch (git exit $status)" ;; + esac +} + +# Walk below $cursor one component at a time: plain names only, and no link anywhere. +walk() { + local component components + IFS=/ read -r -a components <<<"$1" + for component in "${components[@]}"; do + [[ "$component" =~ ^${name}$ ]] || fail "$2 must be plain names joined by /" + cursor="$cursor/$component" + [[ ! -L "$cursor" ]] || fail "$2 passes through a link" + done +} + +resolve_manifest() { + local deploy="${DEPLOY_PATH:-}" manifest default + [[ "$deploy" != *$'\n'* && "$deploy" != *$'\r'* ]] || fail 'deploy-path must not contain a line break' + deploy="${deploy#./}" + [[ -n "$deploy" && "$deploy" =~ ^${name}(/${name})*$ ]] || + fail "deploy-path must be a relative directory of plain names (no leading '/', no empty, '.' or '..' component)" + cursor=. + walk "$deploy" deploy-path + [[ -d "$cursor" ]] || fail "deploy-path ./$deploy is not a directory" + deploy_dir="$cursor" + manifest_file='' + [[ "$MODE" != manifests ]] || return 0 + default=deployment.yaml + [[ "$MODE" != prebuilt-webapp ]] || default=webapp.yaml + manifest="${APP_MANIFEST:-$default}" + [[ "$manifest" =~ ^${name}(/${name})*$ ]] || + fail "app-manifest must be a relative path of plain names inside deploy-path (letters, digits, '.', '_' and '-'; no leading '/', no empty, '.' or '..' component)" + walk "$manifest" app-manifest + [[ -f "$cursor" ]] || fail "app manifest $cursor not found (check the deploy-path and app-manifest inputs)" + manifest_file="$cursor" + if [[ "$MODE" == deployment ]]; then + local matches + matches="$(APP_NAME="$APP_NAME" yq '[.spec.template.spec.containers[] | select(.name == strenv(APP_NAME))] | length' "$manifest_file")" || + fail "cannot read $manifest_file as a Deployment" + [[ "$matches" == 1 ]] || fail "expected exactly one container named '$APP_NAME' in $manifest_file, found $matches" + fi +} + +# Everything under deploy-path is published, so it may hold only regular files and directories. +check_deploy_contents() { + local unsafe + unsafe="$(find "$deploy_dir" ! -type f ! -type d -print -quit)" || fail 'cannot inspect deploy-path' + [[ -z "$unsafe" ]] || fail 'deploy-path must hold only regular files and directories (no links or special files)' +} + +# The signed manifests must be the tagged source: nothing in deploy-path may differ from the tagged +# commit and nothing untracked (ignored files included) may sit in it. The rest of the workspace may +# hold build outputs. +check_deploy_source() { + local status=0 untracked + git diff --quiet --no-ext-diff --no-textconv HEAD -- "$deploy_dir" || status=$? + case "$status" in + 0) ;; + 1) fail "deploy-path ${deploy_dir#./} differs from the tagged commit; publish only committed manifests" ;; + *) fail 'cannot compare deploy-path with the tagged commit' ;; + esac + untracked="$(git ls-files --others -- "$deploy_dir")" || fail 'cannot list untracked files in deploy-path' + [[ -z "$untracked" ]] || fail "deploy-path ${deploy_dir#./} holds files that are not in the tagged commit" +} + +check_inputs +check_release_tag +check_signer_identity +check_checkout +check_default_branch +resolve_manifest +check_deploy_contents +check_deploy_source + +owner="${GITHUB_REPOSITORY%%/*}" +repository="$(printf '%s' "$GITHUB_REPOSITORY" | tr '[:upper:]' '[:lower:]')" +oci="$repository" +if [[ -n "${OCI_NAME:-}" ]]; then + oci="$(printf '%s' "$OCI_NAME" | tr '[:upper:]' '[:lower:]')" + [[ "${oci%%/*}" == "$(printf '%s' "$owner" | tr '[:upper:]' '[:lower:]')" ]] || + fail "oci-name must stay in the $owner namespace" +fi +segment='[a-z0-9]+([._-][a-z0-9]+)*' +[[ "$oci" =~ ^${segment}(/${segment})+$ && "${#oci}" -lt 240 ]] || + fail 'the OCI name is not a valid registry path; set oci-name in manifests mode' +image='' +staging_image='' +if [[ "$MODE" != manifests ]]; then + image="ghcr.io/$oci" + staging_image="$image:staging-${GITHUB_RUN_ID}-${GITHUB_RUN_ATTEMPT}" +fi +{ + printf 'version=%s\n' "$version" + printf 'image=%s\n' "$image" + printf 'staging-image=%s\n' "$staging_image" + printf 'artifact=%s\n' "ghcr.io/$oci/manifests" + printf 'deploy-path=%s\n' "$deploy_dir" + printf 'manifest=%s\n' "$manifest_file" +} >>"$GITHUB_OUTPUT" +echo "Admitted $tag ($GITHUB_SHA, on the default branch) for $MODE publication as version $version." diff --git a/actions/publish-app/publish.sh b/actions/publish-app/publish.sh new file mode 100644 index 00000000..3dbe44fb --- /dev/null +++ b/actions/publish-app/publish.sh @@ -0,0 +1,140 @@ +#!/usr/bin/env bash +# Pin, stage, sign, verify and only then expose one admitted release. +# +# Everything is signed by digest, as the calling workflow (keyless, GitHub OIDC). Both signatures +# must verify against that workflow's own identity at this tag before any selectable tag moves; +# a failure leaves only run-specific staging tags behind. +# No credential is ever passed on a command line: flux and cosign read the registry login that +# docker/login-action wrote, and the version check reads its credential from a private file. +set +x +set -euo pipefail +umask 077 +export LC_ALL=C + +fail() { + echo "::error::publish-app: $*" >&2 + exit 1 +} + +here="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +issuer=https://token.actions.githubusercontent.com +identity="https://github.com/${GITHUB_WORKFLOW_REF:-}" +tag="${GITHUB_REF#refs/tags/}" +run="${GITHUB_RUN_ID:-}-${GITHUB_RUN_ATTEMPT:-}" + +[[ "${VERSION:-}" =~ ^[0-9A-Za-z][0-9A-Za-z.-]{0,127}$ ]] || fail 'admission did not resolve a version' +[[ "$run" =~ ^[0-9]+-[0-9]+$ ]] || fail 'require a numeric workflow run ID and attempt' +[[ "${ARTIFACT:-}" =~ ^ghcr\.io/[a-z0-9._/-]+/manifests$ ]] || fail 'admission did not resolve the manifests artifact' +[[ -n "${DEPLOY_PATH:-}" && -d "$DEPLOY_PATH" ]] || fail 'admission did not resolve the deploy path' +case "${MODE:-}" in + deployment) digest="${BUILT_DIGEST:-}" ;; + prebuilt-webapp) digest="${PREBUILT_DIGEST:-}" ;; + manifests) digest='' ;; + *) fail 'unknown mode' ;; +esac +if [[ "$MODE" != manifests ]]; then + [[ "${IMAGE:-}" =~ ^ghcr\.io/[a-z0-9._/-]+$ ]] || fail 'admission did not resolve the image' + [[ "$digest" =~ ^sha256:[0-9a-f]{64}$ ]] || fail 'no valid image digest to publish' +fi + +require_unpublished() { # + echo "Checking that $VERSION is still unpublished ($1)." + bash "$here/require-unpublished.sh" +} + +# Deployment mode: inject the digest the build produced into the manifest admission resolved. +pin_image() { + [[ -n "${MANIFEST:-}" && -f "$MANIFEST" && ! -L "$MANIFEST" ]] || fail 'admission did not resolve the app manifest' + PIN="$IMAGE@$digest" yq -i \ + '(.spec.template.spec.containers[] | select(.name == strenv(APP_NAME)).image) = strenv(PIN)' "$MANIFEST" + grep -qF "$IMAGE@$digest" "$MANIFEST" || fail "image pin did not apply to $MANIFEST" +} + +# Push the WHOLE deploy path to an unselectable staging tag and report the digest it produced. +stage_manifests() { + local pushed + pushed="$(flux push artifact "oci://$ARTIFACT:staging-$run" \ + --path="$DEPLOY_PATH" \ + --source="${GITHUB_SERVER_URL:-https://github.com}/$GITHUB_REPOSITORY" \ + --revision="$tag@sha1:$GITHUB_SHA" \ + --output json)" + manifests_digest="$(jq -r '.digest // ""' <<<"$pushed")" + [[ "$manifests_digest" =~ ^sha256:[0-9a-f]{64}$ ]] || fail 'flux push did not report the pushed artifact digest' +} + +# Claims bind each signature to this publication; a verifier can require them. +annotate() { + annotations=( + -a "devantler.repository=$GITHUB_REPOSITORY" + -a "devantler.source-sha=$GITHUB_SHA" + -a "devantler.source-ref=$tag" + -a "devantler.version=$VERSION" + -a "devantler.run-id=$GITHUB_RUN_ID" + -a "devantler.run-attempt=$GITHUB_RUN_ATTEMPT" + -a "devantler.workflow-ref=$GITHUB_WORKFLOW_REF" + ) + [[ "$MODE" == manifests ]] && return 0 + annotations+=( + -a "devantler.image-digest=$digest" + -a "devantler.manifests-digest=$manifests_digest" + ) + if [[ "$MODE" == prebuilt-webapp ]]; then + [[ "${PUBLIC_DIGEST:-}" =~ ^[0-9a-f]{64}$ && "${PREBUILT_CI_RUN_ID:-}" =~ ^[1-9][0-9]{0,14}$ && + "${PREBUILT_CI_RUN_ATTEMPT:-}" =~ ^[1-9][0-9]{0,5}$ ]] || fail 'missing verified original-image publication evidence' + annotations+=( + -a "devantler.public-digest=$PUBLIC_DIGEST" + -a devantler.public-profile=production-template + -a "devantler.ci-run-id=$PREBUILT_CI_RUN_ID" + -a "devantler.ci-run-attempt=$PREBUILT_CI_RUN_ATTEMPT" + -a "devantler.image-producer=https://github.com/$GITHUB_REPOSITORY/.github/workflows/ci.yaml@refs/heads/main" + ) + fi +} + +sign() { + [[ "$MODE" == manifests ]] || cosign sign --yes "${annotations[@]}" "$IMAGE@$digest" + cosign sign --yes "${annotations[@]}" "$ARTIFACT@$manifests_digest" +} + +# Both digests must verify against the calling workflow at this tag before anything is exposed. +verify() { + local ref + for ref in ${IMAGE:+"$IMAGE@$digest"} "$ARTIFACT@$manifests_digest"; do + cosign verify --certificate-identity "$identity" --certificate-oidc-issuer "$issuer" \ + "${annotations[@]}" "$ref" >/dev/null || fail "signature on $ref does not verify as $identity" + done +} + +# A registry-side copy must keep the signed digest before the release version points at it. +check_copy() { + [[ "$MODE" != manifests ]] || return 0 + docker buildx imagetools create --prefer-index=false --tag "$IMAGE:verified-$run" \ + --metadata-file="$RUNNER_TEMP/publish-app-copy.json" "$IMAGE@$digest" + [[ "$(jq -r '.["containerimage.descriptor"].digest // ""' "$RUNNER_TEMP/publish-app-copy.json")" == "$digest" ]] || + fail 'the image copy changed the verified digest' +} + +expose() { + local pointer + for pointer in "$VERSION" latest; do + # A prerelease is selectable by version but never becomes latest. + [[ "$pointer" != latest || "$VERSION" != *-* ]] || continue + [[ "$MODE" == manifests ]] || docker buildx imagetools create --prefer-index=false --tag "$IMAGE:$pointer" "$IMAGE@$digest" + flux tag artifact "oci://$ARTIFACT@$manifests_digest" --tag "$pointer" + done +} + +require_unpublished before-staging +[[ "$MODE" != deployment ]] || pin_image +stage_manifests +annotate +sign +verify +check_copy +require_unpublished before-exposure +expose +{ + printf 'image-digest=%s\n' "$digest" + printf 'manifests-digest=%s\n' "$manifests_digest" +} >>"$GITHUB_OUTPUT" +echo "Published $VERSION, signed and verified as $identity." diff --git a/actions/publish-app/require-unpublished.sh b/actions/publish-app/require-unpublished.sh new file mode 100644 index 00000000..5453df92 --- /dev/null +++ b/actions/publish-app/require-unpublished.sh @@ -0,0 +1,68 @@ +#!/usr/bin/env bash +# Refuse a release version that already exists in any repository this release writes to. Only an +# authenticated, definitive absence passes. +# +# The same contract as .github/scripts/require-unpublished-version.sh, which the retiring reusable +# workflows keep unchanged, except that no credential ever reaches a command line: curl reads the +# registry login and the bearer token from private config files that are removed on exit. +set +x +set -euo pipefail +umask 077 +export LC_ALL=C + +fail() { + echo "::error::publish-app cannot establish an unpublished release version: $*" >&2 + exit 1 +} + +registry=ghcr.io +[[ "${VERSION:-}" =~ ^[A-Za-z0-9_][A-Za-z0-9_.-]{0,127}$ ]] || fail 'invalid normalized OCI version' +[[ "${GITHUB_ACTOR:-}" =~ ^[A-Za-z0-9][A-Za-z0-9-]{0,38}(\[bot\])?$ ]] || fail 'invalid registry user' +# Opaque transport data: exclude whitespace, controls and curl-config quoting without parsing it. +[[ -n "${GH_TOKEN:-}" && "$GH_TOKEN" != *[!A-Za-z0-9._~+/=-]* ]] || fail 'missing or unsafe registry token' +[[ "${ARTIFACT:-}" == "$registry/"* ]] || fail 'missing manifests artifact name' +repositories=("${ARTIFACT#"$registry/"}") +if [[ -n "${IMAGE:-}" ]]; then + [[ "$IMAGE" == "$registry/"* ]] || fail 'invalid image name' + repositories=("${IMAGE#"$registry/"}" "${repositories[@]}") +fi + +work="$(mktemp -d)" +trap 'rm -rf "$work"' EXIT +printf 'user = "%s:%s"\n' "$GITHUB_ACTOR" "$GH_TOKEN" >"$work/login.curlrc" +accept='application/vnd.oci.image.manifest.v1+json, application/vnd.oci.image.index.v1+json, application/vnd.docker.distribution.manifest.v2+json, application/vnd.docker.distribution.manifest.list.v2+json' +for repository in "${repositories[@]}"; do + [[ "$repository" =~ ^[a-z0-9]+([._-][a-z0-9]+)*(/[a-z0-9]+([._-][a-z0-9]+)*)*$ ]] || fail 'invalid OCI repository' + [[ "${#repository}" -lt 256 ]] || fail 'OCI repository is too long' + # The service and scope are query parameters, never an authentication URL from the registry's + # response. No redirects are followed, so the login leaves only for the declared registry. + status="$(curl -q --silent --show-error --proto '=https' --tlsv1.2 \ + --connect-timeout 10 --max-time 30 --max-filesize 1048576 \ + --output "$work/token.json" --write-out '%{http_code}' --get \ + --data-urlencode "service=$registry" --data-urlencode "scope=repository:$repository:pull" \ + --config "$work/login.curlrc" "https://$registry/token")" || fail 'registry authentication request failed' + [[ "$status" == 200 ]] || fail "registry authentication returned HTTP $status" + token="$(jq -ser 'select(length == 1) | .[0] | select(type == "object") | + select(.token == null or .access_token == null or .token == .access_token) | + (.token // .access_token) | select(type == "string" and length > 0)' "$work/token.json" 2>/dev/null)" || + fail 'invalid registry token response' + [[ "$token" =~ ^[A-Za-z0-9._~+/-]+=*$ ]] || fail 'invalid registry bearer token' + printf 'header = "Authorization: Bearer %s"\n' "$token" >"$work/bearer.curlrc" + unset token + status="$(curl -q --silent --show-error --proto '=https' --tlsv1.2 \ + --connect-timeout 10 --max-time 30 --max-filesize 1048576 \ + --output "$work/manifest.json" --write-out '%{http_code}' \ + --config "$work/bearer.curlrc" --header "Accept: $accept" \ + "https://$registry/v2/$repository/manifests/$VERSION")" || fail 'registry version read failed' + rm -f "$work/bearer.curlrc" + case "$status" in + 200) fail "version $VERSION already exists in $repository; publish a new version" ;; + 404) + jq -se 'length == 1 and (.[0] | type == "object" and (.errors | type == "array" and length > 0 and + all(.[]; type == "object" and (.code == "MANIFEST_UNKNOWN" or .code == "NAME_UNKNOWN"))))' \ + "$work/manifest.json" >/dev/null 2>&1 || fail 'registry did not establish version absence' + ;; + *) fail "registry version read returned HTTP $status" ;; + esac +done +echo "Release version $VERSION is absent from every publication target."