-
Notifications
You must be signed in to change notification settings - Fork 138
/
main.go
149 lines (127 loc) · 3.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
package main
import (
"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 name: %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")
}
vsNumber := 0
numVulnerabilites := len(vs)
vs = filterWhitelist(whitelist, vs, image.Name)
numVulnerabilitiesAfterWhitelist := len(vs)
groupBySeverity(vs)
if conf.JSONOutput {
vsNumber = jsonFormat(conf, 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))
switch style := conf.FormatStyle; style {
case "table":
vsNumber = tableFormat(conf, vs)
default:
vsNumber = standardFormat(conf, vs)
}
}
if vsNumber > conf.Threshold {
os.Exit(1)
}
}
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
}