Skip to content

Commit 30c2fe0

Browse files
authored
Selective scans for Scan and DockerScan commands (#612)
1 parent ae5f468 commit 30c2fe0

File tree

11 files changed

+275
-117
lines changed

11 files changed

+275
-117
lines changed

cli/docs/flags.go

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,15 @@ const (
5757
Secrets = "secrets"
5858
WithoutCA = "without-contextual-analysis"
5959

60+
auditSca = auditPrefix + Sca
61+
auditIac = auditPrefix + Iac
62+
auditSast = auditPrefix + Sast
63+
auditSecrets = auditPrefix + Secrets
64+
auditWithoutCA = auditPrefix + WithoutCA
65+
binarySca = scanPrefix + Sca
66+
binarySecrets = scanPrefix + Secrets
67+
binaryWithoutCA = scanPrefix + WithoutCA
68+
6069
// Sast related flags
6170
AddSastRules = "add-sast-rules"
6271
)
@@ -161,6 +170,7 @@ var commandFlags = map[string][]string{
161170
XrScan: {
162171
Url, user, password, accessToken, ServerId, SpecFlag, Threads, scanRecursive, scanRegexp, scanAnt,
163172
scanProjectKey, Watches, RepoPath, Licenses, Sbom, OutputFormat, Fail, ExtendedTable, BypassArchiveLimits, MinSeverity, FixableOnly, ScanVuln, InsecureTls,
173+
binarySca, binarySecrets, binaryWithoutCA, SecretValidation,
164174
},
165175
Enrich: {
166176
Url, user, password, accessToken, ServerId, Threads, InsecureTls,
@@ -169,13 +179,14 @@ var commandFlags = map[string][]string{
169179
Url, user, password, accessToken, ServerId, scanProjectKey, BuildVuln, OutputFormat, Fail, ExtendedTable, Rescan, InsecureTls, TriggerScanRetries,
170180
},
171181
DockerScan: {
172-
Url, XrayUrl, user, password, accessToken, ServerId, scanProjectKey, Watches, RepoPath, Licenses, Sbom, OutputFormat, Fail, ExtendedTable, BypassArchiveLimits, MinSeverity, FixableOnly, ScanVuln, SecretValidation, InsecureTls,
182+
Url, XrayUrl, user, password, accessToken, ServerId, scanProjectKey, Watches, RepoPath, Licenses, Sbom, OutputFormat, Fail, ExtendedTable, BypassArchiveLimits, MinSeverity, FixableOnly, ScanVuln, InsecureTls,
183+
binarySca, binarySecrets, binaryWithoutCA, SecretValidation,
173184
},
174185
Audit: {
175186
Url, XrayUrl, user, password, accessToken, ServerId, InsecureTls, scanProjectKey, Watches, RepoPath, Sbom, Licenses, OutputFormat, ExcludeTestDeps,
176187
useWrapperAudit, DepType, RequirementsFile, Fail, ExtendedTable, WorkingDirs, ExclusionsAudit, Mvn, Gradle, Npm,
177188
Pnpm, Yarn, Go, Swift, Cocoapods, Nuget, Pip, Pipenv, Poetry, MinSeverity, FixableOnly, ThirdPartyContextualAnalysis, Threads,
178-
Sca, Iac, Sast, Secrets, WithoutCA, ScanVuln, SecretValidation, OutputDir, SkipAutoInstall, AllowPartialResults, MaxTreeDepth,
189+
auditSca, auditIac, auditSast, auditSecrets, auditWithoutCA, SecretValidation, ScanVuln, OutputDir, SkipAutoInstall, AllowPartialResults, MaxTreeDepth,
179190
StaticSca, XrayLibPluginBinaryCustomPath, AnalyzerManagerCustomPath, UploadRtRepoPath,
180191
},
181192
UploadCdx: {
@@ -188,7 +199,7 @@ var commandFlags = map[string][]string{
188199
scanProjectKey, Watches, ScanVuln, Fail,
189200
// Scan params
190201
Threads, ExclusionsAudit,
191-
Sca, Iac, Sast, Secrets, WithoutCA, SecretValidation, Sbom,
202+
auditSca, auditIac, auditSast, auditSecrets, auditWithoutCA, SecretValidation, Sbom,
192203
// Output params
193204
Licenses, OutputFormat, ExtendedTable, OutputDir, UploadRtRepoPath,
194205
// Scan Logic params
@@ -307,11 +318,14 @@ var flagsMap = map[string]components.Flag{
307318
StaticSca: components.NewBoolFlag(StaticSca, "Set to true to use the new SCA engine which is based on lock files.", components.SetHiddenBoolFlag()),
308319
CurationOutput: components.NewStringFlag(OutputFormat, "Defines the output format of the command. Acceptable values are: table, json.", components.WithStrDefaultValue("table")),
309320
SolutionPath: components.NewStringFlag(SolutionPath, "Path to the .NET solution file (.sln) to use when multiple solution files are present in the directory."),
310-
Sca: components.NewBoolFlag(Sca, fmt.Sprintf("Selective scanners mode: Execute SCA (Software Composition Analysis) sub-scan. Use --%s to run both SCA and Contextual Analysis. Use --%s --%s to to run SCA. Can be combined with --%s, --%s, --%s.", Sca, Sca, WithoutCA, Secrets, Sast, Iac)),
311-
Iac: components.NewBoolFlag(Iac, fmt.Sprintf("Selective scanners mode: Execute IaC sub-scan. Can be combined with --%s, --%s and --%s.", Sca, Secrets, Sast)),
312-
Sast: components.NewBoolFlag(Sast, fmt.Sprintf("Selective scanners mode: Execute SAST sub-scan. Can be combined with --%s, --%s and --%s.", Sca, Secrets, Iac)),
313-
Secrets: components.NewBoolFlag(Secrets, fmt.Sprintf("Selective scanners mode: Execute Secrets sub-scan. Can be combined with --%s, --%s and --%s.", Sca, Sast, Iac)),
314-
WithoutCA: components.NewBoolFlag(WithoutCA, fmt.Sprintf("Selective scanners mode: Disable Contextual Analysis scanner after SCA. Relevant only with --%s flag.", Sca)),
321+
binarySca: components.NewBoolFlag(Sca, fmt.Sprintf("Selective scanners mode: Execute SCA (Software Composition Analysis) sub-scan. Use --%s to run both SCA and Contextual Analysis. Use --%s --%s to to run SCA. Can be combined with --%s.", Sca, Sca, WithoutCA, Secrets)),
322+
binarySecrets: components.NewBoolFlag(Secrets, fmt.Sprintf("Selective scanners mode: Execute Secrets sub-scan. Can be combined with --%s.", Sca)),
323+
binaryWithoutCA: components.NewBoolFlag(WithoutCA, fmt.Sprintf("Selective scanners mode: Disable Contextual Analysis scanner after SCA. Relevant only with --%s flag.", Sca)),
324+
auditSca: components.NewBoolFlag(Sca, fmt.Sprintf("Selective scanners mode: Execute SCA (Software Composition Analysis) sub-scan. Use --%s to run both SCA and Contextual Analysis. Use --%s --%s to to run SCA. Can be combined with --%s, --%s, --%s.", Sca, Sca, WithoutCA, Secrets, Sast, Iac)),
325+
auditIac: components.NewBoolFlag(Iac, fmt.Sprintf("Selective scanners mode: Execute IaC sub-scan. Can be combined with --%s, --%s and --%s.", Sca, Secrets, Sast)),
326+
auditSast: components.NewBoolFlag(Sast, fmt.Sprintf("Selective scanners mode: Execute SAST sub-scan. Can be combined with --%s, --%s and --%s.", Sca, Secrets, Iac)),
327+
auditSecrets: components.NewBoolFlag(Secrets, fmt.Sprintf("Selective scanners mode: Execute Secrets sub-scan. Can be combined with --%s, --%s and --%s.", Sca, Sast, Iac)),
328+
auditWithoutCA: components.NewBoolFlag(WithoutCA, fmt.Sprintf("Selective scanners mode: Disable Contextual Analysis scanner after SCA. Relevant only with --%s flag.", Sca)),
315329
SecretValidation: components.NewBoolFlag(SecretValidation, fmt.Sprintf("Selective scanners mode: Triggers token validation on found secrets. Relevant only with --%s flag.", Secrets)),
316330

317331
AddSastRules: components.NewStringFlag(AddSastRules, "Incorporate any additional SAST rules (in JSON format, with absolute path) into this local scan."),

cli/scancommands.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,12 @@ func ScanCmd(c *components.Context) error {
295295
if c.IsFlagSet(flags.Watches) {
296296
scanCmd.SetWatches(splitByCommaAndTrim(c.GetStringFlagValue(flags.Watches)))
297297
}
298+
// Check sub-scans to perform
299+
if subScans, err := getSubScansToPreform(c); err != nil {
300+
return err
301+
} else if len(subScans) > 0 {
302+
scanCmd.SetScansToPerform(subScans)
303+
}
298304
return commandsCommon.Exec(scanCmd)
299305
}
300306

@@ -705,6 +711,12 @@ func DockerScan(c *components.Context, image string) error {
705711
return err
706712
}
707713
containerScanCommand := scan.NewDockerScanCommand()
714+
// Check sub-scans to perform
715+
if subScans, err := getSubScansToPreform(c); err != nil {
716+
return err
717+
} else if len(subScans) > 0 {
718+
containerScanCommand.SetScansToPerform(subScans)
719+
}
708720
containerScanCommand.
709721
SetImageTag(image).
710722
SetBomGenerator(indexer.NewIndexerBomGenerator()).

commands/audit/audit.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -315,9 +315,11 @@ func prepareToScan(params *AuditParams) (cmdResults *results.SecurityCommandResu
315315
if err != nil {
316316
return cmdResults.AddGeneralError(fmt.Errorf("failed to get scan logic options: %s", err.Error()), params.AllowPartialResults())
317317
}
318-
// Initialize the BOM generator
319-
if err = params.bomGenerator.WithOptions(bomGenOptions...).PrepareGenerator(); err != nil {
320-
return cmdResults.AddGeneralError(fmt.Errorf("failed to prepare the BOM generator: %s", err.Error()), params.AllowPartialResults())
318+
// Initialize the BOM generator if needed
319+
if params.resultsContext.IncludeSbom || utils.IsScanRequested(cmdResults.CmdType, utils.ScaScan, params.scansToPerform...) {
320+
if err = params.bomGenerator.WithOptions(bomGenOptions...).PrepareGenerator(); err != nil {
321+
return cmdResults.AddGeneralError(fmt.Errorf("failed to prepare the BOM generator: %s", err.Error()), params.AllowPartialResults())
322+
}
321323
}
322324
populateScanTargets(cmdResults, params)
323325
// Initialize the SCA scan strategy

0 commit comments

Comments
 (0)