diff --git a/internal/commands/scan.go b/internal/commands/scan.go index 108c1dc00..2274bc2c5 100644 --- a/internal/commands/scan.go +++ b/internal/commands/scan.go @@ -86,6 +86,7 @@ const ( configFilterKey = "filter" configFilterPlatforms = "platforms" configIncremental = "incremental" + configFastScan = "fastScanMode" configPresetName = "presetName" configEngineVerbose = "engineVerbose" configLanguageMode = "languageMode" @@ -818,45 +819,66 @@ func getResubmitConfiguration(scansWrapper wrappers.ScansWrapper, projectID, use } func addSastScan(cmd *cobra.Command, resubmitConfig []wrappers.Config) map[string]interface{} { - if scanTypeEnabled(commonParams.SastType) { - sastMapConfig := make(map[string]interface{}) - sastConfig := wrappers.SastConfig{} - sastMapConfig[resultsMapType] = commonParams.SastType - incrementalVal, _ := cmd.Flags().GetBool(commonParams.IncrementalSast) + // Check if SAST is enabled + if !scanTypeEnabled(commonParams.SastType) { + return nil + } + + sastMapConfig := make(map[string]interface{}) + sastConfig := wrappers.SastConfig{} + sastMapConfig[resultsMapType] = commonParams.SastType + + sastFastScanChanged := cmd.Flags().Changed(commonParams.SastFastScanFlag) + sastIncrementalChanged := cmd.Flags().Changed(commonParams.IncrementalSast) + + if sastFastScanChanged { fastScan, _ := cmd.Flags().GetBool(commonParams.SastFastScanFlag) - sastConfig.Incremental = strconv.FormatBool(incrementalVal) sastConfig.FastScanMode = strconv.FormatBool(fastScan) - sastConfig.PresetName, _ = cmd.Flags().GetString(commonParams.PresetName) - sastConfig.Filter, _ = cmd.Flags().GetString(commonParams.SastFilterFlag) - for _, config := range resubmitConfig { - if config.Type != commonParams.SastType { - continue - } - resubmitIncremental := config.Value[configIncremental] - if resubmitIncremental != nil && !incrementalVal { - sastConfig.Incremental = resubmitIncremental.(string) - } - resubmitPreset := config.Value[configPresetName] - if resubmitPreset != nil && sastConfig.PresetName == "" { - sastConfig.PresetName = resubmitPreset.(string) - } - resubmitFilter := config.Value[configFilterKey] - if resubmitFilter != nil && sastConfig.Filter == "" { - sastConfig.Filter = resubmitFilter.(string) - } - resubmitEngineVerbose := config.Value[configEngineVerbose] - if resubmitEngineVerbose != nil { - sastConfig.EngineVerbose = resubmitEngineVerbose.(string) - } - resubmitLanguageMode := config.Value[configLanguageMode] - if resubmitLanguageMode != nil { - sastConfig.LanguageMode = resubmitLanguageMode.(string) - } + } + + if sastIncrementalChanged { + incrementalVal, _ := cmd.Flags().GetBool(commonParams.IncrementalSast) + sastConfig.Incremental = strconv.FormatBool(incrementalVal) + } + + sastConfig.PresetName, _ = cmd.Flags().GetString(commonParams.PresetName) + sastConfig.Filter, _ = cmd.Flags().GetString(commonParams.SastFilterFlag) + + for _, config := range resubmitConfig { + if config.Type != commonParams.SastType { + continue } - sastMapConfig[resultsMapValue] = &sastConfig - return sastMapConfig + + overrideSastConfigValue(&sastConfig, config) + } + + sastMapConfig[resultsMapValue] = &sastConfig + return sastMapConfig +} + +func overrideSastConfigValue(sastConfig *wrappers.SastConfig, config wrappers.Config) { + setIfEmpty := func(configValue *string, resubmitValue interface{}) { + if *configValue == "" && resubmitValue != nil { + *configValue = resubmitValue.(string) + } + } + + if resubmitIncremental := config.Value[configIncremental]; resubmitIncremental != nil { + sastConfig.Incremental = resubmitIncremental.(string) + } + if resubmitFastScan := config.Value[configFastScan]; resubmitFastScan != nil { + sastConfig.FastScanMode = resubmitFastScan.(string) + } + + setIfEmpty(&sastConfig.PresetName, config.Value[configPresetName]) + setIfEmpty(&sastConfig.Filter, config.Value[configFilterKey]) + + if resubmitEngineVerbose := config.Value[configEngineVerbose]; resubmitEngineVerbose != nil { + sastConfig.EngineVerbose = resubmitEngineVerbose.(string) + } + if resubmitLanguageMode := config.Value[configLanguageMode]; resubmitLanguageMode != nil { + sastConfig.LanguageMode = resubmitLanguageMode.(string) } - return nil } func addKicsScan(cmd *cobra.Command, resubmitConfig []wrappers.Config) map[string]interface{} { diff --git a/internal/commands/scan_test.go b/internal/commands/scan_test.go index 6cef136f0..7495ebccb 100644 --- a/internal/commands/scan_test.go +++ b/internal/commands/scan_test.go @@ -1765,3 +1765,119 @@ func TestUploadZip_whenUserNotProvideZip_shouldReturnZipFilePathInFailureCase(t assert.Assert(t, strings.Contains(err.Error(), "error from UploadFile"), err.Error()) assert.Equal(t, zipPath, "failureCase.zip") } + +func TestAddSastScan_ScanFlags(t *testing.T) { + var resubmitConfig []wrappers.Config + + tests := []struct { + name string + requiredIncrementalSet bool + requiredFastScanSet bool + fastScanFlag string + incrementalFlag string + expectedConfig wrappers.SastConfig + }{ + { + name: "Fast scan and Incremental scan both false", + requiredIncrementalSet: true, + requiredFastScanSet: true, + fastScanFlag: "false", + incrementalFlag: "false", + expectedConfig: wrappers.SastConfig{ + FastScanMode: "false", + Incremental: "false", + }, + }, + { + name: "Fast scan and Incremental scan both true", + requiredIncrementalSet: true, + requiredFastScanSet: true, + fastScanFlag: "true", + incrementalFlag: "true", + expectedConfig: wrappers.SastConfig{ + FastScanMode: "true", + Incremental: "true", + }, + }, + { + name: "Fast scan and Incremental not set", + requiredIncrementalSet: false, + requiredFastScanSet: false, + expectedConfig: wrappers.SastConfig{}, + }, + { + name: "Fast scan is true and Incremental is false", + requiredIncrementalSet: true, + requiredFastScanSet: true, + fastScanFlag: "true", + incrementalFlag: "false", + expectedConfig: wrappers.SastConfig{ + FastScanMode: "true", + Incremental: "false", + }, + }, + { + name: "Fast scan is false and Incremental is true", + requiredIncrementalSet: true, + requiredFastScanSet: true, + fastScanFlag: "false", + incrementalFlag: "true", + expectedConfig: wrappers.SastConfig{ + FastScanMode: "false", + Incremental: "true", + }, + }, + { + name: "Fast scan is not set and Incremental is true", + requiredIncrementalSet: true, + incrementalFlag: "true", + expectedConfig: wrappers.SastConfig{ + Incremental: "true", + }, + }, + { + name: "Fast scan is true and Incremental is not set", + requiredFastScanSet: true, + fastScanFlag: "true", + expectedConfig: wrappers.SastConfig{ + FastScanMode: "true", + }, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + cmdCommand := &cobra.Command{ + Use: "scan", + Short: "Scan a project", + Long: `Scan a project`, + } + cmdCommand.PersistentFlags().Bool(commonParams.SastFastScanFlag, false, "Fast scan flag") + cmdCommand.PersistentFlags().Bool(commonParams.IncrementalSast, false, "Incremental scan flag") + + _ = cmdCommand.Execute() + + if tt.requiredFastScanSet { + _ = cmdCommand.Flags().Set(commonParams.SastFastScanFlag, tt.fastScanFlag) + } + if tt.requiredIncrementalSet { + _ = cmdCommand.Flags().Set(commonParams.IncrementalSast, tt.incrementalFlag) + } + + result := addSastScan(cmdCommand, resubmitConfig) + + actualSastConfig := wrappers.SastConfig{} + for key, value := range result { + if key == resultsMapType { + assert.Equal(t, commonParams.SastType, value) + } else if key == resultsMapValue { + actualSastConfig = *value.(*wrappers.SastConfig) + } + } + + if !reflect.DeepEqual(actualSastConfig, tt.expectedConfig) { + t.Errorf("Expected %+v, but got %+v", tt.expectedConfig, actualSastConfig) + } + }) + } +} diff --git a/internal/wrappers/export-http.go b/internal/wrappers/export-http.go index b4ca291b2..2222db158 100644 --- a/internal/wrappers/export-http.go +++ b/internal/wrappers/export-http.go @@ -134,7 +134,7 @@ func (e *ExportHTTPWrapper) GetExportReportStatus(reportID string) (*ExportPolli return &model, nil case http.StatusNotFound: _ = resp.Body.Close() - time.Sleep(time.Second) + time.Sleep(retryInterval) default: _ = resp.Body.Close() return nil, errors.Errorf("response status code %d", resp.StatusCode)