From 415b886f5b970ae3d80d78e918270e97cb76ce21 Mon Sep 17 00:00:00 2001 From: Adrien Delorme Date: Wed, 24 Oct 2018 15:08:08 +0200 Subject: [PATCH] post-processor/vagrant-cloud: validate vagrant cloud auth token doing an auth request --- post-processor/vagrant-cloud/client.go | 27 ++++++++++++++----- .../vagrant-cloud/post-processor.go | 10 ++++--- 2 files changed, 27 insertions(+), 10 deletions(-) diff --git a/post-processor/vagrant-cloud/client.go b/post-processor/vagrant-cloud/client.go index a66d3eb4a..0a3a8dae3 100644 --- a/post-processor/vagrant-cloud/client.go +++ b/post-processor/vagrant-cloud/client.go @@ -36,7 +36,7 @@ func (v VagrantCloudErrors) FormatErrors() string { return strings.Join(errs, ". ") } -func (v VagrantCloudClient) New(baseUrl string, token string) *VagrantCloudClient { +func (v VagrantCloudClient) New(baseUrl string, token string) (*VagrantCloudClient, error) { c := &VagrantCloudClient{ client: &http.Client{ Transport: &http.Transport{ @@ -46,7 +46,8 @@ func (v VagrantCloudClient) New(baseUrl string, token string) *VagrantCloudClien BaseURL: baseUrl, AccessToken: token, } - return c + + return c, c.ValidateAuthentication() } func decodeBody(resp *http.Response, out interface{}) error { @@ -65,7 +66,19 @@ func encodeBody(obj interface{}) (io.Reader, error) { return buf, nil } -func (v VagrantCloudClient) Get(path string) (*http.Response, error) { +func (v *VagrantCloudClient) ValidateAuthentication() error { + resp, err := v.Get("authenticate") + if err != nil { + return err + } + defer resp.Body.Close() + if resp.StatusCode != 200 { + return fmt.Errorf("Invalid credentials: %s", resp.Status) + } + return nil +} + +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()) @@ -83,7 +96,7 @@ func (v VagrantCloudClient) Get(path string) (*http.Response, error) { return resp, err } -func (v VagrantCloudClient) Delete(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()) @@ -101,7 +114,7 @@ func (v VagrantCloudClient) Delete(path string) (*http.Response, error) { return resp, err } -func (v VagrantCloudClient) Upload(path string, url string) (*http.Response, error) { +func (v *VagrantCloudClient) Upload(path string, url string) (*http.Response, error) { file, err := os.Open(path) if err != nil { @@ -132,7 +145,7 @@ func (v VagrantCloudClient) Upload(path string, url string) (*http.Response, err return resp, err } -func (v VagrantCloudClient) Post(path string, body interface{}) (*http.Response, error) { +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()) @@ -157,7 +170,7 @@ func (v VagrantCloudClient) Post(path string, body interface{}) (*http.Response, return resp, err } -func (v VagrantCloudClient) Put(path string) (*http.Response, error) { +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()) diff --git a/post-processor/vagrant-cloud/post-processor.go b/post-processor/vagrant-cloud/post-processor.go index 273d2fdb8..cb651017f 100644 --- a/post-processor/vagrant-cloud/post-processor.go +++ b/post-processor/vagrant-cloud/post-processor.go @@ -94,6 +94,13 @@ func (p *PostProcessor) Configure(raws ...interface{}) error { } } + // create the HTTP client + p.client, err = VagrantCloudClient{}.New(p.config.VagrantCloudUrl, p.config.AccessToken) + if err != nil { + errs = packer.MultiErrorAppend( + errs, fmt.Errorf("Failed to verify authentication token: %v", err)) + } + if len(errs.Errors) > 0 { return errs } @@ -118,9 +125,6 @@ func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (pac ui.Message("Warning: Using Vagrant Cloud token found in ATLAS_TOKEN. Please make sure it is correct, or set VAGRANT_CLOUD_TOKEN") } - // create the HTTP client - p.client = VagrantCloudClient{}.New(p.config.VagrantCloudUrl, p.config.AccessToken) - // The name of the provider for vagrant cloud, and vagrant providerName := providerFromBuilderName(artifact.Id())