config.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. package config
  2. import (
  3. "context"
  4. "github.com/aws/aws-sdk-go-v2/aws"
  5. )
  6. // defaultLoaders are a slice of functions that will read external configuration
  7. // sources for configuration values. These values are read by the AWSConfigResolvers
  8. // using interfaces to extract specific information from the external configuration.
  9. var defaultLoaders = []loader{
  10. loadEnvConfig,
  11. loadSharedConfigIgnoreNotExist,
  12. }
  13. // defaultAWSConfigResolvers are a slice of functions that will resolve external
  14. // configuration values into AWS configuration values.
  15. //
  16. // This will setup the AWS configuration's Region,
  17. var defaultAWSConfigResolvers = []awsConfigResolver{
  18. // Resolves the default configuration the SDK's aws.Config will be
  19. // initialized with.
  20. resolveDefaultAWSConfig,
  21. // Sets the logger to be used. Could be user provided logger, and client
  22. // logging mode.
  23. resolveLogger,
  24. resolveClientLogMode,
  25. // Sets the HTTP client and configuration to use for making requests using
  26. // the HTTP transport.
  27. resolveHTTPClient,
  28. resolveCustomCABundle,
  29. // Sets the endpoint resolving behavior the API Clients will use for making
  30. // requests to. Clients default to their own clients this allows overrides
  31. // to be specified. The resolveEndpointResolver option is deprecated, but
  32. // we still need to set it for backwards compatibility on config
  33. // construction.
  34. resolveEndpointResolver,
  35. resolveEndpointResolverWithOptions,
  36. // Sets the retry behavior API clients will use within their retry attempt
  37. // middleware. Defaults to unset, allowing API clients to define their own
  38. // retry behavior.
  39. resolveRetryer,
  40. // Sets the region the API Clients should use for making requests to.
  41. resolveRegion,
  42. resolveEC2IMDSRegion,
  43. resolveDefaultRegion,
  44. // Sets the additional set of middleware stack mutators that will custom
  45. // API client request pipeline middleware.
  46. resolveAPIOptions,
  47. // Resolves the DefaultsMode that should be used by SDK clients. If this
  48. // mode is set to DefaultsModeAuto.
  49. //
  50. // Comes after HTTPClient and CustomCABundle to ensure the HTTP client is
  51. // configured if provided before invoking IMDS if mode is auto. Comes
  52. // before resolving credentials so that those subsequent clients use the
  53. // configured auto mode.
  54. resolveDefaultsModeOptions,
  55. // Sets the resolved credentials the API clients will use for
  56. // authentication. Provides the SDK's default credential chain.
  57. //
  58. // Should probably be the last step in the resolve chain to ensure that all
  59. // other configurations are resolved first in case downstream credentials
  60. // implementations depend on or can be configured with earlier resolved
  61. // configuration options.
  62. resolveCredentials,
  63. // Sets the resolved bearer authentication token API clients will use for
  64. // httpBearerAuth authentication scheme.
  65. resolveBearerAuthToken,
  66. }
  67. // A Config represents a generic configuration value or set of values. This type
  68. // will be used by the AWSConfigResolvers to extract
  69. //
  70. // General the Config type will use type assertion against the Provider interfaces
  71. // to extract specific data from the Config.
  72. type Config interface{}
  73. // A loader is used to load external configuration data and returns it as
  74. // a generic Config type.
  75. //
  76. // The loader should return an error if it fails to load the external configuration
  77. // or the configuration data is malformed, or required components missing.
  78. type loader func(context.Context, configs) (Config, error)
  79. // An awsConfigResolver will extract configuration data from the configs slice
  80. // using the provider interfaces to extract specific functionality. The extracted
  81. // configuration values will be written to the AWS Config value.
  82. //
  83. // The resolver should return an error if it it fails to extract the data, the
  84. // data is malformed, or incomplete.
  85. type awsConfigResolver func(ctx context.Context, cfg *aws.Config, configs configs) error
  86. // configs is a slice of Config values. These values will be used by the
  87. // AWSConfigResolvers to extract external configuration values to populate the
  88. // AWS Config type.
  89. //
  90. // Use AppendFromLoaders to add additional external Config values that are
  91. // loaded from external sources.
  92. //
  93. // Use ResolveAWSConfig after external Config values have been added or loaded
  94. // to extract the loaded configuration values into the AWS Config.
  95. type configs []Config
  96. // AppendFromLoaders iterates over the slice of loaders passed in calling each
  97. // loader function in order. The external config value returned by the loader
  98. // will be added to the returned configs slice.
  99. //
  100. // If a loader returns an error this method will stop iterating and return
  101. // that error.
  102. func (cs configs) AppendFromLoaders(ctx context.Context, loaders []loader) (configs, error) {
  103. for _, fn := range loaders {
  104. cfg, err := fn(ctx, cs)
  105. if err != nil {
  106. return nil, err
  107. }
  108. cs = append(cs, cfg)
  109. }
  110. return cs, nil
  111. }
  112. // ResolveAWSConfig returns a AWS configuration populated with values by calling
  113. // the resolvers slice passed in. Each resolver is called in order. Any resolver
  114. // may overwrite the AWS Configuration value of a previous resolver.
  115. //
  116. // If an resolver returns an error this method will return that error, and stop
  117. // iterating over the resolvers.
  118. func (cs configs) ResolveAWSConfig(ctx context.Context, resolvers []awsConfigResolver) (aws.Config, error) {
  119. var cfg aws.Config
  120. for _, fn := range resolvers {
  121. if err := fn(ctx, &cfg, cs); err != nil {
  122. return aws.Config{}, err
  123. }
  124. }
  125. return cfg, nil
  126. }
  127. // ResolveConfig calls the provide function passing slice of configuration sources.
  128. // This implements the aws.ConfigResolver interface.
  129. func (cs configs) ResolveConfig(f func(configs []interface{}) error) error {
  130. var cfgs []interface{}
  131. for i := range cs {
  132. cfgs = append(cfgs, cs[i])
  133. }
  134. return f(cfgs)
  135. }
  136. // LoadDefaultConfig reads the SDK's default external configurations, and
  137. // populates an AWS Config with the values from the external configurations.
  138. //
  139. // An optional variadic set of additional Config values can be provided as input
  140. // that will be prepended to the configs slice. Use this to add custom configuration.
  141. // The custom configurations must satisfy the respective providers for their data
  142. // or the custom data will be ignored by the resolvers and config loaders.
  143. //
  144. // cfg, err := config.LoadDefaultConfig( context.TODO(),
  145. // WithSharedConfigProfile("test-profile"),
  146. // )
  147. // if err != nil {
  148. // panic(fmt.Sprintf("failed loading config, %v", err))
  149. // }
  150. //
  151. // The default configuration sources are:
  152. // * Environment Variables
  153. // * Shared Configuration and Shared Credentials files.
  154. func LoadDefaultConfig(ctx context.Context, optFns ...func(*LoadOptions) error) (cfg aws.Config, err error) {
  155. var options LoadOptions
  156. for _, optFn := range optFns {
  157. if err := optFn(&options); err != nil {
  158. return aws.Config{}, err
  159. }
  160. }
  161. // assign Load Options to configs
  162. var cfgCpy = configs{options}
  163. cfgCpy, err = cfgCpy.AppendFromLoaders(ctx, defaultLoaders)
  164. if err != nil {
  165. return aws.Config{}, err
  166. }
  167. cfg, err = cfgCpy.ResolveAWSConfig(ctx, defaultAWSConfigResolvers)
  168. if err != nil {
  169. return aws.Config{}, err
  170. }
  171. return cfg, nil
  172. }