diff --git a/.github/workflows/release-image.yml b/.github/workflows/release-image.yml index b372e74..61a88e7 100644 --- a/.github/workflows/release-image.yml +++ b/.github/workflows/release-image.yml @@ -16,9 +16,11 @@ # The provenance is signed in `publish`, the one job that runs no code of the # repository being released: no build, no checkout of it. Its signing account # is a separate identity that the workload identity provider grants only to -# this workflow at a harness tag on a main or version-tag push, so neither a -# Dockerfile step nor a job of the caller can sign. No step of a build job -# holds a credentials file: the build context never contains one. +# this workflow at a harness tag on a main or version-tag push, and only to +# its job in the environment image-provenance (`publish`), so neither a +# Dockerfile step, nor anything else in the build job, nor a job of the +# caller can sign. No step of a build job holds a credentials file: the build +# context never contains one. # # The git tag is set afterwards by release-tag.yml (a job of its own in the # caller, `needs` this one), so this workflow never needs write access to the @@ -397,6 +399,14 @@ jobs: && (needs.build.result == 'success' || (needs.build.result == 'skipped' && needs.verify.outputs.build == 'false')) runs-on: ubuntu-24.04 timeout-minutes: 15 + # The signing identity is granted only to a token whose `environment` + # claim is image-provenance: this job's. Every job of this workflow carries + # the same job_workflow_ref, so without it the build job, which runs the + # Dockerfile's steps, could obtain it too. No deployment is recorded; the + # caller's repository gets the environment on its first release. + environment: + name: image-provenance + deployment: false permissions: contents: read id-token: write @@ -424,10 +434,12 @@ jobs: claims=$(base64 -d <<<"$payload") ref=$(jq -r '.job_workflow_ref // empty' <<<"$claims") sha=$(jq -r '.job_workflow_sha // empty' <<<"$claims") + environment=$(jq -r '.environment // empty' <<<"$claims") case "$ref" in IronFlock/workflows/.github/workflows/release-image.yml@*) ;; *) echo "::error::this job runs from '$ref', not from IronFlock/workflows release-image.yml"; exit 1 ;; esac + [ "$environment" = image-provenance ] || { echo "::error::the job's OIDC token has the environment '$environment', not image-provenance: the signing account would refuse it"; exit 1; } [[ "$sha" =~ ^[0-9a-f]{40}$ ]] || { echo "::error::the job's OIDC token names no workflow commit"; exit 1; } echo "harness: $ref at $sha" echo "sha=$sha" >> "$GITHUB_OUTPUT" diff --git a/.github/workflows/selftest.yml b/.github/workflows/selftest.yml index 309eb47..9467796 100644 --- a/.github/workflows/selftest.yml +++ b/.github/workflows/selftest.yml @@ -582,6 +582,32 @@ jobs: - name: sign, verify, re-run, refuse run: bash scripts/test_sign_provenance_registry.sh localhost:5000 + # The signing account of release-image.yml is granted only to a token whose + # `environment` claim is image-provenance, which its publish job gets from + # `environment: { name: image-provenance, deployment: false }`. This job is + # set up the same way and checks that the claim is there, so a change on + # GitHub's side fails here, not in a release. + provenance-environment-claim: + runs-on: ubuntu-latest + timeout-minutes: 5 + environment: + name: image-provenance + deployment: false + permissions: + contents: read + id-token: write + steps: + - name: the job's OIDC token names the environment + run: | + token=$(curl -fsS -H "Authorization: Bearer $ACTIONS_ID_TOKEN_REQUEST_TOKEN" \ + "$ACTIONS_ID_TOKEN_REQUEST_URL&audience=ironflock-selftest" | jq -r .value) + echo "::add-mask::$token" + payload=$(cut -d. -f2 <<<"$token" | tr '_-' '/+') + while [ $((${#payload} % 4)) != 0 ]; do payload+="="; done + environment=$(base64 -d <<<"$payload" | jq -r '.environment // empty') + echo "environment claim: '$environment'" + [ "$environment" = image-provenance ] || { echo "::error::no environment claim image-provenance with deployment: false; release-image.yml could not sign"; exit 1; } + assert-planted: needs: [security-planted, security-helm, lint-planted] # Also when a scanner job broke: the counts then show what is missing. @@ -617,7 +643,7 @@ jobs: # above succeeded (skipped or cancelled counts as failed). selftest-ok: if: ${{ always() }} - needs: [lint-self, lint-templates, lint-planted, scripts, gate-pass, gate-fail, node-gate-npm, node-gate-npm-tools, node-gate-bun, node-gate-fail, python-gate-pass, python-gate-fail, shell-gate-pass, shell-gate-artifact, shell-gate-fail, shell-gate-arm64, tools-arm64, security-clean, security-clean-node, security-planted, config-scan-skipped-chart, security-helm, release-dry-run, release-dry-run-package-json, build-context-artifact, release-dry-run-build-context, release-dry-run-image-tag, assert-image-tag, provenance-sign, assert-planted] + needs: [lint-self, lint-templates, lint-planted, scripts, gate-pass, gate-fail, node-gate-npm, node-gate-npm-tools, node-gate-bun, node-gate-fail, python-gate-pass, python-gate-fail, shell-gate-pass, shell-gate-artifact, shell-gate-fail, shell-gate-arm64, tools-arm64, security-clean, security-clean-node, security-planted, config-scan-skipped-chart, security-helm, release-dry-run, release-dry-run-package-json, build-context-artifact, release-dry-run-build-context, release-dry-run-image-tag, assert-image-tag, provenance-sign, provenance-environment-claim, assert-planted] runs-on: ubuntu-latest timeout-minutes: 5 steps: diff --git a/README.md b/README.md index 8c55aba..2616809 100644 --- a/README.md +++ b/README.md @@ -151,14 +151,34 @@ the index) and goes to no transparency log, so no image name or digest of a private repository becomes public. The signing account is not the publisher: the workload identity provider maps -an attribute that only this workflow, called at a harness tag (`@v1`) on a -push to `main` or of a version tag, receives, and only that attribute may act -as the signing account, which holds two roles on the key and nothing else: +an attribute that only the `publish` job of this workflow, called at a harness +tag (`@v1`) on a push to `main` or of a version tag, receives, and only that +attribute may act as the signing account. Every job of a reusable workflow +carries the same `job_workflow_ref` claim, so the attribute also requires the +`environment` claim `image-provenance`, which only `publish` has +(`environment: { name: image-provenance, deployment: false }`: no deployment +is recorded; a calling repository gets the environment on its first release). +The mapping: + +```text +attribute.provenance = (assertion.event_name == 'push' + && (assertion.ref == 'refs/heads/main' || assertion.ref.startsWith('refs/tags/v')) + && assertion.job_workflow_ref.startsWith('IronFlock/workflows/.github/workflows/release-image.yml@refs/tags/v') + && has(assertion.environment) && assertion.environment == 'image-provenance') + ? 'release-image' : 'other' +``` + +`has()` keeps the mapping valid for the many tokens without an environment; +a mapping that fails to evaluate refuses every token of the provider. The +self-test job `provenance-environment-claim` checks that GitHub still issues +the claim with `deployment: false`. The signing account holds two roles on the +key and nothing else: `roles/cloudkms.signerVerifier` (sign, read the public key) and `roles/cloudkms.viewer` (cosign reads the key and the key version before it signs; `signerVerifier` alone fails with PermissionDenied on -`cloudkms.cryptoKeys.get`). A caller's own jobs and the Dockerfile's steps, which run in -the build job, cannot obtain it; no build job holds a credentials file. The +`cloudkms.cryptoKeys.get`). A caller's own jobs cannot obtain it, nor can the +build job, where the Dockerfile's steps run, even if a step broke out of +BuildKit's sandbox; no build job holds a credentials file. The `publish` job takes its scripts and cosign from the commit of `release-image.yml` it runs from (the `job_workflow_sha` claim of its OIDC token), never from the caller's `harness-ref`. diff --git a/scripts/sign-provenance.sh b/scripts/sign-provenance.sh index c1184d4..df2381e 100755 --- a/scripts/sign-provenance.sh +++ b/scripts/sign-provenance.sh @@ -33,6 +33,13 @@ set -euo pipefail BUILDKIT_SLSA_V1=https://github.com/moby/buildkit/blob/master/docs/attestations/slsa-definitions.md +# The predicate type, given as its URI: with the short name slsaprovenance1, +# cosign parses the predicate into its SLSA v1 struct and drops BuildKit's +# own metadata (runDetails.metadata.buildkit_metadata: the vcs source and +# revision, the layers; buildkit_completeness). With the URI it signs the +# predicate unchanged, under the same predicate type, so verifying with +# `--type slsaprovenance1` finds it as before. +SLSA_PROVENANCE_V1=https://slsa.dev/provenance/v1 check() { local file=$1 platform=$2 btype target builder revision source attempt @@ -113,7 +120,7 @@ sign() { check "$work/predicate.json" "$platform" "$COSIGN" attest --yes "${registry_flags[@]}" --key "$COSIGN_KEY" \ --signing-config "$work/no-services.json" \ - --type slsaprovenance1 --predicate "$work/predicate.json" "$ref" >/dev/null + --type "$SLSA_PROVENANCE_V1" --predicate "$work/predicate.json" "$ref" >/dev/null echo "$platform: provenance signed and attached" done <<<"$platforms" diff --git a/scripts/test_sign_provenance_registry.sh b/scripts/test_sign_provenance_registry.sh index 5fa8e47..46fc749 100755 --- a/scripts/test_sign_provenance_registry.sh +++ b/scripts/test_sign_provenance_registry.sh @@ -35,6 +35,21 @@ attestations() { --insecure-ignore-tlog "$1" 2>/dev/null || true; } | grep -c . || true } +# same_as_buildkit : every signed predicate on equals, +# key for key, the provenance BuildKit attached for its platform (cosign must +# not reshape it: its typed SLSA v1 parsing drops BuildKit's own metadata) +same_as_buildkit() { + local ref=$1 key=$2 provenance signed predicate platform + provenance=$(docker buildx imagetools inspect "$ref" --format '{{json .Provenance}}') + signed=$("$COSIGN" verify-attestation --allow-http-registry --key "$key" --type slsaprovenance1 \ + --insecure-ignore-tlog "$ref" 2>/dev/null | jq -c '.payload | @base64d | fromjson | .predicate') + [ -n "$signed" ] || return 1 + while read -r predicate; do + platform=$(jq -r '.buildDefinition.internalParameters.targetPlatform' <<<"$predicate") + diff <(jq -S --arg p "$platform" '.[$p].SLSA' <<<"$provenance") <(jq -S . <<<"$predicate") || return 1 + done <<<"$signed" +} + # The build context: a clean git repository, so the provenance names a known # source and revision. src="$work/src" @@ -78,6 +93,8 @@ expect " and nothing attached" 0 test "$(attestations "$index" "$work/release.p expect "both platforms signed" 0 "$script" sign "$index" expect " two attestations verify" 0 test "$(attestations "$index" "$work/release.pub")" = 2 expect " they name both platforms" 0 bash -c "\"$COSIGN\" verify-attestation --allow-http-registry --key '$work/release.pub' --type slsaprovenance1 --insecure-ignore-tlog '$index' 2>/dev/null | jq -r '.payload | @base64d | fromjson | .predicate.buildDefinition.internalParameters.targetPlatform' | sort | paste -sd, - | grep -qx linux/amd64,linux/arm64" +expect " the signed predicates are BuildKit's, unchanged" 0 same_as_buildkit "$index" "$work/release.pub" +expect " as in-toto statements of predicate type SLSA provenance v1" 0 bash -c "\"$COSIGN\" verify-attestation --allow-http-registry --key '$work/release.pub' --type https://slsa.dev/provenance/v1 --insecure-ignore-tlog '$index' 2>/dev/null | jq -r '.payload | @base64d | fromjson | .predicateType' | sort -u | grep -qx https://slsa.dev/provenance/v1" expect " another key verifies none" 0 test "$(attestations "$index" "$work/other.pub")" = 0 expect "re-run signs nothing twice" 0 "$script" sign "$index" expect " still two attestations" 0 test "$(attestations "$index" "$work/release.pub")" = 2