-
Notifications
You must be signed in to change notification settings - Fork 132
/
scan.go
410 lines (359 loc) · 14.3 KB
/
scan.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
406
407
408
409
410
package services
import (
"encoding/json"
"net/http"
"strings"
"time"
"github.com/jfrog/jfrog-client-go/utils/log"
xrayUtils "github.com/jfrog/jfrog-client-go/xray/services/utils"
"github.com/jfrog/jfrog-client-go/artifactory/services/utils"
"github.com/jfrog/jfrog-client-go/auth"
"github.com/jfrog/jfrog-client-go/http/jfroghttpclient"
"github.com/jfrog/jfrog-client-go/utils/errorutils"
"github.com/jfrog/jfrog-client-go/utils/io/httputils"
)
const (
scanGraphAPI = "api/v1/scan/graph"
// Graph scan query params
repoPathQueryParam = "repo_path="
projectQueryParam = "project="
watchesQueryParam = "watch="
scanTypeQueryParam = "scan_type="
// Get scan results query params
includeVulnerabilitiesParam = "?include_vulnerabilities=true"
includeLicensesParam = "?include_licenses=true"
andIncludeLicensesParam = "&include_licenses=true"
// Get scan results timeouts
defaultMaxWaitMinutes = 45 * time.Minute // 45 minutes
defaultSyncSleepInterval = 5 * time.Second // 5 seconds
// ScanType values
Dependency ScanType = "dependency"
Binary ScanType = "binary"
xrayScanStatusFailed = "failed"
// Xsc consts
postXscGitInfoContextAPI = "api/v1/gitinfo"
XscGraphAPI = "api/v1/sca/scan/graph"
multiScanIdParam = "multi_scan_id="
scanTechQueryParam = "tech="
XscVersionAPI = "api/v1/system/version"
XraySuffix = "/xray/"
XscSuffix = "/xsc/"
)
type ScanType string
type ScanService struct {
client *jfroghttpclient.JfrogHttpClient
XrayDetails auth.ServiceDetails
}
// NewScanService creates a new service to scan binaries and audit code projects' dependencies.
func NewScanService(client *jfroghttpclient.JfrogHttpClient) *ScanService {
return &ScanService{client: client}
}
func createScanGraphQueryParams(scanParams XrayGraphScanParams) string {
var params []string
switch {
case scanParams.ProjectKey != "":
params = append(params, projectQueryParam+scanParams.ProjectKey)
case scanParams.RepoPath != "":
params = append(params, repoPathQueryParam+scanParams.RepoPath)
case len(scanParams.Watches) > 0:
for _, watch := range scanParams.Watches {
if watch != "" {
params = append(params, watchesQueryParam+watch)
}
}
}
if scanParams.XscVersion != "" {
params = append(params, multiScanIdParam+scanParams.MultiScanId)
gitInfoContext := scanParams.XscGitInfoContext
if gitInfoContext != nil {
if len(gitInfoContext.Technologies) > 0 {
// Append the tech type, each graph can contain only one tech type
params = append(params, scanTechQueryParam+gitInfoContext.Technologies[0])
}
}
}
if scanParams.ScanType != "" {
params = append(params, scanTypeQueryParam+string(scanParams.ScanType))
}
if len(params) == 0 {
return ""
}
return "?" + strings.Join(params, "&")
}
func (ss *ScanService) ScanGraph(scanParams XrayGraphScanParams) (string, error) {
httpClientsDetails := ss.XrayDetails.CreateHttpClientDetails()
utils.SetContentType("application/json", &httpClientsDetails.Headers)
var err error
var requestBody []byte
if scanParams.DependenciesGraph != nil {
requestBody, err = json.Marshal(scanParams.DependenciesGraph)
} else {
requestBody, err = json.Marshal(scanParams.BinaryGraph)
}
if err != nil {
return "", errorutils.CheckError(err)
}
url := ss.XrayDetails.GetUrl() + scanGraphAPI
// When XSC is enabled, modify the URL.
if scanParams.XscVersion != "" {
url = ss.xrayToXscUrl() + XscGraphAPI
}
url += createScanGraphQueryParams(scanParams)
resp, body, err := ss.client.SendPost(url, requestBody, &httpClientsDetails)
if err != nil {
return "", err
}
if err = errorutils.CheckResponseStatusWithBody(resp, body, http.StatusOK, http.StatusCreated); err != nil {
scanErrorJson := ScanErrorJson{}
if e := json.Unmarshal(body, &scanErrorJson); e == nil {
return "", errorutils.CheckErrorf(scanErrorJson.Error)
}
return "", err
}
scanResponse := RequestScanResponse{}
if err = json.Unmarshal(body, &scanResponse); err != nil {
return "", errorutils.CheckError(err)
}
return scanResponse.ScanId, err
}
func (ss *ScanService) GetScanGraphResults(scanId string, includeVulnerabilities, includeLicenses, xscEnabled bool) (*ScanResponse, error) {
httpClientsDetails := ss.XrayDetails.CreateHttpClientDetails()
utils.SetContentType("application/json", &httpClientsDetails.Headers)
// The scan request may take some time to complete. We expect to receive a 202 response, until the completion.
endPoint := ss.XrayDetails.GetUrl() + scanGraphAPI
// Modify endpoint if XSC is enabled
if xscEnabled {
endPoint = ss.xrayToXscUrl() + XscGraphAPI
}
endPoint += "/" + scanId
if includeVulnerabilities {
endPoint += includeVulnerabilitiesParam
if includeLicenses {
endPoint += andIncludeLicensesParam
}
} else if includeLicenses {
endPoint += includeLicensesParam
}
log.Info("Waiting for scan to complete on JFrog Xray...")
pollingAction := func() (shouldStop bool, responseBody []byte, err error) {
resp, body, _, err := ss.client.SendGet(endPoint, true, &httpClientsDetails)
if err != nil {
return true, nil, err
}
if err = errorutils.CheckResponseStatusWithBody(resp, body, http.StatusOK, http.StatusAccepted); err != nil {
return true, nil, err
}
// Got the full valid response.
if resp.StatusCode == http.StatusOK {
return true, body, nil
}
return false, nil, nil
}
pollingExecutor := &httputils.PollingExecutor{
Timeout: defaultMaxWaitMinutes,
PollingInterval: defaultSyncSleepInterval,
PollingAction: pollingAction,
MsgPrefix: "Get Dependencies Scan results... ",
}
body, err := pollingExecutor.Execute()
if err != nil {
return nil, err
}
scanResponse := ScanResponse{}
if err = json.Unmarshal(body, &scanResponse); err != nil {
return nil, errorutils.CheckErrorf("couldn't parse JFrog Xray server response: " + err.Error())
}
if scanResponse.ScannedStatus == xrayScanStatusFailed {
// Failed due to an internal Xray error
return nil, errorutils.CheckErrorf("received a failure status from JFrog Xray server:\n%s", errorutils.GenerateErrorString(body))
}
return &scanResponse, err
}
func (ss *ScanService) xrayToXscUrl() string {
return strings.Replace(ss.XrayDetails.GetUrl(), XraySuffix, XscSuffix, 1)
}
func (ss *ScanService) SendScanGitInfoContext(details *XscGitInfoContext) (multiScanId string, err error) {
httpClientsDetails := ss.XrayDetails.CreateHttpClientDetails()
utils.SetContentType("application/json", &httpClientsDetails.Headers)
requestBody, err := json.Marshal(details)
if err != nil {
return "", errorutils.CheckError(err)
}
url := ss.xrayToXscUrl() + postXscGitInfoContextAPI
resp, body, err := ss.client.SendPost(url, requestBody, &httpClientsDetails)
if err != nil {
return
}
if err = errorutils.CheckResponseStatusWithBody(resp, body, http.StatusCreated); err != nil {
return
}
xscResponse := XscPostContextResponse{}
if err = json.Unmarshal(body, &xscResponse); err != nil {
return "", errorutils.CheckError(err)
}
return xscResponse.MultiScanId, err
}
// IsXscEnabled will try to get XSC version. If route is not available, user is not entitled for XSC.
func (ss *ScanService) IsXscEnabled() (xsxVersion string, err error) {
httpClientsDetails := ss.XrayDetails.CreateHttpClientDetails()
url := ss.XrayDetails.GetUrl()
// If Xray suffix not found, Xsc is not supported.
if !strings.HasSuffix(url, XraySuffix) {
return
}
url = ss.xrayToXscUrl()
resp, body, _, err := ss.client.SendGet(url+XscVersionAPI, false, &httpClientsDetails)
if err != nil {
err = errorutils.CheckErrorf("failed to get JFrog XSC version, response: " + err.Error())
return
}
if err = errorutils.CheckResponseStatusWithBody(resp, body, http.StatusOK, http.StatusNotFound); err != nil {
return
}
// When XSC is disabled, StatusNotFound is expected. Don't return error as this is optional.
if resp.StatusCode == http.StatusNotFound {
return
}
versionResponse := XscVersionResponse{}
if err = json.Unmarshal(body, &versionResponse); err != nil {
err = errorutils.CheckErrorf("failed to parse JFrog XSC server response: " + err.Error())
return
}
xsxVersion = versionResponse.Version
log.Debug("XSC version:", xsxVersion)
return
}
type XrayGraphScanParams struct {
// A path in Artifactory that this Artifact is intended to be deployed to.
// This will provide a way to extract the watches that should be applied on this graph
RepoPath string
ProjectKey string
Watches []string
ScanType ScanType
// Dependencies Tree
DependenciesGraph *xrayUtils.GraphNode
// Binary tree received from indexer-app
BinaryGraph *xrayUtils.BinaryGraphNode
IncludeVulnerabilities bool
IncludeLicenses bool
XscGitInfoContext *XscGitInfoContext
XscVersion string
MultiScanId string
}
type RequestScanResponse struct {
ScanId string `json:"scan_id,omitempty"`
}
type ScanErrorJson struct {
Error string `json:"error"`
}
type ScanResponse struct {
ScanId string `json:"scan_id,omitempty"`
XrayDataUrl string `json:"xray_data_url,omitempty"`
Violations []Violation `json:"violations,omitempty"`
Vulnerabilities []Vulnerability `json:"vulnerabilities,omitempty"`
Licenses []License `json:"licenses,omitempty"`
ScannedComponentId string `json:"component_id,omitempty"`
ScannedPackageType string `json:"package_type,omitempty"`
ScannedStatus string `json:"status,omitempty"`
}
type Violation struct {
Summary string `json:"summary,omitempty"`
Severity string `json:"severity,omitempty"`
ViolationType string `json:"type,omitempty"`
Components map[string]Component `json:"components,omitempty"`
WatchName string `json:"watch_name,omitempty"`
IssueId string `json:"issue_id,omitempty"`
Cves []Cve `json:"cves,omitempty"`
References []string `json:"references,omitempty"`
FailBuild bool `json:"fail_build,omitempty"`
LicenseKey string `json:"license_key,omitempty"`
LicenseName string `json:"license_name,omitempty"`
IgnoreUrl string `json:"ignore_url,omitempty"`
RiskReason string `json:"risk_reason,omitempty"`
IsEol *bool `json:"is_eol,omitempty"`
EolMessage string `json:"eol_message,omitempty"`
LatestVersion string `json:"latest_version,omitempty"`
NewerVersions *int `json:"newer_versions,omitempty"`
Cadence *float64 `json:"cadence,omitempty"`
Commits *int64 `json:"commits,omitempty"`
Committers *int `json:"committers,omitempty"`
ExtendedInformation *ExtendedInformation `json:"extended_information,omitempty"`
Technology string `json:"-"`
}
type Vulnerability struct {
Cves []Cve `json:"cves,omitempty"`
Summary string `json:"summary,omitempty"`
Severity string `json:"severity,omitempty"`
Components map[string]Component `json:"components,omitempty"`
IssueId string `json:"issue_id,omitempty"`
References []string `json:"references,omitempty"`
ExtendedInformation *ExtendedInformation `json:"extended_information,omitempty"`
Technology string `json:"-"`
}
type License struct {
Key string `json:"license_key,omitempty"`
Name string `json:"name,omitempty"`
Components map[string]Component `json:"components,omitempty"`
Custom bool `json:"custom,omitempty"`
References []string `json:"references,omitempty"`
}
type Component struct {
FixedVersions []string `json:"fixed_versions,omitempty"`
ImpactPaths [][]ImpactPathNode `json:"impact_paths,omitempty"`
Cpes []string `json:"cpes,omitempty"`
}
type ImpactPathNode struct {
ComponentId string `json:"component_id,omitempty"`
FullPath string `json:"full_path,omitempty"`
}
type Cve struct {
Id string `json:"cve,omitempty"`
CvssV2Score string `json:"cvss_v2_score,omitempty"`
CvssV2Vector string `json:"cvss_v2_vector,omitempty"`
CvssV3Score string `json:"cvss_v3_score,omitempty"`
CvssV3Vector string `json:"cvss_v3_vector,omitempty"`
Cwe []string `json:"cwe,omitempty"`
CweDetails map[string]Cwe `json:"cwe_details,omitempty"`
}
type Cwe struct {
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
Categories []CweCategory `json:"categories,omitempty"`
}
type CweCategory struct {
Category string `json:"category,omitempty"`
Rank string `json:"rank,omitempty"`
}
type ExtendedInformation struct {
ShortDescription string `json:"short_description,omitempty"`
FullDescription string `json:"full_description,omitempty"`
JfrogResearchSeverity string `json:"jfrog_research_severity,omitempty"`
JfrogResearchSeverityReasons []JfrogResearchSeverityReason `json:"jfrog_research_severity_reasons,omitempty"`
Remediation string `json:"remediation,omitempty"`
}
type JfrogResearchSeverityReason struct {
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
IsPositive bool `json:"is_positive,omitempty"`
}
type XscPostContextResponse struct {
MultiScanId string `json:"multi_scan_id,omitempty"`
}
type XscVersionResponse struct {
Version string `json:"xsc_version"`
}
type XscGitInfoContext struct {
GitRepoUrl string `json:"git_repo_url"`
GitRepoName string `json:"git_repo_name,omitempty"`
GitProject string `json:"git_project,omitempty"`
GitProvider string `json:"git_provider,omitempty"`
Technologies []string `json:"technologies,omitempty"`
BranchName string `json:"branch_name"`
LastCommit string `json:"last_commit,omitempty"`
CommitHash string `json:"commit_hash"`
CommitMessage string `json:"commit_message,omitempty"`
CommitAuthor string `json:"commit_author,omitempty"`
}
func (gp *XrayGraphScanParams) GetProjectKey() string {
return gp.ProjectKey
}