3
3
[ ![ NPM version] ( https://img.shields.io/npm/v/@aws-sdk/credential-providers/latest.svg )] ( https://www.npmjs.com/package/@aws-sdk/credential-providers )
4
4
[ ![ NPM downloads] ( https://img.shields.io/npm/dm/@aws-sdk/credential-providers.svg )] ( https://www.npmjs.com/package/@aws-sdk/credential-providers )
5
5
6
- A collection of all credential providers, with default clients .
6
+ A collection of all credential providers.
7
7
8
8
# Table of Contents
9
9
10
+ 1 . [ Terminology] ( #terminology )
10
11
1 . [ From Cognito Identity] ( #fromcognitoidentity )
11
12
1 . [ From Cognito Identity Pool] ( #fromcognitoidentitypool )
12
13
1 . [ From Temporary Credentials] ( #fromtemporarycredentials )
@@ -27,12 +28,64 @@ A collection of all credential providers, with default clients.
27
28
1 . [ From Node.js default credentials provider chain] ( #fromNodeProviderChain )
28
29
1 . [ Creating a custom credentials chain] ( #createCredentialChain )
29
30
31
+ ## Terminology
32
+
33
+ #### Credentials Provider
34
+
35
+ An ` AwsCredentialIdentityProvider ` is any function that matches the signature:
36
+
37
+ ``` ts
38
+ () =>
39
+ Promise <{
40
+ /**
41
+ * AWS access key ID
42
+ */
43
+ readonly accessKeyId: string ;
44
+ /**
45
+ * AWS secret access key
46
+ */
47
+ readonly secretAccessKey: string ;
48
+ /**
49
+ * A security or session token to use with these credentials. Usually
50
+ * present for temporary credentials.
51
+ */
52
+ readonly sessionToken? : string ;
53
+ /**
54
+ * A `Date` when the identity or credential will no longer be accepted.
55
+ * You can set or override this on the client side to force a refresh
56
+ * call of the function supplying the credentials when 5 minutes remain.
57
+ */
58
+ readonly expiration? : Date ;
59
+ }>;
60
+ ```
61
+
62
+ #### Outer and inner clients
63
+
64
+ A "parent/outer/upper/caller" (position), or "data" (purpose) client refers
65
+ to a client being initialized explicitly by the SDK user.
66
+
67
+ An "inner" (position), or "credentials" (purpose) client
68
+ refers to a client being initialized by the SDK in the course
69
+ of retrieving credentials. Several AWS SDK credentials providers
70
+ make use of inner clients such as Cognito, SSO, STS, and SSO-OIDC.
71
+
72
+ ``` ts
73
+ // Example: outer client and inner client
74
+ const s3 = new S3Client ({
75
+ credentials: fromIni (),
76
+ });
77
+ ```
78
+
79
+ In the above example, ` S3Client ` is the outer client, and
80
+ if the ` fromIni ` credentials provider uses STS::AssumeRole, the
81
+ ` STSClient ` initialized by the SDK is the inner client.
82
+
30
83
## ` fromCognitoIdentity() `
31
84
32
85
- Uses ` @aws-sdk/client-cognito-identity `
33
86
- Available in browsers & native apps
34
87
35
- The function ` fromCognitoIdentity() ` returns ` CredentialsProvider ` that retrieves credentials for
88
+ The function ` fromCognitoIdentity() ` returns a credentials provider that retrieves credentials for
36
89
the provided identity ID. See [ GetCredentialsForIdentity API] [ getcredentialsforidentity_api ]
37
90
for more information.
38
91
@@ -58,9 +111,10 @@ const client = new FooClient({
58
111
" api.twitter.com" : " TWITTERTOKEN'" ,
59
112
" www.digits.com" : " DIGITSTOKEN" ,
60
113
},
61
- // Optional. Custom client config if you need overwrite default Cognito Identity client
62
- // configuration.
63
- clientConfig: { region },
114
+ // Optional overrides. This is passed to an inner Cognito client
115
+ // instantiated to resolve the credentials. Region and profile
116
+ // are inherited from the upper client if present unless overridden.
117
+ clientConfig: {},
64
118
}),
65
119
});
66
120
```
@@ -106,9 +160,10 @@ const client = new FooClient({
106
160
" api.twitter.com" : " TWITTERTOKEN" ,
107
161
" www.digits.com" : " DIGITSTOKEN" ,
108
162
},
109
- // Optional. Custom client config if you need overwrite default Cognito Identity client
110
- // configuration.
111
- clientConfig: { region },
163
+ // Optional overrides. This is passed to an inner Cognito client
164
+ // instantiated to resolve the credentials. Region and profile
165
+ // are inherited from the upper client if present unless overridden.
166
+ clientConfig: {},
112
167
}),
113
168
});
114
169
```
@@ -144,13 +199,15 @@ const client = new FooClient({
144
199
DurationSeconds: 3600 ,
145
200
// ... For more options see https://docs.aws.amazon.com/STS/latest/APIReference/API_AssumeRole.html
146
201
},
147
- // Optional. Custom STS client configurations overriding the default ones.
148
- clientConfig: { region },
149
202
// Optional. A function that returns a promise fulfilled with an MFA token code for the provided
150
203
// MFA Serial code. Required if `params` has `SerialNumber` config.
151
204
mfaCodeProvider: async (mfaSerial ) => {
152
205
return " token" ;
153
206
},
207
+ // Optional overrides. This is passed to an inner STS client instantiated to
208
+ // resolve the credentials. Region and profile
209
+ // are inherited from the upper client if present unless overridden.
210
+ clientConfig: {},
154
211
}),
155
212
});
156
213
```
@@ -172,24 +229,29 @@ const client = new FooClient({
172
229
credentials: fromWebToken ({
173
230
// Required. ARN of the role that the caller is assuming.
174
231
roleArn: " arn:aws:iam::1234567890:role/RoleA" ,
175
- // Required. The OAuth 2.0 access token or OpenID Connect ID token that is provided by the
176
- // identity provider.
232
+ // Required. The OAuth 2.0 access token or OpenID Connect ID token that is
233
+ // provided by the identity provider.
177
234
webIdentityToken: await openIdProvider (),
178
- // Optional. Custom STS client configurations overriding the default ones.
179
- clientConfig: { region },
180
- // Optional. A function that assumes a role with web identity and returns a promise fulfilled
181
- // with credentials for the assumed role.
235
+ // Optional. A function that assumes a role with web identity and returns
236
+ // a promise fulfilled with credentials for the assumed role.
182
237
roleAssumerWithWebIdentity,
183
238
// Optional. An identifier for the assumed role session.
184
239
roleSessionName: " session_123" ,
185
- // Optional. The fully qualified host component of the domain name of the identity provider.
240
+ // Optional. The fully qualified host component of the domain name of the
241
+ // identity provider.
186
242
providerId: " graph.facebook.com" ,
187
- // Optional. ARNs of the IAM managed policies that you want to use as managed session.
243
+ // Optional. ARNs of the IAM managed policies that you want to use as
244
+ // managed session.
188
245
policyArns: [{ arn: " arn:aws:iam::1234567890:policy/SomePolicy" }],
189
- // Optional. An IAM policy in JSON format that you want to use as an inline session policy.
246
+ // Optional. An IAM policy in JSON format that you want to use as an
247
+ // inline session policy.
190
248
policy: " JSON_STRING" ,
191
- // Optional. The duration, in seconds, of the role session. Default to 3600.
249
+ // Optional. The duration, in seconds, of the role session. Default 3600.
192
250
durationSeconds: 7200 ,
251
+ // Optional overrides. This is passed to an inner STS client
252
+ // instantiated to resolve the credentials. Region and profile
253
+ // are inherited from the upper client if present unless overridden.
254
+ clientConfig: {},
193
255
}),
194
256
});
195
257
```
@@ -380,7 +442,7 @@ const client = new FooClient({
380
442
on how the file is configured.
381
443
- Not available in browsers & native apps.
382
444
383
- ` fromIni ` creates ` AwsCredentialIdentityProvider ` functions that read from a shared credentials file at
445
+ ` fromIni ` creates an ` AwsCredentialIdentityProvider ` function that reads from a shared credentials file at
384
446
` ~/.aws/credentials ` and a shared configuration file at ` ~/.aws/config ` . Both files are expected to
385
447
be INI formatted with section names corresponding to profiles. Sections in the credentials file are
386
448
treated as profile names, whereas profile sections in the config file must have the format of
@@ -395,10 +457,29 @@ import { fromIni } from "@aws-sdk/credential-providers"; // ES6 import
395
457
// const { fromIni } = require("@aws-sdk/credential-providers"); // CommonJS import
396
458
397
459
const client = new FooClient ({
460
+ // As of v3.714.0, an easy way to select a profile is to set it on the client.
461
+ // This will read both client configuration and credentials instructions
462
+ // from that profile. The order of priority is:
463
+ // 1. this field (only applies to this client).
464
+ // 2. AWS_PROFILE environment variable (affects all clients).
465
+ // 3. the default profile.
466
+ profile: " my-profile" ,
467
+
468
+ // Please note that the data client's region
469
+ // will not be used by STS requests originating from the `fromIni`
470
+ // provider if the profile(s) used have their own configured regions.
471
+ // If the profile(s) have no regions set, then the data client's
472
+ // region will be the fallback for the inner STS client.
473
+ // For SSO via `fromIni`, the `sso_region` value will be used, since it is required.
474
+ region: " us-west-2" ,
475
+
398
476
credentials: fromIni ({
399
- // Optional. The configuration profile to use. If not specified, the provider will use the value
400
- // in the `AWS_PROFILE` environment variable or a default of `default`.
401
- profile: " profile" ,
477
+ // Optional. Defaults to the client's profile if that is set.
478
+ // You can specify a profile here as well, but this applies
479
+ // only to the credential resolution and not to the upper client.
480
+ // Use this instead of the client profile if you need a separate profile
481
+ // for credentials.
482
+ profile: " my-profile" ,
402
483
// Optional. The path to the shared credentials file. If not specified, the provider will use
403
484
// the value in the `AWS_SHARED_CREDENTIALS_FILE` environment variable or a default of
404
485
// `~/.aws/credentials`.
@@ -412,8 +493,14 @@ const client = new FooClient({
412
493
mfaCodeProvider: async (mfaSerial ) => {
413
494
return " token" ;
414
495
},
415
- // Optional. Custom STS client configurations overriding the default ones.
416
- clientConfig: { region },
496
+ // Optional overrides. This is passed to an inner STS or SSO client
497
+ // instantiated to resolve the credentials. Region and profile
498
+ // are inherited from the upper client if present unless overridden, so
499
+ // it should not be necessary to set those.
500
+ //
501
+ // Warning: setting a region here overrides the region set in the config file
502
+ // for the selected profile.
503
+ clientConfig: {},
417
504
}),
418
505
});
419
506
```
@@ -540,10 +627,15 @@ import { fromProcess } from "@aws-sdk/credential-providers"; // ES6 import
540
627
// const { fromProcess } = require("@aws-sdk/credential-providers"); // CommonJS import
541
628
542
629
const client = new FooClient ({
630
+ // Optional, available on clients as of v3.714.0.
631
+ profile: " my-profile" ,
543
632
credentials: fromProcess ({
544
- // Optional. The configuration profile to use. If not specified, the provider will use the value
545
- // in the `AWS_PROFILE` environment variable or a default of `default`.
546
- profile: " profile" ,
633
+ // Optional. Defaults to the client's profile if that is set.
634
+ // You can specify a profile here as well, but this applies
635
+ // only to the credential resolution and not to the upper client.
636
+ // Use this instead of the client profile if you need a separate profile
637
+ // for credentials.
638
+ profile: " my-profile" ,
547
639
// Optional. The path to the shared credentials file. If not specified, the provider will use
548
640
// the value in the `AWS_SHARED_CREDENTIALS_FILE` environment variable or a default of
549
641
// `~/.aws/credentials`.
@@ -617,9 +709,12 @@ import { fromTokenFile } from "@aws-sdk/credential-providers"; // ES6 import
617
709
// const { fromTokenFile } = require("@aws-sdk/credential-providers"); // CommonJS import
618
710
619
711
const client = new FooClient ({
712
+ region: " us-west-2" ,
620
713
credentials: fromTokenFile ({
621
- // Optional. STS client config to make the assume role request.
622
- clientConfig: { region }
714
+ // Optional overrides. This is passed to an inner STS client
715
+ // instantiated to resolve the credentials. Region is inherited
716
+ // from the upper client if present unless overridden.
717
+ clientConfig: {}
623
718
});
624
719
});
625
720
```
@@ -655,9 +750,14 @@ import { fromSSO } from "@aws-sdk/credential-providers"; // ES6 import
655
750
// const { fromSSO } = require("@aws-sdk/credential-providers") // CommonJS import
656
751
657
752
const client = new FooClient ({
658
- credentials: fromSSO ({
659
- // Optional. The configuration profile to use. If not specified, the provider will use the value
660
- // in the `AWS_PROFILE` environment variable or `default` by default.
753
+ // Optional, available on clients as of v3.714.0.
754
+ profile: " my-sso-profile" ,
755
+ credentials: fromProcess ({
756
+ // Optional. Defaults to the client's profile if that is set.
757
+ // You can specify a profile here as well, but this applies
758
+ // only to the credential resolution and not to the upper client.
759
+ // Use this instead of the client profile if you need a separate profile
760
+ // for credentials.
661
761
profile: " my-sso-profile" ,
662
762
// Optional. The path to the shared credentials file. If not specified, the provider will use
663
763
// the value in the `AWS_SHARED_CREDENTIALS_FILE` environment variable or a default of
@@ -778,10 +878,19 @@ messages be sent to the Instance Metadata Service
778
878
import { fromNodeProviderChain } from " @aws-sdk/credential-providers" ; // ES6 import
779
879
// const { fromNodeProviderChain } = require("@aws-sdk/credential-providers") // CommonJS import
780
880
const credentialProvider = fromNodeProviderChain ({
781
- // ...any input of fromEnv(), fromSSO(), fromTokenFile(), fromIni(),
782
- // fromProcess(), fromInstanceMetadata(), fromContainerMetadata()
783
- // Optional. Custom STS client configurations overriding the default ones.
784
- clientConfig: { region },
881
+ // This provider accepts any input of fromEnv(), fromSSO(), fromTokenFile(),
882
+ // fromIni(), fromProcess(), fromInstanceMetadata(), fromContainerMetadata()
883
+ // that exist in the default credential chain.
884
+
885
+ // Optional client overrides. This is passed to an inner credentials client
886
+ // that may be STS, SSO, or other instantiated to resolve the credentials.
887
+ // Region and profile are inherited from the upper client if present
888
+ // unless overridden, so it should not be necessary to set those.
889
+ //
890
+ // Warning: setting a region here may override the region set in
891
+ // the config file for the selected profile if profile-based
892
+ // credentials are used.
893
+ clientConfig: {},
785
894
});
786
895
```
787
896
@@ -799,7 +908,7 @@ All the providers from this package are compatible, and can be used to create su
799
908
As with _ any_ function provided to the ` credentials ` SDK client constructor configuration field, if the credential object returned does not contain
800
909
an ` expiration ` (type ` Date ` ), the client will only ever call the provider function once. You do not need to memoize this function.
801
910
802
- To enable automatic refresh, the credential provider function should set an ` expiration ` (` Date ` ) field. When this expiration approaches within 5 minutes, the
911
+ To enable automatic refresh, the credential provider function should set an ` expiration ` (` Date ` ) field. When this expiration approaches within 5 minutes, the
803
912
provider function will be called again by the client in the course of making SDK requests.
804
913
805
914
To assist with this, the ` createCredentialChain ` has a chainable helper ` .expireAfter(milliseconds: number) ` . An example is included below.
0 commit comments