Skip to content
This repository was archived by the owner on Jul 31, 2025. It is now read-only.

Commit f01c77e

Browse files
committed
aws/session: Enable SSO provider to be mixed with other credential provider declarations.
1 parent bba6aba commit f01c77e

File tree

8 files changed

+146
-15
lines changed

8 files changed

+146
-15
lines changed

aws/session/credentials.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,6 @@ func resolveCredsFromProfile(cfg *aws.Config,
101101
sharedCfg.Creds,
102102
)
103103

104-
case sharedCfg.hasSSOConfiguration():
105-
creds, err = resolveSSOCredentials(cfg, sharedCfg, handlers)
106-
107-
case len(sharedCfg.CredentialProcess) != 0:
108-
// Get credentials from CredentialProcess
109-
creds = processcreds.NewCredentials(sharedCfg.CredentialProcess)
110-
111104
case len(sharedCfg.CredentialSource) != 0:
112105
creds, err = resolveCredsFromSource(cfg, envCfg,
113106
sharedCfg, handlers, sessOpts,
@@ -123,6 +116,13 @@ func resolveCredsFromProfile(cfg *aws.Config,
123116
sharedCfg.RoleSessionName,
124117
)
125118

119+
case sharedCfg.hasSSOConfiguration():
120+
creds, err = resolveSSOCredentials(cfg, sharedCfg, handlers)
121+
122+
case len(sharedCfg.CredentialProcess) != 0:
123+
// Get credentials from CredentialProcess
124+
creds = processcreds.NewCredentials(sharedCfg.CredentialProcess)
125+
126126
default:
127127
// Fallback to default credentials provider, include mock errors for
128128
// the credential chain so user can identify why credentials failed to

aws/session/credentials_test.go

Lines changed: 65 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"github.com/aws/aws-sdk-go/aws/request"
2424
"github.com/aws/aws-sdk-go/internal/sdktesting"
2525
"github.com/aws/aws-sdk-go/internal/shareddefaults"
26+
"github.com/aws/aws-sdk-go/private/protocol"
2627
"github.com/aws/aws-sdk-go/service/sts"
2728
)
2829

@@ -63,11 +64,31 @@ func setupCredentialsEndpoints(t *testing.T) (endpoints.Resolver, func()) {
6364

6465
stsServer := httptest.NewServer(http.HandlerFunc(
6566
func(w http.ResponseWriter, r *http.Request) {
66-
w.Write([]byte(fmt.Sprintf(
67-
assumeRoleRespMsg,
68-
time.Now().
69-
Add(15*time.Minute).
70-
Format("2006-01-02T15:04:05Z"))))
67+
if err := r.ParseForm(); err != nil {
68+
w.WriteHeader(500)
69+
return
70+
}
71+
72+
form := r.Form
73+
74+
switch form.Get("Action") {
75+
case "AssumeRole":
76+
w.Write([]byte(fmt.Sprintf(
77+
assumeRoleRespMsg,
78+
time.Now().
79+
Add(15*time.Minute).
80+
Format(protocol.ISO8601TimeFormat))))
81+
return
82+
case "AssumeRoleWithWebIdentity":
83+
w.Write([]byte(fmt.Sprintf(assumeRoleWithWebIdentityResponse,
84+
time.Now().
85+
Add(15*time.Minute).
86+
Format(protocol.ISO8601TimeFormat))))
87+
return
88+
default:
89+
w.WriteHeader(404)
90+
return
91+
}
7192
}))
7293

7394
ssoServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
@@ -275,6 +296,23 @@ func TestSharedConfigCredentialSource(t *testing.T) {
275296
return func() {}, nil
276297
},
277298
},
299+
{
300+
name: "sso mixed with credential process provider",
301+
profile: "sso_mixed_credproc",
302+
expectedAccessKey: "SSO_AKID",
303+
expectedSecretKey: "SSO_SECRET_KEY",
304+
expectedSessionToken: "SSO_SESSION_TOKEN",
305+
init: func() (func(), error) {
306+
return ssoTestSetup()
307+
},
308+
},
309+
{
310+
name: "sso mixed with web identity token provider",
311+
profile: "sso_mixed_webident",
312+
expectedAccessKey: "WEB_IDENTITY_AKID",
313+
expectedSecretKey: "WEB_IDENTITY_SECRET",
314+
expectedSessionToken: "WEB_IDENTITY_SESSION_TOKEN",
315+
},
278316
}
279317

