diff --git a/internal/commands/policymanagement/policy.go b/internal/commands/policymanagement/policy.go index 9c9df3446..bc41bf053 100644 --- a/internal/commands/policymanagement/policy.go +++ b/internal/commands/policymanagement/policy.go @@ -104,7 +104,7 @@ func isPolicyEvaluated( return false, nil, err } if errorModel != nil { - log.Fatalf(fmt.Sprintf("%s: CODE: %d, %s", failedGetting, errorModel.Code, errorModel.Message)) + return false, nil, fmt.Errorf("%s: CODE: %d, %s", failedGetting, errorModel.Code, errorModel.Message) } else if policyResponseModel != nil { if policyResponseModel.Status == evaluatingPolicy { log.Println("Policy status: ", policyResponseModel.Status) diff --git a/internal/commands/scan.go b/internal/commands/scan.go index be5dcbe80..9cc112f97 100644 --- a/internal/commands/scan.go +++ b/internal/commands/scan.go @@ -2101,10 +2101,12 @@ func isScanRunning( var err error scanResponseModel, errorModel, err = scansWrapper.GetByID(scanID) if err != nil { - log.Fatal("Cannot source code temp file.", err) + logger.PrintfIfVerbose("Cannot source code temp file: %s", err) + return false, errors.Wrap(err, "Cannot source code temp file.") } if errorModel != nil { - log.Fatalf(fmt.Sprintf("%s: CODE: %d, %s", failedGetting, errorModel.Code, errorModel.Message)) + logger.PrintfIfVerbose("%s: CODE: %d, %s", failedGetting, errorModel.Code, errorModel.Message) + return false, errors.Errorf("%s: CODE: %d, %s", failedGetting, errorModel.Code, errorModel.Message) } else if scanResponseModel != nil { if scanResponseModel.Status == wrappers.ScanRunning || scanResponseModel.Status == wrappers.ScanQueued { log.Println("Scan status: ", scanResponseModel.Status) diff --git a/internal/services/osinstaller/linux-mac-utils.go b/internal/services/osinstaller/linux-mac-utils.go index e68b930e9..95349517b 100644 --- a/internal/services/osinstaller/linux-mac-utils.go +++ b/internal/services/osinstaller/linux-mac-utils.go @@ -52,22 +52,26 @@ func extractFiles(installationConfiguration *InstallationConfiguration, tarReade } if err != nil { - log.Fatalf("ExtractTarGz: Next() failed: %s", err.Error()) + logger.PrintfIfVerbose("ExtractTarGz: Next() failed: %s", err.Error()) + return err } switch header.Typeflag { case tar.TypeDir: if err := os.Mkdir(header.Name, os.FileMode(dirDefault)); err != nil { - log.Fatalf("ExtractTarGz: Mkdir() failed: %s", err.Error()) + logger.PrintfIfVerbose("ExtractTarGz: Mkdir() failed: %s", err.Error()) + return err } case tar.TypeReg: extractedFilePath := filepath.Join(installationConfiguration.WorkingDir(), header.Name) outFile, err := os.Create(extractedFilePath) if err != nil { - log.Fatalf("ExtractTarGz: Create() failed: %s", err.Error()) + logger.PrintfIfVerbose("ExtractTarGz: Create() failed: %s", err.Error()) + return err } if _, err = io.Copy(outFile, tarReader); err != nil { - log.Fatalf("ExtractTarGz: Copy() failed: %s", err.Error()) + logger.PrintfIfVerbose("ExtractTarGz: Copy() failed: %s", err.Error()) + return err } err = outFile.Close() if err != nil { @@ -78,10 +82,8 @@ func extractFiles(installationConfiguration *InstallationConfiguration, tarReade return err } default: - log.Fatalf( - "ExtractTarGz: uknown type: %v in %s", - header.Typeflag, - header.Name) + logger.PrintfIfVerbose("ExtractTarGz: uknown type: %v in %s", header.Typeflag, header.Name) + return err } } return nil diff --git a/internal/services/projects.go b/internal/services/projects.go index fb2c4a0bb..8b8f625ab 100644 --- a/internal/services/projects.go +++ b/internal/services/projects.go @@ -34,7 +34,7 @@ func FindProject( params := make(map[string]string) params["names"] = projectName resp, _, err := projectsWrapper.Get(params) - if err != nil { + if err != nil || resp == nil { return "", err } diff --git a/internal/wrappers/client.go b/internal/wrappers/client.go index 60ad98cf3..4cb7688e1 100644 --- a/internal/wrappers/client.go +++ b/internal/wrappers/client.go @@ -248,7 +248,7 @@ func addReqMonitor(req *http.Request) *http.Request { } func SendHTTPRequestPasswordAuth(method string, body io.Reader, timeout uint, username, password, adminClientID, adminClientSecret string) (*http.Response, error) { - u, err := getAuthURI() + u, err := GetAuthURI() if err != nil { return nil, err } @@ -375,7 +375,7 @@ func GetWithQueryParamsAndCustomRequest(client *http.Client, customReq *http.Req return request(client, customReq, true) } func GetAccessToken() (string, error) { - authURI, err := getAuthURI() + authURI, err := GetAuthURI() if err != nil { return "", err } @@ -402,7 +402,7 @@ func enrichWithPasswordCredentials( request *http.Request, username, password, adminClientID, adminClientSecret string, ) error { - authURI, err := getAuthURI() + authURI, err := GetAuthURI() if err != nil { return err } @@ -479,7 +479,7 @@ func getNewToken(credentialsPayload, authServerURI string) (string, error) { res, err := doPrivateRequest(client, req) if err != nil { - authURL, _ := getAuthURI() + authURL, _ := GetAuthURI() return "", errors.Errorf("%s %s", checkmarxURLError, authURL) } if res.StatusCode == http.StatusBadRequest { @@ -653,7 +653,7 @@ func hasRedirectStatusCode(resp *http.Response) bool { return resp.StatusCode == http.StatusTemporaryRedirect || resp.StatusCode == http.StatusMovedPermanently } -func getAuthURI() (string, error) { +func GetAuthURI() (string, error) { var authURI string var err error override := viper.GetBool(commonParams.ApikeyOverrideFlag) diff --git a/test/integration/project_test.go b/test/integration/project_test.go index 6f16ba7dc..7cb9e3eef 100644 --- a/test/integration/project_test.go +++ b/test/integration/project_test.go @@ -40,7 +40,7 @@ func TestProjectsE2E(t *testing.T) { assert.Equal(t, len(response), 1, "Total projects should be 1") assert.Equal(t, response[0].ID, projectID, "Project ID should match the created project") - project := showProject(t, projectID) + project := response[0] assert.Equal(t, project.ID, projectID, "Project ID should match the created project") assertTagsAndGroups(t, project, Groups) diff --git a/test/integration/scan_test.go b/test/integration/scan_test.go index 43fd29bf7..f94baa588 100644 --- a/test/integration/scan_test.go +++ b/test/integration/scan_test.go @@ -14,7 +14,6 @@ import ( "os" "path/filepath" "runtime" - "slices" "strings" "testing" "time" @@ -29,6 +28,7 @@ import ( "github.com/checkmarx/ast-cli/internal/params" "github.com/checkmarx/ast-cli/internal/services" "github.com/checkmarx/ast-cli/internal/wrappers" + "github.com/checkmarx/ast-cli/internal/wrappers/utils" "github.com/checkmarx/ast-cli/internal/wrappers/configuration" "github.com/pkg/errors" "github.com/spf13/viper" @@ -292,15 +292,17 @@ func TestScanCreateEmptyProjectName(t *testing.T) { } func TestScanCreate_ExistingApplicationAndExistingProject_CreateScanSuccessfully(t *testing.T) { + projectId, projectName := createProject(t, nil, nil) args := []string{ "scan", "create", flag(params.ApplicationName), "my-application", - flag(params.ProjectName), "my-project", + flag(params.ProjectName), projectName, flag(params.SourcesFlag), ".", flag(params.ScanTypes), "sast", flag(params.BranchFlag), "dummy_branch", + flag(params.DebugFlag), } - + defer deleteProject(t, projectId) err, _ := executeCommand(t, args...) assert.NilError(t, err) } @@ -353,6 +355,7 @@ func TestScanCreate_ApplicationDoesntExist_FailScanWithError(t *testing.T) { flag(params.SourcesFlag), ".", flag(params.ScanTypes), "sast", flag(params.BranchFlag), "dummy_branch", + flag(params.DebugFlag), } err, _ := executeCommand(t, args...) @@ -860,7 +863,7 @@ func executeScanAssertions(t *testing.T, projectID, scanID string, tags map[stri func createScan(t *testing.T, source string, tags map[string]string) (string, string) { if isFFEnabled(t, wrappers.ContainerEngineCLIEnabled) { - return executeCreateScan(t, getCreateArgs(source, tags, "sast , sca , iac-security , api-security, scs,container-security")) + return executeCreateScan(t, getCreateArgs(source, tags, "sast , sca , iac-security , api-security, scs, container-security")) } else { return executeCreateScan(t, getCreateArgs(source, tags, "sast , sca , iac-security , api-security, scs")) } @@ -1025,6 +1028,7 @@ func TestScanWorkflow(t *testing.T) { } cmd := createASTIntegrationTestCommand(t) err := execute(cmd, args...) + log.Println(err) assert.Assert(t, err != nil, "Failed showing a scan: response status code 404") } @@ -1643,7 +1647,6 @@ func TestScanWithPolicy(t *testing.T) { flag(params.ScanTypes), "sast", flag(params.BranchFlag), "main", flag(params.TargetFormatFlag), "markdown,summaryConsole,summaryHTML"} - err, _ := executeCommand(t, args...) assert.NilError(t, err) } @@ -1822,14 +1825,16 @@ func addSCSDefaultFlagsToArgs(args *[]string) { func TestCreateScanAndValidateCheckmarxDomains(t *testing.T) { wrappers.Domains = make(map[string]struct{}) _, _ = executeCreateScan(t, getCreateArgsWithGroups(Zip, Tags, Groups, "sast,iac-security,sca")) - usedDomainsInTests := []string{"deu.iam.checkmarx.net", "deu.ast.checkmarx.net"} - validateCheckmarxDomains(t, usedDomainsInTests) + baseUrl, _ := wrappers.GetURL("", "") + authUri, _ := wrappers.GetAuthURI() + usedDomainsFromConfig := []string{baseUrl, authUri} + validateCheckmarxDomains(t, usedDomainsFromConfig) } func validateCheckmarxDomains(t *testing.T, usedDomainsInTests []string) { usedDomains := wrappers.Domains for domain, _ := range usedDomains { - assert.Assert(t, slices.Contains(usedDomainsInTests, domain), "Domain "+domain+" not found in used domains") + assert.Assert(t, utils.Contains(usedDomainsInTests, domain), "Domain "+domain+" not found in used domains") } } diff --git a/test/integration/util.go b/test/integration/util.go index bdb969654..5967bf79f 100644 --- a/test/integration/util.go +++ b/test/integration/util.go @@ -55,6 +55,7 @@ func flag(f string) string { } func getProjectNameForTest() string { + projectNameRandom = uuid.New().String() return fmt.Sprintf("ast-cli-tests_%s", projectNameRandom) }