From 4595d51910c44e1c911a7efa71466a58b7b32ab8 Mon Sep 17 00:00:00 2001 From: Manfred Touron Date: Wed, 12 Aug 2015 13:06:56 +0200 Subject: [PATCH 1/2] Moved http.Client{} to ScalewayAPI struct --- pkg/api/api.go | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/pkg/api/api.go b/pkg/api/api.go index 3af427610c..d93cfd0753 100644 --- a/pkg/api/api.go +++ b/pkg/api/api.go @@ -43,6 +43,7 @@ type ScalewayAPI struct { // Cache is used to quickly resolve identifiers from names Cache *ScalewayCache + client *http.Client anonuuid anonuuid.AnonUUID } @@ -543,13 +544,17 @@ func NewScalewayAPI(apiEndPoint, accountEndPoint, organization, token string) (* return nil, err } s := &ScalewayAPI{ + // exposed ComputeAPI: apiEndPoint, AccountAPI: accountEndPoint, APIUrl: apiEndPoint, Organization: organization, Token: token, Cache: cache, - anonuuid: *anonuuid.New(), + + // internal + anonuuid: *anonuuid.New(), + client: &http.Client{}, } return s, nil @@ -564,20 +569,18 @@ func (s *ScalewayAPI) Sync() { func (s *ScalewayAPI) GetResponse(resource string) (*http.Response, error) { uri := fmt.Sprintf("%s/%s", strings.TrimRight(s.APIUrl, "/"), resource) log.Debugf("GET %s", uri) - client := &http.Client{} req, err := http.NewRequest("GET", uri, nil) if err != nil { return nil, err } req.Header.Set("X-Auth-Token", s.Token) req.Header.Set("Content-Type", "application/json") - return client.Do(req) + return s.client.Do(req) } // PostResponse returns an http.Response object for the updated resource func (s *ScalewayAPI) PostResponse(resource string, data interface{}) (*http.Response, error) { uri := fmt.Sprintf("%s/%s", strings.TrimRight(s.APIUrl, "/"), resource) - client := &http.Client{} payload := new(bytes.Buffer) encoder := json.NewEncoder(payload) if err := encoder.Encode(data); err != nil { @@ -596,13 +599,12 @@ func (s *ScalewayAPI) PostResponse(resource string, data interface{}) (*http.Res } req.Header.Set("X-Auth-Token", s.Token) req.Header.Set("Content-Type", "application/json") - return client.Do(req) + return s.client.Do(req) } // PatchResponse returns an http.Response object for the updated resource func (s *ScalewayAPI) PatchResponse(resource string, data interface{}) (*http.Response, error) { uri := fmt.Sprintf("%s/%s", strings.TrimRight(s.APIUrl, "/"), resource) - client := &http.Client{} payload := new(bytes.Buffer) encoder := json.NewEncoder(payload) if err := encoder.Encode(data); err != nil { @@ -621,13 +623,12 @@ func (s *ScalewayAPI) PatchResponse(resource string, data interface{}) (*http.Re } req.Header.Set("X-Auth-Token", s.Token) req.Header.Set("Content-Type", "application/json") - return client.Do(req) + return s.client.Do(req) } // PutResponse returns an http.Response object for the updated resource func (s *ScalewayAPI) PutResponse(resource string, data interface{}) (*http.Response, error) { uri := fmt.Sprintf("%s/%s", strings.TrimRight(s.APIUrl, "/"), resource) - client := &http.Client{} payload := new(bytes.Buffer) encoder := json.NewEncoder(payload) if err := encoder.Encode(data); err != nil { @@ -646,13 +647,12 @@ func (s *ScalewayAPI) PutResponse(resource string, data interface{}) (*http.Resp } req.Header.Set("X-Auth-Token", s.Token) req.Header.Set("Content-Type", "application/json") - return client.Do(req) + return s.client.Do(req) } // DeleteResponse returns an http.Response object for the deleted resource func (s *ScalewayAPI) DeleteResponse(resource string) (*http.Response, error) { uri := fmt.Sprintf("%s/%s", strings.TrimRight(s.APIUrl, "/"), resource) - client := &http.Client{} log.Debugf("DELETE %s", uri) req, err := http.NewRequest("DELETE", uri, nil) if err != nil { @@ -660,7 +660,7 @@ func (s *ScalewayAPI) DeleteResponse(resource string) (*http.Response, error) { } req.Header.Set("X-Auth-Token", s.Token) req.Header.Set("Content-Type", "application/json") - return client.Do(req) + return s.client.Do(req) } // GetServers gets the list of servers from the ScalewayAPI From fd4fc92b0cd54909905956d9cdc03f53b3dfc2cd Mon Sep 17 00:00:00 2001 From: Manfred Touron Date: Wed, 12 Aug 2015 13:11:08 +0200 Subject: [PATCH 2/2] Add an header to disable TLS verify (Fix #115) --- README.md | 1 + pkg/api/api.go | 7 +++++++ 2 files changed, 8 insertions(+) diff --git a/README.md b/README.md index 0fe6c409fc..39a73a19a1 100644 --- a/README.md +++ b/README.md @@ -1051,6 +1051,7 @@ $ scw inspect myserver | jq '.[0].public_ip.address' * `scw inspect TYPE:xxx TYPE:yyy` will only refresh cache for `TYPE` * Sorting cache search by Levenshtein distance ([#87](https://github.com/scaleway/scaleway-cli/issues/87)) * Allow set up api endpoint using the environment variable $scaleway_api_endpoint +* Use TLS and verify can now be disabled using `SCALEWAY_TLSVERIFY=0` env var ([#115](https://github.com/scaleway/scaleway-cli/issues/115)) #### Fixes diff --git a/pkg/api/api.go b/pkg/api/api.go index d93cfd0753..66b19a7bea 100644 --- a/pkg/api/api.go +++ b/pkg/api/api.go @@ -9,6 +9,7 @@ package api import ( "bytes" + "crypto/tls" "encoding/json" "fmt" "net/http" @@ -557,6 +558,12 @@ func NewScalewayAPI(apiEndPoint, accountEndPoint, organization, token string) (* client: &http.Client{}, } + if os.Getenv("SCALEWAY_TLSVERIFY") == "0" { + s.client.Transport = &http.Transport{ + TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, + } + } + return s, nil }