-
Notifications
You must be signed in to change notification settings - Fork 171
/
Copy pathscanner.go
426 lines (357 loc) · 12.7 KB
/
scanner.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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
package scanner
import (
"context"
"fmt"
"sync"
"sync/atomic"
grype_models "github.com/anchore/grype/grype/presenter/models"
uuid "github.com/satori/go.uuid"
log "github.com/sirupsen/logrus"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
dockle_types "github.com/Portshift/dockle/pkg/types"
"github.com/Portshift/klar/docker"
"github.com/Portshift/klar/forwarding"
klar_types "github.com/Portshift/klar/types"
"github.com/cisco-open/kubei/pkg/config"
"github.com/cisco-open/kubei/pkg/scanner/creds"
"github.com/cisco-open/kubei/pkg/types"
k8s_utils "github.com/cisco-open/kubei/pkg/utils/k8s"
slice_utils "github.com/cisco-open/kubei/pkg/utils/slice"
)
type Status string
const (
Idle Status = "Idle"
ScanInit Status = "ScanInit"
ScanInitFailure Status = "ScanInitFailure"
Scanning Status = "Scanning"
)
type Scanner struct {
imageToScanData map[string]*scanData
progress types.ScanProgress
status Status
config *config.Config
scanConfig *config.ScanConfig
killSignal chan bool
clientset kubernetes.Interface
logFields log.Fields
credentialAdders []creds.CredentialAdder
sync.Mutex
}
func CreateScanner(config *config.Config, clientset kubernetes.Interface) *Scanner {
s := &Scanner{
progress: types.ScanProgress{},
status: Idle,
config: config,
killSignal: make(chan bool),
clientset: clientset,
logFields: log.Fields{"scanner id": uuid.NewV4().String()},
credentialAdders: []creds.CredentialAdder{
creds.CreateBasicRegCred(clientset, config.CredsSecretNamespace),
creds.CreateECR(clientset, config.CredsSecretNamespace),
creds.CreateGCR(clientset, config.CredsSecretNamespace),
},
Mutex: sync.Mutex{},
}
return s
}
type imagePodContext struct {
containerName string
podName string
namespace string
imagePullSecret string
imageHash string
podUid string
}
type vulnerabilitiesScanResult struct {
result *grype_models.Document
layerCommands []*docker.FsLayerCommand
success bool
completed bool
scanErr *klar_types.ScanError
}
type dockerfileScanResult struct {
result dockle_types.AssessmentMap
success bool
completed bool
scanErr *dockle_types.ScanError
}
type scanData struct {
imageName string
contexts []*imagePodContext // All the pods that contain this image
scanUUID string
vulnerabilitiesResult vulnerabilitiesScanResult
dockerfileResult dockerfileScanResult
shouldScanDockerfile bool
resultChan chan bool
success bool
completed bool
timeout bool
scanErr *types.ScanError
}
func (sd *scanData) getScanErrors() []*types.ScanError {
var errors []*types.ScanError
if sd.scanErr != nil {
errors = append(errors, sd.scanErr)
}
if sd.vulnerabilitiesResult.scanErr != nil {
errors = append(errors, &types.ScanError{
ErrMsg: sd.vulnerabilitiesResult.scanErr.ErrMsg,
ErrType: string(sd.vulnerabilitiesResult.scanErr.ErrType),
ErrSource: types.ScanErrSourceVul,
})
}
if sd.dockerfileResult.scanErr != nil {
errors = append(errors, &types.ScanError{
ErrMsg: sd.dockerfileResult.scanErr.ErrMsg,
ErrType: string(sd.dockerfileResult.scanErr.ErrType),
ErrSource: types.ScanErrSourceDockle,
})
}
return errors
}
func (sd *scanData) setVulnerabilitiesResult(result *vulnerabilitiesScanResult) {
sd.vulnerabilitiesResult = *result
sd.updateResult()
}
func (sd *scanData) setDockerfileResult(result *dockerfileScanResult) {
sd.dockerfileResult = *result
sd.updateResult()
}
func (sd *scanData) updateResult() {
if sd.vulnerabilitiesResult.completed && (!sd.shouldScanDockerfile || sd.dockerfileResult.completed) {
sd.completed = true
}
if sd.vulnerabilitiesResult.success && (!sd.shouldScanDockerfile || sd.dockerfileResult.success) {
sd.success = true
}
}
const (
ignorePodScanLabelKey = "kubeiShouldScan"
ignorePodScanLabelValue = "false"
)
func (s *Scanner) shouldIgnorePod(pod *corev1.Pod) bool {
if slice_utils.ContainsString(s.scanConfig.IgnoredNamespaces, pod.Namespace) {
log.WithFields(s.logFields).Infof("Skipping pod scan, namespace is in the ignored namespaces list. pod=%v ,namespace=%s", pod.Name, pod.Namespace)
return true
}
if pod.Labels != nil && pod.Labels[ignorePodScanLabelKey] == ignorePodScanLabelValue {
log.WithFields(s.logFields).Infof("Skipping pod scan, pod has an ignore label. pod=%v ,namespace=%s", pod.Name, pod.Namespace)
return true
}
return false
}
func (s *Scanner) initScan() error {
s.status = ScanInit
// Get all target pods
podList, err := s.clientset.CoreV1().Pods(s.scanConfig.TargetNamespace).List(context.TODO(), metav1.ListOptions{})
if err != nil {
return fmt.Errorf("failed to list pods. namespace=%s: %v", s.scanConfig.TargetNamespace, err)
}
imageToScanData := make(map[string]*scanData)
// Populate the image to scanData map from all target pods
for _, pod := range podList.Items {
if s.shouldIgnorePod(&pod) {
continue
}
secrets := k8s_utils.GetPodImagePullSecrets(s.clientset, pod)
// Due to scenarios where image name in the `pod.Status.ContainerStatuses` is different
// from image name in the `pod.Spec.Containers` we will take only image id from `pod.Status.ContainerStatuses`.
containerNameToImageId := make(map[string]string)
for _, container := range pod.Status.ContainerStatuses {
containerNameToImageId[container.Name] = container.ImageID
}
for _, container := range pod.Status.InitContainerStatuses {
containerNameToImageId[container.Name] = container.ImageID
}
containers := append(pod.Spec.Containers, pod.Spec.InitContainers...)
for _, container := range containers {
// Create pod context
podContext := &imagePodContext{
containerName: container.Name,
podName: pod.GetName(),
podUid: string(pod.GetUID()),
namespace: pod.GetNamespace(),
imagePullSecret: k8s_utils.GetMatchingSecretName(secrets, container.Image),
imageHash: getImageHash(containerNameToImageId, container),
}
if data, ok := imageToScanData[container.Image]; !ok {
// Image added for the first time, create scan data and append pod context
imageToScanData[container.Image] = &scanData{
imageName: container.Image,
contexts: []*imagePodContext{podContext},
scanUUID: uuid.NewV4().String(),
shouldScanDockerfile: s.scanConfig.ShouldScanDockerFile,
resultChan: make(chan bool),
}
} else {
// Image already exist in map, just append the pod context
data.contexts = append(data.contexts, podContext)
}
}
}
s.imageToScanData = imageToScanData
s.progress = types.ScanProgress{
ImagesToScan: uint32(len(imageToScanData)),
ImagesStartedToScan: 0,
ImagesCompletedToScan: 0,
}
log.WithFields(s.logFields).Infof("Total %d unique images to scan", s.progress.ImagesToScan)
return nil
}
func getImageHash(containerNameToImageId map[string]string, container corev1.Container) string {
imageID, ok := containerNameToImageId[container.Name]
if !ok {
log.Warnf("Image id is missing. container=%v ,image=%v", container.Name, container.Image)
return ""
}
imageHash := k8s_utils.ParseImageHash(imageID)
if imageHash == "" {
log.Warnf("Failed to parse image hash. container=%v ,image=%v, image id=%v", container.Name, container.Image, imageID)
return ""
}
return imageHash
}
func (s *Scanner) Scan(scanConfig *config.ScanConfig) error {
s.Lock()
defer s.Unlock()
s.scanConfig = scanConfig
log.WithFields(s.logFields).Infof("Start scanning...")
err := s.initScan()
if err != nil {
s.status = ScanInitFailure
return fmt.Errorf("failed to initiate scan: %v", err)
}
s.status = Scanning
go s.jobBatchManagement()
return nil
}
func (s *Scanner) ScanProgress() types.ScanProgress {
return types.ScanProgress{
ImagesToScan: s.progress.ImagesToScan,
ImagesStartedToScan: atomic.LoadUint32(&s.progress.ImagesStartedToScan),
ImagesCompletedToScan: atomic.LoadUint32(&s.progress.ImagesCompletedToScan),
}
}
func (s *Scanner) Results() *types.ScanResults {
s.Lock()
defer s.Unlock()
var imageScanResults []*types.ImageScanResult
for _, scanD := range s.imageToScanData {
if !scanD.completed {
continue
}
for _, podContext := range scanD.contexts {
imageScanResults = append(imageScanResults, &types.ImageScanResult{
PodName: podContext.podName,
PodNamespace: podContext.namespace,
ImageName: scanD.imageName,
ContainerName: podContext.containerName,
ImageHash: podContext.imageHash,
PodUid: podContext.podUid,
Vulnerabilities: scanD.vulnerabilitiesResult.result,
DockerfileScanResults: scanD.dockerfileResult.result,
LayerCommands: scanD.vulnerabilitiesResult.layerCommands,
Success: scanD.success,
ScanErrors: scanD.getScanErrors(),
})
}
}
return &types.ScanResults{
ImageScanResults: imageScanResults,
Progress: s.ScanProgress(),
}
}
func (s *Scanner) shouldIgnoreResult(scanD *scanData, resultScanUUID string, image string) bool {
if scanD == nil {
log.WithFields(s.logFields).Warnf("no scan data for image '%v', probably an old scan result - ignoring", image)
return true
}
if resultScanUUID != scanD.scanUUID {
log.WithFields(s.logFields).Warnf("Scan UUID mismatch, probably an old scan result - ignoring. image=%v, received=%v, expected=%v", image, resultScanUUID, scanD.scanUUID)
return true
}
if scanD.timeout {
log.WithFields(s.logFields).Warnf("Scan result after timeout - ignoring. image=%v, scan uuid=%v", image, resultScanUUID)
return true
}
if scanD.completed {
log.WithFields(s.logFields).Warnf("Duplicate result for image scan. image=%v, scan uuid=%v", image, resultScanUUID)
return true
}
return false
}
func (s *Scanner) HandleVulnerabilitiesResult(result *forwarding.ImageVulnerabilities) error {
s.Lock()
defer s.Unlock()
scanD, ok := s.imageToScanData[result.Image]
if !ok || s.shouldIgnoreResult(scanD, result.ScanUUID, result.Image) {
log.WithFields(s.logFields).Warnf("Ignoring vulnerabilities result for image '%v'", result.Image)
return nil
}
vulnerabilitiesResult := &vulnerabilitiesScanResult{
result: result.Vulnerabilities,
layerCommands: result.LayerCommands,
success: result.Success,
completed: true,
scanErr: result.ScanErr,
}
scanD.setVulnerabilitiesResult(vulnerabilitiesResult)
log.WithFields(s.logFields).Infof("Vulnerabilities result was set for image %v", result.Image)
if scanD.vulnerabilitiesResult.success && scanD.vulnerabilitiesResult.result == nil {
log.WithFields(s.logFields).Infof("No vulnerabilities found on image %v.", result.Image)
}
if !scanD.vulnerabilitiesResult.success {
log.WithFields(s.logFields).Warnf("Vulnerabilities scan of image %v has failed: %v", result.Image, scanD.vulnerabilitiesResult.scanErr)
}
if !scanD.completed {
log.WithFields(s.logFields).Infof("Total scan is not yet completed for image %v", result.Image)
return nil
}
select {
case scanD.resultChan <- true:
default:
log.WithFields(s.logFields).Warnf("Failed to notify upon received result scan. image=%v, scan-uuid=%v", result.Image, result.ScanUUID)
}
return nil
}
func (s *Scanner) HandleDockerfileResult(result *dockle_types.ImageAssessment) error {
s.Lock()
defer s.Unlock()
scanD, ok := s.imageToScanData[result.Image]
if !ok || s.shouldIgnoreResult(scanD, result.ScanUUID, result.Image) {
log.WithFields(s.logFields).Warnf("Ignoring dockerfile result for image '%v'", result.Image)
return nil
}
dockerfileResult := &dockerfileScanResult{
result: result.Assessment,
success: result.Success,
completed: true,
scanErr: result.ScanErr,
}
scanD.setDockerfileResult(dockerfileResult)
log.WithFields(s.logFields).Infof("Dockerfile result was set for image %v", result.Image)
if scanD.dockerfileResult.success && len(scanD.dockerfileResult.result) == 0 {
log.WithFields(s.logFields).Infof("No checkpoints found on image %v.", result.Image)
}
if !scanD.dockerfileResult.success {
log.WithFields(s.logFields).Warnf("Dockerfile scan of image %v has failed: %v", result.Image, scanD.dockerfileResult.scanErr)
}
if !scanD.completed {
log.WithFields(s.logFields).Infof("Total scan is not yet completed for image %v", result.Image)
return nil
}
select {
case scanD.resultChan <- true:
default:
log.WithFields(s.logFields).Warnf("Failed to notify upon received result scan. image=%v, scan-uuid=%v", result.Image, result.ScanUUID)
}
return nil
}
func (s *Scanner) Clear() {
s.Lock()
defer s.Unlock()
log.WithFields(s.logFields).Infof("Clearing...")
close(s.killSignal)
}