#!/usr/bin/env bash set -euo pipefail mode=${1:-all} case "$mode" in dependencies|server|agent|all) ;; *) echo 'Usage: security.sh [dependencies|server|agent|all]' >&2; exit 2 ;; esac mkdir -p artifacts/security status=0 if [[ "$mode" == dependencies || "$mode" == all ]]; then echo "Checking restored NuGet dependencies for advisories" dotnet restore Heimdall.slnx --locked-mode dotnet list Heimdall.slnx package --no-restore --vulnerable --include-transitive --format json > artifacts/security/nuget.json || { echo "NuGet advisory query failed; see artifacts/security/nuget.json" >&2 exit 1 } python3 scripts/ci/check-advisories.py artifacts/security/nuget.json || status=1 fi [[ "$mode" == dependencies ]] && exit "$status" sha=$(git rev-parse HEAD) scanner=aquasec/trivy:0.69.3@sha256:bcc376de8d77cfe086a917230e818dc9f8528e3c852f7b1aff648949b6258d1c cache_volume="heimdall-trivy-cache-$sha" echo "Creating Trivy cache volume" docker volume create "$cache_volume" >/dev/null || { echo "Could not create Trivy cache volume" >&2; exit 1; } trap 'docker volume rm "$cache_volume" >/dev/null 2>&1 || true' EXIT scan_image() ( set -euo pipefail component=$1 archive=$(mktemp) scanner_container="" trap 'rm -f "$archive"; if [[ -n "$scanner_container" ]]; then docker rm -f "$scanner_container" >/dev/null 2>&1 || true; fi' EXIT echo "Exporting heimdall-$component:ci-$sha for Trivy" docker save "heimdall-$component:ci-$sha" -o "$archive" || { echo "Could not export heimdall-$component:ci-$sha" >&2; exit 1; } report="artifacts/security/$component.json" # Trivy reopens archive entries: stdin cannot serve as a seekable image tar. # Docker cp streams from the job container, without daemon-host bind paths. # Share the database between scans and retry an infrastructure failure once; # a completed report with findings still fails the gate immediately. for attempt in 1 2; do scanner_container=$(docker create --mount "source=$cache_volume,target=/root/.cache/trivy" \ "$scanner" image \ --db-repository ghcr.io/aquasecurity/trivy-db:2 \ --db-repository mirror.gcr.io/aquasec/trivy-db:2 \ --input /tmp/image.tar --scanners vuln \ --severity HIGH,CRITICAL --exit-code 1 --format json) || { echo "Could not create Trivy scanner for $component" >&2; exit 1; } docker cp "$archive" "$scanner_container:/tmp/image.tar" >/dev/null || { echo "Could not copy image archive to Trivy scanner for $component" >&2; exit 1; } cli_status=0 docker start --attach "$scanner_container" > "$report" || cli_status=$? state=$(docker inspect "$scanner_container" --format '{{.State.Status}} {{.State.ExitCode}}') || exit 1 if [[ -s "$report" ]]; then [[ "$cli_status" == 0 && "$state" == "exited 0" ]] || echo "Trivy scan failed for $component (exit=$cli_status, state=$state); report retained at $report" >&2 [[ "$cli_status" == 0 && "$state" == "exited 0" ]] exit $? fi docker rm -f "$scanner_container" >/dev/null 2>&1 || true scanner_container="" if [[ "$attempt" == 1 ]]; then echo "Trivy infrastructure failure for $component; retrying once" >&2 sleep 2 fi done echo "Trivy returned no report for $component after retry" >&2 exit 1 ) case "$mode" in all) components=(server agent) ;; *) components=("$mode") ;; esac for component in "${components[@]}"; do if ! docker image inspect "heimdall-$component:ci-$sha" >/dev/null 2>&1; then echo "Missing image for security scan: $component" >&2 status=1 continue fi scan_image "$component" || status=1 done exit "$status"