33[ ![ NPM version] ( https://img.shields.io/npm/v/@aws-sdk/credential-providers/latest.svg )] ( https://www.npmjs.com/package/@aws-sdk/credential-providers )
44[ ![ NPM downloads] ( https://img.shields.io/npm/dm/@aws-sdk/credential-providers.svg )] ( https://www.npmjs.com/package/@aws-sdk/credential-providers )
55
6- A collection of all credential providers, with default clients .
6+ A collection of all credential providers.
77
88# Table of Contents
99
10+ 1 . [ Terminology] ( #terminology )
10111 . [ From Cognito Identity] ( #fromcognitoidentity )
11121 . [ From Cognito Identity Pool] ( #fromcognitoidentitypool )
12131 . [ From Temporary Credentials] ( #fromtemporarycredentials )
@@ -27,12 +28,64 @@ A collection of all credential providers, with default clients.
27281 . [ From Node.js default credentials provider chain] ( #fromNodeProviderChain )
28291 . [ Creating a custom credentials chain] ( #createCredentialChain )
2930
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+
3083## ` fromCognitoIdentity() `
3184
3285- Uses ` @aws-sdk/client-cognito-identity `
3386- Available in browsers & native apps
3487
35- The function ` fromCognitoIdentity() ` returns ` CredentialsProvider ` that retrieves credentials for
88+ The function ` fromCognitoIdentity() ` returns a credentials provider that retrieves credentials for
3689the provided identity ID. See [ GetCredentialsForIdentity API] [ getcredentialsforidentity_api ]
3790for more information.
3891
@@ -58,9 +111,10 @@ const client = new FooClient({
58111 " api.twitter.com" : " TWITTERTOKEN'" ,
59112 " www.digits.com" : " DIGITSTOKEN" ,
60113 },
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: {},
64118 }),
65119});
66120```
@@ -106,9 +160,10 @@ const client = new FooClient({
106160 " api.twitter.com" : " TWITTERTOKEN" ,
107161 " www.digits.com" : " DIGITSTOKEN" ,
108162 },
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: {},
112167 }),
113168});
114169```
@@ -144,13 +199,15 @@ const client = new FooClient({
144199 DurationSeconds: 3600 ,
145200 // ... For more options see https://docs.aws.amazon.com/STS/latest/APIReference/API_AssumeRole.html
146201 },
147- // Optional. Custom STS client configurations overriding the default ones.
148- clientConfig: { region },
149202 // Optional. A function that returns a promise fulfilled with an MFA token code for the provided
150203 // MFA Serial code. Required if `params` has `SerialNumber` config.
151204 mfaCodeProvider: async (mfaSerial ) => {
152205 return " token" ;
153206 },
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: {},
154211 }),
155212});
156213```
@@ -172,24 +229,29 @@ const client = new FooClient({
172229 credentials: fromWebToken ({
173230 // Required. ARN of the role that the caller is assuming.
174231 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.
177234 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.
182237 roleAssumerWithWebIdentity,
183238 // Optional. An identifier for the assumed role session.
184239 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.
186242 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.
188245 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.
190248 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.
192250 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: {},
193255 }),
194256});
195257```
@@ -380,7 +442,7 @@ const client = new FooClient({
380442 on how the file is configured.
381443- Not available in browsers & native apps.
382444
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
384446` ~/.aws/credentials ` and a shared configuration file at ` ~/.aws/config ` . Both files are expected to
385447be INI formatted with section names corresponding to profiles. Sections in the credentials file are
386448treated 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
395457// const { fromIni } = require("@aws-sdk/credential-providers"); // CommonJS import
396458
397459const 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+
398476 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" ,
402483 // Optional. The path to the shared credentials file. If not specified, the provider will use
403484 // the value in the `AWS_SHARED_CREDENTIALS_FILE` environment variable or a default of
404485 // `~/.aws/credentials`.
@@ -412,8 +493,14 @@ const client = new FooClient({
412493 mfaCodeProvider: async (mfaSerial ) => {
413494 return " token" ;
414495 },
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: {},
417504 }),
418505});
419506```
@@ -540,10 +627,15 @@ import { fromProcess } from "@aws-sdk/credential-providers"; // ES6 import
540627// const { fromProcess } = require("@aws-sdk/credential-providers"); // CommonJS import
541628
542629const client = new FooClient ({
630+ // Optional, available on clients as of v3.714.0.
631+ profile: " my-profile" ,
543632 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" ,
547639 // Optional. The path to the shared credentials file. If not specified, the provider will use
548640 // the value in the `AWS_SHARED_CREDENTIALS_FILE` environment variable or a default of
549641 // `~/.aws/credentials`.
@@ -617,9 +709,12 @@ import { fromTokenFile } from "@aws-sdk/credential-providers"; // ES6 import
617709// const { fromTokenFile } = require("@aws-sdk/credential-providers"); // CommonJS import
618710
619711const client = new FooClient ({
712+ region: " us-west-2" ,
620713 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: {}
623718 });
624719});
625720```
@@ -655,9 +750,14 @@ import { fromSSO } from "@aws-sdk/credential-providers"; // ES6 import
655750// const { fromSSO } = require("@aws-sdk/credential-providers") // CommonJS import
656751
657752const 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.
661761 profile: " my-sso-profile" ,
662762 // Optional. The path to the shared credentials file. If not specified, the provider will use
663763 // 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
778878import { fromNodeProviderChain } from " @aws-sdk/credential-providers" ; // ES6 import
779879// const { fromNodeProviderChain } = require("@aws-sdk/credential-providers") // CommonJS import
780880const 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: {},
785894});
786895```
787896
@@ -799,7 +908,7 @@ All the providers from this package are compatible, and can be used to create su
799908As with _ any_ function provided to the ` credentials ` SDK client constructor configuration field, if the credential object returned does not contain
800909an ` expiration ` (type ` Date ` ), the client will only ever call the provider function once. You do not need to memoize this function.
801910
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
803912provider function will be called again by the client in the course of making SDK requests.
804913
805914To assist with this, the ` createCredentialChain ` has a chainable helper ` .expireAfter(milliseconds: number) ` . An example is included below.
0 commit comments