|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# WARNING: Please DO NOT edit this file! It is maintained in the Repository Template (https://github.com/NHSDigital/nhs-notify-repository-template). Raise a PR instead. |
| 4 | + |
| 5 | +set -euo pipefail |
| 6 | + |
| 7 | +function usage() { |
| 8 | + cat <<'EOF' |
| 9 | +Usage: ./scripts/terraform/trivy-scan.sh --mode <iac|package> [directory] |
| 10 | +
|
| 11 | +Options: |
| 12 | + --mode, -m Scan type to run. Accepts "iac" or "package" (required). |
| 13 | + --help, -h Show this message. |
| 14 | + [directory] Directory to scan. Defaults to the repository root. |
| 15 | +
|
| 16 | +Environment variables: |
| 17 | + FORCE_USE_DOCKER=true Force execution through Docker even if Trivy is installed locally. |
| 18 | + VERBOSE=true Enable bash -x tracing. |
| 19 | +EOF |
| 20 | +} |
| 21 | + |
| 22 | +function main() { |
| 23 | + cd "$(git rev-parse --show-toplevel)" |
| 24 | + |
| 25 | + local scan_mode="" |
| 26 | + local dir_to_scan="." |
| 27 | + |
| 28 | + while [[ $# -gt 0 ]]; do |
| 29 | + case "$1" in |
| 30 | + --mode|-m) |
| 31 | + if [[ $# -lt 2 ]]; then |
| 32 | + echo "Error: --mode requires an argument." >&2 |
| 33 | + usage |
| 34 | + exit 1 |
| 35 | + fi |
| 36 | + scan_mode="$2" |
| 37 | + shift 2 |
| 38 | + ;; |
| 39 | + --help|-h) |
| 40 | + usage |
| 41 | + exit 0 |
| 42 | + ;; |
| 43 | + --) |
| 44 | + shift |
| 45 | + break |
| 46 | + ;; |
| 47 | + -*) |
| 48 | + echo "Unknown option: $1" >&2 |
| 49 | + usage |
| 50 | + exit 1 |
| 51 | + ;; |
| 52 | + *) |
| 53 | + dir_to_scan="$1" |
| 54 | + shift |
| 55 | + ;; |
| 56 | + esac |
| 57 | + done |
| 58 | + |
| 59 | + if [[ $# -gt 0 ]]; then |
| 60 | + dir_to_scan="$1" |
| 61 | + fi |
| 62 | + |
| 63 | + if [[ -z "$scan_mode" ]]; then |
| 64 | + echo "Error: --mode must be provided (iac|package)." >&2 |
| 65 | + usage |
| 66 | + exit 1 |
| 67 | + fi |
| 68 | + |
| 69 | + case "$scan_mode" in |
| 70 | + iac|package) |
| 71 | + ;; |
| 72 | + *) |
| 73 | + echo "Error: unknown mode '$scan_mode'. Expected 'iac' or 'package'." >&2 |
| 74 | + usage |
| 75 | + exit 1 |
| 76 | + ;; |
| 77 | + esac |
| 78 | + |
| 79 | + if command -v trivy > /dev/null 2>&1 && ! is-arg-true "${FORCE_USE_DOCKER:-false}"; then |
| 80 | + run-trivy-natively "$scan_mode" "$dir_to_scan" |
| 81 | + else |
| 82 | + run-trivy-in-docker "$scan_mode" "$dir_to_scan" |
| 83 | + fi |
| 84 | +} |
| 85 | + |
| 86 | +function run-trivy-natively() { |
| 87 | + local scan_mode="$1" |
| 88 | + local dir_to_scan="$2" |
| 89 | + |
| 90 | + echo "Trivy found locally, running natively" |
| 91 | + echo "Running Trivy ($scan_mode) on directory: $dir_to_scan" |
| 92 | + |
| 93 | + if execute-trivy-command "$scan_mode" "$dir_to_scan"; then |
| 94 | + check-trivy-status 0 |
| 95 | + else |
| 96 | + local status=$? |
| 97 | + check-trivy-status "$status" |
| 98 | + fi |
| 99 | +} |
| 100 | + |
| 101 | +function run-trivy-in-docker() { |
| 102 | + # shellcheck disable=SC1091 |
| 103 | + source ./scripts/docker/docker.lib.sh |
| 104 | + |
| 105 | + local scan_mode="$1" |
| 106 | + local dir_to_scan="$2" |
| 107 | + |
| 108 | + # shellcheck disable=SC2155 |
| 109 | + local image=$(name=aquasec/trivy docker-get-image-version-and-pull) |
| 110 | + |
| 111 | + echo "Trivy not found locally, running in Docker Container" |
| 112 | + echo "Running Trivy ($scan_mode) on directory: $dir_to_scan" |
| 113 | + |
| 114 | + if execute-trivy-in-docker "$image" "$scan_mode" "$dir_to_scan"; then |
| 115 | + check-trivy-status 0 |
| 116 | + else |
| 117 | + local status=$? |
| 118 | + check-trivy-status "$status" |
| 119 | + fi |
| 120 | +} |
| 121 | + |
| 122 | +function execute-trivy-command() { |
| 123 | + local scan_mode="$1" |
| 124 | + local dir_to_scan="$2" |
| 125 | + |
| 126 | + if [[ "$scan_mode" == "iac" ]]; then |
| 127 | + trivy config \ |
| 128 | + --config scripts/config/trivy.yaml \ |
| 129 | + --tf-exclude-downloaded-modules \ |
| 130 | + "$dir_to_scan" |
| 131 | + else |
| 132 | + trivy \ |
| 133 | + --config scripts/config/trivy.yaml \ |
| 134 | + fs "$dir_to_scan" \ |
| 135 | + --scanners vuln \ |
| 136 | + --severity HIGH,CRITICAL \ |
| 137 | + --include-dev-deps |
| 138 | + fi |
| 139 | +} |
| 140 | + |
| 141 | +function execute-trivy-in-docker() { |
| 142 | + local image="$1" |
| 143 | + local scan_mode="$2" |
| 144 | + local dir_to_scan="$3" |
| 145 | + |
| 146 | + if [[ "$scan_mode" == "iac" ]]; then |
| 147 | + docker run --rm --platform linux/amd64 \ |
| 148 | + --volume "$PWD":/workdir \ |
| 149 | + --workdir /workdir \ |
| 150 | + "$image" \ |
| 151 | + config \ |
| 152 | + --config scripts/config/trivy.yaml \ |
| 153 | + --tf-exclude-downloaded-modules \ |
| 154 | + "$dir_to_scan" |
| 155 | + else |
| 156 | + docker run --rm --platform linux/amd64 \ |
| 157 | + --volume "$PWD":/workdir \ |
| 158 | + --workdir /workdir \ |
| 159 | + "$image" \ |
| 160 | + --config scripts/config/trivy.yaml \ |
| 161 | + fs "$dir_to_scan" \ |
| 162 | + --scanners vuln \ |
| 163 | + --severity HIGH,CRITICAL \ |
| 164 | + --include-dev-deps |
| 165 | + fi |
| 166 | +} |
| 167 | + |
| 168 | +function check-trivy-status() { |
| 169 | + local status="$1" |
| 170 | + |
| 171 | + if [[ "$status" -eq 0 ]]; then |
| 172 | + echo "Trivy completed successfully." |
| 173 | + return 0 |
| 174 | + fi |
| 175 | + |
| 176 | + echo "Trivy found issues." |
| 177 | + exit "$status" |
| 178 | +} |
| 179 | + |
| 180 | +function is-arg-true() { |
| 181 | + if [[ "$1" =~ ^(true|yes|y|on|1|TRUE|YES|Y|ON)$ ]]; then |
| 182 | + return 0 |
| 183 | + else |
| 184 | + return 1 |
| 185 | + fi |
| 186 | +} |
| 187 | + |
| 188 | +# ============================================================================== |
| 189 | + |
| 190 | +is-arg-true "${VERBOSE:-false}" && set -x |
| 191 | + |
| 192 | +main "$@" |
| 193 | + |
| 194 | +exit 0 |
0 commit comments