Skip to content

Commit

Permalink
Merge pull request #1841 from josharian/aws-external-creds
Browse files Browse the repository at this point in the history
providers/aws: detect credentials more robustly
  • Loading branch information
mitchellh committed Jun 29, 2015
2 parents da136d1 + ed67f8f commit 2a5ed6c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 22 deletions.
13 changes: 3 additions & 10 deletions builtin/providers/aws/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,9 @@ func (c *Config) Client() (interface{}, error) {
client.region = c.Region

log.Println("[INFO] Building AWS auth structure")
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{},
})
// We fetched all credential sources in Provider.
// If it is available, it is stored in c.
creds := credentials.NewStaticCredentials(c.AccessKey, c.SecretKey, c.Token)
awsConfig := &aws.Config{
Credentials: creds,
Region: c.Region,
Expand Down
42 changes: 30 additions & 12 deletions builtin/providers/aws/provider.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package aws

import (
"sync"

"github.com/awslabs/aws-sdk-go/aws/credentials"
"github.com/hashicorp/terraform/helper/hashcode"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/terraform"
Expand All @@ -11,35 +14,50 @@ func Provider() terraform.ResourceProvider {
// TODO: Move the validation to this, requires conditional schemas
// TODO: Move the configuration to this, requires validation

// Prepare to handle external sources of credentials.
// Static credentials are intentionally omitted;
// this is used when no static credentials are provided.
creds := credentials.NewChainCredentials([]credentials.Provider{
&credentials.EnvProvider{},
&credentials.SharedCredentialsProvider{},
&credentials.EC2RoleProvider{},
})
var credVal credentials.Value
var credErr error
var once sync.Once
getCreds := func() {
credVal, credErr = creds.Get()
}

return &schema.Provider{
Schema: map[string]*schema.Schema{
"access_key": &schema.Schema{
Type: schema.TypeString,
Required: true,
DefaultFunc: schema.MultiEnvDefaultFunc([]string{
"AWS_ACCESS_KEY",
"AWS_ACCESS_KEY_ID",
}, nil),
DefaultFunc: func() (interface{}, error) {
once.Do(getCreds)
return credVal.AccessKeyID, credErr
},
Description: descriptions["access_key"],
},

"secret_key": &schema.Schema{
Type: schema.TypeString,
Required: true,
DefaultFunc: schema.MultiEnvDefaultFunc([]string{
"AWS_SECRET_KEY",
"AWS_SECRET_ACCESS_KEY",
}, nil),
DefaultFunc: func() (interface{}, error) {
once.Do(getCreds)
return credVal.SecretAccessKey, credErr
},
Description: descriptions["secret_key"],
},

"token": &schema.Schema{
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.MultiEnvDefaultFunc([]string{
"AWS_SESSION_TOKEN",
"AWS_SECURITY_TOKEN",
}, ""),
DefaultFunc: func() (interface{}, error) {
once.Do(getCreds)
return credVal.SessionToken, credErr
},
Description: descriptions["token"],
},

Expand Down

0 comments on commit 2a5ed6c

Please sign in to comment.