#!/usr/bin/env bash # Prints the path of a pinned, checksum-verified tool binary, downloading it once into # ${TOOL_CACHE:-~/.cache/messaging-library}/-/. # # scripts/tool.sh trivy # # A tool is a native binary, not a Maven artifact, so Gradle cannot resolve it; this is the one place # that says which version and which bytes. A download whose SHA-256 differs from the one written here # fails and leaves nothing behind. Exit codes: 0 = path printed, 1 = download or checksum failed, # 2 = usage or unsupported machine. set -euo pipefail tool=${1:-} case "$(uname -s)-$(uname -m)" in Linux-x86_64) arch=amd64 ;; Linux-aarch64 | Linux-arm64) arch=arm64 ;; *) echo "tool.sh: unsupported machine $(uname -s)-$(uname -m)" >&2; exit 2 ;; esac case "$tool" in trivy) # Trivy 0.71.2, the version and checksums the backend template's dependency-vulnerability.yml pins. version=0.71.2 [ "$arch" = amd64 ] && asset="trivy_${version}_Linux-64bit.tar.gz" || asset="trivy_${version}_Linux-ARM64.tar.gz" [ "$arch" = amd64 ] \ && sha256=0510e71e2fd39bf863856d499c8dc19feb4e7336546394c502a8f5cc7ab27460 \ || sha256=fe1c7106e15a5365d485b098a8c338f91e3b7ba71cb0e4963b98a3a098763cfc url="${TRIVY_DOWNLOAD_BASE_URL:-https://github.com/aquasecurity/trivy/releases/download/v${version}}/${asset}" archive=1 ;; *) echo "usage: scripts/tool.sh trivy" >&2; exit 2 ;; esac dir="${TOOL_CACHE:-$HOME/.cache/messaging-library}/${tool}-${version}" binary="$dir/$tool" if [ ! -x "$binary" ]; then mkdir -p "$dir" tmp=$(mktemp "$dir/.download.XXXXXX") trap 'rm -f "$tmp"' EXIT curl --fail --show-error --silent --location --retry 3 --proto '=https' --tlsv1.2 "$url" --output "$tmp" if ! printf '%s %s\n' "$sha256" "$tmp" | sha256sum -c - >/dev/null 2>&1; then echo "tool.sh: $asset does not match the pinned SHA-256 $sha256" >&2 exit 1 fi if [ "$archive" = 1 ]; then tar -xzf "$tmp" -C "$dir" "$tool" else mv "$tmp" "$binary" fi chmod 0755 "$binary" fi printf '%s\n' "$binary"