forked from optiopay/klar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
96 lines (80 loc) · 2.13 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
package main
import (
"fmt"
"os"
"strconv"
"github.com/optiopay/klar/clair"
"github.com/optiopay/klar/docker"
)
var priorities = []string{"Unknown", "Negligible", "Low", "Medium", "High", "Critical", "Defcon1"}
var store = make(map[string][]clair.Vulnerability)
func main() {
if len(os.Args) != 2 {
fmt.Printf("Image name must be provided")
os.Exit(1)
}
clairAddr := os.Getenv("CLAIR_ADDR")
if clairAddr == "" {
fmt.Printf("Clair address must be provided")
os.Exit(1)
}
threshold := 0
thresholdStr := os.Getenv("CLAIR_THRESHOLD")
if thresholdStr != "" {
threshold, _ = strconv.Atoi(thresholdStr)
}
dockerUser := os.Getenv("DOCKER_USER")
dockerPassword := os.Getenv("DOCKER_PASSWORD")
image, err := docker.NewImage(os.Args[1], dockerUser, dockerPassword)
if err != nil {
fmt.Printf("Can't parse qname: %s", err)
os.Exit(1)
}
err = image.Pull()
if err != nil {
fmt.Printf("Can't pull image: %s", err)
os.Exit(1)
}
if len(image.FsLayers) == 0 {
fmt.Printf("Can't pull fsLayers")
os.Exit(1)
} else {
fmt.Printf("Analysing %d layers\n", len(image.FsLayers))
}
c := clair.NewClair(clairAddr)
vs := c.Analyse(image)
groupBySeverity(vs)
fmt.Printf("Found %d vulnerabilities \n", len(vs))
highSevNumber := len(store["High"]) + len(store["Critical"]) + len(store["Defcon1"])
iteratePriorities(func(sev string) {
for _, v := range store[sev] {
fmt.Printf("%s: [%s] \n%s\n%s\n", v.Name, v.Severity, v.Description, v.Link)
fmt.Println("-----------------------------------------")
}
})
iteratePriorities(func(sev string) { fmt.Printf("%s: %d\n", sev, len(store[sev])) })
if highSevNumber > threshold {
os.Exit(1)
}
}
func iteratePriorities(f func(sev string)) {
for _, sev := range priorities {
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
}