7
7
8
8
This module includes functions which get credentials by calling STS assumeRole\* APIs.
9
9
10
- ### fromTokenFile
10
+ ## fromTokenFile
11
11
12
12
The function ` fromTokenFile ` returns ` CredentialProvider ` that reads credentials as follows:
13
13
14
- - Reads file location of where the OIDC token is stored from either environment or config file paramters .
14
+ - Reads file location of where the OIDC token is stored from either environment or config file parameters .
15
15
- Reads IAM role wanting to be assumed from either environment or config file paramters.
16
16
- Reads optional role session name to be used to distinguish sessions from either environment or config file paramters.
17
17
If session name is not defined, it comes up with a role session name.
@@ -25,7 +25,7 @@ The function `fromTokenFile` returns `CredentialProvider` that reads credentials
25
25
| AWS_IAM_ROLE_ARN | role_arn | true | The IAM role wanting to be assumed |
26
26
| AWS_IAM_ROLE_SESSION_NAME | role_session_name | false | The IAM session name used to distinguish sessions |
27
27
28
- #### Supported configuration
28
+ ### Supported configuration
29
29
30
30
The following options are supported:
31
31
@@ -42,3 +42,58 @@ The following options are supported:
42
42
fulfilled with credentials for the assumed role.
43
43
- ` roleAssumerWithWebIdentity ` - A function that assumes a role with web identity
44
44
and returns a promise fulfilled with credentials for the assumed role.
45
+
46
+ ### Examples
47
+
48
+ A basic example of using fromTokenFile:
49
+
50
+ ``` js
51
+ import { STSClient , AssumeRoleWithWebIdentityCommand } from " @aws-sdk/client-sts" ;
52
+ import { fromTokenFile } from " @aws-sdk/credential-provider-assume-role" ;
53
+
54
+ const stsClient = new STSClient ({});
55
+
56
+ const roleAssumerWithWebIdentity = async (params ) => {
57
+ const { Credentials } = await stsClient .send (
58
+ new AssumeRoleWithWebIdentityCommand (params)
59
+ );
60
+ if (! Credentials || ! Credentials .AccessKeyId || ! Credentials .SecretAccessKey ) {
61
+ throw new Error (` Invalid response from STS.assumeRole call with role ${ params .RoleArn } ` );
62
+ }
63
+ return {
64
+ accessKeyId: Credentials .AccessKeyId ,
65
+ secretAccessKey: Credentials .SecretAccessKey ,
66
+ sessionToken: Credentials .SessionToken ,
67
+ expiration: Credentials .Expiration ,
68
+ };
69
+ };
70
+
71
+ const client = new FooClient ({
72
+ credentials: fromTokenFile ({
73
+ roleAssumerWithWebIdentity
74
+ });
75
+ });
76
+ ```
77
+
78
+ #### Values in environment variables
79
+
80
+ The values can be defined in environment variables as follows:
81
+
82
+ ``` console
83
+ $ node
84
+ > Object.fromEntries(Object.entries(process.env).filter(( [key, value]) => key.startsWith("AWS_")) );
85
+ {
86
+ AWS_WEB_IDENTITY_TOKEN_FILE: '/temp/token',
87
+ AWS_ROLE_ARN: 'arn:aws:iam::123456789012:role/example-role-arn'
88
+ }
89
+ ```
90
+
91
+ #### Values in configuration files
92
+
93
+ The values can be defined in configuration files as follows:
94
+
95
+ ```
96
+ [sample-profile]
97
+ web_identity_token_file = /temp/token
98
+ role_session_name = arn:aws:iam::123456789012:role/example-role-arn
99
+ ```
0 commit comments