Skip to content

Commit 48e0568

Browse files
authored
credentials: Fix AssumeRoleProvider documentation for TokenProvider (#1406)
Fixes the AssumeRoleProvider's documentation and error message for using custom TokenProviders. Fixes #1384
1 parent 0023eb2 commit 48e0568

File tree

2 files changed

+23
-15
lines changed

2 files changed

+23
-15
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"id": "ea9496c0-40f4-4c2d-b880-7f5dbc80088a",
3+
"type": "documentation",
4+
"description": "Fixes the AssumeRoleProvider's documentation for using custom TokenProviders.",
5+
"modules": [
6+
"credentials"
7+
]
8+
}

credentials/stscreds/assume_role_provider.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,27 +32,31 @@
3232
// // from assumed role.
3333
// svc := s3.NewFromConfig(cfg)
3434
//
35-
// Assume Role with static MFA Token
35+
// Assume Role with custom MFA Token provider
3636
//
37-
// To assume an IAM role with a MFA token you can either specify a MFA token code
38-
// directly or provide a function to prompt the user each time the credentials
39-
// need to refresh the role's credentials. Specifying the TokenCode should be used
40-
// for short lived operations that will not need to be refreshed, and when you do
41-
// not want to have direct control over the user provides their MFA token.
37+
// To assume an IAM role with a MFA token you can either specify a custom MFA
38+
// token provider or use the SDK's built in StdinTokenProvider that will prompt
39+
// the user for a token code each time the credentials need to to be refreshed.
40+
// Specifying a custom token provider allows you to control where the token
41+
// code is retrieved from, and how it is refreshed.
4242
//
43-
// With TokenCode the AssumeRoleProvider will be not be able to refresh the role's
44-
// credentials.
43+
// With a custom token provider, the provider is responsible for refreshing the
44+
// token code when called.
4545
//
4646
// cfg, err := config.LoadDefaultConfig(context.TODO())
4747
// if err != nil {
4848
// panic(err)
4949
// }
5050
//
51+
// staticTokenProvider := func() (string, error) {
52+
// return someTokenCode, nil
53+
// }
54+
//
5155
// // Create the credentials from AssumeRoleProvider to assume the role
5256
// // referenced by the "myRoleARN" ARN using the MFA token code provided.
5357
// creds := stscreds.NewAssumeRoleProvider(sts.NewFromConfig(cfg), "myRoleArn", func(o *stscreds.AssumeRoleOptions) {
5458
// o.SerialNumber = aws.String("myTokenSerialNumber")
55-
// o.TokenCode = aws.String("00000000")
59+
// o.TokenProvider = staticTokenProvider
5660
// })
5761
//
5862
// cfg.Credentials = aws.NewCredentialsCache(creds)
@@ -209,11 +213,7 @@ type AssumeRoleOptions struct {
209213
// call. See StdinTokenProvider for a provider that prompts and reads from stdin.
210214
//
211215
// This token provider will be called when ever the assumed role's
212-
// credentials need to be refreshed when SerialNumber is also set and
213-
// TokenCode is not set.
214-
//
215-
// If both TokenCode and TokenProvider is set, TokenProvider will be used and
216-
// TokenCode is ignored.
216+
// credentials need to be refreshed when SerialNumber is set.
217217
TokenProvider func() (string, error)
218218

219219
// A list of session tags that you want to pass. Each session tag consists of a key
@@ -281,7 +281,7 @@ func (p *AssumeRoleProvider) Retrieve(ctx context.Context) (aws.Credentials, err
281281
}
282282
input.TokenCode = aws.String(code)
283283
} else {
284-
return aws.Credentials{}, fmt.Errorf("assume role with MFA enabled, but neither TokenCode nor TokenProvider are set")
284+
return aws.Credentials{}, fmt.Errorf("assume role with MFA enabled, but TokenProvider is not set")
285285
}
286286
}
287287

0 commit comments

Comments
 (0)