2016-05-04 16:24:11 +00:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
|
|
"github.com/aws/aws-sdk-go/aws/client"
|
2016-05-05 14:05:36 +00:00
|
|
|
"github.com/aws/aws-sdk-go/aws/credentials"
|
|
|
|
"github.com/aws/aws-sdk-go/aws/credentials/stscreds"
|
|
|
|
"github.com/aws/aws-sdk-go/aws/session"
|
2016-05-04 16:24:11 +00:00
|
|
|
)
|
|
|
|
|
2016-05-06 12:37:51 +00:00
|
|
|
type CredentialConfig struct {
|
|
|
|
Region string
|
|
|
|
AccessKey string
|
|
|
|
SecretKey string
|
|
|
|
RoleARN string
|
|
|
|
Profile string
|
|
|
|
Filename string
|
|
|
|
Token string
|
2016-05-04 16:24:11 +00:00
|
|
|
}
|
|
|
|
|
2016-05-06 12:37:51 +00:00
|
|
|
func (c *CredentialConfig) Credentials() client.ConfigProvider {
|
|
|
|
if c.RoleARN != "" {
|
2016-05-04 16:24:11 +00:00
|
|
|
return c.assumeCredentials()
|
|
|
|
} else {
|
|
|
|
return c.rootCredentials()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-06 12:37:51 +00:00
|
|
|
func (c *CredentialConfig) rootCredentials() client.ConfigProvider {
|
2016-05-04 16:24:11 +00:00
|
|
|
config := &aws.Config{
|
|
|
|
Region: aws.String(c.Region),
|
|
|
|
}
|
|
|
|
if c.AccessKey != "" || c.SecretKey != "" {
|
|
|
|
config.Credentials = credentials.NewStaticCredentials(c.AccessKey, c.SecretKey, c.Token)
|
2016-05-06 12:37:51 +00:00
|
|
|
} else if c.Profile != "" || c.Filename != "" {
|
|
|
|
config.Credentials = credentials.NewSharedCredentials(c.Filename, c.Profile)
|
2016-05-04 16:24:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return session.New(config)
|
|
|
|
}
|
|
|
|
|
2016-05-06 12:37:51 +00:00
|
|
|
func (c *CredentialConfig) assumeCredentials() client.ConfigProvider {
|
2016-05-04 16:24:11 +00:00
|
|
|
rootCredentials := c.rootCredentials()
|
|
|
|
config := &aws.Config{
|
|
|
|
Region: aws.String(c.Region),
|
|
|
|
}
|
2016-05-06 12:37:51 +00:00
|
|
|
config.Credentials = stscreds.NewCredentials(rootCredentials, c.RoleARN)
|
2016-05-04 16:24:11 +00:00
|
|
|
return session.New(config)
|
|
|
|
}
|