forked from optiopay/klar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
195 lines (168 loc) · 4.53 KB
/
main.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
package main
import (
"encoding/json"
"fmt"
"os"
"github.com/optiopay/klar/clair"
"github.com/optiopay/klar/docker"
)
var store = make(map[string][]*clair.Vulnerability)
func main() {
fail := func(format string, a ...interface{}) {
fmt.Fprintf(os.Stderr, fmt.Sprintf("%s\n", format), a...)
os.Exit(2)
}
if len(os.Args) != 2 {
fail("Image name must be provided")
}
conf, err := newConfig(os.Args)
if err != nil {
fail("Invalid options: %s", err)
}
if !conf.JSONOutput {
fmt.Fprintf(os.Stderr, "clair timeout %s\n", conf.ClairTimeout)
fmt.Fprintf(os.Stderr, "docker timeout: %s\n", conf.DockerConfig.Timeout)
}
whitelist := &vulnerabilitiesWhitelist{}
if conf.WhiteListFile != "" {
if !conf.JSONOutput {
fmt.Fprintf(os.Stderr, "whitelist file: %s\n", conf.WhiteListFile)
}
whitelist, err = parseWhitelistFile(conf.WhiteListFile)
if err != nil {
fail("Could not parse whitelist file: %s", err)
}
} else {
if !conf.JSONOutput {
fmt.Fprintf(os.Stderr, "no whitelist file\n")
}
}
image, err := docker.NewImage(&conf.DockerConfig)
if err != nil {
fail("Can't parse qname: %s", err)
}
err = image.Pull()
if err != nil {
fail("Can't pull image: %s", err)
}
output := jsonOutput{
Vulnerabilities: make(map[string][]*clair.Vulnerability),
}
if len(image.FsLayers) == 0 {
fail("Can't pull fsLayers")
} else {
if conf.JSONOutput {
output.LayerCount = len(image.FsLayers)
} else {
fmt.Printf("Analysing %d layers\n", len(image.FsLayers))
}
}
var vs []*clair.Vulnerability
for _, ver := range []int{1, 3} {
c := clair.NewClair(conf.ClairAddr, ver, conf.ClairTimeout)
vs, err = c.Analyse(image)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to analyze using API v%d: %s\n", ver, err)
} else {
if !conf.JSONOutput {
fmt.Printf("Got results from Clair API v%d\n", ver)
}
break
}
}
if err != nil {
fail("Failed to analyze, exiting")
}
//apply whitelist
numVulnerabilites := len(vs)
vs = filterWhitelist(whitelist, vs, image.Name)
numVulnerabilitiesAfterWhitelist := len(vs)
groupBySeverity(vs)
vsNumber := 0
if conf.JSONOutput {
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)
} else {
if numVulnerabilitiesAfterWhitelist < numVulnerabilites {
//display how many vulnerabilities were whitelisted
fmt.Printf("Whitelisted %d vulnerabilities\n", numVulnerabilites-numVulnerabilitiesAfterWhitelist)
}
fmt.Printf("Found %d vulnerabilities\n", len(vs))
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++
}
}
})
}
if vsNumber > conf.Threshold {
os.Exit(1)
}
}
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)
}
}
}
func groupBySeverity(vs []*clair.Vulnerability) {
for _, v := range vs {
sevRow := vulnsBy(v.Severity, store)
store[v.Severity] = append(sevRow, v)
}
}
func vulnsBy(sev string, store map[string][]*clair.Vulnerability) []*clair.Vulnerability {
items, found := store[sev]
if !found {
items = make([]*clair.Vulnerability, 0)
store[sev] = items
}
return items
}
//Filter out whitelisted vulnerabilites
func filterWhitelist(whitelist *vulnerabilitiesWhitelist, vs []*clair.Vulnerability, imageName string) []*clair.Vulnerability {
generalWhitelist := whitelist.General
imageWhitelist := whitelist.Images
filteredVs := make([]*clair.Vulnerability, 0, len(vs))
for _, v := range vs {
if _, exists := generalWhitelist[v.Name]; !exists {
if _, exists := imageWhitelist[imageName][v.Name]; !exists {
//vulnerability is not in the image whitelist, so add it to the list to return
filteredVs = append(filteredVs, v)
}
}
}
return filteredVs
}