How do you sign a container image and attach an SBOM with cosign?
IntermediateHands-onSBOM
Answer
Short answer
Use cosign (Sigstore) to sign the image digest — keyless with your CI's OIDC identity or with a key pair — and attest the SBOM to the same digest. Consumers then verify the signature and attestation before running the image.
Keyless signing in GitHub Actions
permissions: { id-token: write, packages: write, contents: read }
steps:
- uses: sigstore/cosign-installer@v3
- run: |
cosign sign --yes ghcr.io/org/shop-api@${DIGEST}
cosign attest --yes --type cyclonedx --predicate sbom.cdx.json ghcr.io/org/shop-api@${DIGEST}
Verify
cosign verify ghcr.io/org/shop-api@sha256:... \
--certificate-identity-regexp "https://github.com/org/shop-api/.*" \
--certificate-oidc-issuer https://token.actions.githubusercontent.com
cosign verify-attestation --type cyclonedx ghcr.io/org/shop-api@sha256:... \
--certificate-identity-regexp "https://github.com/org/.*" \
--certificate-oidc-issuer https://token.actions.githubusercontent.com
Why sign the digest
Tags (:1.4.0) can be moved to another image; the digest (sha256:...) identifies exact content.
Enforce in Kubernetes
Kyverno verifyImages rule or Sigstore policy-controller: only run images signed by your CI identity.
What to say in the interview
"cosign signs the image digest — keyless via the pipeline's OIDC identity — and attests the SBOM to it. Deployments verify the signature and attestation, and Kyverno enforces that only images signed by our CI can run."
More on SBOM
What is an SBOM and how do you generate one?BeginnerWhat is an SBOM and why does it matter?BeginnerWhat are SPDX and CycloneDX?BeginnerHow do you generate an SBOM with Syft?BeginnerHow do you scan an SBOM for vulnerabilities with Grype or Trivy?BeginnerWhat should an SBOM contain? (NTIA minimum elements)Intermediate