Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sort images by vulnerabilities count #292

Merged
merged 1 commit into from
Apr 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions pkg/registry/scanner/reports/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ func GenerateReports(images map[string]kmapi.ImageInfo, results map[string]resul
}

return &reportsapi.CVEReportResponse{
Images: sortImageInfosByImageName(imginfos),
Images: sortImageInfosByVulnerabilities(imginfos),
Vulnerabilities: reportsapi.VulnerabilityInfo{
Stats: getVulnerabilityStats(totalOccurrence, vuls),
CVEs: getCVEsFromVulnerabilityInfoMap(vuls),
Expand Down Expand Up @@ -412,17 +412,27 @@ func getVulnerabilityStats(totalOccurrence map[string]int, vuls map[string]trivy
return stats
}

func sortImageInfosByImageName(imginfos map[string]reportsapi.ImageInfo) []reportsapi.ImageInfo {
func sortImageInfosByVulnerabilities(imginfos map[string]reportsapi.ImageInfo) []reportsapi.ImageInfo {
images := make([]reportsapi.ImageInfo, 0, len(imginfos))
for _, ii := range imginfos {
images = append(images, ii)
}
sort.Slice(images, func(i, j int) bool {
return images[i].Image.Name < images[j].Image.Name
return calculateVulnerabilities(images[i].Stats) < calculateVulnerabilities(images[j].Stats)
})
return images
}

func calculateVulnerabilities(stats map[string]reportsapi.RiskStats) int {
count := 0
for _, key := range []string{"HIGH", "LOW", "MEDIUM", "CRITICAL", "UNKNOWN"} {
if val, ok := stats[key]; ok {
count += val.Count
}
}
return count
}

func getCVEsFromVulnerabilityInfoMap(vuls map[string]trivy.VulnerabilityInfo) []trivy.VulnerabilityInfo {
cves := make([]trivy.VulnerabilityInfo, 0, len(vuls))
for _, vul := range vuls {
Expand Down
Loading