|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
| 3 | +# verify-sbom.sh: Enforces policy that when dependency manifest files change, the SBOM file must also change. |
| 4 | +# Generic so patterns can be repurposed for other artifact freshness checks. |
| 5 | +# |
| 6 | +# Configurable variables (override via env): |
| 7 | +# MANIFEST_PATTERNS Space-delimited gitignore-style patterns to monitor. |
| 8 | +# Default: gradle/libs.versions.toml gradle.properties settings.gradle.kts **/build.gradle.kts |
| 9 | +# SBOM_FILE Path to canonical SBOM file (default sbom.cdx.json) |
| 10 | +# EXIT_CODE_MISSING Exit code when manifests changed but SBOM did not (default 10) |
| 11 | +# SKIP_SBOM_VERIFY If set to 1, skip verification (default unset) |
| 12 | +# DIFF_BASE Explicit git ref/commit to diff against (optional). If unset, merge-base with origin/main or HEAD~1 fallback. |
| 13 | +# VERBOSE If set to 1, prints expanded manifest list and full diff file list. |
| 14 | +# |
| 15 | +# Behavior: |
| 16 | +# 1. Determine diff range (patch vs mainline) by attempting merge-base with origin/main. |
| 17 | +# 2. Capture changed files including both committed changes and staged/uncommitted (by combining git diff --name-status for range and working tree). |
| 18 | +# 3. Expand manifest patterns via git ls-files. |
| 19 | +# 4. If any manifest changed but SBOM_FILE not changed, fail with guidance. |
| 20 | +# 5. If SBOM_FILE changed, pass. |
| 21 | +# 6. If no manifest changed, pass. |
| 22 | +# |
| 23 | +# Local usage: run from repo root after making changes; exit codes surface policy status. |
| 24 | + |
| 25 | +MANIFEST_PATTERNS="${MANIFEST_PATTERNS:-build.gradle.kts gradle/libs.versions.toml gradle.properties settings.gradle.kts **/build.gradle.kts}" |
| 26 | +SBOM_FILE="${SBOM_FILE:-sbom.json}" |
| 27 | +EXIT_CODE_MISSING="${EXIT_CODE_MISSING:-10}" |
| 28 | +DIFF_BASE="${DIFF_BASE:-}" # optional user-provided base ref |
| 29 | +VERBOSE="${VERBOSE:-0}" |
| 30 | + |
| 31 | +log() { printf '\n[verify-sbom] %s\n' "$*"; } |
| 32 | + |
| 33 | +if [[ "${SKIP_SBOM_VERIFY:-}" == "1" ]]; then |
| 34 | + log "Skipping verification (SKIP_SBOM_VERIFY=1)"; exit 0; fi |
| 35 | + |
| 36 | +# Determine base for diff |
| 37 | +if [[ -n "$DIFF_BASE" ]]; then |
| 38 | + base_ref="$DIFF_BASE" |
| 39 | +else |
| 40 | + git fetch origin main >/dev/null 2>&1 || true |
| 41 | + base_ref="$(git merge-base HEAD origin/main 2>/dev/null || echo '')" |
| 42 | + if [[ -z "$base_ref" ]]; then |
| 43 | + # Fallback to previous commit |
| 44 | + base_ref="HEAD~1" |
| 45 | + fi |
| 46 | +fi |
| 47 | + |
| 48 | +range="$base_ref..HEAD" |
| 49 | +log "Using diff range: $range" |
| 50 | + |
| 51 | +# Committed diff |
| 52 | +committed_status="$(git diff --name-status $range || true)" |
| 53 | +# Working tree (staged + unstaged) diff vs HEAD |
| 54 | +wt_status="$(git diff --name-status HEAD || true)" |
| 55 | +# Combine and extract filenames (second column); preserve uniqueness |
| 56 | +changed_files=$(printf "%s\n%s\n" "$committed_status" "$wt_status" | awk '{print $2}' | grep -v '^$' | sort -u) |
| 57 | + |
| 58 | +if [[ $VERBOSE == 1 ]]; then |
| 59 | + log "Changed files:"; echo "$changed_files" |
| 60 | +fi |
| 61 | + |
| 62 | +# Expand manifest patterns to tracked files |
| 63 | +expanded_manifests="" |
| 64 | +for pattern in $MANIFEST_PATTERNS; do |
| 65 | + # git ls-files supports pathspec; '**' requires extglob-like; rely on grep fallback |
| 66 | + matches=$(git ls-files "$pattern" 2>/dev/null || true) |
| 67 | + if [[ -z "$matches" && "$pattern" == *"**"* ]]; then |
| 68 | + # Manual glob expansion for recursive pattern |
| 69 | + matches=$(git ls-files | grep -E "$(echo "$pattern" | sed 's/**/.*'/ | sed 's/\./\\./g')" || true) |
| 70 | + fi |
| 71 | + expanded_manifests+="$matches\n" |
| 72 | +done |
| 73 | +expanded_manifests=$(echo -e "$expanded_manifests" | grep -v '^$' | sort -u) |
| 74 | + |
| 75 | +if [[ $VERBOSE == 1 ]]; then |
| 76 | + log "Expanded manifests:"; echo "$expanded_manifests" |
| 77 | +fi |
| 78 | + |
| 79 | +manifest_hit=0 |
| 80 | +manifest_changed_list=() |
| 81 | +while IFS= read -r mf; do |
| 82 | + if echo "$changed_files" | grep -Fxq "$mf"; then |
| 83 | + manifest_hit=1 |
| 84 | + manifest_changed_list+=("$mf") |
| 85 | + fi |
| 86 | +done <<< "$expanded_manifests" |
| 87 | + |
| 88 | +if [[ $manifest_hit -eq 0 ]]; then |
| 89 | + log "No manifest changes detected; passing." |
| 90 | + exit 0 |
| 91 | +fi |
| 92 | + |
| 93 | +if echo "$changed_files" | grep -Fxq "$SBOM_FILE"; then |
| 94 | + log "SBOM file '$SBOM_FILE' updated alongside manifest changes; pass." |
| 95 | + exit 0 |
| 96 | +fi |
| 97 | + |
| 98 | +log "FAILURE: Manifest files changed but SBOM '$SBOM_FILE' was not modified." >&2 |
| 99 | +log "Changed manifest(s):" >&2 |
| 100 | +for mf in "${manifest_changed_list[@]}"; do echo " - $mf" >&2; done |
| 101 | +log "Regenerate SBOM locally (e.g., bash .evergreen/generate-sbom.sh) and commit '$SBOM_FILE'." >&2 |
| 102 | +exit "$EXIT_CODE_MISSING" |
0 commit comments