Skip to content

Commit

Permalink
add sca-hide-dev-test-dependencies flag
Browse files Browse the repository at this point in the history
  • Loading branch information
BenAlvo1 committed Sep 25, 2024
1 parent 41edccc commit 0c49d67
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 54 deletions.
32 changes: 22 additions & 10 deletions internal/commands/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ const (
redundantLabel = "redundant"
delayValueForReport = 10
fixLinkPrefix = "https://devhub.checkmarx.com/cve-details/"
snoozeLabel = "Snooze"
muteLabel = "Muted"
ScaDevAndTestExclusionParam = "DEV_AND_TEST"
ScaExcludeResultTypesParam = "exclude-result-types"
)

var summaryFormats = []string{
Expand Down Expand Up @@ -272,6 +272,8 @@ func resultShowSubCommand(
resultShowCmd.PersistentFlags().Bool(commonParams.IgnorePolicyFlag, false, "Do not evaluate policies")
resultShowCmd.PersistentFlags().Bool(commonParams.SastRedundancyFlag, false,
"Populate SAST results 'data.redundancy' with values '"+fixLabel+"' (to fix) or '"+redundantLabel+"' (no need to fix)")
resultShowCmd.PersistentFlags().Bool(commonParams.ScaHideDevAndTestDepFlag, false, "Hide dev and test dependencies in SCA scan results")

return resultShowCmd
}

Expand Down Expand Up @@ -941,15 +943,22 @@ func runGetResultCommand(
formatSbomOptions, _ := cmd.Flags().GetString(commonParams.ReportSbomFormatFlag)
sastRedundancy, _ := cmd.Flags().GetBool(commonParams.SastRedundancyFlag)
agent, _ := cmd.Flags().GetString(commonParams.AgentFlag)
scaHideDevAndTestDep, _ := cmd.Flags().GetBool(commonParams.ScaHideDevAndTestDepFlag)

scanID, _ := cmd.Flags().GetString(commonParams.ScanIDFlag)
if scanID == "" {
return errors.Errorf("%s: Please provide a scan ID", failedListingResults)
}

params, err := getFilters(cmd)
if err != nil {
return errors.Wrapf(err, "%s", failedListingResults)
}

if scaHideDevAndTestDep {
params[ScaExcludeResultTypesParam] = ScaDevAndTestExclusionParam
}

scan, errorModel, scanErr := scanWrapper.GetByID(scanID)
if scanErr != nil {
return errors.Wrapf(scanErr, "%s", failedGetting)
Expand Down Expand Up @@ -1099,7 +1108,7 @@ func CreateScanReport(
targetFile,
targetPath string,
agent string,
params map[string]string,
resultsParam map[string]string,
featureFlagsWrapper wrappers.FeatureFlagsWrapper,
) error {
reportList := strings.Split(reportTypes, ",")
Expand All @@ -1118,7 +1127,7 @@ func CreateScanReport(
return err
}
if !scanPending {
results, err = ReadResults(resultsWrapper, exportWrapper, scan, params)
results, err = ReadResults(resultsWrapper, exportWrapper, scan, resultsParam)
if err != nil {
return err
}
Expand Down Expand Up @@ -1362,15 +1371,17 @@ func ReadResults(
resultsWrapper wrappers.ResultsWrapper,
exportWrapper wrappers.ExportWrapper,
scan *wrappers.ScanResponseModel,
params map[string]string,
resultsParams map[string]string,
) (results *wrappers.ScanResultsCollection, err error) {
var resultsModel *wrappers.ScanResultsCollection
var errorModel *wrappers.WebError

params[commonParams.ScanIDQueryParam] = scan.ID
_, sastRedundancy := params[commonParams.SastRedundancyFlag]
resultsParams[commonParams.ScanIDQueryParam] = scan.ID
_, sastRedundancy := resultsParams[commonParams.SastRedundancyFlag]

scaHideDevAndTestDep := resultsParams[ScaExcludeResultTypesParam] == ScaDevAndTestExclusionParam

resultsModel, errorModel, err = resultsWrapper.GetAllResultsByScanID(params)
resultsModel, errorModel, err = resultsWrapper.GetAllResultsByScanID(resultsParams)

if err != nil {
return nil, errors.Wrapf(err, "%s", failedListingResults)
Expand All @@ -1384,7 +1395,7 @@ func ReadResults(
// Compute SAST results redundancy
resultsModel = ComputeRedundantSastResults(resultsModel)
}
resultsModel, err = enrichScaResults(exportWrapper, scan, resultsModel)
resultsModel, err = enrichScaResults(exportWrapper, scan, resultsModel, scaHideDevAndTestDep)
if err != nil {
return nil, err
}
Expand All @@ -1399,9 +1410,10 @@ func enrichScaResults(
exportWrapper wrappers.ExportWrapper,
scan *wrappers.ScanResponseModel,
resultsModel *wrappers.ScanResultsCollection,
scaHideDevAndTestDep bool,
) (*wrappers.ScanResultsCollection, error) {
if slices.Contains(scan.Engines, commonParams.ScaType) {
scaExportDetails, err := services.GetExportPackage(exportWrapper, scan.ID)
scaExportDetails, err := services.GetExportPackage(exportWrapper, scan.ID, scaHideDevAndTestDep)
if err != nil {
return nil, errors.Wrapf(err, "%s", failedListingResults)
}
Expand Down
11 changes: 9 additions & 2 deletions internal/commands/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,7 @@ func scanCreateSubCommand(
createScanCmd.PersistentFlags().String(commonParams.SCSRepoTokenFlag, "", "Provide a token with read permission for the repo that you are scanning (for scorecard scans)")
createScanCmd.PersistentFlags().String(commonParams.SCSRepoURLFlag, "", "The URL of the repo that you are scanning with scs (for scorecard scans)")
createScanCmd.PersistentFlags().String(commonParams.SCSEnginesFlag, "", "Specify which scs engines will run (default: all licensed engines)")
createScanCmd.PersistentFlags().Bool(commonParams.ScaHideDevAndTestDepFlag, false, "Hide dev and test dependencies in SCA scan results")

return createScanCmd
}
Expand Down Expand Up @@ -1901,11 +1902,17 @@ func createReportsAfterScan(
formatPdfOptions, _ := cmd.Flags().GetString(commonParams.ReportFormatPdfOptionsFlag)
formatSbomOptions, _ := cmd.Flags().GetString(commonParams.ReportSbomFormatFlag)
agent, _ := cmd.Flags().GetString(commonParams.AgentFlag)
scaHideDevAndTestDep, _ := cmd.Flags().GetBool(commonParams.ScaHideDevAndTestDepFlag)

params, err := getFilters(cmd)
if err != nil {
return err
}

if scaHideDevAndTestDep {
params[ScaExcludeResultTypesParam] = ScaDevAndTestExclusionParam
}

if !strings.Contains(reportFormats, printer.FormatSummaryConsole) {
reportFormats += "," + printer.FormatSummaryConsole
}
Expand Down Expand Up @@ -2042,11 +2049,11 @@ func getSummaryThresholdMap(
resultsWrapper wrappers.ResultsWrapper,
exportWrapper wrappers.ExportWrapper,
scan *wrappers.ScanResponseModel,
params map[string]string,
resultsParams map[string]string,
risksOverviewWrapper wrappers.RisksOverviewWrapper,
) (map[string]int, error) {
summaryMap := make(map[string]int)
results, err := ReadResults(resultsWrapper, exportWrapper, scan, params)
results, err := ReadResults(resultsWrapper, exportWrapper, scan, resultsParams)

if err != nil {
return nil, err
Expand Down
80 changes: 40 additions & 40 deletions internal/params/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,47 +106,47 @@ const (
Threshold = "threshold"
ThresholdFlagUsage = "Local build threshold. Format <engine>-<severity>=<limit>. " +
"Example: scan --threshold \"sast-high=10;sca-high=5;iac-security-low=10\""
KeyValuePairSize = 2
WaitDelayDefault = 5
SimilarityIDFlag = "similarity-id"
SeverityFlag = "severity"
StateFlag = "state"
CommentFlag = "comment"
LanguageFlag = "language"
VulnerabilityTypeFlag = "vulnerability-type"
CweIDFlag = "cwe-id"
SCMTokenFlag = "token"
AzureTokenUsage = "Azure DevOps personal access token. Requires “Connected server” and “Code“ scope."
GithubTokenUsage = "GitHub OAuth token. Requires “Repo” scope and organization SSO authorization, if enforced by the organization"
GitLabTokenUsage = "GitLab OAuth token"
BotCount = "Note: dependabot is not counted but other bots might be considered as contributors."
DisabledReposCount = "Note: Disabled repositories are not counted."
URLFlag = "url"
GitLabURLFlag = "url-gitlab"
URLFlagUsage = "API base URL"
QueryIDFlag = "query-id"
SSHKeyFlag = "ssh-key"
RepoURLFlag = "repo-url"
AstToken = "ast-token"
SSHValue = "ssh-value"
KicsContainerNameKey = "kics-container-name"
KicsPlatformsFlag = "kics-platforms"
KicsPlatformsFlagUsage = "KICS Platform Flag. Use ',' as the delimiter for arrays."
IacsPlatformsFlag = "iac-security-platforms"
IacsPlatformsFlagUsage = "IaC Security Platform Flag"
ApikeyOverrideFlag = "apikey-override"
ExploitablePathFlag = "sca-exploitable-path"
LastSastScanTime = "sca-last-sast-scan-time"
ProjecPrivatePackageFlag = "project-private-package"
SastRedundancyFlag = "sast-redundancy"
ContainerImagesFlag = "container-images"
ContainersTypeFlag = "container-security"
VSCodeAgent = "VS Code"
EclipseAgent = "Eclipse"
VisualStudioAgent = "Visual Studio"
JetbrainsAgent = "Jetbrains"

KeyValuePairSize = 2
WaitDelayDefault = 5
SimilarityIDFlag = "similarity-id"
SeverityFlag = "severity"
StateFlag = "state"
CommentFlag = "comment"
LanguageFlag = "language"
VulnerabilityTypeFlag = "vulnerability-type"
CweIDFlag = "cwe-id"
SCMTokenFlag = "token"
AzureTokenUsage = "Azure DevOps personal access token. Requires “Connected server” and “Code“ scope."
GithubTokenUsage = "GitHub OAuth token. Requires “Repo” scope and organization SSO authorization, if enforced by the organization"
GitLabTokenUsage = "GitLab OAuth token"
BotCount = "Note: dependabot is not counted but other bots might be considered as contributors."
DisabledReposCount = "Note: Disabled repositories are not counted."
URLFlag = "url"
GitLabURLFlag = "url-gitlab"
URLFlagUsage = "API base URL"
QueryIDFlag = "query-id"
SSHKeyFlag = "ssh-key"
RepoURLFlag = "repo-url"
AstToken = "ast-token"
SSHValue = "ssh-value"
KicsContainerNameKey = "kics-container-name"
KicsPlatformsFlag = "kics-platforms"
KicsPlatformsFlagUsage = "KICS Platform Flag. Use ',' as the delimiter for arrays."
IacsPlatformsFlag = "iac-security-platforms"
IacsPlatformsFlagUsage = "IaC Security Platform Flag"
ApikeyOverrideFlag = "apikey-override"
ExploitablePathFlag = "sca-exploitable-path"
LastSastScanTime = "sca-last-sast-scan-time"
ProjecPrivatePackageFlag = "project-private-package"
SastRedundancyFlag = "sast-redundancy"
ContainerImagesFlag = "container-images"
ContainersTypeFlag = "container-security"
VSCodeAgent = "VS Code"
EclipseAgent = "Eclipse"
VisualStudioAgent = "Visual Studio"
JetbrainsAgent = "Jetbrains"
ScaPrivatePackageVersionFlag = "sca-private-package-version"
ScaHideDevAndTestDepFlag = "sca-hide-dev-test-dependencies"

// INDIVIDUAL FILTER FLAGS
SastFilterFlag = "sast-filter"
Expand Down
5 changes: 4 additions & 1 deletion internal/services/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,17 @@ const (
pollingTimeout = 5 // minutes
)

func GetExportPackage(exportWrapper wrappers.ExportWrapper, scanID string) (*wrappers.ScaPackageCollectionExport, error) {
func GetExportPackage(exportWrapper wrappers.ExportWrapper, scanID string, scaHideDevAndTestDep bool) (*wrappers.ScaPackageCollectionExport, error) {
var scaPackageCollection = &wrappers.ScaPackageCollectionExport{
Packages: []wrappers.ScaPackage{},
ScaTypes: []wrappers.ScaType{},
}
payload := &wrappers.ExportRequestPayload{
ScanID: scanID,
FileFormat: "ScanReportJson",
ExportParameters: wrappers.ExportParameters{
HideDevAndTestDependencies: scaHideDevAndTestDep,
},
}

exportID, err := exportWrapper.InitiateExportRequest(payload)
Expand Down
26 changes: 26 additions & 0 deletions test/integration/result_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,32 @@ func TestResultsShow_ScanIDWithSnoozedAndMutedAllVulnerabilities_NoVulnerabiliti
}
}

func TestResultsShow_WithScaHideDevAndTestDependencies_NoVulnerabilitiesInScan(t *testing.T) {
scanID := "28d29a61-bc5e-4f5a-9fdd-e18c5a10c05b"
reportFilePath := fmt.Sprintf("%s%s.%s", resultsDirectory, fileName, printer.FormatJSON)

_ = executeCmdNilAssertion(
t, "Results show generating JSON report with options should pass",
"results", "show",
flag(params.ScanIDFlag), scanID,
flag(params.TargetFormatFlag), printer.FormatJSON,
flag(params.TargetPathFlag), resultsDirectory,
flag(params.TargetFlag), fileName,
flag(params.ScaHideDevAndTestDepFlag),
)

defer func() {
_ = os.RemoveAll(resultsDirectory)
}()

assertFileExists(t, reportFilePath)

var result wrappers.ScanResultsCollection
readAndUnmarshalFile(t, reportFilePath, &result)

assert.Equal(t, len(result.Results), 0, "Should have no results")
}

func assertFileExists(t *testing.T, path string) {
_, err := os.Stat(path)
assert.NilError(t, err, "Report file should exist at path "+path)
Expand Down
2 changes: 1 addition & 1 deletion test/integration/scan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1945,7 +1945,7 @@ func TestCreateAsyncScan_CallExportServiceBeforeScanFinishWithRetry_Success(t *t
flag(params.ScanInfoFormatFlag), printer.FormatJSON,
}
scanID, _ := executeCreateScan(t, args)
exportRes, err := services.GetExportPackage(wrappers.NewExportHTTPWrapper("api/sca/export"), scanID)
exportRes, err := services.GetExportPackage(wrappers.NewExportHTTPWrapper("api/sca/export"), scanID, false)
asserts.Nil(t, err)
assert.Assert(t, exportRes != nil, "Export response should not be nil")
}

0 comments on commit 0c49d67

Please sign in to comment.