2014-06-24 04:01:57 +02:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
2014-07-30 00:22:37 +02:00
|
|
|
"fmt"
|
2014-11-21 17:58:34 +01:00
|
|
|
"log"
|
2015-04-20 00:54:42 +02:00
|
|
|
"strings"
|
2014-06-24 04:01:57 +02:00
|
|
|
|
2014-11-21 17:58:34 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/multierror"
|
2015-02-12 17:48:48 +01:00
|
|
|
|
2015-04-16 22:02:04 +02:00
|
|
|
"github.com/awslabs/aws-sdk-go/aws"
|
2015-05-05 12:26:26 +02:00
|
|
|
"github.com/awslabs/aws-sdk-go/aws/credentials"
|
2015-04-16 14:10:17 +02:00
|
|
|
"github.com/awslabs/aws-sdk-go/service/autoscaling"
|
2015-04-16 22:02:04 +02:00
|
|
|
"github.com/awslabs/aws-sdk-go/service/ec2"
|
2015-04-26 03:53:21 +02:00
|
|
|
"github.com/awslabs/aws-sdk-go/service/elasticache"
|
2015-04-16 22:02:04 +02:00
|
|
|
"github.com/awslabs/aws-sdk-go/service/elb"
|
2015-04-15 22:05:44 +02:00
|
|
|
"github.com/awslabs/aws-sdk-go/service/iam"
|
2015-04-13 22:45:55 +02:00
|
|
|
"github.com/awslabs/aws-sdk-go/service/rds"
|
2015-04-16 22:02:04 +02:00
|
|
|
"github.com/awslabs/aws-sdk-go/service/route53"
|
|
|
|
"github.com/awslabs/aws-sdk-go/service/s3"
|
2014-06-24 04:01:57 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type Config struct {
|
2015-05-03 11:08:47 +02:00
|
|
|
AccessKey string
|
|
|
|
SecretKey string
|
|
|
|
Token string
|
|
|
|
Region string
|
|
|
|
MaxRetries int
|
2015-04-20 00:54:42 +02:00
|
|
|
|
|
|
|
AllowedAccountIds []interface{}
|
|
|
|
ForbiddenAccountIds []interface{}
|
2014-11-21 17:58:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type AWSClient struct {
|
2015-02-20 23:26:43 +01:00
|
|
|
ec2conn *ec2.EC2
|
|
|
|
elbconn *elb.ELB
|
|
|
|
autoscalingconn *autoscaling.AutoScaling
|
|
|
|
s3conn *s3.S3
|
|
|
|
r53conn *route53.Route53
|
|
|
|
region string
|
2015-02-27 17:50:21 +01:00
|
|
|
rdsconn *rds.RDS
|
2015-03-24 21:34:13 +01:00
|
|
|
iamconn *iam.IAM
|
2015-04-26 03:53:21 +02:00
|
|
|
elasticacheconn *elasticache.ElastiCache
|
2014-11-21 17:58:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Client configures and returns a fully initailized AWSClient
|
|
|
|
func (c *Config) Client() (interface{}, error) {
|
|
|
|
var client AWSClient
|
|
|
|
|
|
|
|
// Get the auth and region. This can fail if keys/regions were not
|
|
|
|
// specified and we're attempting to use the environment.
|
|
|
|
var errs []error
|
|
|
|
|
|
|
|
log.Println("[INFO] Building AWS region structure")
|
2015-03-13 15:42:50 +01:00
|
|
|
err := c.ValidateRegion()
|
2014-11-21 17:58:34 +01:00
|
|
|
if err != nil {
|
|
|
|
errs = append(errs, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(errs) == 0 {
|
2015-02-19 22:38:56 +01:00
|
|
|
// store AWS region in client struct, for region specific operations such as
|
|
|
|
// bucket storage in S3
|
|
|
|
client.region = c.Region
|
2015-02-20 15:55:54 +01:00
|
|
|
|
2015-03-13 15:42:50 +01:00
|
|
|
log.Println("[INFO] Building AWS auth structure")
|
2015-05-05 19:44:19 +02: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: ""},
|
|
|
|
&credentials.EC2RoleProvider{},
|
|
|
|
})
|
2015-04-16 22:02:04 +02:00
|
|
|
awsConfig := &aws.Config{
|
|
|
|
Credentials: creds,
|
2015-04-14 23:41:36 +02:00
|
|
|
Region: c.Region,
|
2015-05-03 11:08:47 +02:00
|
|
|
MaxRetries: c.MaxRetries,
|
2015-04-13 21:01:41 +02:00
|
|
|
}
|
|
|
|
|
2015-04-16 22:02:04 +02:00
|
|
|
log.Println("[INFO] Initializing ELB connection")
|
2015-04-13 21:01:41 +02:00
|
|
|
client.elbconn = elb.New(awsConfig)
|
2015-04-14 23:41:36 +02:00
|
|
|
|
2014-11-21 17:58:34 +01:00
|
|
|
log.Println("[INFO] Initializing S3 connection")
|
2015-04-16 20:51:12 +02:00
|
|
|
client.s3conn = s3.New(awsConfig)
|
2015-02-20 15:55:54 +01:00
|
|
|
|
2015-04-16 22:02:04 +02:00
|
|
|
log.Println("[INFO] Initializing RDS Connection")
|
2015-04-13 22:45:55 +02:00
|
|
|
client.rdsconn = rds.New(awsConfig)
|
2015-04-13 21:01:41 +02:00
|
|
|
|
2015-04-16 22:02:04 +02:00
|
|
|
log.Println("[INFO] Initializing IAM Connection")
|
2015-04-15 21:36:08 +02:00
|
|
|
client.iamconn = iam.New(awsConfig)
|
2015-04-20 00:54:42 +02:00
|
|
|
|
|
|
|
err := c.ValidateAccountId(client.iamconn)
|
|
|
|
if err != nil {
|
|
|
|
errs = append(errs, err)
|
|
|
|
}
|
|
|
|
|
2015-04-16 22:02:04 +02:00
|
|
|
log.Println("[INFO] Initializing AutoScaling connection")
|
2015-04-16 14:10:17 +02:00
|
|
|
client.autoscalingconn = autoscaling.New(awsConfig)
|
2015-04-14 16:51:23 +02:00
|
|
|
|
2015-04-16 22:02:04 +02:00
|
|
|
log.Println("[INFO] Initializing EC2 Connection")
|
|
|
|
client.ec2conn = ec2.New(awsConfig)
|
2015-04-14 16:51:23 +02:00
|
|
|
|
|
|
|
// aws-sdk-go uses v4 for signing requests, which requires all global
|
|
|
|
// endpoints to use 'us-east-1'.
|
|
|
|
// See http://docs.aws.amazon.com/general/latest/gr/sigv4_changes.html
|
2015-04-16 22:18:01 +02:00
|
|
|
log.Println("[INFO] Initializing Route 53 connection")
|
2015-04-16 22:02:04 +02:00
|
|
|
client.r53conn = route53.New(&aws.Config{
|
|
|
|
Credentials: creds,
|
2015-04-14 16:51:23 +02:00
|
|
|
Region: "us-east-1",
|
2015-05-03 11:08:47 +02:00
|
|
|
MaxRetries: c.MaxRetries,
|
2015-04-14 16:51:23 +02:00
|
|
|
})
|
|
|
|
|
2015-04-26 03:53:21 +02:00
|
|
|
log.Println("[INFO] Initializing Elasticache Connection")
|
|
|
|
client.elasticacheconn = elasticache.New(awsConfig)
|
2014-11-21 17:58:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(errs) > 0 {
|
|
|
|
return nil, &multierror.Error{Errors: errs}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &client, nil
|
2014-06-24 04:01:57 +02:00
|
|
|
}
|
|
|
|
|
2015-04-27 20:06:49 +02:00
|
|
|
// ValidateRegion returns an error if the configured region is not a
|
|
|
|
// valid aws region and nil otherwise.
|
2015-03-13 15:42:50 +01:00
|
|
|
func (c *Config) ValidateRegion() error {
|
2014-10-30 15:07:12 +01:00
|
|
|
var regions = [11]string{"us-east-1", "us-west-2", "us-west-1", "eu-west-1",
|
|
|
|
"eu-central-1", "ap-southeast-1", "ap-southeast-2", "ap-northeast-1",
|
|
|
|
"sa-east-1", "cn-north-1", "us-gov-west-1"}
|
2014-07-30 00:22:37 +02:00
|
|
|
|
|
|
|
for _, valid := range regions {
|
|
|
|
if c.Region == valid {
|
2015-03-13 15:42:50 +01:00
|
|
|
return nil
|
2014-07-30 00:22:37 +02:00
|
|
|
}
|
|
|
|
}
|
2015-03-13 15:42:50 +01:00
|
|
|
return fmt.Errorf("Not a valid region: %s", c.Region)
|
2014-06-24 04:01:57 +02:00
|
|
|
}
|
2015-04-20 00:54:42 +02:00
|
|
|
|
2015-04-27 20:06:49 +02:00
|
|
|
// ValidateAccountId returns a context-specific error if the configured account
|
|
|
|
// id is explicitly forbidden or not authorised; and nil if it is authorised.
|
2015-04-20 00:54:42 +02:00
|
|
|
func (c *Config) ValidateAccountId(iamconn *iam.IAM) error {
|
|
|
|
if c.AllowedAccountIds == nil && c.ForbiddenAccountIds == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[INFO] Validating account ID")
|
|
|
|
|
|
|
|
out, err := iamconn.GetUser(nil)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed getting account ID from IAM: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
account_id := strings.Split(*out.User.ARN, ":")[4]
|
|
|
|
|
|
|
|
if c.ForbiddenAccountIds != nil {
|
|
|
|
for _, id := range c.ForbiddenAccountIds {
|
|
|
|
if id == account_id {
|
|
|
|
return fmt.Errorf("Forbidden account ID (%s)", id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.AllowedAccountIds != nil {
|
|
|
|
for _, id := range c.AllowedAccountIds {
|
|
|
|
if id == account_id {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return fmt.Errorf("Account ID not allowed (%s)", account_id)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|