defaults.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // Package defaults is a collection of helpers to retrieve the SDK's default
  2. // configuration and handlers.
  3. //
  4. // Generally this package shouldn't be used directly, but session.Session
  5. // instead. This package is useful when you need to reset the defaults
  6. // of a session or service client to the SDK defaults before setting
  7. // additional parameters.
  8. package defaults
  9. import (
  10. "fmt"
  11. "net/http"
  12. "os"
  13. "time"
  14. "github.com/aws/aws-sdk-go/aws"
  15. "github.com/aws/aws-sdk-go/aws/corehandlers"
  16. "github.com/aws/aws-sdk-go/aws/credentials"
  17. "github.com/aws/aws-sdk-go/aws/credentials/ec2rolecreds"
  18. "github.com/aws/aws-sdk-go/aws/credentials/endpointcreds"
  19. "github.com/aws/aws-sdk-go/aws/ec2metadata"
  20. "github.com/aws/aws-sdk-go/aws/endpoints"
  21. "github.com/aws/aws-sdk-go/aws/request"
  22. )
  23. // A Defaults provides a collection of default values for SDK clients.
  24. type Defaults struct {
  25. Config *aws.Config
  26. Handlers request.Handlers
  27. }
  28. // Get returns the SDK's default values with Config and handlers pre-configured.
  29. func Get() Defaults {
  30. cfg := Config()
  31. handlers := Handlers()
  32. cfg.Credentials = CredChain(cfg, handlers)
  33. return Defaults{
  34. Config: cfg,
  35. Handlers: handlers,
  36. }
  37. }
  38. // Config returns the default configuration without credentials.
  39. // To retrieve a config with credentials also included use
  40. // `defaults.Get().Config` instead.
  41. //
  42. // Generally you shouldn't need to use this method directly, but
  43. // is available if you need to reset the configuration of an
  44. // existing service client or session.
  45. func Config() *aws.Config {
  46. return aws.NewConfig().
  47. WithCredentials(credentials.AnonymousCredentials).
  48. WithRegion(os.Getenv("AWS_REGION")).
  49. WithHTTPClient(http.DefaultClient).
  50. WithMaxRetries(aws.UseServiceDefaultRetries).
  51. WithLogger(aws.NewDefaultLogger()).
  52. WithLogLevel(aws.LogOff).
  53. WithEndpointResolver(endpoints.DefaultResolver())
  54. }
  55. // Handlers returns the default request handlers.
  56. //
  57. // Generally you shouldn't need to use this method directly, but
  58. // is available if you need to reset the request handlers of an
  59. // existing service client or session.
  60. func Handlers() request.Handlers {
  61. var handlers request.Handlers
  62. handlers.Validate.PushBackNamed(corehandlers.ValidateEndpointHandler)
  63. handlers.Validate.AfterEachFn = request.HandlerListStopOnError
  64. handlers.Build.PushBackNamed(corehandlers.SDKVersionUserAgentHandler)
  65. handlers.Build.AfterEachFn = request.HandlerListStopOnError
  66. handlers.Sign.PushBackNamed(corehandlers.BuildContentLengthHandler)
  67. handlers.Send.PushBackNamed(corehandlers.ValidateReqSigHandler)
  68. handlers.Send.PushBackNamed(corehandlers.SendHandler)
  69. handlers.AfterRetry.PushBackNamed(corehandlers.AfterRetryHandler)
  70. handlers.ValidateResponse.PushBackNamed(corehandlers.ValidateResponseHandler)
  71. return handlers
  72. }
  73. // CredChain returns the default credential chain.
  74. //
  75. // Generally you shouldn't need to use this method directly, but
  76. // is available if you need to reset the credentials of an
  77. // existing service client or session's Config.
  78. func CredChain(cfg *aws.Config, handlers request.Handlers) *credentials.Credentials {
  79. return credentials.NewCredentials(&credentials.ChainProvider{
  80. VerboseErrors: aws.BoolValue(cfg.CredentialsChainVerboseErrors),
  81. Providers: []credentials.Provider{
  82. &credentials.EnvProvider{},
  83. &credentials.SharedCredentialsProvider{Filename: "", Profile: ""},
  84. RemoteCredProvider(*cfg, handlers),
  85. },
  86. })
  87. }
  88. // RemoteCredProvider returns a credenitials provider for the default remote
  89. // endpoints such as EC2 or ECS Roles.
  90. func RemoteCredProvider(cfg aws.Config, handlers request.Handlers) credentials.Provider {
  91. ecsCredURI := os.Getenv("AWS_CONTAINER_CREDENTIALS_RELATIVE_URI")
  92. if len(ecsCredURI) > 0 {
  93. return ecsCredProvider(cfg, handlers, ecsCredURI)
  94. }
  95. return ec2RoleProvider(cfg, handlers)
  96. }
  97. func ecsCredProvider(cfg aws.Config, handlers request.Handlers, uri string) credentials.Provider {
  98. const host = `169.254.170.2`
  99. return endpointcreds.NewProviderClient(cfg, handlers,
  100. fmt.Sprintf("http://%s%s", host, uri),
  101. func(p *endpointcreds.Provider) {
  102. p.ExpiryWindow = 5 * time.Minute
  103. },
  104. )
  105. }
  106. func ec2RoleProvider(cfg aws.Config, handlers request.Handlers) credentials.Provider {
  107. resolver := cfg.EndpointResolver
  108. if resolver == nil {
  109. resolver = endpoints.DefaultResolver()
  110. }
  111. e, _ := resolver.EndpointFor(endpoints.Ec2metadataServiceID, "")
  112. return &ec2rolecreds.EC2RoleProvider{
  113. Client: ec2metadata.NewClient(cfg, handlers, e.URL, e.SigningRegion),
  114. ExpiryWindow: 5 * time.Minute,
  115. }
  116. }