Skip to content

Commit 0eb14ee

Browse files
PR: Trivy Scanner v0.0.4 - Upgraded to LTS 0.41.0 and Fixed Null Iteration and Skip Empty Report Bugs (#579)
1 parent f6d3a47 commit 0eb14ee

File tree

3 files changed

+22
-15
lines changed

3 files changed

+22
-15
lines changed

incubating/trivy/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM aquasec/trivy:latest
1+
FROM aquasec/trivy:0.41.0
22

33
ADD entrypoint.sh /usr/local/bin
44

incubating/trivy/entrypoint.sh

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ function echoSection {
1919

2020
unset_empty_vars() {
2121
echoSection "Unsetting empty vars"
22-
for var in $(env); do
23-
if [[ "${var##*=}" == "\${{${var%=*}}}" ]]; then
24-
echo "Unsetting ${var%=*}";
22+
for var in $(env); do
23+
if [[ "${var##*=}" == "\${{${var%=*}}}" ]]; then
24+
echo "Unsetting ${var%=*}";
2525
unset ${var%=*};
2626
fi;
2727
done
@@ -34,8 +34,8 @@ set_trivy_ignore() {
3434
if [[ ! -z $TRIVY_IGNORE_FILE ]]; then
3535
stat -c "%n" "$TRIVY_IGNORE_FILE"
3636
cp $TRIVY_IGNORE_FILE $TRIVY_IGNOREFILE
37-
fi
38-
local IFS=$','
37+
fi
38+
local IFS=$','
3939
for cve in $TRIVY_IGNORE_LIST; do
4040
echo $cve >> $TRIVY_IGNOREFILE
4141
done
@@ -62,24 +62,31 @@ generate_images_list() {
6262

6363
scan_template() {
6464
local image=$1
65-
local object=$(trivy -q -f json --cache-dir ${CACHE_DIR} --ignorefile ${TRIVY_IGNOREFILE} ${image} | sed 's|null|\[\]|')
66-
count=$( echo $object | jq length)
65+
local object=$(trivy image -q -f json --cache-dir ${CACHE_DIR} --ignorefile ${TRIVY_IGNOREFILE} ${image} | sed 's|null|\[\]|')
66+
count=$( echo $object | jq '.Results | length')
6767
for ((i = 0 ; i < $count ; i++)); do
68-
local vuln_length=$(echo $object | jq -r --arg index "${i}" '.[($index|tonumber)].Vulnerabilities | length')
68+
local vuln_length=$(echo $object | jq -r --arg index "${i}" '.Results[($index|tonumber)].Vulnerabilities // [] | length')
6969
if [[ "$vuln_length" -eq "0" ]] && [[ "$SKIP_EMPTY" == "true" ]]; then
7070
continue
7171
fi
72-
echo -E "\n"Target: $(echo $object | jq -r --arg index "${i}" '.[($index|tonumber)].Target')
72+
echo -E "\n"Target: $(echo $object | jq -r --arg index "${i}" '.Results[($index|tonumber)].Target')
7373
echo "..."
74-
echo $object | jq -r --arg index "${i}" '.[($index|tonumber)].Vulnerabilities[] | "\(.PkgName) \(.VulnerabilityID) \(.Severity)"' | column -t | sort -k3
74+
if [[ "$vuln_length" -eq "0" ]]; then
75+
# Return a non-empty default value
76+
echo "No vulnerabilities found."
77+
continue
78+
fi
79+
echo $object | jq -r --arg index "${i}" '.Results[($index|tonumber)].Vulnerabilities // [] | .[] | "\(.PkgName) \(.VulnerabilityID) \(.Severity)"' | column -t | sort -k3
7580
done
7681
}
7782

7883
slack_image_section() {
7984
local image=$1
8085
local header="*${image}*"
8186
local body=$(scan_template $image | awk '{print}' ORS='\\n')
82-
if [[ -z $body ]]; then return; fi
87+
if [[ -z $body ]]; then
88+
return
89+
fi
8390
echo -E "{
8491
\"type\": \"section\",
8592
\"text\": {
@@ -102,7 +109,7 @@ main() {
102109
fi
103110

104111
echoSection "Update trivy DB"
105-
trivy --download-db-only --cache-dir ${CACHE_DIR}
112+
trivy image --download-db-only --cache-dir ${CACHE_DIR}
106113

107114
SLACK_REPORT_MESSAGE='{"blocks":[]}'
108115

incubating/trivy/step.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ kind: step-type
22
version: '1.0'
33
metadata:
44
name: trivy-scan
5-
version: 0.0.3
5+
version: 0.0.4
66
isPublic: true
77
description: The step scans the list of docker images and sends the report to a Slack webhook URL.
88
sources:
@@ -87,7 +87,7 @@ spec:
8787
steps:
8888
main:
8989
name: scan
90-
image: quay.io/codefreshplugins/trivy-scan:latest
90+
image: quay.io/codefreshplugins/trivy-scan:0.0.4
9191
environment:
9292
- 'GITHUB_TOKEN=${{GITHUB_TOKEN}}'
9393
- 'IMAGES_LIST=${{IMAGES_LIST}}'

0 commit comments

Comments
 (0)