/
clair.go
200 lines (167 loc) · 4.44 KB
/
clair.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
195
196
197
198
199
200
package clair
import (
"errors"
"fmt"
"time"
"github.com/edersonbrilhante/vilicus/pkg/types"
"github.com/edersonbrilhante/vilicus/pkg/util/config"
"github.com/go-resty/resty/v2"
"context"
"encoding/json"
"strings"
"net/http"
"net/url"
"sync"
)
// Clair stores pointers to config and results from scan
type Clair struct {
Config *config.Clair
resultRaw *vulnerabilityReport
analysis *types.Analysis
}
var (
rtMu sync.Mutex
rtMap = map[string]http.RoundTripper{}
)
// Analyzer runs an analysis and stores the results in Clair.resultRaw
func (c *Clair) Analyzer(al *types.Analysis) error {
c.analysis = al
imgResp, err := c.addImage()
if err != nil {
return err
}
_, err = c.monitorResult(imgResp.ManifestHash)
if err != nil {
return err
}
vulnResp, err := c.getVuln(imgResp.ManifestHash)
if err != nil {
return err
}
c.resultRaw = &vulnResp
return nil
}
// Parser parses Clair.resultRaw and store the final data into a type Analysis
func (c *Clair) Parser() error {
if c.resultRaw == nil {
return errors.New("Result is empty")
}
fmt.Printf("%v: found %v vulns\n", c.analysis.Image, len(c.resultRaw.Vulnerabilities))
r := types.VendorResults{}
for p, vis := range c.resultRaw.PackageVulnerabilities {
for _, vi := range vis {
pkg := c.resultRaw.Packages[p]
v := c.resultRaw.Vulnerabilities[vi]
vuln := types.Vuln{
Fix: v.FixedInVersion,
URL: filterValidURLs(strings.Split(v.Links, " ")),
Name: v.Name,
Vendor: "Clair",
Severity: v.NormalizedSeverity,
PackageName: pkg.Name,
PackageVersion: pkg.Version,
}
switch vuln.Severity {
case "Unknown":
r.UnknownVulns = append(r.UnknownVulns, vuln)
case "Negligible":
r.NegligibleVulns = append(r.NegligibleVulns, vuln)
case "Low":
r.LowVulns = append(r.LowVulns, vuln)
case "Medium":
r.MediumVulns = append(r.MediumVulns, vuln)
case "High":
r.HighVulns = append(r.HighVulns, vuln)
case "Critical":
r.CriticalVulns = append(r.CriticalVulns, vuln)
}
}
}
c.analysis.Results.ClairResult = r
return nil
}
func (c *Clair) addImage() (indexReport, error) {
imgResp := indexReport{}
ctx := context.Background()
manifest, err := inspect(ctx, c.analysis.Image)
if err != nil {
return imgResp, err
}
client := resty.New()
resp, err := client.R().
SetBody(manifest).
Post(c.Config.URL + "/indexer/api/v1/index_report")
if err != nil {
return imgResp, err
}
if resp.StatusCode() != 201 {
return imgResp, errors.New("analysis failed: " + resp.String())
}
err = json.Unmarshal(resp.Body(), &imgResp)
return imgResp, err
}
func (c *Clair) monitorResult(rid string) (indexReport, error) {
ireport := indexReport{}
timeout := time.After(60 * time.Minute)
retryTick := time.NewTicker(10 * time.Second)
for {
select {
case <-timeout:
return ireport, errors.New("time out")
case <-retryTick.C:
fmt.Println(ireport.State)
ireport, err := c.getAnalysis(rid)
if err != nil {
return ireport, err
}
if ireport.State == "IndexFinished" {
return ireport, nil
}
if ireport.State == "IndexError" {
return ireport, errors.New("analysis failed: " + ireport.Err)
}
}
}
}
func (c *Clair) getAnalysis(rid string) (indexReport, error) {
client := resty.New()
ireport := indexReport{}
resp, err := client.R().
Get(c.Config.URL + "/indexer/api/v1/index_report/" + rid)
if err != nil {
return ireport, err
}
if resp.StatusCode() != 200 {
return ireport, errors.New("analysis failed: " + resp.String())
}
err = json.Unmarshal(resp.Body(), &ireport)
return ireport, err
}
func (c *Clair) getVuln(rid string) (vulnerabilityReport, error) {
client := resty.New()
client.SetRetryCount(3)
client.AddRetryCondition(resty.RetryConditionFunc(func(r *resty.Response, _ error) bool {
return r.StatusCode() >= http.StatusInternalServerError
}))
vulnResp := vulnerabilityReport{}
resp, err := client.R().
Get(c.Config.URL + "/matcher/api/v1/vulnerability_report/" + rid)
if err != nil {
return vulnResp, err
}
if resp.StatusCode() != 200 {
return vulnResp, errors.New("analysis failed: " + resp.String())
}
err = json.Unmarshal(resp.Body(), &vulnResp)
return vulnResp, err
}
func filterValidURLs(urls []string) []string {
validURLs := []string{}
for _, ur := range urls {
u, err := url.Parse(ur)
if err == nil && u.Scheme != "" && u.Host != "" {
validURLs = append(validURLs, u.String())
}
}
return validURLs
}