Skip to content
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

Enable assume role with web identity using config.WithWebIdentityRoleCredentialOptions #2424

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .changelog/ade6f215447f4db38352c773474788a4.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"id": "ade6f215-447f-4db3-8352-c773474788a4",
"type": "bugfix",
"description": "Insert LoadOptions into cred chain switch to enable directly loading web identity role",
"modules": [
"config"
]
}
24 changes: 24 additions & 0 deletions config/resolve_credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,11 @@ func resolveCredentialChain(ctx context.Context, cfg *aws.Config, configs config
return err
}

loadOptions := getLoadOptions(other)

switch {
case loadOptions.WebIdentityRoleCredentialOptions != nil:
err = assumeWebIdentity(ctx, cfg, "WebIdTokenFilePathPlaceHolder", "RoleARNFPlaceHolder", "RoleSessionNamePlaceHolder", configs)
case sharedProfileSet:
err = resolveCredsFromProfile(ctx, cfg, envConfig, sharedConfig, other)
case envConfig.Credentials.HasKeys():
Expand Down Expand Up @@ -182,6 +186,26 @@ func resolveCredsFromProfile(ctx context.Context, cfg *aws.Config, envConfig *En
return nil
}

func getLoadOptions(cfgs configs) (loadOptions *LoadOptions) {
for _, cfg := range cfgs {
switch c := cfg.(type) {
case LoadOptions:
if loadOptions == nil {
loadOptions = &c
}
case *LoadOptions:
if loadOptions == nil {
loadOptions = c
}
default:
}
}
if loadOptions == nil {
loadOptions = &LoadOptions{}
}
return
}

func resolveSSOCredentials(ctx context.Context, cfg *aws.Config, sharedConfig *SharedConfig, configs configs) error {
if err := sharedConfig.validateSSOConfiguration(); err != nil {
return err
Expand Down
28 changes: 28 additions & 0 deletions config/resolve_web_identity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package config

import (
"context"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/credentials/stscreds"
"github.com/aws/aws-sdk-go-v2/feature/ec2/imds"
"github.com/aws/aws-sdk-go-v2/internal/awstesting"
"os"
"path/filepath"
Expand Down Expand Up @@ -56,4 +58,30 @@ func TestResolveWebIdentityWithOptions(t *testing.T) {
t.Fatalf("expected profile parsing error, got %v", err)
}
})

t.Run("token supplied directly from loadOptions", func(t *testing.T) {
restoreEnv := initConfigTestEnv()
defer awstesting.PopEnv(restoreEnv)

var tokenFile = filepath.Join("testdata", "wit.txt")
os.Setenv("AWS_REGION", "us-east-1")

config, err := LoadDefaultConfig(context.Background(),
WithEC2IMDSClientEnableState(imds.ClientDisabled),
WithWebIdentityRoleCredentialOptions(func(options *stscreds.WebIdentityRoleOptions) {
options.TokenRetriever = stscreds.IdentityTokenFile(tokenFile)
options.RoleARN = "test-arn"
options.RoleSessionName = "test-session"
}),
)

if err != nil {
t.Fatalf("expect no error, got %v", err)
}

target := stscreds.WebIdentityRoleProvider{}
if !aws.IsCredentialsProvider(config.Credentials, &target) {
t.Fatalf("expected type %T", target)
}
})
}
Loading