pass vagrant cloud auth token as http header

* stoped using url to pass the auth token and put it in the headers
* added newRequest method to VagrantCloudClient that sets json and auth headesr
* made VagrantCloudClient method pointers to avoid copies
This commit is contained in:
Adrien Delorme 2018-10-24 15:39:09 +02:00
parent 415b886f5b
commit ff4b6d4442
2 changed files with 55 additions and 32 deletions

View File

@ -7,7 +7,6 @@ import (
"io"
"log"
"net/http"
"net/url"
"os"
"strings"
)
@ -79,16 +78,11 @@ func (v *VagrantCloudClient) ValidateAuthentication() error {
}
func (v *VagrantCloudClient) Get(path string) (*http.Response, error) {
params := url.Values{}
params.Set("access_token", v.AccessToken)
reqUrl := fmt.Sprintf("%s/%s?%s", v.BaseURL, path, params.Encode())
reqUrl := fmt.Sprintf("%s/%s", v.BaseURL, path)
// Scrub API key for logs
scrubbedUrl := strings.Replace(reqUrl, v.AccessToken, "ACCESS_TOKEN", -1)
log.Printf("Post-Processor Vagrant Cloud API GET: %s", scrubbedUrl)
log.Printf("Post-Processor Vagrant Cloud API GET: %s", reqUrl)
req, err := http.NewRequest("GET", reqUrl, nil)
req.Header.Add("Content-Type", "application/json")
req, _ := v.newRequest("GET", reqUrl, nil)
resp, err := v.client.Do(req)
log.Printf("Post-Processor Vagrant Cloud API Response: \n\n%+v", resp)
@ -97,16 +91,15 @@ func (v *VagrantCloudClient) Get(path string) (*http.Response, error) {
}
func (v *VagrantCloudClient) Delete(path string) (*http.Response, error) {
params := url.Values{}
params.Set("access_token", v.AccessToken)
reqUrl := fmt.Sprintf("%s/%s?%s", v.BaseURL, path, params.Encode())
reqUrl := fmt.Sprintf("%s/%s", v.BaseURL, path)
// Scrub API key for logs
scrubbedUrl := strings.Replace(reqUrl, v.AccessToken, "ACCESS_TOKEN", -1)
log.Printf("Post-Processor Vagrant Cloud API DELETE: %s", scrubbedUrl)
req, err := http.NewRequest("DELETE", reqUrl, nil)
req, _ := http.NewRequest("DELETE", reqUrl, nil)
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", v.AccessToken))
resp, err := v.client.Do(req)
log.Printf("Post-Processor Vagrant Cloud API Response: \n\n%+v", resp)
@ -129,7 +122,7 @@ func (v *VagrantCloudClient) Upload(path string, url string) (*http.Response, er
defer file.Close()
request, err := http.NewRequest("PUT", url, file)
request, err := v.newRequest("PUT", url, file)
if err != nil {
return nil, fmt.Errorf("Error preparing upload request: %s", err)
@ -146,9 +139,7 @@ func (v *VagrantCloudClient) Upload(path string, url string) (*http.Response, er
}
func (v *VagrantCloudClient) Post(path string, body interface{}) (*http.Response, error) {
params := url.Values{}
params.Set("access_token", v.AccessToken)
reqUrl := fmt.Sprintf("%s/%s?%s", v.BaseURL, path, params.Encode())
reqUrl := fmt.Sprintf("%s/%s", v.BaseURL, path)
encBody, err := encodeBody(body)
@ -156,12 +147,9 @@ func (v *VagrantCloudClient) Post(path string, body interface{}) (*http.Response
return nil, fmt.Errorf("Error encoding body for request: %s", err)
}
// Scrub API key for logs
scrubbedUrl := strings.Replace(reqUrl, v.AccessToken, "ACCESS_TOKEN", -1)
log.Printf("Post-Processor Vagrant Cloud API POST: %s. \n\n Body: %s", scrubbedUrl, encBody)
log.Printf("Post-Processor Vagrant Cloud API POST: %s. \n\n Body: %s", reqUrl, encBody)
req, err := http.NewRequest("POST", reqUrl, encBody)
req.Header.Add("Content-Type", "application/json")
req, _ := v.newRequest("POST", reqUrl, encBody)
resp, err := v.client.Do(req)
@ -171,16 +159,11 @@ func (v *VagrantCloudClient) Post(path string, body interface{}) (*http.Response
}
func (v *VagrantCloudClient) Put(path string) (*http.Response, error) {
params := url.Values{}
params.Set("access_token", v.AccessToken)
reqUrl := fmt.Sprintf("%s/%s?%s", v.BaseURL, path, params.Encode())
reqUrl := fmt.Sprintf("%s/%s", v.BaseURL, path)
// Scrub API key for logs
scrubbedUrl := strings.Replace(reqUrl, v.AccessToken, "ACCESS_TOKEN", -1)
log.Printf("Post-Processor Vagrant Cloud API PUT: %s", scrubbedUrl)
log.Printf("Post-Processor Vagrant Cloud API PUT: %s", reqUrl)
req, err := http.NewRequest("PUT", reqUrl, nil)
req.Header.Add("Content-Type", "application/json")
req, _ := v.newRequest("PUT", reqUrl, nil)
resp, err := v.client.Do(req)
@ -188,3 +171,13 @@ func (v *VagrantCloudClient) Put(path string) (*http.Response, error) {
return resp, err
}
func (v *VagrantCloudClient) newRequest(method, url string, body io.Reader) (*http.Request, error) {
req, err := http.NewRequest(method, url, body)
if err != nil {
return nil, err
}
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", v.AccessToken))
return req, err
}

View File

@ -2,6 +2,9 @@ package vagrantcloud
import (
"bytes"
"fmt"
"net/http"
"net/http/httptest"
"os"
"testing"
@ -25,9 +28,25 @@ func testBadConfig() map[string]interface{} {
}
}
func newSecureServer(token string, handler http.HandlerFunc) *httptest.Server {
token = fmt.Sprintf("Bearer %s", token)
return httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
if req.Header.Get("authorization") != token {
http.Error(rw, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
return
}
if handler != nil {
handler(rw, req)
}
}))
}
func TestPostProcessor_Configure_fromVagrantEnv(t *testing.T) {
var p PostProcessor
config := testGoodConfig()
server := newSecureServer("bar", nil)
defer server.Close()
config["vagrant_cloud_url"] = server.URL
config["access_token"] = ""
os.Setenv("VAGRANT_CLOUD_TOKEN", "bar")
defer func() {
@ -48,6 +67,9 @@ func TestPostProcessor_Configure_fromAtlasEnv(t *testing.T) {
var p PostProcessor
config := testGoodConfig()
config["access_token"] = ""
server := newSecureServer("foo", nil)
defer server.Close()
config["vagrant_cloud_url"] = server.URL
os.Setenv("ATLAS_TOKEN", "foo")
defer func() {
os.Setenv("ATLAS_TOKEN", "")
@ -68,15 +90,23 @@ func TestPostProcessor_Configure_fromAtlasEnv(t *testing.T) {
}
func TestPostProcessor_Configure_Good(t *testing.T) {
config := testGoodConfig()
server := newSecureServer("foo", nil)
defer server.Close()
config["vagrant_cloud_url"] = server.URL
var p PostProcessor
if err := p.Configure(testGoodConfig()); err != nil {
if err := p.Configure(config); err != nil {
t.Fatalf("err: %s", err)
}
}
func TestPostProcessor_Configure_Bad(t *testing.T) {
config := testBadConfig()
server := newSecureServer("foo", nil)
defer server.Close()
config["vagrant_cloud_url"] = server.URL
var p PostProcessor
if err := p.Configure(testBadConfig()); err == nil {
if err := p.Configure(config); err == nil {
t.Fatalf("should have err")
}
}