forked from neuvector/neuvector
/
scan.go
333 lines (291 loc) · 8.07 KB
/
scan.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
package scan
import (
"fmt"
"net/url"
"regexp"
"strconv"
"strings"
"time"
log "github.com/sirupsen/logrus"
"github.com/neuvector/neuvector/controller/api"
"github.com/neuvector/neuvector/controller/kv"
"github.com/neuvector/neuvector/controller/resource"
"github.com/neuvector/neuvector/controller/scheduler"
"github.com/neuvector/neuvector/share"
"github.com/neuvector/neuvector/share/cluster"
"github.com/neuvector/neuvector/share/global"
"github.com/neuvector/neuvector/share/utils"
)
var clusHelper kv.ClusterHelper
type Context struct {
AuditQueue cluster.ObjectQueueInterface
ScanChan chan *resource.Event
TimerWheel *utils.TimerWheel
ScanLog *log.Logger
MutexLog *log.Logger
}
type scanMethod struct {
isLeader bool
isScanner bool
db share.CLUSScannerDB
auditQueue cluster.ObjectQueueInterface
timerWheel *utils.TimerWheel
scanChan chan *resource.Event
scanLog *log.Logger
mutexLog *log.Logger
httpProxy string
httpsProxy string
}
var smd *scanMethod
func isScanner() bool {
return smd.isScanner
}
// count vul. with the consideration of vul. profile (alives)
func countVuln(vuls []*share.ScanVulnerability, alives utils.Set) ([]string, []string, int, float32, map[string]map[string]share.CLUSScannedVulInfo, []share.CLUSScannedVulInfoSimple) {
highs := make([]string, 0)
meds := make([]string, 0)
var highWithFix, others int
var scoreTemp int
for _, v := range vuls {
if !alives.Contains(v.Name) {
continue
}
if v.Severity == share.VulnSeverityHigh {
highs = append(highs, v.Name)
if v.FixedVersion != "" {
highWithFix++
}
} else if v.Severity == share.VulnSeverityMedium {
meds = append(meds, v.Name)
} else {
others++
}
scoreTemp += int(10 * v.Score)
}
highVulPublishDate := make(map[string]share.CLUSScannedVulInfo, len(highs))
mediumVulPublishDate := make(map[string]share.CLUSScannedVulInfo, len(meds))
otherVuls := make([]share.CLUSScannedVulInfoSimple, others)
var targetMap map[string]share.CLUSScannedVulInfo
var low bool
var idxLow int
var score float32
for _, v := range vuls {
if !alives.Contains(v.Name) {
continue
}
low = false
targetMap = nil
score = v.Score
if v.ScoreV3 > v.Score {
score = v.ScoreV3
}
if v.Severity == share.VulnSeverityHigh {
targetMap = highVulPublishDate
} else if v.Severity == share.VulnSeverityMedium {
targetMap = mediumVulPublishDate
} else {
low = true
}
if targetMap != nil {
withFix := false
if v.FixedVersion != "" {
withFix = true
}
n, err := strconv.ParseInt(v.PublishedDate, 10, 64)
if err != nil {
// if vul's publish date is unavailable, treat it as 2 yeaars ago
n = time.Now().UTC().AddDate(-2, 0, 0).Unix()
}
name := fmt.Sprintf("%s::%s", v.Name, v.PackageName)
targetMap[name] = share.CLUSScannedVulInfo{
PublishDate: n,
WithFix: withFix,
Score: score,
}
} else if low {
otherVuls[idxLow] = share.CLUSScannedVulInfoSimple{Score: score}
idxLow++
}
}
vulPublishDate := map[string]map[string]share.CLUSScannedVulInfo{
share.VulnSeverityHigh: highVulPublishDate,
share.VulnSeverityMedium: mediumVulPublishDate,
}
s := fmt.Sprintf("%d.%s", scoreTemp/10, strconv.Itoa(scoreTemp%10))
totalScore, _ := strconv.ParseFloat(s, 32)
return highs, meds, highWithFix, float32(totalScore), vulPublishDate, otherVuls
}
func imageWatcher() {
for {
select {
case ev := <-smd.scanChan:
smd.scanLog.WithFields(log.Fields{"event": ev.Event, "type": ev.ResourceType}).Debug("Event received")
if ev.ResourceNew == nil && ev.ResourceOld == nil {
break
}
switch ev.ResourceType {
case resource.RscTypeImage:
var n, o *resource.Image
if ev.ResourceNew != nil {
n = ev.ResourceNew.(*resource.Image)
}
if ev.ResourceOld != nil {
o = ev.ResourceOld.(*resource.Image)
}
if n != nil {
imageBankUpdate(n)
} else if n == nil && o != nil {
imageBankDelete(o)
}
}
}
}
}
func ScannerChangeNotify(isScanner bool) {
log.WithFields(log.Fields{"scanner": isScanner}).Info()
smd.isScanner = isScanner
if isScanner {
becomeScanner()
}
}
func LeadChangeNotify(isLeader bool) {
log.WithFields(log.Fields{"leader": isLeader}).Info()
smd.isLeader = isLeader
}
func AddScanner(id string) {
log.WithFields(log.Fields{"id": id}).Info()
if regScher == nil {
regScher = &scheduler.Schd{}
regScher.Init()
}
regScher.AddProcessor(id)
}
func RemoveScanner(id string) {
log.WithFields(log.Fields{"id": id}).Info()
if regScher != nil {
regScher.DelProcessor(id)
}
}
// used internally, so access control
func GetRegistryCount() int {
regReadLock()
defer regReadUnlock()
return len(regMap)
}
func ScannerDBChange(db *share.CLUSScannerDB) {
log.WithFields(log.Fields{"old": smd.db.CVEDBVersion, "new": db.CVEDBVersion}).Debug("")
oldVer := smd.db.CVEDBVersion
smd.db = *db
// rescan registries. Skip if this is first time scanner is registered, because it is
// likely the controller just starts, we don't want to scan starts automatically.
if oldVer != "" && oldVer != db.CVEDBVersion && isScanner() {
regs := regMapToArray()
for _, reg := range regs {
if reg.config.RescanImage {
reg.stateLock()
state := clusHelper.GetRegistryState(reg.config.Name)
if state != nil {
if state.Status == api.RegistryStatusScanning {
if reg.sctx != nil {
reg.stopScan()
}
reg.startScan()
} else {
smd.scanLog.WithFields(log.Fields{"registry": reg.config.Name}).Debug("CVE Database updated. Start re-scan")
state := &share.CLUSRegistryState{Status: api.RegistryStatusScanning, StartedAt: time.Now().Unix()}
clusHelper.PutRegistryState(reg.config.Name, state)
}
}
reg.stateUnlock()
}
}
}
}
func Init(ctx *Context, leader bool) ScanInterface {
log.Info()
if smd == nil {
smd = &scanMethod{
auditQueue: ctx.AuditQueue,
scanChan: ctx.ScanChan,
scanLog: ctx.ScanLog,
mutexLog: ctx.MutexLog,
timerWheel: ctx.TimerWheel,
isLeader: leader,
}
} else {
smd.auditQueue = ctx.AuditQueue
smd.scanChan = ctx.ScanChan
smd.scanLog = ctx.ScanLog
smd.mutexLog = ctx.MutexLog
smd.timerWheel = ctx.TimerWheel
smd.isLeader = leader
}
clusHelper = kv.GetClusterHelper()
registryInit()
go imageWatcher()
return smd
}
func parseProxy(proxy *share.CLUSProxy) string {
if proxy != nil && proxy.Enable {
url, err := url.Parse(proxy.URL)
if err != nil {
return ""
}
if proxy.Username != "" {
return fmt.Sprintf("%s://%s:%s@%s:%s/",
url.Scheme, proxy.Username, proxy.Password, url.Hostname(), url.Port())
} else {
return fmt.Sprintf("%s://%s:%s/",
url.Scheme, url.Hostname(), url.Port())
}
}
return ""
}
func UpdateProxy(httpProxy, httpsProxy *share.CLUSProxy) {
log.WithFields(log.Fields{"http": httpProxy, "https": httpsProxy}).Debug()
if smd == nil {
smd = &scanMethod{
httpProxy: parseProxy(httpProxy),
httpsProxy: parseProxy(httpsProxy),
}
} else {
smd.httpProxy = parseProxy(httpProxy)
smd.httpsProxy = parseProxy(httpsProxy)
}
}
func GetProxy(registry string) string {
u, err := url.Parse(registry)
if err != nil {
smd.scanLog.WithFields(log.Fields{"error": err}).Error()
return ""
}
// Check if proxy should be skipped for the registry URL
var httpProxy, httpsProxy, noProxy string
if global.RT != nil { // in case of unitest
httpProxy, httpsProxy, noProxy = global.RT.GetProxy()
noProxyHosts := strings.Split(noProxy, ",")
for _, noProxyHost := range noProxyHosts {
if noProxyHost != "" {
noProxyHost = strings.Replace(noProxyHost, "*", ".*", -1)
if regx, err := regexp.Compile(noProxyHost); err == nil && regx.MatchString(u.Hostname()) {
smd.scanLog.WithFields(log.Fields{"hostname": u.Hostname(), "noProxyHost": noProxyHost}).Debug("No need proxy")
return ""
}
}
}
}
// Return configured proxy if enabled, otherwise return container runtime's settings
if u.Scheme == "https" {
if smd.httpsProxy != "" {
return smd.httpsProxy
} else {
return httpsProxy
}
} else {
if smd.httpProxy != "" {
return smd.httpProxy
} else {
return httpProxy
}
}
}