From dd79072011ef06c04a8eddb622692ef6e029c2d0 Mon Sep 17 00:00:00 2001 From: npamudika Date: Thu, 15 Oct 2020 14:14:30 +0530 Subject: [PATCH] Add proxy support for APIMCLI --- import-export-cli/cmd/importAPI.go | 65 +++++++++------------------- import-export-cli/cmd/importApp.go | 62 ++++++++------------------- import-export-cli/utils/utils.go | 68 +++++++++++++++++++++++++++++- 3 files changed, 105 insertions(+), 90 deletions(-) diff --git a/import-export-cli/cmd/importAPI.go b/import-export-cli/cmd/importAPI.go index 322c0b52e..22ce676bc 100644 --- a/import-export-cli/cmd/importAPI.go +++ b/import-export-cli/cmd/importAPI.go @@ -20,7 +20,6 @@ package cmd import ( "bytes" - "crypto/tls" "encoding/json" "encoding/pem" "errors" @@ -36,8 +35,8 @@ import ( "regexp" "strconv" "strings" - "time" + "github.com/go-resty/resty" "github.com/wso2/product-apim-tooling/import-export-cli/specs/params" "github.com/mitchellh/go-homedir" @@ -720,7 +719,7 @@ func validateApiDefinition(def *v2.APIDefinition) error { // Helper function for forming multi-part form data // Returns the formed http request and errors func NewFileUploadRequest(uri string, method string, params map[string]string, paramName, path, - b64encodedCredentials string) (*http.Request, error) { + b64encodedCredentials string) (*resty.Response, error) { file, err := os.Open(path) if err != nil { return nil, err @@ -743,64 +742,40 @@ func NewFileUploadRequest(uri string, method string, params map[string]string, p return nil, err } - request, err := http.NewRequest(method, uri, body) - request.Header.Add(utils.HeaderAuthorization, utils.HeaderValueAuthBasicPrefix+" "+b64encodedCredentials) - request.Header.Add(utils.HeaderContentType, writer.FormDataContentType()) - request.Header.Add(utils.HeaderAccept, "*/*") - request.Header.Add(utils.HeaderConnection, utils.HeaderValueKeepAlive) + // Set headers + headers := make(map[string]string) + headers[utils.HeaderContentType] = writer.FormDataContentType() + headers[utils.HeaderAuthorization] = utils.HeaderValueAuthBasicPrefix+" "+b64encodedCredentials + headers[utils.HeaderAccept] = "*/*" + headers[utils.HeaderConnection] = utils.HeaderValueKeepAlive - return request, err + if method == http.MethodPost { + resp, err := utils.InvokePOSTRequestWithBytes(uri, headers, body.Bytes()) + return resp, err + } else { + resp, err := utils.InvokePUTRequestWithBytes(uri, headers, body.Bytes()) + return resp, err + } } // importAPI imports an API to the API manager func importAPI(endpoint, httpMethod, filePath, accessToken string, extraParams map[string]string) error { - req, err := NewFileUploadRequest(endpoint, httpMethod, extraParams, "file", + resp, err := NewFileUploadRequest(endpoint, httpMethod, extraParams, "file", filePath, accessToken) if err != nil { return err } - var tr *http.Transport - if utils.Insecure { - tr = &http.Transport{ - TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, - } - } else { - tr = &http.Transport{} - } - - client := &http.Client{ - Transport: tr, - Timeout: time.Duration(utils.HttpRequestTimeout) * time.Second, - } - - resp, err := client.Do(req) - if err != nil { - utils.Logln(utils.LogPrefixError, err) - return err - } - //var bodyContent []byte - if resp.StatusCode == http.StatusCreated || resp.StatusCode == http.StatusOK { + if resp.StatusCode() == http.StatusCreated || resp.StatusCode() == http.StatusOK { // 201 Created or 200 OK - _ = resp.Body.Close() - fmt.Println("Successfully imported API") + fmt.Println("Successfully imported API.") return nil } else { // We have an HTTP error fmt.Println("Error importing API.") - fmt.Println("Status: " + resp.Status) - - bodyBuf, err := ioutil.ReadAll(resp.Body) - _ = resp.Body.Close() - if err != nil { - return err - } - - strBody := string(bodyBuf) - fmt.Println("Response:", strBody) - - return errors.New(resp.Status) + fmt.Println("Status: " + resp.Status()) + return errors.New(resp.Status()) } } diff --git a/import-export-cli/cmd/importApp.go b/import-export-cli/cmd/importApp.go index e929791cb..b239f8e34 100644 --- a/import-export-cli/cmd/importApp.go +++ b/import-export-cli/cmd/importApp.go @@ -20,7 +20,6 @@ package cmd import ( "bytes" - "crypto/tls" "fmt" "io" "mime/multipart" @@ -28,8 +27,8 @@ import ( "os" "path/filepath" "strconv" - "time" + "github.com/go-resty/resty" "github.com/spf13/cobra" "github.com/wso2/product-apim-tooling/import-export-cli/credentials" "github.com/wso2/product-apim-tooling/import-export-cli/utils" @@ -111,7 +110,7 @@ func ImportApplication(filename, appOwner, adminEndpiont, accessToken, exportDir adminEndpiont = utils.AppendSlashToString(adminEndpiont) applicationImportEndpoint := adminEndpiont + "import/applications" - url := applicationImportEndpoint + "?appOwner=" + appOwner + utils.SearchAndTag + "preserveOwner=" + + applicationImportUrl := applicationImportEndpoint + "?appOwner=" + appOwner + utils.SearchAndTag + "preserveOwner=" + strconv.FormatBool(preserveOwner) + utils.SearchAndTag + "skipSubscriptions=" + strconv.FormatBool(skipSubscriptions) + utils.SearchAndTag + "skipApplicationKeys=" + strconv.FormatBool(importAppSkipKeys) + utils.SearchAndTag + "update=" + strconv.FormatBool(importAppUpdateApplication) @@ -125,55 +124,27 @@ func ImportApplication(filename, appOwner, adminEndpiont, accessToken, exportDir extraParams := map[string]string{} - req, err := NewAppFileUploadRequest(url, extraParams, "file", zipFilePath, accessToken) + resp, err := NewAppFileUploadRequest(applicationImportUrl, extraParams, "file", zipFilePath, accessToken) if err != nil { - utils.HandleErrorAndExit("Error creating request.", err) - } - - var tr *http.Transport - if utils.Insecure { - tr = &http.Transport{ - TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, - } - } else { - tr = &http.Transport{} + utils.HandleErrorAndExit("Error executing request.", err) } - client := &http.Client{ - Transport: tr, - Timeout: time.Duration(utils.HttpRequestTimeout) * time.Second, - } - - resp, err := client.Do(req) - - if err != nil { - utils.Logln(utils.LogPrefixError, err) - } else { - //var bodyContent []byte - - if resp.StatusCode == http.StatusCreated || resp.StatusCode == http.StatusOK || - resp.StatusCode == http.StatusMultiStatus { + if resp.StatusCode() == http.StatusCreated || resp.StatusCode() == http.StatusOK || + resp.StatusCode() == http.StatusMultiStatus { // 207 Multi Status or 201 Created or 200 OK fmt.Printf("\nCompleted importing the Application '" + filename + "'\n") } else { fmt.Printf("\nUnable to import the Application\n") - fmt.Println("Status: " + resp.Status) + fmt.Println("Status: " + resp.Status()) } - - //fmt.Println(resp.Header) - //resp.Body.Read(bodyContent) - //resp.Body.Close() - //fmt.Println(bodyContent) - } - - return resp, err + return resp.RawResponse, err } // NewFileUploadRequest form an HTTP Put request // Helper function for forming multi-part form data // Returns the formed http request and errors func NewAppFileUploadRequest(uri string, params map[string]string, paramName, path, - accessToken string) (*http.Request, error) { + accessToken string) (*resty.Response, error) { file, err := os.Open(path) if err != nil { return nil, err @@ -196,13 +167,16 @@ func NewAppFileUploadRequest(uri string, params map[string]string, paramName, pa return nil, err } - request, err := http.NewRequest(http.MethodPost, uri, body) - request.Header.Add(utils.HeaderAuthorization, utils.HeaderValueAuthBearerPrefix+" "+accessToken) - request.Header.Add(utils.HeaderContentType, writer.FormDataContentType()) - request.Header.Add(utils.HeaderAccept, "*/*") - request.Header.Add(utils.HeaderConnection, utils.HeaderValueKeepAlive) + // set headers + headers := make(map[string]string) + headers[utils.HeaderContentType] = writer.FormDataContentType() + headers[utils.HeaderAuthorization] = utils.HeaderValueAuthBearerPrefix+" "+accessToken + headers[utils.HeaderAccept] = "*/*" + headers[utils.HeaderConnection] = utils.HeaderValueKeepAlive - return request, err + resp, err := utils.InvokePOSTRequestWithBytes(uri, headers, body.Bytes()) + + return resp, err } func init() { diff --git a/import-export-cli/utils/utils.go b/import-export-cli/utils/utils.go index 18dbff1d2..9e1a5e1e4 100644 --- a/import-export-cli/utils/utils.go +++ b/import-export-cli/utils/utils.go @@ -37,17 +37,75 @@ func InvokePOSTRequest(url string, headers map[string]string, body string) (*res if Insecure { resty.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true}) // To bypass errors in SSL certificates } + if os.Getenv("HTTP_PROXY") != "" { + resty.SetProxy(os.Getenv("HTTP_PROXY")) + } else if os.Getenv("HTTPS_PROXY") != "" { + resty.SetProxy(os.Getenv("HTTPS_PROXY")) + } else if os.Getenv("http_proxy") != "" { + resty.SetProxy(os.Getenv("http_proxy")) + } else if os.Getenv("https_proxy") != "" { + resty.SetProxy(os.Getenv("https_proxy")) + } + resty.SetTimeout(time.Duration(HttpRequestTimeout) * time.Millisecond) + resp, err := resty.R().SetHeaders(headers).SetBody(body).Post(url) + + return resp, err +} + +// Invoke http-post request using go-resty with byte[] body +func InvokePOSTRequestWithBytes(url string, headers map[string]string, body []byte) (*resty.Response, error) { + if Insecure { + resty.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true}) // To bypass errors in SSL certificates + } + if os.Getenv("HTTP_PROXY") != "" { + resty.SetProxy(os.Getenv("HTTP_PROXY")) + } else if os.Getenv("HTTPS_PROXY") != "" { + resty.SetProxy(os.Getenv("HTTPS_PROXY")) + } else if os.Getenv("http_proxy") != "" { + resty.SetProxy(os.Getenv("http_proxy")) + } else if os.Getenv("https_proxy") != "" { + resty.SetProxy(os.Getenv("https_proxy")) + } resty.SetTimeout(time.Duration(HttpRequestTimeout) * time.Millisecond) resp, err := resty.R().SetHeaders(headers).SetBody(body).Post(url) return resp, err } +// Invoke http-post request using go-resty with byte[] body +func InvokePUTRequestWithBytes(url string, headers map[string]string, body []byte) (*resty.Response, error) { + if Insecure { + resty.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true}) // To bypass errors in SSL certificates + } + if os.Getenv("HTTP_PROXY") != "" { + resty.SetProxy(os.Getenv("HTTP_PROXY")) + } else if os.Getenv("HTTPS_PROXY") != "" { + resty.SetProxy(os.Getenv("HTTPS_PROXY")) + } else if os.Getenv("http_proxy") != "" { + resty.SetProxy(os.Getenv("http_proxy")) + } else if os.Getenv("https_proxy") != "" { + resty.SetProxy(os.Getenv("https_proxy")) + } + resty.SetTimeout(time.Duration(HttpRequestTimeout) * time.Millisecond) + resp, err := resty.R().SetHeaders(headers).SetBody(body).Put(url) + + return resp, err +} + // Invoke http-get request using go-resty func InvokeGETRequest(url string, headers map[string]string) (*resty.Response, error) { if Insecure { resty.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true}) // To bypass errors in SSL certificates } + if os.Getenv("HTTP_PROXY") != "" { + resty.SetProxy(os.Getenv("HTTP_PROXY")) + } else if os.Getenv("HTTPS_PROXY") != "" { + resty.SetProxy(os.Getenv("HTTPS_PROXY")) + } else if os.Getenv("http_proxy") != "" { + resty.SetProxy(os.Getenv("http_proxy")) + } else if os.Getenv("https_proxy") != "" { + resty.SetProxy(os.Getenv("https_proxy")) + } resty.SetTimeout(time.Duration(HttpRequestTimeout) * time.Millisecond) resp, err := resty.R().SetHeaders(headers).Get(url) @@ -60,6 +118,15 @@ func InvokeGETRequestWithQueryParam(queryParam string, paramValue string, url st if Insecure { resty.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true}) // To bypass errors in SSL certificates } + if os.Getenv("HTTP_PROXY") != "" { + resty.SetProxy(os.Getenv("HTTP_PROXY")) + } else if os.Getenv("HTTPS_PROXY") != "" { + resty.SetProxy(os.Getenv("HTTPS_PROXY")) + } else if os.Getenv("http_proxy") != "" { + resty.SetProxy(os.Getenv("http_proxy")) + } else if os.Getenv("https_proxy") != "" { + resty.SetProxy(os.Getenv("https_proxy")) + } resty.SetTimeout(time.Duration(HttpRequestTimeout) * time.Millisecond) resp, err := resty.R().SetHeaders(headers).SetQueryParam(queryParam, paramValue).Get(url) @@ -137,5 +204,4 @@ func WriteToFileSystem(exportAPIName, exportAPIVersion, exportEnvironment, expor } fmt.Println("Succesfully exported API!") fmt.Println("Find the exported API at " + pFile) - }