forked from aws/aws-sdk-go-v2
/
provider.go
325 lines (277 loc) · 9.54 KB
/
provider.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
package external
import (
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/aws/ec2metadata"
)
// SharedConfigProfileProvider provides access to the shared config profile
// name external configuration value.
type SharedConfigProfileProvider interface {
GetSharedConfigProfile() (string, error)
}
// WithSharedConfigProfile wraps a strings to satisfy the SharedConfigProfileProvider
// interface so a slice of custom shared config files ared used when loading the
// SharedConfig.
type WithSharedConfigProfile string
// GetSharedConfigProfile returns the shared config profile.
func (c WithSharedConfigProfile) GetSharedConfigProfile() (string, error) {
return string(c), nil
}
// GetSharedConfigProfile searchds the Configs for a SharedConfigProfileProvider
// and returns the value if found. Returns an error if a provider fails before a
// value is found.
func GetSharedConfigProfile(configs Configs) (string, bool, error) {
for _, cfg := range configs {
if p, ok := cfg.(SharedConfigProfileProvider); ok {
v, err := p.GetSharedConfigProfile()
if err != nil {
return "", false, err
}
if len(v) > 0 {
return v, true, nil
}
}
}
return "", false, nil
}
// SharedConfigFilesProvider provides access to the shared config filesnames
// external configuration value.
type SharedConfigFilesProvider interface {
GetSharedConfigFiles() ([]string, error)
}
// WithSharedConfigFiles wraps a slice of strings to satisfy the
// SharedConfigFilesProvider interface so a slice of custom shared config files
// ared used when loading the SharedConfig.
type WithSharedConfigFiles []string
// GetSharedConfigFiles returns the slice of shared config files.
func (c WithSharedConfigFiles) GetSharedConfigFiles() ([]string, error) {
return []string(c), nil
}
// GetSharedConfigFiles searchds the Configs for a SharedConfigFilesProvider
// and returns the value if found. Returns an error if a provider fails before a
// value is found.
func GetSharedConfigFiles(configs Configs) ([]string, bool, error) {
for _, cfg := range configs {
if p, ok := cfg.(SharedConfigFilesProvider); ok {
v, err := p.GetSharedConfigFiles()
if err != nil {
return nil, false, err
}
if len(v) > 0 {
return v, true, nil
}
}
}
return nil, false, nil
}
// CustomCABundleProvider provides access to the custom CA bundle PEM bytes.
type CustomCABundleProvider interface {
GetCustomCABundle() ([]byte, error)
}
// WithCustomCABundle provides wrapping of a region string to satisfy the
// CustomCABundleProvider interface.
type WithCustomCABundle []byte
// GetCustomCABundle returns the CA bundle PEM bytes.
func (v WithCustomCABundle) GetCustomCABundle() ([]byte, error) {
return []byte(v), nil
}
// GetCustomCABundle searchds the Configs for a CustomCABundleProvider
// and returns the value if found. Returns an error if a provider fails before a
// value is found.
func GetCustomCABundle(configs Configs) ([]byte, bool, error) {
for _, cfg := range configs {
if p, ok := cfg.(CustomCABundleProvider); ok {
v, err := p.GetCustomCABundle()
if err != nil {
return nil, false, err
}
if len(v) > 0 {
return v, true, nil
}
}
}
return nil, false, nil
}
// RegionProvider provides access to the region external configuration value.
type RegionProvider interface {
GetRegion() (string, error)
}
// WithRegion provides wrapping of a region string to satisfy the RegionProvider
// interface.
type WithRegion string
// GetRegion returns the region string.
func (v WithRegion) GetRegion() (string, error) {
return string(v), nil
}
// GetRegion searchds the Configs for a RegionProvider and returns the value
// if found. Returns an error if a provider fails before a value is found.
func GetRegion(configs Configs) (string, bool, error) {
for _, cfg := range configs {
if p, ok := cfg.(RegionProvider); ok {
v, err := p.GetRegion()
if err != nil {
return "", false, err
}
if len(v) > 0 {
return v, true, nil
}
}
}
return "", false, nil
}
// CredentialsValueProvider provides access to the credentials external
// configuration value.
type CredentialsValueProvider interface {
GetCredentialsValue() (aws.Credentials, error)
}
// WithCredentialsValue provides wrapping of a credentials Value to satisfy the
// CredentialsValueProvider interface.
type WithCredentialsValue aws.Credentials
// GetCredentialsValue returns the credentials value.
func (v WithCredentialsValue) GetCredentialsValue() (aws.Credentials, error) {
return aws.Credentials(v), nil
}
// GetCredentialsValue searchds the Configs for a CredentialsValueProvider
// and returns the value if found. Returns an error if a provider fails before a
// value is found.
func GetCredentialsValue(configs Configs) (aws.Credentials, bool, error) {
for _, cfg := range configs {
if p, ok := cfg.(CredentialsValueProvider); ok {
v, err := p.GetCredentialsValue()
if err != nil {
return aws.Credentials{}, false, err
}
if v.HasKeys() {
return v, true, nil
}
}
}
return aws.Credentials{}, false, nil
}
// CredentialsEndpointProvider provides access to the credentials endpoint
// external configuration value.
type CredentialsEndpointProvider interface {
GetCredentialsEndpoint() (string, error)
}
// WithCredentialsEndpoint provides wrapping of a string to satisfy the
// CredentialsEndpointProvider interface.
type WithCredentialsEndpoint string
// GetCredentialsEndpoint returns the endpoint.
func (p WithCredentialsEndpoint) GetCredentialsEndpoint() (string, error) {
return string(p), nil
}
// GetCredentialsEndpoint searchds the Configs for a CredentialsEndpointProvider
// and returns the value if found. Returns an error if a provider fails before a
// value is found.
func GetCredentialsEndpoint(configs Configs) (string, bool, error) {
for _, cfg := range configs {
if p, ok := cfg.(CredentialsEndpointProvider); ok {
v, err := p.GetCredentialsEndpoint()
if err != nil {
return "", false, err
}
if len(v) > 0 {
return v, true, nil
}
}
}
return "", false, nil
}
// ContainerCredentialsEndpointPathProvider provides access to the credentials endpoint path
// external configuration value.
type ContainerCredentialsEndpointPathProvider interface {
GetContainerCredentialsEndpointPath() (string, error)
}
// WithContainerCredentialsEndpointPath provides wrapping of a string to satisfy the
// ContainerCredentialsEndpointPathProvider interface.
type WithContainerCredentialsEndpointPath string
// GetContainerCredentialsEndpointPath returns the endpoint path.
func (p WithContainerCredentialsEndpointPath) GetContainerCredentialsEndpointPath() (string, error) {
return string(p), nil
}
// GetContainerCredentialsEndpointPath searchds the Configs for a
// ContainerCredentialsEndpointPathProvider and returns the value if found.
// Returns an error if a provider fails before a
// value is found.
func GetContainerCredentialsEndpointPath(configs Configs) (string, bool, error) {
for _, cfg := range configs {
if p, ok := cfg.(ContainerCredentialsEndpointPathProvider); ok {
v, err := p.GetContainerCredentialsEndpointPath()
if err != nil {
return "", false, err
}
if len(v) > 0 {
return v, true, nil
}
}
}
return "", false, nil
}
// AssumeRoleConfigProvider provides access to the assume role config
// external configuration value.
type AssumeRoleConfigProvider interface {
GetAssumeRoleConfig() (AssumeRoleConfig, error)
}
// WithAssumeRoleConfig provides wrapping of a string to satisfy the
// AssumeRoleConfigProvider interface.
type WithAssumeRoleConfig AssumeRoleConfig
// GetAssumeRoleConfig returns the AssumeRoleConfig.
func (p WithAssumeRoleConfig) GetAssumeRoleConfig() (AssumeRoleConfig, error) {
return AssumeRoleConfig(p), nil
}
// GetAssumeRoleConfig searchds the Configs for a AssumeRoleConfigProvider
// and returns the value if found. Returns an error if a provider fails before a
// value is found.
func GetAssumeRoleConfig(configs Configs) (AssumeRoleConfig, bool, error) {
for _, cfg := range configs {
if p, ok := cfg.(AssumeRoleConfigProvider); ok {
v, err := p.GetAssumeRoleConfig()
if err != nil {
return AssumeRoleConfig{}, false, err
}
if len(v.RoleARN) > 0 && v.Source != nil {
return v, true, nil
}
}
}
return AssumeRoleConfig{}, false, nil
}
// MFATokenFuncProvider provides access to the MFA token function needed for
// Assume Role with MFA.
type MFATokenFuncProvider interface {
GetMFATokenFunc() (func() (string, error), error)
}
// WithMFATokenFunc provides wrapping of a string to satisfy the
// MFATokenFuncProvider interface.
type WithMFATokenFunc func() (string, error)
// GetMFATokenFunc returns the MFA Token function.
func (p WithMFATokenFunc) GetMFATokenFunc() (func() (string, error), error) {
return p, nil
}
// GetMFATokenFunc searchds the Configs for a MFATokenFuncProvider
// and returns the value if found. Returns an error if a provider fails before a
// value is found.
func GetMFATokenFunc(configs Configs) (func() (string, error), bool, error) {
for _, cfg := range configs {
if p, ok := cfg.(MFATokenFuncProvider); ok {
v, err := p.GetMFATokenFunc()
if err != nil {
return nil, false, err
}
if v != nil {
return v, true, nil
}
}
}
return nil, false, nil
}
// WithEC2MetadataRegion provides a RegionProvider that retrieves the region
// from the EC2 Metadata service.
//
// TODO add this provider to the default config loading?
type WithEC2MetadataRegion struct {
Client *ec2metadata.EC2Metadata
}
// GetRegion attempts to retreive the region from EC2 Metadata service.
func (p WithEC2MetadataRegion) GetRegion() (string, error) {
return p.Client.Region()
}