forked from aws/aws-sdk-go-v2
/
config.go
132 lines (114 loc) · 4.56 KB
/
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
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
package external
import (
"github.com/aws/aws-sdk-go-v2/aws"
)
// DefaultConfigLoaders are a slice of functions that will read external configuration
// sources for configuration values. These values are read by the AWSConfigResolvers
// using interfaces to extract specific information from the external configuration.
var DefaultConfigLoaders = []ConfigLoader{
LoadEnvConfig,
LoadSharedConfigIgnoreNotExist,
}
// DefaultAWSConfigResolvers are a slice of functions that will resolve external
// configuration values into AWS configuration values.
//
// This will setup the AWS configuration's Region,
var DefaultAWSConfigResolvers = []AWSConfigResolver{
ResolveDefaultAWSConfig,
ResolveCustomCABundle,
ResolveRegion,
ResolveFallbackEC2Credentials, // Initial defauilt credentails provider.
ResolveCredentialsValue,
ResolveEndpointCredentials,
ResolveContainerEndpointPathCredentials, // TODO is this order right?
ResolveAssumeRoleCredentials,
}
// A Config represents a generic configuration value or set of values. This type
// will be used by the AWSConfigResolvers to extract
//
// General the Config type will use type assertion against the Provider interfaces
// to extract specific data from the Config.
type Config interface{}
// A ConfigLoader is used to load external configuration data and returns it as
// a generic Config type.
//
// The loader should return an error if it fails to load the external configuration
// or the configuration data is malformed, or required components missing.
type ConfigLoader func(Configs) (Config, error)
// An AWSConfigResolver will extract configuration data from the Configs slice
// using the provider interfaces to extract specific functionality. The extracted
// configuration values will be written to the AWS Config value.
//
// The resolver should return an error if it it fails to extract the data, the
// data is malformed, or incomplete.
type AWSConfigResolver func(cfg *aws.Config, configs Configs) error
// Configs is a slice of Config values. These values will be used by the
// AWSConfigResolvers to extract external configuration values to populate the
// AWS Config type.
//
// Use AppendFromLoaders to add additional external Config values that are
// loaded from external sources.
//
// Use ResolveAWSConfig after external Config values have been added or loaded
// to extract the loaded configuration values into the AWS Config.
type Configs []Config
// AppendFromLoaders iterates over the slice of loaders passed in calling each
// loader function in order. The external config value returned by the loader
// will be added to the returned Configs slice.
//
// If a loader returns an error this method will stop iterating and return
// that error.
func (cs Configs) AppendFromLoaders(loaders []ConfigLoader) (Configs, error) {
for _, fn := range loaders {
cfg, err := fn(cs)
if err != nil {
return nil, err
}
cs = append(cs, cfg)
}
return cs, nil
}
// ResolveAWSConfig returns a AWS configuration populated with values by calling
// the resolvers slice passed in. Each resolver is called in order. Any resolver
// may overwrite the AWs Configuration value of a previous resolver.
//
// If an resolver returns an error this method will return that error, and stop
// iterating over the resolvers.
func (cs Configs) ResolveAWSConfig(resolvers []AWSConfigResolver) (aws.Config, error) {
var cfg aws.Config
for _, fn := range resolvers {
if err := fn(&cfg, cs); err != nil {
// TODO provide better error?
return aws.Config{}, err
}
}
return cfg, nil
}
// LoadDefaultAWSConfig reads the SDK's default external configurations, and
// populates an AWS Config with the values from the external configurations.
//
// An optional variadic set of additional Config values can be provided as input
// that will be prepended to the Configs slice. Use this to add custom configuration.
// The custom configurations must satisfy the respective providers for their data
// or the custom data will be ignored by the resolvers and config loaders.
//
// cfg, err := external.LoadDefaultAWSConfig(
// WithSharedConfigProfile("test-profile"),
// )
// if err != nil {
// panic(fmt.Sprintf("failed loading config, %v", err))
// }
//
//
// The default configuration sources are:
// * Environment Variables
// * Shared Configuration and Shared Credentials files.
func LoadDefaultAWSConfig(configs ...Config) (aws.Config, error) {
var cfgs Configs
cfgs = append(cfgs, configs...)
cfgs, err := cfgs.AppendFromLoaders(DefaultConfigLoaders)
if err != nil {
return aws.Config{}, err
}
return cfgs.ResolveAWSConfig(DefaultAWSConfigResolvers)
}