@aws-sdk/credential-providers 3.624.0 → 3.630.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -25,6 +25,7 @@ A collection of all credential providers, with default clients.
25
25
  1. [SSO login with AWS CLI](#sso-login-with-the-aws-cli)
26
26
  1. [Sample Files](#sample-files-2)
27
27
  1. [From Node.js default credentials provider chain](#fromNodeProviderChain)
28
+ 1. [Creating a custom credentials chain](#createCredentialChain)
28
29
 
29
30
  ## `fromCognitoIdentity()`
30
31
 
@@ -704,14 +705,14 @@ CLI profile name [123456789011_ReadOnly]: my-sso-profile<ENTER>
704
705
 
705
706
  ```javascript
706
707
  //...
707
- const client = new FooClient({ credentials: fromSSO({ profile: "my-sso-profile" })});
708
+ const client = new FooClient({ credentials: fromSSO({ profile: "my-sso-profile" }) });
708
709
  ```
709
710
 
710
711
  Alternatively, the SSO credential provider is supported in shared INI credentials provider
711
712
 
712
713
  ```javascript
713
714
  //...
714
- const client = new FooClient({ credentials: fromIni({ profile: "my-sso-profile" })});
715
+ const client = new FooClient({ credentials: fromIni({ profile: "my-sso-profile" }) });
715
716
  ```
716
717
 
717
718
  3. To log out from the current SSO session, use the AWS CLI:
@@ -784,6 +785,60 @@ const credentialProvider = fromNodeProviderChain({
784
785
  });
785
786
  ```
786
787
 
788
+ ## `createCredentialChain()`
789
+
790
+ You can use this helper to create a credential chain of your own.
791
+
792
+ A credential chain is created from a list of functions of the signature () => Promise<[AwsCredentialIdentity](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/Package/-smithy-types/Interface/AwsCredentialIdentity/)>,
793
+ composed together such that the overall chain has the **same** signature.
794
+
795
+ That is why you can provide the chained credential provider to the same field (`credentials`) as any single provider function.
796
+
797
+ All the providers from this package are compatible, and can be used to create such a chain.
798
+
799
+ As with _any_ function provided to the `credentials` SDK client constructor configuration field, if the credential object returned does not contain
800
+ an `expiration` (type `Date`), the client will only ever call the provider function once. You do not need to memoize this function.
801
+
802
+ To enable automatic refresh, the credential provider function should set an `expiration` (`Date`) field. When this expiration approaches within 5 minutes, the
803
+ provider function will be called again by the client in the course of making SDK requests.
804
+
805
+ To assist with this, the `createCredentialChain` has a chainable helper `.expireAfter(milliseconds: number)`. An example is included below.
806
+
807
+ ```ts
808
+ import { fromEnv, fromIni, createCredentialChain } from "@aws-sdk/credential-providers";
809
+ import { S3 } from "@aws-sdk/client-s3";
810
+
811
+ // You can mix existing AWS SDK credential providers
812
+ // and custom async functions returning credential objects.
813
+ new S3({
814
+ credentials: createCredentialChain(
815
+ fromEnv(),
816
+ async () => {
817
+ // credentials customized by your code...
818
+ return credentials;
819
+ },
820
+ fromIni()
821
+ ),
822
+ });
823
+
824
+ // Set a max duration on the credentials (client side only).
825
+ // A set expiration will cause the credentials function to be called again
826
+ // when the time left is less than 5 minutes.
827
+ new S3({
828
+ // This setting indicates expiry after 15 minutes (in milliseconds) with `15 * 60_000`.
829
+ // Due to the 5 minute expiry window, the function will be called approximately every
830
+ // 10 minutes under continuous usage of this client.
831
+ credentials: createCredentialChain(fromEnv(), fromIni()).expireAfter(15 * 60_000),
832
+ });
833
+
834
+ // Apply shared init properties.
835
+ const init = { logger: console };
836
+
837
+ new S3({
838
+ credentials: createCredentialChain(fromEnv(init), fromIni(init)),
839
+ });
840
+ ```
841
+
787
842
  ## Add Custom Headers to STS assume-role calls
788
843
 
789
844
  You can specify the plugins--groups of middleware, to inject to the STS client.
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createCredentialChain = void 0;
4
+ const property_provider_1 = require("@smithy/property-provider");
5
+ const createCredentialChain = (...credentialProviders) => {
6
+ let expireAfter = -1;
7
+ const baseFunction = async () => {
8
+ const credentials = await (0, property_provider_1.chain)(...credentialProviders)();
9
+ if (!credentials.expiration && expireAfter !== -1) {
10
+ credentials.expiration = new Date(Date.now() + expireAfter);
11
+ }
12
+ return credentials;
13
+ };
14
+ const withOptions = Object.assign(baseFunction, {
15
+ expireAfter(milliseconds) {
16
+ if (milliseconds < 5 * 60000) {
17
+ throw new Error("@aws-sdk/credential-providers - createCredentialChain(...).expireAfter(ms) may not be called with a duration lower than five minutes.");
18
+ }
19
+ expireAfter = milliseconds;
20
+ return withOptions;
21
+ },
22
+ });
23
+ return withOptions;
24
+ };
25
+ exports.createCredentialChain = createCredentialChain;
package/dist-cjs/index.js CHANGED
@@ -2,12 +2,13 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.fromHttp = void 0;
4
4
  const tslib_1 = require("tslib");
5
+ tslib_1.__exportStar(require("./createCredentialChain"), exports);
5
6
  tslib_1.__exportStar(require("./fromCognitoIdentity"), exports);
6
7
  tslib_1.__exportStar(require("./fromCognitoIdentityPool"), exports);
7
8
  tslib_1.__exportStar(require("./fromContainerMetadata"), exports);
8
- tslib_1.__exportStar(require("./fromEnv"), exports);
9
9
  var credential_provider_http_1 = require("@aws-sdk/credential-provider-http");
10
10
  Object.defineProperty(exports, "fromHttp", { enumerable: true, get: function () { return credential_provider_http_1.fromHttp; } });
11
+ tslib_1.__exportStar(require("./fromEnv"), exports);
11
12
  tslib_1.__exportStar(require("./fromIni"), exports);
12
13
  tslib_1.__exportStar(require("./fromInstanceMetadata"), exports);
13
14
  tslib_1.__exportStar(require("./fromNodeProviderChain"), exports);
@@ -0,0 +1,21 @@
1
+ import { chain as propertyProviderChain } from "@smithy/property-provider";
2
+ export const createCredentialChain = (...credentialProviders) => {
3
+ let expireAfter = -1;
4
+ const baseFunction = async () => {
5
+ const credentials = await propertyProviderChain(...credentialProviders)();
6
+ if (!credentials.expiration && expireAfter !== -1) {
7
+ credentials.expiration = new Date(Date.now() + expireAfter);
8
+ }
9
+ return credentials;
10
+ };
11
+ const withOptions = Object.assign(baseFunction, {
12
+ expireAfter(milliseconds) {
13
+ if (milliseconds < 5 * 60000) {
14
+ throw new Error("@aws-sdk/credential-providers - createCredentialChain(...).expireAfter(ms) may not be called with a duration lower than five minutes.");
15
+ }
16
+ expireAfter = milliseconds;
17
+ return withOptions;
18
+ },
19
+ });
20
+ return withOptions;
21
+ };
package/dist-es/index.js CHANGED
@@ -1,8 +1,9 @@
1
+ export * from "./createCredentialChain";
1
2
  export * from "./fromCognitoIdentity";
2
3
  export * from "./fromCognitoIdentityPool";
3
4
  export * from "./fromContainerMetadata";
4
- export * from "./fromEnv";
5
5
  export { fromHttp } from "@aws-sdk/credential-provider-http";
6
+ export * from "./fromEnv";
6
7
  export * from "./fromIni";
7
8
  export * from "./fromInstanceMetadata";
8
9
  export * from "./fromNodeProviderChain";
@@ -0,0 +1,44 @@
1
+ import type { AwsCredentialIdentityProvider } from "@smithy/types";
2
+ export interface CustomCredentialChainOptions {
3
+ expireAfter(milliseconds: number): AwsCredentialIdentityProvider & CustomCredentialChainOptions;
4
+ }
5
+ /**
6
+ * @example
7
+ * ```js
8
+ * import { fromEnv, fromIni, createCredentialChain } from '@aws-sdk/credential-providers';
9
+ * import { S3 } from '@aws-sdk/client-s3';
10
+ *
11
+ * // You can mix existing AWS SDK credential providers
12
+ * // and custom async functions returning credential objects.
13
+ * new S3({
14
+ * credentials: createCredentialChain(
15
+ * fromEnv(),
16
+ * async () => {
17
+ * // credentials customized by your code...
18
+ * return credentials;
19
+ * },
20
+ * fromIni()
21
+ * ),
22
+ * });
23
+ *
24
+ * // Set a max duration on the credentials (client side only).
25
+ * // A set expiration will cause the credentials function to be called again
26
+ * // when the time left is less than 5 minutes.
27
+ * new S3({
28
+ * // expire after 15 minutes (in milliseconds).
29
+ * credentials: createCredentialChain(fromEnv(), fromIni()).expireAfter(15 * 60_000),
30
+ * });
31
+ *
32
+ * // Apply shared init properties.
33
+ * const init = { logger: console };
34
+ *
35
+ * new S3({
36
+ * credentials: createCredentialChain(fromEnv(init), fromIni(init)),
37
+ * });
38
+ * ```
39
+ *
40
+ * @param credentialProviders - one or more credential providers.
41
+ * @returns a single AwsCredentialIdentityProvider that calls the given
42
+ * providers in sequence until one succeeds or all fail.
43
+ */
44
+ export declare const createCredentialChain: (...credentialProviders: AwsCredentialIdentityProvider[]) => AwsCredentialIdentityProvider & CustomCredentialChainOptions;
@@ -1,8 +1,9 @@
1
+ export * from "./createCredentialChain";
1
2
  export * from "./fromCognitoIdentity";
2
3
  export * from "./fromCognitoIdentityPool";
3
4
  export * from "./fromContainerMetadata";
4
- export * from "./fromEnv";
5
5
  export { fromHttp, FromHttpOptions, HttpProviderCredentials } from "@aws-sdk/credential-provider-http";
6
+ export * from "./fromEnv";
6
7
  export * from "./fromIni";
7
8
  export * from "./fromInstanceMetadata";
8
9
  export * from "./fromNodeProviderChain";
@@ -0,0 +1,9 @@
1
+ import { AwsCredentialIdentityProvider } from "@smithy/types";
2
+ export interface CustomCredentialChainOptions {
3
+ expireAfter(
4
+ milliseconds: number
5
+ ): AwsCredentialIdentityProvider & CustomCredentialChainOptions;
6
+ }
7
+ export declare const createCredentialChain: (
8
+ ...credentialProviders: AwsCredentialIdentityProvider[]
9
+ ) => AwsCredentialIdentityProvider & CustomCredentialChainOptions;
@@ -1,12 +1,13 @@
1
+ export * from "./createCredentialChain";
1
2
  export * from "./fromCognitoIdentity";
2
3
  export * from "./fromCognitoIdentityPool";
3
4
  export * from "./fromContainerMetadata";
4
- export * from "./fromEnv";
5
5
  export {
6
6
  fromHttp,
7
7
  FromHttpOptions,
8
8
  HttpProviderCredentials,
9
9
  } from "@aws-sdk/credential-provider-http";
10
+ export * from "./fromEnv";
10
11
  export * from "./fromIni";
11
12
  export * from "./fromInstanceMetadata";
12
13
  export * from "./fromNodeProviderChain";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aws-sdk/credential-providers",
3
- "version": "3.624.0",
3
+ "version": "3.630.0",
4
4
  "description": "A collection of credential providers, without requiring service clients like STS, Cognito",
5
5
  "main": "./dist-cjs/index.js",
6
6
  "module": "./dist-es/index.js",
@@ -29,16 +29,16 @@
29
29
  },
30
30
  "license": "Apache-2.0",
31
31
  "dependencies": {
32
- "@aws-sdk/client-cognito-identity": "3.624.0",
33
- "@aws-sdk/client-sso": "3.624.0",
34
- "@aws-sdk/client-sts": "3.624.0",
35
- "@aws-sdk/credential-provider-cognito-identity": "3.624.0",
32
+ "@aws-sdk/client-cognito-identity": "3.629.0",
33
+ "@aws-sdk/client-sso": "3.629.0",
34
+ "@aws-sdk/client-sts": "3.629.0",
35
+ "@aws-sdk/credential-provider-cognito-identity": "3.629.0",
36
36
  "@aws-sdk/credential-provider-env": "3.620.1",
37
37
  "@aws-sdk/credential-provider-http": "3.622.0",
38
- "@aws-sdk/credential-provider-ini": "3.624.0",
39
- "@aws-sdk/credential-provider-node": "3.624.0",
38
+ "@aws-sdk/credential-provider-ini": "3.629.0",
39
+ "@aws-sdk/credential-provider-node": "3.629.0",
40
40
  "@aws-sdk/credential-provider-process": "3.620.1",
41
- "@aws-sdk/credential-provider-sso": "3.624.0",
41
+ "@aws-sdk/credential-provider-sso": "3.629.0",
42
42
  "@aws-sdk/credential-provider-web-identity": "3.621.0",
43
43
  "@aws-sdk/types": "3.609.0",
44
44
  "@smithy/credential-provider-imds": "^3.2.0",