From 3ac33d21ac6db079cde7be7ae9214163dd6d099c Mon Sep 17 00:00:00 2001 From: kooomix Date: Mon, 12 Dec 2022 15:20:48 +0200 Subject: [PATCH] All prints and outputs to get data by control ID --- .../printer/v2/controltable.go | 13 +++++++++++ .../resultshandling/printer/v2/htmlprinter.go | 2 +- core/pkg/resultshandling/printer/v2/pdf.go | 14 ++++++------ .../printer/v2/prettyprinter.go | 22 +++++++++---------- .../printer/v2/resourcetable.go | 2 +- 5 files changed, 33 insertions(+), 20 deletions(-) diff --git a/core/pkg/resultshandling/printer/v2/controltable.go b/core/pkg/resultshandling/printer/v2/controltable.go index e78288fb..ce50e48c 100644 --- a/core/pkg/resultshandling/printer/v2/controltable.go +++ b/core/pkg/resultshandling/printer/v2/controltable.go @@ -78,6 +78,19 @@ func getColor(controlSeverity int) color.Attribute { } } +func getSortedControlsIDs(controls reportsummary.ControlSummaries) [][]string { + controlIDs := make([][]string, 5) + for k := range controls { + c := controls[k] + i := apis.ControlSeverityToInt(c.GetScoreFactor()) + controlIDs[i] = append(controlIDs[i], c.GetID()) + } + for i := range controlIDs { + sort.Strings(controlIDs[i]) + } + return controlIDs +} + func getSortedControlsNames(controls reportsummary.ControlSummaries) [][]string { controlNames := make([][]string, 5) for k := range controls { diff --git a/core/pkg/resultshandling/printer/v2/htmlprinter.go b/core/pkg/resultshandling/printer/v2/htmlprinter.go index 958c6e25..df6b5fe2 100644 --- a/core/pkg/resultshandling/printer/v2/htmlprinter.go +++ b/core/pkg/resultshandling/printer/v2/htmlprinter.go @@ -140,7 +140,7 @@ func buildResourceControlResultTable(resourceControls []resourcesresults.Resourc var ctlResults []ResourceControlResult for _, resourceControl := range resourceControls { if resourceControl.GetStatus(nil).IsFailed() { - control := summaryDetails.Controls.GetControl(reportsummary.EControlCriteriaName, resourceControl.GetName()) + control := summaryDetails.Controls.GetControl(reportsummary.EControlCriteriaID, resourceControl.GetID()) ctlResult := buildResourceControlResult(resourceControl, control) ctlResults = append(ctlResults, ctlResult) diff --git a/core/pkg/resultshandling/printer/v2/pdf.go b/core/pkg/resultshandling/printer/v2/pdf.go index cffdd1f4..bb71bca5 100644 --- a/core/pkg/resultshandling/printer/v2/pdf.go +++ b/core/pkg/resultshandling/printer/v2/pdf.go @@ -76,13 +76,13 @@ func (pdfPrinter *PdfPrinter) printInfo(m pdf.Maroto, summaryDetails *reportsumm } func (pdfPrinter *PdfPrinter) ActionPrint(opaSessionObj *cautils.OPASessionObj) { - sortedControlNames := getSortedControlsNames(opaSessionObj.Report.SummaryDetails.Controls) + sortedControlIDs := getSortedControlsIDs(opaSessionObj.Report.SummaryDetails.Controls) infoToPrintInfo := mapInfoToPrintInfo(opaSessionObj.Report.SummaryDetails.Controls) m := pdf.NewMaroto(consts.Portrait, consts.A4) pdfPrinter.printHeader(m) pdfPrinter.printFramework(m, opaSessionObj.Report.SummaryDetails.ListFrameworks()) - pdfPrinter.printTable(m, &opaSessionObj.Report.SummaryDetails, sortedControlNames) + pdfPrinter.printTable(m, &opaSessionObj.Report.SummaryDetails, sortedControlIDs) pdfPrinter.printFinalResult(m, &opaSessionObj.Report.SummaryDetails) pdfPrinter.printInfo(m, &opaSessionObj.Report.SummaryDetails, infoToPrintInfo) @@ -149,16 +149,16 @@ func (pdfPrinter *PdfPrinter) printFramework(m pdf.Maroto, frameworks []reportsu } // Create pdf table -func (pdfPrinter *PdfPrinter) printTable(m pdf.Maroto, summaryDetails *reportsummary.SummaryDetails, sortedControlNames [][]string) { +func (pdfPrinter *PdfPrinter) printTable(m pdf.Maroto, summaryDetails *reportsummary.SummaryDetails, sortedControlIDs [][]string) { headers := getControlTableHeaders() infoToPrintInfoMap := mapInfoToPrintInfo(summaryDetails.Controls) - controls := make([][]string, len(sortedControlNames)) + controls := make([][]string, len(sortedControlIDs)) for i := range controls { controls[i] = make([]string, len(headers)) } - for i := len(sortedControlNames) - 1; i >= 0; i-- { - for _, c := range sortedControlNames[i] { - controls[i] = generateRow(summaryDetails.Controls.GetControl(reportsummary.EControlCriteriaName, c), infoToPrintInfoMap, true) + for i := len(sortedControlIDs) - 1; i >= 0; i-- { + for _, c := range sortedControlIDs[i] { + controls[i] = generateRow(summaryDetails.Controls.GetControl(reportsummary.EControlCriteriaID, c), infoToPrintInfoMap, true) } } diff --git a/core/pkg/resultshandling/printer/v2/prettyprinter.go b/core/pkg/resultshandling/printer/v2/prettyprinter.go index 09ebaefc..4e6e9611 100644 --- a/core/pkg/resultshandling/printer/v2/prettyprinter.go +++ b/core/pkg/resultshandling/printer/v2/prettyprinter.go @@ -34,18 +34,18 @@ func NewPrettyPrinter(verboseMode bool, formatVersion string, viewType cautils.V func (prettyPrinter *PrettyPrinter) ActionPrint(opaSessionObj *cautils.OPASessionObj) { fmt.Fprintf(prettyPrinter.writer, "\n"+getSeparator("^")+"\n") - sortedControlNames := getSortedControlsNames(opaSessionObj.Report.SummaryDetails.Controls) // ListControls().All()) + sortedControlIDs := getSortedControlsIDs(opaSessionObj.Report.SummaryDetails.Controls) // ListControls().All()) switch prettyPrinter.viewType { case cautils.ControlViewType: - prettyPrinter.printResults(&opaSessionObj.Report.SummaryDetails.Controls, opaSessionObj.AllResources, sortedControlNames) + prettyPrinter.printResults(&opaSessionObj.Report.SummaryDetails.Controls, opaSessionObj.AllResources, sortedControlIDs) case cautils.ResourceViewType: if prettyPrinter.verboseMode { prettyPrinter.resourceTable(opaSessionObj) } } - prettyPrinter.printSummaryTable(&opaSessionObj.Report.SummaryDetails, sortedControlNames) + prettyPrinter.printSummaryTable(&opaSessionObj.Report.SummaryDetails, sortedControlIDs) } @@ -56,10 +56,10 @@ func (prettyPrinter *PrettyPrinter) SetWriter(outputFile string) { func (prettyPrinter *PrettyPrinter) Score(score float32) { } -func (prettyPrinter *PrettyPrinter) printResults(controls *reportsummary.ControlSummaries, allResources map[string]workloadinterface.IMetadata, sortedControlNames [][]string) { - for i := len(sortedControlNames) - 1; i >= 0; i-- { - for _, c := range sortedControlNames[i] { - controlSummary := controls.GetControl(reportsummary.EControlCriteriaName, c) // summaryDetails.Controls ListControls().All() Controls.GetControl(ca) +func (prettyPrinter *PrettyPrinter) printResults(controls *reportsummary.ControlSummaries, allResources map[string]workloadinterface.IMetadata, sortedControlIDs [][]string) { + for i := len(sortedControlIDs) - 1; i >= 0; i-- { + for _, c := range sortedControlIDs[i] { + controlSummary := controls.GetControl(reportsummary.EControlCriteriaID, c) // summaryDetails.Controls ListControls().All() Controls.GetControl(ca) prettyPrinter.printTitle(controlSummary) prettyPrinter.printResources(controlSummary, allResources) prettyPrinter.printSummary(c, controlSummary) @@ -185,7 +185,7 @@ func generateFooter(summaryDetails *reportsummary.SummaryDetails) []string { return row } -func (prettyPrinter *PrettyPrinter) printSummaryTable(summaryDetails *reportsummary.SummaryDetails, sortedControlNames [][]string) { +func (prettyPrinter *PrettyPrinter) printSummaryTable(summaryDetails *reportsummary.SummaryDetails, sortedControlIDs [][]string) { if summaryDetails.NumberOfControls().All() == 0 { fmt.Fprintf(prettyPrinter.writer, "\nKubescape did not scan any of the resources, make sure you are scanning valid kubernetes manifests (Deployments, Pods, etc.)\n") @@ -209,9 +209,9 @@ func (prettyPrinter *PrettyPrinter) printSummaryTable(summaryDetails *reportsumm } infoToPrintInfo := mapInfoToPrintInfo(summaryDetails.Controls) - for i := len(sortedControlNames) - 1; i >= 0; i-- { - for _, c := range sortedControlNames[i] { - row := generateRow(summaryDetails.Controls.GetControl(reportsummary.EControlCriteriaName, c), infoToPrintInfo, printAll) + for i := len(sortedControlIDs) - 1; i >= 0; i-- { + for _, c := range sortedControlIDs[i] { + row := generateRow(summaryDetails.Controls.GetControl(reportsummary.EControlCriteriaID, c), infoToPrintInfo, printAll) if len(row) > 0 { summaryTable.Append(row) } diff --git a/core/pkg/resultshandling/printer/v2/resourcetable.go b/core/pkg/resultshandling/printer/v2/resourcetable.go index af5d46b1..90901eff 100644 --- a/core/pkg/resultshandling/printer/v2/resourcetable.go +++ b/core/pkg/resultshandling/printer/v2/resourcetable.go @@ -77,7 +77,7 @@ func generateResourceRows(controls []resourcesresults.ResourceAssociatedControl, row[resourceColumnPath] = strings.Join(append(failedPathsToString(&controls[i]), fixPathsToString(&controls[i])...), "\n") row[resourceColumnName] = controls[i].GetName() - if c := summaryDetails.Controls.GetControl(reportsummary.EControlCriteriaName, controls[i].GetName()); c != nil { + if c := summaryDetails.Controls.GetControl(reportsummary.EControlCriteriaID, controls[i].GetID()); c != nil { row[resourceColumnSeverity] = getSeverityColumn(c) }