@@ -99,7 +99,6 @@ mkdir -p "$SUMMARY_OF_SEVERITY_COUNTS"
99
99
echo " Started scanning images"
100
100
101
101
files=($( find " $DIRECTORY " -type f -name " *.txt" ! -name " kf_latest_all_images.txt" ) )
102
- echo " Files to scan: $files "
103
102
104
103
# Loop through each text file in the specified directory
105
104
for file in " ${files[@]} " ; do
@@ -108,7 +107,6 @@ for file in "${files[@]}"; do
108
107
109
108
# Extract the base name of the file (without the directory and extension)
110
109
file_base_name=$( basename " $file " .txt)
111
- echo " Scanning images in $file "
112
110
113
111
# Directory to save reports for this specific file
114
112
file_reports_dir=" ${SCAN_REPORTS_DIR} /${file_base_name} "
@@ -131,33 +129,29 @@ for file in "${files[@]}"; do
131
129
image_name_scan=" ${image_name_scan} _${image_tag} "
132
130
fi
133
131
134
- # Run Trivy scan and capture output
135
- trivy_output=$( trivy image --format json --output /dev/null " $line " 2>&1 )
136
-
137
- # Check if the scan was successful
138
- if [[ $? -eq 0 ]]; then
139
- trivy_output=$( trivy image --format json --output " ${file_reports_dir} /${image_name_scan} _scan.json" " $line " )
140
- # Check if results exist in the scan file (before processing)
141
- is_json_empty=$( jq -r ' .Results // false' " ${file_reports_dir} /${image_name_scan} _scan.json" )
142
-
143
- if [[ " $is_json_empty " == " false" ]]; then
144
- echo " No vulnerabilities found in $image_name :$image_tag "
145
- else
146
- # Filter results to include only elements with vulnerabilities
147
- results=$( jq -r ' .Results? | .[] | select(.Vulnerabilities) | .Vulnerabilities | length > 0' " ${file_reports_dir} /${image_name_scan} _scan.json" )
148
- if [[ " $results " == " " || " $results " == " false" ]]; then
149
- echo " The vulnerability detection may be insufficient because security updates are not provided for $image_name :$image_tag "
150
- else
151
- # Count the number of vulnerabilities by severity
152
- severity_counts=$( jq ' reduce (.Results[].Vulnerabilities? // [])[] as $v ({"LOW": 0, "MEDIUM": 0, "HIGH": 0, "CRITICAL": 0}; .[$v.Severity]+=1)' " ${file_reports_dir} /${image_name_scan} _scan.json" )
153
- report=$( jq -n --arg image " $line " --argjson counts " $severity_counts " ' {image: $image, severity_counts: $counts}' )
154
- echo " $report " > " ${severity_count} /${image_name_scan} _severity_report.json"
155
- fi
156
- fi
132
+ echo " Scanning $image_name_scan "
157
133
134
+ trivy image --format json --output " ${file_reports_dir} /${image_name_scan} _scan.json" " $line "
135
+ if [ $? -ne 0 ]; then
136
+ echo " Error scanning $image_name :$image_tag "
158
137
else
159
- # Trivy scan failed, handle the error (optional)
160
- echo " Error scanning $image_name :$image_tag : $trivy_output "
138
+ # Check if results exist in the scan file (before processing)
139
+ is_json_empty=$( jq -r ' .Results // false' " ${file_reports_dir} /${image_name_scan} _scan.json" )
140
+
141
+ if [[ " $is_json_empty " == " false" ]]; then
142
+ echo " No vulnerabilities found in $image_name :$image_tag "
143
+ else
144
+ # Filter results to include only elements with vulnerabilities
145
+ results=$( jq -r ' .Results? | .[] | select(.Vulnerabilities) | .Vulnerabilities | length > 0' " ${file_reports_dir} /${image_name_scan} _scan.json" )
146
+ if [[ " $results " == " " || " $results " == " false" ]]; then
147
+ echo " The vulnerability detection may be insufficient because security updates are not provided for $image_name :$image_tag "
148
+ else
149
+ # Count the number of vulnerabilities by severity
150
+ severity_counts=$( jq ' reduce (.Results[].Vulnerabilities? // [])[] as $v ({"LOW": 0, "MEDIUM": 0, "HIGH": 0, "CRITICAL": 0}; .[$v.Severity]+=1)' " ${file_reports_dir} /${image_name_scan} _scan.json" )
151
+ report=$( jq -n --arg image " $line " --argjson counts " $severity_counts " ' {image: $image, severity_counts: $counts}' )
152
+ echo " $report " > " ${severity_count} /${image_name_scan} _severity_report.json"
153
+ fi
154
+ fi
161
155
fi
162
156
163
157
done < " $file "
@@ -207,7 +201,7 @@ for file in "$severity_dir"/*.json; do
207
201
# Process the JSON file
208
202
data=$( jq -r ' .data[] | {LOW: .severity_counts.LOW, MEDIUM: .severity_counts.MEDIUM, HIGH: .severity_counts.HIGH, CRITICAL: .severity_counts.CRITICAL}' " $file " )
209
203
210
- # Check if data is empty (no severity counts)
204
+ # Check if data is empty
211
205
if [[ -z " $data " ]]; then
212
206
data=" {\" LOW\" : 0, \" MEDIUM\" : 0, \" HIGH\" : 0, \" CRITICAL\" : 0}"
213
207
fi
@@ -241,7 +235,6 @@ for file in "$severity_dir"/*.json; do
241
235
all_data=" {\" $filename \" : $file_data ,\" total\" : { \" images\" : $total_images , \" LOW\" : $total_low , \" MEDIUM\" : $total_medium , \" HIGH\" : $total_high , \" CRITICAL\" : $total_critical }}"
242
236
merged_data=$( jq -s ' .[0] * .[1]' <( echo " $merged_data " ) <( echo " $all_data " ) )
243
237
244
-
245
238
done
246
239
247
240
# Write the final output to a file
0 commit comments