Skip to content

Engine api hands on exercise-anjali (AST-92867) #1112

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

Open
wants to merge 34 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
32e64a8
add development and test dependency flags to SCA results
cx-elchanan-arbiv Apr 3, 2025
53d7512
Delete .pre-commit-config.yaml
cx-elchanan-arbiv Apr 3, 2025
59e3af0
Add scan id flag to risk management command (AST-000)
cx-daniel-greenspan Apr 17, 2025
b879271
Add scan id flag to risk management command (AST-000)
cx-daniel-greenspan Apr 17, 2025
3a84f68
Add scan id flag to risk management command (AST-000)
cx-daniel-greenspan Apr 17, 2025
745189c
Engine Api task implementation
cx-anjali-deore Apr 14, 2025
d3d30e5
reframed the API description
cx-anjali-deore Apr 14, 2025
f41399f
rebase from master
cx-anjali-deore Apr 23, 2025
a222dba
formatted the code as per lint
cx-anjali-deore Apr 14, 2025
bb63659
added enginewrapper in util command
cx-anjali-deore Apr 14, 2025
c55295c
added enginePath
cx-anjali-deore Apr 14, 2025
0b33c3c
added newHTTPEngineWrapper in integration test
cx-anjali-deore Apr 14, 2025
aabad43
Add scan id flag to risk management command (AST-000)
cx-daniel-greenspan Apr 16, 2025
fb694d0
Added comments
cx-anjali-deore Apr 23, 2025
1e6f2f0
checking
cx-anjali-deore Apr 23, 2025
5d08e15
rebase from master
cx-anjali-deore Apr 23, 2025
0a8c6f7
rebase from master
cx-anjali-deore Apr 23, 2025
ae9fe14
Add support on custom config file path
tiagobcx Apr 8, 2025
2cf0af9
Add scan id flag to risk management command (AST-000)
cx-daniel-greenspan Apr 17, 2025
b1af3f7
Add scan id flag to risk management command (AST-000)
cx-daniel-greenspan Apr 17, 2025
7e2b825
Add scan id flag to risk management command (AST-000)
cx-daniel-greenspan Apr 17, 2025
9d8f912
Added comments
cx-anjali-deore Apr 23, 2025
a18b7d8
checking
cx-anjali-deore Apr 23, 2025
815aaf8
Add scan id flag to risk management command (AST-000)
cx-daniel-greenspan Apr 17, 2025
5f4c6e5
Add scan id flag to risk management command (AST-000)
cx-daniel-greenspan Apr 17, 2025
060051c
Add scan id flag to risk management command (AST-000)
cx-daniel-greenspan Apr 17, 2025
99d0c40
Added comments
cx-anjali-deore Apr 23, 2025
c1579be
checking
cx-anjali-deore Apr 23, 2025
754a1f4
fixed syntax issues
cx-anjali-deore Apr 23, 2025
791ac76
added comments
cx-anjali-deore Apr 23, 2025
c6c2da3
added comments
cx-anjali-deore Apr 23, 2025
e5fbd15
pull from main
cx-anjali-deore Apr 29, 2025
44f1963
added chmod
cx-anjali-deore Apr 29, 2025
560776b
removed chmod
cx-anjali-deore Apr 29, 2025
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
3 changes: 3 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
repos:
- repo: local
hooks: []
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ USER nonroot

COPY cx /app/bin/cx


ENTRYPOINT ["/app/bin/cx"]

