Skip to content

Commit 673a128

Browse files
authored
Add options for binary commands to pass output dir (jfrog#656)
1 parent 5fe56e3 commit 673a128

File tree

4 files changed

+26
-9
lines changed

4 files changed

+26
-9
lines changed

cli/docs/flags.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ var commandFlags = map[string][]string{
176176
XrScan: {
177177
Url, XrayUrl, user, password, accessToken, ServerId, SpecFlag, Threads, scanRecursive, scanRegexp, scanAnt,
178178
scanProjectKey, Watches, RepoPath, Licenses, Sbom, OutputFormat, Fail, ExtendedTable, BypassArchiveLimits, MinSeverity, FixableOnly, ScanVuln, InsecureTls,
179-
binarySca, binarySecrets, binaryWithoutCA, SecretValidation,
179+
binarySca, binarySecrets, binaryWithoutCA, SecretValidation, OutputDir,
180180
},
181181
Enrich: {
182182
Url, XrayUrl, user, password, accessToken, ServerId, Threads, InsecureTls,
@@ -189,7 +189,7 @@ var commandFlags = map[string][]string{
189189
},
190190
DockerScan: {
191191
Url, XrayUrl, user, password, accessToken, ServerId, scanProjectKey, Watches, RepoPath, Licenses, Sbom, OutputFormat, Fail, ExtendedTable, BypassArchiveLimits, MinSeverity, FixableOnly, ScanVuln, InsecureTls,
192-
binarySca, binarySecrets, binaryWithoutCA, SecretValidation,
192+
binarySca, binarySecrets, binaryWithoutCA, SecretValidation, OutputDir,
193193
},
194194
Audit: {
195195
Url, XrayUrl, user, password, accessToken, ServerId, InsecureTls, scanProjectKey, Watches, RepoPath, Sbom, Licenses, OutputFormat, ExcludeTestDeps,

cli/scancommands.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,7 @@ func ScanCmd(c *components.Context) error {
332332
SetThreads(threads).
333333
SetSpec(specFile).
334334
SetOutputFormat(format).
335+
SetOutputDir(c.GetStringFlagValue(flags.OutputDir)).
335336
SetProject(getProject(c)).
336337
SetBaseRepoPath(repoPath).
337338
SetIncludeVulnerabilities(c.GetBoolFlagValue(flags.Vuln) || shouldIncludeVulnerabilities(c)).
@@ -778,6 +779,7 @@ func DockerScan(c *components.Context, image string) error {
778779
SetXrayVersion(xrayVersion).
779780
SetXscVersion(xscVersion).
780781
SetOutputFormat(format).
782+
SetOutputDir(c.GetStringFlagValue(flags.OutputDir)).
781783
SetProject(getProject(c)).
782784
SetBaseRepoPath(addTrailingSlashToRepoPathIfNeeded(c)).
783785
SetIncludeVulnerabilities(c.GetBoolFlagValue(flags.Vuln) || shouldIncludeVulnerabilities(c)).

commands/scan/scan.go

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ type ScanCommand struct {
6363
threads int
6464
// The location of the downloaded Xray indexer binary on the local file system.
6565
outputFormat format.OutputFormat
66+
outputDir string
6667
minSeverityFilter severityutils.Severity
6768
fail bool
6869
printExtendedTable bool
@@ -124,6 +125,11 @@ func (scanCmd *ScanCommand) SetThreads(threads int) *ScanCommand {
124125
return scanCmd
125126
}
126127

128+
func (scanCmd *ScanCommand) SetOutputDir(outputDir string) *ScanCommand {
129+
scanCmd.outputDir = outputDir
130+
return scanCmd
131+
}
132+
127133
func (scanCmd *ScanCommand) SetOutputFormat(format format.OutputFormat) *ScanCommand {
128134
scanCmd.outputFormat = format
129135
return scanCmd
@@ -248,6 +254,7 @@ func (scanCmd *ScanCommand) RunAndRecordResults(cmdType utils.CommandType, recor
248254

249255
if err = output.NewResultsWriter(cmdResults).
250256
SetOutputFormat(scanCmd.outputFormat).
257+
SetOutputDir(scanCmd.outputDir).
251258
SetPlatformUrl(scanCmd.serverDetails.Url).
252259
SetPrintExtendedTable(scanCmd.printExtendedTable).
253260
SetSubScansPerformed(scanCmd.scansToPerform).
@@ -517,6 +524,13 @@ func (scanCmd *ScanCommand) RunBinaryScaScan(fileTarget string, cmdResults *resu
517524
}
518525
targetResults.ScaScanResults(scan.GetScaScansStatusCode(err, *graphScanResults), *graphScanResults)
519526
targetResults.Technology = techutils.ToTechnology(graphScanResults.ScannedPackageType)
527+
// Dump scan response if requested
528+
if scanCmd.outputDir == "" {
529+
return
530+
}
531+
if e := scan.DumpScanResponseToFileIfNeeded(*graphScanResults, scanCmd.outputDir, utils.ScaScan, scanThreadId); e != nil {
532+
log.Warn(fmt.Sprintf(clientutils.GetLogMsgPrefix(scanThreadId, false)+"Failed to dump SCA scan results for target %s: %s", targetResults.Target, e.Error()))
533+
}
520534
return
521535
}
522536

@@ -574,11 +588,12 @@ func (scanCmd *ScanCommand) RunBinaryJasScans(cmdType utils.CommandType, msi str
574588
}
575589
log.Debug(fmt.Sprintf("Using analyzer manager executable at: %s", scanner.AnalyzerManager.AnalyzerManagerFullPath))
576590
jasParams := runner.JasRunnerParams{
577-
Runner: jasFileProducerConsumer,
578-
ServerDetails: scanCmd.serverDetails,
579-
Scanner: scanner,
580-
Module: module,
581-
ScansToPerform: scanCmd.scansToPerform,
591+
Runner: jasFileProducerConsumer,
592+
ServerDetails: scanCmd.serverDetails,
593+
Scanner: scanner,
594+
Module: module,
595+
TargetOutputDir: scanCmd.outputDir,
596+
ScansToPerform: scanCmd.scansToPerform,
582597
CvesProvider: func() (directCves []string, indirectCves []string) {
583598
if graphScanResults == nil {
584599
// No SCA scan results, return empty CVE lists.

sca/scan/scascan.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ func scaScanTask(strategy SbomScanStrategy, params ScaScanParams) (err error) {
160160
return err
161161
}
162162
log.Info(utils.GetScanFindingsLog(utils.ScaScan, len(scanResults.Vulnerabilities), startTime, params.ThreadId))
163-
return dumpScanResponseToFileIfNeeded(scanResults, params.ResultsOutputDir, utils.ScaScan, params.ThreadId)
163+
return DumpScanResponseToFileIfNeeded(scanResults, params.ResultsOutputDir, utils.ScaScan, params.ThreadId)
164164
}
165165
// New flow: we scan the SBOM and enrich it with CVE vulnerabilities and calculate violations.
166166
bomWithVulnerabilities, err := strategy.SbomEnrichTask(params.ScanResults.ScaResults.Sbom)
@@ -192,7 +192,7 @@ func GetScaScansStatusCode(err error, results ...services.ScanResponse) int {
192192

193193
// If an output dir was provided through --output-dir flag, we create in the provided path new file containing the scan results
194194
// TODO: remove this function once the new flow is fully implemented.
195-
func dumpScanResponseToFileIfNeeded(results services.ScanResponse, scanResultsOutputDir string, scanType utils.SubScanType, threadId int) (err error) {
195+
func DumpScanResponseToFileIfNeeded(results services.ScanResponse, scanResultsOutputDir string, scanType utils.SubScanType, threadId int) (err error) {
196196
if scanResultsOutputDir == "" {
197197
return
198198
}

0 commit comments

Comments
 (0)