-
Notifications
You must be signed in to change notification settings - Fork 37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for AssumeRoleWithWebIdentity #178
Conversation
credentials.go
Outdated
@@ -153,6 +158,25 @@ Error: %w`, err) | |||
return provider, creds.Source, err | |||
} | |||
|
|||
func webIdentityCredentialsProvider(ctx context.Context, awsConfig aws.Config, c *Config) (aws.CredentialsProvider, error) { | |||
ar := c.AssumeRole |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I reused config.AssumeRole
'block' here, although not every option is valid for 'assumeRoleWithWebIdentity'.
@gdavison - let me know if it's ok, or if you prefer to have something like config.WebIdentityAssumeRole
In hashicorp/terraform-provider-aws#22907 (comment) you accepted combining both at the provider level, but I don't know how it should be done here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm reconsidering that statement 🙂 Since the two operations support different options, it would probably be better to explicitly choose one or the other, both here and in the provider.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, I changed the Config and created a separate struct for AssumeRoleWithWebIdentity
internal/config/config.go
Outdated
@@ -79,3 +80,7 @@ func (c Config) ResolveSharedCredentialsFiles() ([]string, error) { | |||
} | |||
return v, nil | |||
} | |||
|
|||
func (c Config) GetIdentityToken() ([]byte, error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I need it for https://pkg.go.dev/github.com/aws/aws-sdk-go-v2/credentials@v1.10.0/stscreds#NewWebIdentityRoleProvider / https://pkg.go.dev/github.com/aws/aws-sdk-go-v2/credentials@v1.10.0/stscreds#IdentityTokenRetriever
Let me know if you prefer to have 'implementation' of that interface separately from Config
type
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since it only references fields from c.AssumeRoleWithWebIdentity
, it would probably be good to move it there
credentials.go
Outdated
@@ -133,6 +133,11 @@ func getCredentialsProvider(ctx context.Context, c *Config) (aws.CredentialsProv | |||
return nil, "", fmt.Errorf("loading configuration: %w", err) | |||
} | |||
|
|||
if c.WebIdentityToken != "" && c.AssumeRole != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let me know if you'd like to additionally validate/error when AssumeRole is nil when WebIdentityToken is passed.
I also tested it against a real AWS account/provider/role setup with simple code like
|
Oh I hit up against this exact problem today. Was gonna make in gitlab CI project to deploy something across a dozen accounts in my org via aliased providers, found I can't share one identity token file via CLI and get each provider to assume a unique role. Guess I'm in the cutting edge of terraform a CD! Thanks so much for implementing this. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for creating this, @mwieczorek. It's looking good. I have a few comments so far.
credentials.go
Outdated
@@ -153,6 +158,25 @@ Error: %w`, err) | |||
return provider, creds.Source, err | |||
} | |||
|
|||
func webIdentityCredentialsProvider(ctx context.Context, awsConfig aws.Config, c *Config) (aws.CredentialsProvider, error) { | |||
ar := c.AssumeRole |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm reconsidering that statement 🙂 Since the two operations support different options, it would probably be better to explicitly choose one or the other, both here and in the provider.
@@ -189,6 +189,39 @@ func TestAWSGetCredentials_sharedCredentialsFile(t *testing.T) { | |||
validateCredentialsProvider(credsParam, "accesskey2", "secretkey2", "", sharedConfigCredentialsSource(fileParamName), t) | |||
} | |||
|
|||
func TestAWSGetCredentials_webIdentityToken(t *testing.T) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've created #227 that adds tests for configuring this using environment variables or the shared config files. Those tests can be updated with support for the configuration block in addition to this test
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The PR #227 has been merged, so if you rebase this branch or merge main
into this branch, you can pick up those tests
client := stsClient(awsConfig, c) | ||
|
||
appCreds := stscreds.NewWebIdentityRoleProvider(client, ar.RoleARN, c, func(opts *stscreds.WebIdentityRoleOptions) { | ||
opts.RoleSessionName = ar.SessionName |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FYI stscreds.WebIdentityRoleProvider
doesn't currently support the Policy
field from sts.AssumeRoleWithWebIdentityInput
. I've created aws/aws-sdk-go-v2#1662 and may create a PR in the SDK
Hi @gdavison , thank you for the review. I updated the PR, please take a look. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's looking good
internal/config/config.go
Outdated
@@ -79,3 +80,7 @@ func (c Config) ResolveSharedCredentialsFiles() ([]string, error) { | |||
} | |||
return v, nil | |||
} | |||
|
|||
func (c Config) GetIdentityToken() ([]byte, error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since it only references fields from c.AssumeRoleWithWebIdentity
, it would probably be good to move it there
credentials_test.go
Outdated
if a, e := source, ""; a != e { | ||
t.Errorf("Expected initial source to be %q, %q given", e, a) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For the other credentials providers, we return the name of the credentials source. In this case, it would be stscreds.WebIdentityProviderName
(i.e. WebIdentityCredentials
)
@@ -189,6 +189,39 @@ func TestAWSGetCredentials_sharedCredentialsFile(t *testing.T) { | |||
validateCredentialsProvider(credsParam, "accesskey2", "secretkey2", "", sharedConfigCredentialsSource(fileParamName), t) | |||
} | |||
|
|||
func TestAWSGetCredentials_webIdentityToken(t *testing.T) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The PR #227 has been merged, so if you rebase this branch or merge main
into this branch, you can pick up those tests
hi @gdavison I also rebased this PR, and updated test cases in TestAssumeRoleWithWebIdentity. I changed the input struct (removed |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is great, @mwieczorek. Since AWS has added Duration
and Policy
to stscreds.WebIdentityRoleOptions
(aws/aws-sdk-go-v2#1670), I've added them to AssumeRoleWithWebIdentity
. I've also added the ability to assume another role after using the web identity
thanks @gdavison for all your help :) |
Community Note
Closes #149 <!--- Pull Requests should have an associated issue or may be closed --->
This PR adds an option to pass webIdentityToken to
Config
and use it for getting credentials. No other 'source credentials' are required.