github.com/tkak/terraform@v0.5.4-0.20150712180941-7f738dc27225/builtin/providers/aws/config.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "log" 6 "strings" 7 8 "github.com/hashicorp/terraform/helper/multierror" 9 10 "github.com/aws/aws-sdk-go/aws" 11 "github.com/aws/aws-sdk-go/aws/credentials" 12 "github.com/aws/aws-sdk-go/service/autoscaling" 13 "github.com/aws/aws-sdk-go/service/cloudwatch" 14 "github.com/aws/aws-sdk-go/service/dynamodb" 15 "github.com/aws/aws-sdk-go/service/ec2" 16 "github.com/aws/aws-sdk-go/service/ecs" 17 "github.com/aws/aws-sdk-go/service/elasticache" 18 "github.com/aws/aws-sdk-go/service/elb" 19 "github.com/aws/aws-sdk-go/service/iam" 20 "github.com/aws/aws-sdk-go/service/kinesis" 21 "github.com/aws/aws-sdk-go/service/lambda" 22 "github.com/aws/aws-sdk-go/service/rds" 23 "github.com/aws/aws-sdk-go/service/route53" 24 "github.com/aws/aws-sdk-go/service/s3" 25 "github.com/aws/aws-sdk-go/service/sns" 26 "github.com/aws/aws-sdk-go/service/sqs" 27 ) 28 29 type Config struct { 30 AccessKey string 31 SecretKey string 32 Token string 33 Region string 34 MaxRetries int 35 36 AllowedAccountIds []interface{} 37 ForbiddenAccountIds []interface{} 38 } 39 40 type AWSClient struct { 41 cloudwatchconn *cloudwatch.CloudWatch 42 dynamodbconn *dynamodb.DynamoDB 43 ec2conn *ec2.EC2 44 ecsconn *ecs.ECS 45 elbconn *elb.ELB 46 autoscalingconn *autoscaling.AutoScaling 47 s3conn *s3.S3 48 sqsconn *sqs.SQS 49 snsconn *sns.SNS 50 r53conn *route53.Route53 51 region string 52 rdsconn *rds.RDS 53 iamconn *iam.IAM 54 kinesisconn *kinesis.Kinesis 55 elasticacheconn *elasticache.ElastiCache 56 lambdaconn *lambda.Lambda 57 } 58 59 // Client configures and returns a fully initailized AWSClient 60 func (c *Config) Client() (interface{}, error) { 61 var client AWSClient 62 63 // Get the auth and region. This can fail if keys/regions were not 64 // specified and we're attempting to use the environment. 65 var errs []error 66 67 log.Println("[INFO] Building AWS region structure") 68 err := c.ValidateRegion() 69 if err != nil { 70 errs = append(errs, err) 71 } 72 73 if len(errs) == 0 { 74 // store AWS region in client struct, for region specific operations such as 75 // bucket storage in S3 76 client.region = c.Region 77 78 log.Println("[INFO] Building AWS auth structure") 79 // We fetched all credential sources in Provider. If they are 80 // available, they'll already be in c. See Provider definition. 81 creds := credentials.NewStaticCredentials(c.AccessKey, c.SecretKey, c.Token) 82 awsConfig := &aws.Config{ 83 Credentials: creds, 84 Region: c.Region, 85 MaxRetries: c.MaxRetries, 86 } 87 88 log.Println("[INFO] Initializing DynamoDB connection") 89 client.dynamodbconn = dynamodb.New(awsConfig) 90 91 log.Println("[INFO] Initializing ELB connection") 92 client.elbconn = elb.New(awsConfig) 93 94 log.Println("[INFO] Initializing S3 connection") 95 client.s3conn = s3.New(awsConfig) 96 97 log.Println("[INFO] Initializing SQS connection") 98 client.sqsconn = sqs.New(awsConfig) 99 100 log.Println("[INFO] Initializing SNS connection") 101 client.snsconn = sns.New(awsConfig) 102 103 log.Println("[INFO] Initializing RDS Connection") 104 client.rdsconn = rds.New(awsConfig) 105 106 log.Println("[INFO] Initializing IAM Connection") 107 client.iamconn = iam.New(awsConfig) 108 109 log.Println("[INFO] Initializing Kinesis Connection") 110 client.kinesisconn = kinesis.New(awsConfig) 111 112 err := c.ValidateAccountId(client.iamconn) 113 if err != nil { 114 errs = append(errs, err) 115 } 116 117 log.Println("[INFO] Initializing AutoScaling connection") 118 client.autoscalingconn = autoscaling.New(awsConfig) 119 120 log.Println("[INFO] Initializing EC2 Connection") 121 client.ec2conn = ec2.New(awsConfig) 122 123 log.Println("[INFO] Initializing ECS Connection") 124 client.ecsconn = ecs.New(awsConfig) 125 126 // aws-sdk-go uses v4 for signing requests, which requires all global 127 // endpoints to use 'us-east-1'. 128 // See http://docs.aws.amazon.com/general/latest/gr/sigv4_changes.html 129 log.Println("[INFO] Initializing Route 53 connection") 130 client.r53conn = route53.New(&aws.Config{ 131 Credentials: creds, 132 Region: "us-east-1", 133 MaxRetries: c.MaxRetries, 134 }) 135 136 log.Println("[INFO] Initializing Elasticache Connection") 137 client.elasticacheconn = elasticache.New(awsConfig) 138 139 log.Println("[INFO] Initializing Lambda Connection") 140 client.lambdaconn = lambda.New(awsConfig) 141 142 log.Println("[INFO] Initializing CloudWatch SDK connection") 143 client.cloudwatchconn = cloudwatch.New(awsConfig) 144 } 145 146 if len(errs) > 0 { 147 return nil, &multierror.Error{Errors: errs} 148 } 149 150 return &client, nil 151 } 152 153 // ValidateRegion returns an error if the configured region is not a 154 // valid aws region and nil otherwise. 155 func (c *Config) ValidateRegion() error { 156 var regions = [11]string{"us-east-1", "us-west-2", "us-west-1", "eu-west-1", 157 "eu-central-1", "ap-southeast-1", "ap-southeast-2", "ap-northeast-1", 158 "sa-east-1", "cn-north-1", "us-gov-west-1"} 159 160 for _, valid := range regions { 161 if c.Region == valid { 162 return nil 163 } 164 } 165 return fmt.Errorf("Not a valid region: %s", c.Region) 166 } 167 168 // ValidateAccountId returns a context-specific error if the configured account 169 // id is explicitly forbidden or not authorised; and nil if it is authorised. 170 func (c *Config) ValidateAccountId(iamconn *iam.IAM) error { 171 if c.AllowedAccountIds == nil && c.ForbiddenAccountIds == nil { 172 return nil 173 } 174 175 log.Printf("[INFO] Validating account ID") 176 177 out, err := iamconn.GetUser(nil) 178 if err != nil { 179 return fmt.Errorf("Failed getting account ID from IAM: %s", err) 180 } 181 182 account_id := strings.Split(*out.User.ARN, ":")[4] 183 184 if c.ForbiddenAccountIds != nil { 185 for _, id := range c.ForbiddenAccountIds { 186 if id == account_id { 187 return fmt.Errorf("Forbidden account ID (%s)", id) 188 } 189 } 190 } 191 192 if c.AllowedAccountIds != nil { 193 for _, id := range c.AllowedAccountIds { 194 if id == account_id { 195 return nil 196 } 197 } 198 return fmt.Errorf("Account ID not allowed (%s)", account_id) 199 } 200 201 return nil 202 }