Skip to content

Commit

Permalink
Merge branch 'master' into go-mod
Browse files Browse the repository at this point in the history
  • Loading branch information
americk0 committed Jun 17, 2020
2 parents 16c3476 + 6b4049d commit 342d8eb
Showing 1 changed file with 50 additions and 3 deletions.
53 changes: 50 additions & 3 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (

"github.com/Cox-Automotive/alks-go"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/credentials/ec2rolecreds"
"github.com/aws/aws-sdk-go/aws/session"
Expand All @@ -25,6 +26,9 @@ import (
// Version number, to be injected at link time
// to set, add `-ldflags "-X main.versionNumber=1.2.3"` to the go build command
var versionNumber string
var ErrNoValidCredentialSources = errors.New(`No valid credential sources found for ALKS Provider.
Please see https://github.com/Cox-Automotive/terraform-provider-alks#authentication for more information on
providing credentials for the ALKS Provider`)

// Config stores ALKS configuration and credentials
type Config struct {
Expand Down Expand Up @@ -82,6 +86,41 @@ func getCredentials(c *Config) *credentials.Credentials {
return credentials.NewChainCredentials(providers)
}

func getCredentialsFromSession(c *Config) (*credentials.Credentials, error) {
var sess *session.Session
var err error
if c.Profile == "" {
sess, err = session.NewSession()
if err != nil {
return nil, ErrNoValidCredentialSources
}
} else {
options := &session.Options{
Config: aws.Config{
HTTPClient: cleanhttp.DefaultClient(),
MaxRetries: aws.Int(0),
Region: aws.String("us-east-1"),
},
}
options.Profile = c.Profile
options.SharedConfigState = session.SharedConfigEnable

sess, err = session.NewSessionWithOptions(*options)
if err != nil {
if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == "NoCredentialProviders" {
return nil, ErrNoValidCredentialSources
}
return nil, fmt.Errorf("Error creating AWS session: %s", err)
}
}
creds := sess.Config.Credentials
_, err = sess.Config.Credentials.Get()
if err != nil {
return nil, ErrNoValidCredentialSources
}
return creds, nil
}

// Client returns a properly configured ALKS client or an appropriate error if initialization fails
func (c *Config) Client() (*alks.Client, error) {
log.Println("[DEBUG] Validting STS credentials")
Expand All @@ -92,9 +131,17 @@ func (c *Config) Client() (*alks.Client, error) {

// validate we have credentials
if cpErr != nil {
return nil, errors.New(`No valid credential sources found for ALKS Provider.
Please see https://github.com/Cox-Automotive/terraform-provider-alks#authentication for more information on
providing credentials for the ALKS Provider`)
if awsErr, ok := cpErr.(awserr.Error); ok && awsErr.Code() == "NoCredentialProviders" {
var err error
creds, err = getCredentialsFromSession(c)
if err != nil {
return nil, err
}
cp, cpErr = creds.Get()
}
}
if cpErr != nil {
return nil, ErrNoValidCredentialSources
}

// create a new session to test credentails
Expand Down

0 comments on commit 342d8eb

Please sign in to comment.