packer-cn/builder/amazon/common/access_config.go

118 lines
3.2 KiB
Go
Raw Normal View History

package common
import (
2013-07-29 19:42:35 -04:00
"fmt"
2017-06-09 14:24:30 -04:00
"log"
"os"
"time"
2015-06-03 17:13:52 -04:00
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
2017-06-14 19:30:18 -04:00
"github.com/aws/aws-sdk-go/aws/ec2metadata"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/hashicorp/go-cleanhttp"
2017-04-04 16:39:01 -04:00
"github.com/hashicorp/packer/template/interpolate"
)
// AccessConfig is for common configuration related to AWS access
type AccessConfig struct {
AccessKey string `mapstructure:"access_key"`
CustomEndpointEc2 string `mapstructure:"custom_endpoint_ec2"`
2017-02-27 13:44:07 -05:00
MFACode string `mapstructure:"mfa_code"`
2017-03-01 19:43:09 -05:00
ProfileName string `mapstructure:"profile"`
RawRegion string `mapstructure:"region"`
SecretKey string `mapstructure:"secret_key"`
SkipValidation bool `mapstructure:"skip_region_validation"`
Token string `mapstructure:"token"`
2017-03-01 19:43:09 -05:00
session *session.Session
}
// Config returns a valid aws.Config object for access to AWS services, or
// an error if the authentication and region couldn't be resolved
2017-03-01 19:43:09 -05:00
func (c *AccessConfig) Session() (*session.Session, error) {
if c.session != nil {
return c.session, nil
}
config := aws.NewConfig().WithMaxRetries(11).WithCredentialsChainVerboseErrors(true)
2017-06-09 14:24:30 -04:00
if c.ProfileName != "" {
if err := os.Setenv("AWS_PROFILE", c.ProfileName); err != nil {
2017-11-07 15:52:03 -05:00
return nil, fmt.Errorf("Set env error: %s", err)
2017-06-09 14:24:30 -04:00
}
} else if c.RawRegion != "" {
config = config.WithRegion(c.RawRegion)
} else if region := c.metadataRegion(); region != "" {
2017-10-30 18:02:39 -04:00
config = config.WithRegion(region)
}
2017-03-01 19:43:09 -05:00
if c.CustomEndpointEc2 != "" {
2017-06-09 14:01:27 -04:00
config = config.WithEndpoint(c.CustomEndpointEc2)
}
2017-06-09 13:46:01 -04:00
2017-03-01 19:43:09 -05:00
if c.AccessKey != "" {
creds := credentials.NewChainCredentials(
[]credentials.Provider{
&credentials.StaticProvider{
Value: credentials.Value{
AccessKeyID: c.AccessKey,
SecretAccessKey: c.SecretKey,
SessionToken: c.Token,
},
},
})
config = config.WithCredentials(creds)
}
2017-02-27 13:44:07 -05:00
2017-03-01 19:43:09 -05:00
opts := session.Options{
SharedConfigState: session.SharedConfigEnable,
Config: *config,
}
2017-03-01 19:43:09 -05:00
if c.MFACode != "" {
opts.AssumeRoleTokenProvider = func() (string, error) {
return c.MFACode, nil
}
}
if session, err := session.NewSessionWithOptions(opts); err != nil {
2017-03-01 19:43:09 -05:00
return nil, err
} else if *session.Config.Region == "" {
return nil, fmt.Errorf("Could not find AWS region, make sure it's set.")
} else {
c.session = session
2017-03-01 19:43:09 -05:00
}
return c.session, nil
}
// metadataRegion returns the region from the metadata service
func (c *AccessConfig) metadataRegion() string {
2013-07-29 19:42:35 -04:00
client := cleanhttp.DefaultClient()
// Keep the default timeout (100ms) low as we don't want to wait in non-EC2 environments
client.Timeout = 100 * time.Millisecond
ec2meta := ec2metadata.New(session.New(), &aws.Config{
HTTPClient: client,
})
2017-10-30 18:02:39 -04:00
region, err := ec2meta.Region()
2017-06-14 19:30:18 -04:00
if err != nil {
log.Println("Error getting region from metadata service, "+
"probably because we're not running on AWS.", err)
2017-10-30 18:02:39 -04:00
return ""
2017-06-14 19:30:18 -04:00
}
2017-10-30 18:02:39 -04:00
return region
2013-07-29 19:42:35 -04:00
}
func (c *AccessConfig) Prepare(ctx *interpolate.Context) []error {
var errs []error
if c.RawRegion != "" && !c.SkipValidation {
2017-03-28 21:29:55 -04:00
if valid := ValidateRegion(c.RawRegion); !valid {
errs = append(errs, fmt.Errorf("Unknown region: %s", c.RawRegion))
2013-07-29 19:42:35 -04:00
}
}
return errs
}