Skip to content

Commit b5332a3

Browse files
authored
Merge branch 'dev' into update_cli_deps
2 parents e1570e9 + 8dbf035 commit b5332a3

25 files changed

+632
-465
lines changed

audit_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,7 @@ func getNoJasAuditMockCommand() components.Command {
575575
Name: docs.Audit,
576576
Flags: docs.GetCommandFlags(docs.Audit),
577577
Action: func(c *components.Context) error {
578-
auditCmd, err := cli.CreateAuditCmd(c)
578+
_, _, _, auditCmd, err := cli.CreateAuditCmd(c)
579579
if err != nil {
580580
return err
581581
}

cli/scancommands.go

Lines changed: 54 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,10 @@ func ScanCmd(c *components.Context) error {
206206
if err != nil {
207207
return err
208208
}
209+
xrayVersion, xscVersion, err := GetJfrogServicesVersion(serverDetails)
210+
if err != nil {
211+
return err
212+
}
209213
var specFile *spec.SpecFiles
210214
if c.IsFlagSet(flags.SpecFlag) && len(c.GetStringFlagValue(flags.SpecFlag)) > 0 {
211215
specFile, err = pluginsCommon.GetFileSystemSpec(c)
@@ -233,6 +237,8 @@ func ScanCmd(c *components.Context) error {
233237
return err
234238
}
235239
scanCmd := scan.NewScanCommand().
240+
SetXrayVersion(xrayVersion).
241+
SetXscVersion(xscVersion).
236242
SetServerDetails(serverDetails).
237243
SetThreads(threads).
238244
SetSpec(specFile).
@@ -369,7 +375,7 @@ func BuildScan(c *components.Context) error {
369375
}
370376

371377
func AuditCmd(c *components.Context) error {
372-
auditCmd, err := CreateAuditCmd(c)
378+
xrayVersion, xscVersion, serverDetails, auditCmd, err := CreateAuditCmd(c)
373379
if err != nil {
374380
return err
375381
}
@@ -419,7 +425,7 @@ func AuditCmd(c *components.Context) error {
419425
auditCmd.SetThreads(threads)
420426
err = progressbar.ExecWithProgress(auditCmd)
421427
// Reporting error if Xsc service is enabled
422-
reportErrorIfExists(err, auditCmd)
428+
reportErrorIfExists(xrayVersion, xscVersion, serverDetails, err)
423429
return err
424430
}
425431

@@ -428,46 +434,42 @@ func shouldAddSubScan(subScan utils.SubScanType, c *components.Context) bool {
428434
(subScan == utils.ContextualAnalysisScan && c.GetBoolFlagValue(flags.Sca) && !c.GetBoolFlagValue(flags.WithoutCA)) || (subScan == utils.SecretTokenValidationScan && c.GetBoolFlagValue(flags.Secrets) && c.GetBoolFlagValue(flags.SecretValidation))
429435
}
430436

431-
func reportErrorIfExists(err error, auditCmd *audit.AuditCommand) {
437+
func reportErrorIfExists(xrayVersion, xscVersion string, serverDetails *coreConfig.ServerDetails, err error) {
432438
if err == nil || !usage.ShouldReportUsage() {
433439
return
434440
}
435-
var serverDetails *coreConfig.ServerDetails
436-
serverDetails, innerError := auditCmd.ServerDetails()
437-
if innerError != nil {
438-
log.Debug(fmt.Sprintf("failed to get server details for error report: %q", innerError))
439-
return
440-
}
441-
if reportError := xsc.ReportError(serverDetails, err, "cli"); reportError != nil {
441+
if reportError := xsc.ReportError(xrayVersion, xscVersion, serverDetails, err, "cli"); reportError != nil {
442442
log.Debug("failed to report error log:" + reportError.Error())
443443
}
444444
}
445445

446-
func CreateAuditCmd(c *components.Context) (*audit.AuditCommand, error) {
446+
func CreateAuditCmd(c *components.Context) (string, string, *coreConfig.ServerDetails, *audit.AuditCommand, error) {
447447
auditCmd := audit.NewGenericAuditCommand()
448448
serverDetails, err := createServerDetailsWithConfigOffer(c)
449449
if err != nil {
450-
return nil, err
450+
return "", "", nil, nil, err
451451
}
452452
err = validateXrayContext(c, serverDetails)
453453
if err != nil {
454-
return nil, err
454+
return "", "", nil, nil, err
455+
}
456+
xrayVersion, xscVersion, err := GetJfrogServicesVersion(serverDetails)
457+
if err != nil {
458+
return "", "", nil, nil, err
455459
}
456460
format, err := outputFormat.GetOutputFormat(c.GetStringFlagValue(flags.OutputFormat))
457461
if err != nil {
458-
return nil, err
462+
return "", "", nil, nil, err
459463
}
460464
minSeverity, err := getMinimumSeverity(c)
461465
if err != nil {
462-
return nil, err
466+
return "", "", nil, nil, err
463467
}
464468
scansOutputDir, err := getAndValidateOutputDirExistsIfProvided(c)
465469
if err != nil {
466-
return nil, err
470+
return "", "", nil, nil, err
467471
}
468472

469-
auditCmd.SetAnalyticsMetricsService(xsc.NewAnalyticsMetricsService(serverDetails))
470-
471473
auditCmd.SetTargetRepoPath(addTrailingSlashToRepoPathIfNeeded(c)).
472474
SetProject(getProject(c)).
473475
SetIncludeVulnerabilities(c.GetBoolFlagValue(flags.Vuln) || shouldIncludeVulnerabilities(c)).
@@ -489,6 +491,8 @@ func CreateAuditCmd(c *components.Context) (*audit.AuditCommand, error) {
489491
auditCmd.SetWorkingDirs(splitByCommaAndTrim(c.GetStringFlagValue(flags.WorkingDirs)))
490492
}
491493
auditCmd.SetServerDetails(serverDetails).
494+
SetXrayVersion(xrayVersion).
495+
SetXscVersion(xscVersion).
492496
SetExcludeTestDependencies(c.GetBoolFlagValue(flags.ExcludeTestDeps)).
493497
SetOutputFormat(format).
494498
SetUseJas(true).
@@ -497,7 +501,7 @@ func CreateAuditCmd(c *components.Context) (*audit.AuditCommand, error) {
497501
SetNpmScope(c.GetStringFlagValue(flags.DepType)).
498502
SetPipRequirementsFile(c.GetStringFlagValue(flags.RequirementsFile)).
499503
SetExclusions(pluginsCommon.GetStringsArrFlagValue(c, flags.Exclusions))
500-
return auditCmd, err
504+
return xrayVersion, xscVersion, serverDetails, auditCmd, err
501505
}
502506

503507
func logNonGenericAuditCommandDeprecation(cmdName string) {
@@ -513,7 +517,7 @@ func logNonGenericAuditCommandDeprecation(cmdName string) {
513517

514518
func AuditSpecificCmd(c *components.Context, technology techutils.Technology) error {
515519
logNonGenericAuditCommandDeprecation(c.CommandName)
516-
auditCmd, err := CreateAuditCmd(c)
520+
xrayVersion, xscVersion, serverDetails, auditCmd, err := CreateAuditCmd(c)
517521
if err != nil {
518522
return err
519523
}
@@ -522,7 +526,7 @@ func AuditSpecificCmd(c *components.Context, technology techutils.Technology) er
522526
err = progressbar.ExecWithProgress(auditCmd)
523527

524528
// Reporting error if Xsc service is enabled
525-
reportErrorIfExists(err, auditCmd)
529+
reportErrorIfExists(xrayVersion, xscVersion, serverDetails, err)
526530
return err
527531
}
528532

@@ -709,6 +713,10 @@ func DockerScan(c *components.Context, image string) error {
709713
if err != nil {
710714
return err
711715
}
716+
xrayVersion, xscVersion, err := GetJfrogServicesVersion(serverDetails)
717+
if err != nil {
718+
return err
719+
}
712720
containerScanCommand := scan.NewDockerScanCommand()
713721
format, err := outputFormat.GetOutputFormat(c.GetStringFlagValue(flags.OutputFormat))
714722
if err != nil {
@@ -721,6 +729,8 @@ func DockerScan(c *components.Context, image string) error {
721729
containerScanCommand.SetImageTag(image).
722730
SetTargetRepoPath(addTrailingSlashToRepoPathIfNeeded(c)).
723731
SetServerDetails(serverDetails).
732+
SetXrayVersion(xrayVersion).
733+
SetXscVersion(xscVersion).
724734
SetOutputFormat(format).
725735
SetProject(getProject(c)).
726736
SetIncludeVulnerabilities(c.GetBoolFlagValue(flags.Vuln) || shouldIncludeVulnerabilities(c)).
@@ -731,10 +741,32 @@ func DockerScan(c *components.Context, image string) error {
731741
SetFixableOnly(c.GetBoolFlagValue(flags.FixableOnly)).
732742
SetMinSeverityFilter(minSeverity).
733743
SetThreads(threads).
734-
SetAnalyticsMetricsService(xsc.NewAnalyticsMetricsService(serverDetails)).
735744
SetSecretValidation(c.GetBoolFlagValue(flags.SecretValidation))
736745
if c.GetStringFlagValue(flags.Watches) != "" {
737746
containerScanCommand.SetWatches(splitByCommaAndTrim(c.GetStringFlagValue(flags.Watches)))
738747
}
739748
return progressbar.ExecWithProgress(containerScanCommand)
740749
}
750+
751+
func GetJfrogServicesVersion(serverDetails *coreConfig.ServerDetails) (xrayVersion, xscVersion string, err error) {
752+
xrayManager, err := xray.CreateXrayServiceManager(serverDetails)
753+
if err != nil {
754+
return
755+
}
756+
xrayVersion, err = xrayManager.GetVersion()
757+
if err != nil {
758+
return
759+
}
760+
log.Debug("Xray version: " + xrayVersion)
761+
xscService, err := xsc.CreateXscService(xrayVersion, serverDetails)
762+
if err != nil {
763+
return
764+
}
765+
xscVersion, e := xscService.GetVersion()
766+
if e != nil {
767+
log.Debug("Using Xray: " + e.Error())
768+
return
769+
}
770+
log.Debug("XSC version: " + xscVersion)
771+
return
772+
}

commands/audit/audit.go

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,14 @@ import (
3333
)
3434

3535
type AuditCommand struct {
36-
watches []string
37-
projectKey string
38-
targetRepoPath string
39-
IncludeVulnerabilities bool
40-
IncludeLicenses bool
41-
Fail bool
42-
PrintExtendedTable bool
43-
analyticsMetricsService *xsc.AnalyticsMetricsService
44-
Threads int
36+
watches []string
37+
projectKey string
38+
targetRepoPath string
39+
IncludeVulnerabilities bool
40+
IncludeLicenses bool
41+
Fail bool
42+
PrintExtendedTable bool
43+
Threads int
4544
AuditParams
4645
}
4746

@@ -84,11 +83,6 @@ func (auditCmd *AuditCommand) SetPrintExtendedTable(printExtendedTable bool) *Au
8483
return auditCmd
8584
}
8685

87-
func (auditCmd *AuditCommand) SetAnalyticsMetricsService(analyticsMetricsService *xsc.AnalyticsMetricsService) *AuditCommand {
88-
auditCmd.analyticsMetricsService = analyticsMetricsService
89-
return auditCmd
90-
}
91-
9286
func (auditCmd *AuditCommand) SetThreads(threads int) *AuditCommand {
9387
auditCmd.Threads = threads
9488
return auditCmd
@@ -103,7 +97,6 @@ func (auditCmd *AuditCommand) CreateCommonGraphScanParams() *scangraph.CommonGra
10397
commonParams.ProjectKey = auditCmd.projectKey
10498
commonParams.IncludeVulnerabilities = auditCmd.IncludeVulnerabilities
10599
commonParams.IncludeLicenses = auditCmd.IncludeLicenses
106-
commonParams.MultiScanId, commonParams.XscVersion = xsc.GetXscMsiAndVersion(auditCmd.analyticsMetricsService)
107100
return commonParams
108101
}
109102

@@ -114,9 +107,18 @@ func (auditCmd *AuditCommand) Run() (err error) {
114107
if err != nil {
115108
return
116109
}
110+
serverDetails, err := auditCmd.ServerDetails()
111+
if err != nil {
112+
return
113+
}
114+
115+
multiScanId, startTime := xsc.SendNewScanEvent(
116+
auditCmd.GetXrayVersion(),
117+
auditCmd.GetXscVersion(),
118+
serverDetails,
119+
xsc.CreateAnalyticsEvent(xscservices.CliProduct, xscservices.CliEventType, serverDetails),
120+
)
117121

118-
// Should be called before creating the audit params, so the params will contain XSC information.
119-
auditCmd.analyticsMetricsService.AddGeneralEvent(auditCmd.analyticsMetricsService.CreateGeneralEvent(xscservices.CliProduct, xscservices.CliEventType))
120122
auditParams := NewAuditParams().
121123
SetWorkingDirs(workingDirs).
122124
SetMinSeverityFilter(auditCmd.minSeverityFilter).
@@ -125,11 +127,12 @@ func (auditCmd *AuditCommand) Run() (err error) {
125127
SetCommonGraphScanParams(auditCmd.CreateCommonGraphScanParams()).
126128
SetThirdPartyApplicabilityScan(auditCmd.thirdPartyApplicabilityScan).
127129
SetThreads(auditCmd.Threads).
128-
SetScansResultsOutputDir(auditCmd.scanResultsOutputDir)
130+
SetScansResultsOutputDir(auditCmd.scanResultsOutputDir).SetStartTime(startTime).SetMultiScanId(multiScanId)
129131
auditParams.SetIsRecursiveScan(isRecursiveScan).SetExclusions(auditCmd.Exclusions())
130132

131133
auditResults := RunAudit(auditParams)
132-
auditCmd.analyticsMetricsService.UpdateGeneralEvent(auditCmd.analyticsMetricsService.CreateXscAnalyticsGeneralEventFinalizeFromAuditResults(auditResults))
134+
135+
xsc.SendScanEndedWithResults(serverDetails, auditResults)
133136

134137
if auditCmd.Progress() != nil {
135138
if err = auditCmd.Progress().Quit(); err != nil {
@@ -217,7 +220,7 @@ func isEntitledForJas(xrayManager *xray.XrayServicesManager, auditParams *AuditP
217220
// Dry run without JAS
218221
return false, nil
219222
}
220-
return jas.IsEntitledForJas(xrayManager, auditParams.xrayVersion)
223+
return jas.IsEntitledForJas(xrayManager, auditParams.GetXrayVersion())
221224
}
222225

223226
func RunJasScans(auditParallelRunner *utils.SecurityParallelRunner, auditParams *AuditParams, scanResults *results.SecurityCommandResults, jfrogAppsConfig *jfrogappsconfig.JFrogAppsConfig) (jasScanner *jas.JasScanner, generalError error) {
@@ -231,7 +234,7 @@ func RunJasScans(auditParallelRunner *utils.SecurityParallelRunner, auditParams
231234
return
232235
}
233236
auditParallelRunner.ResultsMu.Lock()
234-
jasScanner, err = jas.CreateJasScanner(serverDetails, scanResults.SecretValidation, auditParams.minSeverityFilter, jas.GetAnalyzerManagerXscEnvVars(auditParams.commonGraphScanParams.MultiScanId, scanResults.GetTechnologies()...), auditParams.Exclusions()...)
237+
jasScanner, err = jas.CreateJasScanner(serverDetails, scanResults.SecretValidation, auditParams.minSeverityFilter, jas.GetAnalyzerManagerXscEnvVars(auditParams.GetMultiScanId(), scanResults.GetTechnologies()...), auditParams.Exclusions()...)
235238
auditParallelRunner.ResultsMu.Unlock()
236239
if err != nil {
237240
generalError = fmt.Errorf("failed to create jas scanner: %s", err.Error())
@@ -300,13 +303,16 @@ func initAuditCmdResults(params *AuditParams) (cmdResults *results.SecurityComma
300303
if err != nil {
301304
return cmdResults.AddGeneralError(err, false)
302305
}
303-
var xrayManager *xray.XrayServicesManager
304-
if xrayManager, params.xrayVersion, err = xrayutils.CreateXrayServiceManagerAndGetVersion(serverDetails); err != nil {
306+
if err = clientutils.ValidateMinimumVersion(clientutils.Xray, params.GetXrayVersion(), scangraph.GraphScanMinXrayVersion); err != nil {
305307
return cmdResults.AddGeneralError(err, false)
306-
} else {
307-
cmdResults.SetXrayVersion(params.xrayVersion)
308308
}
309-
if err = clientutils.ValidateMinimumVersion(clientutils.Xray, params.xrayVersion, scangraph.GraphScanMinXrayVersion); err != nil {
309+
cmdResults.SetXrayVersion(params.GetXrayVersion())
310+
cmdResults.SetXscVersion(params.GetXscVersion())
311+
cmdResults.SetMultiScanId(params.GetMultiScanId())
312+
cmdResults.SetStartTime(params.StartTime())
313+
// Send entitlement requests
314+
xrayManager, err := xrayutils.CreateXrayServiceManager(serverDetails)
315+
if err != nil {
310316
return cmdResults.AddGeneralError(err, false)
311317
}
312318
entitledForJas, err := isEntitledForJas(xrayManager, params)
@@ -316,9 +322,8 @@ func initAuditCmdResults(params *AuditParams) (cmdResults *results.SecurityComma
316322
cmdResults.SetEntitledForJas(entitledForJas)
317323
}
318324
if entitledForJas {
319-
cmdResults.SetSecretValidation(jas.CheckForSecretValidation(xrayManager, params.xrayVersion, slices.Contains(params.AuditBasicParams.ScansToPerform(), utils.SecretTokenValidationScan)))
325+
cmdResults.SetSecretValidation(jas.CheckForSecretValidation(xrayManager, params.GetXrayVersion(), slices.Contains(params.AuditBasicParams.ScansToPerform(), utils.SecretTokenValidationScan)))
320326
}
321-
cmdResults.SetMultiScanId(params.commonGraphScanParams.MultiScanId)
322327
// Initialize targets
323328
detectScanTargets(cmdResults, params)
324329
if params.IsRecursiveScan() && len(params.workingDirs) == 1 && len(cmdResults.Targets) == 0 {

0 commit comments

Comments
 (0)