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

Feature/kobih/exit code command #711

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
72 commits
Select commit Hold shift + click to select a range
af48757
results exit-code
checkmarx-kobi-hagmi Apr 7, 2024
c5335fb
Added handling for ScanCanceled, ScanRunning and ScanQueued
checkmarx-kobi-hagmi Apr 8, 2024
e8e9f15
using one model for response
checkmarx-kobi-hagmi Apr 8, 2024
f327cc8
removed fmt prints
checkmarx-kobi-hagmi Apr 8, 2024
8889358
Added additional unit tests and integration test
checkmarx-kobi-hagmi Apr 8, 2024
3a347b8
linter
checkmarx-kobi-hagmi Apr 8, 2024
b8f3d5a
linter
checkmarx-kobi-hagmi Apr 8, 2024
cbf3784
Merge branch 'main' into feature/kobih/exit-code-command
checkmarx-kobi-hagmi Apr 8, 2024
1b9687b
Fixed integration test
checkmarx-kobi-hagmi Apr 9, 2024
23e0c24
Attempt to pass test
checkmarx-kobi-hagmi Apr 10, 2024
2d8dd02
Merge branch 'main' into feature/kobih/exit-code-command
checkmarx-kobi-hagmi Apr 10, 2024
7641415
Merge branch 'main' into feature/kobih/exit-code-command
checkmarx-kobi-hagmi Apr 10, 2024
855642e
fixed PR comments
checkmarx-kobi-hagmi Apr 10, 2024
c7fa38b
Fixed linter issue
checkmarx-kobi-hagmi Apr 10, 2024
1dd3365
Fixed CR comments
checkmarx-kobi-hagmi Apr 10, 2024
6fbebe3
fixed linter issues
checkmarx-kobi-hagmi Apr 10, 2024
33636df
fixed documnetation
checkmarx-kobi-hagmi Apr 10, 2024
11cf648
experiment to fix tests. revert later
checkmarx-kobi-hagmi Apr 10, 2024
1419adf
trying to fix. revert later
checkmarx-kobi-hagmi Apr 10, 2024
46d003e
Fix tests
checkmarx-kobi-hagmi Apr 10, 2024
528867e
not printing General
checkmarx-kobi-hagmi Apr 10, 2024
6768b45
Added test a completed test
checkmarx-kobi-hagmi Apr 10, 2024
10687dd
small improvements
checkmarx-kobi-hagmi Apr 11, 2024
d7220f1
changed assertion
checkmarx-kobi-hagmi Apr 11, 2024
cff8250
refactoring + changed tests
checkmarx-kobi-hagmi Apr 11, 2024
032cfba
removed deletions because rans in parallel
checkmarx-kobi-hagmi Apr 11, 2024
f521c08
discard var
checkmarx-kobi-hagmi Apr 11, 2024
b600289
returns json object is scan completed
checkmarx-kobi-hagmi Apr 11, 2024
f18bef8
fixed unit test
checkmarx-kobi-hagmi Apr 11, 2024
317c1db
Increased threshold
checkmarx-kobi-hagmi Apr 11, 2024
1c3b9a1
Merge branch 'main' into feature/kobih/exit-code-command
OrShamirCM Apr 14, 2024
1249dbc
Added exit code support for containers
checkmarx-kobi-hagmi Apr 14, 2024
f170513
shorter test name
checkmarx-kobi-hagmi Apr 14, 2024
9ac8f1a
reverted threshold and re-added project and scan deletions
checkmarx-kobi-hagmi Apr 14, 2024
a392cb3
Removed containers. added unit tests
checkmarx-kobi-hagmi Apr 15, 2024
2d6d5fb
Added integration test
checkmarx-kobi-hagmi Apr 15, 2024
d1abd62
removed comment
checkmarx-kobi-hagmi Apr 15, 2024
303bef2
Update result.go
checkmarx-kobi-hagmi Apr 16, 2024
7e5e627
cr comment fix
checkmarx-kobi-hagmi Apr 17, 2024
8ba2f49
Merge remote-tracking branch 'origin/revert-709-revert-690-feature/ko…
checkmarx-kobi-hagmi Apr 17, 2024
f54127a
fix lint issue + go fmt
checkmarx-kobi-hagmi Apr 17, 2024
544d884
refactoring and modifying integration test
checkmarx-kobi-hagmi Apr 17, 2024
79054de
fix lint issue +go fmt
checkmarx-kobi-hagmi Apr 17, 2024
869a513
attempt to see if coverage is affected
checkmarx-kobi-hagmi Apr 18, 2024
deb31a1
Update result_test.go
checkmarx-kobi-hagmi Apr 18, 2024
a97a653
improved test
checkmarx-kobi-hagmi Apr 18, 2024
2e5c04c
added test to increase coverage
checkmarx-kobi-hagmi Apr 18, 2024
e446185
CLI | Add redirected request mechanism (AST-37864) (#712)
AlvoBen Apr 22, 2024
1027001
Bump github.com/stretchr/testify from 1.8.4 to 1.9.0
dependabot[bot] Apr 22, 2024
55060b4
including general in errors list
checkmarx-kobi-hagmi Apr 24, 2024
2980abc
CLI | Add threshold user input validation (AST-40169) (#715)
AlvoBen Apr 28, 2024
17c9495
Project Application association Issue - Workaround - AST-40286 (#714)
OrShamirCM Apr 30, 2024
db0f589
Merge branch 'main' into dependabot/go_modules/github.com/stretchr/te…
OrShamirCM Apr 30, 2024
25bf4f5
Update release.yml
OrShamirCM Apr 30, 2024
ac3dd9b
Merge pull request #708 from Checkmarx/dependabot/go_modules/github.c…
pedrompflopes Apr 30, 2024
e774092
Update release.yml
OrShamirCM Apr 30, 2024
87dd309
Add Makefile
checkmarx-kobi-hagmi May 1, 2024
a34356c
Update README.md
checkmarx-kobi-hagmi May 1, 2024
ecd348d
Merge pull request #718 from Checkmarx/feature/kobih/adding-makefile
checkmarx-kobi-hagmi May 1, 2024
df376c4
Merge branch 'main' into feature/kobih/exit-code-command
checkmarx-kobi-hagmi May 1, 2024
fbb9905
go mod tidy
checkmarx-kobi-hagmi May 1, 2024
866ff10
fixed unit tests. removed unused code
checkmarx-kobi-hagmi May 2, 2024
2ae7aab
Revert "Add Makefile" (#720)
OrShamirCM May 2, 2024
48f82c3
Revert "Revert "Add Makefile" (#720)" (#721)
OrShamirCM May 2, 2024
bd5d658
Try release (#722)
OrShamirCM May 2, 2024
f6fde8f
Update The Release to work with VM for Docker - AST-42234 (#724)
OrShamirCM May 5, 2024
b4b0b86
Merge branch 'main' into feature/kobih/exit-code-command
OrShamirCM May 5, 2024
2ce9a81
Fix project association polling issue (AST-40286) (#723)
OrShamirCM May 5, 2024
c174d15
Merge branch 'main' into feature/kobih/exit-code-command
OrShamirCM May 5, 2024
72e9390
update .gitignore (#727)
OrShamirCM May 5, 2024
5d65554
FIx Unit Tests Timeout (#726)
OrShamirCM May 5, 2024
4a25ed4
Merge branch 'main' into feature/kobih/exit-code-command
OrShamirCM May 5, 2024
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
9 changes: 3 additions & 6 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ permissions:

jobs:
build:
runs-on: macos-latest
runs-on: macos-13
env:
AC_PASSWORD: ${{ secrets.AC_PASSWORD }}
APPLE_DEVELOPER_CERTIFICATE_P12_BASE64: ${{ secrets.APPLE_DEVELOPER_CERTIFICATE_P12_BASE64 }}
Expand Down Expand Up @@ -62,12 +62,9 @@ jobs:
- name: Install gon
run: |
brew install Bearer/tap/gon
- name: Install and start docker
- name: Setup Docker on macOS
if: inputs.dev == false
run: |
brew install docker
colima start
sudo ln -sf $HOME/.colima/default/docker.sock /var/run/docker.sock
uses: douglascamata/setup-docker-macos-action@v1-alpha
- name: Test docker
if: inputs.dev == false
run: |
Expand Down
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,8 @@ override.tf.json

# Include tfplan files to ignore the plan output of command: terraform plan -out=tfplan
# example: *tfplan*dist/
/dist
/dist

# Ignore CLI configuration files and installation log files
**/colima-Darwin-x86_64
**/install.log
11 changes: 11 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
.DEFAULT_GOAL := vet

.PHONY:fmt vet build lint
fmt:
go fmt ./...
vet: fmt
go vet ./...
build: vet
go build -o bin/cx.exe ./cmd
lint: fmt
golangci-lint run -c .golangci.yml
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,15 @@ export GOOS=darwin
export GOARCH=amd64
go build -o ./bin/cx-mac ./cmd
```
### Makefile
For ease of use, a Makefile is provided to build the project for all platforms.

Install Make for Mac: https://formulae.brew.sh/formula/make

Install Make for Windows: https://sourceforge.net/projects/gnuwin32/files/make/3.81/make-3.81.exe/download

Run the following command to build the project:
``` make build ```

## Contribution
We appreciate feedback and contribution to the CLI! Before you get started, please see the following:
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ require (
github.com/pkg/errors v0.9.1
github.com/spf13/cobra v1.8.0
github.com/spf13/viper v1.18.2
github.com/stretchr/testify v1.8.4
github.com/stretchr/testify v1.9.0
github.com/tomnomnom/linkheader v0.0.0-20180905144013-02ca5825eb80
golang.org/x/crypto v0.22.0
golang.org/x/text v0.14.0
Expand Down
3 changes: 2 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,9 @@ github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpE
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8=
github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU=
github.com/tomnomnom/linkheader v0.0.0-20180905144013-02ca5825eb80 h1:nrZ3ySNYwJbSpD6ce9duiP+QkD3JuLCcWkdaehUS/3Y=
Expand Down
2 changes: 1 addition & 1 deletion internal/commands/.scripts/integration_up.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ rm -rf ScaResolver-linux64.tar.gz
go test \
-tags integration \
-v \
-timeout 90m \
-timeout 210m \
-coverpkg github.com/checkmarx/ast-cli/internal/commands,github.com/checkmarx/ast-cli/internal/wrappers \
-coverprofile cover.out \
github.com/checkmarx/ast-cli/test/integration
Expand Down
2 changes: 1 addition & 1 deletion internal/commands/.scripts/up.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ wget https://sca-downloads.s3.amazonaws.com/cli/latest/ScaResolver-linux64.tar.g
tar -xzvf ScaResolver-linux64.tar.gz -C /tmp
rm -rf ScaResolver-linux64.tar.gz
# ignore mock and wrappers packages, as they checked by integration tests
go test $(go list ./... | grep -v "mock" | grep -v "wrappers" | grep -v "bitbucketserver" | grep -v "logger") -coverprofile cover.out
go test $(go list ./... | grep -v "mock" | grep -v "wrappers" | grep -v "bitbucketserver" | grep -v "logger") -timeout 940.000s -coverprofile cover.out
2 changes: 1 addition & 1 deletion internal/commands/groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func getGroupIds(groups []*wrappers.Group) []string {
return groupIds
}

func assignGroupsToProject(projectID string, projectName string, groups []*wrappers.Group,
func assignGroupsToProjectNewAccessManagement(projectID string, projectName string, groups []*wrappers.Group,
accessManagement wrappers.AccessManagementWrapper) error {
if !wrappers.FeatureFlags[accessManagementEnabled] {
return nil
Expand Down
26 changes: 13 additions & 13 deletions internal/commands/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,18 @@ import (
)

const (
failedCreatingProj = "Failed creating a project"
failedUpdatingProj = "Failed updating a project"

failedGettingProj = "Failed getting a project"
failedDeletingProj = "Failed deleting a project"
failedGettingBranches = "Failed getting branches for project"
failedFindingGroup = "Failed finding groups"
projOriginLevel = "Project"
repoConfKey = "scan.handler.git.repository"
sshConfKey = "scan.handler.git.sshKey"
mandatoryRepoURLError = "flag --repo-url is mandatory when --ssh-key is provided"
invalidRepoURL = "provided repository url doesn't need a key. Make sure you are defining the right repository or remove the flag --ssh-key"
failedCreatingProj = "Failed creating a project"
failedUpdatingProj = "Failed updating a project"
failedProjectApplicationAssociation = "Failed association project to application"
failedGettingProj = "Failed getting a project"
failedDeletingProj = "Failed deleting a project"
failedGettingBranches = "Failed getting branches for project"
failedFindingGroup = "Failed finding groups"
projOriginLevel = "Project"
repoConfKey = "scan.handler.git.repository"
sshConfKey = "scan.handler.git.sshKey"
mandatoryRepoURLError = "flag --repo-url is mandatory when --ssh-key is provided"
invalidRepoURL = "provided repository url doesn't need a key. Make sure you are defining the right repository or remove the flag --ssh-key"
)

var (
Expand Down Expand Up @@ -291,7 +291,7 @@ func runCreateProjectCommand(
return errors.Wrapf(err, "%s", failedCreatingProj)
}
}
err = assignGroupsToProject(projResponseModel.ID, projResponseModel.Name, groups, accessManagementWrapper)
err = assignGroupsToProjectNewAccessManagement(projResponseModel.ID, projResponseModel.Name, groups, accessManagementWrapper)
if err != nil {
return err
}
Expand Down
138 changes: 137 additions & 1 deletion internal/commands/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/checkmarx/ast-cli/internal/commands/policymanagement"
"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"
"github.com/checkmarx/ast-cli/internal/logger"
"golang.org/x/text/cases"
"golang.org/x/text/language"
Expand Down Expand Up @@ -163,12 +164,32 @@ func NewResultsCommand(
showResultCmd := resultShowSubCommand(resultsWrapper, scanWrapper, resultsSbomWrapper, resultsPdfReportsWrapper, risksOverviewWrapper, policyWrapper)
codeBashingCmd := resultCodeBashing(codeBashingWrapper)
bflResultCmd := resultBflSubCommand(bflWrapper)
exitCodeSubcommand := exitCodeSubCommand(scanWrapper)
resultCmd.AddCommand(
showResultCmd, bflResultCmd, codeBashingCmd,
showResultCmd, bflResultCmd, codeBashingCmd, exitCodeSubcommand,
)
return resultCmd
}

func exitCodeSubCommand(scanWrapper wrappers.ScansWrapper) *cobra.Command {
exitCodeCmd := &cobra.Command{
Use: "exit-code",
Short: "Get exit code and details of a scan",
Long: "The exit-code command enables you to get the exit code and failure details of a requested scan in Checkmarx One.",
Example: heredoc.Doc(
`
$ cx results exit-code --scan-id <scan Id> --scan-types <sast | sca | iac-security | apisec>
`,
),
RunE: runGetExitCodeCommand(scanWrapper),
}

exitCodeCmd.PersistentFlags().String(commonParams.ScanIDFlag, "", "Scan ID")
exitCodeCmd.PersistentFlags().String(commonParams.ScanTypes, "", "Scan types")

return exitCodeCmd
}

func resultShowSubCommand(
resultsWrapper wrappers.ResultsWrapper,
scanWrapper wrappers.ScansWrapper,
Expand Down Expand Up @@ -252,6 +273,110 @@ func resultBflSubCommand(bflWrapper wrappers.BflWrapper) *cobra.Command {
return resultBflCmd
}

func runGetExitCodeCommand(scanWrapper wrappers.ScansWrapper) func(cmd *cobra.Command, args []string) error {
return func(cmd *cobra.Command, args []string) error {
scanID, _ := cmd.Flags().GetString(commonParams.ScanIDFlag)
if scanID == "" {
return errors.New(applicationErrors.ScanIDRequired)
}
scanTypesFlagValue, _ := cmd.Flags().GetString(commonParams.ScanTypes)
results, err := GetScannerResults(scanWrapper, scanID, scanTypesFlagValue)
if err != nil {
return err
}

if len(results) == 0 {
return nil
}

return printer.Print(cmd.OutOrStdout(), results, printer.FormatIndentedJSON)
}
}

func GetScannerResults(scanWrapper wrappers.ScansWrapper, scanID, scanTypesFlagValue string) ([]ScannerResponse, error) {
scanResponseModel, errorModel, err := scanWrapper.GetByID(scanID)
if err != nil {
return nil, errors.Wrapf(err, "%s", failedGetting)
}
if errorModel != nil {
return nil, errors.Errorf("%s: CODE: %d, %s", failedGettingScan, errorModel.Code, errorModel.Message)
}
results := getScannerResponse(scanTypesFlagValue, scanResponseModel)
return results, nil
}

func getScannerResponse(scanTypesFlagValue string, scanResponseModel *wrappers.ScanResponseModel) []ScannerResponse {
var results []ScannerResponse

if scanResponseModel.Status == wrappers.ScanCanceled ||
scanResponseModel.Status == wrappers.ScanRunning ||
scanResponseModel.Status == wrappers.ScanQueued ||
scanResponseModel.Status == wrappers.ScanCompleted {
result := ScannerResponse{
ScanID: scanResponseModel.ID,
Status: string(scanResponseModel.Status),
}
results = append(results, result)
return results
}

if scanTypesFlagValue == "" {
results = createAllFailedScannersResponse(scanResponseModel)
} else {
scanTypes := sanitizeScannerNames(scanTypesFlagValue)
results = createRequestedScannersResponse(scanTypes, scanResponseModel)
}

return results
}

func createRequestedScannersResponse(scanTypes map[string]string, scanResponseModel *wrappers.ScanResponseModel) []ScannerResponse {
var results []ScannerResponse
for i := range scanResponseModel.StatusDetails {
if _, ok := scanTypes[scanResponseModel.StatusDetails[i].Name]; ok {
results = append(results, createScannerResponse(&scanResponseModel.StatusDetails[i]))
}
}
return results
}

func createAllFailedScannersResponse(scanResponseModel *wrappers.ScanResponseModel) []ScannerResponse {
var results []ScannerResponse
for i := range scanResponseModel.StatusDetails {
if scanResponseModel.StatusDetails[i].Status == wrappers.ScanFailed {
results = append(results, createScannerResponse(&scanResponseModel.StatusDetails[i]))
}
}
return results
}

func sanitizeScannerNames(scanTypes string) map[string]string {
scanTypeSlice := strings.Split(scanTypes, ",")
scanTypeMap := make(map[string]string)
for i := range scanTypeSlice {
lowered := strings.ToLower(scanTypeSlice[i])
scanTypeMap[lowered] = lowered
}

return scanTypeMap
}

func createScannerResponse(statusDetails *wrappers.StatusInfo) ScannerResponse {
return ScannerResponse{
Name: statusDetails.Name,
Status: statusDetails.Status,
Details: statusDetails.Details,
ErrorCode: stringifyErrorCode(statusDetails.ErrorCode),
}
}

func stringifyErrorCode(errorCode int) string {
if errorCode == 0 {
return ""
}
return strconv.Itoa(errorCode)
}

func runGetBestFixLocationCommand(bflWrapper wrappers.BflWrapper) func(cmd *cobra.Command, args []string) error {
return func(cmd *cobra.Command, args []string) error {
var bflResponseModel *wrappers.BFLResponseModel
Expand Down Expand Up @@ -901,6 +1026,9 @@ func createReport(format,
resultsPdfReportsWrapper wrappers.ResultsPdfWrapper,
useSCALocalFlow bool,
retrySBOM int) error {
if printer.IsFormat(format, printer.FormatIndentedJSON) {
return nil
}
if printer.IsFormat(format, printer.FormatSarif) && isValidScanStatus(summary.Status, printer.FormatSarif) {
sarifRpt := createTargetName(targetFile, targetPath, printer.FormatSarif)
return exportSarifResults(sarifRpt, results)
Expand Down Expand Up @@ -1862,3 +1990,11 @@ func filterViolatedRules(policyModel wrappers.PolicyResponseModel) *wrappers.Pol
policyModel.Policies = policyModel.Policies[:i]
return &policyModel
}

type ScannerResponse struct {
ScanID string `json:"ScanID,omitempty"`
Name string `json:"Name,omitempty"`
Status string `json:"Status,omitempty"`
Details string `json:"Details,omitempty"`
ErrorCode string `json:"ErrorCode,omitempty"`
}
Loading
Loading