@@ -41,6 +41,11 @@ func FormatKeyForDisplay(k string) string {
41
41
return fmt .Sprintf ("****************%s" , k [len (k )- 4 :])
42
42
}
43
43
44
+ func isMasterCredentialsProvider (credsProvider aws.CredentialsProvider ) bool {
45
+ _ , ok := credsProvider .(* KeyringProvider )
46
+ return ok
47
+ }
48
+
44
49
// NewMasterCredentialsProvider creates a provider for the master credentials
45
50
func NewMasterCredentialsProvider (k * CredentialKeyring , credentialsName string ) * KeyringProvider {
46
51
return & KeyringProvider {k , credentialsName }
@@ -243,52 +248,60 @@ func (t *TempCredentialsCreator) getSourceCreds(config *ProfileConfig, hasStored
243
248
return nil , fmt .Errorf ("profile %s: credentials missing" , config .ProfileName )
244
249
}
245
250
246
- func (t * TempCredentialsCreator ) GetProviderForProfile (config * ProfileConfig ) (aws.CredentialsProvider , error ) {
247
- hasStoredCredentials , err : = t .Keyring . Has (config . ProfileName )
251
+ func (t * TempCredentialsCreator ) getSourceCredWithSession (config * ProfileConfig , hasStoredCredentials bool ) (sourcecredsProvider aws.CredentialsProvider , err error ) {
252
+ sourcecredsProvider , err = t .getSourceCreds (config , hasStoredCredentials )
248
253
if err != nil {
249
254
return nil , err
250
255
}
251
256
252
- if ! hasStoredCredentials {
253
- if config .HasSSOStartURL () {
254
- log .Printf ("profile %s: using SSO role credentials" , config .ProfileName )
255
- return NewSSORoleCredentialsProvider (t .Keyring .Keyring , config , ! t .DisableCache )
256
- }
257
-
258
- if config .HasWebIdentity () {
259
- log .Printf ("profile %s: using web identity" , config .ProfileName )
260
- return NewAssumeRoleWithWebIdentityProvider (t .Keyring .Keyring , config , ! t .DisableCache )
257
+ if config .HasRole () {
258
+ isMfaChained := config .MfaSerial != "" && config .MfaSerial == t .chainedMfa
259
+ if isMfaChained {
260
+ config .MfaSerial = ""
261
261
}
262
+ log .Printf ("profile %s: using AssumeRole %s" , config .ProfileName , mfaDetails (isMfaChained , config ))
263
+ return NewAssumeRoleProvider (sourcecredsProvider , t .Keyring .Keyring , config , ! t .DisableCache )
264
+ }
262
265
263
- if config .HasCredentialProcess () {
264
- log .Printf ("profile %s: using credential process" , config .ProfileName )
265
- return NewCredentialProcessProvider (t .Keyring .Keyring , config , ! t .DisableCache )
266
+ if isMasterCredentialsProvider (sourcecredsProvider ) {
267
+ canUseGetSessionToken , reason := t .canUseGetSessionToken (config )
268
+ if canUseGetSessionToken {
269
+ t .chainedMfa = config .MfaSerial
270
+ log .Printf ("profile %s: using GetSessionToken %s" , config .ProfileName , mfaDetails (false , config ))
271
+ return NewSessionTokenProvider (sourcecredsProvider , t .Keyring .Keyring , config , ! t .DisableCache )
266
272
}
273
+ log .Printf ("profile %s: skipping GetSessionToken because %s" , config .ProfileName , reason )
267
274
}
268
275
269
- sourcecredsProvider , err := t .getSourceCreds (config , hasStoredCredentials )
276
+ return sourcecredsProvider , nil
277
+ }
278
+
279
+ func (t * TempCredentialsCreator ) GetProviderForProfile (config * ProfileConfig ) (aws.CredentialsProvider , error ) {
280
+ hasStoredCredentials , err := t .Keyring .Has (config .ProfileName )
270
281
if err != nil {
271
282
return nil , err
272
283
}
273
284
274
- if config .HasRole () {
275
- isMfaChained := config .MfaSerial != "" && config .MfaSerial == t .chainedMfa
276
- if isMfaChained {
277
- config .MfaSerial = ""
278
- }
279
- log .Printf ("profile %s: using AssumeRole %s" , config .ProfileName , mfaDetails (isMfaChained , config ))
280
- return NewAssumeRoleProvider (sourcecredsProvider , t .Keyring .Keyring , config , ! t .DisableCache )
285
+ if hasStoredCredentials || config .HasSourceProfile () {
286
+ return t .getSourceCredWithSession (config , hasStoredCredentials )
281
287
}
282
288
283
- canUseGetSessionToken , reason := t .canUseGetSessionToken (config )
284
- if canUseGetSessionToken {
285
- t .chainedMfa = config .MfaSerial
286
- log .Printf ("profile %s: using GetSessionToken %s" , config .ProfileName , mfaDetails (false , config ))
287
- return NewSessionTokenProvider (sourcecredsProvider , t .Keyring .Keyring , config , ! t .DisableCache )
289
+ if config .HasSSOStartURL () {
290
+ log .Printf ("profile %s: using SSO role credentials" , config .ProfileName )
291
+ return NewSSORoleCredentialsProvider (t .Keyring .Keyring , config , ! t .DisableCache )
288
292
}
289
293
290
- log .Printf ("profile %s: skipping GetSessionToken because %s" , config .ProfileName , reason )
291
- return sourcecredsProvider , nil
294
+ if config .HasWebIdentity () {
295
+ log .Printf ("profile %s: using web identity" , config .ProfileName )
296
+ return NewAssumeRoleWithWebIdentityProvider (t .Keyring .Keyring , config , ! t .DisableCache )
297
+ }
298
+
299
+ if config .HasCredentialProcess () {
300
+ log .Printf ("profile %s: using credential process" , config .ProfileName )
301
+ return NewCredentialProcessProvider (t .Keyring .Keyring , config , ! t .DisableCache )
302
+ }
303
+
304
+ return nil , fmt .Errorf ("profile %s: credentials missing" , config .ProfileName )
292
305
}
293
306
294
307
// canUseGetSessionToken determines if GetSessionToken should be used, and if not returns a reason
0 commit comments