diff --git a/internal/commands/scan.go b/internal/commands/scan.go index be5dcbe80..0b1367fcd 100644 --- a/internal/commands/scan.go +++ b/internal/commands/scan.go @@ -93,6 +93,7 @@ const ( resultsMapValue = "value" resultsMapType = "type" trueString = "true" + configTwoms = "2ms" falseString = "false" maxPollingWaitTime = 60 engineNotAllowed = "It looks like the \"%s\" scan type does not exist or you are trying to run a scan without the \"%s\" package license." + @@ -779,7 +780,7 @@ func setupScanTypeProjectAndConfig( configArr = append(configArr, containersConfig) } - var SCSConfig, scsErr = addSCSScan(cmd) + var SCSConfig, scsErr = addSCSScan(cmd, resubmitConfig) if scsErr != nil { return scsErr } else if SCSConfig != nil { @@ -973,35 +974,57 @@ func addAPISecScan(cmd *cobra.Command) map[string]interface{} { } return nil } - -func addSCSScan(cmd *cobra.Command) (map[string]interface{}, error) { - if scanTypeEnabled(commonParams.ScsType) { +func createResubmitConfig(resubmitConfig []wrappers.Config, scsRepoToken, scsRepoURL string) wrappers.SCSConfig { + scsConfig := wrappers.SCSConfig{} + for _, config := range resubmitConfig { + resubmitTwoms := config.Value[configTwoms] + if resubmitTwoms != nil { + scsConfig.Twoms = resubmitTwoms.(string) + } + scsConfig.RepoURL = scsRepoURL + scsConfig.RepoToken = scsRepoToken + resubmitScoreCard := config.Value[ScsScoreCardType] + if resubmitScoreCard == trueString && scsRepoToken != "" && scsRepoURL != "" { + scsConfig.Scorecard = trueString + } else { + scsConfig.Scorecard = falseString + } + } + return scsConfig +} +func addSCSScan(cmd *cobra.Command, resubmitConfig []wrappers.Config) (map[string]interface{}, error) { + if scanTypeEnabled(commonParams.ScsType) || scanTypeEnabled(commonParams.MicroEnginesType) { + scsConfig := wrappers.SCSConfig{} SCSMapConfig := make(map[string]interface{}) - SCSConfig := wrappers.SCSConfig{} SCSMapConfig[resultsMapType] = commonParams.MicroEnginesType // scs is still microengines in the scans API userScanTypes, _ := cmd.Flags().GetString(commonParams.ScanTypes) - SCSRepoToken, _ := cmd.Flags().GetString(commonParams.SCSRepoTokenFlag) - SCSRepoURL, _ := cmd.Flags().GetString(commonParams.SCSRepoURLFlag) + scsRepoToken, _ := cmd.Flags().GetString(commonParams.SCSRepoTokenFlag) + scsRepoURL, _ := cmd.Flags().GetString(commonParams.SCSRepoURLFlag) SCSEngines, _ := cmd.Flags().GetString(commonParams.SCSEnginesFlag) + if resubmitConfig != nil { + scsConfig = createResubmitConfig(resubmitConfig, scsRepoToken, scsRepoURL) + SCSMapConfig[resultsMapValue] = &scsConfig + return SCSMapConfig, nil + } if SCSEngines != "" { SCSEnginesTypes := strings.Split(SCSEngines, ",") for _, engineType := range SCSEnginesTypes { engineType = strings.TrimSpace(engineType) switch engineType { case ScsSecretDetectionType: - SCSConfig.Twoms = trueString + scsConfig.Twoms = trueString case ScsScoreCardType: - SCSConfig.Scorecard = trueString + scsConfig.Scorecard = trueString } } } else { - SCSConfig.Scorecard = trueString - SCSConfig.Twoms = trueString + scsConfig.Scorecard = trueString + scsConfig.Twoms = trueString } - if SCSConfig.Scorecard == trueString { - if SCSRepoToken != "" && SCSRepoURL != "" { - SCSConfig.RepoToken = SCSRepoToken - SCSConfig.RepoURL = strings.ToLower(SCSRepoURL) + if scsConfig.Scorecard == trueString { + if scsRepoToken != "" && scsRepoURL != "" { + scsConfig.RepoToken = scsRepoToken + scsConfig.RepoURL = strings.ToLower(scsRepoURL) } else { if userScanTypes == "" { fmt.Println(ScsRepoRequiredMsg) @@ -1010,7 +1033,7 @@ func addSCSScan(cmd *cobra.Command) (map[string]interface{}, error) { return nil, errors.Errorf(ScsRepoRequiredMsg) } } - SCSMapConfig[resultsMapValue] = &SCSConfig + SCSMapConfig[resultsMapValue] = &scsConfig return SCSMapConfig, nil } return nil, nil diff --git a/internal/commands/scan_test.go b/internal/commands/scan_test.go index 5bf0ba199..2f9ae39d4 100644 --- a/internal/commands/scan_test.go +++ b/internal/commands/scan_test.go @@ -664,6 +664,89 @@ func TestAddScaScan(t *testing.T) { t.Errorf("Expected %+v, but got %+v", scaMapConfig, result) } } +func TestAddSCSScan_ResubmitWithOutScorecardFlags_ShouldPass(t *testing.T) { + cmdCommand := &cobra.Command{ + Use: "scan", + Short: "Scan a project", + } + cmdCommand.PersistentFlags().String(commonParams.ScanTypes, "", "Scan types") + cmdCommand.PersistentFlags().String(commonParams.SCSRepoTokenFlag, "", "SCS Repo Token") + cmdCommand.PersistentFlags().String(commonParams.SCSRepoURLFlag, "", "SCS Repo URL") + + _ = cmdCommand.Execute() + + _ = cmdCommand.Flags().Set(commonParams.ScanTypes, commonParams.ScsType) + _ = cmdCommand.Flags().Set(commonParams.SCSRepoURLFlag, "") + _ = cmdCommand.Flags().Set(commonParams.SCSRepoTokenFlag, "") + + resubmitConfig := []wrappers.Config{ + { + Type: commonParams.ScsType, + Value: map[string]interface{}{ + configTwoms: trueString, + ScsScoreCardType: falseString, + }, + }, + } + + result, _ := addSCSScan(cmdCommand, resubmitConfig) + + expectedConfig := wrappers.SCSConfig{ + Twoms: trueString, + Scorecard: falseString, + } + + expectedMapConfig := make(map[string]interface{}) + expectedMapConfig[resultsMapType] = commonParams.MicroEnginesType + expectedMapConfig[resultsMapValue] = &expectedConfig + + if !reflect.DeepEqual(result, expectedMapConfig) { + t.Errorf("Expected %+v, but got %+v", expectedMapConfig, result) + } +} + +func TestAddSCSScan_ResubmitWithScorecardFlags_ShouldPass(t *testing.T) { + cmdCommand := &cobra.Command{ + Use: "scan", + Short: "Scan a project", + } + cmdCommand.PersistentFlags().String(commonParams.ScanTypes, "", "Scan types") + cmdCommand.PersistentFlags().String(commonParams.SCSRepoTokenFlag, "", "SCS Repo Token") + cmdCommand.PersistentFlags().String(commonParams.SCSRepoURLFlag, "", "SCS Repo URL") + + _ = cmdCommand.Execute() + + _ = cmdCommand.Flags().Set(commonParams.ScanTypes, commonParams.ScsType) + _ = cmdCommand.Flags().Set(commonParams.SCSRepoURLFlag, dummyRepo) + _ = cmdCommand.Flags().Set(commonParams.SCSRepoTokenFlag, dummyToken) + + resubmitConfig := []wrappers.Config{ + { + Type: commonParams.ScsType, + Value: map[string]interface{}{ + configTwoms: trueString, + ScsScoreCardType: trueString, + }, + }, + } + + result, _ := addSCSScan(cmdCommand, resubmitConfig) + + expectedConfig := wrappers.SCSConfig{ + Twoms: "true", + Scorecard: trueString, + RepoToken: dummyToken, + RepoURL: dummyRepo, + } + + expectedMapConfig := make(map[string]interface{}) + expectedMapConfig[resultsMapType] = commonParams.MicroEnginesType + expectedMapConfig[resultsMapValue] = &expectedConfig + + if !reflect.DeepEqual(result, expectedMapConfig) { + t.Errorf("Expected %+v, but got %+v", expectedMapConfig, result) + } +} func TestAddSastScan_WithFastScanFlag_ShouldPass(t *testing.T) { var resubmitConfig []wrappers.Config @@ -809,6 +892,7 @@ func TestCreateScan_WithSCSScorecard_ShouldFail(t *testing.T) { } func TestCreateScan_WithSCSSecretDetectionAndScorecard_scsMapHasBoth(t *testing.T) { + var resubmitConfig []wrappers.Config cmdCommand := &cobra.Command{ Use: "scan", Short: "Scan a project", @@ -822,7 +906,7 @@ func TestCreateScan_WithSCSSecretDetectionAndScorecard_scsMapHasBoth(t *testing. _ = cmdCommand.Flags().Set(commonParams.SCSRepoTokenFlag, dummyToken) _ = cmdCommand.Flags().Set(commonParams.SCSRepoURLFlag, dummyRepo) - result, _ := addSCSScan(cmdCommand) + result, _ := addSCSScan(cmdCommand, resubmitConfig) scsConfig := wrappers.SCSConfig{ Twoms: "true", @@ -840,6 +924,7 @@ func TestCreateScan_WithSCSSecretDetectionAndScorecard_scsMapHasBoth(t *testing. } func TestCreateScan_WithSCSSecretDetection_scsMapHasSecretDetection(t *testing.T) { + var resubmitConfig []wrappers.Config cmdCommand := &cobra.Command{ Use: "scan", Short: "Scan a project", @@ -849,7 +934,7 @@ func TestCreateScan_WithSCSSecretDetection_scsMapHasSecretDetection(t *testing.T _ = cmdCommand.Execute() _ = cmdCommand.Flags().Set(commonParams.SCSEnginesFlag, "secret-detection") - result, _ := addSCSScan(cmdCommand) + result, _ := addSCSScan(cmdCommand, resubmitConfig) scsConfig := wrappers.SCSConfig{ Twoms: "true",