forked from PinSpace-Org/GistPin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsign-image.sh
More file actions
executable file
·84 lines (71 loc) · 2.42 KB
/
Copy pathsign-image.sh
File metadata and controls
executable file
·84 lines (71 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/env bash
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "${SCRIPT_DIR}/../.." && pwd)"
IMAGE="${1:-}"
if [[ -z "${IMAGE}" ]]; then
echo "Usage: sign-image.sh <image:tag>"
exit 1
fi
COSIGN_PASSWORD="${COSIGN_PASSWORD:-}"
KEY_FILE="${COSIGN_KEY_FILE:-${HOME}/.cosign/cosign.key}"
COSIGN_BIN="${HOME}/.local/bin/cosign"
log() { echo "[$(date +%H:%M:%S)] $*"; }
# Check dependencies
for cmd in jq curl; do
if ! command -v "${cmd}" &>/dev/null; then
log "ERROR: ${cmd} is required but not installed"
exit 1
fi
done
# Check if cosign is installed
if ! command -v cosign &>/dev/null; then
log "cosign not found, installing to ${COSIGN_BIN}..."
mkdir -p "$(dirname "${COSIGN_BIN}")"
curl -sSfL https://github.com/sigstore/cosign/releases/latest/download/cosign-linux-amd64 \
-o "${COSIGN_BIN}" 2>/dev/null || {
log "Download failed, attempting go install..."
if command -v go &>/dev/null; then
go install github.com/sigstore/cosign/v2/cmd/cosign@latest
else
log "ERROR: Cannot install cosign — install manually"
exit 1
fi
}
chmod +x "${COSIGN_BIN}" 2>/dev/null || true
fi
log "Signing image: ${IMAGE}"
# Attempt keyless signing (OIDC + Fulcio)
if [[ -n "${GITHUB_ACTIONS:-}" ]]; then
log "GitHub Actions detected — using keyless signing"
cosign sign \
--yes \
--oidc-issuer "https://token.actions.githubusercontent.com" \
--identity-token "$(curl -sH "Authorization: bearer ${ACTIONS_ID_TOKEN_REQUEST_TOKEN}" "${ACTIONS_ID_TOKEN_REQUEST_URL}&audience=sigstore" | jq -r .value)" \
"${IMAGE}"
else
# Key-based signing fallback
if [[ ! -f "${KEY_FILE}" ]]; then
log "Generating new Cosign key pair..."
COSIGN_PASSWORD="${COSIGN_PASSWORD}" cosign generate-key-pair
fi
log "Signing with key: ${KEY_FILE}"
COSIGN_PASSWORD="${COSIGN_PASSWORD}" cosign sign \
--key "${KEY_FILE}" \
--yes \
"${IMAGE}"
fi
log "Attesting SBOM..."
cosign attest \
--key "${KEY_FILE}" \
--predicate <(echo '{"builder":"gistpin-ci","build_type":"docker"}') \
--type custom \
"${IMAGE}" 2>/dev/null || log "SBOM attestation skipped (optional)"
log "Image signed successfully: ${IMAGE}"
# Verify signature immediately
log "Verifying signature..."
if ! bash "${SCRIPT_DIR}/verify-image.sh" "${IMAGE}"; then
log "ERROR: Signature verification failed immediately after signing"
exit 1
fi
log "Signing complete ✓"