provider.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  1. package config
  2. import (
  3. "context"
  4. "io"
  5. "net/http"
  6. "github.com/aws/aws-sdk-go-v2/aws"
  7. "github.com/aws/aws-sdk-go-v2/credentials/ec2rolecreds"
  8. "github.com/aws/aws-sdk-go-v2/credentials/endpointcreds"
  9. "github.com/aws/aws-sdk-go-v2/credentials/processcreds"
  10. "github.com/aws/aws-sdk-go-v2/credentials/ssocreds"
  11. "github.com/aws/aws-sdk-go-v2/credentials/stscreds"
  12. "github.com/aws/aws-sdk-go-v2/feature/ec2/imds"
  13. smithybearer "github.com/aws/smithy-go/auth/bearer"
  14. "github.com/aws/smithy-go/logging"
  15. "github.com/aws/smithy-go/middleware"
  16. )
  17. // sharedConfigProfileProvider provides access to the shared config profile
  18. // name external configuration value.
  19. type sharedConfigProfileProvider interface {
  20. getSharedConfigProfile(ctx context.Context) (string, bool, error)
  21. }
  22. // getSharedConfigProfile searches the configs for a sharedConfigProfileProvider
  23. // and returns the value if found. Returns an error if a provider fails before a
  24. // value is found.
  25. func getSharedConfigProfile(ctx context.Context, configs configs) (value string, found bool, err error) {
  26. for _, cfg := range configs {
  27. if p, ok := cfg.(sharedConfigProfileProvider); ok {
  28. value, found, err = p.getSharedConfigProfile(ctx)
  29. if err != nil || found {
  30. break
  31. }
  32. }
  33. }
  34. return
  35. }
  36. // sharedConfigFilesProvider provides access to the shared config filesnames
  37. // external configuration value.
  38. type sharedConfigFilesProvider interface {
  39. getSharedConfigFiles(ctx context.Context) ([]string, bool, error)
  40. }
  41. // getSharedConfigFiles searches the configs for a sharedConfigFilesProvider
  42. // and returns the value if found. Returns an error if a provider fails before a
  43. // value is found.
  44. func getSharedConfigFiles(ctx context.Context, configs configs) (value []string, found bool, err error) {
  45. for _, cfg := range configs {
  46. if p, ok := cfg.(sharedConfigFilesProvider); ok {
  47. value, found, err = p.getSharedConfigFiles(ctx)
  48. if err != nil || found {
  49. break
  50. }
  51. }
  52. }
  53. return
  54. }
  55. // sharedCredentialsFilesProvider provides access to the shared credentials filesnames
  56. // external configuration value.
  57. type sharedCredentialsFilesProvider interface {
  58. getSharedCredentialsFiles(ctx context.Context) ([]string, bool, error)
  59. }
  60. // getSharedCredentialsFiles searches the configs for a sharedCredentialsFilesProvider
  61. // and returns the value if found. Returns an error if a provider fails before a
  62. // value is found.
  63. func getSharedCredentialsFiles(ctx context.Context, configs configs) (value []string, found bool, err error) {
  64. for _, cfg := range configs {
  65. if p, ok := cfg.(sharedCredentialsFilesProvider); ok {
  66. value, found, err = p.getSharedCredentialsFiles(ctx)
  67. if err != nil || found {
  68. break
  69. }
  70. }
  71. }
  72. return
  73. }
  74. // customCABundleProvider provides access to the custom CA bundle PEM bytes.
  75. type customCABundleProvider interface {
  76. getCustomCABundle(ctx context.Context) (io.Reader, bool, error)
  77. }
  78. // getCustomCABundle searches the configs for a customCABundleProvider
  79. // and returns the value if found. Returns an error if a provider fails before a
  80. // value is found.
  81. func getCustomCABundle(ctx context.Context, configs configs) (value io.Reader, found bool, err error) {
  82. for _, cfg := range configs {
  83. if p, ok := cfg.(customCABundleProvider); ok {
  84. value, found, err = p.getCustomCABundle(ctx)
  85. if err != nil || found {
  86. break
  87. }
  88. }
  89. }
  90. return
  91. }
  92. // regionProvider provides access to the region external configuration value.
  93. type regionProvider interface {
  94. getRegion(ctx context.Context) (string, bool, error)
  95. }
  96. // getRegion searches the configs for a regionProvider and returns the value
  97. // if found. Returns an error if a provider fails before a value is found.
  98. func getRegion(ctx context.Context, configs configs) (value string, found bool, err error) {
  99. for _, cfg := range configs {
  100. if p, ok := cfg.(regionProvider); ok {
  101. value, found, err = p.getRegion(ctx)
  102. if err != nil || found {
  103. break
  104. }
  105. }
  106. }
  107. return
  108. }
  109. // ec2IMDSRegionProvider provides access to the ec2 imds region
  110. // configuration value
  111. type ec2IMDSRegionProvider interface {
  112. getEC2IMDSRegion(ctx context.Context) (string, bool, error)
  113. }
  114. // getEC2IMDSRegion searches the configs for a ec2IMDSRegionProvider and
  115. // returns the value if found. Returns an error if a provider fails before
  116. // a value is found.
  117. func getEC2IMDSRegion(ctx context.Context, configs configs) (region string, found bool, err error) {
  118. for _, cfg := range configs {
  119. if provider, ok := cfg.(ec2IMDSRegionProvider); ok {
  120. region, found, err = provider.getEC2IMDSRegion(ctx)
  121. if err != nil || found {
  122. break
  123. }
  124. }
  125. }
  126. return
  127. }
  128. // credentialsProviderProvider provides access to the credentials external
  129. // configuration value.
  130. type credentialsProviderProvider interface {
  131. getCredentialsProvider(ctx context.Context) (aws.CredentialsProvider, bool, error)
  132. }
  133. // getCredentialsProvider searches the configs for a credentialsProviderProvider
  134. // and returns the value if found. Returns an error if a provider fails before a
  135. // value is found.
  136. func getCredentialsProvider(ctx context.Context, configs configs) (p aws.CredentialsProvider, found bool, err error) {
  137. for _, cfg := range configs {
  138. if provider, ok := cfg.(credentialsProviderProvider); ok {
  139. p, found, err = provider.getCredentialsProvider(ctx)
  140. if err != nil || found {
  141. break
  142. }
  143. }
  144. }
  145. return
  146. }
  147. // credentialsCacheOptionsProvider is an interface for retrieving a function for setting
  148. // the aws.CredentialsCacheOptions.
  149. type credentialsCacheOptionsProvider interface {
  150. getCredentialsCacheOptions(ctx context.Context) (func(*aws.CredentialsCacheOptions), bool, error)
  151. }
  152. // getCredentialsCacheOptionsProvider is an interface for retrieving a function for setting
  153. // the aws.CredentialsCacheOptions.
  154. func getCredentialsCacheOptionsProvider(ctx context.Context, configs configs) (
  155. f func(*aws.CredentialsCacheOptions), found bool, err error,
  156. ) {
  157. for _, config := range configs {
  158. if p, ok := config.(credentialsCacheOptionsProvider); ok {
  159. f, found, err = p.getCredentialsCacheOptions(ctx)
  160. if err != nil || found {
  161. break
  162. }
  163. }
  164. }
  165. return
  166. }
  167. // bearerAuthTokenProviderProvider provides access to the bearer authentication
  168. // token external configuration value.
  169. type bearerAuthTokenProviderProvider interface {
  170. getBearerAuthTokenProvider(context.Context) (smithybearer.TokenProvider, bool, error)
  171. }
  172. // getBearerAuthTokenProvider searches the config sources for a
  173. // bearerAuthTokenProviderProvider and returns the value if found. Returns an
  174. // error if a provider fails before a value is found.
  175. func getBearerAuthTokenProvider(ctx context.Context, configs configs) (p smithybearer.TokenProvider, found bool, err error) {
  176. for _, cfg := range configs {
  177. if provider, ok := cfg.(bearerAuthTokenProviderProvider); ok {
  178. p, found, err = provider.getBearerAuthTokenProvider(ctx)
  179. if err != nil || found {
  180. break
  181. }
  182. }
  183. }
  184. return
  185. }
  186. // bearerAuthTokenCacheOptionsProvider is an interface for retrieving a function for
  187. // setting the smithy-go auth/bearer#TokenCacheOptions.
  188. type bearerAuthTokenCacheOptionsProvider interface {
  189. getBearerAuthTokenCacheOptions(context.Context) (func(*smithybearer.TokenCacheOptions), bool, error)
  190. }
  191. // getBearerAuthTokenCacheOptionsProvider is an interface for retrieving a function for
  192. // setting the smithy-go auth/bearer#TokenCacheOptions.
  193. func getBearerAuthTokenCacheOptions(ctx context.Context, configs configs) (
  194. f func(*smithybearer.TokenCacheOptions), found bool, err error,
  195. ) {
  196. for _, config := range configs {
  197. if p, ok := config.(bearerAuthTokenCacheOptionsProvider); ok {
  198. f, found, err = p.getBearerAuthTokenCacheOptions(ctx)
  199. if err != nil || found {
  200. break
  201. }
  202. }
  203. }
  204. return
  205. }
  206. // ssoTokenProviderOptionsProvider is an interface for retrieving a function for
  207. // setting the SDK's credentials/ssocreds#SSOTokenProviderOptions.
  208. type ssoTokenProviderOptionsProvider interface {
  209. getSSOTokenProviderOptions(context.Context) (func(*ssocreds.SSOTokenProviderOptions), bool, error)
  210. }
  211. // getSSOTokenProviderOptions is an interface for retrieving a function for
  212. // setting the SDK's credentials/ssocreds#SSOTokenProviderOptions.
  213. func getSSOTokenProviderOptions(ctx context.Context, configs configs) (
  214. f func(*ssocreds.SSOTokenProviderOptions), found bool, err error,
  215. ) {
  216. for _, config := range configs {
  217. if p, ok := config.(ssoTokenProviderOptionsProvider); ok {
  218. f, found, err = p.getSSOTokenProviderOptions(ctx)
  219. if err != nil || found {
  220. break
  221. }
  222. }
  223. }
  224. return
  225. }
  226. // ssoTokenProviderOptionsProvider
  227. // processCredentialOptions is an interface for retrieving a function for setting
  228. // the processcreds.Options.
  229. type processCredentialOptions interface {
  230. getProcessCredentialOptions(ctx context.Context) (func(*processcreds.Options), bool, error)
  231. }
  232. // getProcessCredentialOptions searches the slice of configs and returns the first function found
  233. func getProcessCredentialOptions(ctx context.Context, configs configs) (f func(*processcreds.Options), found bool, err error) {
  234. for _, config := range configs {
  235. if p, ok := config.(processCredentialOptions); ok {
  236. f, found, err = p.getProcessCredentialOptions(ctx)
  237. if err != nil || found {
  238. break
  239. }
  240. }
  241. }
  242. return
  243. }
  244. // ec2RoleCredentialOptionsProvider is an interface for retrieving a function
  245. // for setting the ec2rolecreds.Provider options.
  246. type ec2RoleCredentialOptionsProvider interface {
  247. getEC2RoleCredentialOptions(ctx context.Context) (func(*ec2rolecreds.Options), bool, error)
  248. }
  249. // getEC2RoleCredentialProviderOptions searches the slice of configs and returns the first function found
  250. func getEC2RoleCredentialProviderOptions(ctx context.Context, configs configs) (f func(*ec2rolecreds.Options), found bool, err error) {
  251. for _, config := range configs {
  252. if p, ok := config.(ec2RoleCredentialOptionsProvider); ok {
  253. f, found, err = p.getEC2RoleCredentialOptions(ctx)
  254. if err != nil || found {
  255. break
  256. }
  257. }
  258. }
  259. return
  260. }
  261. // defaultRegionProvider is an interface for retrieving a default region if a region was not resolved from other sources
  262. type defaultRegionProvider interface {
  263. getDefaultRegion(ctx context.Context) (string, bool, error)
  264. }
  265. // getDefaultRegion searches the slice of configs and returns the first fallback region found
  266. func getDefaultRegion(ctx context.Context, configs configs) (value string, found bool, err error) {
  267. for _, config := range configs {
  268. if p, ok := config.(defaultRegionProvider); ok {
  269. value, found, err = p.getDefaultRegion(ctx)
  270. if err != nil || found {
  271. break
  272. }
  273. }
  274. }
  275. return
  276. }
  277. // endpointCredentialOptionsProvider is an interface for retrieving a function for setting
  278. // the endpointcreds.ProviderOptions.
  279. type endpointCredentialOptionsProvider interface {
  280. getEndpointCredentialOptions(ctx context.Context) (func(*endpointcreds.Options), bool, error)
  281. }
  282. // getEndpointCredentialProviderOptions searches the slice of configs and returns the first function found
  283. func getEndpointCredentialProviderOptions(ctx context.Context, configs configs) (f func(*endpointcreds.Options), found bool, err error) {
  284. for _, config := range configs {
  285. if p, ok := config.(endpointCredentialOptionsProvider); ok {
  286. f, found, err = p.getEndpointCredentialOptions(ctx)
  287. if err != nil || found {
  288. break
  289. }
  290. }
  291. }
  292. return
  293. }
  294. // webIdentityRoleCredentialOptionsProvider is an interface for retrieving a function for setting
  295. // the stscreds.WebIdentityRoleProvider.
  296. type webIdentityRoleCredentialOptionsProvider interface {
  297. getWebIdentityRoleCredentialOptions(ctx context.Context) (func(*stscreds.WebIdentityRoleOptions), bool, error)
  298. }
  299. // getWebIdentityCredentialProviderOptions searches the slice of configs and returns the first function found
  300. func getWebIdentityCredentialProviderOptions(ctx context.Context, configs configs) (f func(*stscreds.WebIdentityRoleOptions), found bool, err error) {
  301. for _, config := range configs {
  302. if p, ok := config.(webIdentityRoleCredentialOptionsProvider); ok {
  303. f, found, err = p.getWebIdentityRoleCredentialOptions(ctx)
  304. if err != nil || found {
  305. break
  306. }
  307. }
  308. }
  309. return
  310. }
  311. // assumeRoleCredentialOptionsProvider is an interface for retrieving a function for setting
  312. // the stscreds.AssumeRoleOptions.
  313. type assumeRoleCredentialOptionsProvider interface {
  314. getAssumeRoleCredentialOptions(ctx context.Context) (func(*stscreds.AssumeRoleOptions), bool, error)
  315. }
  316. // getAssumeRoleCredentialProviderOptions searches the slice of configs and returns the first function found
  317. func getAssumeRoleCredentialProviderOptions(ctx context.Context, configs configs) (f func(*stscreds.AssumeRoleOptions), found bool, err error) {
  318. for _, config := range configs {
  319. if p, ok := config.(assumeRoleCredentialOptionsProvider); ok {
  320. f, found, err = p.getAssumeRoleCredentialOptions(ctx)
  321. if err != nil || found {
  322. break
  323. }
  324. }
  325. }
  326. return
  327. }
  328. // HTTPClient is an HTTP client implementation
  329. type HTTPClient interface {
  330. Do(*http.Request) (*http.Response, error)
  331. }
  332. // httpClientProvider is an interface for retrieving HTTPClient
  333. type httpClientProvider interface {
  334. getHTTPClient(ctx context.Context) (HTTPClient, bool, error)
  335. }
  336. // getHTTPClient searches the slice of configs and returns the HTTPClient set on configs
  337. func getHTTPClient(ctx context.Context, configs configs) (client HTTPClient, found bool, err error) {
  338. for _, config := range configs {
  339. if p, ok := config.(httpClientProvider); ok {
  340. client, found, err = p.getHTTPClient(ctx)
  341. if err != nil || found {
  342. break
  343. }
  344. }
  345. }
  346. return
  347. }
  348. // apiOptionsProvider is an interface for retrieving APIOptions
  349. type apiOptionsProvider interface {
  350. getAPIOptions(ctx context.Context) ([]func(*middleware.Stack) error, bool, error)
  351. }
  352. // getAPIOptions searches the slice of configs and returns the APIOptions set on configs
  353. func getAPIOptions(ctx context.Context, configs configs) (apiOptions []func(*middleware.Stack) error, found bool, err error) {
  354. for _, config := range configs {
  355. if p, ok := config.(apiOptionsProvider); ok {
  356. // retrieve APIOptions from configs and set it on cfg
  357. apiOptions, found, err = p.getAPIOptions(ctx)
  358. if err != nil || found {
  359. break
  360. }
  361. }
  362. }
  363. return
  364. }
  365. // endpointResolverProvider is an interface for retrieving an aws.EndpointResolver from a configuration source
  366. type endpointResolverProvider interface {
  367. getEndpointResolver(ctx context.Context) (aws.EndpointResolver, bool, error)
  368. }
  369. // getEndpointResolver searches the provided config sources for a EndpointResolverFunc that can be used
  370. // to configure the aws.Config.EndpointResolver value.
  371. func getEndpointResolver(ctx context.Context, configs configs) (f aws.EndpointResolver, found bool, err error) {
  372. for _, c := range configs {
  373. if p, ok := c.(endpointResolverProvider); ok {
  374. f, found, err = p.getEndpointResolver(ctx)
  375. if err != nil || found {
  376. break
  377. }
  378. }
  379. }
  380. return
  381. }
  382. // endpointResolverWithOptionsProvider is an interface for retrieving an aws.EndpointResolverWithOptions from a configuration source
  383. type endpointResolverWithOptionsProvider interface {
  384. getEndpointResolverWithOptions(ctx context.Context) (aws.EndpointResolverWithOptions, bool, error)
  385. }
  386. // getEndpointResolver searches the provided config sources for a EndpointResolverFunc that can be used
  387. // to configure the aws.Config.EndpointResolver value.
  388. func getEndpointResolverWithOptions(ctx context.Context, configs configs) (f aws.EndpointResolverWithOptions, found bool, err error) {
  389. for _, c := range configs {
  390. if p, ok := c.(endpointResolverWithOptionsProvider); ok {
  391. f, found, err = p.getEndpointResolverWithOptions(ctx)
  392. if err != nil || found {
  393. break
  394. }
  395. }
  396. }
  397. return
  398. }
  399. // loggerProvider is an interface for retrieving a logging.Logger from a configuration source.
  400. type loggerProvider interface {
  401. getLogger(ctx context.Context) (logging.Logger, bool, error)
  402. }
  403. // getLogger searches the provided config sources for a logging.Logger that can be used
  404. // to configure the aws.Config.Logger value.
  405. func getLogger(ctx context.Context, configs configs) (l logging.Logger, found bool, err error) {
  406. for _, c := range configs {
  407. if p, ok := c.(loggerProvider); ok {
  408. l, found, err = p.getLogger(ctx)
  409. if err != nil || found {
  410. break
  411. }
  412. }
  413. }
  414. return
  415. }
  416. // clientLogModeProvider is an interface for retrieving the aws.ClientLogMode from a configuration source.
  417. type clientLogModeProvider interface {
  418. getClientLogMode(ctx context.Context) (aws.ClientLogMode, bool, error)
  419. }
  420. func getClientLogMode(ctx context.Context, configs configs) (m aws.ClientLogMode, found bool, err error) {
  421. for _, c := range configs {
  422. if p, ok := c.(clientLogModeProvider); ok {
  423. m, found, err = p.getClientLogMode(ctx)
  424. if err != nil || found {
  425. break
  426. }
  427. }
  428. }
  429. return
  430. }
  431. // retryProvider is an configuration provider for custom Retryer.
  432. type retryProvider interface {
  433. getRetryer(ctx context.Context) (func() aws.Retryer, bool, error)
  434. }
  435. func getRetryer(ctx context.Context, configs configs) (v func() aws.Retryer, found bool, err error) {
  436. for _, c := range configs {
  437. if p, ok := c.(retryProvider); ok {
  438. v, found, err = p.getRetryer(ctx)
  439. if err != nil || found {
  440. break
  441. }
  442. }
  443. }
  444. return
  445. }
  446. // logConfigurationWarningsProvider is an configuration provider for
  447. // retrieving a boolean indicating whether configuration issues should
  448. // be logged when loading from config sources
  449. type logConfigurationWarningsProvider interface {
  450. getLogConfigurationWarnings(ctx context.Context) (bool, bool, error)
  451. }
  452. func getLogConfigurationWarnings(ctx context.Context, configs configs) (v bool, found bool, err error) {
  453. for _, c := range configs {
  454. if p, ok := c.(logConfigurationWarningsProvider); ok {
  455. v, found, err = p.getLogConfigurationWarnings(ctx)
  456. if err != nil || found {
  457. break
  458. }
  459. }
  460. }
  461. return
  462. }
  463. // ssoCredentialOptionsProvider is an interface for retrieving a function for setting
  464. // the ssocreds.Options.
  465. type ssoCredentialOptionsProvider interface {
  466. getSSOProviderOptions(context.Context) (func(*ssocreds.Options), bool, error)
  467. }
  468. func getSSOProviderOptions(ctx context.Context, configs configs) (v func(options *ssocreds.Options), found bool, err error) {
  469. for _, c := range configs {
  470. if p, ok := c.(ssoCredentialOptionsProvider); ok {
  471. v, found, err = p.getSSOProviderOptions(ctx)
  472. if err != nil || found {
  473. break
  474. }
  475. }
  476. }
  477. return v, found, err
  478. }
  479. type defaultsModeIMDSClientProvider interface {
  480. getDefaultsModeIMDSClient(context.Context) (*imds.Client, bool, error)
  481. }
  482. func getDefaultsModeIMDSClient(ctx context.Context, configs configs) (v *imds.Client, found bool, err error) {
  483. for _, c := range configs {
  484. if p, ok := c.(defaultsModeIMDSClientProvider); ok {
  485. v, found, err = p.getDefaultsModeIMDSClient(ctx)
  486. if err != nil || found {
  487. break
  488. }
  489. }
  490. }
  491. return v, found, err
  492. }
  493. type defaultsModeProvider interface {
  494. getDefaultsMode(context.Context) (aws.DefaultsMode, bool, error)
  495. }
  496. func getDefaultsMode(ctx context.Context, configs configs) (v aws.DefaultsMode, found bool, err error) {
  497. for _, c := range configs {
  498. if p, ok := c.(defaultsModeProvider); ok {
  499. v, found, err = p.getDefaultsMode(ctx)
  500. if err != nil || found {
  501. break
  502. }
  503. }
  504. }
  505. return v, found, err
  506. }
  507. type retryMaxAttemptsProvider interface {
  508. GetRetryMaxAttempts(context.Context) (int, bool, error)
  509. }
  510. func getRetryMaxAttempts(ctx context.Context, configs configs) (v int, found bool, err error) {
  511. for _, c := range configs {
  512. if p, ok := c.(retryMaxAttemptsProvider); ok {
  513. v, found, err = p.GetRetryMaxAttempts(ctx)
  514. if err != nil || found {
  515. break
  516. }
  517. }
  518. }
  519. return v, found, err
  520. }
  521. type retryModeProvider interface {
  522. GetRetryMode(context.Context) (aws.RetryMode, bool, error)
  523. }
  524. func getRetryMode(ctx context.Context, configs configs) (v aws.RetryMode, found bool, err error) {
  525. for _, c := range configs {
  526. if p, ok := c.(retryModeProvider); ok {
  527. v, found, err = p.GetRetryMode(ctx)
  528. if err != nil || found {
  529. break
  530. }
  531. }
  532. }
  533. return v, found, err
  534. }