-
Notifications
You must be signed in to change notification settings - Fork 197
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli): Show top 5 failed workload configuration checks in html re…
- Loading branch information
Showing
6 changed files
with
391 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package report | ||
|
||
import ( | ||
"sort" | ||
|
||
"github.com/aquasecurity/starboard/pkg/report/templates" | ||
) | ||
|
||
type LessFunc func(p1, p2 *templates.CheckWithCount) bool | ||
|
||
// multiSorter implements the Sort interface, sorting the reports within. | ||
type multiSorter struct { | ||
checks []templates.CheckWithCount | ||
less []LessFunc | ||
} | ||
|
||
// SortDesc sorts the argument slice according to the LessFunc functions passed to OrderedBy. | ||
func (ms *multiSorter) SortDesc(reports []templates.CheckWithCount) { | ||
ms.checks = reports | ||
sort.Stable(sort.Reverse(ms)) | ||
} | ||
|
||
// OrderedBy returns a Sorter that sorts using the LessFunc functions, in order. | ||
// Call its Sort method to sort the data. | ||
func OrderedBy(less ...LessFunc) *multiSorter { | ||
return &multiSorter{ | ||
less: less, | ||
} | ||
} | ||
|
||
// Len is part of sort.Interface. | ||
func (ms *multiSorter) Len() int { | ||
return len(ms.checks) | ||
} | ||
|
||
// Swap is part of sort.Interface. | ||
func (ms *multiSorter) Swap(i, j int) { | ||
ms.checks[i], ms.checks[j] = ms.checks[j], ms.checks[i] | ||
} | ||
|
||
// Less is part of sort.Interface. It is implemented by looping along the | ||
// less functions until it finds a comparison that discriminates between | ||
// the two items (one is less than the other). Note that it can call the | ||
// less functions twice per call. We could change the functions to return | ||
// -1, 0, 1 and reduce the number of calls for greater efficiency: an | ||
// exercise for the reader. | ||
func (ms *multiSorter) Less(i, j int) bool { | ||
p, q := &ms.checks[i], &ms.checks[j] | ||
// Try all but the last comparison. | ||
var k int | ||
for k = 0; k < len(ms.less)-1; k++ { | ||
less := ms.less[k] | ||
switch { | ||
case less(p, q): | ||
// p < q, so we have a decision. | ||
return true | ||
case less(q, p): | ||
// p > q, so we have a decision. | ||
return false | ||
} | ||
// p == q; try the next comparison. | ||
} | ||
// All comparisons to here said "equal", so just return whatever | ||
// the final comparison reports. | ||
return ms.less[k](p, q) | ||
} | ||
|
||
var ( | ||
checkCompareFunc = []LessFunc{ | ||
func(r1, r2 *templates.CheckWithCount) bool { | ||
return r1.AffectedWorkloads < r2.AffectedWorkloads | ||
}, func(r1, r2 *templates.CheckWithCount) bool { | ||
return r1.Severity > r2.Severity | ||
}, func(r1, r2 *templates.CheckWithCount) bool { | ||
return r1.ID > r2.ID | ||
}} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package report | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/aquasecurity/starboard/pkg/apis/aquasecurity/v1alpha1" | ||
"github.com/aquasecurity/starboard/pkg/report/templates" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestOrderedBy(t *testing.T) { | ||
checks := []templates.CheckWithCount{ | ||
{ | ||
Check: v1alpha1.Check{ | ||
ID: "privilegeEscalationAllowed", | ||
Severity: "danger", | ||
Category: "Security", | ||
}, | ||
AffectedWorkloads: 10, | ||
}, | ||
{ | ||
Check: v1alpha1.Check{ | ||
ID: "cpuLimitsMissing", | ||
Severity: "warning", | ||
Category: "Efficiency", | ||
}, | ||
AffectedWorkloads: 12, | ||
}, | ||
{ | ||
Check: v1alpha1.Check{ | ||
ID: "cpuRequestsMissing", | ||
Severity: "warning", | ||
Category: "Efficiency", | ||
}, | ||
AffectedWorkloads: 8, | ||
}, | ||
{ | ||
Check: v1alpha1.Check{ | ||
ID: "livenessProbeMissing", | ||
Severity: "warning", | ||
Category: "Reliability", | ||
}, | ||
AffectedWorkloads: 5, | ||
}, | ||
{ | ||
Check: v1alpha1.Check{ | ||
ID: "insecureCapabilities", | ||
Severity: "warning", | ||
Category: "Security", | ||
}, | ||
AffectedWorkloads: 5, | ||
}, | ||
} | ||
|
||
OrderedBy(checkCompareFunc...).SortDesc(checks) | ||
assert.Equal(t, []templates.CheckWithCount{ | ||
{ | ||
Check: v1alpha1.Check{ | ||
ID: "cpuLimitsMissing", | ||
Severity: "warning", | ||
Category: "Efficiency", | ||
}, | ||
AffectedWorkloads: 12, | ||
}, | ||
{ | ||
Check: v1alpha1.Check{ | ||
ID: "privilegeEscalationAllowed", | ||
Severity: "danger", | ||
Category: "Security", | ||
}, | ||
AffectedWorkloads: 10, | ||
}, | ||
{ | ||
Check: v1alpha1.Check{ | ||
ID: "cpuRequestsMissing", | ||
Severity: "warning", | ||
Category: "Efficiency", | ||
}, | ||
AffectedWorkloads: 8, | ||
}, | ||
{ | ||
Check: v1alpha1.Check{ | ||
ID: "insecureCapabilities", | ||
Severity: "warning", | ||
Category: "Security", | ||
}, | ||
AffectedWorkloads: 5, | ||
}, | ||
{ | ||
Check: v1alpha1.Check{ | ||
ID: "livenessProbeMissing", | ||
Severity: "warning", | ||
Category: "Reliability", | ||
}, | ||
AffectedWorkloads: 5, | ||
}, | ||
}, checks) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.