HEALTHCHECK NONE
4 changes: 3 additions & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func main() {
sastMetadataPath := viper.GetString(params.SastMetadataPathKey)
accessManagementPath := viper.GetString(params.AccessManagementPathKey)
byorPath := viper.GetString(params.ByorPathKey)

enginePath := viper.GetString(params.EnginePathKey)
customStatesWrapper := wrappers.NewCustomStatesHTTPWrapper()
scansWrapper := wrappers.NewHTTPScansWrapper(scans)
resultsPdfReportsWrapper := wrappers.NewResultsPdfReportsHTTPWrapper(resultsPdfPath)
Expand Down Expand Up @@ -91,6 +91,7 @@ func main() {
accessManagementWrapper := wrappers.NewAccessManagementHTTPWrapper(accessManagementPath)
byorWrapper := wrappers.NewByorHTTPWrapper(byorPath)
containerResolverWrapper := wrappers.NewContainerResolverWrapper()
engineWrapper := wrappers.NewHTTPEngineWrapper(enginePath)

astCli := commands.NewAstCLI(
applicationsWrapper,
Expand Down Expand Up @@ -127,6 +128,7 @@ func main() {
accessManagementWrapper,
byorWrapper,
containerResolverWrapper,
engineWrapper,
)
exitListener()
err = astCli.Execute()
Expand Down
94 changes: 94 additions & 0 deletions internal/commands/engine.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package commands

import (
"github.com/MakeNowJust/heredoc"
commonParams "github.com/checkmarx/ast-cli/internal/params"
"github.com/checkmarx/ast-cli/internal/services"
"github.com/checkmarx/ast-cli/internal/wrappers"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)

type EngineView struct {
EngineID string `format:"name:Engine ID"`
EngineName string `format:"name:Engine Name"`
ApiName string `format:"name:Api Name"`
ApiURL string `format:"name:Api URL"`
Description string `format:"name:Description"`
}

// engine command implementation
func EngineCommand(engineWrapper wrappers.EngineWrapper) *cobra.Command {
EngineCmd := &cobra.Command{
Use: "engine",
Short: "manages the engine ",
Long: "The scan command enables the ability to manage engine in Checkmarx One.",
}
listScanCmd := listEngineSubCmd(engineWrapper)
EngineCmd.AddCommand(listScanCmd)
return EngineCmd
}

// list Api of engines implementation
func listEngineSubCmd(engineWrapper wrappers.EngineWrapper) *cobra.Command {
enginelistCmd := &cobra.Command{
Use: "list-api",
Short: "lists all APIs of CXOne engines",
Long: "The list command provides a list of all APIs of the engines in Checkmarx One.",
Example: heredoc.Doc(
`$cx engine list-api
`,
),
RunE: runListEnginesCommand(engineWrapper),
}
enginelistCmd.PersistentFlags().String(commonParams.EngineName, "", "Name of the engine")
enginelistCmd.PersistentFlags().String(commonParams.EngineOutputFormat, "table", "Name of format")
return enginelistCmd
}

// This will call the get method to list the APIs of the engine/engines

func runListEnginesCommand(engineWrapper wrappers.EngineWrapper) func(cmd *cobra.Command, args []string) error {
return func(cmd *cobra.Command, args []string) error {
var allEngineModel *wrappers.EnginesCollectionResponseModel
var errorModel *wrappers.ErrorModel
engineName, _ := cmd.Flags().GetString(commonParams.EngineName)
var err error
allEngineModel, err = engineWrapper.Get(engineName)
if err != nil {
return errors.Wrapf(err, "%s", "failed getting")
}
if errorModel != nil {
return errors.Errorf(services.ErrorCodeFormat, failedGettingAll, errorModel.Code, errorModel.Message)
} else if allEngineModel != nil {
views, err := toEngineView(allEngineModel.Engines)
if err != nil {
return err
}
err = printByOutputFormat(cmd, views)
if err != nil {
return err
}
}
return nil
}
}

// Converting the values of EngineResponseModel (returned from API) proper view which can be presented by format
func toEngineView(engines []wrappers.EngineResponseModel) ([]*EngineView, error) {

views := make([]*EngineView, 0)
for i := 0; i < len(engines); i++ {
for j := 0; j < len(engines[i].Apis); j++ {
views = append(views, &EngineView{
EngineID: engines[i].EngineID,
EngineName: engines[i].EngineName,
ApiName: engines[i].Apis[j].ApiName,
ApiURL: engines[i].Apis[j].ApiURL,
Description: engines[i].Apis[j].Description,
})
}
}

return views, nil
}
37 changes: 37 additions & 0 deletions internal/commands/engine_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//go:build !integration

package commands

import (
"gotest.tools/assert"
"testing"
)

const (
unknownEngineFlag = "unknown flag: --chibutero"
)

func TestEngineNoSub(t *testing.T) {
execCmdNilAssertion(t, "engine")
}

func TestGetAllEnginesAPI(t *testing.T) {
execCmdNilAssertion(t, "engine", "list-api")
}

func TestRunGetAllEngineAPIFlagNonExist(t *testing.T) {
err := execCmdNotNilAssertion(t, "engine", "list-api", "--chibutero")
assert.Assert(t, err.Error() == unknownEngineFlag)
}

func TestGetSASTEnginesAPI(t *testing.T) {
execCmdNilAssertion(t, "engine", "list-api", "--engine-name", "sast")
}

func TestGetSCAEngineAPI(t *testing.T) {
execCmdNilAssertion(t, "engine", "list-api", "--engine-name", "sca")
}

func TestGetDASTEngineAPI(t *testing.T) {
execCmdNilAssertion(t, "engine", "list-api", "--engine-name", "dast")
}
12 changes: 11 additions & 1 deletion internal/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ func NewAstCLI(
accessManagementWrapper wrappers.AccessManagementWrapper,
byorWrapper wrappers.ByorWrapper,
containerResolverWrapper wrappers.ContainerResolverWrapper,
engineWrapper wrappers.EngineWrapper,
) *cobra.Command {
// Create the root
rootCmd := &cobra.Command{
Expand Down Expand Up @@ -173,7 +174,7 @@ func NewAstCLI(
policyWrapper,
featureFlagsWrapper,
)

enginesCmd := EngineCommand(engineWrapper)
versionCmd := util.NewVersionCommand()
authCmd := NewAuthCommand(authWrapper)
utilsCmd := util.NewUtilsCommand(
Expand Down Expand Up @@ -209,6 +210,7 @@ func NewAstCLI(
resultsCmd,
triageCmd,
versionCmd,
enginesCmd,
authCmd,
utilsCmd,
configCmd,
Expand Down Expand Up @@ -319,7 +321,15 @@ func printByFormat(cmd *cobra.Command, view interface{}) error {
f, _ := cmd.Flags().GetString(params.FormatFlag)
return printer.Print(cmd.OutOrStdout(), view, f)
}

func printByScanInfoFormat(cmd *cobra.Command, view interface{}) error {
f, _ := cmd.Flags().GetString(params.ScanInfoFormatFlag)
return printer.Print(cmd.OutOrStdout(), view, f)
}

// print by --output-format flag

func printByOutputFormat(cmd *cobra.Command, view interface{}) error {
f, _ := cmd.Flags().GetString(params.EngineOutputFormat)
return printer.Print(cmd.OutOrStdout(), view, f)
}
3 changes: 3 additions & 0 deletions internal/commands/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ func createASTTestCommand() *cobra.Command {
byorWrapper := &mock.ByorMockWrapper{}
containerResolverMockWrapper := &mock.ContainerResolverMockWrapper{}
customStatesMockWrapper := &mock.CustomStatesMockWrapper{}
// Using the same wrapper as it as mocked response
engineMockWrapper := &wrappers.EngineHTTPWrapper{}
return NewAstCLI(
applicationWrapper,
scansMockWrapper,
Expand Down Expand Up @@ -103,6 +105,7 @@ func createASTTestCommand() *cobra.Command {
accessManagementWrapper,
byorWrapper,
containerResolverMockWrapper,
engineMockWrapper,
)
}

Expand Down
3 changes: 3 additions & 0 deletions internal/constants/errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ const (
FailedUploadFileMsgWithDomain = "Unable to upload the file to the pre-signed URL. Try adding the domain: %s to your allow list."
FailedUploadFileMsgWithURL = "Unable to upload the file to the pre-signed URL. Try adding the URL: %s to your allow list."

// engine
EngineDoesNotExist = "Engine does not exits or user has no permission to the engine "

// asca Engine
FileExtensionIsRequired = "file must have an extension"
)
3 changes: 2 additions & 1 deletion internal/params/binds.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ var EnvVarsBinds = []struct {
{AiProxyCheckmarxAiRouteKey, AiProxyCheckmarxAiRouteEnv, "api/ai-proxy/redirect/azure"},
{ASCAPortKey, ASCAPortEnv, ""},
{ScsRepoTokenKey, ScsRepoTokenEnv, ""},
{RiskManagementPathKey, RiskManagementPathEnv, "api/risk-management/projects/%s/results?scanID=%s"},
{RiskManagementPathKey, RiskManagementPathEnv, "api/risk-management/projects/%s/results"},
{EnginePathKey, EnginePathEnv, "api/engines"},
{ConfigFilePathKey, ConfigFilePathEnv, ""},
}
1 change: 1 addition & 0 deletions internal/params/envs.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,6 @@ const (
ASCAPortEnv = "CX_ASCA_PORT"
ScsRepoTokenEnv = "SCS_REPO_TOKEN"
RiskManagementPathEnv = "CX_RISK_MANAGEMENT_PATH"
EnginePathEnv = "CX_ENGINE_PATH"
ConfigFilePathEnv = "CX_CONFIG_FILE_PATH"
)
4 changes: 4 additions & 0 deletions internal/params/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,10 @@ const (
ContainersImageTagFilterFlag = "containers-image-tag-filter"
ContainersPackageFilterFlag = "containers-package-filter"
ContainersExcludeNonFinalStagesFlag = "containers-exclude-non-final-stages"

//Engines
EngineName = "engine-name"
EngineOutputFormat = "output-format"
)

// Parameter values
Expand Down
1 change: 1 addition & 0 deletions internal/params/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,5 @@ var (
ScsRepoTokenKey = strings.ToLower(ScsRepoTokenEnv)
RiskManagementPathKey = strings.ToLower(RiskManagementPathEnv)
ConfigFilePathKey = strings.ToLower(ConfigFilePathEnv)
EnginePathKey = strings.ToLower(ResultsPathKey)
)
Loading
Loading