forked from kyma-project/Cx1ClientGo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresults.go
405 lines (346 loc) · 13.5 KB
/
results.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
package Cx1ClientGo
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"strings"
"github.com/google/go-querystring/query"
)
func (c Cx1Client) GetScanResultsByID(scanID string, limit uint64) (ScanResultSet, error) {
c.logger.Debugf("Get %d Cx1 Scan Results for scan %v", limit, scanID)
_, results, err := c.GetXScanResultsFiltered(ScanResultsFilter{
BaseFilter: BaseFilter{Limit: c.pagination.Results},
ScanID: scanID,
}, limit)
return results, err
}
func (c Cx1Client) GetAllScanResultsByID(scanID string) (ScanResultSet, error) {
c.logger.Debugf("Get all Cx1 Scan Results for scan %v", scanID)
_, results, err := c.GetAllScanResultsFiltered(ScanResultsFilter{
BaseFilter: BaseFilter{Limit: c.pagination.Results},
ScanID: scanID,
})
return results, err
}
func (c Cx1Client) GetScanResultsCountByID(scanID string) (uint64, error) {
c.logger.Debugf("Get Cx1 Scan Results count for scan %v", scanID)
count, _, err := c.GetScanResultsFiltered(ScanResultsFilter{
BaseFilter: BaseFilter{Limit: 0},
ScanID: scanID,
})
return count, err
}
// returns one 'page' of scan results matching the filter
// returns items (filter.Offset*filter.Limit) to (filter.Offset + 1)*filter.Limit
// returns the count of items retrieved, however some items may not be parsed into the result
// set depending on support in cx1clientgo
func (c Cx1Client) GetScanResultsFiltered(filter ScanResultsFilter) (uint64, ScanResultSet, error) {
params, _ := query.Values(filter)
results := ScanResultSet{}
data, err := c.sendRequest(http.MethodGet, fmt.Sprintf("/results/?%v", params.Encode()), nil, nil)
if err != nil {
err = fmt.Errorf("failed to fetch scans matching filter %v: %s", params.Encode(), err)
c.logger.Tracef("Error: %s", err)
return 0, results, err
}
count, results, err := c.parseScanResults(data)
return count, results, err
}
func (s *ScanResultsFilter) Bump() { // this one does offset in pages rather than items
s.Offset++
}
// gets all of the results available matching a filter
// the counter returned represents the total number of results which were parsed
// this may not include some of the returned results depending on Cx1ClientGo support
func (c Cx1Client) GetAllScanResultsFiltered(filter ScanResultsFilter) (uint64, ScanResultSet, error) {
var results ScanResultSet
count, rs, err := c.GetScanResultsFiltered(filter)
results = rs
for err == nil && count > (filter.Offset+1)*filter.Limit && filter.Limit > 0 {
filter.Bump()
_, rs, err = c.GetScanResultsFiltered(filter)
results.Append(&rs)
}
return rs.Count(), results, err
}
// will return at least X results matching the filter
// May return more due to paging eg: requesting 101 with a 100-item page can return 200 results
func (c Cx1Client) GetXScanResultsFiltered(filter ScanResultsFilter, desiredcount uint64) (uint64, ScanResultSet, error) {
var results ScanResultSet
_, rs, err := c.GetScanResultsFiltered(filter)
results = rs
for err == nil && desiredcount > (filter.Offset+1)*filter.Limit && filter.Limit > 0 {
filter.Bump()
_, rs, err = c.GetScanResultsFiltered(filter)
results.Append(&rs)
}
return rs.Count(), results, err
}
// convenience function
func (r ScanSASTResult) CreateResultsPredicate(projectId string) SASTResultsPredicates {
return SASTResultsPredicates{
ResultsPredicatesBase{SimilarityID: r.SimilarityID,
ProjectID: projectId,
State: r.State,
Severity: r.Severity,
},
}
}
func (r ScanKICSResult) CreateResultsPredicate(projectId string) KICSResultsPredicates {
return KICSResultsPredicates{
ResultsPredicatesBase{SimilarityID: r.SimilarityID,
ProjectID: projectId,
State: r.State,
Severity: r.Severity,
},
}
}
// results
func (c Cx1Client) AddSASTResultsPredicates(predicates []SASTResultsPredicates) error {
c.logger.Debugf("Adding %d SAST results predicates", len(predicates))
jsonBody, err := json.Marshal(predicates)
if err != nil {
c.logger.Tracef("Failed to add SAST results predicates: %s", err)
return err
}
_, err = c.sendRequest(http.MethodPost, "/sast-results-predicates", bytes.NewReader(jsonBody), nil)
return err
}
func (c Cx1Client) AddKICSResultsPredicates(predicates []KICSResultsPredicates) error {
c.logger.Debugf("Adding %d KICS results predicates", len(predicates))
jsonBody, err := json.Marshal(predicates)
if err != nil {
c.logger.Tracef("Failed to add KICS results predicates: %s", err)
return err
}
_, err = c.sendRequest(http.MethodPost, "/kics-results-predicates", bytes.NewReader(jsonBody), nil)
return err
}
func (c Cx1Client) GetSASTResultsPredicatesByID(SimilarityID string, ProjectID string) ([]SASTResultsPredicates, error) {
c.logger.Debugf("Fetching SAST results predicates for project %v similarityId %v", ProjectID, SimilarityID)
var Predicates struct {
PredicateHistoryPerProject []struct {
ProjectID string
SimilarityID string `json:"similarityId"`
Predicates []SASTResultsPredicates
TotalCount uint
}
TotalCount uint
}
response, err := c.sendRequest(http.MethodGet, fmt.Sprintf("/sast-results-predicates/%v?project-ids=%v", SimilarityID, ProjectID), nil, nil)
if err != nil {
return []SASTResultsPredicates{}, err
}
err = json.Unmarshal(response, &Predicates)
if err != nil {
return []SASTResultsPredicates{}, err
}
if Predicates.TotalCount == 0 {
return []SASTResultsPredicates{}, nil
}
return Predicates.PredicateHistoryPerProject[0].Predicates, err
}
func (c Cx1Client) GetKICSResultsPredicatesByID(SimilarityID string, ProjectID string) ([]KICSResultsPredicates, error) {
c.logger.Debugf("Fetching KICS results predicates for project %v similarityId %v", ProjectID, SimilarityID)
var Predicates struct {
PredicateHistoryPerProject []struct {
ProjectID string
SimilarityID string `json:"similarityId"`
Predicates []KICSResultsPredicates
TotalCount uint
}
TotalCount uint
}
response, err := c.sendRequest(http.MethodGet, fmt.Sprintf("/kics-results-predicates/%v?project-ids=%v", SimilarityID, ProjectID), nil, nil)
if err != nil {
return []KICSResultsPredicates{}, err
}
err = json.Unmarshal(response, &Predicates)
if err != nil {
return []KICSResultsPredicates{}, err
}
if Predicates.TotalCount == 0 {
return []KICSResultsPredicates{}, nil
}
return Predicates.PredicateHistoryPerProject[0].Predicates, err
}
// convenience function
func (p *ResultsPredicatesBase) Update(state, severity, comment string) {
if state != "" && state != p.State {
p.State = state
}
if severity != "" && severity != p.Severity {
p.Severity = severity
}
if comment != "" {
p.Comment = comment
}
}
func (r ScanSASTResult) String() string {
return fmt.Sprintf("%v (%v) - %v to %v - in file %v:%d", r.Data.QueryName, r.SimilarityID, r.Data.Nodes[0].Name, r.Data.Nodes[len(r.Data.Nodes)-1].Name, r.Data.Nodes[0].FileName, r.Data.Nodes[0].Line)
}
func (r ScanKICSResult) String() string {
return fmt.Sprintf("%v - %v (%v) - %v to %v - in file %v:%d", r.Data.Group, r.Data.QueryName, r.SimilarityID, r.Data.IssueType, r.Data.Value, r.Data.FileName, r.Data.Line)
}
func (r ScanSCAResult) String() string {
return fmt.Sprintf("%v - %v (%v) - recommended version %v: %v", r.Data.PackageIdentifier, r.Data.PublishedAt, r.SimilarityID, r.Data.RecommendedVersion, r.Data.GetType("Advisory").URL)
}
func (r ScanSCAContainerResult) String() string {
return fmt.Sprintf("%v %v - %v (%v)", r.Data.PackageName, r.Data.PackageVersion, r.Data.PublishedAt, r.SimilarityID)
}
func (r ScanContainersResult) String() string {
return fmt.Sprintf("%v %v / %v #%v (%v)", r.Data.PackageName, r.Data.PackageVersion, r.Data.ImageName, r.Data.ImageTag, r.SimilarityID)
}
func (r ScanSCAResultData) GetType(packageDataType string) ScanSCAResultPackageData {
for _, p := range r.PackageData {
if strings.EqualFold(p.Type, packageDataType) {
return p
}
}
return ScanSCAResultPackageData{}
}
func addResultStatus(summary *ScanResultStatusSummary, result *ScanSASTResult) {
switch result.State {
case "CONFIRMED":
summary.Confirmed++
case "URGENT":
summary.Urgent++
case "URGENT ":
summary.Urgent++
case "PROPOSED_NOT_EXPLOITABLE":
summary.ProposedNotExploitable++
case "NOT_EXPLOITABLE":
summary.NotExploitable++
default:
summary.ToVerify++
}
}
func (c Cx1Client) GetScanSASTResultSummary(results *ScanResultSet) ScanResultSummary {
summary := ScanResultSummary{}
for _, result := range results.SAST {
switch result.Severity {
case "HIGH":
addResultStatus(&(summary.High), &result)
case "MEDIUM":
addResultStatus(&(summary.Medium), &result)
case "LOW":
addResultStatus(&(summary.Low), &result)
default:
addResultStatus(&(summary.Information), &result)
}
}
return summary
}
func (s ScanResultSet) String() string {
return fmt.Sprintf("Result set with %d SAST, %d SCA, %d SCAContainer, %d KICS, and %d Containers results", len(s.SAST), len(s.SCA), len(s.SCAContainer), len(s.KICS), len(s.Containers))
}
func (s ScanResultSet) Count() uint64 {
return uint64(len(s.SAST) + len(s.SCA) + len(s.SCAContainer) + len(s.KICS) + len(s.Containers))
}
func (s *ScanResultSet) Append(results *ScanResultSet) {
if len(results.KICS) > 0 {
s.KICS = append(s.KICS, results.KICS...)
}
if len(results.SCA) > 0 {
s.SCA = append(s.SCA, results.SCA...)
}
if len(results.SCAContainer) > 0 {
s.SCAContainer = append(s.SCAContainer, results.SCAContainer...)
}
if len(results.SAST) > 0 {
s.SAST = append(s.SAST, results.SAST...)
}
if len(results.Containers) > 0 {
s.Containers = append(s.Containers, results.Containers...)
}
}
func (s ScanResultStatusSummary) Total() uint64 {
return s.ToVerify + s.Confirmed + s.Urgent + s.ProposedNotExploitable + s.NotExploitable
}
func (s ScanResultStatusSummary) String() string {
return fmt.Sprintf("To Verify: %d, Confirmed: %d, Urgent: %d, Proposed NE: %d, NE: %d", s.ToVerify, s.Confirmed, s.Urgent, s.ProposedNotExploitable, s.NotExploitable)
}
func (s ScanResultSummary) String() string {
return fmt.Sprintf("%v\n%v\n%v", fmt.Sprintf("\tHigh: %v\n\tMedium: %v\n\tLow: %v\n\tInfo: %v", s.High.String(), s.Medium.String(), s.Low.String(), s.Information.String()),
fmt.Sprintf("\tTotal High: %d, Medium: %d, Low: %d, Info: %d", s.High.Total(), s.Medium.Total(), s.Low.Total(), s.Information.Total()),
fmt.Sprintf("\tTotal ToVerify: %d, Confirmed: %d, Urgent: %d, Proposed NE: %d, NE: %d",
s.High.ToVerify+s.Medium.ToVerify+s.Low.ToVerify+s.Information.ToVerify,
s.High.Confirmed+s.Medium.Confirmed+s.Low.Confirmed+s.Information.Confirmed,
s.High.Urgent+s.Medium.Urgent+s.Low.Urgent+s.Information.Urgent,
s.High.ProposedNotExploitable+s.Medium.ProposedNotExploitable+s.Low.ProposedNotExploitable+s.Information.ProposedNotExploitable,
s.High.NotExploitable+s.Medium.NotExploitable+s.Low.NotExploitable+s.Information.NotExploitable))
}
// Note: response.TotalCount may be greater than the resultset, due to limited cx1clientgo engine support
func (c Cx1Client) parseScanResults(response []byte) (uint64, ScanResultSet, error) {
var resultResponse struct {
Results []map[string]interface{}
TotalCount uint64
}
var ResultSet ScanResultSet
dec := json.NewDecoder(bytes.NewReader(response))
dec.UseNumber()
err := dec.Decode(&resultResponse)
if err != nil {
c.logger.Tracef("Failed while parsing response: %s", err)
c.logger.Tracef("Response contents: %s", string(response))
return resultResponse.TotalCount, ResultSet, err
}
//c.logger.Debugf("Retrieved %d results", resultResponse.TotalCount)
/*
if uint64(len(resultResponse.Results)) != resultResponse.TotalCount {
c.logger.Warnf("Expected results total count %d but parsed only %d", resultResponse.TotalCount, len(resultResponse.Results))
c.logger.Tracef("Response was: %v", string(response))
}
*/
for _, r := range resultResponse.Results {
//c.logger.Infof("Result %v: %v", r["similarityId"].(string), r["type"].(string))
jsonResult, _ := json.Marshal(r)
switch r["type"].(string) {
case "sast":
var SASTResult ScanSASTResult
err := json.Unmarshal(jsonResult, &SASTResult)
if err != nil {
c.logger.Warnf("Failed to unmarshal result %v to SAST type: %s", r["similarityId"].(string), err)
} else {
ResultSet.SAST = append(ResultSet.SAST, SASTResult)
}
case "sca":
var SCAResult ScanSCAResult
err := json.Unmarshal(jsonResult, &SCAResult)
if err != nil {
c.logger.Warnf("Failed to unmarshal result %v to SCA type: %s", r["similarityId"].(string), err)
} else {
ResultSet.SCA = append(ResultSet.SCA, SCAResult)
}
case "kics":
var KICSResult ScanKICSResult
err := json.Unmarshal(jsonResult, &KICSResult)
if err != nil {
c.logger.Warnf("Failed to unmarshal result %v to KICS type: %s", r["similarityId"].(string), err)
} else {
ResultSet.KICS = append(ResultSet.KICS, KICSResult)
}
case "sca-container":
var SCACResult ScanSCAContainerResult
err := json.Unmarshal(jsonResult, &SCACResult)
if err != nil {
c.logger.Warnf("Failed to unmarshal result %v to SCAContainer type: %s", r["similarityId"].(string), err)
} else {
ResultSet.SCAContainer = append(ResultSet.SCAContainer, SCACResult)
}
case "containers":
var ContainerResult ScanContainersResult
err := json.Unmarshal(jsonResult, &ContainerResult)
if err != nil {
c.logger.Warnf("Failed to unmarshal result %v to Containers type: %s", r["similarityId"].(string), err)
} else {
ResultSet.Containers = append(ResultSet.Containers, ContainerResult)
}
default:
c.logger.Warnf("Unable to unmarshal result %v of unknown type %v", r["similarityId"].(string), r["type"].(string))
}
}
c.logger.Debugf("Retrieved %d of %d results", ResultSet.Count(), resultResponse.TotalCount)
return resultResponse.TotalCount, ResultSet, nil
}