From d6a47a82d24e20567bb721583df2eabc865a80aa Mon Sep 17 00:00:00 2001 From: Daniel Grunberger <84905812+Daniel-GrunbergerCA@users.noreply.github.com> Date: Wed, 16 Aug 2023 13:01:32 +0300 Subject: [PATCH] improve cli output (#1347) Signed-off-by: Daniel Grunberger Co-authored-by: Daniel Grunberger --- .../printer/v2/prettyprinter/imagescan.go | 4 +- .../tableprinter/imageprinter/utils.go | 6 +- .../printer/v2/prettyprinter/utils.go | 13 +++ .../printer/v2/prettyprinter/utils_test.go | 101 ++++++++++++++++++ 4 files changed, 118 insertions(+), 6 deletions(-) diff --git a/core/pkg/resultshandling/printer/v2/prettyprinter/imagescan.go b/core/pkg/resultshandling/printer/v2/prettyprinter/imagescan.go index 926fedcb..8adbb32d 100644 --- a/core/pkg/resultshandling/printer/v2/prettyprinter/imagescan.go +++ b/core/pkg/resultshandling/printer/v2/prettyprinter/imagescan.go @@ -5,7 +5,6 @@ import ( "github.com/kubescape/kubescape/v2/core/cautils" "github.com/kubescape/kubescape/v2/core/pkg/resultshandling/printer/v2/prettyprinter/tableprinter/imageprinter" - "github.com/kubescape/opa-utils/reporthandling/apis" "github.com/kubescape/opa-utils/reporthandling/results/v1/reportsummary" ) @@ -37,8 +36,7 @@ func (ip *ImagePrinter) PrintImageScanning(summary *imageprinter.ImageScanSummar func (ip *ImagePrinter) PrintImageScanningTable(summary imageprinter.ImageScanSummary) { if !ip.verboseMode { - // filter out vulnerabilities with severity lower than High - summary.CVEs = filterCVEsBySeverities(summary.CVEs, []string{apis.SeverityCriticalString, apis.SeverityHighString}) + summary.CVEs = getFilteredCVEs(summary.CVEs) } ip.imageTablePrinter.PrintImageScanningTable(ip.writer, summary) diff --git a/core/pkg/resultshandling/printer/v2/prettyprinter/tableprinter/imageprinter/utils.go b/core/pkg/resultshandling/printer/v2/prettyprinter/tableprinter/imageprinter/utils.go index a01a45a7..201d3bdf 100644 --- a/core/pkg/resultshandling/printer/v2/prettyprinter/tableprinter/imageprinter/utils.go +++ b/core/pkg/resultshandling/printer/v2/prettyprinter/tableprinter/imageprinter/utils.go @@ -77,7 +77,7 @@ func getImageScanningColumnsAlignments() []int { return []int{tablewriter.ALIGN_CENTER, tablewriter.ALIGN_LEFT, tablewriter.ALIGN_LEFT, tablewriter.ALIGN_LEFT, tablewriter.ALIGN_LEFT} } -func getColor(severity string) (func(...string) string) { +func getColor(severity string) func(...string) string { switch severity { case apis.SeverityCriticalString: return gchalk.WithAnsi256(1).Bold @@ -88,8 +88,8 @@ func getColor(severity string) (func(...string) string) { case apis.SeverityLowString: return gchalk.WithAnsi256(220).Bold case apis.SeverityNegligibleString: - return gchalk.WithAnsi256(16).Bold + return gchalk.WithAnsi256(39).Bold default: - return gchalk.WithAnsi256(16).Bold + return gchalk.WithAnsi256(30).Bold } } diff --git a/core/pkg/resultshandling/printer/v2/prettyprinter/utils.go b/core/pkg/resultshandling/printer/v2/prettyprinter/utils.go index fcf0416a..fcb262e8 100644 --- a/core/pkg/resultshandling/printer/v2/prettyprinter/utils.go +++ b/core/pkg/resultshandling/printer/v2/prettyprinter/utils.go @@ -102,6 +102,19 @@ func addEmptySeverities(mapSeverityTSummary map[string]*imageprinter.SeveritySum } } +// getFilteredCVEs returns a list of CVEs to show in the table. If there are no vulnerabilities with severity Critical or High, it will return vulnerabilities with severity Medium. Otherwise it will return vulnerabilities with severity Critical or High +func getFilteredCVEs(cves []imageprinter.CVE) []imageprinter.CVE { + // filter out vulnerabilities with severity lower than High + filteredCVEs := filterCVEsBySeverities(cves, []string{apis.SeverityCriticalString, apis.SeverityHighString}) + + // if there are no vulnerabilities with severity Critical or High, add vulnerabilities with severity Medium + if len(filteredCVEs) == 0 { + filteredCVEs = filterCVEsBySeverities(cves, []string{apis.SeverityMediumString}) + } + + return filteredCVEs +} + // filterCVEsBySeverities returns a list of CVEs only with the severities that are in the severities list func filterCVEsBySeverities(cves []imageprinter.CVE, severities []string) []imageprinter.CVE { var filteredCVEs []imageprinter.CVE diff --git a/core/pkg/resultshandling/printer/v2/prettyprinter/utils_test.go b/core/pkg/resultshandling/printer/v2/prettyprinter/utils_test.go index 0b3e169c..60a568f2 100644 --- a/core/pkg/resultshandling/printer/v2/prettyprinter/utils_test.go +++ b/core/pkg/resultshandling/printer/v2/prettyprinter/utils_test.go @@ -535,3 +535,104 @@ func TestGetSortedCVEsBySeverity(t *testing.T) { }) } } + +func TestGetFilteredCVEs(t *testing.T) { + tests := []struct { + name string + cves []imageprinter.CVE + expectedCVEs []imageprinter.CVE + }{ + { + name: "High and Critical", + cves: []imageprinter.CVE{ + { + Severity: "High", + }, + { + Severity: "Critical", + }, + { + Severity: "Medium", + }, + { + Severity: "Low", + }, + { + Severity: "Negligible", + }, + }, + expectedCVEs: []imageprinter.CVE{ + { + Severity: "High", + }, + { + Severity: "Critical", + }, + }, + }, + { + name: "Only High", + cves: []imageprinter.CVE{ + { + Severity: "High", + }, + { + Severity: "Medium", + }}, + expectedCVEs: []imageprinter.CVE{ + { + Severity: "High", + }, + }, + }, + { + name: "Only Critical", + cves: []imageprinter.CVE{ + { + Severity: "Critical", + }, + { + Severity: "Medium", + }}, + expectedCVEs: []imageprinter.CVE{ + { + Severity: "Critical", + }, + }, + }, + { + name: "No High or Critical", + cves: []imageprinter.CVE{ + { + Severity: "Low", + }, + { + Severity: "Medium", + }}, + + expectedCVEs: []imageprinter.CVE{ + { + Severity: "Medium", + }, + }, + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + filteredCVEs := getFilteredCVEs(tc.cves) + for _, cve := range filteredCVEs { + found := false + for _, expectedCVE := range tc.expectedCVEs { + if cve.Severity == expectedCVE.Severity { + found = true + break + } + } + if !found { + t.Errorf("Expected: %v, Got: %v", tc.expectedCVEs, filteredCVEs) + } + } + }) + } +}