@@ -19,9 +19,9 @@ function echoSection {
1919
2020unset_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
6363scan_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
7883slack_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
0 commit comments