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
55 changes: 39 additions & 16 deletions internal/commands/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -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." +
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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]
Copy link
Contributor

Choose a reason for hiding this comment

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

what is the difference between the regular config building and resubmit? Can't we re-use the regular build config?

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
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 {
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)
Expand All @@ -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
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: 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
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