Skip to content

Commit cabac7d

Browse files
committed
feat(ci): update Trivy setup script with upstream verification and cosign integration
1 parent 328bb76 commit cabac7d

1 file changed

Lines changed: 69 additions & 2 deletions

File tree

scripts/ci/setup-trivy.sh

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,74 @@
11
#!/bin/sh
2-
# Install Trivy .deb for CI (same package as scan / docker workflows).
2+
# Install Trivy .deb for CI (scan / docker workflows). Upstream path: Sigstore on
3+
# trivy_${VER}_checksums.txt, SHA256 of the .deb against that file, then Sigstore on the .deb.
4+
# Custom mirror: TRIVY_DEB_URL and TRIVY_DEB_SHA256 (sha256sum -c format, hex only).
35
set -eu
46

5-
curl -fsSL -o /tmp/trivy.deb https://git.quad4.io/Quad4-Software/Trivy-Assets/raw/commit/fdfe96b77d2f7b7f5a90cea00af5024c9f728f17/trivy_0.69.3_Linux-64bit.deb
7+
COSIGN_VERSION="${COSIGN_VERSION:-3.0.6}"
8+
TRIVY_VERSION="${TRIVY_VERSION:-0.69.3}"
9+
TRIVY_RELEASE_BASE="https://github.com/aquasecurity/trivy/releases/download/v${TRIVY_VERSION}"
10+
# Keyless signing identity for aquasecurity/trivy reusable release workflow (any semver tag).
11+
TRIVY_CERT_IDENTITY_RE='^https://github.com/aquasecurity/trivy/\.github/workflows/reusable-release\.yaml@refs/tags/v[0-9]+\.[0-9]+\.[0-9]+$'
12+
TRIVY_CERT_ISSUER_RE='^https://token\.actions\.githubusercontent\.com$'
13+
14+
ensure_cosign() {
15+
if command -v cosign >/dev/null 2>&1; then
16+
return 0
17+
fi
18+
sh scripts/ci/setup-cosign.sh "${COSIGN_VERSION}"
19+
}
20+
21+
verify_upstream_deb() {
22+
deb_arch="$1"
23+
DEB_BASE="trivy_${TRIVY_VERSION}_${deb_arch}.deb"
24+
25+
ensure_cosign
26+
export COSIGN_YES="${COSIGN_YES:-true}"
27+
28+
curl -fsSL -o /tmp/trivy_checksums.txt "${TRIVY_RELEASE_BASE}/trivy_${TRIVY_VERSION}_checksums.txt"
29+
curl -fsSL -o /tmp/trivy_checksums.sigstore.json "${TRIVY_RELEASE_BASE}/trivy_${TRIVY_VERSION}_checksums.txt.sigstore.json"
30+
cosign verify-blob /tmp/trivy_checksums.txt --bundle /tmp/trivy_checksums.sigstore.json \
31+
--certificate-identity-regexp="${TRIVY_CERT_IDENTITY_RE}" \
32+
--certificate-oidc-issuer-regexp="${TRIVY_CERT_ISSUER_RE}"
33+
34+
EXPECTED_SHA="$(awk -v f="${DEB_BASE}" '$2 == f { print $1; exit }' /tmp/trivy_checksums.txt)"
35+
if [ -z "${EXPECTED_SHA}" ]; then
36+
echo "setup-trivy.sh: no SHA256 line for ${DEB_BASE} in checksums.txt" >&2
37+
exit 1
38+
fi
39+
40+
curl -fsSL -o /tmp/trivy.deb "${TRIVY_RELEASE_BASE}/${DEB_BASE}"
41+
echo "${EXPECTED_SHA} /tmp/trivy.deb" | sha256sum -c
42+
43+
curl -fsSL -o /tmp/trivy.deb.sigstore.json "${TRIVY_RELEASE_BASE}/${DEB_BASE}.sigstore.json"
44+
cosign verify-blob /tmp/trivy.deb --bundle /tmp/trivy.deb.sigstore.json \
45+
--certificate-identity-regexp="${TRIVY_CERT_IDENTITY_RE}" \
46+
--certificate-oidc-issuer-regexp="${TRIVY_CERT_ISSUER_RE}"
47+
48+
rm -f /tmp/trivy_checksums.txt /tmp/trivy_checksums.sigstore.json /tmp/trivy.deb.sigstore.json
49+
}
50+
51+
if [ -n "${TRIVY_DEB_URL:-}" ]; then
52+
if [ -z "${TRIVY_DEB_SHA256:-}" ]; then
53+
echo "setup-trivy.sh: TRIVY_DEB_URL requires TRIVY_DEB_SHA256" >&2
54+
exit 1
55+
fi
56+
curl -fsSL -o /tmp/trivy.deb "${TRIVY_DEB_URL}"
57+
echo "${TRIVY_DEB_SHA256} /tmp/trivy.deb" | sha256sum -c
58+
else
59+
arch="$(uname -m)"
60+
deb_arch=
61+
case "$arch" in
62+
x86_64|amd64) deb_arch=Linux-64bit ;;
63+
aarch64|arm64) deb_arch=Linux-ARM64 ;;
64+
armv7l|armv6l|armhf) deb_arch=Linux-ARM ;;
65+
*)
66+
echo "setup-trivy.sh: unsupported uname -m: ${arch} (set TRIVY_DEB_URL)" >&2
67+
exit 1
68+
;;
69+
esac
70+
verify_upstream_deb "${deb_arch}"
71+
fi
72+
673
sh scripts/ci/exec-priv.sh dpkg -i /tmp/trivy.deb || sh scripts/ci/exec-priv.sh apt-get install -f -y
774
trivy --version

0 commit comments

Comments
 (0)