Skip to content

Commit 6356dd3

Browse files
committed
CI: Minor image scanning tweaks
1 parent 2b56f2a commit 6356dd3

File tree

2 files changed

+38
-25
lines changed

2 files changed

+38
-25
lines changed

.github/workflows/stackhpc-container-image-build.yml

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,12 @@ on:
3333
type: boolean
3434
required: false
3535
default: true
36-
push-dirty:
36+
sbom:
37+
description: Generate SBOM?
38+
type: boolean
39+
required: false
40+
default: true
41+
push-critical:
3742
description: Push scanned images that have critical vulnerabilities?
3843
type: boolean
3944
required: false
@@ -254,14 +259,14 @@ jobs:
254259
run: if [ $(wc -l < ${{ matrix.distro.name }}-${{ matrix.distro.release }}-container-images) -le 1 ]; then exit 1; fi
255260

256261
- name: Scan built container images
257-
run: src/kayobe-config/tools/scan-images.sh ${{ matrix.distro.name }}-${{ matrix.distro.release }} ${{ steps.write-kolla-tag.outputs.kolla-tag }}
262+
run: src/kayobe-config/tools/scan-images.sh ${{ matrix.distro.name }}-${{ matrix.distro.release }} ${{ steps.write-kolla-tag.outputs.kolla-tag }} ${{ inputs.sbom && '--sbom' }}
258263

259264
- name: Move image scan logs to output artifact
260265
run: mv image-scan-output image-build-logs/image-scan-output
261266

262-
- name: Fail if no images have passed scanning
267+
- name: Fail if any images have critical vulnerabilities
263268
run: if [ $(wc -l < image-build-logs/image-scan-output/critical-images.txt) -gt 0 ]; then exit 1; fi
264-
if: ${{ !inputs.push-dirty }}
269+
if: ${{ !inputs.push-critical }}
265270

266271
- name: Copy clean images to push-attempt-images list
267272
run: cp image-build-logs/image-scan-output/clean-images.txt image-build-logs/push-attempt-images.txt
@@ -271,13 +276,13 @@ jobs:
271276
# This should be reverted when it's decided to filter high level CVEs as well.
272277
- name: Append dirty images to push list
273278
run: |
274-
cat image-build-logs/image-scan-output/dirty-images.txt >> image-build-logs/push-attempt-images.txt
279+
cat image-build-logs/image-scan-output/high-images.txt >> image-build-logs/push-attempt-images.txt
275280
if: ${{ inputs.push }}
276281

277282
- name: Append images with critical vulnerabilities to push list
278283
run: |
279284
cat image-build-logs/image-scan-output/critical-images.txt >> image-build-logs/push-attempt-images.txt
280-
if: ${{ inputs.push && inputs.push-dirty }}
285+
if: ${{ inputs.push && inputs.push-critical }}
281286

282287
- name: Push images
283288
run: |
@@ -326,12 +331,12 @@ jobs:
326331
# This can be used again instead of "Fail when critical vulnerabilities are found" when it's
327332
# decided to fail the job on detecting high CVEs as well.
328333
# - name: Fail when images failed scanning
329-
# run: if [ $(wc -l < image-build-logs/image-scan-output/dirty-images.txt) -gt 0 ]; then cat image-build-logs/image-scan-output/dirty-images.txt && exit 1; fi
330-
# if: ${{ !inputs.push-dirty && !cancelled() }}
334+
# run: if [ $(wc -l < image-build-logs/image-scan-output/high-images.txt) -gt 0 ]; then cat image-build-logs/image-scan-output/high-images.txt && exit 1; fi
335+
# if: ${{ !inputs.push-critical && !cancelled() }}
331336

332337
- name: Fail when critical vulnerabilities are found
333338
run: if [ $(wc -l < image-build-logs/image-scan-output/critical-images.txt) -gt 0 ]; then cat image-build-logs/image-scan-output/critical-images.txt && exit 1; fi
334-
if: ${{ !inputs.push-dirty && !cancelled() }}
339+
if: ${{ !inputs.push-critical && !cancelled() }}
335340

336341
- name: Remove locally built images for this run
337342
if: always() && runner.arch == 'ARM64'

tools/scan-images.sh

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ check_deps_installed() {
3535
file_prep() {
3636
rm -rf image-scan-output
3737
mkdir -p image-scan-output
38-
touch image-scan-output/clean-images.txt image-scan-output/dirty-images.txt image-scan-output/critical-images.txt
38+
touch image-scan-output/clean-images.txt image-scan-output/high-images.txt image-scan-output/critical-images.txt
3939
}
4040

4141
# Gather image lists
@@ -99,7 +99,21 @@ categorise_image() {
9999
fi
100100
}
101101

102-
# Scan images, generate SBOMs if requested
102+
# Generate SBOM, return correct scan command for SBOM
103+
generate_sbom() {
104+
local imagename=$1
105+
local filename=$2
106+
local image=$3
107+
trivy image \
108+
--format spdx-json \
109+
--output image-scan-output/${imagename}/${filename}-sbom.json \
110+
$image > /dev/null 2>&1
111+
echo "trivy sbom $scan_common_args \
112+
--output image-scan-output/${imagename}/${filename}-scan.json \
113+
image-scan-output/${imagename}/${filename}-sbom.json"
114+
}
115+
116+
# Scan images, generate SBOMs if requested
103117
scan_image() {
104118
local image=$1
105119
local filename=$(basename $image | sed 's/:/\./g')
@@ -108,25 +122,19 @@ scan_image() {
108122
mkdir -p image-scan-output/$imagename
109123
generate_trivy_ignore $imagename
110124

111-
echo "Scanning $imagename"
112-
113-
# If SBOM is required, generate that first, then generate scan results from it
125+
# If SBOM is required, generate it first and scan the results, otherwise we
126+
# scan the image directly.
114127
if $generate_sbom; then
115-
trivy image \
116-
--format spdx-json \
117-
--output image-scan-output/${imagename}/${filename}-sbom.json \
118-
$image
119-
scan_command="trivy sbom $scan_common_args \
120-
--output image-scan-output/${imagename}/${filename}-scan.json \
121-
image-scan-output/${imagename}/${filename}-sbom.json"
128+
echo "Generating SBOM for $imagename"
129+
scan_command=$(generate_sbom $imagename $filename $image)
122130
else
123131
scan_command="trivy image $scan_common_args \
124132
--output image-scan-output/${imagename}/${filename}-scan.json $image"
125133
fi
126-
echo "scan command"
127-
echo "$scan_command"
128-
# Run scan, against image or SBOM. If no results, delete files.
129-
if $scan_command; then
134+
135+
# Run scan against image or SBOM, format output. If no results, delete files.
136+
echo "Scanning $imagename for vulnerabilities"
137+
if $scan_command > /dev/null 2>&1; then
130138
rm -f image-scan-output/${imagename}/${filename}-scan.json
131139
echo "${image}" >> image-scan-output/clean-images.txt
132140
else

0 commit comments

Comments
 (0)