From 6b4f8a5f0e1623ff915ba787c247e9717afb8e58 Mon Sep 17 00:00:00 2001 From: Daniel Amsellem Date: Sun, 12 Apr 2020 11:12:32 +0300 Subject: [PATCH 01/11] Code review changes --- internal/wrappers/results-http.go | 3 ++- test/integration/result_test.go | 5 ----- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/internal/wrappers/results-http.go b/internal/wrappers/results-http.go index 449aef36a..12088dbfc 100644 --- a/internal/wrappers/results-http.go +++ b/internal/wrappers/results-http.go @@ -2,6 +2,7 @@ package wrappers import ( "encoding/json" + "fmt" "net/http" "github.com/pkg/errors" @@ -25,7 +26,7 @@ func NewHTTPResultsWrapper(url string) ResultsWrapper { } func (r *ResultsHTTPWrapper) GetByScanID(scanID string, limit, offset uint64) ([]ResultResponseModel, *ResultError, error) { - resp, err := getRequestWithLimitAndOffset(r.url+"/"+scanID+"/items", limit, offset) + resp, err := getRequestWithLimitAndOffset(fmt.Sprintf("%s/%s/items", r.url, scanID), limit, offset) if err != nil { return nil, nil, err } diff --git a/test/integration/result_test.go b/test/integration/result_test.go index 2f341045a..6c11539a5 100644 --- a/test/integration/result_test.go +++ b/test/integration/result_test.go @@ -13,11 +13,6 @@ import ( "gotest.tools/assert" ) -const ( - numOfFullScanResults = 575 - numOfIncScanResults = 572 -) - func getResultsNumberForScan(t *testing.T, scanID string) int { b := bytes.NewBufferString("") getResultsCmd := createASTIntegrationTestCommand() From b760ac63bf084edd87c29414fbf4acdacc852c85 Mon Sep 17 00:00:00 2001 From: Daniel Amsellem Date: Sun, 12 Apr 2020 11:28:09 +0300 Subject: [PATCH 02/11] Integration tests changes --- test/integration/scan_test.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/test/integration/scan_test.go b/test/integration/scan_test.go index b10f4394c..9ae31641c 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) From f5e92df2b1bc0c4a8bdcbdd5071acd59cd8085c8 Mon Sep 17 00:00:00 2001 From: Daniel Amsellem Date: Sun, 12 Apr 2020 11:33:14 +0300 Subject: [PATCH 03/11] Bug fix in scans --- cmd/main.go | 2 +- internal/commands/scan.go | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/cmd/main.go b/cmd/main.go index 7a3a7492d..02063d421 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -104,4 +104,4 @@ func bindKeyToEnvAndDefault(key, env, defaultVal string) error { // 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" scan get 4d9a9189-ddcc-4aa0-ba2f-9d6d7f92eceb diff --git a/internal/commands/scan.go b/internal/commands/scan.go index c70418b36..583e7ee62 100644 --- a/internal/commands/scan.go +++ b/internal/commands/scan.go @@ -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 } From 9a695e476c4aff2b07b217495e637274cd7f1256 Mon Sep 17 00:00:00 2001 From: Daniel Amsellem Date: Sun, 12 Apr 2020 18:59:57 +0300 Subject: [PATCH 04/11] Refactoring and name changes --- cmd/main.go | 5 +- internal/commands/project.go | 1 + internal/commands/project_test.go | 4 +- internal/commands/result.go | 1 + internal/commands/root.go | 67 +++++++++------------ internal/commands/scan_test.go | 4 +- internal/wrappers/credentials.go | 60 +++++++++++++++++-- internal/wrappers/projects-http.go | 32 ++++++++-- internal/wrappers/results-http.go | 4 +- internal/wrappers/results-mock.go | 3 +- internal/wrappers/scans-http.go | 55 +++++++++++++---- internal/wrappers/uploads-http.go | 3 +- test/integration/project_test.go | 14 ++--- test/integration/result_test.go | 2 +- test/integration/root_test.go | 96 ++++++++++++++++++++---------- test/integration/scan_test.go | 14 ++--- 16 files changed, 249 insertions(+), 116 deletions(-) diff --git a/cmd/main.go b/cmd/main.go index 02063d421..ca8fd997a 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -12,6 +12,7 @@ import ( ) const ( + // TODO change to URI for AST astSchemaEnv = "AST_SCHEMA" astHostEnv = "AST_HOST" astPortEnv = "AST_PORT" @@ -65,7 +66,7 @@ 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) @@ -103,5 +104,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" -v scan create --input-file ./internal/commands/payloads/uploads.json --sources ./internal/commands/payloads/sources.zip // "bin/ast.exe" scan get 4d9a9189-ddcc-4aa0-ba2f-9d6d7f92eceb diff --git a/internal/commands/project.go b/internal/commands/project.go index 5e544ec97..fb49847c0 100644 --- a/internal/commands/project.go +++ b/internal/commands/project.go @@ -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..1f1005fc8 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) } diff --git a/internal/commands/result.go b/internal/commands/result.go index 600168cc1..2b98ded8a 100644 --- a/internal/commands/result.go +++ b/internal/commands/result.go @@ -41,6 +41,7 @@ func runGetResultByScanIDCommand(resultsWrapper wrappers.ResultsWrapper) func(cm } scanID := args[0] limit, offset := getLimitAndOffset(cmd) + resultResponseModel, errorModel, err = resultsWrapper.GetByScanID(scanID, limit, offset) if err != nil { return errors.Wrapf(err, "%s", failedGettingResults) diff --git a/internal/commands/root.go b/internal/commands/root.go index 9b1261abc..164d2f4af 100644 --- a/internal/commands/root.go +++ b/internal/commands/root.go @@ -10,34 +10,36 @@ 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" ) 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,6 +55,7 @@ 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) // 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 @@ -62,6 +65,7 @@ func NewAstCLI(scansWrapper wrappers.ScansWrapper, // 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)) scanCmd := NewScanCommand(scansWrapper, uploadsWrapper) projectCmd := NewProjectCommand(projectsWrapper) @@ -73,19 +77,6 @@ func NewAstCLI(scansWrapper wrappers.ScansWrapper, 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_test.go b/internal/commands/scan_test.go index cbe0f6963..4ef9b32dd 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) } diff --git a/internal/wrappers/credentials.go b/internal/wrappers/credentials.go index 8a05019d7..d033b314d 100644 --- a/internal/wrappers/credentials.go +++ b/internal/wrappers/credentials.go @@ -1,7 +1,59 @@ package wrappers -type Credentials struct { - AuthenticationHost string - AccessKeyID string - AccessKeySecret string +import ( + "encoding/json" + "fmt" + "io/ioutil" + "net/http" + "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 getClientCredentials(authServerURI, accessKeyID, accessKeySecret string) (*ClientCredentialsInfo, error) { + payload := strings.NewReader(getCredentialsPayload(accessKeyID, accessKeySecret)) + req, err := http.NewRequest("POST", 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) +} + +func GetRequestWithCredentials(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 } diff --git a/internal/wrappers/projects-http.go b/internal/wrappers/projects-http.go index e405281e6..338b084f5 100644 --- a/internal/wrappers/projects-http.go +++ b/internal/wrappers/projects-http.go @@ -19,7 +19,6 @@ const ( type ProjectsHTTPWrapper struct { url string contentType string - credentials *Credentials } func NewHTTPProjectsWrapper(url string) ProjectsWrapper { @@ -37,7 +36,16 @@ func (p *ProjectsHTTPWrapper) Create(model *projectsRESTApi.Project) ( return nil, nil, err } - resp, err := http.Post(p.url, p.contentType, bytes.NewBuffer(jsonBytes)) + client := &http.Client{} + req, err := http.NewRequest(http.MethodPost, p.url, bytes.NewBuffer(jsonBytes)) + if err != nil { + return nil, nil, err + } + + resp, err := client.Do(req) + if err != nil { + return nil, nil, err + } return handleProjectResponseWithBody(resp, err, http.StatusCreated) } @@ -76,7 +84,13 @@ func (p *ProjectsHTTPWrapper) GetByID(projectID string) ( *projectsRESTApi.ProjectResponseModel, *projectsRESTApi.ErrorModel, error) { - resp, err := http.Get(p.url + "/" + projectID) + client := &http.Client{} + req, err := http.NewRequest("GET", p.url+"/"+projectID, nil) + if err != nil { + return nil, nil, err + } + + resp, err := client.Do(req) if err != nil { return nil, nil, err } @@ -99,7 +113,13 @@ func (p *ProjectsHTTPWrapper) Tags() ( *[]string, *projectsRESTApi.ErrorModel, error) { - resp, err := http.Get(p.url + "/tags") + client := &http.Client{} + req, err := http.NewRequest("GET", p.url+"/tags", nil) + if err != nil { + return nil, nil, err + } + + resp, err := client.Do(req) if err != nil { return nil, nil, err } @@ -141,6 +161,10 @@ func getRequestWithLimitAndOffset(url string, limit, offset uint64) (*http.Respo q.Add(offsetQueryParam, strconv.FormatUint(offset, 10)) } req.URL.RawQuery = q.Encode() + req, err = GetRequestWithCredentials(req) + if err != nil { + return nil, err + } resp, err := client.Do(req) return resp, err } diff --git a/internal/wrappers/results-http.go b/internal/wrappers/results-http.go index 12088dbfc..98f7d304e 100644 --- a/internal/wrappers/results-http.go +++ b/internal/wrappers/results-http.go @@ -15,7 +15,6 @@ const ( type ResultsHTTPWrapper struct { url string contentType string - credentials *Credentials } func NewHTTPResultsWrapper(url string) ResultsWrapper { @@ -25,7 +24,8 @@ func NewHTTPResultsWrapper(url string) ResultsWrapper { } } -func (r *ResultsHTTPWrapper) GetByScanID(scanID string, limit, offset uint64) ([]ResultResponseModel, *ResultError, error) { +func (r *ResultsHTTPWrapper) GetByScanID(scanID string, + limit, offset uint64) ([]ResultResponseModel, *ResultError, error) { resp, err := getRequestWithLimitAndOffset(fmt.Sprintf("%s/%s/items", r.url, scanID), limit, offset) if err != nil { return nil, nil, err 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..fce84471e 100644 --- a/internal/wrappers/scans-http.go +++ b/internal/wrappers/scans-http.go @@ -17,7 +17,13 @@ const ( 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,8 +31,21 @@ func (s *ScansHTTPWrapper) Create(model *scansApi.Scan) (*scansApi.ScanResponseM if err != nil { return nil, nil, err } + client := &http.Client{} + req, err := http.NewRequest(http.MethodPost, s.url, bytes.NewBuffer(jsonBytes)) + if err != nil { + return nil, nil, err + } + req, err = GetRequestWithCredentials(req) + if err != nil { + return nil, nil, err + } + + resp, err := client.Do(req) + if err != nil { + return nil, nil, err + } - resp, err := http.Post(s.url, s.contentType, bytes.NewBuffer(jsonBytes)) return handleScanResponseWithBody(resp, err, http.StatusCreated) } @@ -60,10 +79,16 @@ 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) + client := &http.Client{} + req, err := http.NewRequest(http.MethodGet, s.url+"/"+scanID, nil) + if err != nil { + return nil, nil, err + } + req, err = GetRequestWithCredentials(req) if err != nil { return nil, nil, err } + resp, err := client.Do(req) return handleScanResponseWithBody(resp, err, http.StatusOK) } @@ -73,12 +98,27 @@ func (s *ScansHTTPWrapper) Delete(scanID string) (*scansApi.ErrorModel, error) { if err != nil { return nil, err } + req, err = GetRequestWithCredentials(req) + 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") + client := &http.Client{} + req, err := http.NewRequest("GET", s.url+"/tags", nil) + if err != nil { + return nil, nil, err + } + + req, err = GetRequestWithCredentials(req) + if err != nil { + return nil, nil, err + } + + resp, err := client.Do(req) if err != nil { return nil, nil, err } @@ -105,10 +145,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..70ca22016 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) { diff --git a/test/integration/project_test.go b/test/integration/project_test.go index da8fc0d35..a47942446 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,7 +59,7 @@ 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) assert.NilError(t, err, "Getting a project should pass") @@ -80,7 +80,7 @@ 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 @@ -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 6c11539a5..ac5f5cae8 100644 --- a/test/integration/result_test.go +++ b/test/integration/result_test.go @@ -15,7 +15,7 @@ import ( 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 diff --git a/test/integration/root_test.go b/test/integration/root_test.go index 275af09c6..77bb024ae 100644 --- a/test/integration/root_test.go +++ b/test/integration/root_test.go @@ -8,24 +8,34 @@ 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" + astSchemaEnv = "AST_SCHEMA" + astHostEnv = "AST_HOST" + astPortEnv = "AST_PORT" + 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 +53,50 @@ 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) +func createASTIntegrationTestCommand(t *testing.T) *cobra.Command { + astSchemaKey := strings.ToLower(astSchemaEnv) + err := bindKeyToEnvAndDefault(astSchemaKey, astSchemaEnv, "http") + assert.NilError(t, err) + schema := viper.GetString(astSchemaKey) + + astHostKey := strings.ToLower(astHostEnv) + err = bindKeyToEnvAndDefault(astHostKey, astHostEnv, "localhost") + assert.NilError(t, err) + host := viper.GetString(astHostKey) + + astPortKey := strings.ToLower(astPortEnv) + err = bindKeyToEnvAndDefault(astPortKey, astPortEnv, "80") + assert.NilError(t, err) + port := viper.GetString(astPortKey) + + scansPathKey := strings.ToLower(scansPathEnv) + err = bindKeyToEnvAndDefault(scansPathKey, scansPathEnv, "scans") + assert.NilError(t, err) + scans := viper.GetString(scansPathKey) - scans := viper.GetString(scansPath) - uploads := viper.GetString(uploadsPath) - projects := viper.GetString(projectsPath) - results := viper.GetString(resultsPath) + projectsPathKey := strings.ToLower(projectsPathEnv) + err = bindKeyToEnvAndDefault(projectsPathKey, projectsPathEnv, "projects") + assert.NilError(t, err) + projects := viper.GetString(projectsPathKey) + + resultsPathKey := strings.ToLower(resultsPathEnv) + err = bindKeyToEnvAndDefault(resultsPathKey, resultsPathEnv, "results") + assert.NilError(t, err) + results := viper.GetString(resultsPathKey) + + uploadsPathKey := strings.ToLower(uploadsPathEnv) + err = bindKeyToEnvAndDefault(uploadsPathKey, uploadsPathEnv, "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) + + ast := fmt.Sprintf("%s://%s:%s/api", schema, host, port) scansURL := fmt.Sprintf("%s/%s", ast, scans) uploadsURL := fmt.Sprintf("%s/%s", ast, uploads) @@ -79,7 +108,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 9ae31641c..f7be7eea5 100644 --- a/test/integration/scan_test.go +++ b/test/integration/scan_test.go @@ -55,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 @@ -77,7 +77,7 @@ 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 @@ -100,7 +100,7 @@ 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) assert.NilError(t, err) @@ -117,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") @@ -135,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 From daf5a304e26955c77792db5723029eb62228a3fb Mon Sep 17 00:00:00 2001 From: Daniel Amsellem Date: Mon, 13 Apr 2020 09:56:08 +0300 Subject: [PATCH 05/11] Consolidated multiple url parts to one --- cmd/main.go | 33 +++++++++------------------------ test/integration/root_test.go | 30 ++++++++---------------------- 2 files changed, 17 insertions(+), 46 deletions(-) diff --git a/cmd/main.go b/cmd/main.go index ca8fd997a..57ea01a31 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -12,10 +12,7 @@ import ( ) const ( - // TODO change to URI for AST - astSchemaEnv = "AST_SCHEMA" - astHostEnv = "AST_HOST" - astPortEnv = "AST_PORT" + astURIEnv = "AST_URI" scansPathEnv = "SCANS_PATH" projectsPathEnv = "PROJECTS_PATH" resultsPathEnv = "RESULTS_PATH" @@ -26,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) @@ -69,8 +56,6 @@ func main() { 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) diff --git a/test/integration/root_test.go b/test/integration/root_test.go index 77bb024ae..490df1608 100644 --- a/test/integration/root_test.go +++ b/test/integration/root_test.go @@ -18,9 +18,7 @@ import ( ) const ( - astSchemaEnv = "AST_SCHEMA" - astHostEnv = "AST_HOST" - astPortEnv = "AST_PORT" + astURIEnv = "AST_URI" scansPathEnv = "SCANS_PATH" projectsPathEnv = "PROJECTS_PATH" resultsPathEnv = "RESULTS_PATH" @@ -54,38 +52,28 @@ func TestMain(m *testing.M) { } func createASTIntegrationTestCommand(t *testing.T) *cobra.Command { - astSchemaKey := strings.ToLower(astSchemaEnv) - err := bindKeyToEnvAndDefault(astSchemaKey, astSchemaEnv, "http") + astURIKey := strings.ToLower(astURIEnv) + err := bindKeyToEnvAndDefault(astURIKey, astURIEnv, "http://localhost:80") assert.NilError(t, err) - schema := viper.GetString(astSchemaKey) - - astHostKey := strings.ToLower(astHostEnv) - err = bindKeyToEnvAndDefault(astHostKey, astHostEnv, "localhost") - assert.NilError(t, err) - host := viper.GetString(astHostKey) - - astPortKey := strings.ToLower(astPortEnv) - err = bindKeyToEnvAndDefault(astPortKey, astPortEnv, "80") - assert.NilError(t, err) - port := viper.GetString(astPortKey) + ast := viper.GetString(astURIKey) scansPathKey := strings.ToLower(scansPathEnv) - err = bindKeyToEnvAndDefault(scansPathKey, scansPathEnv, "scans") + err = bindKeyToEnvAndDefault(scansPathKey, scansPathEnv, "api/scans") assert.NilError(t, err) scans := viper.GetString(scansPathKey) projectsPathKey := strings.ToLower(projectsPathEnv) - err = bindKeyToEnvAndDefault(projectsPathKey, projectsPathEnv, "projects") + err = bindKeyToEnvAndDefault(projectsPathKey, projectsPathEnv, "api/projects") assert.NilError(t, err) projects := viper.GetString(projectsPathKey) resultsPathKey := strings.ToLower(resultsPathEnv) - err = bindKeyToEnvAndDefault(resultsPathKey, resultsPathEnv, "results") + err = bindKeyToEnvAndDefault(resultsPathKey, resultsPathEnv, "api/results") assert.NilError(t, err) results := viper.GetString(resultsPathKey) uploadsPathKey := strings.ToLower(uploadsPathEnv) - err = bindKeyToEnvAndDefault(uploadsPathKey, uploadsPathEnv, "uploads") + err = bindKeyToEnvAndDefault(uploadsPathKey, uploadsPathEnv, "api/uploads") assert.NilError(t, err) uploads := viper.GetString(uploadsPathKey) @@ -96,8 +84,6 @@ func createASTIntegrationTestCommand(t *testing.T) *cobra.Command { err = bindKeyToEnvAndDefault(commands.AstAuthenticationURIConfigKey, commands.AstAuthenticationURIEnv, "") assert.NilError(t, 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) From 94cce8aafd89b92c2b0d1721f42edc7486fbcfd0 Mon Sep 17 00:00:00 2001 From: Daniel Amsellem Date: Mon, 13 Apr 2020 16:36:22 +0300 Subject: [PATCH 06/11] Added insecure option --- internal/commands/root.go | 8 +- internal/wrappers/client.go | 114 +++++++++++++++++++++++++++++ internal/wrappers/credentials.go | 59 --------------- internal/wrappers/projects-http.go | 62 ++-------------- internal/wrappers/results-http.go | 16 ++-- internal/wrappers/scans-http.go | 41 ++--------- internal/wrappers/uploads-http.go | 14 +--- 7 files changed, 143 insertions(+), 171 deletions(-) create mode 100644 internal/wrappers/client.go delete mode 100644 internal/wrappers/credentials.go diff --git a/internal/commands/root.go b/internal/commands/root.go index 164d2f4af..2dbbd3f85 100644 --- a/internal/commands/root.go +++ b/internal/commands/root.go @@ -34,6 +34,8 @@ const ( AstAuthenticationURIEnv = "AST_AUTHENTICATION_URI" astAuthenticationURIFlag = "auth-uri" astAuthenticationURIFlagUsage = "The authentication URI for AST" + insecureFlag = "insecure" + insecureFlagUsage = "Ignore TLS certificate validations" ) var ( @@ -56,16 +58,16 @@ func NewAstCLI(scansWrapper wrappers.ScansWrapper, 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) 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 d033b314d..000000000 --- a/internal/wrappers/credentials.go +++ /dev/null @@ -1,59 +0,0 @@ -package wrappers - -import ( - "encoding/json" - "fmt" - "io/ioutil" - "net/http" - "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 getClientCredentials(authServerURI, accessKeyID, accessKeySecret string) (*ClientCredentialsInfo, error) { - payload := strings.NewReader(getCredentialsPayload(accessKeyID, accessKeySecret)) - req, err := http.NewRequest("POST", 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) -} - -func GetRequestWithCredentials(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 -} diff --git a/internal/wrappers/projects-http.go b/internal/wrappers/projects-http.go index 338b084f5..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,14 +16,12 @@ const ( ) type ProjectsHTTPWrapper struct { - url string - contentType string + url string } func NewHTTPProjectsWrapper(url string) ProjectsWrapper { return &ProjectsHTTPWrapper{ - url: url, - contentType: "application/json", + url: url, } } @@ -36,13 +33,7 @@ func (p *ProjectsHTTPWrapper) Create(model *projectsRESTApi.Project) ( return nil, nil, err } - client := &http.Client{} - req, err := http.NewRequest(http.MethodPost, p.url, bytes.NewBuffer(jsonBytes)) - if err != nil { - return nil, nil, err - } - - resp, err := client.Do(req) + resp, err := SendHTTPRequest(http.MethodPost, p.url, bytes.NewBuffer(jsonBytes)) if err != nil { return nil, nil, err } @@ -52,7 +43,7 @@ func (p *ProjectsHTTPWrapper) Create(model *projectsRESTApi.Project) ( 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 } @@ -84,28 +75,18 @@ func (p *ProjectsHTTPWrapper) GetByID(projectID string) ( *projectsRESTApi.ProjectResponseModel, *projectsRESTApi.ErrorModel, error) { - client := &http.Client{} - req, err := http.NewRequest("GET", p.url+"/"+projectID, nil) - if err != nil { - return nil, nil, err - } - - resp, err := client.Do(req) + 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) } @@ -113,16 +94,11 @@ func (p *ProjectsHTTPWrapper) Tags() ( *[]string, *projectsRESTApi.ErrorModel, error) { - client := &http.Client{} - req, err := http.NewRequest("GET", p.url+"/tags", nil) + resp, err := SendHTTPRequest(http.MethodGet, p.url+"/tags", nil) if err != nil { return nil, nil, err } - resp, err := client.Do(req) - if err != nil { - return nil, nil, err - } decoder := json.NewDecoder(resp.Body) defer resp.Body.Close() @@ -146,25 +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() - req, err = GetRequestWithCredentials(req) - if err != nil { - return nil, err - } - resp, err := client.Do(req) - return resp, err -} diff --git a/internal/wrappers/results-http.go b/internal/wrappers/results-http.go index 98f7d304e..638b6c9fc 100644 --- a/internal/wrappers/results-http.go +++ b/internal/wrappers/results-http.go @@ -13,23 +13,25 @@ const ( ) type ResultsHTTPWrapper struct { - url string - contentType string + 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(fmt.Sprintf("%s/%s/items", r.url, scanID), 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/scans-http.go b/internal/wrappers/scans-http.go index fce84471e..5e9e30e52 100644 --- a/internal/wrappers/scans-http.go +++ b/internal/wrappers/scans-http.go @@ -31,26 +31,18 @@ func (s *ScansHTTPWrapper) Create(model *scansApi.Scan) (*scansApi.ScanResponseM if err != nil { return nil, nil, err } - client := &http.Client{} - req, err := http.NewRequest(http.MethodPost, s.url, bytes.NewBuffer(jsonBytes)) + resp, err := SendHTTPRequest(http.MethodPost, s.url, bytes.NewBuffer(jsonBytes)) if err != nil { return nil, nil, err } - req, err = GetRequestWithCredentials(req) if err != nil { return nil, nil, err } - - resp, err := client.Do(req) - 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 } @@ -79,46 +71,23 @@ func (s *ScansHTTPWrapper) Get(limit, offset uint64) (*scansApi.SlicedScansRespo } func (s *ScansHTTPWrapper) GetByID(scanID string) (*scansApi.ScanResponseModel, *scansApi.ErrorModel, error) { - client := &http.Client{} - req, err := http.NewRequest(http.MethodGet, s.url+"/"+scanID, nil) + resp, err := SendHTTPRequest(http.MethodGet, s.url+"/"+scanID, nil) if err != nil { return nil, nil, err } - req, err = GetRequestWithCredentials(req) - if err != nil { - return nil, nil, err - } - resp, err := client.Do(req) return handleScanResponseWithBody(resp, err, http.StatusOK) } 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 } - req, err = GetRequestWithCredentials(req) - 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) { - client := &http.Client{} - req, err := http.NewRequest("GET", s.url+"/tags", nil) - if err != nil { - return nil, nil, err - } - - req, err = GetRequestWithCredentials(req) - if err != nil { - return nil, nil, err - } - - resp, err := client.Do(req) + resp, err := SendHTTPRequest(http.MethodGet, s.url+"/tags", nil) if err != nil { return nil, nil, err } diff --git a/internal/wrappers/uploads-http.go b/internal/wrappers/uploads-http.go index 70ca22016..10e2c68af 100644 --- a/internal/wrappers/uploads-http.go +++ b/internal/wrappers/uploads-http.go @@ -42,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()) } @@ -67,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()) } From fe7960042493b7ea2c431166d2758daaf41fd7fb Mon Sep 17 00:00:00 2001 From: Daniel Amsellem Date: Mon, 13 Apr 2020 16:49:27 +0300 Subject: [PATCH 07/11] Renamed get-all to list --- internal/commands/project.go | 16 ++++++++-------- internal/commands/project_test.go | 12 ++++++------ internal/commands/scan.go | 12 ++++++------ internal/commands/scan_test.go | 4 ++-- internal/wrappers/scans-http.go | 2 +- test/integration/project_test.go | 2 +- test/integration/scan_test.go | 2 +- 7 files changed, 25 insertions(+), 25 deletions(-) diff --git a/internal/commands/project.go b/internal/commands/project.go index fb49847c0..67ce90774 100644 --- a/internal/commands/project.go +++ b/internal/commands/project.go @@ -36,13 +36,13 @@ 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", @@ -62,7 +62,7 @@ func NewProjectCommand(projectsWrapper wrappers.ProjectsWrapper) *cobra.Command RunE: runGetProjectsTagsCommand(projectsWrapper), } - projCmd.AddCommand(createProjCmd, getProjCmd, getAllProjCmd, deleteProjCmd, tagsCmd) + projCmd.AddCommand(createProjCmd, getProjCmd, 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 diff --git a/internal/commands/project_test.go b/internal/commands/project_test.go index 1f1005fc8..81af6905d 100644 --- a/internal/commands/project_test.go +++ b/internal/commands/project_test.go @@ -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/scan.go b/internal/commands/scan.go index 583e7ee62..bc817cad4 100644 --- a/internal/commands/scan.go +++ b/internal/commands/scan.go @@ -41,13 +41,13 @@ 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", @@ -67,7 +67,7 @@ func NewScanCommand(scansWrapper wrappers.ScansWrapper, uploadsWrapper wrappers. RunE: runGetTagsCommand(scansWrapper), } - scanCmd.AddCommand(createScanCmd, getScanCmd, getAllScansCmd, deleteScanCmd, tagsCmd) + scanCmd.AddCommand(createScanCmd, getScanCmd, listScansCmd, deleteScanCmd, tagsCmd) return scanCmd } diff --git a/internal/commands/scan_test.go b/internal/commands/scan_test.go index 4ef9b32dd..a34ce3bd3 100644 --- a/internal/commands/scan_test.go +++ b/internal/commands/scan_test.go @@ -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/scans-http.go b/internal/wrappers/scans-http.go index 5e9e30e52..29f49c025 100644 --- a/internal/wrappers/scans-http.go +++ b/internal/wrappers/scans-http.go @@ -10,7 +10,7 @@ import ( ) const ( - failedToParseGetAll = "Failed to parse get-all response" + failedToParseGetAll = "Failed to parse list response" failedToParseTags = "Failed to parse tags response" ) diff --git a/test/integration/project_test.go b/test/integration/project_test.go index a47942446..f9d0b363e 100644 --- a/test/integration/project_test.go +++ b/test/integration/project_test.go @@ -84,7 +84,7 @@ func getAllProjects(t *testing.T, projectID string) { 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 diff --git a/test/integration/scan_test.go b/test/integration/scan_test.go index f7be7eea5..4e6e9f32c 100644 --- a/test/integration/scan_test.go +++ b/test/integration/scan_test.go @@ -83,7 +83,7 @@ func getAllScans(t *testing.T) { 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 From ca91e70300097a32c9341780bf5cc50e73c8a6c3 Mon Sep 17 00:00:00 2001 From: Daniel Amsellem Date: Mon, 13 Apr 2020 17:03:14 +0300 Subject: [PATCH 08/11] Renamed get to show --- cmd/main.go | 2 +- internal/commands/project.go | 8 ++++---- internal/commands/project_test.go | 4 ++-- internal/commands/result.go | 22 +++++++++++----------- internal/commands/result_test.go | 6 +++--- internal/commands/scan.go | 8 ++++---- internal/commands/scan_test.go | 6 +++--- internal/wrappers/results-http.go | 2 +- test/integration/project_test.go | 2 +- test/integration/result_test.go | 2 +- test/integration/scan_test.go | 2 +- 11 files changed, 32 insertions(+), 32 deletions(-) diff --git a/cmd/main.go b/cmd/main.go index 57ea01a31..56c96566d 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -90,4 +90,4 @@ func bindKeyToEnvAndDefault(key, env, defaultVal string) error { // 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 --input-file ./internal/commands/payloads/uploads.json --sources ./internal/commands/payloads/sources.zip -// "bin/ast.exe" scan get 4d9a9189-ddcc-4aa0-ba2f-9d6d7f92eceb +// "bin/ast.exe" scan list 4d9a9189-ddcc-4aa0-ba2f-9d6d7f92eceb diff --git a/internal/commands/project.go b/internal/commands/project.go index 67ce90774..5138d58fa 100644 --- a/internal/commands/project.go +++ b/internal/commands/project.go @@ -44,9 +44,9 @@ func NewProjectCommand(projectsWrapper wrappers.ProjectsWrapper) *cobra.Command 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, listProjectsCmd, deleteProjCmd, tagsCmd) + projCmd.AddCommand(createProjCmd, showProjectCmd, listProjectsCmd, deleteProjCmd, tagsCmd) return projCmd } diff --git a/internal/commands/project_test.go b/internal/commands/project_test.go index 81af6905d..00fafcb86 100644 --- a/internal/commands/project_test.go +++ b/internal/commands/project_test.go @@ -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) { diff --git a/internal/commands/result.go b/internal/commands/result.go index 2b98ded8a..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,23 +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/scan.go b/internal/commands/scan.go index bc817cad4..e57a30304 100644 --- a/internal/commands/scan.go +++ b/internal/commands/scan.go @@ -49,9 +49,9 @@ func NewScanCommand(scansWrapper wrappers.ScansWrapper, uploadsWrapper wrappers. 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, listScansCmd, deleteScanCmd, tagsCmd) + scanCmd.AddCommand(createScanCmd, showScanCmd, listScansCmd, deleteScanCmd, tagsCmd) return scanCmd } diff --git a/internal/commands/scan_test.go b/internal/commands/scan_test.go index a34ce3bd3..9a59c2c14 100644 --- a/internal/commands/scan_test.go +++ b/internal/commands/scan_test.go @@ -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) { diff --git a/internal/wrappers/results-http.go b/internal/wrappers/results-http.go index 638b6c9fc..a2631d890 100644 --- a/internal/wrappers/results-http.go +++ b/internal/wrappers/results-http.go @@ -9,7 +9,7 @@ import ( ) const ( - failedToParseGetResults = "Failed to parse get results" + failedToParseGetResults = "Failed to parse list results" ) type ResultsHTTPWrapper struct { diff --git a/test/integration/project_test.go b/test/integration/project_test.go index f9d0b363e..356e8bb92 100644 --- a/test/integration/project_test.go +++ b/test/integration/project_test.go @@ -61,7 +61,7 @@ func getProjectByID(t *testing.T, projectID string) { b := bytes.NewBufferString("") 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 diff --git a/test/integration/result_test.go b/test/integration/result_test.go index ac5f5cae8..3a12389e7 100644 --- a/test/integration/result_test.go +++ b/test/integration/result_test.go @@ -21,7 +21,7 @@ func getResultsNumberForScan(t *testing.T, scanID string) int { 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/scan_test.go b/test/integration/scan_test.go index 4e6e9f32c..bd180b07a 100644 --- a/test/integration/scan_test.go +++ b/test/integration/scan_test.go @@ -102,7 +102,7 @@ func getScanByID(t *testing.T, scanID string) *scansRESTApi.ScanResponseModel { getBuffer := bytes.NewBufferString("") 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 From a0e57ae260b30f7982d9be806db5c8fd86e0ca0d Mon Sep 17 00:00:00 2001 From: ronenl2 Date: Mon, 13 Apr 2020 21:09:43 +0300 Subject: [PATCH 09/11] Add cluster command --- internal/commands/cluster.go | 39 ++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 internal/commands/cluster.go diff --git a/internal/commands/cluster.go b/internal/commands/cluster.go new file mode 100644 index 000000000..f5e7e00da --- /dev/null +++ b/internal/commands/cluster.go @@ -0,0 +1,39 @@ +package commands + +import ( + "fmt" + + "github.com/spf13/cobra" +) + +func NewClusterCommand() *cobra.Command { + clusterCmd := &cobra.Command{ + Use: "cluster", + Short: "Manage AST cluster", + } + + // installClusterCmd := &cobra.Command{ + // Use: "install", + // Short: "Install a new cluster", + // RunE: runInstallClusterCommand(projectsWrapper), + // } + + // lsClusterCmd := &cobra.Command{ + // Use: "ls", + // Short: "Returns cluster information", + // RunE: runLsClusterCommand(projectsWrapper), + // } + + 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 { + fmt.Println("deploy cluster") + return nil +} From 18b6ff13857964ecd1d97e59f732d65398756a0b Mon Sep 17 00:00:00 2001 From: Daniel Amsellem Date: Mon, 13 Apr 2020 22:48:16 +0300 Subject: [PATCH 10/11] Added cluster command --- internal/commands/cluster.go | 9 +++++---- internal/commands/root.go | 3 ++- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/internal/commands/cluster.go b/internal/commands/cluster.go index f5e7e00da..f4d63ee75 100644 --- a/internal/commands/cluster.go +++ b/internal/commands/cluster.go @@ -2,7 +2,6 @@ package commands import ( "fmt" - "github.com/spf13/cobra" ) @@ -29,11 +28,13 @@ func NewClusterCommand() *cobra.Command { Short: "Deploy AST resources", RunE: runDeployClusterCommand(), } - clusterCmd.AddCommand(deployClusterCmd) return clusterCmd } + func runDeployClusterCommand() func(cmd *cobra.Command, args []string) error { - fmt.Println("deploy cluster") - return nil + return func(cmd *cobra.Command, args []string) error { + fmt.Println("deploy cluster") + return nil + } } diff --git a/internal/commands/root.go b/internal/commands/root.go index 2dbbd3f85..c7653e7a2 100644 --- a/internal/commands/root.go +++ b/internal/commands/root.go @@ -73,8 +73,9 @@ func NewAstCLI(scansWrapper wrappers.ScansWrapper, 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 } From 87d61cd07e0cdb77843c1935c3d08f57b4f6781a Mon Sep 17 00:00:00 2001 From: Daniel Amsellem Date: Mon, 13 Apr 2020 23:14:32 +0300 Subject: [PATCH 11/11] Fixed linter issues --- internal/commands/cluster.go | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/internal/commands/cluster.go b/internal/commands/cluster.go index f4d63ee75..b0d034a14 100644 --- a/internal/commands/cluster.go +++ b/internal/commands/cluster.go @@ -2,6 +2,7 @@ package commands import ( "fmt" + "github.com/spf13/cobra" ) @@ -11,18 +12,6 @@ func NewClusterCommand() *cobra.Command { Short: "Manage AST cluster", } - // installClusterCmd := &cobra.Command{ - // Use: "install", - // Short: "Install a new cluster", - // RunE: runInstallClusterCommand(projectsWrapper), - // } - - // lsClusterCmd := &cobra.Command{ - // Use: "ls", - // Short: "Returns cluster information", - // RunE: runLsClusterCommand(projectsWrapper), - // } - deployClusterCmd := &cobra.Command{ Use: "deploy", Short: "Deploy AST resources",