Geavanceerd
Zoeken inHow-to-gidsen
Zo zoek je
  • Typ de taak in je eigen woorden (poort vinden, firewall openen, schijf vol) of plak een foutmelding tussen aanhalingstekens.
  • Kies "Snel antwoord" om alleen de commando's te zien, of een gebied om in één vakgebied te blijven.
  • "twee woorden" vindt precies die zin.
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken
Probeer:

How-to 124Security · Supply chain

How to sign and verify an image with Cosign

A tag like shop/api:1.4.2 proves nothing — anyone with push rights can replace the image behind it. A signature proves the image was built by your pipeline and not changed. Cosign (Sigstore) signs images by digest, with a key pair or keyless with the CI's identity, and Kubernetes can refuse unsigned images. This how-to signs, verifies, and enforces it.

  • 5 Lessen
  • 3 Commando's

Dit deel is nog niet vertaald. Je ziet de Engelse tekst.

$ als gewone gebruiker# als root (of met sudo)

124.1

The quick answer

With a key pair:

cosign generate-key-pair
cosign sign --key cosign.key registry.example.com/shop/api@sha256:4a1f…
cosign verify --key cosign.pub registry.example.com/shop/api@sha256:4a1f…

Keyless in GitHub Actions (no key to store): cosign sign --yes IMAGE@DIGEST with permissions: id-token: write.

124.2

Step by step

  1. Always sign the digest, never only the tag — get it from the push or:
docker buildx imagetools inspect registry.example.com/shop/api:1.4.2 --format '{{.Manifest.Digest}}'
  1. Key pair (quick answer): cosign.key is encrypted with a password — keep it in a secret store (or use a cloud KMS: --key awskms://…, azurekms://…). Share only cosign.pub.
  2. Keyless in CI — the signature is tied to the workflow identity, recorded in the public transparency log (Rekor):
.github/workflows/release.yml (part)
permissions:
  contents: read
  packages: write
  id-token: write
steps:
  - run: cosign sign --yes ghcr.io/org/shop@${{ steps.build.outputs.digest }}
  1. Verify keyless — check who signed (the workflow) and the issuer:
cosign verify ghcr.io/org/shop@sha256:4a1f… --certificate-identity "https://github.com/org/shop/.github/workflows/release.yml@refs/heads/main" --certificate-oidc-issuer "https://token.actions.githubusercontent.com" -o text
  1. See what is attached (signatures, SBOM attestations):
cosign tree registry.example.com/shop/api@sha256:4a1f…
  1. Enforce it in Kubernetes — Kyverno verifyImages (or Sigstore policy-controller) refuses Pods with unsigned images:
policies/verify-images.yaml (part)
      verifyImages:
        - imageReferences: ["registry.example.com/shop/*"]
          attestors:
            - entries:
                - keys:
                    publicKeys: |-
                      -----BEGIN PUBLIC KEY-----
                      <cosign.pub content>
                      -----END PUBLIC KEY-----

124.3

Check that it worked

cosign verify prints the verified signature (exit code 0); verifying a different digest fails; and in the cluster a Pod with an unsigned image is refused (or reported, in Audit mode).

124.4

Common problems

ProblemCauseFix
no matching signaturesSigned another digest (or only the tag moved).Sign and verify the same @sha256: digest.
Keyless verify fails on identity--certificate-identity does not match the workflow path/branch exactly.Copy it from the signature (cosign verify … -o text); or use --certificate-identity-regexp.
UNAUTHORIZED when signingNo push rights to the registry (signatures are stored there).Log in with a robot/token that can push.
Private/air-gapped setupNo access to the public transparency log.Key pair with --tlog-upload=false (and verify with --insecure-ignore-tlog), or a private Sigstore.
Lost cosign.key passwordKey can not be used.Make a new key pair; re-sign; distribute the new public key.
Pods blocked after enabling the policyOld, unsigned images still in use.Start in Audit mode; sign images; then Enforce.

124.5

Go further

1Why sign the digest and not the tag?

A tag can point to another image later; a digest always means exactly one image.

2What does keyless signing in CI use instead of a stored private key?

The workflow's OIDC identity — a short-lived certificate recorded in the transparency log.

Alle commando's in dit hoofdstuk