Skip to content

Commit 8541016

Browse files
Merge pull request #1106 from Checkmarx/feature/add-asmp-traits
Add traits to ASPM results object (AST-000)
2 parents dc2c340 + 42748af commit 8541016

File tree

7 files changed

+59
-24
lines changed

7 files changed

+59
-24
lines changed

internal/commands/result.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -234,13 +234,14 @@ func riskManagementSubCommand(riskManagement wrappers.RiskManagementWrapper, fea
234234
Long: "The risk-management command displays risk management results for a specific project in Checkmarx One",
235235
Example: heredoc.Doc(
236236
`
237-
$ cx results risk-management --project-id <project Id> --limit <limit> (1-50, default: 50)
237+
$ cx results risk-management --project-id <project Id> --scan-id <scan ID> --limit <limit> (1-50, default: 50)
238238
`,
239239
),
240240
RunE: runRiskManagementCommand(riskManagement, featureFlagsWrapper),
241241
}
242242

243243
riskManagementCmd.PersistentFlags().String(commonParams.ProjectIDFlag, "", "Project ID")
244+
riskManagementCmd.PersistentFlags().String(commonParams.ScanIDFlag, "", "Scan ID")
244245
riskManagementCmd.PersistentFlags().Int(commonParams.LimitFlag, -1, "Limit")
245246

