forked from Versent/unicreds
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aws_config.go
66 lines (52 loc) · 1.89 KB
/
aws_config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package unicreds
import (
"fmt"
"github.com/apex/log"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials/stscreds"
"github.com/aws/aws-sdk-go/aws/session"
)
const (
zoneURL = "http://169.254.169.254/latest/meta-data/placement/availability-zone"
)
// SetAwsConfig configure the AWS region with a fallback for discovery
// on EC2 hosts.
func SetAwsConfig(region, profile *string, role *string) (err error) {
if region == nil {
// Try to get our region based on instance metadata
region, err = getRegion()
if err != nil {
return err
}
}
// This is to work around a limitation of the credentials
// chain when providing an AWS profile as a flag
if aws.StringValue(region) == "" && aws.StringValue(profile) != "" {
return fmt.Errorf("Must provide a region flag when specifying a profile")
}
setAwsConfig(region, profile, role)
return nil
}
func setAwsConfig(region, profile, role *string) {
log.WithFields(log.Fields{"region": aws.StringValue(region), "profile": aws.StringValue(profile)}).Debug("Configure AWS")
sess := getAwsSession(region, profile, role)
SetDynamoDBSession(sess)
SetKMSSession(sess)
}
func getAwsSession(region, profile, role *string) *session.Session {
config := aws.Config{Region: region}
// If no role is supplied, use the shared AWS config
sess := session.Must(session.NewSessionWithOptions(session.Options{
Config: config,
SharedConfigState: session.SharedConfigEnable,
Profile: aws.StringValue(profile),
}))
// If a role is supplied, return a new session using STS-generated credentials
if aws.StringValue(role) != "" {
log.WithFields(log.Fields{"role": aws.StringValue(role), "profile": aws.StringValue(profile)}).Debug("AssumeRole")
config.Credentials = stscreds.NewCredentials(sess, *role)
return session.Must(session.NewSession(&config))
}
// If no role is assumed, return initial session
return sess
}