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