diff --git a/cmd/main.go b/cmd/main.go index 7a3a7492d..56c96566d 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -12,9 +12,7 @@ import ( ) const ( - astSchemaEnv = "AST_SCHEMA" - astHostEnv = "AST_HOST" - astPortEnv = "AST_PORT" + astURIEnv = "AST_URI" scansPathEnv = "SCANS_PATH" projectsPathEnv = "PROJECTS_PATH" resultsPathEnv = "RESULTS_PATH" @@ -25,39 +23,29 @@ const ( ) func main() { - // Key ast_schema will be bound to AST_SCHEMA - astSchemaKey := strings.ToLower(astSchemaEnv) - err := bindKeyToEnvAndDefault(astSchemaKey, astSchemaEnv, "http") + // Key ast_uri will be bound to AST_URI + astURIKey := strings.ToLower(astURIEnv) + err := bindKeyToEnvAndDefault(astURIKey, astURIEnv, "http://localhost:80") exitIfError(err) - schema := viper.GetString(astSchemaKey) - - astHostKey := strings.ToLower(astHostEnv) - err = bindKeyToEnvAndDefault(astHostKey, astHostEnv, "localhost") - exitIfError(err) - host := viper.GetString(astHostKey) - - astPortKey := strings.ToLower(astPortEnv) - err = bindKeyToEnvAndDefault(astPortKey, astPortEnv, "80") - exitIfError(err) - port := viper.GetString(astPortKey) + ast := viper.GetString(astURIKey) scansPathKey := strings.ToLower(scansPathEnv) - err = bindKeyToEnvAndDefault(scansPathKey, scansPathEnv, "scans") + err = bindKeyToEnvAndDefault(scansPathKey, scansPathEnv, "api/scans") exitIfError(err) scans := viper.GetString(scansPathKey) projectsPathKey := strings.ToLower(projectsPathEnv) - err = bindKeyToEnvAndDefault(projectsPathKey, projectsPathEnv, "projects") + err = bindKeyToEnvAndDefault(projectsPathKey, projectsPathEnv, "api/projects") exitIfError(err) projects := viper.GetString(projectsPathKey) resultsPathKey := strings.ToLower(resultsPathEnv) - err = bindKeyToEnvAndDefault(resultsPathKey, resultsPathEnv, "results") + err = bindKeyToEnvAndDefault(resultsPathKey, resultsPathEnv, "api/results") exitIfError(err) results := viper.GetString(resultsPathKey) uploadsPathKey := strings.ToLower(uploadsPathEnv) - err = bindKeyToEnvAndDefault(uploadsPathKey, uploadsPathEnv, "uploads") + err = bindKeyToEnvAndDefault(uploadsPathKey, uploadsPathEnv, "api/uploads") exitIfError(err) uploads := viper.GetString(uploadsPathKey) @@ -65,11 +53,9 @@ func main() { exitIfError(err) err = bindKeyToEnvAndDefault(commands.AccessKeySecretConfigKey, commands.AccessKeySecretEnv, "") exitIfError(err) - err = bindKeyToEnvAndDefault(commands.AstAuthenticationHostConfigKey, commands.AstAuthenticationHostEnv, "") + err = bindKeyToEnvAndDefault(commands.AstAuthenticationURIConfigKey, commands.AstAuthenticationURIEnv, "") exitIfError(err) - ast := fmt.Sprintf("%s://%s:%s/api", schema, host, port) - scansURL := fmt.Sprintf("%s/%s", ast, scans) uploadsURL := fmt.Sprintf("%s/%s", ast, uploads) projectsURL := fmt.Sprintf("%s/%s", ast, projects) @@ -103,5 +89,5 @@ func bindKeyToEnvAndDefault(key, env, defaultVal string) error { // When building an executable for Windows and providing a name, // be sure to explicitly specify the .exe suffix when setting the executable’s name. // env GOOS=windows GOARCH=amd64 go build -o ./bin/ast.exe ./cmd -// "bin/ast.exe" -v scan create --inputFile ./internal/commands/payloads/uploads.json --sources ./internal/commands/payloads/sources.zip -// "bin/ast.exe" scan get --id 4d9a9189-ddcc-4aa0-ba2f-9d6d7f92eceb +// "bin/ast.exe" -v scan create --input-file ./internal/commands/payloads/uploads.json --sources ./internal/commands/payloads/sources.zip +// "bin/ast.exe" scan list 4d9a9189-ddcc-4aa0-ba2f-9d6d7f92eceb diff --git a/internal/commands/cluster.go b/internal/commands/cluster.go new file mode 100644 index 000000000..b0d034a14 --- /dev/null +++ b/internal/commands/cluster.go @@ -0,0 +1,29 @@ +package commands + +import ( + "fmt" + + "github.com/spf13/cobra" +) + +func NewClusterCommand() *cobra.Command { + clusterCmd := &cobra.Command{ + Use: "cluster", + Short: "Manage AST cluster", + } + + deployClusterCmd := &cobra.Command{ + Use: "deploy", + Short: "Deploy AST resources", + RunE: runDeployClusterCommand(), + } + clusterCmd.AddCommand(deployClusterCmd) + return clusterCmd +} + +func runDeployClusterCommand() func(cmd *cobra.Command, args []string) error { + return func(cmd *cobra.Command, args []string) error { + fmt.Println("deploy cluster") + return nil + } +} diff --git a/internal/commands/project.go b/internal/commands/project.go index 5e544ec97..5138d58fa 100644 --- a/internal/commands/project.go +++ b/internal/commands/project.go @@ -36,17 +36,17 @@ func NewProjectCommand(projectsWrapper wrappers.ProjectsWrapper) *cobra.Command createProjCmd.PersistentFlags().StringP(inputFileFlag, inputFileFlagSh, "", "A file holding the requested project object in JSON format. Takes precedence over --input") - getAllProjCmd := &cobra.Command{ - Use: "get-all", - Short: "Returns all projects in the system", - RunE: runGetAllProjectsCommand(projectsWrapper), + listProjectsCmd := &cobra.Command{ + Use: "list", + Short: "List all projects in the system", + RunE: runListProjectsCommand(projectsWrapper), } - getAllProjCmd.PersistentFlags().Uint64P(limitFlag, limitFlagSh, 0, limitUsage) - getAllProjCmd.PersistentFlags().Uint64P(offsetFlag, offsetFlagSh, 0, offsetUsage) + listProjectsCmd.PersistentFlags().Uint64P(limitFlag, limitFlagSh, 0, limitUsage) + listProjectsCmd.PersistentFlags().Uint64P(offsetFlag, offsetFlagSh, 0, offsetUsage) - getProjCmd := &cobra.Command{ - Use: "get", - Short: "Returns information about a project", + showProjectCmd := &cobra.Command{ + Use: "show", + Short: "Show information about a project", RunE: runGetProjectByIDCommand(projectsWrapper), } @@ -62,7 +62,7 @@ func NewProjectCommand(projectsWrapper wrappers.ProjectsWrapper) *cobra.Command RunE: runGetProjectsTagsCommand(projectsWrapper), } - projCmd.AddCommand(createProjCmd, getProjCmd, getAllProjCmd, deleteProjCmd, tagsCmd) + projCmd.AddCommand(createProjCmd, showProjectCmd, listProjectsCmd, deleteProjCmd, tagsCmd) return projCmd } @@ -132,7 +132,7 @@ func runCreateProjectCommand(projectsWrapper wrappers.ProjectsWrapper) func(cmd } } -func runGetAllProjectsCommand(projectsWrapper wrappers.ProjectsWrapper) func(cmd *cobra.Command, args []string) error { +func runListProjectsCommand(projectsWrapper wrappers.ProjectsWrapper) func(cmd *cobra.Command, args []string) error { return func(cmd *cobra.Command, args []string) error { var allProjectsModel *projectsRESTApi.SlicedProjectsResponseModel var errorModel *projectsRESTApi.ErrorModel @@ -226,6 +226,7 @@ func runGetProjectsTagsCommand(projectsWrapper wrappers.ProjectsWrapper) func(cm var tags *[]string var errorModel *projectsRESTApi.ErrorModel var err error + tags, errorModel, err = projectsWrapper.Tags() if err != nil { return errors.Wrapf(err, "%s", failedGettingTags) diff --git a/internal/commands/project_test.go b/internal/commands/project_test.go index 8f3084e4f..00fafcb86 100644 --- a/internal/commands/project_test.go +++ b/internal/commands/project_test.go @@ -23,9 +23,9 @@ func TestProjectNoSub(t *testing.T) { func TestRunCreateProjectCommandWithFile(t *testing.T) { cmd := createASTTestCommand() - err := executeTestCommand(cmd, "-v", "project", "create", "--inputFile", "./payloads/nonsense.json") + err := executeTestCommand(cmd, "-v", "project", "create", "--input-file", "./payloads/nonsense.json") assert.Assert(t, err != nil) - err = executeTestCommand(cmd, "-v", "project", "create", "--inputFile", "./payloads/projects.json") + err = executeTestCommand(cmd, "-v", "project", "create", "--input-file", "./payloads/projects.json") assert.NilError(t, err) } @@ -50,7 +50,7 @@ func TestRunCreateProjectCommandWithInputBadFormat(t *testing.T) { func TestRunGetProjectByIdCommandNoScanID(t *testing.T) { cmd := createASTTestCommand() - err := executeTestCommand(cmd, "-v", "project", "get") + err := executeTestCommand(cmd, "-v", "project", "show") assert.Assert(t, err != nil) assert.Assert(t, err.Error() == "Failed getting a project: Please provide a project ID") } @@ -64,7 +64,7 @@ func TestRunGetProjectByIdCommandFlagNonExist(t *testing.T) { func TestRunGetProjectByIdCommand(t *testing.T) { cmd := createASTTestCommand() - err := executeTestCommand(cmd, "-v", "project", "get", "MOCK") + err := executeTestCommand(cmd, "-v", "project", "show", "MOCK") assert.NilError(t, err) } func TestRunDeleteProjectByIdCommandNoProjectID(t *testing.T) { @@ -89,30 +89,30 @@ func TestRunDeleteProjectByIdCommand(t *testing.T) { func TestRunGetAllProjectsCommand(t *testing.T) { cmd := createASTTestCommand() - err := executeTestCommand(cmd, "-v", "project", "get-all") + err := executeTestCommand(cmd, "-v", "project", "list") assert.NilError(t, err) } func TestRunGetAllProjectsCommandFlagNonExist(t *testing.T) { cmd := createASTTestCommand() - err := executeTestCommand(cmd, "-v", "project", "get-all", "--chibutero") + err := executeTestCommand(cmd, "-v", "project", "list", "--chibutero") assert.Assert(t, err != nil) assert.Assert(t, err.Error() == unknownFlag) } func TestRunGetAllProjectsCommandWithLimit(t *testing.T) { cmd := createASTTestCommand() - err := executeTestCommand(cmd, "-v", "project", "get-all", "--limit", "40") + err := executeTestCommand(cmd, "-v", "project", "list", "--limit", "40") assert.NilError(t, err) - err = executeTestCommand(cmd, "-v", "project", "get-all", "-l", "40") + err = executeTestCommand(cmd, "-v", "project", "list", "-l", "40") assert.NilError(t, err) } func TestRunGetAllProjectsCommandWithOffset(t *testing.T) { cmd := createASTTestCommand() - err := executeTestCommand(cmd, "-v", "project", "get-all", "--offset", "150") + err := executeTestCommand(cmd, "-v", "project", "list", "--offset", "150") assert.NilError(t, err) - err = executeTestCommand(cmd, "-v", "project", "get-all", "-o", "150") + err = executeTestCommand(cmd, "-v", "project", "list", "-o", "150") assert.NilError(t, err) } diff --git a/internal/commands/result.go b/internal/commands/result.go index 600168cc1..d7eaa9ec9 100644 --- a/internal/commands/result.go +++ b/internal/commands/result.go @@ -10,7 +10,7 @@ import ( ) const ( - failedGettingResults = "Failed getting results" + failedListingResults = "Failed listing results" ) func NewResultCommand(resultsWrapper wrappers.ResultsWrapper) *cobra.Command { @@ -19,15 +19,15 @@ func NewResultCommand(resultsWrapper wrappers.ResultsWrapper) *cobra.Command { Short: "Retrieve AST results", } - getResultsCmd := &cobra.Command{ - Use: "get", - Short: "Returns results for a given scan", + listResultsCmd := &cobra.Command{ + Use: "list", + Short: "List results for a given scan", RunE: runGetResultByScanIDCommand(resultsWrapper), } - getResultsCmd.PersistentFlags().Uint64P(limitFlag, limitFlagSh, 0, limitUsage) - getResultsCmd.PersistentFlags().Uint64P(offsetFlag, offsetFlagSh, 0, offsetUsage) + listResultsCmd.PersistentFlags().Uint64P(limitFlag, limitFlagSh, 0, limitUsage) + listResultsCmd.PersistentFlags().Uint64P(offsetFlag, offsetFlagSh, 0, offsetUsage) - resultCmd.AddCommand(getResultsCmd) + resultCmd.AddCommand(listResultsCmd) return resultCmd } @@ -37,22 +37,23 @@ func runGetResultByScanIDCommand(resultsWrapper wrappers.ResultsWrapper) func(cm var errorModel *wrappers.ResultError var err error if len(args) == 0 { - return errors.Errorf("%s: Please provide a scan ID", failedGettingResults) + return errors.Errorf("%s: Please provide a scan ID", failedListingResults) } scanID := args[0] limit, offset := getLimitAndOffset(cmd) + resultResponseModel, errorModel, err = resultsWrapper.GetByScanID(scanID, limit, offset) if err != nil { - return errors.Wrapf(err, "%s", failedGettingResults) + return errors.Wrapf(err, "%s", failedListingResults) } // Checking the response if errorModel != nil { - return errors.Errorf("%s: CODE: %d, %s", failedGettingResults, errorModel.Code, errorModel.Message) + return errors.Errorf("%s: CODE: %d, %s", failedListingResults, errorModel.Code, errorModel.Message) } else if resultResponseModel != nil { var responseModelJSON []byte responseModelJSON, err = json.Marshal(resultResponseModel) if err != nil { - return errors.Wrapf(err, "%s: failed to serialize results response ", failedGettingResults) + return errors.Wrapf(err, "%s: failed to serialize results response ", failedListingResults) } cmdOut := cmd.OutOrStdout() fmt.Fprintln(cmdOut, string(responseModelJSON)) diff --git a/internal/commands/result_test.go b/internal/commands/result_test.go index 2d3599233..fbd557e23 100644 --- a/internal/commands/result_test.go +++ b/internal/commands/result_test.go @@ -16,12 +16,12 @@ func TestResultHelp(t *testing.T) { func TestRunGetResultsByScanIDCommandNoScanID(t *testing.T) { cmd := createASTTestCommand() - err := executeTestCommand(cmd, "-v", "result", "get") + err := executeTestCommand(cmd, "-v", "result", "list") assert.Assert(t, err != nil) - assert.Assert(t, err.Error() == "Failed getting results: Please provide a scan ID") + assert.Assert(t, err.Error() == "Failed listing results: Please provide a scan ID") } func TestRunGetResultsByScanIDCommand(t *testing.T) { cmd := createASTTestCommand() - err := executeTestCommand(cmd, "-v", "result", "get", "MOCK") + err := executeTestCommand(cmd, "-v", "result", "list", "MOCK") assert.NilError(t, err) } diff --git a/internal/commands/root.go b/internal/commands/root.go index 9b1261abc..c7653e7a2 100644 --- a/internal/commands/root.go +++ b/internal/commands/root.go @@ -10,34 +10,38 @@ import ( ) const ( - verboseFlag = "verbose" - verboseFlagSh = "v" - verboseUsage = "Verbose mode" - sourcesFlag = "sources" - sourcesFlagSh = "s" - inputFlag = "input" - inputFlagSh = "i" - inputFileFlag = "inputFile" - inputFileFlagSh = "f" - limitFlag = "limit" - limitFlagSh = "l" - limitUsage = "The number of items to return" - offsetFlag = "offset" - offsetFlagSh = "o" - offsetUsage = "The number of items to skip before collecting the results" - AccessKeyIDEnv = "AST_ACCESS_KEY_ID" - accessKeyIDFlag = "key" - accessKeyIDFlagUsage = "The access key ID for AST" - AccessKeySecretEnv = "AST_ACCESS_KEY_SECRET" - accessKeySecretFlag = "secret" - accessKeySecretFlagUsage = "The access key secret for AST" - AstAuthenticationHostEnv = "AST_AUTHENTICATION_HOST" + verboseFlag = "verbose" + verboseFlagSh = "v" + verboseUsage = "Verbose mode" + sourcesFlag = "sources" + sourcesFlagSh = "s" + inputFlag = "input" + inputFlagSh = "i" + inputFileFlag = "input-file" + inputFileFlagSh = "f" + limitFlag = "limit" + limitFlagSh = "l" + limitUsage = "The number of items to return" + offsetFlag = "offset" + offsetFlagSh = "o" + offsetUsage = "The number of items to skip before collecting the results" + AccessKeyIDEnv = "AST_ACCESS_KEY_ID" + accessKeyIDFlag = "key" + accessKeyIDFlagUsage = "The access key ID for AST" + AccessKeySecretEnv = "AST_ACCESS_KEY_SECRET" + accessKeySecretFlag = "secret" + accessKeySecretFlagUsage = "The access key secret for AST" + AstAuthenticationURIEnv = "AST_AUTHENTICATION_URI" + astAuthenticationURIFlag = "auth-uri" + astAuthenticationURIFlagUsage = "The authentication URI for AST" + insecureFlag = "insecure" + insecureFlagUsage = "Ignore TLS certificate validations" ) var ( - AccessKeyIDConfigKey = strings.ToLower(AccessKeyIDEnv) - AccessKeySecretConfigKey = strings.ToLower(AccessKeySecretEnv) - AstAuthenticationHostConfigKey = strings.ToLower(AstAuthenticationHostEnv) + AccessKeyIDConfigKey = strings.ToLower(AccessKeyIDEnv) + AccessKeySecretConfigKey = strings.ToLower(AccessKeySecretEnv) + AstAuthenticationURIConfigKey = strings.ToLower(AstAuthenticationURIEnv) ) // Return an AST CLI root command to execute @@ -53,39 +57,29 @@ func NewAstCLI(scansWrapper wrappers.ScansWrapper, rootCmd.PersistentFlags().BoolP(verboseFlag, verboseFlagSh, false, verboseUsage) rootCmd.PersistentFlags().String(accessKeyIDFlag, "", accessKeyIDFlagUsage) rootCmd.PersistentFlags().String(accessKeySecretFlag, "", accessKeySecretFlagUsage) + rootCmd.PersistentFlags().String(astAuthenticationURIFlag, "", astAuthenticationURIFlagUsage) + rootCmd.PersistentFlags().Bool(insecureFlag, false, insecureFlagUsage) // Bind the viper key ast_access_key_id to flag --key of the root command and // to the environment variable AST_ACCESS_KEY_ID so that it will be taken from environment variables first // and can be overridden by command flag --key _ = viper.BindPFlag(AccessKeyIDConfigKey, rootCmd.PersistentFlags().Lookup(accessKeyIDFlag)) - // Bind the viper key ast_access_key_secret to flag --secret of the root command and - // to the environment variable AST_ACCESS_KEY_SECRET so that it will be taken from environment variables first - // and can be overridden by command flag --secret _ = viper.BindPFlag(AccessKeySecretConfigKey, rootCmd.PersistentFlags().Lookup(accessKeySecretFlag)) + _ = viper.BindPFlag(AstAuthenticationURIConfigKey, rootCmd.PersistentFlags().Lookup(astAuthenticationURIFlag)) + // Key here is the actual flag since it doesn't use an environment variable + _ = viper.BindPFlag(insecureFlag, rootCmd.PersistentFlags().Lookup(insecureFlag)) scanCmd := NewScanCommand(scansWrapper, uploadsWrapper) projectCmd := NewProjectCommand(projectsWrapper) resultCmd := NewResultCommand(resultsWrapper) versionCmd := NewVersionCommand() + clusterCmd := NewClusterCommand() - rootCmd.AddCommand(scanCmd, projectCmd, resultCmd, versionCmd) + rootCmd.AddCommand(clusterCmd, scanCmd, projectCmd, resultCmd, versionCmd) rootCmd.SilenceUsage = true return rootCmd } -/* -func login(cmd *cobra.Command, args []string) error { - accessKeyID := viper.GetString(AccessKeyIDConfigKey) - accessKeySecret := viper.GetString(AccessKeySecretConfigKey) - authHost := viper.GetString(AstAuthenticationHostConfigKey) - var err error - - fmt.Println("Authenticating with:", authHost) - fmt.Println("Key is:", accessKeyID) - fmt.Println("Secret IS:", accessKeySecret) - return err -} -*/ func PrintIfVerbose(verbose bool, msg string) { if verbose { fmt.Println(msg) diff --git a/internal/commands/scan.go b/internal/commands/scan.go index c70418b36..e57a30304 100644 --- a/internal/commands/scan.go +++ b/internal/commands/scan.go @@ -41,17 +41,17 @@ func NewScanCommand(scansWrapper wrappers.ScansWrapper, uploadsWrapper wrappers. createScanCmd.PersistentFlags().StringP(inputFileFlag, inputFileFlagSh, "", "A file holding the requested scan object in JSON format. Takes precedence over --input") - getAllScansCmd := &cobra.Command{ - Use: "get-all", - Short: "Returns all scans in the system", + listScansCmd := &cobra.Command{ + Use: "list", + Short: "List all scans in the system", RunE: runGetAllScansCommand(scansWrapper), } - getAllScansCmd.PersistentFlags().Uint64P(limitFlag, limitFlagSh, 0, limitUsage) - getAllScansCmd.PersistentFlags().Uint64P(offsetFlag, offsetFlagSh, 0, offsetUsage) + listScansCmd.PersistentFlags().Uint64P(limitFlag, limitFlagSh, 0, limitUsage) + listScansCmd.PersistentFlags().Uint64P(offsetFlag, offsetFlagSh, 0, offsetUsage) - getScanCmd := &cobra.Command{ - Use: "get", - Short: "Returns information about a scan", + showScanCmd := &cobra.Command{ + Use: "show", + Short: "Show information about a scan", RunE: runGetScanByIDCommand(scansWrapper), } @@ -67,7 +67,7 @@ func NewScanCommand(scansWrapper wrappers.ScansWrapper, uploadsWrapper wrappers. RunE: runGetTagsCommand(scansWrapper), } - scanCmd.AddCommand(createScanCmd, getScanCmd, getAllScansCmd, deleteScanCmd, tagsCmd) + scanCmd.AddCommand(createScanCmd, showScanCmd, listScansCmd, deleteScanCmd, tagsCmd) return scanCmd } @@ -184,6 +184,16 @@ func runGetAllScansCommand(scansWrapper wrappers.ScansWrapper) func(cmd *cobra.C } fmt.Fprintln(cmdOut, string(allScansJSON)) } + for _, scan := range allScansModel.Scans { + var responseModelJSON []byte + responseModelJSON, err = json.Marshal(scan) + if err != nil { + return errors.Wrapf(err, "%s: failed to serialize project response ", failedGettingAll) + } + fmt.Fprintln(os.Stdout, "----------------------------") + fmt.Fprintln(os.Stdout, string(responseModelJSON)) + } + fmt.Fprintln(os.Stdout, "----------------------------") } return nil } diff --git a/internal/commands/scan_test.go b/internal/commands/scan_test.go index cbe0f6963..9a59c2c14 100644 --- a/internal/commands/scan_test.go +++ b/internal/commands/scan_test.go @@ -28,9 +28,9 @@ func TestScanNoSub(t *testing.T) { func TestRunCreateScanCommandWithFile(t *testing.T) { cmd := createASTTestCommand() - err := executeTestCommand(cmd, "-v", "scan", "create", "--inputFile", "./payloads/nonsense.json") + err := executeTestCommand(cmd, "-v", "scan", "create", "--input-file", "./payloads/nonsense.json") assert.Assert(t, err != nil) - err = executeTestCommand(cmd, "-v", "scan", "create", "--inputFile", "./payloads/uploads.json", "--sources", "./payloads/sources.zip") + err = executeTestCommand(cmd, "-v", "scan", "create", "--input-file", "./payloads/uploads.json", "--sources", "./payloads/sources.zip") assert.NilError(t, err) } @@ -57,21 +57,21 @@ func TestRunCreateScanCommandWithInputBadFormat(t *testing.T) { func TestRunGetScanByIdCommandNoScanID(t *testing.T) { cmd := createASTTestCommand() - err := executeTestCommand(cmd, "-v", "scan", "get") + err := executeTestCommand(cmd, "-v", "scan", "show") assert.Assert(t, err != nil) assert.Assert(t, err.Error() == "Failed getting a scan: Please provide a scan ID") } func TestRunGetScanByIdCommandFlagNonExist(t *testing.T) { cmd := createASTTestCommand() - err := executeTestCommand(cmd, "-v", "scan", "get", "--chibutero") + err := executeTestCommand(cmd, "-v", "scan", "show", "--chibutero") assert.Assert(t, err != nil) assert.Assert(t, err.Error() == unknownFlag) } func TestRunGetScanByIdCommand(t *testing.T) { cmd := createASTTestCommand() - err := executeTestCommand(cmd, "-v", "scan", "get", "MOCK") + err := executeTestCommand(cmd, "-v", "scan", "show", "MOCK") assert.NilError(t, err) } func TestRunDeleteScanByIdCommandNoScanID(t *testing.T) { @@ -96,13 +96,13 @@ func TestRunDeleteScanByIdCommand(t *testing.T) { func TestRunGetAllCommand(t *testing.T) { cmd := createASTTestCommand() - err := executeTestCommand(cmd, "-v", "scan", "get-all") + err := executeTestCommand(cmd, "-v", "scan", "list") assert.NilError(t, err) } func TestRunGetAllCommandFlagNonExist(t *testing.T) { cmd := createASTTestCommand() - err := executeTestCommand(cmd, "-v", "scan", "get-all", "--chibutero") + err := executeTestCommand(cmd, "-v", "scan", "list", "--chibutero") assert.Assert(t, err != nil) assert.Assert(t, err.Error() == unknownFlag) } diff --git a/internal/wrappers/client.go b/internal/wrappers/client.go new file mode 100644 index 000000000..176090b11 --- /dev/null +++ b/internal/wrappers/client.go @@ -0,0 +1,114 @@ +package wrappers + +import ( + "crypto/tls" + "encoding/json" + "fmt" + "io" + "io/ioutil" + "net/http" + "strconv" + "strings" + + "github.com/spf13/viper" +) + +type ClientCredentialsInfo struct { + AccessToken string `json:"access_token"` + ExpiresIn int `json:"expires_in"` + RefreshExpiresIn int `json:"refresh_expires_in"` + RefreshToken string `json:"refresh_token"` + TokenType string `json:"token_type"` + SessionState string `json:"session_state"` + Scope string `json:"scope"` +} + +func getClient() *http.Client { + insecure := viper.GetBool("insecure") + tr := &http.Transport{ + TLSClientConfig: &tls.Config{InsecureSkipVerify: insecure}, + } + return &http.Client{Transport: tr} +} + +func SendHTTPRequest(method, url string, body io.Reader) (*http.Response, error) { + client := getClient() + req, err := http.NewRequest(method, url, body) + if err != nil { + return nil, err + } + req, err = enrichWithCredentials(req) + if err != nil { + return nil, err + } + var resp *http.Response + resp, err = client.Do(req) + if err != nil { + return nil, err + } + return resp, nil +} + +func SendHTTPRequestWithLimitAndOffset(method, url string, limit, offset uint64, body io.Reader) (*http.Response, error) { + client := getClient() + req, err := http.NewRequest(method, url, body) + if err != nil { + return nil, err + } + q := req.URL.Query() + if limit > 0 { + q.Add(limitQueryParam, strconv.FormatUint(limit, 10)) + } + if offset > 0 { + q.Add(offsetQueryParam, strconv.FormatUint(offset, 10)) + } + req.URL.RawQuery = q.Encode() + req, err = enrichWithCredentials(req) + if err != nil { + return nil, err + } + var resp *http.Response + resp, err = client.Do(req) + if err != nil { + return nil, err + } + return resp, nil +} + +func enrichWithCredentials(request *http.Request) (*http.Request, error) { + authHost := viper.GetString("ast_authentication_uri") + accessKeyID := viper.GetString("ast_access_key_id") + accessKeySecret := viper.GetString("ast_access_key_secret") + + credentialsInfo, err := getClientCredentials(authHost, accessKeyID, accessKeySecret) + if err != nil { + return nil, err + } + request.Header.Add("Authorization", credentialsInfo.AccessToken) + return request, nil +} + +func getClientCredentials(authServerURI, accessKeyID, accessKeySecret string) (*ClientCredentialsInfo, error) { + payload := strings.NewReader(getCredentialsPayload(accessKeyID, accessKeySecret)) + req, err := http.NewRequest(http.MethodPost, authServerURI, payload) + if err != nil { + return nil, err + } + req.Header.Add("content-type", "application/x-www-form-urlencoded") + res, err := http.DefaultClient.Do(req) + if err != nil { + return nil, err + } + defer res.Body.Close() + body, _ := ioutil.ReadAll(res.Body) + info := ClientCredentialsInfo{} + err = json.Unmarshal(body, &info) + if err != nil { + return nil, err + } + return &info, nil +} + +func getCredentialsPayload(accessKeyID, accessKeySecret string) string { + return fmt.Sprintf("grant_type=client_credentials&client_id=%s&client_secret=%s", accessKeyID, accessKeySecret) +} diff --git a/internal/wrappers/credentials.go b/internal/wrappers/credentials.go deleted file mode 100644 index 8a05019d7..000000000 --- a/internal/wrappers/credentials.go +++ /dev/null @@ -1,7 +0,0 @@ -package wrappers - -type Credentials struct { - AuthenticationHost string - AccessKeyID string - AccessKeySecret string -} diff --git a/internal/wrappers/projects-http.go b/internal/wrappers/projects-http.go index e405281e6..b2ec116cf 100644 --- a/internal/wrappers/projects-http.go +++ b/internal/wrappers/projects-http.go @@ -4,7 +4,6 @@ import ( "bytes" "encoding/json" "net/http" - "strconv" "github.com/pkg/errors" @@ -17,15 +16,12 @@ const ( ) type ProjectsHTTPWrapper struct { - url string - contentType string - credentials *Credentials + url string } func NewHTTPProjectsWrapper(url string) ProjectsWrapper { return &ProjectsHTTPWrapper{ - url: url, - contentType: "application/json", + url: url, } } @@ -37,14 +33,17 @@ func (p *ProjectsHTTPWrapper) Create(model *projectsRESTApi.Project) ( return nil, nil, err } - resp, err := http.Post(p.url, p.contentType, bytes.NewBuffer(jsonBytes)) + resp, err := SendHTTPRequest(http.MethodPost, p.url, bytes.NewBuffer(jsonBytes)) + if err != nil { + return nil, nil, err + } return handleProjectResponseWithBody(resp, err, http.StatusCreated) } func (p *ProjectsHTTPWrapper) Get(limit, offset uint64) ( *projectsRESTApi.SlicedProjectsResponseModel, *projectsRESTApi.ErrorModel, error) { - resp, err := getRequestWithLimitAndOffset(p.url, limit, offset) + resp, err := SendHTTPRequestWithLimitAndOffset(http.MethodGet, p.url, limit, offset, nil) if err != nil { return nil, nil, err } @@ -76,22 +75,18 @@ func (p *ProjectsHTTPWrapper) GetByID(projectID string) ( *projectsRESTApi.ProjectResponseModel, *projectsRESTApi.ErrorModel, error) { - resp, err := http.Get(p.url + "/" + projectID) + resp, err := SendHTTPRequest(http.MethodGet, p.url+"/"+projectID, nil) if err != nil { return nil, nil, err } return handleProjectResponseWithBody(resp, err, http.StatusOK) } -func (p *ProjectsHTTPWrapper) Delete(projectID string) ( - *projectsRESTApi.ErrorModel, - error) { - client := &http.Client{} - req, err := http.NewRequest("DELETE", p.url+"/"+projectID, nil) +func (p *ProjectsHTTPWrapper) Delete(projectID string) (*projectsRESTApi.ErrorModel, error) { + resp, err := SendHTTPRequest(http.MethodDelete, p.url+"/"+projectID, nil) if err != nil { return nil, err } - resp, err := client.Do(req) return handleProjectResponseWithNoBody(resp, err, http.StatusNoContent) } @@ -99,10 +94,11 @@ func (p *ProjectsHTTPWrapper) Tags() ( *[]string, *projectsRESTApi.ErrorModel, error) { - resp, err := http.Get(p.url + "/tags") + resp, err := SendHTTPRequest(http.MethodGet, p.url+"/tags", nil) if err != nil { return nil, nil, err } + decoder := json.NewDecoder(resp.Body) defer resp.Body.Close() @@ -126,21 +122,3 @@ func (p *ProjectsHTTPWrapper) Tags() ( return nil, nil, errors.Errorf("Unknown response status code %d", resp.StatusCode) } } - -func getRequestWithLimitAndOffset(url string, limit, offset uint64) (*http.Response, error) { - client := &http.Client{} - req, err := http.NewRequest("GET", url, nil) - if err != nil { - return nil, err - } - q := req.URL.Query() - if limit > 0 { - q.Add(limitQueryParam, strconv.FormatUint(limit, 10)) - } - if offset > 0 { - q.Add(offsetQueryParam, strconv.FormatUint(offset, 10)) - } - req.URL.RawQuery = q.Encode() - resp, err := client.Do(req) - return resp, err -} diff --git a/internal/wrappers/results-http.go b/internal/wrappers/results-http.go index 449aef36a..a2631d890 100644 --- a/internal/wrappers/results-http.go +++ b/internal/wrappers/results-http.go @@ -2,33 +2,36 @@ package wrappers import ( "encoding/json" + "fmt" "net/http" "github.com/pkg/errors" ) const ( - failedToParseGetResults = "Failed to parse get results" + failedToParseGetResults = "Failed to parse list results" ) type ResultsHTTPWrapper struct { - url string - contentType string - credentials *Credentials + url string } func NewHTTPResultsWrapper(url string) ResultsWrapper { return &ResultsHTTPWrapper{ - url: url, - contentType: "application/json", + url: url, } } -func (r *ResultsHTTPWrapper) GetByScanID(scanID string, limit, offset uint64) ([]ResultResponseModel, *ResultError, error) { - resp, err := getRequestWithLimitAndOffset(r.url+"/"+scanID+"/items", limit, offset) +func (r *ResultsHTTPWrapper) GetByScanID( + scanID string, + limit, + offset uint64) ([]ResultResponseModel, *ResultError, error) { + resp, err := SendHTTPRequestWithLimitAndOffset(http.MethodGet, + fmt.Sprintf("%s/%s/items", r.url, scanID), limit, offset, nil) if err != nil { return nil, nil, err } + decoder := json.NewDecoder(resp.Body) defer resp.Body.Close() diff --git a/internal/wrappers/results-mock.go b/internal/wrappers/results-mock.go index 339c478bf..3eb59c384 100644 --- a/internal/wrappers/results-mock.go +++ b/internal/wrappers/results-mock.go @@ -2,6 +2,7 @@ package wrappers type ResultsMockWrapper struct{} -func (r ResultsMockWrapper) GetByScanID(scanID string, limit, offset uint64) ([]ResultResponseModel, *ResultError, error) { +func (r ResultsMockWrapper) GetByScanID(scanID string, + limit, offset uint64) ([]ResultResponseModel, *ResultError, error) { return []ResultResponseModel{}, nil, nil } diff --git a/internal/wrappers/scans-http.go b/internal/wrappers/scans-http.go index 90bc9f287..29f49c025 100644 --- a/internal/wrappers/scans-http.go +++ b/internal/wrappers/scans-http.go @@ -10,14 +10,20 @@ import ( ) const ( - failedToParseGetAll = "Failed to parse get-all response" + failedToParseGetAll = "Failed to parse list response" failedToParseTags = "Failed to parse tags response" ) type ScansHTTPWrapper struct { url string contentType string - credentials *Credentials +} + +func NewHTTPScansWrapper(url string) ScansWrapper { + return &ScansHTTPWrapper{ + url: url, + contentType: "application/json", + } } func (s *ScansHTTPWrapper) Create(model *scansApi.Scan) (*scansApi.ScanResponseModel, *scansApi.ErrorModel, error) { @@ -25,13 +31,18 @@ func (s *ScansHTTPWrapper) Create(model *scansApi.Scan) (*scansApi.ScanResponseM if err != nil { return nil, nil, err } - - resp, err := http.Post(s.url, s.contentType, bytes.NewBuffer(jsonBytes)) + resp, err := SendHTTPRequest(http.MethodPost, s.url, bytes.NewBuffer(jsonBytes)) + if err != nil { + return nil, nil, err + } + if err != nil { + return nil, nil, err + } return handleScanResponseWithBody(resp, err, http.StatusCreated) } func (s *ScansHTTPWrapper) Get(limit, offset uint64) (*scansApi.SlicedScansResponseModel, *scansApi.ErrorModel, error) { - resp, err := getRequestWithLimitAndOffset(s.url, limit, offset) + resp, err := SendHTTPRequestWithLimitAndOffset(http.MethodGet, s.url, limit, offset, nil) if err != nil { return nil, nil, err } @@ -60,7 +71,7 @@ func (s *ScansHTTPWrapper) Get(limit, offset uint64) (*scansApi.SlicedScansRespo } func (s *ScansHTTPWrapper) GetByID(scanID string) (*scansApi.ScanResponseModel, *scansApi.ErrorModel, error) { - resp, err := http.Get(s.url + "/" + scanID) + resp, err := SendHTTPRequest(http.MethodGet, s.url+"/"+scanID, nil) if err != nil { return nil, nil, err } @@ -68,17 +79,15 @@ func (s *ScansHTTPWrapper) GetByID(scanID string) (*scansApi.ScanResponseModel, } func (s *ScansHTTPWrapper) Delete(scanID string) (*scansApi.ErrorModel, error) { - client := &http.Client{} - req, err := http.NewRequest("DELETE", s.url+"/"+scanID, nil) + resp, err := SendHTTPRequest(http.MethodDelete, s.url+"/"+scanID, nil) if err != nil { return nil, err } - resp, err := client.Do(req) return handleScanResponseWithNoBody(resp, err, http.StatusOK) } func (s *ScansHTTPWrapper) Tags() (*[]string, *scansApi.ErrorModel, error) { - resp, err := http.Get(s.url + "/tags") + resp, err := SendHTTPRequest(http.MethodGet, s.url+"/tags", nil) if err != nil { return nil, nil, err } @@ -105,10 +114,3 @@ func (s *ScansHTTPWrapper) Tags() (*[]string, *scansApi.ErrorModel, error) { return nil, nil, errors.Errorf("Unknown response status code %d", resp.StatusCode) } } - -func NewHTTPScansWrapper(url string) ScansWrapper { - return &ScansHTTPWrapper{ - url: url, - contentType: "application/json", - } -} diff --git a/internal/wrappers/uploads-http.go b/internal/wrappers/uploads-http.go index e386b8513..10e2c68af 100644 --- a/internal/wrappers/uploads-http.go +++ b/internal/wrappers/uploads-http.go @@ -18,8 +18,7 @@ const ( ) type UploadsHTTPWrapper struct { - url string - credentials *Credentials + url string } func (u *UploadsHTTPWrapper) UploadFile(sourcesFile string) (*string, error) { @@ -43,7 +42,7 @@ func (u *UploadsHTTPWrapper) UploadFile(sourcesFile string) (*string, error) { } var req *http.Request - req, err = http.NewRequest("PUT", *preSignedURL, bytes.NewReader(fileBytes)) + req, err = http.NewRequest(http.MethodPut, *preSignedURL, bytes.NewReader(fileBytes)) if err != nil { return nil, errors.Errorf("Requesting error model failed - %s", err.Error()) } @@ -68,17 +67,7 @@ func (u *UploadsHTTPWrapper) UploadFile(sourcesFile string) (*string, error) { } func (u *UploadsHTTPWrapper) getPresignedURLForUploading() (*string, error) { - req, err := http.NewRequest("POST", u.url, nil) - if err != nil { - return nil, errors.Errorf("Requesting pre-signed URL failed - %s", err.Error()) - } - - var client = &http.Client{ - Timeout: time.Second * time.Duration(httpClientTimeout), - } - var resp *http.Response - - resp, err = client.Do(req) + resp, err := SendHTTPRequest(http.MethodPost, u.url, nil) if err != nil { return nil, errors.Errorf("Invoking HTTP request to get pre-signed URL failed - %s", err.Error()) } diff --git a/test/integration/project_test.go b/test/integration/project_test.go index da8fc0d35..356e8bb92 100644 --- a/test/integration/project_test.go +++ b/test/integration/project_test.go @@ -27,15 +27,15 @@ func TestProjectsE2E(t *testing.T) { func createProjectFromInputFile(t *testing.T) string { b := bytes.NewBufferString("") - createProjCommand := createASTIntegrationTestCommand() + createProjCommand := createASTIntegrationTestCommand(t) createProjCommand.SetOut(b) - err := execute(createProjCommand, "-v", "project", "create", "--inputFile", "project_payload.json") + err := execute(createProjCommand, "-v", "project", "create", "--input-file", "project_payload.json") return executeCreateProject(t, err, b) } func createProjectFromInput(t *testing.T, projectID string, tags []string) string { b := bytes.NewBufferString("") - createProjCommand := createASTIntegrationTestCommand() + createProjCommand := createASTIntegrationTestCommand(t) createProjCommand.SetOut(b) tagsJSON, err := json.Marshal(tags) assert.NilError(t, err, "Marshaling tags should pass") @@ -59,9 +59,9 @@ func executeCreateProject(t *testing.T, err error, b *bytes.Buffer) string { func getProjectByID(t *testing.T, projectID string) { b := bytes.NewBufferString("") - getProjectCommand := createASTIntegrationTestCommand() + getProjectCommand := createASTIntegrationTestCommand(t) getProjectCommand.SetOut(b) - err := execute(getProjectCommand, "-v", "project", "get", projectID) + err := execute(getProjectCommand, "-v", "project", "show", projectID) assert.NilError(t, err, "Getting a project should pass") // Read response from buffer var projectJSON []byte @@ -80,11 +80,11 @@ func getProjectByID(t *testing.T, projectID string) { func getAllProjects(t *testing.T, projectID string) { b := bytes.NewBufferString("") - getAllCommand := createASTIntegrationTestCommand() + getAllCommand := createASTIntegrationTestCommand(t) getAllCommand.SetOut(b) var limit uint64 = 40 var offset uint64 = 0 - err := execute(getAllCommand, "-v", "project", "get-all", "--limit", strconv.FormatUint(limit, 10), "--offset", strconv.FormatUint(offset, 10)) + err := execute(getAllCommand, "-v", "project", "list", "--limit", strconv.FormatUint(limit, 10), "--offset", strconv.FormatUint(offset, 10)) assert.NilError(t, err, "Getting all projects should pass") // Read response from buffer var getAllJSON []byte @@ -101,14 +101,14 @@ func getAllProjects(t *testing.T, projectID string) { } func deleteProject(t *testing.T, projectID string) { - deleteProjCommand := createASTIntegrationTestCommand() + deleteProjCommand := createASTIntegrationTestCommand(t) err := execute(deleteProjCommand, "-v", "project", "delete", projectID) assert.NilError(t, err, "Deleting a project should pass") } func getProjectTags(t *testing.T) { b := bytes.NewBufferString("") - tagsCommand := createASTIntegrationTestCommand() + tagsCommand := createASTIntegrationTestCommand(t) tagsCommand.SetOut(b) err := execute(tagsCommand, "-v", "project", "tags") assert.NilError(t, err, "Getting tags should pass") diff --git a/test/integration/result_test.go b/test/integration/result_test.go index 2f341045a..3a12389e7 100644 --- a/test/integration/result_test.go +++ b/test/integration/result_test.go @@ -13,20 +13,15 @@ import ( "gotest.tools/assert" ) -const ( - numOfFullScanResults = 575 - numOfIncScanResults = 572 -) - func getResultsNumberForScan(t *testing.T, scanID string) int { b := bytes.NewBufferString("") - getResultsCmd := createASTIntegrationTestCommand() + getResultsCmd := createASTIntegrationTestCommand(t) getResultsCmd.SetOut(b) var limit uint64 = 600 var offset uint64 = 0 l := strconv.FormatUint(limit, 10) o := strconv.FormatUint(offset, 10) - err := execute(getResultsCmd, "-v", "result", "get", scanID, "--limit", l, "--offset", o) + err := execute(getResultsCmd, "-v", "result", "list", scanID, "--limit", l, "--offset", o) assert.NilError(t, err, "Getting all results should pass") // Read response from buffer var getAllJSON []byte diff --git a/test/integration/root_test.go b/test/integration/root_test.go index 275af09c6..490df1608 100644 --- a/test/integration/root_test.go +++ b/test/integration/root_test.go @@ -8,24 +8,32 @@ import ( "github.com/checkmarxDev/ast-cli/internal/wrappers" "github.com/spf13/cobra" "github.com/spf13/viper" + "gotest.tools/assert" "log" "math/rand" "os" + "strings" "testing" "time" ) const ( - astSchema = "AST_SCHEMA" - astHost = "AST_HOST" - astPort = "80" - scansPath = "SCANS_PATH" - projectsPath = "PROJECTS_PATH" - resultsPath = "RESULTS_PATH" - uploadsPath = "UPLOADS_PATH" - letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" + astURIEnv = "AST_URI" + scansPathEnv = "SCANS_PATH" + projectsPathEnv = "PROJECTS_PATH" + resultsPathEnv = "RESULTS_PATH" + uploadsPathEnv = "UPLOADS_PATH" + letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" + successfulExitCode = 0 + failureExitCode = 1 ) +func bindKeyToEnvAndDefault(key, env, defaultVal string) error { + err := viper.BindEnv(key, env) + viper.SetDefault(key, defaultVal) + return err +} + func RandomizeString(length int) string { seededRand := rand.New(rand.NewSource(time.Now().UnixNano())) b := make([]byte, length) @@ -43,31 +51,38 @@ func TestMain(m *testing.M) { os.Exit(exitVal) } -func createASTIntegrationTestCommand() *cobra.Command { - log.Println("Reading env variables") - viper.AutomaticEnv() - viper.AddConfigPath(".") - viper.SetConfigName("config") - viper.SetConfigType("env") - _ = viper.ReadInConfig() - - viper.SetDefault(astSchema, "http") - viper.SetDefault(astHost, "localhost") - viper.SetDefault(astPort, "80") - viper.SetDefault(scansPath, "scans") - viper.SetDefault(projectsPath, "projects") - viper.SetDefault(uploadsPath, "uploads") - viper.SetDefault(resultsPath, "scan") - - schema := viper.GetString(astSchema) - host := viper.GetString(astHost) - port := viper.GetString(astPort) - ast := fmt.Sprintf("%s://%s:%s/api", schema, host, port) - - scans := viper.GetString(scansPath) - uploads := viper.GetString(uploadsPath) - projects := viper.GetString(projectsPath) - results := viper.GetString(resultsPath) +func createASTIntegrationTestCommand(t *testing.T) *cobra.Command { + astURIKey := strings.ToLower(astURIEnv) + err := bindKeyToEnvAndDefault(astURIKey, astURIEnv, "http://localhost:80") + assert.NilError(t, err) + ast := viper.GetString(astURIKey) + + scansPathKey := strings.ToLower(scansPathEnv) + err = bindKeyToEnvAndDefault(scansPathKey, scansPathEnv, "api/scans") + assert.NilError(t, err) + scans := viper.GetString(scansPathKey) + + projectsPathKey := strings.ToLower(projectsPathEnv) + err = bindKeyToEnvAndDefault(projectsPathKey, projectsPathEnv, "api/projects") + assert.NilError(t, err) + projects := viper.GetString(projectsPathKey) + + resultsPathKey := strings.ToLower(resultsPathEnv) + err = bindKeyToEnvAndDefault(resultsPathKey, resultsPathEnv, "api/results") + assert.NilError(t, err) + results := viper.GetString(resultsPathKey) + + uploadsPathKey := strings.ToLower(uploadsPathEnv) + err = bindKeyToEnvAndDefault(uploadsPathKey, uploadsPathEnv, "api/uploads") + assert.NilError(t, err) + uploads := viper.GetString(uploadsPathKey) + + err = bindKeyToEnvAndDefault(commands.AccessKeyIDConfigKey, commands.AccessKeyIDEnv, "") + assert.NilError(t, err) + err = bindKeyToEnvAndDefault(commands.AccessKeySecretConfigKey, commands.AccessKeySecretEnv, "") + assert.NilError(t, err) + err = bindKeyToEnvAndDefault(commands.AstAuthenticationURIConfigKey, commands.AstAuthenticationURIEnv, "") + assert.NilError(t, err) scansURL := fmt.Sprintf("%s/%s", ast, scans) uploadsURL := fmt.Sprintf("%s/%s", ast, uploads) @@ -79,7 +94,8 @@ func createASTIntegrationTestCommand() *cobra.Command { projectsWrapper := wrappers.NewHTTPProjectsWrapper(projectsURL) resultsWrapper := wrappers.NewHTTPResultsWrapper(resultsURL) - return commands.NewAstCLI(scansWrapper, uploadsWrapper, projectsWrapper, resultsWrapper) + astCli := commands.NewAstCLI(scansWrapper, uploadsWrapper, projectsWrapper, resultsWrapper) + return astCli } func execute(cmd *cobra.Command, args ...string) error { diff --git a/test/integration/scan_test.go b/test/integration/scan_test.go index b10f4394c..bd180b07a 100644 --- a/test/integration/scan_test.go +++ b/test/integration/scan_test.go @@ -36,7 +36,6 @@ func TestScansE2E(t *testing.T) { // Validate the results for full scan scanResults := getResultsNumberForScan(t, scanID) - assert.Assert(t, scanResults == numOfFullScanResults, "Wrong number of full scan results") incScanID := createIncScan(t) log.Printf("Waiting %d seconds for the incremental scan to complete...\n", incScanWaitTime) // Wait for the inc scan to finish. See it's completed successfully @@ -47,7 +46,7 @@ func TestScansE2E(t *testing.T) { // Validate the results for inc scan incScanResults := getResultsNumberForScan(t, incScanID) - assert.Assert(t, incScanResults == numOfIncScanResults, "Wrong number of inc scan results") + assert.Assert(t, incScanResults < scanResults, "Wrong number of inc scan results") getAllScans(t) getScansTags(t) @@ -56,9 +55,9 @@ func TestScansE2E(t *testing.T) { func createScanSourcesFile(t *testing.T) string { // Create a full scan b := bytes.NewBufferString("") - createCommand := createASTIntegrationTestCommand() + createCommand := createASTIntegrationTestCommand(t) createCommand.SetOut(b) - err := execute(createCommand, "-v", "scan", "create", "--inputFile", "scan_payload.json", "--sources", "sources.zip") + err := execute(createCommand, "-v", "scan", "create", "--input-file", "scan_payload.json", "--sources", "sources.zip") assert.NilError(t, err, "Creating a scan should pass") // Read response from buffer var createdScanJSON []byte @@ -78,13 +77,13 @@ func deleteScan(t *testing.T) { func getAllScans(t *testing.T) { b := bytes.NewBufferString("") - getAllCommand := createASTIntegrationTestCommand() + getAllCommand := createASTIntegrationTestCommand(t) getAllCommand.SetOut(b) var limit uint64 = 40 var offset uint64 = 0 l := strconv.FormatUint(limit, 10) o := strconv.FormatUint(offset, 10) - err := execute(getAllCommand, "-v", "scan", "get-all", "--limit", l, "--offset", o) + err := execute(getAllCommand, "-v", "scan", "list", "--limit", l, "--offset", o) assert.NilError(t, err, "Getting all scans should pass") // Read response from buffer var getAllJSON []byte @@ -101,9 +100,9 @@ func getAllScans(t *testing.T) { func getScanByID(t *testing.T, scanID string) *scansRESTApi.ScanResponseModel { getBuffer := bytes.NewBufferString("") - getCommand := createASTIntegrationTestCommand() + getCommand := createASTIntegrationTestCommand(t) getCommand.SetOut(getBuffer) - err := execute(getCommand, "-v", "scan", "get", scanID) + err := execute(getCommand, "-v", "scan", "show", scanID) assert.NilError(t, err) // Read response from buffer var getScanJSON []byte @@ -118,7 +117,7 @@ func getScanByID(t *testing.T, scanID string) *scansRESTApi.ScanResponseModel { func getScansTags(t *testing.T) { b := bytes.NewBufferString("") - tagsCommand := createASTIntegrationTestCommand() + tagsCommand := createASTIntegrationTestCommand(t) tagsCommand.SetOut(b) err := execute(tagsCommand, "-v", "scan", "tags") assert.NilError(t, err, "Getting tags should pass") @@ -136,9 +135,9 @@ func getScansTags(t *testing.T) { func createIncScan(t *testing.T) string { // Create an incremental scan incBuff := bytes.NewBufferString("") - createIncCommand := createASTIntegrationTestCommand() + createIncCommand := createASTIntegrationTestCommand(t) createIncCommand.SetOut(incBuff) - err := execute(createIncCommand, "-v", "scan", "create", "--inputFile", "scan_inc_payload.json", "--sources", "sources_inc.zip") + err := execute(createIncCommand, "-v", "scan", "create", "--input-file", "scan_inc_payload.json", "--sources", "sources_inc.zip") assert.NilError(t, err, "Creating an incremental scan should pass") // Read response from buffer var createdIncScanJSON []byte