Skip to content

Commit

Permalink
Fix fast scan and incremental scan initialization in sast configurati…
Browse files Browse the repository at this point in the history
…on base on user input
  • Loading branch information
BenAlvo1 committed Jan 16, 2025
1 parent e25172e commit db8a612
Show file tree
Hide file tree
Showing 3 changed files with 174 additions and 36 deletions.
92 changes: 57 additions & 35 deletions internal/commands/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ const (
configFilterKey = "filter"
configFilterPlatforms = "platforms"
configIncremental = "incremental"
configFastScan = "fastScanMode"
configPresetName = "presetName"
configEngineVerbose = "engineVerbose"
configLanguageMode = "languageMode"
Expand Down Expand Up @@ -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{} {
Expand Down
116 changes: 116 additions & 0 deletions internal/commands/scan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})
}
}
2 changes: 1 addition & 1 deletion internal/wrappers/export-http.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit db8a612

Please sign in to comment.