|
|
|
|
@ -2,7 +2,8 @@ package common
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"time"
|
|
|
|
|
"log"
|
|
|
|
|
"os"
|
|
|
|
|
|
|
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
|
|
|
"github.com/aws/aws-sdk-go/aws/credentials"
|
|
|
|
|
@ -19,19 +20,30 @@ type AccessConfig struct {
|
|
|
|
|
AssumeRoleArn string `mapstructure:"assume_role_arn"`
|
|
|
|
|
CustomEndpointEc2 string `mapstructure:"custom_endpoint_ec2"`
|
|
|
|
|
ExternalID string `mapstructure:"external_id"`
|
|
|
|
|
ExternalID string `mapstructure:"external_id"`
|
|
|
|
|
MFACode string `mapstructure:"mfa_code"`
|
|
|
|
|
MFACode string `mapstructure:"mfa_code"`
|
|
|
|
|
MFASerial string `mapstructure:"mfa_serial"`
|
|
|
|
|
MFASerial string `mapstructure:"mfa_serial"`
|
|
|
|
|
ProfileName string `mapstructure:"profile"`
|
|
|
|
|
ProfileName string `mapstructure:"profile"`
|
|
|
|
|
RawRegion string `mapstructure:"region"`
|
|
|
|
|
RawRegion string `mapstructure:"region"`
|
|
|
|
|
SecretKey string `mapstructure:"secret_key"`
|
|
|
|
|
SecretKey string `mapstructure:"secret_key"`
|
|
|
|
|
SkipValidation bool `mapstructure:"skip_region_validation"`
|
|
|
|
|
SkipValidation bool `mapstructure:"skip_region_validation"`
|
|
|
|
|
Token string `mapstructure:"token"`
|
|
|
|
|
Token string `mapstructure:"token"`
|
|
|
|
|
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
|
|
|
|
|
func (c *AccessConfig) Config() (*aws.Config, error) {
|
|
|
|
|
var creds *credentials.Credentials
|
|
|
|
|
func (c *AccessConfig) Session() (*session.Session, error) {
|
|
|
|
|
if c.session != nil {
|
|
|
|
|
return c.session, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
region, err := c.Region()
|
|
|
|
|
if err != nil {
|
|
|
|
|
@ -59,26 +71,55 @@ func (c *AccessConfig) Config() (*aws.Config, error) {
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
if c.AssumeRoleArn != "" {
|
|
|
|
|
var mfa func(*stscreds.AssumeRoleProvider)
|
|
|
|
|
var options []func(*stscreds.AssumeRoleProvider)
|
|
|
|
|
if c.MFACode != "" {
|
|
|
|
|
mfa = func(p *stscreds.AssumeRoleProvider) {
|
|
|
|
|
options = append(options, func(p *stscreds.AssumeRoleProvider) {
|
|
|
|
|
p.SerialNumber = aws.String(c.MFASerial)
|
|
|
|
|
p.TokenProvider = func() (string, error) {
|
|
|
|
|
return c.MFACode, nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sess := session.Must(session.NewSession(config.WithCredentials(creds)))
|
|
|
|
|
creds = stscreds.NewCredentials(sess, c.AssumeRoleArn, mfa, func(p *stscreds.AssumeRoleProvider) {
|
|
|
|
|
p.Duration = time.Duration(60) * time.Minute
|
|
|
|
|
if len(c.ExternalID) > 0 {
|
|
|
|
|
p.ExternalID = aws.String(c.ExternalID)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
if c.ProfileName != "" {
|
|
|
|
|
err := os.Setenv("AWS_PROFILE", c.ProfileName)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Printf("Set env error: %s", err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
config := aws.NewConfig().WithRegion(region).WithMaxRetries(11).WithCredentialsChainVerboseErrors(true)
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
opts := session.Options{
|
|
|
|
|
SharedConfigState: session.SharedConfigEnable,
|
|
|
|
|
Config: *config,
|
|
|
|
|
}
|
|
|
|
|
return config.WithCredentials(creds), nil
|
|
|
|
|
if c.MFACode != "" {
|
|
|
|
|
opts.AssumeRoleTokenProvider = func() (string, error) {
|
|
|
|
|
return c.MFACode, nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
c.session, err = session.NewSessionWithOptions(opts)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return c.session, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Region returns the aws.Region object for access to AWS services, requesting
|
|
|
|
|
@ -113,7 +154,8 @@ func (c *AccessConfig) Prepare(ctx *interpolate.Context) []error {
|
|
|
|
|
hasAssumeRoleArn := len(c.AssumeRoleArn) > 0
|
|
|
|
|
hasMFASerial := len(c.MFASerial) > 0
|
|
|
|
|
hasMFACode := len(c.MFACode) > 0
|
|
|
|
|
if hasAssumeRoleArn && (!hasMFACode || !hasMFASerial) {
|
|
|
|
|
if hasAssumeRoleArn && (hasMFACode != hasMFASerial) {
|
|
|
|
|
// either both mfa code and serial must be set, or neither.
|
|
|
|
|
errs = append(errs, fmt.Errorf("Both mfa_serial and mfa_code must be specified."))
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|