@@ -12,12 +12,12 @@ import (
12
12
)
13
13
14
14
const (
15
- ACCESS_ENV_KEY = "AWS_ACCESS_KEY"
16
- SECRET_ENV_KEY = "AWS_SECRET_KEY"
15
+ AccessEnvKey = "AWS_ACCESS_KEY"
16
+ SecretEnvKey = "AWS_SECRET_KEY"
17
17
18
- AWS_METADATA_SERVER = "169.254.169.254"
19
- AWS_IAM_CREDS_PATH = "/latest/meta-data/iam/security-credentials"
20
- AWS_IAM_CREDS_URL = "http://" + AWS_METADATA_SERVER + AWS_IAM_CREDS_PATH
18
+ AWSMetadataServer = "169.254.169.254"
19
+ AWSIAMCredsPath = "/latest/meta-data/iam/security-credentials"
20
+ AWSIAMCredsURL = "http://" + AWSMetadataServer + "/" + AWSIAMCredsPath
21
21
)
22
22
23
23
// Auth interface for authentication credentials and information
@@ -31,7 +31,8 @@ type Auth interface {
31
31
Sign (* Service , time.Time ) []byte
32
32
}
33
33
34
- type auth struct {
34
+ // AuthCredentials holds the AWS credentials and metadata
35
+ type AuthCredentials struct {
35
36
// accessKey, secretKey are the standard AWS auth credentials
36
37
accessKey , secretKey , token string
37
38
@@ -41,22 +42,24 @@ type auth struct {
41
42
expiry time.Time
42
43
}
43
44
44
- func NewAuth (accessKey , secretKey string ) Auth {
45
- return & auth {
45
+ // NewAuth creates a *AuthCredentials struct that adheres to the Auth interface to
46
+ // dynamically retrieve AWS credentials
47
+ func NewAuth (accessKey , secretKey string ) * AuthCredentials {
48
+ return & AuthCredentials {
46
49
accessKey : accessKey ,
47
50
secretKey : secretKey ,
48
51
}
49
52
}
50
53
51
54
// NewAuthFromEnv retrieves auth credentials from environment vars
52
- func NewAuthFromEnv () (Auth , error ) {
53
- accessKey := os .Getenv (ACCESS_ENV_KEY )
54
- secretKey := os .Getenv (SECRET_ENV_KEY )
55
+ func NewAuthFromEnv () (* AuthCredentials , error ) {
56
+ accessKey := os .Getenv (AccessEnvKey )
57
+ secretKey := os .Getenv (SecretEnvKey )
55
58
if accessKey == "" {
56
- return nil , fmt .Errorf ("Unable to retrieve access key from %s env variable" , ACCESS_ENV_KEY )
59
+ return nil , fmt .Errorf ("Unable to retrieve access key from %s env variable" , AccessEnvKey )
57
60
}
58
61
if secretKey == "" {
59
- return nil , fmt .Errorf ("Unable to retrieve secret key from %s env variable" , SECRET_ENV_KEY )
62
+ return nil , fmt .Errorf ("Unable to retrieve secret key from %s env variable" , SecretEnvKey )
60
63
}
61
64
62
65
return NewAuth (accessKey , secretKey ), nil
@@ -68,41 +71,41 @@ func NewAuthFromEnv() (Auth, error) {
68
71
//
69
72
// TODO: specify custom network (connect, read) timeouts, else this will block
70
73
// for the default timeout durations.
71
- func NewAuthFromMetadata () (Auth , error ) {
72
- auth := & auth {}
74
+ func NewAuthFromMetadata () (* AuthCredentials , error ) {
75
+ auth := & AuthCredentials {}
73
76
if err := auth .Renew (); err != nil {
74
77
return nil , err
75
78
}
76
79
return auth , nil
77
80
}
78
81
79
82
// HasExpiration returns true if the expiration time is non-zero and false otherwise
80
- func (a * auth ) HasExpiration () bool {
83
+ func (a * AuthCredentials ) HasExpiration () bool {
81
84
return ! a .expiry .IsZero ()
82
85
}
83
86
84
87
// GetExpiration retrieves the current expiration time
85
- func (a * auth ) GetExpiration () time.Time {
88
+ func (a * AuthCredentials ) GetExpiration () time.Time {
86
89
return a .expiry
87
90
}
88
91
89
92
// GetToken returns the token
90
- func (a * auth ) GetToken () string {
93
+ func (a * AuthCredentials ) GetToken () string {
91
94
return a .token
92
95
}
93
96
94
97
// GetSecretKey returns the secret key
95
- func (a * auth ) GetSecretKey () string {
98
+ func (a * AuthCredentials ) GetSecretKey () string {
96
99
return a .secretKey
97
100
}
98
101
99
102
// GetAccessKey returns the access key
100
- func (a * auth ) GetAccessKey () string {
103
+ func (a * AuthCredentials ) GetAccessKey () string {
101
104
return a .accessKey
102
105
}
103
106
104
107
// Renew retrieves a new token and mutates it on an instance of the Auth struct
105
- func (a * auth ) Renew () error {
108
+ func (a * AuthCredentials ) Renew () error {
106
109
role , err := retrieveIAMRole ()
107
110
if err != nil {
108
111
return err
@@ -127,7 +130,7 @@ func (a *auth) Renew() error {
127
130
// Sign API request by
128
131
// http://docs.amazonwebservices.com/general/latest/gr/signature-version-4.html
129
132
130
- func (a * auth ) Sign (s * Service , t time.Time ) []byte {
133
+ func (a * AuthCredentials ) Sign (s * Service , t time.Time ) []byte {
131
134
h := ghmac ([]byte ("AWS4" + a .GetSecretKey ()), []byte (t .Format (iSO8601BasicFormatShort )))
132
135
h = ghmac (h , []byte (s .Region ))
133
136
h = ghmac (h , []byte (s .Name ))
@@ -138,7 +141,7 @@ func (a *auth) Sign(s *Service, t time.Time) []byte {
138
141
func retrieveAWSCredentials (role string ) (map [string ]string , error ) {
139
142
var bodybytes []byte
140
143
// Retrieve the json for this role
141
- resp , err := http .Get (AWS_IAM_CREDS_URL + "/" + role )
144
+ resp , err := http .Get (fmt . Sprintf ( "%s/%s" , AWSIAMCredsURL , role ) )
142
145
if err != nil || resp .StatusCode != http .StatusOK {
143
146
return nil , err
144
147
}
@@ -161,7 +164,7 @@ func retrieveAWSCredentials(role string) (map[string]string, error) {
161
164
func retrieveIAMRole () (string , error ) {
162
165
var bodybytes []byte
163
166
164
- resp , err := http .Get (AWS_IAM_CREDS_URL )
167
+ resp , err := http .Get (AWSIAMCredsURL )
165
168
if err != nil || resp .StatusCode != http .StatusOK {
166
169
return "" , err
167
170
}
0 commit comments