Skip to content

Commit

Permalink
Revert "Map CLI exit codes by scanner type (AST-37829)"
Browse files Browse the repository at this point in the history
  • Loading branch information
OrShamirCM authored Apr 16, 2024
1 parent cee4a75 commit 4c4fcf1
Show file tree
Hide file tree
Showing 10 changed files with 23 additions and 210 deletions.
45 changes: 4 additions & 41 deletions internal/commands/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"time"

applicationErrors "github.com/checkmarx/ast-cli/internal/errors"
exitCodes "github.com/checkmarx/ast-cli/internal/errors/exit-codes"

"github.com/checkmarx/ast-cli/internal/commands/scarealtime"
"github.com/checkmarx/ast-cli/internal/commands/util"
Expand Down Expand Up @@ -885,6 +884,7 @@ func getApplication(applicationName string, applicationsWrapper wrappers.Applica
params["name"] = applicationName
resp, err := applicationsWrapper.Get(params)
if err != nil {

return nil, err
}
if resp.Applications != nil && len(resp.Applications) > 0 {
Expand Down Expand Up @@ -1955,8 +1955,7 @@ func waitForScanCompletion(
if errorModel != nil {
return errors.Errorf(ErrorCodeFormat, failedCanceling, errorModel.Code, errorModel.Message)
}

return wrappers.NewAstError(exitCodes.MultipleEnginesFailedExitCode, errors.Errorf("Timeout of %d minute(s) for scan reached", timeoutMinutes))
return errors.Errorf("Timeout of %d minute(s) for scan reached", timeoutMinutes)
}
i++
}
Expand Down Expand Up @@ -2001,49 +2000,13 @@ func isScanRunning(
if reportErr != nil {
return false, errors.New("unable to create report for partial scan")
}
exitCode := getExitCode(scanResponseModel)
return false, wrappers.NewAstError(exitCode, errors.New("scan completed partially"))
return false, errors.New("scan completed partially")
} else if scanResponseModel.Status != wrappers.ScanCompleted {
exitCode := getExitCode(scanResponseModel)
return false, wrappers.NewAstError(exitCode, errors.New("scan did not complete successfully"))
return false, errors.New("scan did not complete successfully")
}
return false, nil
}

func getExitCode(scanResponseModel *wrappers.ScanResponseModel) int {
failedStatuses := make([]int, 0)
for _, scanner := range scanResponseModel.StatusDetails {
scannerNameLowerCase := strings.ToLower(scanner.Name)
scannerErrorExitCode, errorCodeByScannerExists := errorCodesByScanner[scannerNameLowerCase]
if scanner.Status == wrappers.ScanFailed && scanner.Name != General && errorCodeByScannerExists {
failedStatuses = append(failedStatuses, scannerErrorExitCode)
}
}
if len(failedStatuses) == 1 {
return failedStatuses[0]
}

return exitCodes.MultipleEnginesFailedExitCode
}

const (
General = "general"
Sast = "sast"
Sca = "sca"
IacSecurity = "iac-security" // We get 'kics' from AST. Added for forward compatibility
Kics = "kics"
APISec = "apisec"
)

var errorCodesByScanner = map[string]int{
General: exitCodes.MultipleEnginesFailedExitCode,
Sast: exitCodes.SastEngineFailedExitCode,
Sca: exitCodes.ScaEngineFailedExitCode,
IacSecurity: exitCodes.IacSecurityEngineFailedExitCode,
Kics: exitCodes.KicsEngineFailedExitCode,
APISec: exitCodes.ApisecEngineFailedExitCode,
}

