Skip to content

Commit 60d543b

Browse files
Engine Api task implementation
1 parent 4ffb9f0 commit 60d543b

File tree

12 files changed

+337
-2
lines changed

12 files changed

+337
-2
lines changed

cmd/main.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func main() {
5555
sastMetadataPath := viper.GetString(params.SastMetadataPathKey)
5656
accessManagementPath := viper.GetString(params.AccessManagementPathKey)
5757
byorPath := viper.GetString(params.ByorPathKey)
58-
58+
enginePath := viper.GetString(params.EnginePathKey)
5959
customStatesWrapper := wrappers.NewCustomStatesHTTPWrapper()
6060
scansWrapper := wrappers.NewHTTPScansWrapper(scans)
6161
resultsPdfReportsWrapper := wrappers.NewResultsPdfReportsHTTPWrapper(resultsPdfPath)
@@ -90,6 +90,7 @@ func main() {
9090
accessManagementWrapper := wrappers.NewAccessManagementHTTPWrapper(accessManagementPath)
9191
byorWrapper := wrappers.NewByorHTTPWrapper(byorPath)
9292
containerResolverWrapper := wrappers.NewContainerResolverWrapper()
93+
engineWrapper := wrappers.NewHTTPEngineWrapper(enginePath)
9394

9495
astCli := commands.NewAstCLI(
9596
applicationsWrapper,
@@ -126,6 +127,7 @@ func main() {
126127
accessManagementWrapper,
127128
byorWrapper,
128129
containerResolverWrapper,
130+
engineWrapper,
129131
)
130132
exitListener()
131133
err = astCli.Execute()

internal/commands/engine.go

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package commands
2+
3+
import (
4+
"github.com/MakeNowJust/heredoc"
5+
commonParams "github.com/checkmarx/ast-cli/internal/params"
6+
"github.com/checkmarx/ast-cli/internal/services"
7+
"github.com/checkmarx/ast-cli/internal/wrappers"
8+
"github.com/pkg/errors"
9+
"github.com/spf13/cobra"
10+
)
11+
12+
type EngineView struct {
13+
EngineID string `format:"name:Engine ID"`
14+
EngineName string `format:"name:Engine Name"`
15+
ApiName string `format:"name:Api Name"`
16+
ApiURL string `format:"name:Api URL"`
17+
Description string `format:"name:Description"`
18+
}
19+
20+
// engine command implementation
21+
func EngineCommand(engineWrapper wrappers.EngineWrapper) *cobra.Command {
22+
EngineCmd := &cobra.Command{
23+
Use: "engine",
24+
Short: "manages the engine ",
25+
Long: "The scan command enables the ability to manage engine in Checkmarx One.",
26+
}
27+
listScanCmd := listEngineSubCmd(engineWrapper)
28+
EngineCmd.AddCommand(listScanCmd)
29+
return EngineCmd
30+
}
31+
32+
// list Api of engines implementation
33+
func listEngineSubCmd(engineWrapper wrappers.EngineWrapper) *cobra.Command {
34+
enginelistCmd := &cobra.Command{
35+
Use: "list-api",
36+
Short: "lists all APIs of CXOne engines",
37+
Long: "The list command provides a list of all APIs of the engines in Checkmarx One.",
38+
Example: heredoc.Doc(
39+
`$cx engine list-api
40+
`,
41+
),
42+
RunE: runListEnginesCommand(engineWrapper),
43+
}
44+
enginelistCmd.PersistentFlags().String(commonParams.EngineName, "", "Name of the engine")
45+
enginelistCmd.PersistentFlags().String(commonParams.EngineOutputFormat, "table", "Name of format")
46+
return enginelistCmd
47+
}
48+
49+
// This will call the get method to list the APIs of the engine/engines
50+
51+
func runListEnginesCommand(engineWrapper wrappers.EngineWrapper) func(cmd *cobra.Command, args []string) error {
52+
return func(cmd *cobra.Command, args []string) error {
53+
var allEngineModel *wrappers.EnginesCollectionResponseModel
54+
var errorModel *wrappers.ErrorModel
55+
engineName, _ := cmd.Flags().GetString(commonParams.EngineName)
56+
var err error
57+
allEngineModel, err = engineWrapper.Get(engineName)
58+
if err != nil {
59+
return errors.Wrapf(err, "%s", "failed getting")
60+
}
61+
if errorModel != nil {
62+
return errors.Errorf(services.ErrorCodeFormat, failedGettingAll, errorModel.Code, errorModel.Message)
63+
} else if allEngineModel != nil {
64+
views, err := toEngineView(allEngineModel.Engines)
65+
if err != nil {
66+
return err
67+
}
68+
err = printByOutputFormat(cmd, views)
69+
if err != nil {
70+
return err
71+
}
72+
}
73+
return nil
74+
}
75+
}
76+
77+
// Converting the values of EngineResponseModel (returned from API) proper view which can be presented by format
78+
func toEngineView(engines []wrappers.EngineResponseModel) ([]*EngineView, error) {
79+
80+
views := make([]*EngineView, 0)
81+
for i := 0; i < len(engines); i++ {
82+
for j := 0; j < len(engines[i].Apis); j++ {
83+
views = append(views, &EngineView{
84+
EngineID: engines[i].EngineID,
85+
EngineName: engines[i].EngineName,
86+
ApiName: engines[i].Apis[j].ApiName,
87+
ApiURL: engines[i].Apis[j].ApiURL,
88+
Description: engines[i].Apis[j].Description,
89+
})
90+
}
91+
}
92+
93+
return views, nil
94+
}

internal/commands/engine_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//go:build !integration
2+
3+
package commands
4+
5+
import (
6+
"gotest.tools/assert"
7+
"testing"
8+
)
9+
10+
const (
11+
unknownEngineFlag = "unknown flag: --chibutero"
12+
)
13+
14+
func TestEngineNoSub(t *testing.T) {
15+
execCmdNilAssertion(t, "engine")
16+
}
17+
18+
func TestGetAllEnginesAPI(t *testing.T) {
19+
execCmdNilAssertion(t, "engine", "list-api")
20+
}
21+
22+
func TestRunGetAllEngineAPIFlagNonExist(t *testing.T) {
23+
err := execCmdNotNilAssertion(t, "engine", "list-api", "--chibutero")
24+
assert.Assert(t, err.Error() == unknownEngineFlag)
25+
}
26+
27+
func TestGetSASTEnginesAPI(t *testing.T) {
28+
execCmdNilAssertion(t, "engine", "list-api", "--engine-name", "sast")
29+
}
30+
31+
func TestGetSCAEngineAPI(t *testing.T) {
32+
execCmdNilAssertion(t, "engine", "list-api", "--engine-name", "sca")
33+
}
34+
35+
func TestGetDASTEngineAPI(t *testing.T) {
36+
execCmdNilAssertion(t, "engine", "list-api", "--engine-name", "dast")
37+
}

internal/commands/root.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ func NewAstCLI(
5555
accessManagementWrapper wrappers.AccessManagementWrapper,
5656
byorWrapper wrappers.ByorWrapper,
5757
containerResolverWrapper wrappers.ContainerResolverWrapper,
58+
engineWrapper wrappers.EngineWrapper,
5859
) *cobra.Command {
5960
// Create the root
6061
rootCmd := &cobra.Command{
@@ -173,7 +174,7 @@ func NewAstCLI(
173174
policyWrapper,
174175
featureFlagsWrapper,
175176
)
176-
177+
enginesCmd := EngineCommand(engineWrapper)
177178
versionCmd := util.NewVersionCommand()
178179
authCmd := NewAuthCommand(authWrapper)
179180
utilsCmd := util.NewUtilsCommand(
@@ -209,6 +210,7 @@ func NewAstCLI(
209210
resultsCmd,
210211
triageCmd,
211212
versionCmd,
213+
enginesCmd,
212214
authCmd,
213215
utilsCmd,
214216
configCmd,
@@ -319,7 +321,15 @@ func printByFormat(cmd *cobra.Command, view interface{}) error {
319321
f, _ := cmd.Flags().GetString(params.FormatFlag)
320322
return printer.Print(cmd.OutOrStdout(), view, f)
321323
}
324+
322325
func printByScanInfoFormat(cmd *cobra.Command, view interface{}) error {
323326
f, _ := cmd.Flags().GetString(params.ScanInfoFormatFlag)
324327
return printer.Print(cmd.OutOrStdout(), view, f)
325328
}
329+
330+
// print by --output-format flag
331+
332+
func printByOutputFormat(cmd *cobra.Command, view interface{}) error {
333+
f, _ := cmd.Flags().GetString(params.EngineOutputFormat)
334+
return printer.Print(cmd.OutOrStdout(), view, f)
335+
}

internal/commands/root_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ func createASTTestCommand() *cobra.Command {
6868
byorWrapper := &mock.ByorMockWrapper{}
6969
containerResolverMockWrapper := &mock.ContainerResolverMockWrapper{}
7070
customStatesMockWrapper := &mock.CustomStatesMockWrapper{}
71+
// Using the same wrapper as it as mocked response
72+
engineMockWrapper := &wrappers.EngineHTTPWrapper{}
7173
return NewAstCLI(
7274
applicationWrapper,
7375
scansMockWrapper,
@@ -103,6 +105,7 @@ func createASTTestCommand() *cobra.Command {
103105
accessManagementWrapper,
104106
byorWrapper,
105107
containerResolverMockWrapper,
108+
engineMockWrapper,
106109
)
107110
}
108111

internal/constants/errors/errors.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ const (
2323
FailedUploadFileMsgWithDomain = "Unable to upload the file to the pre-signed URL. Try adding the domain: %s to your allow list."
2424
FailedUploadFileMsgWithURL = "Unable to upload the file to the pre-signed URL. Try adding the URL: %s to your allow list."
2525

26+
//engine
27+
EngineDoesNotExist = "Engine does not exits or user has no permission to the engine "
28+
2629
// asca Engine
2730
FileExtensionIsRequired = "file must have an extension"
2831
)

internal/params/binds.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,5 @@ var EnvVarsBinds = []struct {
7474
{ASCAPortKey, ASCAPortEnv, ""},
7575
{ScsRepoTokenKey, ScsRepoTokenEnv, ""},
7676
{RiskManagementPathKey, RiskManagementPathEnv, "api/risk-management/projects/%s/results"},
77+
{EnginePathKey, EnginePathEnv, "api/engines"},
7778
}

internal/params/envs.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,5 @@ const (
7373
ASCAPortEnv = "CX_ASCA_PORT"
7474
ScsRepoTokenEnv = "SCS_REPO_TOKEN"
7575
RiskManagementPathEnv = "CX_RISK_MANAGEMENT_PATH"
76+
EnginePathEnv = "CX_ENGINE_PATH"
7677
)

internal/params/flags.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,10 @@ const (
217217
ContainersImageTagFilterFlag = "containers-image-tag-filter"
218218
ContainersPackageFilterFlag = "containers-package-filter"
219219
ContainersExcludeNonFinalStagesFlag = "containers-exclude-non-final-stages"
220+
221+
//Engines
222+
EngineName = "engine-name"
223+
EngineOutputFormat = "output-format"
220224
)
221225

222226
// Parameter values

internal/params/keys.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,5 @@ var (
7373
ASCAPortKey = strings.ToLower(ASCAPortEnv)
7474
ScsRepoTokenKey = strings.ToLower(ScsRepoTokenEnv)
7575
RiskManagementPathKey = strings.ToLower(RiskManagementPathEnv)
76+
EnginePathKey = strings.ToLower(ResultsPathKey)
7677
)

0 commit comments

Comments
 (0)