246247
addFormatFlag(riskManagementCmd, printer.FormatJSON, printer.FormatTable, printer.FormatList)
@@ -355,14 +356,16 @@ func runRiskManagementCommand(riskManagement wrappers.RiskManagementWrapper, fea
355356
) func(cmd *cobra.Command, args []string) error {
356357
return func(cmd *cobra.Command, args []string) error {
357358
projectID, _ := cmd.Flags().GetString(commonParams.ProjectIDFlag)
359+
scanID, _ := cmd.Flags().GetString(commonParams.ScanIDFlag)
360+
358361
limit, _ := cmd.Flags().GetInt(commonParams.LimitFlag)
359362

360363
flagResponse, _ := wrappers.GetSpecificFeatureFlag(featureFlagsWrapper, wrappers.RiskManagementEnabled)
361364
ASPMEnabled := flagResponse.Status
362365
if !ASPMEnabled {
363366
return errors.Errorf("%s", "Risk management results are currently unavailable for your tenant.")
364367
}
365-
results, err := getRiskManagementResults(riskManagement, projectID)
368+
results, err := getRiskManagementResults(riskManagement, projectID, scanID)
366369
if err != nil {
367370
return err
368371
}
@@ -372,8 +375,8 @@ func runRiskManagementCommand(riskManagement wrappers.RiskManagementWrapper, fea
372375
}
373376
}
374377

375-
func getRiskManagementResults(riskManagement wrappers.RiskManagementWrapper, projectID string) (*wrappers.ASPMResult, error) {
376-
ASPMResult, errorModel, err := riskManagement.GetTopVulnerabilitiesByProjectID(projectID)
378+
func getRiskManagementResults(riskManagement wrappers.RiskManagementWrapper, projectID, scanID string) (*wrappers.ASPMResult, error) {
379+
ASPMResult, errorModel, err := riskManagement.GetTopVulnerabilitiesByProjectID(projectID, scanID)
377380
if err != nil {
378381
return nil, errors.Wrapf(err, "%s", failedListingResults)
379382
}

internal/params/binds.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,6 @@ var EnvVarsBinds = []struct {
7373
{AiProxyCheckmarxAiRouteKey, AiProxyCheckmarxAiRouteEnv, "api/ai-proxy/redirect/azure"},
7474
{ASCAPortKey, ASCAPortEnv, ""},
7575
{ScsRepoTokenKey, ScsRepoTokenEnv, ""},
76-
{RiskManagementPathKey, RiskManagementPathEnv, "api/risk-management/projects/%s/results"},
76+
{RiskManagementPathKey, RiskManagementPathEnv, "api/risk-management/projects/%s/results?scanID=%s"},
7777
{ConfigFilePathKey, ConfigFilePathEnv, ""},
7878
}

internal/wrappers/mock/risk-management-mock.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@ import (
66

77
type RiskManagementMockWrapper struct{}
88

9-
func (r *RiskManagementMockWrapper) GetTopVulnerabilitiesByProjectID(projectID string) (*wrappers.ASPMResult, *wrappers.WebError, error) {
9+
func (r *RiskManagementMockWrapper) GetTopVulnerabilitiesByProjectID(projectID string, scanID string) (*wrappers.ASPMResult, *wrappers.WebError, error) {
1010
mockResults := []wrappers.RiskManagementResult{
11-
{ID: "1", Name: "Vuln1", Severity: "High"},
12-
{ID: "2", Name: "Vuln2", Severity: "Medium"},
11+
{ID: "1", Name: "Vuln1", Severity: "High", Traits: map[string]string{wrappers.ExpPathKey: wrappers.ExpPathValue}},
12+
{ID: "2", Name: "Vuln2", Severity: "Medium", Traits: map[string]string{wrappers.SuspMalwareKey: wrappers.SuspMalwareValue}},
1313
}
1414

1515
mockASPMResult := &wrappers.ASPMResult{
1616
ProjectID: projectID,
17+
ScanID: scanID,
1718
Results: mockResults,
1819
}
1920

internal/wrappers/risk-management-http.go

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,33 @@ package wrappers
33
import (
44
"encoding/json"
55
"fmt"
6+
"github.com/checkmarx/ast-cli/internal/logger"
7+
"github.com/checkmarx/ast-cli/internal/wrappers/configuration"
68
"net/http"
79

810
commonParams "github.com/checkmarx/ast-cli/internal/params"
911
"github.com/pkg/errors"
1012
"github.com/spf13/viper"
1113
)
1214

15+
const riskManagementDefaultPath = "api/risk-management/projects/%s/results?scanID=%s"
16+
1317
type RiskManagementHTTPWrapper struct {
1418
path string
1519
}
1620

1721
func NewHTTPRiskManagementWrapper(path string) RiskManagementWrapper {
22+
validPath := setRMDefaultPath(path)
1823
return &RiskManagementHTTPWrapper{
19-
path: path,
24+
path: validPath,
2025
}
2126
}
2227

23-
func (r *RiskManagementHTTPWrapper) GetTopVulnerabilitiesByProjectID(projectID string) (
24-
*ASPMResult,
25-
*WebError,
26-
error,
27-
) {
28+
func (r *RiskManagementHTTPWrapper) GetTopVulnerabilitiesByProjectID(projectID string, scanID string) (*ASPMResult, *WebError, error) {
2829
clientTimeout := viper.GetUint(commonParams.ClientTimeoutKey)
29-
path := fmt.Sprintf(r.path, projectID)
30-
resp, err := SendHTTPRequest(http.MethodGet, path, http.NoBody, true, clientTimeout)
30+
31+
path := fmt.Sprintf(r.path, projectID, scanID)
32+
resp, err := SendHTTPRequest(http.MethodGet, path, nil, true, clientTimeout)
3133
if err != nil {
3234
return nil, nil, err
3335
}
@@ -58,3 +60,17 @@ func (r *RiskManagementHTTPWrapper) GetTopVulnerabilitiesByProjectID(projectID s
5860
return nil, nil, errors.Errorf("response status code %d", resp.StatusCode)
5961
}
6062
}
63+
64+
func setRMDefaultPath(path string) string {
65+
if path != riskManagementDefaultPath {
66+
configFilePath, err := configuration.GetConfigFilePath()
67+
if err != nil {
68+
logger.PrintfIfVerbose("Error getting config file path: %v", err)
69+
}
70+
err = configuration.SafeWriteSingleConfigKeyString(configFilePath, commonParams.RiskManagementPathKey, riskManagementDefaultPath)
71+
if err != nil {
72+
logger.PrintfIfVerbose("Error writing Risk Management path to config file: %v", err)
73+
}
74+
}
75+
return riskManagementDefaultPath
76+
}

internal/wrappers/risk-management.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,24 @@ package wrappers
22

33
import "time"
44

5+
const (
6+
SuspMalwareKey = "suspMalware"
7+
ExpPathKey = "explPath"
8+
//PubExposedKey = "pubExposed"
9+
//RuntimeKey = "runtime"
10+
11+
SuspMalwareValue = "Suspected Malware"
12+
ExpPathValue = "Exploitable Path"
13+
//PubExposedValue = "Public Exposed"
14+
//RuntimeValue = "Runtime"
15+
)
16+
517
type RiskManagementWrapper interface {
6-
GetTopVulnerabilitiesByProjectID(projectID string) (*ASPMResult, *WebError, error)
18+
GetTopVulnerabilitiesByProjectID(projectID string, scanID string) (*ASPMResult, *WebError, error)
19+
}
20+
21+
type GetASPMResultRequest struct {
22+
ScanId string `json:"scanID"`
723
}
824

925
type ApplicationScore struct {
@@ -27,6 +43,7 @@ type RiskManagementResult struct {
2743
Severity string `json:"severity"`
2844
RiskScore float64 `json:"riskScore"`
2945
EnrichmentSources map[string]string `json:"enrichmentSources"`
46+
Traits map[string]string `json:"traits"`
3047
CreatedAt time.Time `json:"createdAt"`
3148
ApplicationsScores []ApplicationScore `json:"applicationsScores"`
3249
}

test/integration/predicate_test.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ import (
1616
"gotest.tools/assert"
1717
)
1818

19-
var projectID string
20-
2119
func TestSastUpdateAndGetPredicatesForSimilarityId(t *testing.T) {
2220

2321
fmt.Println("Step 1: Testing the command 'triage update' to update an issue from the project.")
@@ -101,7 +99,6 @@ func TestSastUpdateAndGetPredicatesForSimilarityId(t *testing.T) {
10199
}
102100

103101
func TestGetAndUpdatePredicateWithInvalidScannerType(t *testing.T) {
104-
105102
err, _ := executeCommand(
106103
t, "triage", "update",
107104
flag(params.ProjectIDFlag), "1234",

test/integration/result_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -616,7 +616,7 @@ func readAndUnmarshalFile(t *testing.T, path string, v interface{}) {
616616
}
617617

618618
func TestRiskManagementResults_ReturnResults(t *testing.T) {
619-
projectName := "ast-phoenix-test-project"
619+
projectName := GenerateRandomProjectNameForScan()
620620
_ = executeCmdNilAssertion(
621621
t, "Create project should pass",
622622
"project", "create",
@@ -631,20 +631,21 @@ func TestRiskManagementResults_ReturnResults(t *testing.T) {
631631
flag(params.ScanInfoFormatFlag), printer.FormatJSON,
632632
flag(params.ScanTypes), params.SastType,
633633
}
634-
_, projectID := executeCreateScan(t, args)
634+
scanId, projectId := executeCreateScan(t, args)
635635

636636
defer func() {
637637
_ = executeCmdNilAssertion(
638638
t, "Delete project should pass",
639639
"project", "delete",
640-
flag(params.ProjectIDFlag), projectID,
640+
flag(params.ProjectIDFlag), projectId,
641641
)
642642
}()
643643

644644
outputBuffer := executeCmdNilAssertion(
645645
t, "Results risk-management generating JSON report should pass",
646646
"results", "risk-management",
647-
flag(params.ProjectIDFlag), projectID,
647+
flag(params.ProjectIDFlag), projectId,
648+
flag(params.ScanIDFlag), scanId,
648649
flag(params.LimitFlag), "20",
649650
)
650651
result := wrappers.ASPMResult{}

0 commit comments

Comments
 (0)