Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support scs engine In scan create Resubmit command (AST-63906) #840

Merged
merged 13 commits into from
Aug 26, 2024
25 changes: 22 additions & 3 deletions internal/commands/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
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." +
Expand Down Expand Up @@ -779,7 +780,7 @@
configArr = append(configArr, containersConfig)
}

var SCSConfig, scsErr = addSCSScan(cmd)
var SCSConfig, scsErr = addSCSScan(cmd, resubmitConfig)
if scsErr != nil {
return scsErr
} else if SCSConfig != nil {
Expand Down Expand Up @@ -974,15 +975,33 @@
return nil
}

func addSCSScan(cmd *cobra.Command) (map[string]interface{}, error) {
if scanTypeEnabled(commonParams.ScsType) {
func addSCSScan(cmd *cobra.Command, resubmitConfig []wrappers.Config) (map[string]interface{}, error) {

Check failure on line 978 in internal/commands/scan.go

View workflow job for this annotation

GitHub Actions / lint

cyclomatic complexity 17 of func `addSCSScan` is high (> 15) (gocyclo)
if scanTypeEnabled(commonParams.ScsType) || scanTypeEnabled(commonParams.MicroEnginesType) {
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)
SCSEngines, _ := cmd.Flags().GetString(commonParams.SCSEnginesFlag)
if resubmitConfig != nil {
for _, config := range resubmitConfig {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extract this scope to a new method - createSCSResubmitConfig(...).
This will make the function more readable and will answer the Single Responsibility Principle.

Also, it will probably fix your lint error

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
}
}
SCSMapConfig[resultsMapValue] = &SCSConfig
return SCSMapConfig, nil
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dont you need the config builder to finish his work first?

}
if SCSEngines != "" {
SCSEnginesTypes := strings.Split(SCSEngines, ",")
for _, engineType := range SCSEnginesTypes {
Expand Down
89 changes: 87 additions & 2 deletions internal/commands/scan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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: "true",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

trueString

ScsScoreCardType: falseString,
},
},
}

result, _ := addSCSScan(cmdCommand, resubmitConfig)

expectedConfig := wrappers.SCSConfig{
Twoms: "true",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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: "true",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand Down
Loading