#!/bin/sh # Build unbound-sec and (optionally) push + sign with cosign in keyless mode. # # COSIGN_SIGN=1 requires a REGISTRY_IMAGE (a registry reference, e.g. # ghcr.io/ric/unbound-sec:latest). The image is pushed first, then the exact # digest that was pushed is signed — so the signature is bound to the built # image, not to a (possibly pre-existing) registry tag. # # Verification (identity conditions live on the VERIFY side, not sign): # cosign verify --certificate-identity-regexp 'ric@.*' \ # --certificate-oidc-issuer-regexp 'accounts\.google\.com' \ # "$REGISTRY_IMAGE@sha256:" set -eu IMAGE="${IMAGE:-unbound-sec:latest}" # Version from the latest git tag (vX.Y.Z) if available, else "dev". VERSION="$(git describe --tags --always 2>/dev/null | sed 's/^v//' || echo dev)" # A unique cache-buster per run is NOT used here on purpose: reproducible # builds rely on layer caching. The test suite passes --build-arg CACHEBUST # when it needs a forced rebuild. docker build \ --pull \ -t "$IMAGE" \ -t "unbound-sec:v${VERSION}" \ . if [ "${COSIGN_SIGN:-0}" = "1" ]; then REG="${REGISTRY_IMAGE:-}" if [ -z "$REG" ]; then echo "COSIGN_SIGN=1 requires REGISTRY_IMAGE (registry reference)" >&2 exit 1 fi case "$REG" in */*) : ;; # has a registry/host component *) echo "REGISTRY_IMAGE must be a registry reference (host/repo:tag)" >&2; exit 1 ;; esac echo "==> pushing $REG" docker tag "$IMAGE" "$REG" PUSH_OUT="$(docker push "$REG" 2>&1)" printf '%s\n' "$PUSH_OUT" # Take the digest from the PUSH result itself ("digest: sha256:..."), so the # signature is bound to the registry reference that was just published. This # is more explicit than .RepoDigests[0], which with multiple entries need not # be the one just pushed. DG="$(printf '%s\n' "$PUSH_OUT" | grep -oE 'digest: sha256:[0-9a-f]+' | head -1 | awk '{print $2}')" if [ -z "$DG" ]; then echo "could not determine pushed digest from push output" >&2 exit 1 fi echo "==> cosign keyless sign $REG@${DG}" cosign sign --yes "$REG@${DG}" echo "==> done. Verify with:" echo " cosign verify --certificate-identity-regexp '@.*' \\" echo " --certificate-oidc-issuer-regexp 'accounts\\.google\\.com' \\" echo " $REG@${DG}" fi