|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Default scanner is 'grype' |
| 4 | +scanner="grype" |
| 5 | + |
| 6 | +# Default scanner is 'grype' |
| 7 | +scanner="grype" |
| 8 | + |
| 9 | +# Check if --scanner option is provided |
| 10 | +if [[ "$1" == "--scanner=trivy" ]]; then |
| 11 | + scanner="trivy" |
| 12 | +elif [[ "$1" == "--scanner=grype" ]]; then |
| 13 | + scanner="grype" |
| 14 | +elif [[ -n "$1" ]]; then |
| 15 | + echo "Unknown option: $1" |
| 16 | + exit 1 |
| 17 | +fi |
| 18 | + |
| 19 | +images=( |
| 20 | + "openjdk:21-jdk" |
| 21 | + "openjdk:17-jdk" |
| 22 | + "openjdk:11-jdk" |
| 23 | + "openjdk:8-jdk" |
| 24 | +) |
| 25 | + |
| 26 | +echo "" |
| 27 | +echo "Image Size On Disk:" |
| 28 | + |
| 29 | +# Loop through each item and append ":latest" if no tag is present |
| 30 | +for i in "${!images[@]}"; do |
| 31 | + if [[ "${images[i]}" != *:* ]]; then |
| 32 | + images[i]="${images[i]}:latest" |
| 33 | + fi |
| 34 | + |
| 35 | + origimagestr="${images[i]}" |
| 36 | + |
| 37 | + # Pull the image and check for errors |
| 38 | + if docker pull "${images[i]}" 2>&1 | grep -iq "error"; then |
| 39 | + echo "Error encountered while pulling ${images[i]}. Exiting..." |
| 40 | + exit 1 |
| 41 | + fi |
| 42 | + |
| 43 | + images[i]=$(docker inspect "${images[i]}" | jq -r '.[0].RepoDigests[0]') |
| 44 | + size=$(docker inspect "${images[i]}" | jq -r '.[0].Size // 0') |
| 45 | + size_mb=$(echo "scale=2; $size / 1024 / 1024" | bc) |
| 46 | + |
| 47 | + echo "$origimagestr: $size_mb MB" |
| 48 | + |
| 49 | +done |
| 50 | + |
| 51 | +echo "---------------------------------------------" |
| 52 | + |
| 53 | +json='{"items":[]}' |
| 54 | +totalCritical=0 |
| 55 | +totalHigh=0 |
| 56 | +totalMedium=0 |
| 57 | +totalLow=0 |
| 58 | +totalWontFix=0 |
| 59 | +totalCount=0 |
| 60 | + |
| 61 | +echo "Scanning images..." |
| 62 | +for image in "${!image_digests[@]}"; do |
| 63 | + echo "Image: $image, Digest: ${image_digests[$image]}, " |
| 64 | +done |
| 65 | + |
| 66 | +for IMAGE in "${images[@]}"; do |
| 67 | + |
| 68 | + if [[ "$scanner" == "grype" ]]; then |
| 69 | + # Grype |
| 70 | + output=$(grype $IMAGE -o json 2>/dev/null | jq -c '{Total: [.matches[].vulnerability] | length, Critical: [.matches[] | select(.vulnerability.severity == "Critical")] | length, High: [.matches[] | select(.vulnerability.severity == "High")] | length, Medium: [.matches[] | select(.vulnerability.severity == "Medium")] | length, Low: [.matches[] | select(.vulnerability.severity == "Low")] | length, WontFix: [.matches[] | select(.vulnerability.fix.state == "wont-fix")] | length }') |
| 71 | + critical=$(jq '.Critical' <<< "$output") |
| 72 | + high=$(jq '.High' <<< "$output") |
| 73 | + medium=$(jq '.Medium' <<< "$output") |
| 74 | + low=$(jq '.Low' <<< "$output") |
| 75 | + wontfix=$(jq '.WontFix' <<< "$output") |
| 76 | + total=$(jq '.Total' <<< "$output") |
| 77 | + |
| 78 | + elif [[ "$scanner" == "trivy" ]]; then |
| 79 | + # Trivy |
| 80 | + output=$(trivy image -f json "$IMAGE" 2>/dev/null | jq -c 'if (.Results | length) == 0 then { Total: 0, Critical: 0, High: 0, Medium: 0, Low: 0, WontFix: 0 } else [.Results[] | select(has("Vulnerabilities")) | .Vulnerabilities[]] | { Total: length, Critical: (map(select(.Severity == "CRITICAL")) | length), High: (map(select(.Severity == "HIGH")) | length), Medium: (map(select(.Severity == "MEDIUM")) | length), Low: (map(select(.Severity == "LOW")) | length), WontFix: (map(select(.Status == "will_not_fix")) | length)} end') |
| 81 | + critical=$(jq '.Critical' <<< "$output") |
| 82 | + high=$(jq '.High' <<< "$output") |
| 83 | + medium=$(jq '.Medium' <<< "$output") |
| 84 | + low=$(jq '.Low' <<< "$output") |
| 85 | + wontfix=$(jq '.WontFix' <<< "$output") |
| 86 | + total=$(jq '.Total' <<< "$output") |
| 87 | + fi |
| 88 | + |
| 89 | + echo "$output" |
| 90 | + json=$(jq --arg image "$IMAGE" \ |
| 91 | + --arg critical "$critical" \ |
| 92 | + --arg high "$high" \ |
| 93 | + --arg medium "$medium" \ |
| 94 | + --arg low "$low" \ |
| 95 | + --arg wontfix "$wontfix" \ |
| 96 | + --arg total "$total" \ |
| 97 | + '.items += [{ |
| 98 | + image: $image, |
| 99 | + scan: { |
| 100 | + type: "grype", |
| 101 | + critical: ($critical | tonumber), |
| 102 | + high: ($high | tonumber), |
| 103 | + medium: ($medium | tonumber), |
| 104 | + low: ($low | tonumber), |
| 105 | + wontfix: ($wontfix | tonumber), |
| 106 | + total: ($total | tonumber) |
| 107 | + } |
| 108 | + }]' <<< "$json") |
| 109 | + |
| 110 | + |
| 111 | + totalCritical=$((totalCritical + critical)) |
| 112 | + totalHigh=$((totalHigh + high)) |
| 113 | + totalMedium=$((totalMedium + medium)) |
| 114 | + totalLow=$((totalLow + low)) |
| 115 | + totalWontFix=$((totalWontFix + wontfix)) |
| 116 | + totalCount=$((totalCount + total)) |
| 117 | + |
| 118 | +done |
| 119 | +echo "---------------------------------------------" |
| 120 | + |
| 121 | +# Calculate averages |
| 122 | +averageCritical=$((totalCritical / ${#images[@]})) |
| 123 | +averageHigh=$((totalHigh / ${#images[@]})) |
| 124 | +averageMedium=$((totalMedium / ${#images[@]})) |
| 125 | +averageLow=$((totalLow / ${#images[@]})) |
| 126 | +averageWontFix=$((totalWontFix / ${#images[@]})) |
| 127 | + |
| 128 | +# Display totals and averages |
| 129 | +echo "Total Vulnerabilities: $totalCount" |
| 130 | +echo "Total Critcal CVEs: $totalCritical" |
| 131 | +echo "Total High CVEs: $totalHigh" |
| 132 | +echo "Total Medium CVEs: $totalMedium" |
| 133 | +echo "Total Low CVEs: $totalLow" |
| 134 | +echo -n "Average Vulnerabilities: "; echo "scale=2; $totalCount / ${#images[@]}" | bc |
| 135 | +echo -n "Average Critcal CVEs: "; echo "scale=2; $totalCritical / ${#images[@]}" | bc |
| 136 | +echo -n "Average High CVEs: "; echo "scale=2; $totalHigh / ${#images[@]}" | bc |
| 137 | +echo -n "Average Medium CVEs: "; echo "scale=2; $totalMedium / ${#images[@]}" | bc |
| 138 | +echo -n "Average Low CVEs: "; echo "scale=2; $totalLow / ${#images[@]}" | bc |
| 139 | + |
| 140 | +echo "JSON Output:" |
| 141 | +echo "$json" |
| 142 | +echo "CSV Output:" |
| 143 | +echo "$json" | jq -r '.items[] | [.image, .scan.total, .scan.critical, .scan.high, .scan.medium, .scan.low, .scan.wontfix] | @csv' |
0 commit comments