Skip to content

Commit

Permalink
Merge pull request #1080 from stackhpc/fail-kolla-image-build-when-cr…
Browse files Browse the repository at this point in the history
…itical-cve

Fail kolla image build when critical CVEs are detected
  • Loading branch information
markgoddard authored Jul 3, 2024
2 parents 928f96c + 9c4c16e commit 0c07da3
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 15 deletions.
27 changes: 20 additions & 7 deletions .github/workflows/stackhpc-container-image-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,10 @@ on:
required: false
default: true
push-dirty:
description: Push scanned images that have vulnerabilities?
description: Push scanned images that have critical vulnerabilities?
type: boolean
required: false
# NOTE(Alex-Welsh): This default should be flipped once we resolve existing failures
default: true
default: false

env:
ANSIBLE_FORCE_COLOR: True
Expand Down Expand Up @@ -181,7 +180,7 @@ jobs:
KAYOBE_VAULT_PASSWORD: ${{ secrets.KAYOBE_VAULT_PASSWORD }}

- name: Create build logs output directory
run: mkdir image-build-logs
run: mkdir image-build-logs

- name: Build kolla overcloud images
id: build_overcloud_images
Expand Down Expand Up @@ -240,9 +239,16 @@ jobs:
run: cp image-build-logs/image-scan-output/clean-images.txt image-build-logs/push-attempt-images.txt
if: inputs.push

# NOTE(seunghun1ee): This always appends dirty images with CVEs severity lower than critical.
# This should be reverted when it's decided to filter high level CVEs as well.
- name: Append dirty images to push list
run: |
cat image-build-logs/image-scan-output/dirty-images.txt >> image-build-logs/push-attempt-images.txt
if: ${{ inputs.push }}

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

- name: Push images
Expand All @@ -254,7 +260,7 @@ jobs:
while read -r image; do
# Retries!
for i in {1..5}; do
for i in {1..5}; do
if docker push $image; then
echo "Pushed $image"
break
Expand Down Expand Up @@ -288,8 +294,15 @@ jobs:
run: if [ $(wc -l < image-build-logs/push-failed-images.txt) -gt 0 ]; then cat image-build-logs/push-failed-images.txt && exit 1; fi
if: ${{ !cancelled() }}

- name: Fail when images failed scanning
run: if [ $(wc -l < image-build-logs/dirty-images.txt) -gt 0 ]; then cat image-build-logs/dirty-images.txt && exit 1; fi
# NOTE(seunghun1ee): Currently we want to mark the job fail only when critical CVEs are detected.
# This can be used again instead of "Fail when critical vulnerabilities are found" when it's
# decided to fail the job on detecting high CVEs as well.
# - name: Fail when images failed scanning
# 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
# if: ${{ !inputs.push-dirty && !cancelled() }}

- name: Fail when critical vulnerabilities are found
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
if: ${{ !inputs.push-dirty && !cancelled() }}

# NOTE(mgoddard): Trigger another CI workflow in the
Expand Down
24 changes: 16 additions & 8 deletions tools/scan-images.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@ docker image ls --filter "reference=ark.stackhpc.com/stackhpc-dev/$1-*:$2" > $1-
images=$(grep --invert-match --no-filename ^REPOSITORY $1-scanned-container-images.txt | sed 's/ \+/:/g' | cut -f 1,2 -d:)

# Ensure output files exist
touch image-scan-output/clean-images.txt image-scan-output/dirty-images.txt
touch image-scan-output/clean-images.txt image-scan-output/dirty-images.txt image-scan-output/critical-images.txt

# If Trivy detects no vulnerabilities, add the image name to clean-images.txt.
# If there are vulnerabilities detected, add it to dirty-images.txt and
# generate a csv summary
# If the image contains at least one critical vulnerabilities, add it to
# critical-images.txt
for image in $images; do
filename=$(basename $image | sed 's/:/\./g')
if $(trivy image \
Expand All @@ -49,15 +51,13 @@ for image in $images; do
# Add the image to the clean list
echo "${image}" >> image-scan-output/clean-images.txt
else
# Add the image to the dirty list
echo "${image}" >> image-scan-output/dirty-images.txt


# Write a header for the summary CSV
echo '"PkgName","PkgPath","PkgID","VulnerabilityID","FixedVersion","PrimaryURL","Severity"' > image-scan-output/${filename}.summary.csv

# Write the summary CSV data
jq -r '.Results[]
| select(.Vulnerabilities)
jq -r '.Results[]
| select(.Vulnerabilities)
| .Vulnerabilities
# Ignore packages with "kernel" in the PkgName
| map(select(.PkgName | test("kernel") | not ))
Expand All @@ -72,8 +72,16 @@ for image in $images; do
.[0].PrimaryURL,
.[0].Severity
]
)
| .[]
)
| .[]
| @csv' image-scan-output/${filename}.json >> image-scan-output/${filename}.summary.csv

if [ $(grep "CRITICAL" image-scan-output/${filename}.summary.csv -c) -gt 0 ]; then
# If the image contains critical vulnerabilities, add the image to critical list
echo "${image}" >> image-scan-output/critical-images.txt
else
# Otherwise, add the image to the dirty list
echo "${image}" >> image-scan-output/dirty-images.txt
fi
fi
done

0 comments on commit 0c07da3

Please sign in to comment.