-
Notifications
You must be signed in to change notification settings - Fork 524
/
presenter.go
141 lines (115 loc) · 3.37 KB
/
presenter.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
package table
import (
"fmt"
"io"
"sort"
"strings"
"github.com/olekukonko/tablewriter"
grypeDb "github.com/anchore/grype/grype/db/v5"
"github.com/anchore/grype/grype/match"
"github.com/anchore/grype/grype/pkg"
"github.com/anchore/grype/grype/presenter/models"
"github.com/anchore/grype/grype/vulnerability"
)
const (
appendSuppressed = " (suppressed)"
)
// Presenter is a generic struct for holding fields needed for reporting
type Presenter struct {
results match.Matches
ignoredMatches []match.IgnoredMatch
packages []pkg.Package
metadataProvider vulnerability.MetadataProvider
}
// NewPresenter is a *Presenter constructor
func NewPresenter(pb models.PresenterConfig) *Presenter {
return &Presenter{
results: pb.Matches,
ignoredMatches: pb.IgnoredMatches,
packages: pb.Packages,
metadataProvider: pb.MetadataProvider,
}
}
// Present creates a JSON-based reporting
func (pres *Presenter) Present(output io.Writer) error {
rows := make([][]string, 0)
columns := []string{"Name", "Installed", "Fixed-In", "Type", "Vulnerability", "Severity"}
// Generate rows for matching vulnerabilities
for m := range pres.results.Enumerate() {
row, err := createRow(m, pres.metadataProvider, "")
if err != nil {
return err
}
rows = append(rows, row)
}
// Generate rows for suppressed vulnerabilities
for _, m := range pres.ignoredMatches {
row, err := createRow(m.Match, pres.metadataProvider, appendSuppressed)
if err != nil {
return err
}
rows = append(rows, row)
}
if len(rows) == 0 {
_, err := io.WriteString(output, "No vulnerabilities found\n")
return err
}
// sort by name, version, then type
sort.SliceStable(rows, func(i, j int) bool {
for col := 0; col < len(columns); col++ {
if rows[i][col] != rows[j][col] {
return rows[i][col] < rows[j][col]
}
}
return false
})
rows = removeDuplicateRows(rows)
table := tablewriter.NewWriter(output)
table.SetHeader(columns)
table.SetAutoWrapText(false)
table.SetHeaderAlignment(tablewriter.ALIGN_LEFT)
table.SetAlignment(tablewriter.ALIGN_LEFT)
table.SetHeaderLine(false)
table.SetBorder(false)
table.SetAutoFormatHeaders(true)
table.SetCenterSeparator("")
table.SetColumnSeparator("")
table.SetRowSeparator("")
table.SetTablePadding(" ")
table.SetNoWhiteSpace(true)
table.AppendBulk(rows)
table.Render()
return nil
}
func removeDuplicateRows(items [][]string) [][]string {
seen := map[string][]string{}
var result [][]string
for _, v := range items {
key := strings.Join(v, "|")
if seen[key] != nil {
// dup!
continue
}
seen[key] = v
result = append(result, v)
}
return result
}
func createRow(m match.Match, metadataProvider vulnerability.MetadataProvider, severitySuffix string) ([]string, error) {
var severity string
metadata, err := metadataProvider.GetMetadata(m.Vulnerability.ID, m.Vulnerability.Namespace)
if err != nil {
return nil, fmt.Errorf("unable to fetch vuln=%q metadata: %+v", m.Vulnerability.ID, err)
}
if metadata != nil {
severity = metadata.Severity + severitySuffix
}
fixVersion := strings.Join(m.Vulnerability.Fix.Versions, ", ")
switch m.Vulnerability.Fix.State {
case grypeDb.WontFixState:
fixVersion = "(won't fix)"
case grypeDb.UnknownFixState:
fixVersion = ""
}
return []string{m.Package.Name, m.Package.Version, fixVersion, string(m.Package.Type), m.Vulnerability.ID, severity}, nil
}