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

Delete log fatal #834

Closed
wants to merge 24 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion internal/commands/policymanagement/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 4 additions & 2 deletions internal/commands/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
18 changes: 10 additions & 8 deletions internal/services/osinstaller/linux-mac-utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion internal/services/projects.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
10 changes: 5 additions & 5 deletions internal/wrappers/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion test/integration/project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
21 changes: 13 additions & 8 deletions test/integration/scan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"os"
"path/filepath"
"runtime"
"slices"
"strings"
"testing"
"time"
Expand All @@ -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"
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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...)
Expand Down Expand Up @@ -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"))
}
Expand Down Expand Up @@ -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")
}

Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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")
}
}

Expand Down
1 change: 1 addition & 0 deletions test/integration/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ func flag(f string) string {
}

func getProjectNameForTest() string {
projectNameRandom = uuid.New().String()
return fmt.Sprintf("ast-cli-tests_%s", projectNameRandom)
}

Expand Down
Loading