2013-07-16 00:08:19 -04:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
2013-07-29 19:42:35 -04:00
|
|
|
"fmt"
|
2015-04-05 17:58:48 -04:00
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
2013-07-29 19:42:35 -04:00
|
|
|
"strings"
|
|
|
|
"unicode"
|
2015-05-27 14:35:56 -04:00
|
|
|
|
2015-06-03 17:13:52 -04:00
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
|
|
"github.com/aws/aws-sdk-go/aws/credentials"
|
2015-08-10 16:59:56 -04:00
|
|
|
"github.com/aws/aws-sdk-go/aws/credentials/ec2rolecreds"
|
2016-02-19 20:10:05 -05:00
|
|
|
"github.com/aws/aws-sdk-go/aws/ec2metadata"
|
|
|
|
"github.com/aws/aws-sdk-go/aws/session"
|
2015-05-27 14:35:56 -04:00
|
|
|
"github.com/mitchellh/packer/template/interpolate"
|
2013-07-16 00:08:19 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// AccessConfig is for common configuration related to AWS access
|
|
|
|
type AccessConfig struct {
|
2016-02-01 19:55:59 -05:00
|
|
|
AccessKey string `mapstructure:"access_key"`
|
|
|
|
SecretKey string `mapstructure:"secret_key"`
|
|
|
|
RawRegion string `mapstructure:"region"`
|
2016-06-06 14:17:12 -04:00
|
|
|
SkipValidation bool `mapstructure:"skip_region_validation"`
|
2016-02-01 19:55:59 -05:00
|
|
|
Token string `mapstructure:"token"`
|
|
|
|
ProfileName string `mapstructure:"profile"`
|
2013-07-16 00:08:19 -04:00
|
|
|
}
|
|
|
|
|
2015-04-05 17:58:48 -04:00
|
|
|
// Config returns a valid aws.Config object for access to AWS services, or
|
|
|
|
// an error if the authentication and region couldn't be resolved
|
|
|
|
func (c *AccessConfig) Config() (*aws.Config, error) {
|
2016-02-01 19:55:59 -05:00
|
|
|
var creds *credentials.Credentials
|
|
|
|
|
2015-04-05 17:58:48 -04:00
|
|
|
region, err := c.Region()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2013-09-18 16:59:23 -04:00
|
|
|
}
|
2016-02-03 16:53:01 -05:00
|
|
|
config := aws.NewConfig().WithRegion(region).WithMaxRetries(11)
|
2016-02-01 19:55:59 -05:00
|
|
|
if c.ProfileName != "" {
|
2016-02-03 16:53:01 -05:00
|
|
|
profile, err := NewFromProfile(c.ProfileName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
creds, err = profile.CredentialsFromProfile(config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else {
|
2016-02-19 20:10:05 -05:00
|
|
|
sess := session.New(config)
|
2016-02-01 19:55:59 -05:00
|
|
|
creds = credentials.NewChainCredentials([]credentials.Provider{
|
|
|
|
&credentials.StaticProvider{Value: credentials.Value{
|
|
|
|
AccessKeyID: c.AccessKey,
|
|
|
|
SecretAccessKey: c.SecretKey,
|
|
|
|
SessionToken: c.Token,
|
|
|
|
}},
|
|
|
|
&credentials.EnvProvider{},
|
|
|
|
&credentials.SharedCredentialsProvider{Filename: "", Profile: ""},
|
2016-02-19 20:10:05 -05:00
|
|
|
&ec2rolecreds.EC2RoleProvider{
|
|
|
|
Client: ec2metadata.New(sess),
|
|
|
|
},
|
2016-02-01 19:55:59 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
return config.WithCredentials(creds), nil
|
|
|
|
}
|
|
|
|
|
2013-07-29 19:42:35 -04:00
|
|
|
// Region returns the aws.Region object for access to AWS services, requesting
|
|
|
|
// the region from the instance metadata if possible.
|
2015-04-05 17:58:48 -04:00
|
|
|
func (c *AccessConfig) Region() (string, error) {
|
2013-07-29 19:42:35 -04:00
|
|
|
if c.RawRegion != "" {
|
2016-06-06 14:17:12 -04:00
|
|
|
if(c.SkipValidation == false) {
|
|
|
|
if valid := ValidateRegion(c.RawRegion); valid == false {
|
|
|
|
return "", fmt.Errorf("Not a valid region: %s", c.RawRegion)
|
|
|
|
}
|
2015-04-05 17:58:48 -04:00
|
|
|
}
|
|
|
|
return c.RawRegion, nil
|
2013-07-29 19:42:35 -04:00
|
|
|
}
|
|
|
|
|
2015-04-05 17:58:48 -04:00
|
|
|
md, err := GetInstanceMetaData("placement/availability-zone")
|
2013-07-29 19:42:35 -04:00
|
|
|
if err != nil {
|
2015-04-05 17:58:48 -04:00
|
|
|
return "", err
|
2013-07-29 19:42:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
region := strings.TrimRightFunc(string(md), unicode.IsLetter)
|
2015-04-05 17:58:48 -04:00
|
|
|
return region, nil
|
2013-07-29 19:42:35 -04:00
|
|
|
}
|
|
|
|
|
2015-05-27 14:35:56 -04:00
|
|
|
func (c *AccessConfig) Prepare(ctx *interpolate.Context) []error {
|
|
|
|
var errs []error
|
2013-07-29 19:42:35 -04:00
|
|
|
if c.RawRegion != "" {
|
2016-06-06 14:17:12 -04:00
|
|
|
if(c.SkipValidation == false) {
|
|
|
|
if valid := ValidateRegion(c.RawRegion); valid == false {
|
|
|
|
errs = append(errs, fmt.Errorf("Unknown region: %s", c.RawRegion))
|
|
|
|
}
|
2013-07-29 19:42:35 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-08 17:29:46 -04:00
|
|
|
if len(errs) > 0 {
|
|
|
|
return errs
|
|
|
|
}
|
|
|
|
|
2013-07-16 00:08:19 -04:00
|
|
|
return nil
|
|
|
|
}
|
2015-04-05 17:58:48 -04:00
|
|
|
|
|
|
|
func GetInstanceMetaData(path string) (contents []byte, err error) {
|
|
|
|
url := "http://169.254.169.254/latest/meta-data/" + path
|
|
|
|
|
|
|
|
resp, err := http.Get(url)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
if resp.StatusCode != 200 {
|
|
|
|
err = fmt.Errorf("Code %d returned for url %s", resp.StatusCode, url)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return []byte(body), err
|
|
|
|
}
|