From b371fbad0128a8bfaac8e562e7c398a8003fbe73 Mon Sep 17 00:00:00 2001 From: dwertent Date: Tue, 19 Oct 2021 15:08:00 +0300 Subject: [PATCH] adding summary --- docs/run-options.md | 2 +- resultshandling/printer/summary.go | 23 ++++++++++++++++------- resultshandling/printer/summeryhelpers.go | 14 ++++++++------ 3 files changed, 25 insertions(+), 14 deletions(-) diff --git a/docs/run-options.md b/docs/run-options.md index 4a7a0a80..8a94afc0 100644 --- a/docs/run-options.md +++ b/docs/run-options.md @@ -51,7 +51,7 @@ kubescape scan framework nsa --exclude-namespaces kube-system,kube-public --form kubescape scan framework nsa --exclude-namespaces kube-system,kube-public --format junit --output results.xml ``` -* Scan with exceptions, objects with exceptions will be presented as `warning` and not `fail` +* Scan with exceptions, objects with exceptions will be presented as `warning` and not `fail` ``` kubescape scan framework nsa --exceptions examples/exceptions.json ``` diff --git a/resultshandling/printer/summary.go b/resultshandling/printer/summary.go index 1bc90911..9df245f6 100644 --- a/resultshandling/printer/summary.go +++ b/resultshandling/printer/summary.go @@ -13,13 +13,14 @@ func NewSummary() Summary { } type ControlSummary struct { - TotalResources int - TotalFailed int - TotalWarnign int - Description string - Remediation string - ListInputKinds []string - WorkloadSummary map[string][]WorkloadSummary // :[] + TotalResources int + TotalFailed int + TotalWarnign int + Description string + Remediation string + ListInputKinds []string + FailedWorkloads map[string][]WorkloadSummary // :[] + ExcludedWorkloads map[string][]WorkloadSummary // :[] } type WorkloadSummary struct { @@ -41,3 +42,11 @@ func (controlSummary *ControlSummary) ToSlice() []string { func (workloadSummary *WorkloadSummary) ToString() string { return fmt.Sprintf("/%s/%s/%s/%s", workloadSummary.Group, workloadSummary.Namespace, workloadSummary.Kind, workloadSummary.Name) } + +func workloadSummaryFailed(workloadSummary *WorkloadSummary) bool { + return workloadSummary.Exception == nil +} + +func workloadSummaryExclude(workloadSummary *WorkloadSummary) bool { + return workloadSummary.Exception != nil && workloadSummary.Exception.IsAlertOnly() +} diff --git a/resultshandling/printer/summeryhelpers.go b/resultshandling/printer/summeryhelpers.go index 3cd11edb..cf7a4e43 100644 --- a/resultshandling/printer/summeryhelpers.go +++ b/resultshandling/printer/summeryhelpers.go @@ -8,14 +8,16 @@ import ( ) // Group workloads by namespace - return {"namespace": <[]WorkloadSummary>} -func groupByNamespace(resources []WorkloadSummary) map[string][]WorkloadSummary { +func groupByNamespace(resources []WorkloadSummary, status func(workloadSummary *WorkloadSummary) bool) map[string][]WorkloadSummary { mapResources := make(map[string][]WorkloadSummary) for i := range resources { - if r, ok := mapResources[resources[i].Namespace]; ok { - r = append(r, resources[i]) - mapResources[resources[i].Namespace] = r - } else { - mapResources[resources[i].Namespace] = []WorkloadSummary{resources[i]} + if status(&resources[i]) { + if r, ok := mapResources[resources[i].Namespace]; ok { + r = append(r, resources[i]) + mapResources[resources[i].Namespace] = r + } else { + mapResources[resources[i].Namespace] = []WorkloadSummary{resources[i]} + } } } return mapResources