diff --git a/pkg/k8s/commands/cluster.go b/pkg/k8s/commands/cluster.go index 06bc143efa..03c7c7b56d 100644 --- a/pkg/k8s/commands/cluster.go +++ b/pkg/k8s/commands/cluster.go @@ -22,5 +22,5 @@ func clusterRun(ctx context.Context, opts flag.Options, cluster k8s.Cluster) err return xerrors.Errorf("get k8s artifacts error: %w", err) } - return run(ctx, opts, cluster.GetCurrentContext(), artifacts) + return run(ctx, opts, cluster.GetCurrentContext(), artifacts, true) } diff --git a/pkg/k8s/commands/namespace.go b/pkg/k8s/commands/namespace.go index a967dc1261..16072c68ff 100644 --- a/pkg/k8s/commands/namespace.go +++ b/pkg/k8s/commands/namespace.go @@ -24,7 +24,7 @@ func namespaceRun(ctx context.Context, opts flag.Options, cluster k8s.Cluster) e return xerrors.Errorf("get k8s artifacts error: %w", err) } - return run(ctx, opts, cluster.GetCurrentContext(), artifacts) + return run(ctx, opts, cluster.GetCurrentContext(), artifacts, true) } func getNamespace(opts flag.Options, currentNamespace string) string { diff --git a/pkg/k8s/commands/resource.go b/pkg/k8s/commands/resource.go index c727e7b3d4..4ae8dcaf4f 100644 --- a/pkg/k8s/commands/resource.go +++ b/pkg/k8s/commands/resource.go @@ -33,7 +33,7 @@ func resourceRun(ctx context.Context, args []string, opts flag.Options, cluster return err } - return run(ctx, opts, cluster.GetCurrentContext(), targets) + return run(ctx, opts, cluster.GetCurrentContext(), targets, false) } // pod/NAME or pod NAME etc @@ -42,7 +42,7 @@ func resourceRun(ctx context.Context, args []string, opts flag.Options, cluster return err } - return run(ctx, opts, cluster.GetCurrentContext(), []*artifacts.Artifact{artifact}) + return run(ctx, opts, cluster.GetCurrentContext(), []*artifacts.Artifact{artifact}, false) } func extractKindAndName(args []string) (string, string, error) { diff --git a/pkg/k8s/commands/run.go b/pkg/k8s/commands/run.go index 49f92c8d7f..faae176c91 100644 --- a/pkg/k8s/commands/run.go +++ b/pkg/k8s/commands/run.go @@ -41,7 +41,7 @@ func Run(ctx context.Context, args []string, opts flag.Options) error { } } -func run(ctx context.Context, opts flag.Options, cluster string, artifacts []*artifacts.Artifact) error { +func run(ctx context.Context, opts flag.Options, cluster string, artifacts []*artifacts.Artifact, showEmpty bool) error { ctx, cancel := context.WithTimeout(ctx, opts.Timeout) defer cancel() @@ -76,7 +76,7 @@ func run(ctx context.Context, opts flag.Options, cluster string, artifacts []*ar Report: opts.ReportFormat, Output: opts.Output, Severities: opts.Severities, - }, opts.ScanOptions.SecurityChecks); err != nil { + }, opts.ScanOptions.SecurityChecks, showEmpty); err != nil { return xerrors.Errorf("unable to write results: %w", err) } diff --git a/pkg/k8s/report/report.go b/pkg/k8s/report/report.go index d8b24e1de2..d7651a7002 100644 --- a/pkg/k8s/report/report.go +++ b/pkg/k8s/report/report.go @@ -83,6 +83,10 @@ func (r Report) Failed() bool { return false } +func (r Report) empty() bool { + return len(r.Misconfigurations) == 0 && len(r.Vulnerabilities) == 0 +} + func (r Report) consolidate() ConsolidatedReport { consolidated := ConsolidatedReport{ SchemaVersion: r.SchemaVersion, @@ -124,30 +128,38 @@ type Writer interface { } // Write writes the results in the give format -func Write(report Report, option Option, securityChecks []string) error { +func Write(report Report, option Option, securityChecks []string, showEmpty bool) error { switch option.Format { case jsonFormat: jwriter := JSONWriter{Output: option.Output, Report: option.Report} return jwriter.Write(report) case tableFormat: workloadReport, rbacReport := separateMisConfigRoleAssessment(report, securityChecks) - WorkloadWriter := &TableWriter{ - Output: option.Output, - Report: option.Report, - Severities: option.Severities, - ColumnHeading: ColumnHeading(securityChecks, WorkloadColumns()), + + if !workloadReport.empty() || showEmpty { + WorkloadWriter := &TableWriter{ + Output: option.Output, + Report: option.Report, + Severities: option.Severities, + ColumnHeading: ColumnHeading(securityChecks, WorkloadColumns()), + } + err := WorkloadWriter.Write(workloadReport) + if err != nil { + return err + } } - err := WorkloadWriter.Write(workloadReport) - if err != nil { - return err + + if !rbacReport.empty() || showEmpty { + rbacWriter := &TableWriter{ + Output: option.Output, + Report: option.Report, + Severities: option.Severities, + ColumnHeading: ColumnHeading(securityChecks, RoleColumns()), + } + return rbacWriter.Write(rbacReport) } - rbacWriter := &TableWriter{ - Output: option.Output, - Report: option.Report, - Severities: option.Severities, - ColumnHeading: ColumnHeading(securityChecks, RoleColumns()), - } - return rbacWriter.Write(rbacReport) + + return nil default: return xerrors.Errorf(`unknown format %q. Use "json" or "table"`, option.Format) }