-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathvulnerability_exceptions.go
474 lines (420 loc) · 15.3 KB
/
vulnerability_exceptions.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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
//
// Author:: Darren Murray (<[email protected]>)
// Copyright:: Copyright 2021, Lacework Inc.
// License:: Apache License, Version 2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package api
import (
"fmt"
"strings"
"time"
"github.com/pkg/errors"
)
// VulnerabilityExceptionsService is the service that interacts with
// the VulnerabilityExceptions schema from the Lacework APIv2 Server
type VulnerabilityExceptionsService struct {
client *Client
}
// vulnerabilityExceptionResourceScope is an interface for the 2 types of vulnerability exceptions resource scopes:
// 'VulnerabilityExceptionContainerResourceScope' or 'VulnerabilityExceptionHostResourceScope'
type vulnerabilityExceptionResourceScope interface {
Type() vulnerabilityExceptionType
Scope() VulnerabilityExceptionResourceScope
}
// vulnerabilityExceptionReason represents the types of vulnerability exceptions reasons:
// 'False Positive', 'Accepted Risk', 'Compensating Controls', 'Fix Pending' or 'Other'
type vulnerabilityExceptionReason int
const (
VulnerabilityExceptionReasonAcceptedRisk vulnerabilityExceptionReason = iota
VulnerabilityExceptionReasonAcceptedFalsePositive
VulnerabilityExceptionReasonCompensatingControls
VulnerabilityExceptionReasonFixPending
VulnerabilityExceptionReasonOther
VulnerabilityExceptionReasonUnknown
)
var VulnerabilityExceptionReasons = map[vulnerabilityExceptionReason]string{
VulnerabilityExceptionReasonAcceptedRisk: "Accepted Risk",
VulnerabilityExceptionReasonAcceptedFalsePositive: "False Positive",
VulnerabilityExceptionReasonCompensatingControls: "Compensating Controls",
VulnerabilityExceptionReasonFixPending: "Fix Pending",
VulnerabilityExceptionReasonOther: "Other",
VulnerabilityExceptionReasonUnknown: "Unknown",
}
func (i vulnerabilityExceptionReason) String() string {
return VulnerabilityExceptionReasons[i]
}
func NewVulnerabilityExceptionReason(reason string) vulnerabilityExceptionReason {
switch reason {
case "Accepted Risk":
return VulnerabilityExceptionReasonAcceptedRisk
case "False Positive":
return VulnerabilityExceptionReasonAcceptedFalsePositive
case "Compensating Controls":
return VulnerabilityExceptionReasonCompensatingControls
case "Fix Pending":
return VulnerabilityExceptionReasonFixPending
case "Other":
return VulnerabilityExceptionReasonOther
default:
return VulnerabilityExceptionReasonUnknown
}
}
// vulnerabilityExceptionType represents the types of vulnerability exceptions 'Host' or 'Container'
type vulnerabilityExceptionType int
const (
VulnerabilityExceptionTypeHost vulnerabilityExceptionType = iota
VulnerabilityExceptionTypeContainer
)
var VulnerabilityExceptionTypes = map[vulnerabilityExceptionType]string{
VulnerabilityExceptionTypeHost: "Host",
VulnerabilityExceptionTypeContainer: "Container",
}
func (i vulnerabilityExceptionType) String() string {
return VulnerabilityExceptionTypes[i]
}
// vulnerabilityExceptionSeverity represents the types of vulnerability severities:
// 'Critical', 'High', 'Medium', 'Low' or 'Info'
type vulnerabilityExceptionSeverity string
type VulnerabilityExceptionSeverities []vulnerabilityExceptionSeverity
func (sevs VulnerabilityExceptionSeverities) ToStringSlice() []string {
var res []string
for _, i := range sevs {
switch i {
case VulnerabilityExceptionSeverityCritical:
res = append(res, "Critical")
case VulnerabilityExceptionSeverityHigh:
res = append(res, "High")
case VulnerabilityExceptionSeverityMedium:
res = append(res, "Medium")
case VulnerabilityExceptionSeverityLow:
res = append(res, "Low")
case VulnerabilityExceptionSeverityInfo:
res = append(res, "Info")
default:
continue
}
}
return res
}
func NewVulnerabilityExceptionSeverities(sevSlice []string) VulnerabilityExceptionSeverities {
var res VulnerabilityExceptionSeverities
for _, i := range sevSlice {
sev := convertVulnerabilityExceptionSeverity(i)
if sev != VulnerabilityExceptionSeverityUnknown {
res = append(res, sev)
}
}
return res
}
func convertVulnerabilityExceptionSeverity(sev string) vulnerabilityExceptionSeverity {
switch strings.ToLower(sev) {
case "critical":
return VulnerabilityExceptionSeverityCritical
case "high":
return VulnerabilityExceptionSeverityHigh
case "medium":
return VulnerabilityExceptionSeverityMedium
case "low":
return VulnerabilityExceptionSeverityLow
case "info":
return VulnerabilityExceptionSeverityInfo
default:
return VulnerabilityExceptionSeverityUnknown
}
}
const (
VulnerabilityExceptionSeverityCritical vulnerabilityExceptionSeverity = "Critical"
VulnerabilityExceptionSeverityHigh vulnerabilityExceptionSeverity = "High"
VulnerabilityExceptionSeverityMedium vulnerabilityExceptionSeverity = "Medium"
VulnerabilityExceptionSeverityLow vulnerabilityExceptionSeverity = "Low"
VulnerabilityExceptionSeverityInfo vulnerabilityExceptionSeverity = "Info"
VulnerabilityExceptionSeverityUnknown vulnerabilityExceptionSeverity = "Unknown"
)
// NewVulnerabilityException returns an instance of the VulnerabilityException struct
//
// Basic usage: Initialize a new VulnerabilityException struct, then
//
// use the new instance to do CRUD operations
//
// client, err := api.NewClient("account")
// if err != nil {
// return err
// }
//
// exception := api.VulnerabilityExceptionConfig{
// Type: api.VulnerabilityExceptionTypeHost,
// Description: "This is a vuln exception",
// ExceptionReason: api.VulnerabilityExceptionReasonCompensatingControls,
// Severities: api.VulnerabilityExceptionSeverities{api.VulnerabilityExceptionSeverityCritical},
// Fixable: true,
// ResourceScope: api.VulnerabilityExceptionContainerResourceScope{
// ImageID: []string{""},
// ImageTag: []string{""},
// Registry: []string{""},
// Repository: []string{""},
// Namespace: []string{""},
// },
// ExpiryTime: time.Now().AddDate(0, 1, 0),
// }
//
// vulnerabilityException := api.NewVulnerabilityException("vulnerabilityException", exception)
//
// client.V2.VulnerabilityExceptions.Create(vulnerabilityException)
func NewVulnerabilityException(name string, exception VulnerabilityExceptionConfig) VulnerabilityException {
var (
packages = aggregatePackages(exception.Package)
vulnException = VulnerabilityException{
Enabled: 1,
ExceptionName: name,
ExceptionReason: exception.ExceptionReason.String(),
Props: VulnerabilityExceptionProps{Description: exception.Description},
VulnerabilityCriteria: VulnerabilityExceptionCriteria{
Severity: exception.Severities.ToStringSlice(),
Package: packages,
Cve: exception.Cve,
Fixable: exception.FixableEnabled(),
},
}
)
if !exception.ExpiryTime.IsZero() {
vulnException.ExpiryTime = exception.ExpiryTime.UTC().Format(time.RFC3339)
}
vulnException.ExceptionType = exception.Type.String()
vulnException.setResourceScope(exception.ResourceScope)
return vulnException
}
func aggregatePackages(packages []VulnerabilityExceptionPackage) []map[string][]string {
var packs []map[string][]string
for _, pck := range packages {
var packagesMap = make(map[string][]string)
//aggregate packages with same name
if len(packs) > 0 {
if _, ok := packs[0][pck.Name]; ok {
packs[0][pck.Name] = append(packs[0][pck.Name], pck.Version)
continue
}
}
packagesMap[pck.Name] = []string{pck.Version}
packs = append(packs, packagesMap)
}
return packs
}
func (exception *VulnerabilityException) setResourceScope(scope vulnerabilityExceptionResourceScope) {
if scope == nil {
return
}
switch scope.Type() {
case VulnerabilityExceptionTypeContainer:
ctr := scope.Scope()
exception.ResourceScope = &VulnerabilityExceptionResourceScope{
ImageID: ctr.ImageID,
ImageTag: ctr.ImageTag,
Registry: ctr.Registry,
Repository: ctr.Repository,
Namespace: ctr.Namespace,
}
case VulnerabilityExceptionTypeHost:
host := scope.Scope()
exception.ResourceScope = &VulnerabilityExceptionResourceScope{
Hostname: host.Hostname,
ClusterName: host.ClusterName,
ExternalIP: host.ExternalIP,
Namespace: host.Namespace,
}
default:
exception.ResourceScope = &VulnerabilityExceptionResourceScope{}
}
}
func (exception VulnerabilityException) Status() string {
if exception.Enabled == 1 {
return "Enabled"
}
return "Disabled"
}
func (cfg VulnerabilityExceptionConfig) FixableEnabled() []int {
if cfg.Fixable == nil {
return nil
}
if *cfg.Fixable {
return []int{1}
}
return []int{0}
}
// List returns a list of Vulnerability Exceptions
func (svc *VulnerabilityExceptionsService) List() (response VulnerabilityExceptionsResponse, err error) {
err = svc.client.RequestDecoder("GET", apiV2VulnerabilityExceptions, nil, &response)
return
}
// Create creates a single Vulnerability Exception
func (svc *VulnerabilityExceptionsService) Create(vuln VulnerabilityException) (
response VulnerabilityExceptionResponse,
err error,
) {
err = svc.client.RequestEncoderDecoder("POST", apiV2VulnerabilityExceptions, vuln, &response)
return
}
// Delete deletes a Vulnerability Exception that matches the provided guid
func (svc *VulnerabilityExceptionsService) Delete(guid string) error {
if guid == "" {
return errors.New("specify an intgGuid")
}
return svc.client.RequestDecoder(
"DELETE",
fmt.Sprintf(apiV2VulnerabilityExceptionFromGUID, guid),
nil,
nil,
)
}
// Update updates a single Vulnerability Exception.
func (svc *VulnerabilityExceptionsService) Update(data VulnerabilityException) (
response VulnerabilityExceptionResponse,
err error,
) {
if data.Guid == "" {
err = errors.New("specify a Guid")
return
}
apiPath := fmt.Sprintf(apiV2VulnerabilityExceptionFromGUID, data.Guid)
// Request is invalid if it contains the ID field. We set the id field to empty
data.Guid = ""
err = svc.client.RequestEncoderDecoder("PATCH", apiPath, data, &response)
return
}
// Get returns a raw response of the Vulnerability Exception with the matching guid.
func (svc *VulnerabilityExceptionsService) Get(guid string, response interface{}) error {
if guid == "" {
return errors.New("specify a Guid")
}
apiPath := fmt.Sprintf(apiV2VulnerabilityExceptionFromGUID, guid)
return svc.client.RequestDecoder("GET", apiPath, nil, &response)
}
type VulnerabilityExceptionConfig struct {
Description string
Type vulnerabilityExceptionType
ExceptionReason vulnerabilityExceptionReason
Severities VulnerabilityExceptionSeverities
Cve []string
Package []VulnerabilityExceptionPackage
Fixable *bool
ResourceScope vulnerabilityExceptionResourceScope
ExpiryTime time.Time
}
type VulnerabilityExceptionContainerResourceScope struct {
ImageID []string `json:"imageId,omitempty"`
ImageTag []string `json:"imageTag,omitempty"`
Registry []string `json:"registry,omitempty"`
Repository []string `json:"repository,omitempty"`
Namespace []string `json:"namespace,omitempty"`
}
func (ctr VulnerabilityExceptionContainerResourceScope) Type() vulnerabilityExceptionType {
return VulnerabilityExceptionTypeContainer
}
func (ctr VulnerabilityExceptionContainerResourceScope) Scope() VulnerabilityExceptionResourceScope {
return VulnerabilityExceptionResourceScope{
ImageID: ctr.ImageID,
ImageTag: ctr.ImageTag,
Registry: ctr.Registry,
Repository: ctr.Repository,
Namespace: ctr.Namespace,
}
}
func (host VulnerabilityExceptionHostResourceScope) Scope() VulnerabilityExceptionResourceScope {
return VulnerabilityExceptionResourceScope{
Hostname: host.Hostname,
ExternalIP: host.ExternalIP,
ClusterName: host.ClusterName,
Namespace: host.Namespace,
}
}
type VulnerabilityExceptionHostResourceScope struct {
Hostname []string `json:"hostname,omitempty"`
ExternalIP []string `json:"externalIp,omitempty"`
ClusterName []string `json:"clusterName,omitempty"`
Namespace []string `json:"namespace,omitempty"`
}
func (host VulnerabilityExceptionHostResourceScope) Type() vulnerabilityExceptionType {
return VulnerabilityExceptionTypeHost
}
type VulnerabilityException struct {
Guid string `json:"exceptionGuid,omitempty"`
Enabled int `json:"state"`
ExceptionName string `json:"exceptionName"`
ExceptionType string `json:"exceptionType"`
ExceptionReason string `json:"exceptionReason"`
Props VulnerabilityExceptionProps `json:"props"`
VulnerabilityCriteria VulnerabilityExceptionCriteria `json:"vulnerabilityCriteria"`
ResourceScope *VulnerabilityExceptionResourceScope `json:"resourceScope,omitempty"`
CreatedTime string `json:"createdTime,omitempty"`
UpdatedTime string `json:"updatedTime,omitempty"`
ExpiryTime string `json:"expiryTime,omitempty"`
}
type VulnerabilityExceptionProps struct {
Description string `json:"description,omitempty"`
CreatedBy string `json:"createdBy,omitempty"`
UpdatedBy string `json:"updatedBy,omitempty"`
}
type VulnerabilityExceptionResourceScope struct {
// Container properties
ImageID []string `json:"imageId,omitempty"`
ImageTag []string `json:"imageTag,omitempty"`
Registry []string `json:"registry,omitempty"`
Repository []string `json:"repository,omitempty"`
// Host properties
Hostname []string `json:"hostname,omitempty"`
ExternalIP []string `json:"externalIp,omitempty"`
ClusterName []string `json:"clusterName,omitempty"`
// Shared properties
Namespace []string `json:"namespace,omitempty"`
}
type VulnerabilityExceptionCriteria struct {
Cve []string `json:"cve,omitempty"`
Package []map[string][]string `json:"package,omitempty"`
Severity []string `json:"severity,omitempty"`
Fixable []int `json:"fixable,omitempty"`
}
type VulnerabilityExceptionResponse struct {
Data VulnerabilityException `json:"data"`
}
type VulnerabilityExceptionsResponse struct {
Data []VulnerabilityException `json:"data"`
}
type VulnerabilityExceptionPackage struct {
Name string
Version string
}
func (vc VulnerabilityExceptionCriteria) FixableEnabled() *bool {
if vc.Fixable == nil || len(vc.Fixable) == 0 {
return nil
}
if len(vc.Fixable) > 0 {
truePtr := vc.Fixable[0] == 1
return &truePtr
}
falsePtr := false
return &falsePtr
}
func NewVulnerabilityExceptionPackages(packageMap []map[string]string) []VulnerabilityExceptionPackage {
var packages []VulnerabilityExceptionPackage
for _, m := range packageMap {
for k, v := range m {
pck := VulnerabilityExceptionPackage{
Name: k,
Version: v,
}
packages = append(packages, pck)
}
}
return packages
}