improve cli output (#1347)

Signed-off-by: Daniel Grunberger <danielgrunberger@armosec.io>
Co-authored-by: Daniel Grunberger <danielgrunberger@armosec.io>
This commit is contained in:
Daniel Grunberger
2023-08-16 13:01:32 +03:00
committed by GitHub
co-authored by Daniel Grunberger
parent 936cb26c06
commit d6a47a82d2
4 changed files with 118 additions and 6 deletions
@@ -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)
@@ -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
}
}
@@ -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
@@ -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)
}
}
})
}
}