-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathqueries.go
More file actions
352 lines (318 loc) · 9.27 KB
/
queries.go
File metadata and controls
352 lines (318 loc) · 9.27 KB
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
package Cx1ClientGo
import (
"encoding/json"
"fmt"
"net/http"
"strconv"
"strings"
)
/*
This is separate from audit.go to split the functions that require a Web-Audit Session from those that do not.
This file contains the generic query-related functions that do not need a valid audit session.
*/
// this struct is used specifically for the to-be-deprecated /cx-audit/queries endpoint
type AuditQuery_v312 struct {
QueryID uint64 `json:"Id,string"`
Level string
LevelID string `json:"-"`
Path string
Modified string
Source string
Name string
Group string
Language string `json:"lang"`
Severity string
Cwe int64
IsExecutable bool
CxDescriptionId int64
QueryDescriptionId string
Key string
Title string
}
func (q AuditQuery_v312) ToQuery() SASTQuery {
return SASTQuery{
QueryID: q.QueryID,
Level: q.Level,
LevelID: q.LevelID,
Path: q.Path,
Modified: q.Modified,
Source: q.Source,
Name: q.Name,
Group: q.Group,
Language: q.Language,
Severity: q.Severity,
CweID: q.Cwe,
IsExecutable: q.IsExecutable,
QueryDescriptionId: q.CxDescriptionId,
Custom: q.Level != AUDIT_QUERY.PRODUCT,
EditorKey: q.Key,
SastID: 0,
}
}
// This function uses the cx-audit/queries endpoint, which will at some point be deprecated.
func (c *Cx1Client) GetQueriesByLevelID(level, levelId string) (SASTQueryCollection, error) {
c.depwarn("GetQueriesByLevelID", "GetAuditSASTQueriesByLevelID")
c.config.Logger.Debugf("Get all queries for %v", level)
var url string
collection := SASTQueryCollection{}
var queries_v312 []AuditQuery_v312
var queries []SASTQuery
switch level {
case AUDIT_QUERY.TENANT:
url = "/cx-audit/queries"
case AUDIT_QUERY.PROJECT:
url = fmt.Sprintf("/cx-audit/queries?projectId=%v", levelId)
default:
return collection, fmt.Errorf("invalid level %v, options are currently: Corp or Project", level)
}
response, err := c.sendRequest(http.MethodGet, url, nil, nil)
if err != nil {
return collection, err
}
err = json.Unmarshal(response, &queries_v312)
if err != nil {
return collection, err
}
applicationId := ""
for id := range queries_v312 {
switch queries_v312[id].Level {
case AUDIT_QUERY.TENANT:
queries_v312[id].LevelID = c.QueryTypeTenant()
case AUDIT_QUERY.PROJECT:
queries_v312[id].LevelID = levelId
case AUDIT_QUERY.APPLICATION:
if applicationId == "" {
project, err := c.GetProjectByID(levelId)
if err != nil {
return collection, fmt.Errorf("failed to retrieve project with ID %v", levelId)
}
if len(*project.Applications) == 0 {
return collection, fmt.Errorf("project %v has an application-level query defined, but has no application associated", project.String())
} else if len(*project.Applications) > 1 {
return collection, fmt.Errorf("project %v has an application-level query defined, but has multiple application associated", project.String())
}
applicationId = (*project.Applications)[0]
}
queries_v312[id].LevelID = applicationId
case AUDIT_QUERY.PRODUCT:
queries_v312[id].LevelID = c.QueryTypeProduct()
}
queries = append(queries, queries_v312[id].ToQuery())
}
collection.AddQueries(&queries)
return collection, nil
}
func (c *Cx1Client) GetQueryMappings() (map[uint64]uint64, error) {
var mapping map[uint64]uint64 = make(map[uint64]uint64)
var responsemap struct {
Mappings []struct {
AstId uint64 `json:"astId,string"`
SastId uint64 `json:"sastId,string"`
} `json:"mappings"`
}
response, err := c.sendRequest(http.MethodGet, "/queries/mappings", nil, nil)
if err != nil {
return mapping, err
}
err = json.Unmarshal(response, &responsemap)
if err != nil {
return mapping, err
}
for _, qm := range responsemap.Mappings {
mapping[qm.SastId] = qm.AstId
}
return mapping, nil
}
// convenience
func (c *Cx1Client) GetSeverityID(severity string) uint {
return GetSeverityID(severity)
}
func GetSeverityID(severity string) uint {
switch strings.ToUpper(severity) {
case "INFO":
return 0
case "INFORMATION":
return 0
case "LOW":
return 1
case "MEDIUM":
return 2
case "HIGH":
return 3
case "CRITICAL":
return 4
}
return 0
}
func (c *Cx1Client) GetSeverity(severity uint) string {
return GetSeverity(severity)
}
func (c *Cx1Client) GetCx1QueryFromSAST(sastId uint64, language, group, name string, mapping *map[uint64]uint64, qc *SASTQueryCollection) *SASTQuery {
if cx1id, ok := (*mapping)[sastId]; ok {
return qc.GetQueryByID(cx1id)
}
return qc.GetQueryByName(language, group, name)
}
func GetSeverity(severity uint) string {
switch severity {
case 0:
return "Info"
case 1:
return "Low"
case 2:
return "Medium"
case 3:
return "High"
case 4:
return "Critical"
}
return "Unknown"
}
func (q *SASTQuery) MergeQuery(nq SASTQuery) {
if q.QueryID == 0 && nq.QueryID != 0 {
q.QueryID = nq.QueryID
}
if q.Path == "" && nq.Path != "" {
q.Path = nq.Path
}
if q.EditorKey == "" && nq.EditorKey != "" {
q.EditorKey = nq.EditorKey
}
if q.Level == "" && nq.Level != "" {
q.Level = nq.Level
}
if q.LevelID == "" && nq.LevelID != "" {
q.LevelID = nq.LevelID
}
if q.Source == "" && nq.Source != "" {
q.Source = nq.Source
}
q.IsExecutable = nq.IsExecutable
if q.CweID == 0 && nq.CweID != 0 {
q.CweID = nq.CweID
}
if q.QueryDescriptionId == 0 && nq.QueryDescriptionId != 0 {
q.QueryDescriptionId = nq.QueryDescriptionId
}
if q.SastID == 0 && nq.SastID != 0 {
q.SastID = nq.SastID
}
}
func (q *IACQuery) MergeQuery(nq IACQuery) {
if q.QueryID == "" && nq.QueryID != "" {
q.QueryID = nq.QueryID
}
if q.Key == "" && nq.Key != "" {
q.Key = nq.Key
}
if q.Path == "" && nq.Path != "" {
q.Path = nq.Path
}
if q.Level == "" && nq.Level != "" {
q.Level = nq.Level
}
if q.LevelID == "" && nq.LevelID != "" {
q.LevelID = nq.LevelID
}
if q.Source == "" && nq.Source != "" {
q.Source = nq.Source
}
if q.Category == "" && nq.Category != "" {
q.Category = nq.Category
}
if q.Group == "" && nq.Group != "" {
q.Group = nq.Group
}
if q.Platform == "" && nq.Platform != "" {
q.Platform = nq.Platform
}
if q.Description == "" && nq.Description != "" {
q.Description = nq.Description
}
if q.DescriptionID == "" && nq.DescriptionID != "" {
q.DescriptionID = nq.DescriptionID
}
if q.DescriptionURL == "" && nq.DescriptionURL != "" {
q.DescriptionURL = nq.DescriptionURL
}
}
func (q SASTQuery) StringDetailed() string {
var scope string
switch q.Level {
case AUDIT_QUERY.PRODUCT:
scope = "Product"
case AUDIT_QUERY.TENANT:
scope = "Tenant"
default:
scope = fmt.Sprintf("%v %v", q.Level, ShortenGUID(q.LevelID))
}
return fmt.Sprintf("%v: %v -> %v -> %v, %v risk [ID %v, Key %v]", scope, q.Language, q.Group, q.Name, q.Severity, ShortenGUID(strconv.FormatUint(q.QueryID, 10)), ShortenGUID(q.EditorKey))
}
func (q SASTQuery) String() string {
return fmt.Sprintf("[%d] %v -> %v -> %v", q.QueryID, q.Language, q.Group, q.Name)
}
func (q IACQuery) String() string {
return fmt.Sprintf("[%v] %v -> %v -> %v", ShortenGUID(q.Key), q.Platform, q.Group, q.Name)
}
func (q IACQuery) StringDetailed() string {
var scope string
switch q.Level {
case AUDIT_QUERY.PRODUCT:
scope = "Product"
case AUDIT_QUERY.TENANT:
scope = "Tenant"
default:
scope = fmt.Sprintf("%v %v", q.Level, ShortenGUID(q.LevelID))
}
return fmt.Sprintf("%v: %v -> %v -> %v, %v risk [Key %v]", scope, q.Platform, q.Group, q.Name, q.Severity, ShortenGUID(q.Key))
}
func (q SASTQuery) GetMetadata() AuditSASTQueryMetadata {
return AuditSASTQueryMetadata{
Cwe: q.CweID,
IsExecutable: q.IsExecutable,
CxDescriptionID: q.QueryDescriptionId,
Language: q.Language,
Group: q.Group,
Severity: q.Severity,
SastID: q.SastID,
Name: q.Name,
}
}
func (q SASTQuery) MetadataDifferent(metadata AuditSASTQueryMetadata) bool {
return q.CweID != metadata.Cwe ||
q.IsExecutable != metadata.IsExecutable ||
q.QueryDescriptionId != metadata.CxDescriptionID ||
q.Language != metadata.Language ||
q.Group != metadata.Group ||
!strings.EqualFold(q.Severity, metadata.Severity) ||
q.SastID != metadata.SastID ||
q.Name != metadata.Name
}
func (q IACQuery) GetMetadata() AuditIACQueryMetadata {
return AuditIACQueryMetadata{
Cwe: q.CWE,
Aggregation: "",
Category: q.Category,
Description: q.Description,
DescriptionID: q.DescriptionID,
DescriptionURL: q.DescriptionURL,
Platform: q.Platform,
QueryID: q.QueryID,
Name: q.Name,
Severity: q.Severity,
}
}
func (q IACQuery) MetadataDifferent(metadata AuditIACQueryMetadata) bool {
return q.CWE != metadata.Cwe ||
q.Category != metadata.Category ||
q.Description != metadata.Description ||
q.DescriptionID != metadata.DescriptionID ||
q.DescriptionURL != metadata.DescriptionURL ||
q.Platform != metadata.Platform ||
q.QueryID != metadata.QueryID ||
q.Name != metadata.Name ||
!strings.EqualFold(q.Severity, metadata.Severity)
}
func (c *Cx1Client) QueryLink(q *SASTQuery) string {
return fmt.Sprintf("%v/audit/?queryid=%d", c.config.Cx1Url, q.QueryID)
}