-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathreports.go
More file actions
257 lines (222 loc) · 8.55 KB
/
reports.go
File metadata and controls
257 lines (222 loc) · 8.55 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
package Cx1ClientGo
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"strings"
"time"
)
// Reports
// Added the 'sections' variable, originally: "ScanSummary", "ExecutiveSummary", "ScanResults",
func (c *Cx1Client) RequestNewReportByID(scanID, projectID, branch, reportType string, engines, sections []string) (string, error) {
jsonData := map[string]interface{}{
"fileFormat": reportType,
"reportType": "ui",
"reportName": "scan-report",
"data": map[string]interface{}{
"scanId": scanID,
"projectId": projectID,
"branchName": branch,
"sections": sections,
"scanners": engines,
"host": "",
},
}
jsonBody, err := json.Marshal(jsonData)
if err != nil {
return "", err
}
data, err := c.sendRequest(http.MethodPost, "/reports", bytes.NewReader(jsonBody), nil)
if err != nil {
return "", fmt.Errorf("failed to trigger report generation for scan %v: %s", scanID, err)
}
var reportResponse struct {
ReportId string
}
err = json.Unmarshal([]byte(data), &reportResponse)
return reportResponse.ReportId, err
}
// the v2 report is the "improved scan report" which can be used the same as the existing RequestNewReportByID
// returns the report ID which can be passed to GetReportStatusByID or ReportPollingByID
// supports pdf, csv, and json format (not xml)
func (c *Cx1Client) RequestNewReportByIDv2(scanID string, scanners []string, format string) (string, error) {
c.depwarn("RequestNewReportByIDv2", "RequestNewReportByScanIDv2")
return c.RequestNewReportByScanIDv2(scanID, scanners, []string{}, []string{}, format)
}
func (c *Cx1Client) RequestNewReportByScanIDv2(scanID string, scanners, emails, tags []string, format string) (string, error) {
severities := []string{"high", "medium"}
if flag, _ := c.CheckFlag("CVSS_V3_ENABLED"); flag {
severities = append(severities, "critical")
}
return c.RequestNewReportByIDsv2(ReportRequest{
EntityType: "scan",
IDs: []string{scanID},
Sections: []string{"scan-information", "results-overview", "scan-results", "categories", "resolved-results", "vulnerability-details"},
Scanners: scanners,
Severities: severities,
States: []string{"to-verify", "confirmed", "urgent"},
Statuses: []string{"new", "recurrent"},
Emails: emails,
Tags: tags,
Format: format,
})
}
func (c *Cx1Client) RequestNewReportByProjectIDv2(projectIDs, scanners, emails, tags []string, format string) (string, error) {
severities := []string{"high", "medium"}
if flag, _ := c.CheckFlag("CVSS_V3_ENABLED"); flag {
severities = append(severities, "critical")
}
return c.RequestNewReportByIDsv2(ReportRequest{
EntityType: "project",
IDs: projectIDs,
Sections: []string{"projects-overview", "total-vulnerabilities-overview", "vulnerabilities-insights"},
Scanners: scanners,
Severities: severities,
States: []string{"to-verify", "confirmed", "urgent"},
Statuses: []string{"new", "recurrent"},
Emails: emails,
Tags: tags,
Format: format,
})
}
// function used by RequestNewReportByIDv2
func (c *Cx1Client) RequestNewReportByIDsv2(request ReportRequest) (string, error) {
jsonData := map[string]interface{}{
"reportName": fmt.Sprintf("improved-%v-report", request.EntityType),
"sections": request.Sections,
"entities": []map[string]interface{}{
{
"entity": request.EntityType,
"ids": request.IDs,
"tags": request.Tags,
},
},
"filters": map[string][]string{
"scanners": request.Scanners,
"severities": request.Severities,
"states": request.States,
"status": request.Statuses,
},
"reportType": "ui",
"fileFormat": request.Format,
"emails": request.Emails,
}
if request.Timezone != "" {
jsonData["timezone"] = request.Timezone
}
jsonValue, _ := json.Marshal(jsonData)
data, err := c.sendRequest(http.MethodPost, "/reports/v2", bytes.NewReader(jsonValue), nil)
if err != nil {
return "", fmt.Errorf("failed to trigger report v2 generation for %v(s) %v: %s", request.EntityType, strings.Join(request.IDs, ","), err)
}
var reportResponse struct {
ReportId string
}
err = json.Unmarshal(data, &reportResponse)
return reportResponse.ReportId, err
}
func (c *Cx1Client) GetReportStatusByID(reportID string) (ReportStatus, error) {
var response ReportStatus
data, err := c.sendRequest(http.MethodGet, fmt.Sprintf("/reports/%v?returnUrl=true", reportID), nil, nil)
if err != nil {
c.config.Logger.Tracef("Failed to fetch report status for reportID %v: %s", reportID, err)
return response, fmt.Errorf("failed to fetch report status for reportID %v: %s", reportID, err)
}
err = json.Unmarshal([]byte(data), &response)
return response, err
}
func (c *Cx1Client) DownloadReport(reportUrl string) ([]byte, error) {
data, err := c.sendRequestInternal(http.MethodGet, reportUrl, nil, nil)
if err != nil {
return []byte{}, fmt.Errorf("failed to download report from url %v: %s", reportUrl, err)
}
return data, nil
}
// convenience function, polls and returns the URL to download the report
func (c *Cx1Client) ReportPollingByID(reportID string) (string, error) {
return c.ReportPollingByIDWithTimeout(reportID, c.config.Polling.ReportPollingDelaySeconds, c.config.Polling.ReportPollingMaxSeconds)
}
func (c *Cx1Client) ReportPollingByIDWithTimeout(reportID string, delaySeconds, maxSeconds int) (string, error) {
pollingCounter := 0
for {
status, err := c.GetReportStatusByID(reportID)
if err != nil {
return "", err
}
switch status.Status {
case "completed":
return status.ReportURL, nil
case "failed":
return "", fmt.Errorf("report generation failed")
}
if maxSeconds != 0 && pollingCounter > maxSeconds {
return "", fmt.Errorf("report %v polling reached %d seconds, aborting - use cx1client.get/setclientvars to change", ShortenGUID(reportID), pollingCounter)
}
time.Sleep(time.Duration(delaySeconds) * time.Second)
pollingCounter += delaySeconds
}
}
// SCA-specific Export for SBOM
// formats: CycloneDxjson, CycloneDxxml, Spdxjson
func (c *Cx1Client) RequestNewExportByID(scanId, format string, hidePrivatePackages, hideDevAndTestDependencies, showOnlyEffectiveLicenses bool) (string, error) {
jsonData := map[string]interface{}{
"ScanId": scanId,
"FileFormat": format,
"ExportParameters": map[string]interface{}{
"hidePrivatePackages": hidePrivatePackages,
"hideDevAndTestDependencies": hideDevAndTestDependencies,
"showOnlyEffectiveLicenses": showOnlyEffectiveLicenses,
},
}
jsonValue, _ := json.Marshal(jsonData)
data, err := c.sendRequest(http.MethodPost, "/sca/export/requests", bytes.NewReader(jsonValue), nil)
if err != nil {
return "", fmt.Errorf("failed to trigger %v export generation for scan %v: %s", format, scanId, err)
}
var exportResponse struct {
ExportId string
}
err = json.Unmarshal(data, &exportResponse)
return exportResponse.ExportId, err
}
func (c *Cx1Client) GetExportStatusByID(exportID string) (ExportStatus, error) {
var response ExportStatus
data, err := c.sendRequest(http.MethodGet, fmt.Sprintf("/sca/export/requests?exportId=%v", exportID), nil, nil)
if err != nil {
c.config.Logger.Tracef("Failed to fetch export status for exportID %v: %s", exportID, err)
return response, fmt.Errorf("failed to fetch export status for exportID %v: %s", exportID, err)
}
err = json.Unmarshal([]byte(data), &response)
return response, err
}
func (c *Cx1Client) DownloadExport(exportUrl string) ([]byte, error) {
data, err := c.sendRequestInternal(http.MethodGet, exportUrl, nil, nil)
if err != nil {
return []byte{}, fmt.Errorf("failed to download export from url %v: %s", exportUrl, err)
}
return data, nil
}
// convenience function, polls and returns the URL to download the export
func (c *Cx1Client) ExportPollingByID(exportID string) (string, error) {
return c.ExportPollingByIDWithTimeout(exportID, c.config.Polling.ExportPollingDelaySeconds, c.config.Polling.ExportPollingMaxSeconds)
}
func (c *Cx1Client) ExportPollingByIDWithTimeout(exportID string, delaySeconds, maxSeconds int) (string, error) {
pollingCounter := 0
for {
status, err := c.GetExportStatusByID(exportID)
if err != nil {
return "", err
}
if strings.EqualFold(status.Status, "completed") {
return status.ExportURL, nil
} else if strings.EqualFold(status.Status, "failed") {
return "", fmt.Errorf("export generation failed")
}
if maxSeconds != 0 && pollingCounter > maxSeconds {
return "", fmt.Errorf("export %v polling reached %d seconds, aborting - use cx1client.get/setclientvars to change", ShortenGUID(exportID), pollingCounter)
}
time.Sleep(time.Duration(delaySeconds) * time.Second)
pollingCounter += delaySeconds
}
}