-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscans.go
More file actions
383 lines (321 loc) · 10.9 KB
/
scans.go
File metadata and controls
383 lines (321 loc) · 10.9 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
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
package CxSASTClientGo
import (
"encoding/base64"
"encoding/json"
"encoding/xml"
"fmt"
"regexp"
"slices"
"strings"
"time"
"github.com/pkg/errors"
)
func (c SASTClient) GetScanByID(scanid uint64) (Scan, error) {
c.logger.Debugf("Get SAST scan %d", scanid)
var scan Scan
response, err := c.get(fmt.Sprintf("/sast/scans/%d", scanid))
if err != nil {
return scan, err
}
err = json.Unmarshal(response, &scan)
return scan, err
}
func (c SASTClient) GetScanSchedules() ([]ScanSchedule, error) {
schedules := []ScanSchedule{}
response, err := c.getV("/sast/scheduledJobs", "4.0")
if err != nil {
return schedules, err
}
err = json.Unmarshal(response, &schedules)
return schedules, err
}
func (s ScanSchedule) String() string {
return fmt.Sprintf("Project %v [%d]: on %v at %v", s.ProjectName, s.ProjectID, strings.Join(s.Days, ","), s.Time)
}
func (c SASTClient) GetLastScanByID(projectid uint64) (Scan, error) {
var scans []Scan
response, err := c.get(fmt.Sprintf("/sast/scans?projectId=%d&scanStatus=Finished&last=1", projectid))
if err != nil {
return Scan{}, err
}
err = json.Unmarshal(response, &scans)
if err != nil {
return Scan{}, err
}
if len(scans) == 0 {
return Scan{}, errors.New(fmt.Sprintf("No scans found in project %d", projectid))
}
return scans[0], nil
}
func (c SASTClient) GetEngineConfigurations() ([]EngineConfiguration, error) {
var confs []EngineConfiguration
response, err := c.get("/sast/engineConfigurations")
if err != nil {
return confs, err
}
err = json.Unmarshal(response, &confs)
return confs, err
}
func (c SASTClient) GetEngineConfigurationsSOAP() ([]EngineConfiguration, error) {
var confs []EngineConfiguration
response, err := c.sendSOAPRequest("GetConfigurationSetList", "<SessionID></SessionID>")
if err != nil {
return confs, err
}
var xmlResponse struct {
XMLName xml.Name `xml:"Envelope"`
Body struct {
XMLName xml.Name `xml:"Body"`
Response struct {
XMLName xml.Name `xml:"GetConfigurationSetListResponse"`
Result struct {
XMLName xml.Name `xml:"GetConfigurationSetListResult"`
IsSuccesfull bool `xml:"IsSuccesfull"`
ErrorMessage string
ConfigSetList struct {
XMLName xml.Name `xml:"ConfigSetList"`
ConfigSets []struct {
ConfigSetName string
ID uint64
} `xml:"ConfigurationSet"`
}
}
}
}
}
err = xml.Unmarshal(response, &xmlResponse)
if err != nil {
c.logger.Errorf("Failed to parse SOAP response: %s", err)
c.logger.Tracef("Parsed from: %v", string(response))
return confs, err
}
if !xmlResponse.Body.Response.Result.IsSuccesfull {
return confs, fmt.Errorf("failed to get engine configurations: %v", xmlResponse.Body.Response.Result.ErrorMessage)
}
confs = make([]EngineConfiguration, len(xmlResponse.Body.Response.Result.ConfigSetList.ConfigSets))
for id, conf := range xmlResponse.Body.Response.Result.ConfigSetList.ConfigSets {
confs[id] = EngineConfiguration{
ID: conf.ID,
Name: conf.ConfigSetName,
}
}
return confs, err
}
func (c SASTClient) GetProjectLastFullScanIDODATA(project *Project) (uint64, error) {
response, err := c.sendODATARequest(fmt.Sprintf("v1/Scans?$filter=IsIncremental%%20eq%%20false%%20and%%20ScanType%%20eq%%201%%20and%%20ProjectId%%20eq%%20%d&$select=Id&$orderby=EngineFinishedOn%%20desc&$top=1", project.ProjectID))
if err != nil {
return 0, err
}
type responseStruct struct {
Value []struct {
Id uint64 `json:"Id"`
} `json:"value"`
}
var rs responseStruct
err = json.Unmarshal(response, &rs)
if err != nil {
return 0, err
}
if len(rs.Value) != 1 {
return 0, fmt.Errorf("no last full scan available")
}
return rs.Value[0].Id, nil
}
func (c SASTClient) GetScanSourceCodeByIDSOAP(scanId uint64) ([]byte, error) {
var zipfile []byte
type Envelope struct {
XMLName xml.Name `xml:"Envelope"`
Text string `xml:",chardata"`
Soap string `xml:"soap,attr"`
Xsi string `xml:"xsi,attr"`
Xsd string `xml:"xsd,attr"`
Body struct {
Text string `xml:",chardata"`
GetSourceCodeForScanResponse struct {
Text string `xml:",chardata"`
Xmlns string `xml:"xmlns,attr"`
GetSourceCodeForScanResult struct {
Text string `xml:",chardata"`
IsSuccesfull string `xml:"IsSuccesfull"`
SourceCodeContainer struct {
Text string `xml:",chardata"`
ZippedFile string `xml:"ZippedFile"`
FileName string `xml:"FileName"`
} `xml:"sourceCodeContainer"`
} `xml:"GetSourceCodeForScanResult"`
} `xml:"GetSourceCodeForScanResponse"`
} `xml:"Body"`
}
response, err := c.sendSOAPRequestAudit_v7("GetSourceCodeForScan", fmt.Sprintf("<sessionID></sessionID><scanId>%v</scanId>", scanId))
if err != nil {
return zipfile, err
}
var env Envelope
if err = xml.Unmarshal(response, &env); err != nil {
return zipfile, err
}
return base64.StdEncoding.DecodeString(env.Body.GetSourceCodeForScanResponse.GetSourceCodeForScanResult.SourceCodeContainer.ZippedFile)
}
func (c SASTClient) GetSourcesByScanIDSOAP(scanId uint64, files []string) ([]SourceFile, error) {
sourceFiles := make([]SourceFile, 0)
type Envelope struct {
XMLName xml.Name `xml:"Envelope"`
Text string `xml:",chardata"`
Soap string `xml:"soap,attr"`
Xsi string `xml:"xsi,attr"`
Xsd string `xml:"xsd,attr"`
Body struct {
Text string `xml:",chardata"`
GetSourcesByScanIDResponse struct {
Text string `xml:",chardata"`
Xmlns string `xml:"xmlns,attr"`
GetSourcesByScanIDResult struct {
Text string `xml:",chardata"`
IsSuccesfull string `xml:"IsSuccesfull"`
CxWSResponseSourcesContent struct {
Text string `xml:",chardata"`
CxWSResponseSourceContent []struct {
Text string `xml:",chardata"`
IsSuccesfull string `xml:"IsSuccesfull"`
Source string `xml:"Source"`
} `xml:"CxWSResponseSourceContent"`
} `xml:"cxWSResponseSourcesContent"`
Encode string `xml:"Encode"`
} `xml:"GetSourcesByScanIDResult"`
} `xml:"GetSourcesByScanIDResponse"`
} `xml:"Body"`
}
normalizedFiles := []string{}
for _, file := range files {
if file[0] != '/' && file[0] != '\\' {
normalizedFiles = append(normalizedFiles, "/"+file)
} else {
normalizedFiles = append(normalizedFiles, file)
}
}
//c.logger.Tracef("Download scan %d files: %v", scanId, strings.Join(normalizedFiles, ", "))
response, err := c.sendSOAPRequest("GetSourcesByScanID", fmt.Sprintf("<sessionID></sessionID><scanID>%v</scanID><filesToRetreive><string>%v</string></filesToRetreive>", scanId, strings.Join(normalizedFiles, "</string><string>")))
if err != nil {
return sourceFiles, err
}
var env Envelope
if err = xml.Unmarshal(response, &env); err != nil {
return sourceFiles, err
}
for id, f := range env.Body.GetSourcesByScanIDResponse.GetSourcesByScanIDResult.CxWSResponseSourcesContent.CxWSResponseSourceContent {
sourceFiles = append(sourceFiles, SourceFile{
Filename: files[id],
Source: f.Source,
})
}
return sourceFiles, nil
}
func (c SASTClient) GetScanSettingsByIDSOAP(scanId uint64) (ScanSettingsSOAP, error) {
type Envelope struct {
XMLName xml.Name `xml:"Envelope"`
Text string `xml:",chardata"`
Soap string `xml:"soap,attr"`
Xsi string `xml:"xsi,attr"`
Xsd string `xml:"xsd,attr"`
Body struct {
Text string `xml:",chardata"`
GetScanSummaryResponse struct {
Text string `xml:",chardata"`
Xmlns string `xml:"xmlns,attr"`
GetScanSummaryResult ScanSettingsSOAP `xml:"GetScanSummaryResult"`
} `xml:"GetScanSummaryResponse"`
} `xml:"Body"`
}
response, err := c.sendSOAPRequest("GetScanSummary", fmt.Sprintf("<i_SessionID></i_SessionID><i_ScanID>%v</i_ScanID>", scanId))
if err != nil {
return ScanSettingsSOAP{}, err
}
var env Envelope
if err = xml.Unmarshal(response, &env); err != nil {
return ScanSettingsSOAP{}, err
}
return env.Body.GetScanSummaryResponse.GetScanSummaryResult, nil
}
func (s *Scan) String() string {
return fmt.Sprintf("Scan %d - Project %d, %d LOC: %v %v", s.ScanID, s.Project.ID, s.ScanState.LOC, s.Status.Name, s.DateAndTime.FinishedOn.Format(time.RFC3339))
}
func (c SASTClient) GetAllPathResultInfos(scanId uint64) ([]PathResultInfo, error) {
var pris []PathResultInfo
results, err := c.GetResultsForScanSOAP(scanId)
if err != nil {
return pris, err
}
sourceFiles := []string{}
for _, r := range results {
if len(r.Nodes) > 0 {
if !slices.Contains(sourceFiles, r.Nodes[0].FileName) {
sourceFiles = append(sourceFiles, r.Nodes[0].FileName)
}
if !slices.Contains(sourceFiles, r.Nodes[len(r.Nodes)-1].FileName) {
sourceFiles = append(sourceFiles, r.Nodes[len(r.Nodes)-1].FileName)
}
}
}
//c.logger.Tracef("Expecting to get %d files", len(sourceFiles))
code := make(map[string][]string)
batchSize := 10
for start := 0; start < len(sourceFiles); start += batchSize {
end := start + batchSize
if end > len(sourceFiles) {
end = len(sourceFiles)
}
//c.logger.Tracef("Downloading files: %v", strings.Join(sourceFiles[start:end], ", "))
files, err := c.GetSourcesByScanIDSOAP(scanId, sourceFiles[start:end])
if err != nil {
return pris, err
}
for _, sf := range files {
code[sf.Filename] = regexp.MustCompile("\r?\n").Split(sf.Source, -1)
//c.logger.Tracef("Saved source for %v", sf.Filename)
}
}
for _, r := range results {
if len(r.Nodes) > 0 {
lastnode := len(r.Nodes) - 1
pris = append(pris, PathResultInfo{
Source1: code[r.Nodes[0].FileName],
AbsoluteFileName1: r.Nodes[0].FileName,
Line1: r.Nodes[0].Line,
Column1: r.Nodes[0].Column,
MethodLine1: r.Nodes[0].MethodLine,
Source2: code[r.Nodes[lastnode].FileName],
AbsoluteFileName2: r.Nodes[lastnode].FileName,
Line2: r.Nodes[lastnode].Line,
Column2: r.Nodes[lastnode].Column,
MethodLine2: r.Nodes[lastnode].MethodLine,
QueryID: r.QueryID,
State: r.State,
PathID: r.PathID,
Severity: r.Severity,
SimilarityID: r.SimilarityID,
Comment: r.Comment,
})
}
}
return pris, nil
}
func (c SASTClient) GetScanCustomStateUsedODATA(scanId uint64, stateId uint) (bool, error) {
response, err := c.sendODATARequest(fmt.Sprintf("v1/Results?$filter=ScanId%%20eq%%20%d%%20and%%20StateId%%20eq%%20%d$select=ScanId,StateId&$top=1", scanId, stateId))
if err != nil {
return false, err
}
type responseStruct struct {
Value []struct {
Id uint64 `json:"Id"`
} `json:"value"`
}
var rs responseStruct
err = json.Unmarshal(response, &rs)
if err != nil {
return false, err
}
if len(rs.Value) > 1 {
return true, nil
}
return false, nil
}