Skip to content

Commit

Permalink
Merge branch 'master' of ssh://github.com/armosec/opa-utils
Browse files Browse the repository at this point in the history
  • Loading branch information
LiorAlafiArmo committed Jan 3, 2022
2 parents ea9029d + c69d6c7 commit 862b191
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 16 deletions.
49 changes: 46 additions & 3 deletions reporthandling/helpers/v1/listing.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@ func (all *AllLists) All() []string {
l = append(l, all.passed...)
l = append(l, all.skipped...)
l = append(l, all.other...)
return l
return shared.SliceStringToUnique(l)
}

// Append append single string to matching status list
func (all *AllLists) Append(status apis.ScanningStatus, str string) {

switch status {
case apis.StatusPassed:
all.passed = append(all.passed, str)
Expand All @@ -46,6 +47,9 @@ func (all *AllLists) Append(status apis.ScanningStatus, str string) {
default:
all.other = append(all.other, str)
}

// make sure the lists are unique
all.toUnique()
}

// Update AllLists objects with
Expand All @@ -55,12 +59,51 @@ func (all *AllLists) Update(all2 *AllLists) {
all.excluded = append(all.excluded, all2.excluded...)
all.skipped = append(all.skipped, all2.skipped...)
all.other = append(all.other, all2.other...)

// make sure the lists are unique
all.toUnique()
}

func (all *AllLists) ToUnique() {
all.passed = shared.SliceStringToUnique(all.passed)
func (all *AllLists) toUnique() {
// remove duplications from each resource list
all.failed = shared.SliceStringToUnique(all.failed)
all.excluded = shared.SliceStringToUnique(all.excluded)
all.passed = shared.SliceStringToUnique(all.passed)
all.skipped = shared.SliceStringToUnique(all.skipped)
all.other = shared.SliceStringToUnique(all.other)

// remove failed from excluded list
all.excluded = trimUnique(all.excluded, all.failed)

// remove failed and excluded from passed list
trimmed := append(all.failed, all.excluded...)
all.passed = trimUnique(all.passed, trimmed)

// remove failed, excluded and passed from skipped list
trimmed = append(trimmed, all.passed...)
all.skipped = trimUnique(all.skipped, trimmed)

// remove failed, excluded, passed and skipped from other list
trimmed = append(trimmed, all.skipped...)
all.other = trimUnique(all.other, trimmed)
}

// trimUnique trim the list, return original list without the "trimFrom" list
func trimUnique(origin, trimFrom []string) []string {
if len(origin) == 0 || len(trimFrom) == 0 { // if there is nothing to trim
return origin
}
unique := map[string]bool{}
originList := []string{}

for i := range trimFrom {
unique[trimFrom[i]] = true
}

for i := range origin {
if found := unique[origin[i]]; !found {
originList = append(originList, origin[i])
}
}
return originList
}
30 changes: 22 additions & 8 deletions reporthandling/helpers/v1/listing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ func TestAllListsUpdate(t *testing.T) {
listB := mockAllListsB()
listB.Update(listA)

oldListB := mockAllListsB()
assert.Equal(t, len(listA.Passed())+len(oldListB.Passed()), len(listB.Passed()))
assert.Equal(t, len(listA.Excluded())+len(oldListB.Excluded()), len(listB.Excluded()))
assert.Equal(t, len(listA.Failed())+len(oldListB.Failed()), len(listB.Failed()))
assert.Equal(t, len(listA.Skipped())+len(oldListB.Skipped()), len(listB.Skipped()))
assert.Equal(t, len(listA.Other())+len(oldListB.Other()), len(listB.Other()))
assert.Equal(t, 11, len(listB.All()))
assert.Equal(t, 0, len(listB.Passed()))
assert.Equal(t, 4, len(listB.Excluded()))
assert.Equal(t, 3, len(listB.Failed()))
assert.Equal(t, 4, len(listB.Skipped()))
assert.Equal(t, 0, len(listB.Other()))
}

func TestAllListsAppend(t *testing.T) {
Expand All @@ -60,7 +60,7 @@ func TestAllListsAppend(t *testing.T) {

oldListA := mockAllListsA()

assert.Equal(t, len(oldListA.Excluded())+3, len(listA.Excluded()))
assert.Equal(t, len(oldListA.Excluded()), len(listA.Excluded()))
}

func TestAllListsUnique(t *testing.T) {
Expand All @@ -71,6 +71,20 @@ func TestAllListsUnique(t *testing.T) {

oldListA := mockAllListsA()

listA.ToUnique()
assert.Equal(t, len(oldListA.Passed()), len(listA.Passed()))

listMock := AllLists{}

listMock.Append(apis.StatusPassed, "a")
assert.Equal(t, 1, len(listMock.All()))
assert.Equal(t, len(listMock.All()), len(listMock.Passed()))
assert.Equal(t, 0, len(listMock.Failed()))
assert.Equal(t, 0, len(listMock.Excluded()))

listMock.Append(apis.StatusExcluded, "a")
listMock.Append(apis.StatusFailed, "a")

assert.Equal(t, 1, len(listMock.All()))
assert.Equal(t, len(listMock.All()), len(listMock.Failed()))
assert.Equal(t, 0, len(listMock.Passed()))
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ func (frameworkSummary *FrameworkSummary) initResourcesSummary() {
for _, control := range frameworkSummary.Controls {
frameworkSummary.resourceIDs.Update(control.List())
}
frameworkSummary.resourceIDs.ToUnique()

frameworkSummary.ResourceCounters.Set(&frameworkSummary.resourceIDs)
}
Expand Down
5 changes: 2 additions & 3 deletions reporthandling/results/v1/reportsummary/summarydetails.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,15 @@ func (summaryDetails *SummaryDetails) Increase(status apis.IStatus) {

// InitResourcesSummary must run this AFTER initializing the controls
func (summaryDetails *SummaryDetails) InitResourcesSummary() {
for _, frameworks := range summaryDetails.Frameworks {
frameworks.initResourcesSummary()
for i := range summaryDetails.Frameworks {
summaryDetails.Frameworks[i].initResourcesSummary()
}

summaryDetails.resourceIDs = helpersv1.AllLists{}

for _, control := range summaryDetails.Controls {
summaryDetails.resourceIDs.Update(control.List())
}
summaryDetails.resourceIDs.ToUnique()

summaryDetails.ResourceCounters.Set(&summaryDetails.resourceIDs)
}
Expand Down
6 changes: 5 additions & 1 deletion reporthandling/results/v1/resourcesresults/resultmethods.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,13 @@ func (result *Result) ListRulesNames(f *helpersv1.Filters) *helpersv1.AllLists {
// ListRulesNames return list of rules names
func (result *Result) ListRules() []ResourceAssociatedRule {
rules := []ResourceAssociatedRule{}
ruleNames := map[string]bool{}
for i := range result.AssociatedControls {
for j := range result.AssociatedControls[i].ResourceAssociatedRules {
rules = append(rules, result.AssociatedControls[i].ResourceAssociatedRules[j])
if _, ok := ruleNames[result.AssociatedControls[i].ResourceAssociatedRules[j].GetName()]; !ok {
rules = append(rules, result.AssociatedControls[i].ResourceAssociatedRules[j])
ruleNames[result.AssociatedControls[i].ResourceAssociatedRules[j].GetName()] = true
}
}
}
return rules
Expand Down

0 comments on commit 862b191

Please sign in to comment.