/
reports.go
115 lines (97 loc) · 3.41 KB
/
reports.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
package CxSASTClientGo
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"time"
"github.com/pkg/errors"
)
func (c SASTClient) RequestNewReport(scanID uint64, reportType string) (Report, error) {
c.depwarn("RequestNewReport", "RequestNewReportByID")
return c.RequestNewReportByID(scanID, reportType)
}
func (c SASTClient) RequestNewReportByID(scanID uint64, reportType string) (Report, error) {
report := Report{}
jsonData := map[string]interface{}{
"scanId": scanID,
"reportType": reportType,
"comment": "Scan report triggered by CxSASTClientGo",
}
jsonValue, _ := json.Marshal(jsonData)
header := http.Header{}
header.Set("cxOrigin", "GolangScript")
header.Set("Content-Type", "application/json")
data, err := c.sendRequest(http.MethodPost, "/reports/sastScan", bytes.NewReader(jsonValue), header)
if err != nil {
return report, errors.Wrapf(err, "Failed to trigger report generation for scan %d", scanID)
}
err = json.Unmarshal(data, &report)
return report, err
}
func (c SASTClient) GetReportStatus(reportID uint64) (ReportStatusResponse, error) {
c.depwarn("GetReportStatus", "GetReportStatusByID")
return c.GetReportStatusByID(reportID)
}
func (c SASTClient) GetReportStatusByID(reportID uint64) (ReportStatusResponse, error) {
var response ReportStatusResponse
header := http.Header{}
header.Set("Accept", "application/json")
data, err := c.sendRequest(http.MethodGet, fmt.Sprintf("/reports/sastScan/%d/status", reportID), nil, header)
if err != nil {
c.logger.Errorf("Failed to fetch report status for reportID %d: %s", reportID, err)
return response, errors.Wrapf(err, "failed to fetch report status for reportID %d", reportID)
}
json.Unmarshal(data, &response)
return response, nil
}
func (c SASTClient) DownloadReport(reportID uint64) ([]byte, error) {
c.depwarn("DownloadReport", "DownloadReportByID")
return c.DownloadReportByID(reportID)
}
func (c SASTClient) DownloadReportByID(reportID uint64) ([]byte, error) {
header := http.Header{}
header.Set("Accept", "application/json")
data, err := c.sendRequest(http.MethodGet, fmt.Sprintf("/reports/sastScan/%d", reportID), nil, header)
if err != nil {
return []byte{}, errors.Wrapf(err, "failed to download report with reportID %d", reportID)
}
return data, nil
}
// convenience function
func (c SASTClient) GenerateAndDownloadReport(scanID uint64, reportType string) ([]byte, error) {
c.depwarn("GenerateAndDownloadReport", "GenerateAndDownloadReportByID")
return c.GenerateAndDownloadReportByID(scanID, reportType)
}
func (c SASTClient) GenerateAndDownloadReportByID(scanID uint64, reportType string) ([]byte, error) {
var reportBytes []byte
report, err := c.RequestNewReport(scanID, reportType)
if err != nil {
c.logger.Error("Error requesting report: " + err.Error())
return reportBytes, err
}
finalStatus := 1
for {
reportStatus, err := c.GetReportStatus(report.ReportID)
if err != nil {
c.logger.Error("Error generating report: " + err.Error())
return reportBytes, err
}
finalStatus = reportStatus.Status.ID
if finalStatus != 1 {
break
}
time.Sleep(10 * time.Second)
}
if finalStatus == 2 {
reportBytes, err = c.DownloadReport(report.ReportID)
if err != nil {
c.logger.Error("Error downloading report: " + err.Error())
return reportBytes, err
}
} else {
c.logger.Info("Failure during report generation")
return reportBytes, errors.New("Failed during report generation")
}
return reportBytes, nil
}