From d4e540733c29f1a08fd8408e424daa51557361e2 Mon Sep 17 00:00:00 2001 From: dwertent Date: Thu, 6 Jan 2022 14:16:44 +0200 Subject: [PATCH] adding list controls and update resource id list --- .../v1/reportsummary/controlsummarymethods.go | 21 ++++++++++++------- .../v1/reportsummary/datastructures.go | 5 ++--- .../reportsummary/frameworksummarymethods.go | 13 ++---------- .../results/v1/reportsummary/interface.go | 2 ++ .../v1/reportsummary/summarydetails.go | 19 ++++++++++++----- 5 files changed, 34 insertions(+), 26 deletions(-) diff --git a/reporthandling/results/v1/reportsummary/controlsummarymethods.go b/reporthandling/results/v1/reportsummary/controlsummarymethods.go index 1336b426..0dde461d 100644 --- a/reporthandling/results/v1/reportsummary/controlsummarymethods.go +++ b/reporthandling/results/v1/reportsummary/controlsummarymethods.go @@ -52,6 +52,11 @@ func (controlSummary *ControlSummary) GetScore() float32 { return controlSummary.Score } +// GetScoreFactor return control score +func (controlSummary *ControlSummary) GetScoreFactor() float32 { + return controlSummary.ScoreFactor +} + // =================================== Name ============================================ // GetName return control name @@ -126,14 +131,16 @@ func (controlSummaries *ControlSummaries) ListResourcesIDs() *helpersv1.AllLists //I've implemented it like this because i wanted to support future changes and access things only via interfaces(Lior) ctrlIDs := controlSummaries.ListControlsIDs().All() for _, ctrlID := range ctrlIDs { - tmp := controlSummaries.GetControl(EControlCriteriaID, ctrlID).ListResourcesIDs() - allList.Append(apis.StatusFailed, controlSummaries.GetControl(EControlCriteriaID, ctrlID).ListResourcesIDs().Failed()...) - allList.Append(apis.StatusExcluded, controlSummaries.GetControl(EControlCriteriaID, ctrlID).ListResourcesIDs().Excluded()...) - allList.Append(apis.StatusPassed, tmp.Passed()...) - allList.Append(apis.StatusSkipped, controlSummaries.GetControl(EControlCriteriaID, ctrlID).ListResourcesIDs().Skipped()...) - allList.Append(apis.StatusUnknown, controlSummaries.GetControl(EControlCriteriaID, ctrlID).ListResourcesIDs().Other()...) + resourcesIDs := controlSummaries.GetControl(EControlCriteriaID, ctrlID).ListResourcesIDs() + allList.Append(apis.StatusFailed, resourcesIDs.Failed()...) + allList.Append(apis.StatusExcluded, resourcesIDs.Excluded()...) + allList.Append(apis.StatusPassed, resourcesIDs.Passed()...) + allList.Append(apis.StatusSkipped, resourcesIDs.Skipped()...) + allList.Append(apis.StatusUnknown, resourcesIDs.Other()...) } - // allList.ToUnique() + // initialize resources IDs + allList.ToUnique() + return allList } diff --git a/reporthandling/results/v1/reportsummary/datastructures.go b/reporthandling/results/v1/reportsummary/datastructures.go index 687546df..d25ab10e 100644 --- a/reporthandling/results/v1/reportsummary/datastructures.go +++ b/reporthandling/results/v1/reportsummary/datastructures.go @@ -17,7 +17,6 @@ type SummaryDetails struct { Frameworks []FrameworkSummary `json:"frameworks"` // list of framework summary Controls ControlSummaries `json:"controls,omitempty"` // mapping of control - map[] ResourceCounters ResourceCounters `json:",inline"` - resourceIDs helpersv1.AllLists `json:"-"` } // FrameworkSummary summary of scanning from a single framework perspective @@ -28,7 +27,6 @@ type FrameworkSummary struct { Version string `json:"version"` // framework version Controls ControlSummaries `json:"controls,omitempty"` // mapping of control - map[] ResourceCounters ResourceCounters `json:",inline"` - resourceIDs helpersv1.AllLists `json:"-"` } type ControlSummaries map[string]ControlSummary @@ -39,8 +37,9 @@ type ControlSummary struct { Name string `json:"name"` Status apis.ScanningStatus `json:"status"` Score float32 `json:"score"` - ResourceCounters ResourceCounters `json:",inline"` + ScoreFactor float32 `json:"scoreFactor"` ResourceIDs helpersv1.AllLists `json:"resourceIDs"` + ResourceCounters ResourceCounters `json:",inline"` Description string `json:"-"` Remediation string `json:"-"` } diff --git a/reporthandling/results/v1/reportsummary/frameworksummarymethods.go b/reporthandling/results/v1/reportsummary/frameworksummarymethods.go index 86d65916..8b59f01d 100644 --- a/reporthandling/results/v1/reportsummary/frameworksummarymethods.go +++ b/reporthandling/results/v1/reportsummary/frameworksummarymethods.go @@ -38,7 +38,7 @@ func (frameworkSummary *FrameworkSummary) ListControls() IControlsSummaries { // List resources IDs func (frameworkSummary *FrameworkSummary) ListResourcesIDs() *helpersv1.AllLists { - return &frameworkSummary.resourceIDs + return frameworkSummary.Controls.ListResourcesIDs() } func (frameworkSummary *FrameworkSummary) NumberOfControls() ICounters { @@ -52,24 +52,15 @@ func (frameworkSummary *FrameworkSummary) NumberOfControls() ICounters { // initResourcesSummary must run this AFTER initializing the controls func (frameworkSummary *FrameworkSummary) initResourcesSummary() { - frameworkSummary.resourceIDs = helpersv1.AllLists{} for k, control := range frameworkSummary.Controls { control.initResourcesSummary() frameworkSummary.Controls[k] = control - frameworkSummary.resourceIDs.Update(control.ListResourcesIDs()) } - frameworkSummary.ResourceCounters.Set(&frameworkSummary.resourceIDs) + frameworkSummary.ResourceCounters.Set(frameworkSummary.Controls.ListResourcesIDs()) frameworkSummary.CalculateStatus() } -// Append increases the counter based on the status -func (frameworkSummary *FrameworkSummary) Append(status apis.IStatus, ids ...string) { - for i := range ids { - frameworkSummary.resourceIDs.Append(status.Status(), ids[i]) - } -} - // =================================== Score ============================================ // GetScore return framework score diff --git a/reporthandling/results/v1/reportsummary/interface.go b/reporthandling/results/v1/reportsummary/interface.go index 60f9a988..fcd9b4a3 100644 --- a/reporthandling/results/v1/reportsummary/interface.go +++ b/reporthandling/results/v1/reportsummary/interface.go @@ -31,10 +31,12 @@ type IFrameworkSummary interface { type IControlSummary interface { IPolicies + GetScoreFactor() float32 } type IControlsSummaries interface { GetControl(criteria ControlCriteria, value string) IControlSummary + NumberOfControls() ICounters ListControlsIDs() *helpersv1.AllLists ListResourcesIDs() *helpersv1.AllLists //avoid using this outside of kubescape diff --git a/reporthandling/results/v1/reportsummary/summarydetails.go b/reporthandling/results/v1/reportsummary/summarydetails.go index 15317c3f..692dea04 100644 --- a/reporthandling/results/v1/reportsummary/summarydetails.go +++ b/reporthandling/results/v1/reportsummary/summarydetails.go @@ -39,15 +39,12 @@ func (summaryDetails *SummaryDetails) InitResourcesSummary() { summaryDetails.Frameworks[i].initResourcesSummary() } - summaryDetails.resourceIDs = helpersv1.AllLists{} - for k, control := range summaryDetails.Controls { control.initResourcesSummary() summaryDetails.Controls[k] = control - summaryDetails.resourceIDs.Update(control.ListResourcesIDs()) } - summaryDetails.ResourceCounters.Set(&summaryDetails.resourceIDs) + summaryDetails.ResourceCounters.Set(summaryDetails.Controls.ListResourcesIDs()) summaryDetails.CalculateStatus() } @@ -100,6 +97,18 @@ func (summaryDetails *SummaryDetails) ListControls() *ListPolicies { return &controls } +//NumberOfControls get number of controls +func (summaryDetails *SummaryDetails) NumberOfControls() ICounters { + + return &PostureCounters{ + PassedCounter: len(summaryDetails.ListControlsIDs().Passed()), + FailedCounter: len(summaryDetails.ListControlsIDs().Failed()), + ExcludedCounter: len(summaryDetails.ListControlsIDs().Excluded()), + SkippedCounter: len(summaryDetails.ListControlsIDs().Skipped()), + UnknownCounter: len(summaryDetails.ListControlsIDs().Other()), + } +} + // ================================================================================ func (summaryDetails *SummaryDetails) ControlName(controlID string) string { if c, ok := summaryDetails.Controls[controlID]; ok { @@ -110,7 +119,7 @@ func (summaryDetails *SummaryDetails) ControlName(controlID string) string { // ListResourcesIDs list all resources IDs func (summaryDetails *SummaryDetails) ListResourcesIDs() *helpersv1.AllLists { - return &summaryDetails.resourceIDs + return summaryDetails.Controls.ListResourcesIDs() } // updateSummaryWithResource get the result of a single resource. If resource not found will return nil