-
Notifications
You must be signed in to change notification settings - Fork 28
Add environments list API (AST-129883) #1415
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
base: main
Are you sure you want to change the base?
Changes from 32 commits
5d957d9
c717b9c
07fe229
d73c7bb
07c3e13
e1098db
b1d0c5f
f8e4c08
698a9f5
63882fa
d3f3c33
ee6af7d
7e638ff
d1058d2
c7cfcb4
8a4831c
4dc61d5
76ebb37
2c87f20
b66c948
926b095
d451fb3
323a78c
47e53b3
61fd3b2
8d431ea
e3b3e48
ebe7323
ca414a9
5b8d645
252b50c
d1c4598
7801fa6
2613c8f
1792976
d69f692
5427428
f338a55
2dff0d0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| package commandutils | ||
|
|
||
| import ( | ||
| "errors" | ||
| "fmt" | ||
| "strings" | ||
|
|
||
| "github.com/checkmarx/ast-cli/internal/commands/util/printer" | ||
| "github.com/checkmarx/ast-cli/internal/params" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| func AddFormatFlagToMultipleCommands(commands []*cobra.Command, defaultFormat string, otherAvailableFormats ...string) { | ||
| for _, c := range commands { | ||
| AddFormatFlag(c, defaultFormat, otherAvailableFormats...) | ||
| } | ||
| } | ||
|
|
||
| func AddFormatFlag(cmd *cobra.Command, defaultFormat string, otherAvailableFormats ...string) { | ||
| cmd.PersistentFlags().String( | ||
| params.FormatFlag, defaultFormat, | ||
| fmt.Sprintf(params.FormatFlagUsageFormat, append(otherAvailableFormats, defaultFormat)), | ||
| ) | ||
| } | ||
|
|
||
| func PrintByFormat(cmd *cobra.Command, view interface{}) error { | ||
| f, _ := cmd.Flags().GetString(params.FormatFlag) | ||
| return printer.Print(cmd.OutOrStdout(), view, f) | ||
| } | ||
|
|
||
| func GetFilters(cmd *cobra.Command) (map[string]string, error) { | ||
| filters, _ := cmd.Flags().GetStringSlice(params.FilterFlag) | ||
| allFilters := make(map[string]string) | ||
| for _, filter := range filters { | ||
| filterKeyVal := strings.Split(filter, "=") | ||
| if len(filterKeyVal) != params.KeyValuePairSize { | ||
| return nil, errors.New("invalid filters, filters should be in a KEY=VALUE format") | ||
| } | ||
| allFilters[filterKeyVal[0]] = strings.Replace( | ||
| filterKeyVal[1], ";", ",", | ||
| strings.Count(filterKeyVal[1], ";"), | ||
| ) | ||
| } | ||
| return allFilters, nil | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| package dast | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "strings" | ||
|
|
||
| "github.com/MakeNowJust/heredoc" | ||
| "github.com/checkmarx/ast-cli/internal/commands/commandutils" | ||
| "github.com/checkmarx/ast-cli/internal/commands/util/printer" | ||
| 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" | ||
| ) | ||
|
|
||
| const ( | ||
| failedGettingDastEnvironments = "Failed getting DAST environments" | ||
| ) | ||
|
|
||
| var ( | ||
| filterDastEnvironmentsListFlagUsage = fmt.Sprintf( | ||
| "Filter the list of DAST environments. Use ';' as the delimiter for arrays. Available filters are: %s", | ||
| strings.Join( | ||
| []string{ | ||
| commonParams.FromQueryParam, | ||
| commonParams.ToQueryParam, | ||
| commonParams.SearchQueryParam, | ||
| commonParams.SortQueryParam, | ||
| }, ",", | ||
| ), | ||
| ) | ||
| ) | ||
|
|
||
| // NewDastEnvironmentsCommand creates the DAST environments command | ||
| func NewDastEnvironmentsCommand(dastEnvironmentsWrapper wrappers.DastEnvironmentsWrapper) *cobra.Command { | ||
| environmentsCmd := &cobra.Command{ | ||
| Use: "dast-environments", | ||
| Short: "Manage DAST environments", | ||
| Long: "The environments command enables the ability to manage DAST environments in Checkmarx One", | ||
| Annotations: map[string]string{ | ||
| "command:doc": heredoc.Doc( | ||
| ` | ||
| https://checkmarx.com/resource/documents/en/todo | ||
| `, | ||
| ), | ||
| }, | ||
| } | ||
|
|
||
| listDastEnvironmentsCmd := &cobra.Command{ | ||
| Use: "list", | ||
| Short: "List all DAST environments in the system", | ||
| Example: heredoc.Doc( | ||
| ` | ||
| $ cx dast-environments list --format list | ||
| $ cx dast-environments list --filter "from=1,to=10" | ||
| $ cx dast-environments list --filter "search=production,sort=created" | ||
| `, | ||
| ), | ||
| Annotations: map[string]string{ | ||
| "command:doc": heredoc.Doc( | ||
| ` | ||
| https://checkmarx.com/resource/documents/en/todo | ||
| `, | ||
| ), | ||
| }, | ||
| RunE: runListDastEnvironmentsCommand(dastEnvironmentsWrapper), | ||
| } | ||
| listDastEnvironmentsCmd.PersistentFlags().StringSlice(commonParams.FilterFlag, []string{}, filterDastEnvironmentsListFlagUsage) | ||
|
|
||
| commandutils.AddFormatFlagToMultipleCommands( | ||
| []*cobra.Command{listDastEnvironmentsCmd}, | ||
| printer.FormatTable, | ||
| printer.FormatJSON, | ||
| printer.FormatList, | ||
| ) | ||
|
|
||
| environmentsCmd.AddCommand(listDastEnvironmentsCmd) | ||
| return environmentsCmd | ||
| } | ||
|
|
||
| func runListDastEnvironmentsCommand(dastEnvironmentsWrapper wrappers.DastEnvironmentsWrapper) func(cmd *cobra.Command, args []string) error { | ||
| return func(cmd *cobra.Command, args []string) error { | ||
| var allEnvironmentsModel *wrappers.DastEnvironmentsCollectionResponseModel | ||
| var errorModel *wrappers.ErrorModel | ||
|
|
||
| params, err := commandutils.GetFilters(cmd) | ||
| if err != nil { | ||
| return errors.Wrapf(err, "%s", failedGettingDastEnvironments) | ||
| } | ||
|
|
||
| // The API expects: from, to, search, sort | ||
| // from and to are pagination parameters (e.g., from=1, to=10 for first page) | ||
| allEnvironmentsModel, errorModel, err = dastEnvironmentsWrapper.Get(params) | ||
| if err != nil { | ||
| return errors.Wrapf(err, "%s\n", failedGettingDastEnvironments) | ||
| } | ||
|
|
||
| // Checking the response | ||
| if errorModel != nil { | ||
| return errors.Errorf(services.ErrorCodeFormat, failedGettingDastEnvironments, errorModel.Code, errorModel.Message) | ||
| } else if allEnvironmentsModel != nil && allEnvironmentsModel.Environments != nil { | ||
| err = commandutils.PrintByFormat(cmd, toEnvironmentViews(allEnvironmentsModel.Environments)) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
| } | ||
|
|
||
| func toEnvironmentViews(models []wrappers.DastEnvironmentResponseModel) []environmentView { | ||
| result := make([]environmentView, len(models)) | ||
| for i := 0; i < len(models); i++ { | ||
| result[i] = toEnvironmentView(&models[i]) | ||
| } | ||
| return result | ||
| } | ||
|
|
||
| func toEnvironmentView(model *wrappers.DastEnvironmentResponseModel) environmentView { | ||
| return environmentView{ | ||
| EnvironmentID: model.EnvironmentID, | ||
| Domain: model.Domain, | ||
| URL: model.URL, | ||
| ScanType: model.ScanType, | ||
| Created: model.Created, | ||
| RiskRating: model.RiskRating, | ||
| LastScanTime: model.LastScanTime, | ||
| LastStatus: model.LastStatus, | ||
| } | ||
| } | ||
|
|
||
| type environmentView struct { | ||
| EnvironmentID string `format:"name:Environment ID"` | ||
| Domain string | ||
| URL string | ||
| ScanType string `format:"name:Scan Type"` | ||
| Created string | ||
| RiskRating string `format:"name:Risk Rating"` | ||
| LastScanTime string `format:"name:Last Scan Time"` | ||
| LastStatus string `format:"name:Last Status"` | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| //go:build !integration | ||
|
|
||
| package dast | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/checkmarx/ast-cli/internal/wrappers/mock" | ||
| "gotest.tools/assert" | ||
| ) | ||
|
|
||
| func createTestCommand(args ...string) error { | ||
| cmd := NewDastEnvironmentsCommand(&mock.DastEnvironmentsMockWrapper{}) | ||
| cmd.SetArgs(args) | ||
| return cmd.Execute() | ||
| } | ||
|
|
||
| func TestDastEnvironmentsHelp(t *testing.T) { | ||
| err := createTestCommand("--help") | ||
| assert.NilError(t, err) | ||
| } | ||
|
|
||
| func TestDastEnvironmentsNoSub(t *testing.T) { | ||
| err := createTestCommand() | ||
| assert.NilError(t, err) | ||
| } | ||
|
|
||
| func TestDastEnvironmentsList(t *testing.T) { | ||
| err := createTestCommand("list") | ||
| assert.NilError(t, err) | ||
| } | ||
|
|
||
| func TestDastEnvironmentsListWithFormat(t *testing.T) { | ||
| err := createTestCommand("list", "--format", "json") | ||
| assert.NilError(t, err) | ||
| } | ||
|
|
||
| func TestDastEnvironmentsListWithFilters(t *testing.T) { | ||
| err := createTestCommand("list", "--filter", "from=1,to=10") | ||
| assert.NilError(t, err) | ||
| } | ||
|
|
||
| func TestDastEnvironmentsListWithSearch(t *testing.T) { | ||
| err := createTestCommand("list", "--filter", "search=test") | ||
| assert.NilError(t, err) | ||
| } | ||
|
|
||
| func TestDastEnvironmentsListWithSort(t *testing.T) { | ||
| err := createTestCommand("list", "--filter", "sort=domain:asc") | ||
| assert.NilError(t, err) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,6 +9,7 @@ import ( | |
| "encoding/json" | ||
|
|
||
| "github.com/MakeNowJust/heredoc" | ||
| "github.com/checkmarx/ast-cli/internal/commands/commandutils" | ||
| "github.com/checkmarx/ast-cli/internal/commands/util/printer" | ||
| "github.com/checkmarx/ast-cli/internal/logger" | ||
| "github.com/checkmarx/ast-cli/internal/params" | ||
|
|
@@ -35,7 +36,7 @@ func NewResultsPredicatesCommand(resultsPredicatesWrapper wrappers.ResultsPredic | |
| triageUpdateCmd := triageUpdateSubCommand(resultsPredicatesWrapper, featureFlagsWrapper, customStatesWrapper) | ||
| triageGetStatesCmd := triageGetStatesSubCommand(customStatesWrapper, featureFlagsWrapper) | ||
|
|
||
| addFormatFlagToMultipleCommands( | ||
| commandutils.AddFormatFlagToMultipleCommands( | ||
|
||
| []*cobra.Command{triageShowCmd}, | ||
| printer.FormatList, printer.FormatTable, printer.FormatJSON, | ||
| ) | ||
|
|
@@ -184,7 +185,7 @@ func runTriageShow(resultsPredicatesWrapper wrappers.ResultsPredicatesWrapper) f | |
| if err != nil { | ||
| return errors.Wrapf(err, "%s", "Failed showing the predicate") | ||
| } | ||
| err = printByFormat(cmd, toScaPredicateResultView(scaPredicates)) | ||
| err = commandutils.PrintByFormat(cmd, toScaPredicateResultView(scaPredicates)) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
@@ -203,7 +204,7 @@ func runTriageShow(resultsPredicatesWrapper wrappers.ResultsPredicatesWrapper) f | |
| errorModel.Message, | ||
| ) | ||
| } else if predicatesCollection != nil { | ||
| err = printByFormat(cmd, toPredicatesView(*predicatesCollection)) | ||
| err = commandutils.PrintByFormat(cmd, toPredicatesView(*predicatesCollection)) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All these methods are in root.go and mainly used inside command implementations. Do we need this redundant globally available?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The issue I had is that all DAST commands are in the package "dast".
The package "commands" depends on package "dast".
So if DAST commands want to use methods from root.go there will be a dependency cycle.