|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +set -e |
| 4 | + |
| 5 | +# Script to sign SHA256SUMS with GPG and Cosign |
| 6 | +# Usage: sign-checksums.sh <bin-directory> |
| 7 | +# |
| 8 | +# Environment variables: |
| 9 | +# GPG_FINGERPRINT - GPG key fingerprint for signing (required) |
| 10 | +# |
| 11 | +# Outputs: |
| 12 | +# SHA256SUMS.gpgsig - GPG detached signature |
| 13 | +# SHA256SUMS.sig - Cosign signature |
| 14 | +# SHA256SUMS.pem - Cosign certificate |
| 15 | + |
| 16 | +function main { |
| 17 | + local -r bin_dir="${1:-bin}" |
| 18 | + |
| 19 | + if [[ ! -d "$bin_dir" ]]; then |
| 20 | + echo "ERROR: Directory $bin_dir does not exist" |
| 21 | + exit 1 |
| 22 | + fi |
| 23 | + |
| 24 | + if [[ -z "${GPG_FINGERPRINT}" ]]; then |
| 25 | + echo "ERROR: GPG_FINGERPRINT environment variable is not set" |
| 26 | + exit 1 |
| 27 | + fi |
| 28 | + |
| 29 | + # Use pushd/popd to avoid side effects on caller's working directory |
| 30 | + pushd "$bin_dir" || exit 1 |
| 31 | + |
| 32 | + if [[ ! -f "SHA256SUMS" ]]; then |
| 33 | + echo "ERROR: SHA256SUMS file not found in $bin_dir" |
| 34 | + popd || exit 1 |
| 35 | + exit 1 |
| 36 | + fi |
| 37 | + |
| 38 | + # GPG signing |
| 39 | + echo "Signing SHA256SUMS with GPG..." |
| 40 | + gpg --batch --yes -u "${GPG_FINGERPRINT}" \ |
| 41 | + --output SHA256SUMS.gpgsig \ |
| 42 | + --detach-sign SHA256SUMS |
| 43 | + |
| 44 | + echo "GPG signature created: SHA256SUMS.gpgsig" |
| 45 | + |
| 46 | + # Cosign signing (keyless OIDC) |
| 47 | + echo "Signing SHA256SUMS with Cosign..." |
| 48 | + cosign sign-blob SHA256SUMS \ |
| 49 | + --oidc-issuer=https://token.actions.githubusercontent.com \ |
| 50 | + --output-certificate=SHA256SUMS.pem \ |
| 51 | + --output-signature=SHA256SUMS.sig \ |
| 52 | + --yes |
| 53 | + |
| 54 | + echo "Cosign signature created: SHA256SUMS.sig" |
| 55 | + echo "Cosign certificate created: SHA256SUMS.pem" |
| 56 | + |
| 57 | + echo "" |
| 58 | + echo "All signatures generated successfully:" |
| 59 | + ls -la SHA256SUMS* |
| 60 | + |
| 61 | + popd || exit 1 |
| 62 | +} |
| 63 | + |
| 64 | +main "$@" |
0 commit comments