Skip to content

Commit 70e28e0

Browse files
9rnthm-aircall
andauthored
Allow provider to use the external ID when assuming the role in the p… (#357)
* Allow provider to use the external ID when assuming the role in the provider configuration set up * Update the provider documentation * copy pasta no good --------- Co-authored-by: mannai2 <[email protected]>
1 parent c4ba861 commit 70e28e0

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

docs/index.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ The following arguments are supported:
6363
* `username` (Optional) - Username to use to connect to elasticsearch using basic auth. Defaults to `ELASTICSEARCH_USERNAME` from the environment
6464
* `password` (Optional) - Password to use to connect to elasticsearch using basic auth. Defaults to `ELASTICSEARCH_PASSWORD` from the environment
6565
* `aws_assume_role_arn` (Optional) - ARN of role to assume when using AWS Elasticsearch Service domains.
66+
* `aws_assume_role_external_id` (Optional) - External ID configured in the IAM policy of the IAM Role to assume prior to using AWS Elasticsearch Service domains.
6667
* `aws_assume_role_session_name` - AWS IAM session name to use when assuming a role.
6768
* `aws_access_key` (Optional) - The access key for use with AWS Elasticsearch Service domains. It can also be sourced from the `AWS_ACCESS_KEY_ID` environment variable.
6869
* `aws_secret_key` (Optional) - The secret key for use with AWS Elasticsearch Service domains. It can also be sourced from the `AWS_SECRET_ACCESS_KEY` environment variable.
@@ -109,13 +110,15 @@ provider "elasticsearch" {
109110
#### Assume role configuration
110111

111112
You can instruct the provider to assume a role in AWS before interacting with the cluster by setting the `aws_assume_role_arn` variable.
113+
Optionnaly, you can configure the [External ID](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_create_for-user_externalid.html) of IAM role trust policy by setting the `aws_assume_role_external_id` variable.
112114

113115
Example usage:
114116

115117
```tf
116118
provider "elasticsearch" {
117-
url = "https://search-foo-bar-pqrhr4w3u4dzervg41frow4mmy.us-east-1.es.amazonaws.com"
118-
aws_assume_role_arn = "arn:aws:iam::012345678901:role/rolename"
119+
url = "https://search-foo-bar-pqrhr4w3u4dzervg41frow4mmy.us-east-1.es.amazonaws.com"
120+
aws_assume_role_arn = "arn:aws:iam::012345678901:role/rolename"
121+
aws_assume_role_external_id = "Unique ID"
119122
}
120123
```
121124

es/provider.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ type ProviderConf struct {
5757
pingTimeoutSeconds int
5858
awsRegion string
5959
awsAssumeRoleArn string
60+
awsAssumeRoleExternalID string
6061
awsAssumeRoleSessionName string
6162
awsAccessKeyId string
6263
awsSecretAccessKey string
@@ -128,6 +129,12 @@ func Provider() *schema.Provider {
128129
Default: "",
129130
Description: "Amazon Resource Name of an IAM Role to assume prior to making AWS API calls.",
130131
},
132+
"aws_assume_role_external_id": {
133+
Type: schema.TypeString,
134+
Optional: true,
135+
Default: "",
136+
Description: "External ID configured in the IAM policy of the IAM Role to assume prior to making AWS API calls.",
137+
},
131138
"aws_assume_role_session_name": {
132139
Type: schema.TypeString,
133140
Optional: true,
@@ -299,6 +306,7 @@ func providerConfigure(c context.Context, d *schema.ResourceData) (interface{},
299306
awsRegion: d.Get("aws_region").(string),
300307

301308
awsAssumeRoleArn: d.Get("aws_assume_role_arn").(string),
309+
awsAssumeRoleExternalID: d.Get("aws_assume_role_external_id").(string),
302310
awsAssumeRoleSessionName: d.Get("aws_assume_role_session_name").(string),
303311
awsAccessKeyId: d.Get("aws_access_key").(string),
304312
awsSecretAccessKey: d.Get("aws_secret_key").(string),
@@ -553,7 +561,7 @@ func getKibanaClient(conf *ProviderConf) (interface{}, error) {
553561
}
554562
}
555563

556-
func assumeRoleCredentials(region, roleARN, roleSessionName, profile string) *awscredentials.Credentials {
564+
func assumeRoleCredentials(region, roleARN, roleExternalID, roleSessionName, profile string) *awscredentials.Credentials {
557565
sessOpts := awsSessionOptions(region)
558566
sessOpts.Profile = profile
559567

@@ -563,6 +571,7 @@ func assumeRoleCredentials(region, roleARN, roleSessionName, profile string) *aw
563571
Client: stsClient,
564572
RoleARN: roleARN,
565573
RoleSessionName: roleSessionName,
574+
ExternalID: aws.String(roleExternalID),
566575
}
567576

568577
return awscredentials.NewChainCredentials([]awscredentials.Provider{assumeRoleProvider})
@@ -593,14 +602,18 @@ func awsSession(region string, conf *ProviderConf) *awssession.Session {
593602

594603
// 1. access keys take priority
595604
// 2. next is an assume role configuration
605+
// 2.b check if the role external ID is set and use it
596606
// 3. followed by a profile (for assume role)
597607
// 4. let the default credentials provider figure out the rest (env, ec2, etc..)
598608
//
599609
// note: if #1 is chosen, then no further providers will be tested, since we've overridden the credentials with just a static provider
600610
if conf.awsAccessKeyId != "" {
601611
sessOpts.Config.Credentials = awscredentials.NewStaticCredentials(conf.awsAccessKeyId, conf.awsSecretAccessKey, conf.awsSessionToken)
602612
} else if conf.awsAssumeRoleArn != "" {
603-
sessOpts.Config.Credentials = assumeRoleCredentials(region, conf.awsAssumeRoleArn, conf.awsAssumeRoleSessionName, conf.awsProfile)
613+
if conf.awsAssumeRoleExternalID == "" {
614+
conf.awsAssumeRoleExternalID = ""
615+
}
616+
sessOpts.Config.Credentials = assumeRoleCredentials(region, conf.awsAssumeRoleArn, conf.awsAssumeRoleExternalID, conf.awsAssumeRoleSessionName, conf.awsProfile)
604617
} else if conf.awsProfile != "" {
605618
sessOpts.Profile = conf.awsProfile
606619
}

es/provider_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,13 +216,15 @@ func TestAWSCredsAssumeRole(t *testing.T) {
216216
testRegion := "us-east-1"
217217

218218
testConfig := map[string]interface{}{
219-
"aws_assume_role_arn": "test_arn",
219+
"aws_assume_role_arn": "test_arn",
220+
"aws_assume_role_external_id": "secret_id",
220221
}
221222

222223
testConfigData := schema.TestResourceDataRaw(t, Provider().Schema, testConfig)
223224

224225
conf := &ProviderConf{
225-
awsAssumeRoleArn: testConfigData.Get("aws_assume_role_arn").(string),
226+
awsAssumeRoleArn: testConfigData.Get("aws_assume_role_arn").(string),
227+
awsAssumeRoleExternalID: testConfigData.Get("aws_assume_role_external_id").(string),
226228
}
227229
s := awsSession(testRegion, conf)
228230
if s == nil {

0 commit comments

Comments
 (0)