Sub topic 3
Signing from CI and Enforcing Image Trust
Assume you know Docker or OCI images, registries, command-line tools, public-key signatures, and basic GitHub Actions.
Signing from a GitHub Actions workflow
GitHub Actions can sign an image without storing a private signing key. The job receives a short-lived OpenID Connect (OIDC) token. Cosign uses that token to request a certificate from Fulcio, then records the signing event in Rekor.
The workflow needs permission to request an OIDC token. Sign the immutable digest produced by the build step:
name: build-and-sign
on:
push:
branches: [main]
permissions:
contents: read
packages: write
id-token: write
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- uses: docker/build-push-action@v6
id: image
with:
push: true
tags: ghcr.io/${{ github.repository }}:latest
- uses: sigstore/cosign-installer@v3
- name: Sign image digest
run: |
cosign sign --yes \
ghcr.io/${{ github.repository }}@${{ steps.image.outputs.digest }}Verify the certificate identity and issuer before accepting the image:
cosign verify \
--certificate-identity "https://github.com/OWNER/REPO/.github/workflows/build.yml@refs/heads/main" \
--certificate-oidc-issuer "https://token.actions.githubusercontent.com" \
ghcr.io/OWNER/REPO@sha256:<digest>KMS-backed keys
A local key file is simple but must be protected, backed up, and rotated. A cloud key-management service (KMS) keeps the private key under access controls and audit logging. Configure Cosign with the KMS provider URI, then grant CI only the permission to sign.
cosign sign --key awskms:///alias/container-signing "$IMAGE"
cosign verify --key cosign.pub "$IMAGE"Attestations and admission policies
An attestation is a signed statement about an image. An SBOM is a software bill of materials listing its components.
cosign attest --key cosign.key \
--type spdxjson \
--predicate sbom.spdx.json \
"$IMAGE"
cosign verify-attestation --key cosign.pub \
--type spdxjson "$IMAGE"A Kubernetes admission policy can reject unsigned images or images whose signatures do not match the approved identity.
Operational practices
- Rotate KMS keys or keyless identity policies without deleting old verification material.
- Keep old public keys available while signed images remain deployable.
- Check the digest, registry permissions, OIDC permission, certificate identity, and Rekor or network errors when verification fails.
- Test admission policies in audit mode before enforcing rejection.
Exercise
Why does the workflow require id-token: write?
Show answer
Cosign needs the job's short-lived OIDC token to obtain a Fulcio certificate for keyless signing.