Skip to content

Commit 100825a

Browse files
CCM-13343_Trivy_Package_and_Library_Scans
1 parent d2a22cc commit 100825a

File tree

6 files changed

+250
-118
lines changed

6 files changed

+250
-118
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
name: "Trivy IaC Scan"
2+
description: "Scan Terraform IaC using Trivy"
3+
runs:
4+
using: "composite"
5+
steps:
6+
- name: "Trivy Terraform IaC Scan"
7+
shell: bash
8+
run: |
9+
components_exit_code=0
10+
modules_exit_code=0
11+
12+
./scripts/terraform/trivy-scan.sh --mode iac ./infrastructure/terraform/components || components_exit_code=$?
13+
./scripts/terraform/trivy-scan.sh --mode iac ./infrastructure/terraform/modules || modules_exit_code=$?
14+
15+
if [ $components_exit_code -ne 0 ] || [ $modules_exit_code -ne 0 ]; then
16+
echo "Trivy misconfigurations detected."
17+
exit 1
18+
fi
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
name: "Trivy Package Scan"
2+
description: "Scan project packages using Trivy"
3+
runs:
4+
using: "composite"
5+
steps:
6+
- name: "Trivy Package Scan"
7+
shell: bash
8+
run: |
9+
exit_code=0
10+
11+
./scripts/terraform/trivy-scan.sh --mode package . || exit_code=$?
12+
13+
if [ $exit_code -ne 0 ]; then
14+
echo "Trivy has detected package vulnerablilites. Please refer to https://nhsd-confluence.digital.nhs.uk/spaces/RIS/pages/1257636917/PLAT-KOP-012+-+Trivy+Pipeline+Vulnerability+Scanning+Exemption"
15+
exit 1
16+
fi

.github/actions/trivy/action.yaml

Lines changed: 0 additions & 17 deletions
This file was deleted.

.github/workflows/stage-1-commit.yaml

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -146,10 +146,12 @@ jobs:
146146
uses: actions/checkout@v4
147147
- name: "Lint Terraform"
148148
uses: ./.github/actions/lint-terraform
149-
trivy:
150-
name: "Trivy Scan"
149+
trivy-iac:
150+
name: "Trivy IaC Scan"
151+
permissions:
152+
contents: read
151153
runs-on: ubuntu-latest
152-
timeout-minutes: 5
154+
timeout-minutes: 10
153155
needs: detect-terraform-changes
154156
if: needs.detect-terraform-changes.outputs.terraform_changed == 'true'
155157
steps:
@@ -159,8 +161,23 @@ jobs:
159161
uses: asdf-vm/actions/setup@v4
160162
- name: "Perform Setup"
161163
uses: ./.github/actions/setup
162-
- name: "Trivy Scan"
163-
uses: ./.github/actions/trivy
164+
- name: "Trivy IaC Scan"
165+
uses: ./.github/actions/trivy-iac
166+
trivy-package:
167+
name: "Trivy Package Scan"
168+
permissions:
169+
contents: read
170+
runs-on: ubuntu-latest
171+
timeout-minutes: 10
172+
steps:
173+
- name: "Checkout code"
174+
uses: actions/checkout@v4
175+
- name: "Setup ASDF"
176+
uses: asdf-vm/actions/setup@v4
177+
- name: "Perform Setup"
178+
uses: ./.github/actions/setup
179+
- name: "Trivy Package Scan"
180+
uses: ./.github/actions/trivy-package
164181
count-lines-of-code:
165182
name: "Count lines of code"
166183
runs-on: ubuntu-latest

scripts/terraform/trivy-scan.sh

Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
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

scripts/terraform/trivy.sh

Lines changed: 0 additions & 96 deletions
This file was deleted.

0 commit comments

Comments
 (0)