Skip to content

Commit

Permalink
Add support of default AWS credentials chain (closes #36)
Browse files Browse the repository at this point in the history
  • Loading branch information
David Howell authored and oguzhanunlu committed Aug 24, 2020
1 parent 38de873 commit f6e4718
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/emr_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func InitEmrCluster(clusterConfig ClusterConfig) (*EmrCluster, error) {
return nil, err
}

svc := emr.New(session.New(), &aws.Config{
svc := emr.New(session.Must(session.NewSession()), &aws.Config{
Region: aws.String(clusterConfig.Region),
Credentials: creds,
})
Expand Down
2 changes: 1 addition & 1 deletion src/job_flow_steps.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func InitJobFlowSteps(playbookConfig PlaybookConfig, jobflowID string, isAsync b
return nil, err
}

emrSvc := emr.New(session.New(), &aws.Config{
emrSvc := emr.New(session.Must(session.NewSession()), &aws.Config{
Region: aws.String(playbookConfig.Region),
Credentials: creds,
})
Expand Down
7 changes: 4 additions & 3 deletions src/lock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,14 @@ import (
)

func makeClient(t *testing.T) (*api.Client, *testutil.TestServer) {
// Make client config
conf := api.DefaultConfig()
// Create server
server, err := testutil.NewTestServerConfigT(t, nil)
server, err := testutil.NewTestServerT(t)
if err != nil {
t.Fatal(err)
}

// Make client config
conf := api.DefaultConfig()
conf.Address = server.HTTPAddr

// Create client
Expand Down
6 changes: 4 additions & 2 deletions src/logs_downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,14 @@ func InitLogsDownloader(accessKeyID, secretAccessKey, region, jobflowID string)
return nil, err
}

emrSvc := emr.New(session.New(), &aws.Config{
sess := session.Must(session.NewSession())

emrSvc := emr.New(sess, &aws.Config{
Region: aws.String(region),
Credentials: creds,
})

s3Svc := s3.New(session.New(), &aws.Config{
s3Svc := s3.New(sess, &aws.Config{
Region: aws.String(region),
Credentials: creds,
})
Expand Down
19 changes: 16 additions & 3 deletions src/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,29 @@ import (

"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/credentials/ec2rolecreds"
"github.com/aws/aws-sdk-go/aws/defaults"
)

// GetCredentialsProvider attempts to fetch credentials from either:
// 1. IAM Role
// 2. ENV Variables
// 3. Static Credentials
// 3. Default Credential Chain
// 4. Static Credentials
func GetCredentialsProvider(a string, s string) (*credentials.Credentials, error) {
if isIam(a) && isIam(s) {
return credentials.NewCredentials(&ec2rolecreds.EC2RoleProvider{}), nil
} else if isIam(a) || isIam(s) {
return nil, errors.New("access-key and secret-key must both be set to 'iam', or neither")
} else if isEnv(a) && isEnv(s) {
return credentials.NewEnvCredentials(), nil
} else if isDefault(a) && isDefault(s) {
cfg := defaults.Config()
handlers := defaults.Handlers()
return defaults.CredChain(cfg, handlers), nil
} else if isIam(a) || isIam(s) {
return nil, errors.New("access-key and secret-key must both be set to 'iam', or neither")
} else if isEnv(a) || isEnv(s) {
return nil, errors.New("access-key and secret-key must both be set to 'env', or neither")
} else if isDefault(a) || isDefault(s) {
return nil, errors.New("access-key and secret-key must both be set to 'default', or neither")
} else {
return credentials.NewStaticCredentials(a, s, ""), nil
}
Expand All @@ -53,6 +61,11 @@ func isEnv(key string) bool {
return key == "env"
}

// isDefault checks whether or not a variable is asking for default
func isDefault(key string) bool {
return key == "default"
}

// InterfaceToJSONString writes an interface as a JSON
func InterfaceToJSONString(m interface{}, pretty bool) string {
var b []byte
Expand Down
9 changes: 9 additions & 0 deletions src/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ func TestGetCredentialsProvider(t *testing.T) {
assert.NotNil(err)
assert.Equal("access-key and secret-key must both be set to 'env', or neither", err.Error())

res, err = GetCredentialsProvider("default", "faulty")
assert.Nil(res)
assert.NotNil(err)
assert.Equal("access-key and secret-key must both be set to 'default', or neither", err.Error())

res, err = GetCredentialsProvider("iam", "iam")
assert.NotNil(res)
assert.Nil(err)
Expand All @@ -46,6 +51,10 @@ func TestGetCredentialsProvider(t *testing.T) {
assert.NotNil(res)
assert.Nil(err)

res, err = GetCredentialsProvider("default", "default")
assert.NotNil(res)
assert.Nil(err)

res, err = GetCredentialsProvider("access", "secret")
assert.NotNil(res)
assert.Nil(err)
Expand Down

0 comments on commit f6e4718

Please sign in to comment.