post-processor/vagrant-cloud: validate vagrant cloud auth token doing an auth request

This commit is contained in:
Adrien Delorme 2018-10-24 15:08:08 +02:00
parent 95d107a89c
commit 415b886f5b
2 changed files with 27 additions and 10 deletions

View File

@ -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())

View File

@ -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())