280318
for i, c := range cases {
@@ -403,6 +441,28 @@ const assumeRoleRespMsg = `
403441
</AssumeRoleResponse>
404442
`
405443

444+
var assumeRoleWithWebIdentityResponse = `<AssumeRoleWithWebIdentityResponse xmlns="https://sts.amazonaws.com/doc/2011-06-15/">
445+
<AssumeRoleWithWebIdentityResult>
446+
<SubjectFromWebIdentityToken>amzn1.account.AF6RHO7KZU5XRVQJGXK6HB56KR2A</SubjectFromWebIdentityToken>
447+
<Audience>client.5498841531868486423.1548@apps.example.com</Audience>
448+
<AssumedRoleUser>
449+
<Arn>arn:aws:sts::123456789012:assumed-role/FederatedWebIdentityRole/app1</Arn>
450+
<AssumedRoleId>AROACLKWSDQRAOEXAMPLE:app1</AssumedRoleId>
451+
</AssumedRoleUser>
452+
<Credentials>
453+
<AccessKeyId>WEB_IDENTITY_AKID</AccessKeyId>
454+
<SecretAccessKey>WEB_IDENTITY_SECRET</SecretAccessKey>
455+
<SessionToken>WEB_IDENTITY_SESSION_TOKEN</SessionToken>
456+
<Expiration>%s</Expiration>
457+
</Credentials>
458+
<Provider>www.amazon.com</Provider>
459+
</AssumeRoleWithWebIdentityResult>
460+
<ResponseMetadata>
461+
<RequestId>request-id</RequestId>
462+
</ResponseMetadata>
463+
</AssumeRoleWithWebIdentityResponse>
464+
`
465+
406466
const getRoleCredentialsResponse = `{
407467
"roleCredentials": {
408468
"accessKeyId": "SSO_AKID",

aws/session/shared_config.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,6 @@ func (cfg *sharedConfig) validateCredentialType() error {
401401
len(cfg.CredentialSource) != 0,
402402
len(cfg.CredentialProcess) != 0,
403403
len(cfg.WebIdentityTokenFile) != 0,
404-
cfg.hasSSOConfiguration(),
405404
) {
406405
return ErrSharedConfigSourceCollision
407406
}
@@ -459,6 +458,10 @@ func (cfg *sharedConfig) clearCredentialOptions() {
459458
cfg.CredentialProcess = ""
460459
cfg.WebIdentityTokenFile = ""
461460
cfg.Creds = credentials.Value{}
461+
cfg.SSOAccountID = ""
462+
cfg.SSORegion = ""
463+
cfg.SSORoleName = ""
464+
cfg.SSOStartURL = ""
462465
}
463466

464467
func (cfg *sharedConfig) clearAssumeRoleOptions() {

aws/session/shared_config_test.go

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,38 @@ func TestLoadSharedConfig(t *testing.T) {
261261
{
262262
Filenames: []string{testConfigFilename},
263263
Profile: "source_sso_and_assume",
264-
Err: fmt.Errorf("only one credential type may be specified per profile"),
264+
Expected: sharedConfig{
265+
Profile: "source_sso_and_assume",
266+
RoleARN: "source_sso_and_assume_arn",
267+
SourceProfileName: "sso_and_assume",
268+
SourceProfile: &sharedConfig{
269+
Profile: "sso_and_assume",
270+
RoleARN: "sso_with_assume_role_arn",
271+
SourceProfileName: "multiple_assume_role_with_credential_source",
272+
SourceProfile: &sharedConfig{
273+
Profile: "multiple_assume_role_with_credential_source",
274+
RoleARN: "multiple_assume_role_with_credential_source_role_arn",
275+
SourceProfileName: "assume_role_with_credential_source",
276+
SourceProfile: &sharedConfig{
277+
Profile: "assume_role_with_credential_source",
278+
RoleARN: "assume_role_with_credential_source_role_arn",
279+
CredentialSource: credSourceEc2Metadata,
280+
},
281+
},
282+
},
283+
},
284+
},
285+
{
286+
Filenames: []string{testConfigFilename},
287+
Profile: "sso_mixed_credproc",
288+
Expected: sharedConfig{
289+
Profile: "sso_mixed_credproc",
290+
SSOAccountID: "012345678901",
291+
SSORegion: "us-west-2",
292+
SSORoleName: "TestRole",
293+
SSOStartURL: "https://127.0.0.1/start",
294+
CredentialProcess: "/path/to/process",
295+
},
265296
},
266297
}
267298

aws/session/testdata/credential_source_config

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,17 @@ sso_start_url = https://THIS_SHOULD_NOT_BE_IN_TESTDATA_CACHE/start
5757
sso_account_id = 012345678901
5858
sso_role_name = TestRole
5959

60+
[profile sso_mixed_credproc]
61+
sso_account_id = 012345678901
62+
sso_region = us-west-2
63+
sso_role_name = TestRole
64+
sso_start_url = https://127.0.0.1/start
65+
credential_process = cat ./testdata/test_json.json
66+
67+
[profile sso_mixed_webident]
68+
web_identity_token_file = ./testdata/wit.txt
69+
role_arn = sso_mixed_webident_arn
70+
sso_account_id = 012345678901
71+
sso_region = us-west-2
72+
sso_role_name = TestRole
73+
sso_start_url = https://127.0.0.1/start

aws/session/testdata/credential_source_config_for_windows

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,19 @@ credential_process = type .\testdata\test_json.json
77

88
[chained_cred_proc]
99
role_arn = assume_role_w_creds_proc_source_prof
10-
source_profile = cred_proc_no_arn_set
10+
source_profile = cred_proc_no_arn_set
11+
12+
[profile sso_mixed_credproc]
13+
sso_account_id = 012345678901
14+
sso_region = us-west-2
15+
sso_role_name = TestRole
16+
sso_start_url = https://127.0.0.1/start
17+
credential_process = type .\testdata\test_json.json
18+
19+
[profile sso_mixed_webident]
20+
web_identity_token_file = .\testdata\wit.txt
21+
role_arn = sso_mixed_webident_arn
22+
sso_account_id = 012345678901
23+
sso_region = us-west-2
24+
sso_role_name = TestRole
25+
sso_start_url = https://127.0.0.1/start

aws/session/testdata/shared_config

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,3 +140,10 @@ source_profile = multiple_assume_role_with_credential_source
140140
[profile source_sso_and_assume]
141141
role_arn = source_sso_and_assume_arn
142142
source_profile = sso_and_assume
143+
144+
[profile sso_mixed_credproc]
145+
sso_account_id = 012345678901
146+
sso_region = us-west-2
147+
sso_role_name = TestRole
148+
sso_start_url = https://127.0.0.1/start
149+
credential_process = /path/to/process

aws/session/testdata/wit.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
YXdzIHNkayBmb3IgZ28gd2ViIGlkZW50aXR5IHRva2Vu

0 commit comments

Comments
 (0)