-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
table.go
86 lines (75 loc) · 1.91 KB
/
table.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package report
import (
"context"
"fmt"
"io"
"strings"
"golang.org/x/xerrors"
dbTypes "github.com/aquasecurity/trivy-db/pkg/types"
pkgReport "github.com/aquasecurity/trivy/pkg/report/table"
)
type TableWriter struct {
Report string
Output io.Writer
Severities []dbTypes.Severity
ColumnHeading []string
}
const (
NamespaceColumn = "Namespace"
ResourceColumn = "Resource"
VulnerabilitiesColumn = "Vulnerabilities"
MisconfigurationsColumn = "Misconfigurations"
SecretsColumn = "Secrets"
RbacAssessmentColumn = "RBAC Assessment"
)
func WorkloadColumns() []string {
return []string{
VulnerabilitiesColumn,
MisconfigurationsColumn,
SecretsColumn,
}
}
func RoleColumns() []string {
return []string{RbacAssessmentColumn}
}
func InfraColumns() []string {
return []string{
VulnerabilitiesColumn,
MisconfigurationsColumn,
SecretsColumn,
}
}
func (tw TableWriter) Write(ctx context.Context, report Report) error {
switch tw.Report {
case AllReport:
t := pkgReport.Writer{
Output: tw.Output,
Severities: tw.Severities,
}
for i, r := range report.Resources {
if r.Report.Results.Failed() {
updateTargetContext(&report.Resources[i])
err := t.Write(ctx, r.Report)
if err != nil {
return err
}
}
}
case SummaryReport:
writer := NewSummaryWriter(tw.Output, tw.Severities, tw.ColumnHeading)
return writer.Write(report)
default:
return xerrors.Errorf(`report %q not supported. Use "summary" or "all"`, tw.Report)
}
return nil
}
// updateTargetContext add context namespace, kind and name to the target
func updateTargetContext(r *Resource) {
targetName := fmt.Sprintf("namespace: %s, %s: %s", r.Namespace, strings.ToLower(r.Kind), r.Name)
if r.Kind == "NodeComponents" || r.Kind == "NodeInfo" {
targetName = fmt.Sprintf("node: %s", r.Name)
}
for i := range r.Report.Results {
r.Report.Results[i].Target = targetName
}
}