Skip to content

Commit 64f21df

Browse files
committed
[aws] Reuse session options, ensure synchronization.
Fixes additional issue raised in #124.
1 parent 22a9d6e commit 64f21df

File tree

2 files changed

+24
-15
lines changed

2 files changed

+24
-15
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
## Unreleased
33
### Changed
44
- [provider] Change default for healthcheck and sniffing to false, see https://github.com/phillbaker/terraform-provider-elasticsearch/pull/161.
5+
- [aws] Reuse session options, ensure synchronization before using credentials, see https://github.com/phillbaker/terraform-provider-elasticsearch/issues/124.
56

67
### Added
78
- [index] Add include_type_name for compatibility between ESv6/7

es/provider.go

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -412,8 +412,21 @@ func getKibanaClient(conf *ProviderConf) (interface{}, error) {
412412
}
413413

414414
func assumeRoleCredentials(region, roleARN, profile string) *awscredentials.Credentials {
415-
sess := awssession.Must(awssession.NewSessionWithOptions(awssession.Options{
416-
Profile: profile,
415+
sessOpts := awsSessionOptions(region)
416+
sessOpts.Profile = profile
417+
418+
sess := awssession.Must(awssession.NewSessionWithOptions(sessOpts))
419+
stsClient := awssts.New(sess)
420+
assumeRoleProvider := &awsstscreds.AssumeRoleProvider{
421+
Client: stsClient,
422+
RoleARN: roleARN,
423+
}
424+
425+
return awscredentials.NewChainCredentials([]awscredentials.Provider{assumeRoleProvider})
426+
}
427+
428+
func awsSessionOptions(region string) awssession.Options {
429+
return awssession.Options{
417430
Config: aws.Config{
418431
Region: aws.String(region),
419432
LogLevel: aws.LogLevel(aws.LogDebugWithHTTPBody),
@@ -429,22 +442,12 @@ func assumeRoleCredentials(region, roleARN, profile string) *awscredentials.Cred
429442
HTTPClient: &http.Client{Timeout: 10 * time.Second},
430443
},
431444
SharedConfigState: awssession.SharedConfigEnable,
432-
}))
433-
stsClient := awssts.New(sess)
434-
assumeRoleProvider := &awsstscreds.AssumeRoleProvider{
435-
Client: stsClient,
436-
RoleARN: roleARN,
437445
}
438-
439-
return awscredentials.NewChainCredentials([]awscredentials.Provider{assumeRoleProvider})
440446
}
441447

442448
func awsSession(region string, conf *ProviderConf) *awssession.Session {
443-
sessOpts := awssession.Options{
444-
Config: aws.Config{
445-
Region: aws.String(region),
446-
},
447-
}
449+
sessOpts := awsSessionOptions(region)
450+
448451
// 1. access keys take priority
449452
// 2. next is an assume role configuration
450453
// 3. followed by a profile (for assume role)
@@ -457,7 +460,6 @@ func awsSession(region string, conf *ProviderConf) *awssession.Session {
457460
sessOpts.Config.Credentials = assumeRoleCredentials(region, conf.awsAssumeRoleArn, conf.awsProfile)
458461
} else if conf.awsProfile != "" {
459462
sessOpts.Profile = conf.awsProfile
460-
sessOpts.SharedConfigState = awssession.SharedConfigEnable
461463
}
462464

463465
// If configured as insecure, turn off SSL verification
@@ -479,6 +481,12 @@ func awsSession(region string, conf *ProviderConf) *awssession.Session {
479481

480482
func awsHttpClient(region string, conf *ProviderConf, headers map[string]string) *http.Client {
481483
session := awsSession(region, conf)
484+
// Call Get() to ensure concurrency safe retrieval of credentials. Since the
485+
// client is created in many go routines, this synchronizes it.
486+
_, err := session.Config.Credentials.Get()
487+
if err != nil {
488+
log.Fatal(err)
489+
}
482490
signer := awssigv4.NewSigner(session.Config.Credentials)
483491
client, err := aws_signing_client.New(signer, session.Config.HTTPClient, "es", region)
484492
if err != nil {

0 commit comments

Comments
 (0)