Skip to content

Commit

Permalink
adding list controls and update resource id list
Browse files Browse the repository at this point in the history
  • Loading branch information
dwertent committed Jan 6, 2022
1 parent 42ea42a commit d4e5407
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 26 deletions.
21 changes: 14 additions & 7 deletions reporthandling/results/v1/reportsummary/controlsummarymethods.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
5 changes: 2 additions & 3 deletions reporthandling/results/v1/reportsummary/datastructures.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ type SummaryDetails struct {
Frameworks []FrameworkSummary `json:"frameworks"` // list of framework summary
Controls ControlSummaries `json:"controls,omitempty"` // mapping of control - map[<control ID>]<control summary>
ResourceCounters ResourceCounters `json:",inline"`
resourceIDs helpersv1.AllLists `json:"-"`
}

// FrameworkSummary summary of scanning from a single framework perspective
Expand All @@ -28,7 +27,6 @@ type FrameworkSummary struct {
Version string `json:"version"` // framework version
Controls ControlSummaries `json:"controls,omitempty"` // mapping of control - map[<control ID>]<control summary>
ResourceCounters ResourceCounters `json:",inline"`
resourceIDs helpersv1.AllLists `json:"-"`
}

type ControlSummaries map[string]ControlSummary
Expand All @@ -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:"-"`
}
Expand Down
13 changes: 2 additions & 11 deletions reporthandling/results/v1/reportsummary/frameworksummarymethods.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions reporthandling/results/v1/reportsummary/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 14 additions & 5 deletions reporthandling/results/v1/reportsummary/summarydetails.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand Down

0 comments on commit d4e5407

Please sign in to comment.