session.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  1. package session
  2. import (
  3. "crypto/tls"
  4. "crypto/x509"
  5. "fmt"
  6. "io"
  7. "io/ioutil"
  8. "net/http"
  9. "os"
  10. "github.com/aws/aws-sdk-go/aws"
  11. "github.com/aws/aws-sdk-go/aws/awserr"
  12. "github.com/aws/aws-sdk-go/aws/client"
  13. "github.com/aws/aws-sdk-go/aws/corehandlers"
  14. "github.com/aws/aws-sdk-go/aws/credentials"
  15. "github.com/aws/aws-sdk-go/aws/credentials/stscreds"
  16. "github.com/aws/aws-sdk-go/aws/csm"
  17. "github.com/aws/aws-sdk-go/aws/defaults"
  18. "github.com/aws/aws-sdk-go/aws/endpoints"
  19. "github.com/aws/aws-sdk-go/aws/request"
  20. )
  21. // A Session provides a central location to create service clients from and
  22. // store configurations and request handlers for those services.
  23. //
  24. // Sessions are safe to create service clients concurrently, but it is not safe
  25. // to mutate the Session concurrently.
  26. //
  27. // The Session satisfies the service client's client.ConfigProvider.
  28. type Session struct {
  29. Config *aws.Config
  30. Handlers request.Handlers
  31. }
  32. // New creates a new instance of the handlers merging in the provided configs
  33. // on top of the SDK's default configurations. Once the Session is created it
  34. // can be mutated to modify the Config or Handlers. The Session is safe to be
  35. // read concurrently, but it should not be written to concurrently.
  36. //
  37. // If the AWS_SDK_LOAD_CONFIG environment is set to a truthy value, the New
  38. // method could now encounter an error when loading the configuration. When
  39. // The environment variable is set, and an error occurs, New will return a
  40. // session that will fail all requests reporting the error that occurred while
  41. // loading the session. Use NewSession to get the error when creating the
  42. // session.
  43. //
  44. // If the AWS_SDK_LOAD_CONFIG environment variable is set to a truthy value
  45. // the shared config file (~/.aws/config) will also be loaded, in addition to
  46. // the shared credentials file (~/.aws/credentials). Values set in both the
  47. // shared config, and shared credentials will be taken from the shared
  48. // credentials file.
  49. //
  50. // Deprecated: Use NewSession functions to create sessions instead. NewSession
  51. // has the same functionality as New except an error can be returned when the
  52. // func is called instead of waiting to receive an error until a request is made.
  53. func New(cfgs ...*aws.Config) *Session {
  54. // load initial config from environment
  55. envCfg := loadEnvConfig()
  56. if envCfg.EnableSharedConfig {
  57. var cfg aws.Config
  58. cfg.MergeIn(cfgs...)
  59. s, err := NewSessionWithOptions(Options{
  60. Config: cfg,
  61. SharedConfigState: SharedConfigEnable,
  62. })
  63. if err != nil {
  64. // Old session.New expected all errors to be discovered when
  65. // a request is made, and would report the errors then. This
  66. // needs to be replicated if an error occurs while creating
  67. // the session.
  68. msg := "failed to create session with AWS_SDK_LOAD_CONFIG enabled. " +
  69. "Use session.NewSession to handle errors occurring during session creation."
  70. // Session creation failed, need to report the error and prevent
  71. // any requests from succeeding.
  72. s = &Session{Config: defaults.Config()}
  73. s.Config.MergeIn(cfgs...)
  74. s.Config.Logger.Log("ERROR:", msg, "Error:", err)
  75. s.Handlers.Validate.PushBack(func(r *request.Request) {
  76. r.Error = err
  77. })
  78. }
  79. return s
  80. }
  81. s := deprecatedNewSession(cfgs...)
  82. if envCfg.CSMEnabled {
  83. enableCSM(&s.Handlers, envCfg.CSMClientID, envCfg.CSMPort, s.Config.Logger)
  84. }
  85. return s
  86. }
  87. // NewSession returns a new Session created from SDK defaults, config files,
  88. // environment, and user provided config files. Once the Session is created
  89. // it can be mutated to modify the Config or Handlers. The Session is safe to
  90. // be read concurrently, but it should not be written to concurrently.
  91. //
  92. // If the AWS_SDK_LOAD_CONFIG environment variable is set to a truthy value
  93. // the shared config file (~/.aws/config) will also be loaded in addition to
  94. // the shared credentials file (~/.aws/credentials). Values set in both the
  95. // shared config, and shared credentials will be taken from the shared
  96. // credentials file. Enabling the Shared Config will also allow the Session
  97. // to be built with retrieving credentials with AssumeRole set in the config.
  98. //
  99. // See the NewSessionWithOptions func for information on how to override or
  100. // control through code how the Session will be created. Such as specifying the
  101. // config profile, and controlling if shared config is enabled or not.
  102. func NewSession(cfgs ...*aws.Config) (*Session, error) {
  103. opts := Options{}
  104. opts.Config.MergeIn(cfgs...)
  105. return NewSessionWithOptions(opts)
  106. }
  107. // SharedConfigState provides the ability to optionally override the state
  108. // of the session's creation based on the shared config being enabled or
  109. // disabled.
  110. type SharedConfigState int
  111. const (
  112. // SharedConfigStateFromEnv does not override any state of the
  113. // AWS_SDK_LOAD_CONFIG env var. It is the default value of the
  114. // SharedConfigState type.
  115. SharedConfigStateFromEnv SharedConfigState = iota
  116. // SharedConfigDisable overrides the AWS_SDK_LOAD_CONFIG env var value
  117. // and disables the shared config functionality.
  118. SharedConfigDisable
  119. // SharedConfigEnable overrides the AWS_SDK_LOAD_CONFIG env var value
  120. // and enables the shared config functionality.
  121. SharedConfigEnable
  122. )
  123. // Options provides the means to control how a Session is created and what
  124. // configuration values will be loaded.
  125. //
  126. type Options struct {
  127. // Provides config values for the SDK to use when creating service clients
  128. // and making API requests to services. Any value set in with this field
  129. // will override the associated value provided by the SDK defaults,
  130. // environment or config files where relevant.
  131. //
  132. // If not set, configuration values from from SDK defaults, environment,
  133. // config will be used.
  134. Config aws.Config
  135. // Overrides the config profile the Session should be created from. If not
  136. // set the value of the environment variable will be loaded (AWS_PROFILE,
  137. // or AWS_DEFAULT_PROFILE if the Shared Config is enabled).
  138. //
  139. // If not set and environment variables are not set the "default"
  140. // (DefaultSharedConfigProfile) will be used as the profile to load the
  141. // session config from.
  142. Profile string
  143. // Instructs how the Session will be created based on the AWS_SDK_LOAD_CONFIG
  144. // environment variable. By default a Session will be created using the
  145. // value provided by the AWS_SDK_LOAD_CONFIG environment variable.
  146. //
  147. // Setting this value to SharedConfigEnable or SharedConfigDisable
  148. // will allow you to override the AWS_SDK_LOAD_CONFIG environment variable
  149. // and enable or disable the shared config functionality.
  150. SharedConfigState SharedConfigState
  151. // Ordered list of files the session will load configuration from.
  152. // It will override environment variable AWS_SHARED_CREDENTIALS_FILE, AWS_CONFIG_FILE.
  153. SharedConfigFiles []string
  154. // When the SDK's shared config is configured to assume a role with MFA
  155. // this option is required in order to provide the mechanism that will
  156. // retrieve the MFA token. There is no default value for this field. If
  157. // it is not set an error will be returned when creating the session.
  158. //
  159. // This token provider will be called when ever the assumed role's
  160. // credentials need to be refreshed. Within the context of service clients
  161. // all sharing the same session the SDK will ensure calls to the token
  162. // provider are atomic. When sharing a token provider across multiple
  163. // sessions additional synchronization logic is needed to ensure the
  164. // token providers do not introduce race conditions. It is recommend to
  165. // share the session where possible.
  166. //
  167. // stscreds.StdinTokenProvider is a basic implementation that will prompt
  168. // from stdin for the MFA token code.
  169. //
  170. // This field is only used if the shared configuration is enabled, and
  171. // the config enables assume role wit MFA via the mfa_serial field.
  172. AssumeRoleTokenProvider func() (string, error)
  173. // Reader for a custom Credentials Authority (CA) bundle in PEM format that
  174. // the SDK will use instead of the default system's root CA bundle. Use this
  175. // only if you want to replace the CA bundle the SDK uses for TLS requests.
  176. //
  177. // Enabling this option will attempt to merge the Transport into the SDK's HTTP
  178. // client. If the client's Transport is not a http.Transport an error will be
  179. // returned. If the Transport's TLS config is set this option will cause the SDK
  180. // to overwrite the Transport's TLS config's RootCAs value. If the CA
  181. // bundle reader contains multiple certificates all of them will be loaded.
  182. //
  183. // The Session option CustomCABundle is also available when creating sessions
  184. // to also enable this feature. CustomCABundle session option field has priority
  185. // over the AWS_CA_BUNDLE environment variable, and will be used if both are set.
  186. CustomCABundle io.Reader
  187. }
  188. // NewSessionWithOptions returns a new Session created from SDK defaults, config files,
  189. // environment, and user provided config files. This func uses the Options
  190. // values to configure how the Session is created.
  191. //
  192. // If the AWS_SDK_LOAD_CONFIG environment variable is set to a truthy value
  193. // the shared config file (~/.aws/config) will also be loaded in addition to
  194. // the shared credentials file (~/.aws/credentials). Values set in both the
  195. // shared config, and shared credentials will be taken from the shared
  196. // credentials file. Enabling the Shared Config will also allow the Session
  197. // to be built with retrieving credentials with AssumeRole set in the config.
  198. //
  199. // // Equivalent to session.New
  200. // sess := session.Must(session.NewSessionWithOptions(session.Options{}))
  201. //
  202. // // Specify profile to load for the session's config
  203. // sess := session.Must(session.NewSessionWithOptions(session.Options{
  204. // Profile: "profile_name",
  205. // }))
  206. //
  207. // // Specify profile for config and region for requests
  208. // sess := session.Must(session.NewSessionWithOptions(session.Options{
  209. // Config: aws.Config{Region: aws.String("us-east-1")},
  210. // Profile: "profile_name",
  211. // }))
  212. //
  213. // // Force enable Shared Config support
  214. // sess := session.Must(session.NewSessionWithOptions(session.Options{
  215. // SharedConfigState: session.SharedConfigEnable,
  216. // }))
  217. func NewSessionWithOptions(opts Options) (*Session, error) {
  218. var envCfg envConfig
  219. if opts.SharedConfigState == SharedConfigEnable {
  220. envCfg = loadSharedEnvConfig()
  221. } else {
  222. envCfg = loadEnvConfig()
  223. }
  224. if len(opts.Profile) > 0 {
  225. envCfg.Profile = opts.Profile
  226. }
  227. switch opts.SharedConfigState {
  228. case SharedConfigDisable:
  229. envCfg.EnableSharedConfig = false
  230. case SharedConfigEnable:
  231. envCfg.EnableSharedConfig = true
  232. }
  233. // Only use AWS_CA_BUNDLE if session option is not provided.
  234. if len(envCfg.CustomCABundle) != 0 && opts.CustomCABundle == nil {
  235. f, err := os.Open(envCfg.CustomCABundle)
  236. if err != nil {
  237. return nil, awserr.New("LoadCustomCABundleError",
  238. "failed to open custom CA bundle PEM file", err)
  239. }
  240. defer f.Close()
  241. opts.CustomCABundle = f
  242. }
  243. return newSession(opts, envCfg, &opts.Config)
  244. }
  245. // Must is a helper function to ensure the Session is valid and there was no
  246. // error when calling a NewSession function.
  247. //
  248. // This helper is intended to be used in variable initialization to load the
  249. // Session and configuration at startup. Such as:
  250. //
  251. // var sess = session.Must(session.NewSession())
  252. func Must(sess *Session, err error) *Session {
  253. if err != nil {
  254. panic(err)
  255. }
  256. return sess
  257. }
  258. func deprecatedNewSession(cfgs ...*aws.Config) *Session {
  259. cfg := defaults.Config()
  260. handlers := defaults.Handlers()
  261. // Apply the passed in configs so the configuration can be applied to the
  262. // default credential chain
  263. cfg.MergeIn(cfgs...)
  264. if cfg.EndpointResolver == nil {
  265. // An endpoint resolver is required for a session to be able to provide
  266. // endpoints for service client configurations.
  267. cfg.EndpointResolver = endpoints.DefaultResolver()
  268. }
  269. cfg.Credentials = defaults.CredChain(cfg, handlers)
  270. // Reapply any passed in configs to override credentials if set
  271. cfg.MergeIn(cfgs...)
  272. s := &Session{
  273. Config: cfg,
  274. Handlers: handlers,
  275. }
  276. initHandlers(s)
  277. return s
  278. }
  279. func enableCSM(handlers *request.Handlers, clientID string, port string, logger aws.Logger) {
  280. logger.Log("Enabling CSM")
  281. if len(port) == 0 {
  282. port = csm.DefaultPort
  283. }
  284. r, err := csm.Start(clientID, "127.0.0.1:"+port)
  285. if err != nil {
  286. return
  287. }
  288. r.InjectHandlers(handlers)
  289. }
  290. func newSession(opts Options, envCfg envConfig, cfgs ...*aws.Config) (*Session, error) {
  291. cfg := defaults.Config()
  292. handlers := defaults.Handlers()
  293. // Get a merged version of the user provided config to determine if
  294. // credentials were.
  295. userCfg := &aws.Config{}
  296. userCfg.MergeIn(cfgs...)
  297. // Ordered config files will be loaded in with later files overwriting
  298. // previous config file values.
  299. var cfgFiles []string
  300. if opts.SharedConfigFiles != nil {
  301. cfgFiles = opts.SharedConfigFiles
  302. } else {
  303. cfgFiles = []string{envCfg.SharedConfigFile, envCfg.SharedCredentialsFile}
  304. if !envCfg.EnableSharedConfig {
  305. // The shared config file (~/.aws/config) is only loaded if instructed
  306. // to load via the envConfig.EnableSharedConfig (AWS_SDK_LOAD_CONFIG).
  307. cfgFiles = cfgFiles[1:]
  308. }
  309. }
  310. // Load additional config from file(s)
  311. sharedCfg, err := loadSharedConfig(envCfg.Profile, cfgFiles)
  312. if err != nil {
  313. return nil, err
  314. }
  315. if err := mergeConfigSrcs(cfg, userCfg, envCfg, sharedCfg, handlers, opts); err != nil {
  316. return nil, err
  317. }
  318. s := &Session{
  319. Config: cfg,
  320. Handlers: handlers,
  321. }
  322. initHandlers(s)
  323. if envCfg.CSMEnabled {
  324. enableCSM(&s.Handlers, envCfg.CSMClientID, envCfg.CSMPort, s.Config.Logger)
  325. }
  326. // Setup HTTP client with custom cert bundle if enabled
  327. if opts.CustomCABundle != nil {
  328. if err := loadCustomCABundle(s, opts.CustomCABundle); err != nil {
  329. return nil, err
  330. }
  331. }
  332. return s, nil
  333. }
  334. func loadCustomCABundle(s *Session, bundle io.Reader) error {
  335. var t *http.Transport
  336. switch v := s.Config.HTTPClient.Transport.(type) {
  337. case *http.Transport:
  338. t = v
  339. default:
  340. if s.Config.HTTPClient.Transport != nil {
  341. return awserr.New("LoadCustomCABundleError",
  342. "unable to load custom CA bundle, HTTPClient's transport unsupported type", nil)
  343. }
  344. }
  345. if t == nil {
  346. t = &http.Transport{}
  347. }
  348. p, err := loadCertPool(bundle)
  349. if err != nil {
  350. return err
  351. }
  352. if t.TLSClientConfig == nil {
  353. t.TLSClientConfig = &tls.Config{}
  354. }
  355. t.TLSClientConfig.RootCAs = p
  356. s.Config.HTTPClient.Transport = t
  357. return nil
  358. }
  359. func loadCertPool(r io.Reader) (*x509.CertPool, error) {
  360. b, err := ioutil.ReadAll(r)
  361. if err != nil {
  362. return nil, awserr.New("LoadCustomCABundleError",
  363. "failed to read custom CA bundle PEM file", err)
  364. }
  365. p := x509.NewCertPool()
  366. if !p.AppendCertsFromPEM(b) {
  367. return nil, awserr.New("LoadCustomCABundleError",
  368. "failed to load custom CA bundle PEM file", err)
  369. }
  370. return p, nil
  371. }
  372. func mergeConfigSrcs(cfg, userCfg *aws.Config, envCfg envConfig, sharedCfg sharedConfig, handlers request.Handlers, sessOpts Options) error {
  373. // Merge in user provided configuration
  374. cfg.MergeIn(userCfg)
  375. // Region if not already set by user
  376. if len(aws.StringValue(cfg.Region)) == 0 {
  377. if len(envCfg.Region) > 0 {
  378. cfg.WithRegion(envCfg.Region)
  379. } else if envCfg.EnableSharedConfig && len(sharedCfg.Region) > 0 {
  380. cfg.WithRegion(sharedCfg.Region)
  381. }
  382. }
  383. // Configure credentials if not already set
  384. if cfg.Credentials == credentials.AnonymousCredentials && userCfg.Credentials == nil {
  385. if len(envCfg.Creds.AccessKeyID) > 0 {
  386. cfg.Credentials = credentials.NewStaticCredentialsFromCreds(
  387. envCfg.Creds,
  388. )
  389. } else if envCfg.EnableSharedConfig && len(sharedCfg.AssumeRole.RoleARN) > 0 && sharedCfg.AssumeRoleSource != nil {
  390. cfgCp := *cfg
  391. cfgCp.Credentials = credentials.NewStaticCredentialsFromCreds(
  392. sharedCfg.AssumeRoleSource.Creds,
  393. )
  394. if len(sharedCfg.AssumeRole.MFASerial) > 0 && sessOpts.AssumeRoleTokenProvider == nil {
  395. // AssumeRole Token provider is required if doing Assume Role
  396. // with MFA.
  397. return AssumeRoleTokenProviderNotSetError{}
  398. }
  399. cfg.Credentials = stscreds.NewCredentials(
  400. &Session{
  401. Config: &cfgCp,
  402. Handlers: handlers.Copy(),
  403. },
  404. sharedCfg.AssumeRole.RoleARN,
  405. func(opt *stscreds.AssumeRoleProvider) {
  406. opt.RoleSessionName = sharedCfg.AssumeRole.RoleSessionName
  407. // Assume role with external ID
  408. if len(sharedCfg.AssumeRole.ExternalID) > 0 {
  409. opt.ExternalID = aws.String(sharedCfg.AssumeRole.ExternalID)
  410. }
  411. // Assume role with MFA
  412. if len(sharedCfg.AssumeRole.MFASerial) > 0 {
  413. opt.SerialNumber = aws.String(sharedCfg.AssumeRole.MFASerial)
  414. opt.TokenProvider = sessOpts.AssumeRoleTokenProvider
  415. }
  416. },
  417. )
  418. } else if len(sharedCfg.Creds.AccessKeyID) > 0 {
  419. cfg.Credentials = credentials.NewStaticCredentialsFromCreds(
  420. sharedCfg.Creds,
  421. )
  422. } else {
  423. // Fallback to default credentials provider, include mock errors
  424. // for the credential chain so user can identify why credentials
  425. // failed to be retrieved.
  426. cfg.Credentials = credentials.NewCredentials(&credentials.ChainProvider{
  427. VerboseErrors: aws.BoolValue(cfg.CredentialsChainVerboseErrors),
  428. Providers: []credentials.Provider{
  429. &credProviderError{Err: awserr.New("EnvAccessKeyNotFound", "failed to find credentials in the environment.", nil)},
  430. &credProviderError{Err: awserr.New("SharedCredsLoad", fmt.Sprintf("failed to load profile, %s.", envCfg.Profile), nil)},
  431. defaults.RemoteCredProvider(*cfg, handlers),
  432. },
  433. })
  434. }
  435. }
  436. return nil
  437. }
  438. // AssumeRoleTokenProviderNotSetError is an error returned when creating a session when the
  439. // MFAToken option is not set when shared config is configured load assume a
  440. // role with an MFA token.
  441. type AssumeRoleTokenProviderNotSetError struct{}
  442. // Code is the short id of the error.
  443. func (e AssumeRoleTokenProviderNotSetError) Code() string {
  444. return "AssumeRoleTokenProviderNotSetError"
  445. }
  446. // Message is the description of the error
  447. func (e AssumeRoleTokenProviderNotSetError) Message() string {
  448. return fmt.Sprintf("assume role with MFA enabled, but AssumeRoleTokenProvider session option not set.")
  449. }
  450. // OrigErr is the underlying error that caused the failure.
  451. func (e AssumeRoleTokenProviderNotSetError) OrigErr() error {
  452. return nil
  453. }
  454. // Error satisfies the error interface.
  455. func (e AssumeRoleTokenProviderNotSetError) Error() string {
  456. return awserr.SprintError(e.Code(), e.Message(), "", nil)
  457. }
  458. type credProviderError struct {
  459. Err error
  460. }
  461. var emptyCreds = credentials.Value{}
  462. func (c credProviderError) Retrieve() (credentials.Value, error) {
  463. return credentials.Value{}, c.Err
  464. }
  465. func (c credProviderError) IsExpired() bool {
  466. return true
  467. }
  468. func initHandlers(s *Session) {
  469. // Add the Validate parameter handler if it is not disabled.
  470. s.Handlers.Validate.Remove(corehandlers.ValidateParametersHandler)
  471. if !aws.BoolValue(s.Config.DisableParamValidation) {
  472. s.Handlers.Validate.PushBackNamed(corehandlers.ValidateParametersHandler)
  473. }
  474. }
  475. // Copy creates and returns a copy of the current Session, coping the config
  476. // and handlers. If any additional configs are provided they will be merged
  477. // on top of the Session's copied config.
  478. //
  479. // // Create a copy of the current Session, configured for the us-west-2 region.
  480. // sess.Copy(&aws.Config{Region: aws.String("us-west-2")})
  481. func (s *Session) Copy(cfgs ...*aws.Config) *Session {
  482. newSession := &Session{
  483. Config: s.Config.Copy(cfgs...),
  484. Handlers: s.Handlers.Copy(),
  485. }
  486. initHandlers(newSession)
  487. return newSession
  488. }
  489. // ClientConfig satisfies the client.ConfigProvider interface and is used to
  490. // configure the service client instances. Passing the Session to the service
  491. // client's constructor (New) will use this method to configure the client.
  492. func (s *Session) ClientConfig(serviceName string, cfgs ...*aws.Config) client.Config {
  493. // Backwards compatibility, the error will be eaten if user calls ClientConfig
  494. // directly. All SDK services will use ClientconfigWithError.
  495. cfg, _ := s.clientConfigWithErr(serviceName, cfgs...)
  496. return cfg
  497. }
  498. func (s *Session) clientConfigWithErr(serviceName string, cfgs ...*aws.Config) (client.Config, error) {
  499. s = s.Copy(cfgs...)
  500. var resolved endpoints.ResolvedEndpoint
  501. var err error
  502. region := aws.StringValue(s.Config.Region)
  503. if endpoint := aws.StringValue(s.Config.Endpoint); len(endpoint) != 0 {
  504. resolved.URL = endpoints.AddScheme(endpoint, aws.BoolValue(s.Config.DisableSSL))
  505. resolved.SigningRegion = region
  506. } else {
  507. resolved, err = s.Config.EndpointResolver.EndpointFor(
  508. serviceName, region,
  509. func(opt *endpoints.Options) {
  510. opt.DisableSSL = aws.BoolValue(s.Config.DisableSSL)
  511. opt.UseDualStack = aws.BoolValue(s.Config.UseDualStack)
  512. // Support the condition where the service is modeled but its
  513. // endpoint metadata is not available.
  514. opt.ResolveUnknownService = true
  515. },
  516. )
  517. }
  518. return client.Config{
  519. Config: s.Config,
  520. Handlers: s.Handlers,
  521. Endpoint: resolved.URL,
  522. SigningRegion: resolved.SigningRegion,
  523. SigningNameDerived: resolved.SigningNameDerived,
  524. SigningName: resolved.SigningName,
  525. }, err
  526. }
  527. // ClientConfigNoResolveEndpoint is the same as ClientConfig with the exception
  528. // that the EndpointResolver will not be used to resolve the endpoint. The only
  529. // endpoint set must come from the aws.Config.Endpoint field.
  530. func (s *Session) ClientConfigNoResolveEndpoint(cfgs ...*aws.Config) client.Config {
  531. s = s.Copy(cfgs...)
  532. var resolved endpoints.ResolvedEndpoint
  533. region := aws.StringValue(s.Config.Region)
  534. if ep := aws.StringValue(s.Config.Endpoint); len(ep) > 0 {
  535. resolved.URL = endpoints.AddScheme(ep, aws.BoolValue(s.Config.DisableSSL))
  536. resolved.SigningRegion = region
  537. }
  538. return client.Config{
  539. Config: s.Config,
  540. Handlers: s.Handlers,
  541. Endpoint: resolved.URL,
  542. SigningRegion: resolved.SigningRegion,
  543. SigningNameDerived: resolved.SigningNameDerived,
  544. SigningName: resolved.SigningName,
  545. }
  546. }