-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #978 from Checkmarx/feature/Disabling-triggering-a…
…-Scorecard-scan-for-repos-other-than-GitHub Disable triggering scorecard scan for hosts other than https github (AST-78326)
- Loading branch information
Showing
2 changed files
with
242 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ import ( | |
"archive/zip" | ||
"bytes" | ||
"fmt" | ||
"io" | ||
"log" | ||
"os" | ||
"reflect" | ||
|
@@ -31,7 +32,9 @@ const ( | |
unknownFlag = "unknown flag: --chibutero" | ||
blankSpace = " " | ||
errorMissingBranch = "Failed creating a scan: Please provide a branch" | ||
dummyGitlabRepo = "https://gitlab.com/dummy-org/gitlab-dummy" | ||
dummyRepo = "https://github.com/dummyuser/dummy_project.git" | ||
dummyShortenedGithubRepo = "github.com/dummyuser/dummy_project.git" | ||
dummyToken = "dummyToken" | ||
dummySSHRepo = "[email protected]:dummyRepo/dummyProject.git" | ||
errorSourceBadFormat = "Failed creating a scan: Input in bad format: Sources input has bad format: " | ||
|
@@ -1034,6 +1037,165 @@ func TestCreateScan_WithSCSSecretDetection_scsMapHasSecretDetection(t *testing.T | |
} | ||
} | ||
|
||
func TestCreateScan_WithSCSSecretDetectionAndScorecardShortenedGithubRepo_scsMapHasBoth(t *testing.T) { | ||
// Create a pipe for capturing stdout | ||
r, w, _ := os.Pipe() | ||
oldStdout := os.Stdout | ||
defer func() { os.Stdout = oldStdout }() | ||
os.Stdout = w // Redirecting stdout to the pipe | ||
|
||
var resubmitConfig []wrappers.Config | ||
cmdCommand := &cobra.Command{ | ||
Use: "scan", | ||
Short: "Scan a project", | ||
Long: `Scan a project`, | ||
} | ||
cmdCommand.PersistentFlags().String(commonParams.SCSEnginesFlag, "", "SCS Engine flag") | ||
cmdCommand.PersistentFlags().String(commonParams.SCSRepoTokenFlag, "", "GitHub token to be used with SCS engines") | ||
cmdCommand.PersistentFlags().String(commonParams.SCSRepoURLFlag, "", "GitHub url to be used with SCS engines") | ||
_ = cmdCommand.Execute() | ||
_ = cmdCommand.Flags().Set(commonParams.SCSEnginesFlag, "secret-detection,scorecard") | ||
_ = cmdCommand.Flags().Set(commonParams.SCSRepoTokenFlag, dummyToken) | ||
_ = cmdCommand.Flags().Set(commonParams.SCSRepoURLFlag, dummyShortenedGithubRepo) | ||
|
||
result, _ := addSCSScan(cmdCommand, resubmitConfig, true) | ||
|
||
// Close the writer to signal that we are done capturing the output | ||
w.Close() | ||
|
||
// Read from the pipe (stdout) | ||
var buf bytes.Buffer | ||
_, err := io.Copy(&buf, r) // Copy the captured output to a buffer | ||
if err != nil { | ||
t.Fatalf("Failed to capture output: %v", err) | ||
} | ||
|
||
output := buf.String() | ||
if strings.Contains(output, ScsScorecardUnsupportedHostWarningMsg) { | ||
t.Errorf("Expected output to not contain %q, but got %q", ScsScorecardUnsupportedHostWarningMsg, output) | ||
} | ||
|
||
scsConfig := wrappers.SCSConfig{ | ||
Twoms: "true", | ||
Scorecard: "true", | ||
RepoURL: dummyShortenedGithubRepo, | ||
RepoToken: dummyToken, | ||
} | ||
scsMapConfig := make(map[string]interface{}) | ||
scsMapConfig[resultsMapType] = commonParams.MicroEnginesType | ||
scsMapConfig[resultsMapValue] = &scsConfig | ||
|
||
if !reflect.DeepEqual(result, scsMapConfig) { | ||
t.Errorf("Expected %+v, but got %+v", scsMapConfig, result) | ||
} | ||
} | ||
|
||
func TestCreateScan_WithSCSSecretDetectionAndScorecardGitLabRepo_scsMapHasSecretDetection(t *testing.T) { | ||
// Create a pipe for capturing stdout | ||
r, w, _ := os.Pipe() | ||
oldStdout := os.Stdout | ||
defer func() { os.Stdout = oldStdout }() | ||
os.Stdout = w // Redirecting stdout to the pipe | ||
|
||
var resubmitConfig []wrappers.Config | ||
cmdCommand := &cobra.Command{ | ||
Use: "scan", | ||
Short: "Scan a project", | ||
Long: `Scan a project`, | ||
} | ||
cmdCommand.PersistentFlags().String(commonParams.SCSEnginesFlag, "", "SCS Engine flag") | ||
cmdCommand.PersistentFlags().String(commonParams.SCSRepoTokenFlag, "", "GitHub token to be used with SCS engines") | ||
cmdCommand.PersistentFlags().String(commonParams.SCSRepoURLFlag, "", "GitHub url to be used with SCS engines") | ||
_ = cmdCommand.Execute() | ||
_ = cmdCommand.Flags().Set(commonParams.SCSEnginesFlag, "secret-detection,scorecard") | ||
_ = cmdCommand.Flags().Set(commonParams.SCSRepoTokenFlag, dummyToken) | ||
_ = cmdCommand.Flags().Set(commonParams.SCSRepoURLFlag, dummyGitlabRepo) | ||
|
||
result, _ := addSCSScan(cmdCommand, resubmitConfig, true) | ||
|
||
// Close the writer to signal that we are done capturing the output | ||
w.Close() | ||
|
||
// Read from the pipe (stdout) | ||
var buf bytes.Buffer | ||
_, err := io.Copy(&buf, r) // Copy the captured output to a buffer | ||
if err != nil { | ||
t.Fatalf("Failed to capture output: %v", err) | ||
} | ||
|
||
output := buf.String() | ||
if !strings.Contains(output, ScsScorecardUnsupportedHostWarningMsg) { | ||
t.Errorf("Expected output to contain %q, but got %q", ScsScorecardUnsupportedHostWarningMsg, output) | ||
} | ||
|
||
scsConfig := wrappers.SCSConfig{ | ||
Twoms: "true", | ||
Scorecard: "", | ||
RepoURL: "", | ||
RepoToken: "", | ||
} | ||
scsMapConfig := make(map[string]interface{}) | ||
scsMapConfig[resultsMapType] = commonParams.MicroEnginesType | ||
scsMapConfig[resultsMapValue] = &scsConfig | ||
|
||
if !reflect.DeepEqual(result, scsMapConfig) { | ||
t.Errorf("Expected %+v, but got %+v", scsMapConfig, result) | ||
} | ||
} | ||
|
||
func TestCreateScan_WithSCSSecretDetectionAndScorecardGitSSHRepo_scsMapHasSecretDetection(t *testing.T) { | ||
// Create a pipe for capturing stdout | ||
r, w, _ := os.Pipe() | ||
oldStdout := os.Stdout | ||
defer func() { os.Stdout = oldStdout }() | ||
os.Stdout = w // Redirecting stdout to the pipe | ||
|
||
var resubmitConfig []wrappers.Config | ||
cmdCommand := &cobra.Command{ | ||
Use: "scan", | ||
Short: "Scan a project", | ||
Long: `Scan a project`, | ||
} | ||
cmdCommand.PersistentFlags().String(commonParams.SCSEnginesFlag, "", "SCS Engine flag") | ||
cmdCommand.PersistentFlags().String(commonParams.SCSRepoTokenFlag, "", "GitHub token to be used with SCS engines") | ||
cmdCommand.PersistentFlags().String(commonParams.SCSRepoURLFlag, "", "GitHub url to be used with SCS engines") | ||
_ = cmdCommand.Execute() | ||
_ = cmdCommand.Flags().Set(commonParams.SCSEnginesFlag, "secret-detection,scorecard") | ||
_ = cmdCommand.Flags().Set(commonParams.SCSRepoTokenFlag, dummyToken) | ||
_ = cmdCommand.Flags().Set(commonParams.SCSRepoURLFlag, dummySSHRepo) | ||
|
||
result, _ := addSCSScan(cmdCommand, resubmitConfig, true) | ||
|
||
// Close the writer to signal that we are done capturing the output | ||
w.Close() | ||
|
||
// Read from the pipe (stdout) | ||
var buf bytes.Buffer | ||
_, err := io.Copy(&buf, r) // Copy the captured output to a buffer | ||
if err != nil { | ||
t.Fatalf("Failed to capture output: %v", err) | ||
} | ||
|
||
output := buf.String() | ||
if !strings.Contains(output, ScsScorecardUnsupportedHostWarningMsg) { | ||
t.Errorf("Expected output to contain %q, but got %q", ScsScorecardUnsupportedHostWarningMsg, output) | ||
} | ||
|
||
scsConfig := wrappers.SCSConfig{ | ||
Twoms: "true", | ||
Scorecard: "", | ||
RepoURL: "", | ||
RepoToken: "", | ||
} | ||
scsMapConfig := make(map[string]interface{}) | ||
scsMapConfig[resultsMapType] = commonParams.MicroEnginesType | ||
scsMapConfig[resultsMapValue] = &scsConfig | ||
|
||
if !reflect.DeepEqual(result, scsMapConfig) { | ||
t.Errorf("Expected %+v, but got %+v", scsMapConfig, result) | ||
} | ||
} | ||
|
||
func Test_isDirFiltered(t *testing.T) { | ||
type args struct { | ||
filename string | ||
|