-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
summary.go
216 lines (191 loc) · 5.7 KB
/
summary.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package report
import (
"fmt"
"io"
"sort"
"strconv"
"strings"
"golang.org/x/exp/slices"
"golang.org/x/xerrors"
"github.com/aquasecurity/table"
dbTypes "github.com/aquasecurity/trivy-db/pkg/types"
pkgReport "github.com/aquasecurity/trivy/pkg/report/table"
"github.com/aquasecurity/trivy/pkg/types"
)
type SummaryWriter struct {
Output io.Writer
Severities []string
SeverityHeadings []string
ColumnsHeading []string
}
func NewSummaryWriter(output io.Writer, requiredSevs []dbTypes.Severity, columnHeading []string) SummaryWriter {
var severities []string
var severityHeadings []string
severities, severityHeadings = getRequiredSeverities(requiredSevs)
return SummaryWriter{
Output: output,
Severities: severities,
SeverityHeadings: severityHeadings,
ColumnsHeading: columnHeading,
}
}
func ColumnHeading(scanners types.Scanners, components, availableColumns []string) []string {
columns := []string{
NamespaceColumn,
ResourceColumn,
}
securityOptions := make(map[string]interface{}, 0)
//maintain column order (vuln,config,secret)
for _, check := range scanners {
switch check {
case types.VulnerabilityScanner:
securityOptions[VulnerabilitiesColumn] = nil
case types.MisconfigScanner:
if slices.Contains(components, workloadComponent) {
securityOptions[MisconfigurationsColumn] = nil
}
if slices.Contains(components, infraComponent) {
securityOptions[InfraAssessmentColumn] = nil
}
case types.SecretScanner:
securityOptions[SecretsColumn] = nil
case types.RBACScanner:
securityOptions[RbacAssessmentColumn] = nil
}
}
for _, col := range availableColumns {
if _, ok := securityOptions[col]; ok {
columns = append(columns, col)
}
}
return columns
}
// Write writes the results in a summarized table format
func (s SummaryWriter) Write(report Report) error {
// no report column to print
if len(s.ColumnsHeading) == 2 {
return nil
}
consolidated := report.consolidate()
if _, err := fmt.Fprintln(s.Output); err != nil {
return xerrors.Errorf("failed to write summary report: %w", err)
}
if _, err := fmt.Fprintln(s.Output, report.name); err != nil {
return xerrors.Errorf("failed to write summary report title: %w", err)
}
t := table.New(s.Output)
t.SetRowLines(false)
configureHeader(s, t, s.ColumnsHeading)
sort.Slice(consolidated.Findings, func(i, j int) bool {
return consolidated.Findings[i].Namespace > consolidated.Findings[j].Namespace
})
for _, finding := range consolidated.Findings {
if !finding.Results.Failed() {
continue
}
vCount, mCount, sCount := accumulateSeverityCounts(finding)
name := fmt.Sprintf("%s/%s", finding.Kind, finding.Name)
rowParts := []string{
finding.Namespace,
name,
}
if slices.Contains(s.ColumnsHeading, VulnerabilitiesColumn) {
rowParts = append(rowParts, s.generateSummary(vCount)...)
}
if slices.Contains(s.ColumnsHeading, MisconfigurationsColumn) ||
slices.Contains(s.ColumnsHeading, RbacAssessmentColumn) ||
slices.Contains(s.ColumnsHeading, InfraAssessmentColumn) {
rowParts = append(rowParts, s.generateSummary(mCount)...)
}
if slices.Contains(s.ColumnsHeading, SecretsColumn) {
rowParts = append(rowParts, s.generateSummary(sCount)...)
}
t.AddRow(rowParts...)
}
t.Render()
keyParts := []string{"Severities:"}
for _, s := range s.Severities {
keyParts = append(keyParts, fmt.Sprintf("%s=%s", s[:1], pkgReport.ColorizeSeverity(s, s)))
}
_, _ = fmt.Fprintln(s.Output, strings.Join(keyParts, " "))
_, _ = fmt.Fprintln(s.Output)
return nil
}
func (s SummaryWriter) generateSummary(sevCount map[string]int) []string {
var parts []string
for _, sev := range s.Severities {
if count, ok := sevCount[sev]; ok {
parts = append(parts, pkgReport.ColorizeSeverity(strconv.Itoa(count), sev))
} else {
parts = append(parts, " ")
}
}
return parts
}
func getRequiredSeverities(requiredSevs []dbTypes.Severity) ([]string, []string) {
requiredSevOrder := []dbTypes.Severity{
dbTypes.SeverityCritical,
dbTypes.SeverityHigh,
dbTypes.SeverityMedium,
dbTypes.SeverityLow,
dbTypes.SeverityUnknown,
}
var severities []string
var severityHeadings []string
for _, sev := range requiredSevOrder {
for _, p := range requiredSevs {
if p == sev {
severities = append(severities, sev.String())
severityHeadings = append(severityHeadings, strings.ToUpper(sev.String()[:1]))
continue
}
}
}
return severities, severityHeadings
}
func accumulateSeverityCounts(finding Resource) (map[string]int, map[string]int, map[string]int) {
vCount := make(map[string]int)
mCount := make(map[string]int)
sCount := make(map[string]int)
for _, r := range finding.Results {
for _, rv := range r.Vulnerabilities {
vCount[rv.Severity] = vCount[rv.Severity] + 1
}
for _, rv := range r.Misconfigurations {
mCount[rv.Severity] = mCount[rv.Severity] + 1
}
for _, rv := range r.Secrets {
sCount[rv.Severity] = sCount[rv.Severity] + 1
}
}
return vCount, mCount, sCount
}
func configureHeader(s SummaryWriter, t *table.Table, columnHeading []string) {
sevCount := len(s.Severities)
if len(columnHeading) > 2 {
headerRow := []string{
columnHeading[0],
columnHeading[1],
}
// vulnerabilities headings
count := len(columnHeading) - len(headerRow)
colSpan := []int{
1,
1,
}
headerAlignment := []table.Alignment{
table.AlignLeft,
table.AlignLeft,
}
for i := 0; i < count; i++ {
headerRow = append(headerRow, s.SeverityHeadings...)
colSpan = append(colSpan, sevCount)
headerAlignment = append(headerAlignment, table.AlignCenter)
}
t.SetHeaders(columnHeading...)
t.AddHeaders(headerRow...)
t.SetAlignment(headerAlignment...)
t.SetAutoMergeHeaders(true)
t.SetHeaderColSpans(0, colSpan...)
}
}