Skip to content

Commit 122a927

Browse files
committed
[provider] Add aws session role name
Closes #332
1 parent 1cc0cfa commit 122a927

File tree

2 files changed

+46
-36
lines changed

2 files changed

+46
-36
lines changed

docs/index.md

Lines changed: 1 addition & 0 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_session_name` - AWS IAM session name to use when assuming a role.
6667
* `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.
6768
* `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.
6869
* `aws_token` (Optional) - The session token for use with AWS Elasticsearch Service domains. It can also be sourced from the `AWS_SESSION_TOKEN` environment variable.

es/provider.go

Lines changed: 45 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -42,30 +42,31 @@ const (
4242
var awsUrlRegexp = regexp.MustCompile(`([a-z0-9-]+).es.amazonaws.com$`)
4343

4444
type ProviderConf struct {
45-
rawUrl string
46-
insecure bool
47-
sniffing bool
48-
healthchecking bool
49-
cacertFile string
50-
username string
51-
password string
52-
token string
53-
tokenName string
54-
parsedUrl *url.URL
55-
signAWSRequests bool
56-
esVersion string
57-
pingTimeoutSeconds int
58-
awsRegion string
59-
awsAssumeRoleArn string
60-
awsAccessKeyId string
61-
awsSecretAccessKey string
62-
awsSessionToken string
63-
awsSig4Service string
64-
awsProfile string
65-
certPemPath string
66-
keyPemPath string
67-
kibanaUrl string
68-
hostOverride string
45+
rawUrl string
46+
insecure bool
47+
sniffing bool
48+
healthchecking bool
49+
cacertFile string
50+
username string
51+
password string
52+
token string
53+
tokenName string
54+
parsedUrl *url.URL
55+
signAWSRequests bool
56+
esVersion string
57+
pingTimeoutSeconds int
58+
awsRegion string
59+
awsAssumeRoleArn string
60+
awsAssumeRoleSessionName string
61+
awsAccessKeyId string
62+
awsSecretAccessKey string
63+
awsSessionToken string
64+
awsSig4Service string
65+
awsProfile string
66+
certPemPath string
67+
keyPemPath string
68+
kibanaUrl string
69+
hostOverride string
6970
// determined after connecting to the server
7071
flavor ServerFlavor
7172
}
@@ -127,6 +128,12 @@ func Provider() *schema.Provider {
127128
Default: "",
128129
Description: "Amazon Resource Name of an IAM Role to assume prior to making AWS API calls.",
129130
},
131+
"aws_assume_role_session_name": {
132+
Type: schema.TypeString,
133+
Optional: true,
134+
Default: "",
135+
Description: "AWS IAM session name to use when assuming a role.",
136+
},
130137
"aws_access_key": {
131138
Type: schema.TypeString,
132139
Optional: true,
@@ -291,14 +298,15 @@ func providerConfigure(c context.Context, d *schema.ResourceData) (interface{},
291298
pingTimeoutSeconds: d.Get("version_ping_timeout").(int),
292299
awsRegion: d.Get("aws_region").(string),
293300

294-
awsAssumeRoleArn: d.Get("aws_assume_role_arn").(string),
295-
awsAccessKeyId: d.Get("aws_access_key").(string),
296-
awsSecretAccessKey: d.Get("aws_secret_key").(string),
297-
awsSessionToken: d.Get("aws_token").(string),
298-
awsProfile: d.Get("aws_profile").(string),
299-
certPemPath: d.Get("client_cert_path").(string),
300-
keyPemPath: d.Get("client_key_path").(string),
301-
hostOverride: d.Get("host_override").(string),
301+
awsAssumeRoleArn: d.Get("aws_assume_role_arn").(string),
302+
awsAssumeRoleSessionName: d.Get("aws_assume_role_session_name").(string),
303+
awsAccessKeyId: d.Get("aws_access_key").(string),
304+
awsSecretAccessKey: d.Get("aws_secret_key").(string),
305+
awsSessionToken: d.Get("aws_token").(string),
306+
awsProfile: d.Get("aws_profile").(string),
307+
certPemPath: d.Get("client_cert_path").(string),
308+
keyPemPath: d.Get("client_key_path").(string),
309+
hostOverride: d.Get("host_override").(string),
302310
}, nil
303311
}
304312

@@ -545,15 +553,16 @@ func getKibanaClient(conf *ProviderConf) (interface{}, error) {
545553
}
546554
}
547555

548-
func assumeRoleCredentials(region, roleARN, profile string) *awscredentials.Credentials {
556+
func assumeRoleCredentials(region, roleARN, roleSessionName, profile string) *awscredentials.Credentials {
549557
sessOpts := awsSessionOptions(region)
550558
sessOpts.Profile = profile
551559

552560
sess := awssession.Must(awssession.NewSessionWithOptions(sessOpts))
553561
stsClient := awssts.New(sess)
554562
assumeRoleProvider := &awsstscreds.AssumeRoleProvider{
555-
Client: stsClient,
556-
RoleARN: roleARN,
563+
Client: stsClient,
564+
RoleARN: roleARN,
565+
RoleSessionName: roleSessionName,
557566
}
558567

559568
return awscredentials.NewChainCredentials([]awscredentials.Provider{assumeRoleProvider})
@@ -591,7 +600,7 @@ func awsSession(region string, conf *ProviderConf) *awssession.Session {
591600
if conf.awsAccessKeyId != "" {
592601
sessOpts.Config.Credentials = awscredentials.NewStaticCredentials(conf.awsAccessKeyId, conf.awsSecretAccessKey, conf.awsSessionToken)
593602
} else if conf.awsAssumeRoleArn != "" {
594-
sessOpts.Config.Credentials = assumeRoleCredentials(region, conf.awsAssumeRoleArn, conf.awsProfile)
603+
sessOpts.Config.Credentials = assumeRoleCredentials(region, conf.awsAssumeRoleArn, conf.awsAssumeRoleSessionName, conf.awsProfile)
595604
} else if conf.awsProfile != "" {
596605
sessOpts.Profile = conf.awsProfile
597606
}

0 commit comments

Comments
 (0)