-
Notifications
You must be signed in to change notification settings - Fork 138
/
format.go
137 lines (119 loc) · 2.98 KB
/
format.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
package main
import (
"encoding/json"
"fmt"
"os"
"github.com/olekukonko/tablewriter"
"github.com/optiopay/klar/clair"
)
var SeverityStyle = map[string]string{
"Defcon1": "\033[1;31m%s\033[0m",
"Critical": "\033[1;31m%s\033[0m",
"High": "\033[0;31m%s\033[0m",
"Medium": "\033[0;33m%s\033[0m",
"Low": "\033[0;94m%s\033[0m",
"Negligible": "\033[0;94m%s\033[0m",
"Unknown": "\033[0;97m%s\033[0m",
}
func getSeverityStyle(status string) string {
if val, ok := SeverityStyle[status]; ok {
// Return matched style
return fmt.Sprintf(val, status)
}
// Return default style Unknown if not matched
return fmt.Sprintf(SeverityStyle["Unknown"], status)
}
func standardFormat(conf *config, vs []*clair.Vulnerability) int {
vsNumber := 0
iteratePriorities(priorities[0], func(sev string) { fmt.Printf("%s: %d\n", sev, len(store[sev])) })
fmt.Printf("\n")
iteratePriorities(conf.ClairOutput, func(sev string) {
for _, v := range store[sev] {
fmt.Printf("%s: [%s] \nFound in: %s [%s]\nFixed By: %s\n%s\n%s\n", v.Name, v.Severity, v.FeatureName,
v.FeatureVersion, v.FixedBy, v.Description, v.Link)
fmt.Println("-----------------------------------------")
if conf.IgnoreUnfixed {
if v.FixedBy != "" {
vsNumber++
}
} else {
vsNumber++
}
}
})
return vsNumber
}
func jsonFormat(conf *config, output jsonOutput) int {
vsNumber := 0
iteratePriorities(conf.ClairOutput, func(sev string) {
if conf.IgnoreUnfixed {
// need to iterate over store[sev]
for _, v := range store[sev] {
if v.FixedBy != "" {
vsNumber++
}
}
} else {
vsNumber += len(store[sev])
}
output.Vulnerabilities[sev] = store[sev]
})
enc := json.NewEncoder(os.Stdout)
enc.Encode(output)
return vsNumber
}
func tableFormat(conf *config, vs []*clair.Vulnerability) int {
vsNumber := 0
iteratePriorities(priorities[0], func(sev string) { fmt.Printf("%s: %d\n", sev, len(store[sev])) })
fmt.Printf("\n")
table := tablewriter.NewWriter(os.Stdout)
header := []string{
"Severity", "Name", "FeatureName", "FeatureVersion", "FixedBy", "Description", "Link",
}
table.SetHeader(header)
table.SetHeaderAlignment(tablewriter.ALIGN_LEFT)
table.SetRowSeparator("-")
table.SetRowLine(true)
table.SetAlignment(tablewriter.ALIGN_LEFT)
var data [][]string
iteratePriorities(conf.ClairOutput, func(sev string) {
for _, v := range store[sev] {
data = append(data, []string{
getSeverityStyle(v.Severity),
v.Name,
v.FeatureName,
v.FeatureVersion,
v.FixedBy,
v.Description,
v.Link,
})
if conf.IgnoreUnfixed {
if v.FixedBy != "" {
vsNumber++
}
} else {
vsNumber++
}
}
})
table.AppendBulk(data)
if len(data) > 0 {
table.Render()
}
return vsNumber
}
func iteratePriorities(output string, f func(sev string)) {
filtered := true
for _, sev := range priorities {
if filtered {
if sev != output {
continue
} else {
filtered = false
}
}
if len(store[sev]) != 0 {
f(sev)
}
}
}