func runListScansCommand(scansWrapper wrappers.ScansWrapper, sastMetadataWrapper wrappers.SastMetadataWrapper) func(cmd *cobra.Command, args []string) error {
return func(cmd *cobra.Command, args []string) error {
var allScansModel *wrappers.ScansCollectionResponseModel
Expand Down
40 changes: 0 additions & 40 deletions internal/commands/scan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,14 @@
package commands

import (
"fmt"
"reflect"
"strings"
"testing"

applicationErrors "github.com/checkmarx/ast-cli/internal/errors"
exitCodes "github.com/checkmarx/ast-cli/internal/errors/exit-codes"
commonParams "github.com/checkmarx/ast-cli/internal/params"
"github.com/checkmarx/ast-cli/internal/wrappers"
"github.com/checkmarx/ast-cli/internal/wrappers/mock"
"github.com/pkg/errors"
"gotest.tools/assert"

"github.com/checkmarx/ast-cli/internal/commands/util"
Expand Down Expand Up @@ -232,43 +229,6 @@ func TestCreateScanWithScanTypes(t *testing.T) {
execCmdNilAssertion(t, append(baseArgs, "--scan-types", "sast,api-security")...)
}

func TestScanCreate_KicsScannerFail_ReturnCorrectKicsExitCodeAndErrorMessage(t *testing.T) {
baseArgs := []string{"scan", "create", "--project-name", "fake-kics-scanner-fail", "-s", dummyRepo, "-b", "dummy_branch"}
err := execCmdNotNilAssertion(t, append(baseArgs, "--scan-types", Kics)...)
assertAstError(t, err, "scan did not complete successfully", exitCodes.KicsEngineFailedExitCode)
}

func TestScanCreate_MultipleScannersFail_ReturnGeneralExitCodeAndErrorMessage(t *testing.T) {
baseArgs := []string{"scan", "create", "--project-name", "fake-multiple-scanner-fails", "-s", dummyRepo, "-b", "dummy_branch"}
baseArgs = append(baseArgs, "--scan-types", fmt.Sprintf("%s,%s", Kics, Sca))
err := execCmdNotNilAssertion(t, baseArgs...)
assertAstError(t, err, "scan did not complete successfully", exitCodes.MultipleEnginesFailedExitCode)
}

func TestScanCreate_ScaScannersFailPartialScan_ReturnScaExitCodeAndErrorMessage(t *testing.T) {
baseArgs := []string{"scan", "create", "--project-name", "fake-sca-fail-partial", "-s", dummyRepo, "-b", "dummy_branch"}
baseArgs = append(baseArgs, "--scan-types", Sca)
err := execCmdNotNilAssertion(t, baseArgs...)
assertAstError(t, err, "scan completed partially", exitCodes.ScaEngineFailedExitCode)
}

func TestScanCreate_MultipleScannersDifferentStatusesOnlyKicsFail_ReturnKicsExitCodeAndErrorMessage(t *testing.T) {
baseArgs := []string{"scan", "create", "--project-name", "fake-kics-fail-sast-canceled", "-s", dummyRepo, "-b", "dummy_branch"}
baseArgs = append(baseArgs, "--scan-types", fmt.Sprintf("%s,%s,%s", Sca, Sast, Kics))
err := execCmdNotNilAssertion(t, baseArgs...)
assertAstError(t, err, "scan did not complete successfully", exitCodes.KicsEngineFailedExitCode)
}

func assertAstError(t *testing.T, err error, expectedErrorMessage string, expectedExitCode int) {
var e *wrappers.AstError
if errors.As(err, &e) {
assert.Equal(t, e.Error(), expectedErrorMessage)
assert.Equal(t, e.Code, expectedExitCode)
} else {
assert.Assert(t, false, "Error is not of type AstError")
}
}

func TestCreateScanWithNoFilteredProjects(t *testing.T) {
baseArgs := []string{"scan", "create", "-s", dummyRepo, "-b", "dummy_branch"}
// Cover "createProject" when no project is filtered when finding the provided project
Expand Down
5 changes: 2 additions & 3 deletions internal/commands/util/learnmore.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ package util

// nolint:goimports
import (
"html"
"log"

"github.com/MakeNowJust/heredoc"
"github.com/checkmarx/ast-cli/internal/commands/util/printer"
"github.com/checkmarx/ast-cli/internal/params"
"github.com/checkmarx/ast-cli/internal/wrappers"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"html"
"log"
)

const defaultFormat = "list"
Expand Down
10 changes: 0 additions & 10 deletions internal/errors/exit-codes/exit-codes.go

This file was deleted.

24 changes: 3 additions & 21 deletions internal/wrappers/mock/projects-mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,33 +40,15 @@ func (p *ProjectsMockWrapper) Get(params map[string]string) (
filteredTotalCount = 0
}

var model *wrappers.ProjectsCollectionResponseModel
switch name := params["names"]; name {
case "fake-kics-scanner-fail":
model = getProjectResponseModel(fmt.Sprintf("%s-id", name), name, filteredTotalCount)
case "fake-multiple-scanner-fails":
model = getProjectResponseModel(fmt.Sprintf("%s-id", name), name, filteredTotalCount)
case "fake-sca-fail-partial":
model = getProjectResponseModel(fmt.Sprintf("%s-id", name), name, filteredTotalCount)
case "fake-kics-fail-sast-canceled":
model = getProjectResponseModel(fmt.Sprintf("%s-id", name), name, filteredTotalCount)
default:
model = getProjectResponseModel("MOCK", "MOCK", filteredTotalCount)
}

return model, nil, nil
}

func getProjectResponseModel(id, name string, filteredTotalCount int) *wrappers.ProjectsCollectionResponseModel {
return &wrappers.ProjectsCollectionResponseModel{
FilteredTotalCount: uint(filteredTotalCount),
Projects: []wrappers.ProjectResponseModel{
{
ID: id,
Name: name,
ID: "MOCK",
Name: "MOCK",
},
},
}
}, nil, nil
}

func (p *ProjectsMockWrapper) GetByID(projectID string) (
Expand Down
1 change: 0 additions & 1 deletion internal/wrappers/mock/sca-realtime-mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package mock

import (
"fmt"

"github.com/checkmarx/ast-cli/internal/wrappers"
)

Expand Down
69 changes: 1 addition & 68 deletions internal/wrappers/mock/scans-mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,33 +17,8 @@ func (m *ScansMockWrapper) GetWorkflowByID(_ string) ([]*wrappers.ScanTaskRespon
return nil, nil, nil
}

func (m *ScansMockWrapper) Create(scanModel *wrappers.Scan) (*wrappers.ScanResponseModel, *wrappers.ErrorModel, error) {
func (m *ScansMockWrapper) Create(_ *wrappers.Scan) (*wrappers.ScanResponseModel, *wrappers.ErrorModel, error) {
fmt.Println("Called Create in ScansMockWrapper")
if scanModel.Project.ID == "fake-kics-scanner-fail-id" {
return &wrappers.ScanResponseModel{
ID: "fake-scan-id-kics-scanner-fail",
Status: "MOCK",
}, nil, nil
}
if scanModel.Project.ID == "fake-multiple-scanner-fails-id" {
return &wrappers.ScanResponseModel{
ID: "fake-scan-id-multiple-scanner-fails",
Status: "MOCK",
}, nil, nil
}
if scanModel.Project.ID == "fake-sca-fail-partial-id" {
return &wrappers.ScanResponseModel{
ID: "fake-scan-id-sca-fail-partial-id",
Status: "MOCK",
}, nil, nil
}
if scanModel.Project.ID == "fake-kics-fail-sast-canceled-id" {
return &wrappers.ScanResponseModel{
ID: "fake-scan-id-kics-fail-sast-canceled-id",
Status: "MOCK",
}, nil, nil
}

return &wrappers.ScanResponseModel{
ID: uuid.New().String(),
Status: "MOCK",
Expand Down Expand Up @@ -103,48 +78,6 @@ func (m *ScansMockWrapper) Get(_ map[string]string) (

func (m *ScansMockWrapper) GetByID(scanID string) (*wrappers.ScanResponseModel, *wrappers.ErrorModel, error) {
fmt.Println("Called GetByID in ScansMockWrapper")
if scanID == "fake-scan-id-kics-scanner-fail" {
return &wrappers.ScanResponseModel{
ID: "fake-scan-id-kics-scanner-fail",
Status: wrappers.ScanFailed,
StatusDetails: []wrappers.StatusInfo{
{
Status: wrappers.ScanFailed, Name: "kics",
},
},
}, nil, nil
}
if scanID == "fake-scan-id-multiple-scanner-fails" {
return &wrappers.ScanResponseModel{
ID: "fake-scan-id-multiple-scanner-fails",
Status: wrappers.ScanFailed,
StatusDetails: []wrappers.StatusInfo{
{Status: wrappers.ScanFailed, Name: "kics"},
{Status: wrappers.ScanFailed, Name: "sca"},
},
}, nil, nil
}
if scanID == "fake-scan-id-sca-fail-partial-id" {
return &wrappers.ScanResponseModel{
ID: "fake-scan-id-sca-fail-partial-id",
Status: wrappers.ScanPartial,
StatusDetails: []wrappers.StatusInfo{
{Status: wrappers.ScanFailed, Name: "sca"},
},
}, nil, nil
}
if scanID == "fake-scan-id-kics-fail-sast-canceled-id" {
return &wrappers.ScanResponseModel{
ID: "fake-scan-id-kics-fail-sast-canceled-id",
Status: wrappers.ScanFailed,
StatusDetails: []wrappers.StatusInfo{
{Status: wrappers.ScanCompleted, Name: "general"},
{Status: wrappers.ScanCanceled, Name: "sast"},
{Status: wrappers.ScanFailed, Name: "kics"},
},
}, nil, nil
}

var status wrappers.ScanStatus = "Completed"
m.Running = !m.Running
return &wrappers.ScanResponseModel{
Expand Down
3 changes: 1 addition & 2 deletions test/integration/learnmore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
package integration

import (
"testing"

"github.com/checkmarx/ast-cli/internal/params"
"github.com/spf13/viper"
"gotest.tools/assert"
"testing"
)

func TestGetLearnMoreInformationFailure(t *testing.T) {
Expand Down
16 changes: 2 additions & 14 deletions test/integration/scan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,8 @@ import (
"github.com/checkmarx/ast-cli/internal/commands/util"
"github.com/checkmarx/ast-cli/internal/commands/util/printer"
applicationErrors "github.com/checkmarx/ast-cli/internal/errors"
exitCodes "github.com/checkmarx/ast-cli/internal/errors/exit-codes"
"github.com/checkmarx/ast-cli/internal/params"
"github.com/checkmarx/ast-cli/internal/wrappers"
"github.com/pkg/errors"
"github.com/spf13/viper"
"gotest.tools/assert"
)
Expand Down Expand Up @@ -704,19 +702,9 @@ func TestFailedScanWithWrongPreset(t *testing.T) {
flag(params.PolicyTimeoutFlag),
"999999",
}
err, _ := executeCommand(t, args...)
assertAstError(t, err, "scan did not complete successfully", exitCodes.SastEngineFailedExitCode)
}

func assertAstError(t *testing.T, err error, expectedErrorMessage string, expectedExitCode int) {
var e *wrappers.AstError
if errors.As(err, &e) {
assert.Equal(t, e.Error(), expectedErrorMessage)
assert.Equal(t, e.Code, expectedExitCode)
} else {
assertError(t, err, "Error is not of type AstError")
assert.Assert(t, false, fmt.Sprintf("Error is not of type AstError. Error message: %s", err.Error()))
}
err, _ := executeCommand(t, args...)
assertError(t, err, "scan did not complete successfully")
}

func retrieveResultsFromScanId(t *testing.T, scanId string) (wrappers.ScanResultsCollection, error) {
Expand Down
20 changes: 10 additions & 10 deletions test/integration/tenant_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@
package integration

import (
"testing"
"testing"

"github.com/checkmarx/ast-cli/internal/params"
"gotest.tools/assert"
"github.com/checkmarx/ast-cli/internal/params"
"gotest.tools/assert"
)

func TestGetTenantConfigurationSuccessCaseJson(t *testing.T) {
err, _ := executeCommand(
t, "utils", "tenant",
flag(params.FormatFlag), "json",
)
assert.NilError(t, err, "Must not fail")
err, _ := executeCommand(
t, "utils", "tenant",
flag(params.FormatFlag), "json",
)
assert.NilError(t, err, "Must not fail")
}

func TestGetTenantConfigurationSuccessCaseList(t *testing.T) {
err, _ := executeCommand(t, "utils", "tenant")
assert.NilError(t, err, "Must not fail")
err, _ := executeCommand(t, "utils", "tenant")
assert.NilError(t, err, "Must not fail")
}

//func TestGetTenantConfigurationFailCase(t *testing.T) {
Expand Down

0 comments on commit 4c4fcf1

Please sign in to comment.