18
18
package credentials
19
19
20
20
import (
21
+ "net/http"
21
22
"sync"
22
23
"time"
23
24
)
@@ -30,6 +31,10 @@ const (
30
31
defaultExpiryWindow = 0.8
31
32
)
32
33
34
+ // defaultCredContext is used when the credential context doesn't
35
+ // actually matter or the default context is suitable.
36
+ var defaultCredContext = & CredContext {Client : http .DefaultClient }
37
+
33
38
// A Value is the S3 credentials value for individual credential fields.
34
39
type Value struct {
35
40
// S3 Access key ID
@@ -54,13 +59,21 @@ type Value struct {
54
59
type Provider interface {
55
60
// Retrieve returns nil if it successfully retrieved the value.
56
61
// Error is returned if the value were not obtainable, or empty.
57
- Retrieve () (Value , error )
62
+ Retrieve (cc * CredContext ) (Value , error )
58
63
59
64
// IsExpired returns if the credentials are no longer valid, and need
60
65
// to be retrieved.
61
66
IsExpired () bool
62
67
}
63
68
69
+ // CredContext is passed to the Retrieve function of a provider to provide
70
+ // some additional context to retrieve credentials.
71
+ type CredContext struct {
72
+ // Client specifies the HTTP client that should be used if an HTTP
73
+ // request is to be made to fetch the credentials.
74
+ Client * http.Client
75
+ }
76
+
64
77
// A Expiry provides shared expiration logic to be used by credentials
65
78
// providers to implement expiry functionality.
66
79
//
@@ -146,7 +159,24 @@ func New(provider Provider) *Credentials {
146
159
//
147
160
// If Credentials.Expire() was called the credentials Value will be force
148
161
// expired, and the next call to Get() will cause them to be refreshed.
162
+ //
163
+ // Deprecated: Get() exists for historical compatibility and should not be
164
+ // used. To get new credentials use the Credentials.GetWithContext function
165
+ // to ensure the proper context (i.e. HTTP client) will be used.
149
166
func (c * Credentials ) Get () (Value , error ) {
167
+ return c .GetWithContext (defaultCredContext )
168
+ }
169
+
170
+ // GetWithContext returns the credentials value, or error if the
171
+ // credentials Value failed to be retrieved.
172
+ //
173
+ // Will return the cached credentials Value if it has not expired. If the
174
+ // credentials Value has expired the Provider's Retrieve() will be called
175
+ // to refresh the credentials.
176
+ //
177
+ // If Credentials.Expire() was called the credentials Value will be force
178
+ // expired, and the next call to Get() will cause them to be refreshed.
179
+ func (c * Credentials ) GetWithContext (cc * CredContext ) (Value , error ) {
150
180
if c == nil {
151
181
return Value {}, nil
152
182
}
@@ -155,7 +185,7 @@ func (c *Credentials) Get() (Value, error) {
155
185
defer c .Unlock ()
156
186
157
187
if c .isExpired () {
158
- creds , err := c .provider .Retrieve ()
188
+ creds , err := c .provider .Retrieve (cc )
159
189
if err != nil {
160
190
return Value {}, err
161
191
}
0 commit comments