Skip to content

Commit a0ea3c6

Browse files
committed
docs: use clientConfig to override all creds providers client config
1 parent 719a49b commit a0ea3c6

16 files changed

+101
-69
lines changed

UPGRADING.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ Load credentials from Cognito Identity service, normally used in browsers.
270270
const client = new FooClient({
271271
region: "us-east-1",
272272
credentials: fromCognitoIdentityPool({
273-
client: cognitoIdentityClient // Optional
273+
clientConfig: cognitoIdentityClientConfig // Optional
274274
identityPoolId: "us-east-1:1699ebc0-7900-4099-b910-2df94f52a030",
275275
customRoleArn: "arn:aws:iam::1234567890:role/MYAPP-CognitoIdentity", // Optional
276276
logins: {
@@ -291,7 +291,7 @@ Load credentials from Cognito Identity service, normally used in browsers.
291291
const client = new FooClient({
292292
region: "us-east-1",
293293
credentials: fromCognitoIdentity({
294-
client: cognitoIdentityClient, // Optional
294+
clientConfig: cognitoIdentityClientConfig, // Optional
295295
identityId: "us-east-1:128d0a74-c82f-4553-916d-90053e4a8b0f",
296296
customRoleArn: "arn:aws:iam::1234567890:role/MYAPP-CognitoIdentity", // Optional
297297
logins: {
@@ -383,7 +383,7 @@ for more information.
383383
return "some_code";
384384
}, // Optional
385385
profile: "default", // Optional
386-
stsConfig: { region }, // Optional
386+
clientConfig: { region }, // Optional
387387
}),
388388
});
389389
```
@@ -396,14 +396,14 @@ Retrieves credentials using OIDC token from a file on disk. It's commonly used i
396396
- **v3**: [`fromTokenFile`](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/modules/_aws_sdk_credential_provider_web_identity.html#fromtokenfile-1)
397397

398398
```javascript
399-
import { fromTokenFile } from "@aws-sdk/credential-provider-web-identity"; // ES6 import
400-
// const { fromIni } from("@aws-sdk/credential-provider-ini"); // CommonJS import
399+
import { fromTokenFile } from "@aws-sdk/credential-providers"; // ES6 import
400+
// const { fromIni } from("@aws-sdk/credential-providers"); // CommonJS import
401401

402402
const client = new FooClient({
403403
credentials: fromTokenFile({
404404
roleArn: "arn:xxxx" // Optional. Otherwise read from `AWS_ROLE_ARN` environmental variable
405405
roleSessionName: "session:a", // Optional. Otherwise read from `AWS_ROLE_SESSION_NAME` environmental variable
406-
stsConfig: { region } // // Optional. STS client config to make the assume role request.
406+
clientConfig: { region } // // Optional. STS client config to make the assume role request.
407407
})
408408
});
409409
```
@@ -416,14 +416,14 @@ Retrieves credentials from STS web identity federation support.
416416
- **v3**: [`fromWebToken`](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/modules/_aws_sdk_credential_provider_web_identity.html#fromwebtoken-1)
417417

418418
```javascript
419-
import { fromWebToken } from "@aws-sdk/credential-provider-web-identity"; // ES6 import
420-
// const { fromWebToken } from("@aws-sdk/credential-provider-web-identity"); // CommonJS import
419+
import { fromWebToken } from "@aws-sdk/credential-providers"; // ES6 import
420+
// const { fromWebToken } from("@aws-sdk/credential-providers"); // CommonJS import
421421

422422
const client = new FooClient({
423423
credentials: fromWebToken({
424424
roleArn: "arn:xxxx" // Otherwise read from `AWS_ROLE_ARN` environmental variable
425425
roleSessionName: "session:a", // Otherwise read from `AWS_ROLE_SESSION_NAME` environmental variable
426-
stsConfig: { region } // // Optional. STS client config to make the assume role request.
426+
clientConfig: { region } // // Optional. STS client config to make the assume role request.
427427
})
428428
});
429429
```

packages/credential-providers/README.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ const dynamodb = new DynamoDBClient({
5454
"api.twitter.com": "TWITTERTOKEN'",
5555
"www.digits.com": "DIGITSTOKEN"
5656
},
57-
// Optional. Custom client if you need overwrite default client configuration
58-
client: new CognitoIdentityClient({ region })
57+
// Optional. Custom client config if you need overwrite default Cognito Identity client configuration.
58+
clientConfig: { region }
5959
}),
6060
});
6161
```
@@ -95,8 +95,8 @@ const dynamodb = new DynamoDBClient({
9595
'api.twitter.com': 'TWITTERTOKEN',
9696
'www.digits.com': 'DIGITSTOKEN'
9797
},
98-
// Optional. Custom client if you need overwrite default client configuration
99-
client: new CognitoIdentityClient({ region })
98+
// Optional. Custom client config if you need overwrite default Cognito Identity client configuration.
99+
clientConfig: { region }
100100
}),
101101
});
102102
```
@@ -130,7 +130,7 @@ const dynamodb = new DynamoDBClient({
130130
//... For more options see https://docs.aws.amazon.com/STS/latest/APIReference/API_AssumeRole.html
131131
},
132132
// Optional. Custom STS client configurations overriding the default ones.
133-
stsConfig: { region },
133+
clientConfig: { region },
134134
// Optional. A function that returns a promise fulfilled with an MFA token code for the provided MFA Serial code.
135135
// Required if `params` has `SerialNumber` config.
136136
mfaCodeProvider: async mfaSerial => {
@@ -157,7 +157,7 @@ const dynamodb = new DynamoDBClient({
157157
// Required. The OAuth 2.0 access token or OpenID Connect ID token that is provided by the identity provider.
158158
webIdentityToken: await openIdProvider()
159159
// Optional. Custom STS client configurations overriding the default ones.
160-
stsConfig: { region }
160+
clientConfig: { region }
161161
// Optional. A function that assumes a role with web identity and returns a promise fulfilled with credentials for
162162
// the assumed role.
163163
roleAssumerWithWebIdentity,
@@ -278,7 +278,7 @@ const client = new DynamoDBClient({
278278
return "token";
279279
},
280280
// Optional. Custom STS client configurations overriding the default ones.
281-
stsConfig: { region },
281+
clientConfig: { region },
282282
}),
283283
});
284284
```
@@ -473,7 +473,7 @@ import { fromTokenFile } from "@aws-sdk/credential-providers"; // ES6 example
473473
const client = new DynamoDBClient({
474474
credentials: fromTokenFile({
475475
// Optional. STS client config to make the assume role request.
476-
stsConfig: { region }
476+
clientConfig: { region }
477477
});
478478
});
479479
```
@@ -527,9 +527,9 @@ const client = new DynamoDBClient({
527527
// Optional. The name of the AWS role to assume. Required if any of the `sso*` options(except for `ssoClient`) is
528528
// provided.
529529
ssoRoleName: "SampleRole",
530-
// Optional. The SSO Client used to request AWS credentials with the SSO access token. If not specified, a default
530+
// Optional. Overwrite the configuration used construct the SSO service client. If not specified, a default
531531
// SSO client will be created with the region specified in the profile `sso_region` entry.
532-
ssoClient,
532+
clientConfig: { region },
533533
}),
534534
});
535535
```

packages/credential-providers/src/fromCognitoIdentity.spec.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@ describe("fromCognitoIdentity", () => {
2929
expect(CognitoIdentityClient).toBeCalled();
3030
});
3131

32-
it("should use client if supplied", () => {
33-
const client = "CLIENT" as any;
32+
it("should use client config if supplied", () => {
33+
const clientConfig = "CLIENT" as any;
3434
fromCognitoIdentity({
3535
identityId,
36-
client: client,
36+
clientConfig,
3737
});
3838
expect((coreProvider as jest.Mock).mock.calls[0][0]?.identityId).toBe(identityId);
39-
expect((coreProvider as jest.Mock).mock.calls[0][0]?.client).toBe(client);
40-
expect(CognitoIdentityClient).not.toBeCalled();
39+
expect((coreProvider as jest.Mock).mock.calls[0][0]?.client).toBeInstanceOf(CognitoIdentityClient);
40+
expect(CognitoIdentityClient).toBeCalledWith(clientConfig);
4141
});
4242
});

packages/credential-providers/src/fromCognitoIdentity.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { CognitoIdentityClient } from "@aws-sdk/client-cognito-identity";
1+
import { CognitoIdentityClient, CognitoIdentityClientConfig } from "@aws-sdk/client-cognito-identity";
22
import {
33
CognitoIdentityCredentialProvider as _CognitoIdentityCredentialProvider,
44
fromCognitoIdentity as _fromCognitoIdentity,
@@ -7,9 +7,9 @@ import {
77

88
export interface FromCognitoIdentityParameters extends Omit<_FromCognitoIdentityParameters, "client"> {
99
/**
10-
* Custom client if you need overwrite default client configuration
10+
* Custom client configuration if you need overwrite default Cognito Identity client configuration.
1111
*/
12-
client?: CognitoIdentityClient;
12+
clientConfig?: CognitoIdentityClientConfig;
1313
}
1414

1515
export type CognitoIdentityCredentialProvider = _CognitoIdentityCredentialProvider;
@@ -43,14 +43,14 @@ export type CognitoIdentityCredentialProvider = _CognitoIdentityCredentialProvid
4343
* "api.twitter.com": "TWITTERTOKEN'",
4444
* "www.digits.com": "DIGITSTOKEN"
4545
* },
46-
* // Optional. Custom client if you need overwrite default client configuration
47-
* client: new CognitoIdentityClient({ region })
46+
* // Optional. Custom client configuration if you need overwrite default Cognito Identity client configuration.
47+
* clientConfig: { region }
4848
* }),
4949
* });
5050
* ```
5151
*/
5252
export const fromCognitoIdentity = (options: FromCognitoIdentityParameters): CognitoIdentityCredentialProvider =>
5353
_fromCognitoIdentity({
5454
...options,
55-
client: options.client ?? new CognitoIdentityClient({}),
55+
client: new CognitoIdentityClient(options.clientConfig ?? {}),
5656
});

packages/credential-providers/src/fromCognitoIdentityPool.spec.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,17 @@ describe("fromCognitoIdentityPool", () => {
3131
);
3232
expect((coreProvider as jest.Mock).mock.calls[0][0]?.identityPoolId).toBe(identityPoolId);
3333
expect((coreProvider as jest.Mock).mock.calls[0][0]?.client).toBeInstanceOf(CognitoIdentityClient);
34-
expect(CognitoIdentityClient).toBeCalled();
34+
expect(CognitoIdentityClient).toBeCalledWith({});
3535
});
3636

37-
it("should use client if supplied", () => {
38-
const client = "CLIENT" as any;
37+
it("should use client config if supplied", () => {
38+
const clientConfig = "CLIENT" as any;
3939
fromCognitoIdentityPool({
4040
identityPoolId,
41-
client: client,
41+
clientConfig,
4242
});
4343
expect((coreProvider as jest.Mock).mock.calls[0][0]?.identityPoolId).toBe(identityPoolId);
44-
expect((coreProvider as jest.Mock).mock.calls[0][0]?.client).toBe(client);
45-
expect(CognitoIdentityClient).not.toBeCalled();
44+
expect((coreProvider as jest.Mock).mock.calls[0][0]?.client).toBeInstanceOf(CognitoIdentityClient);
45+
expect(CognitoIdentityClient).toBeCalledWith(clientConfig);
4646
});
4747
});

packages/credential-providers/src/fromCognitoIdentityPool.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import { CognitoIdentityClient } from "@aws-sdk/client-cognito-identity";
1+
import { CognitoIdentityClient, CognitoIdentityClientConfig } from "@aws-sdk/client-cognito-identity";
22
import {
33
CognitoIdentityCredentialProvider,
44
fromCognitoIdentityPool as _fromCognitoIdentityPool,
55
FromCognitoIdentityPoolParameters as _FromCognitoIdentityPoolParameters,
66
} from "@aws-sdk/credential-provider-cognito-identity";
77

88
export interface FromCognitoIdentityPoolParameters extends Omit<_FromCognitoIdentityPoolParameters, "client"> {
9-
client?: CognitoIdentityClient;
9+
clientConfig?: CognitoIdentityClientConfig;
1010
}
1111

1212
/**
@@ -43,7 +43,7 @@ export interface FromCognitoIdentityPoolParameters extends Omit<_FromCognitoIden
4343
* 'api.twitter.com': 'TWITTERTOKEN',
4444
* 'www.digits.com': 'DIGITSTOKEN'
4545
* },
46-
* // Optional. Custom client if you need overwrite default client configuration
46+
* // Optional. Custom client configuration if you need overwrite default Cognito Identity client configuration.
4747
* client: new CognitoIdentityClient({ region })
4848
* }),
4949
* });
@@ -54,5 +54,5 @@ export const fromCognitoIdentityPool = (
5454
): CognitoIdentityCredentialProvider =>
5555
_fromCognitoIdentityPool({
5656
...options,
57-
client: options.client ?? new CognitoIdentityClient({}),
57+
client: new CognitoIdentityClient(options.clientConfig ?? {}),
5858
});

packages/credential-providers/src/fromIni.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,11 @@ describe("fromIni", () => {
4848

4949
it("should use supplied sts options", () => {
5050
const profile = "profile";
51-
const stsConfig = {
51+
const clientConfig = {
5252
region: "US_BAR_1",
5353
};
54-
fromIni({ profile, stsConfig });
55-
expect(getDefaultRoleAssumer).toBeCalledWith(stsConfig);
56-
expect(getDefaultRoleAssumerWithWebIdentity).toBeCalledWith(stsConfig);
54+
fromIni({ profile, clientConfig });
55+
expect(getDefaultRoleAssumer).toBeCalledWith(clientConfig);
56+
expect(getDefaultRoleAssumerWithWebIdentity).toBeCalledWith(clientConfig);
5757
});
5858
});

packages/credential-providers/src/fromIni.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { fromIni as _fromIni, FromIniInit as _FromIniInit } from "@aws-sdk/crede
33
import { CredentialProvider } from "@aws-sdk/types";
44

55
export interface FromIniInit extends _FromIniInit {
6-
stsConfig?: STSClientConfig;
6+
clientConfig?: STSClientConfig;
77
}
88

99
/**
@@ -37,14 +37,15 @@ export interface FromIniInit extends _FromIniInit {
3737
* return "token";
3838
* },
3939
* // Optional. Custom STS client configurations overriding the default ones.
40-
* stsConfig: { region },
40+
* clientConfig: { region },
4141
* }),
4242
* });
4343
* ```
4444
*/
4545
export const fromIni = (init: FromIniInit = {}): CredentialProvider =>
4646
_fromIni({
4747
...init,
48-
roleAssumer: init.roleAssumer ?? getDefaultRoleAssumer(init.stsConfig),
49-
roleAssumerWithWebIdentity: init.roleAssumerWithWebIdentity ?? getDefaultRoleAssumerWithWebIdentity(init.stsConfig),
48+
roleAssumer: init.roleAssumer ?? getDefaultRoleAssumer(init.clientConfig),
49+
roleAssumerWithWebIdentity:
50+
init.roleAssumerWithWebIdentity ?? getDefaultRoleAssumerWithWebIdentity(init.clientConfig),
5051
});
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { SSOClient } from "@aws-sdk/client-sso";
2+
3+
import { fromSSO } from "./fromSSO";
4+
5+
jest.mock("@aws-sdk/client-sso", () => ({
6+
SSOClient: jest.fn().mockImplementation(function () {
7+
return "SSO_CLIENT";
8+
}),
9+
}));
10+
11+
describe("fromSSO", () => {
12+
beforeEach(() => {
13+
jest.clearAllMocks();
14+
});
15+
16+
it("should not inject SSO client if no client config supplied", async () => {
17+
fromSSO();
18+
expect(SSOClient as jest.Mock).not.toBeCalled();
19+
});
20+
21+
it("should inject SSO client if client config is supplied", async () => {
22+
const region = "us-foo-1";
23+
fromSSO({ clientConfig: { region } });
24+
expect(SSOClient as jest.Mock).toBeCalledWith({ region });
25+
});
26+
});

packages/credential-providers/src/fromSSO.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
import { SSOClient, SSOClientConfig } from "@aws-sdk/client-sso";
12
import { fromSSO as _fromSSO, FromSSOInit as _FromSSOInit } from "@aws-sdk/credential-provider-sso";
23
import { CredentialProvider } from "@aws-sdk/types";
34

4-
export interface FromSSOInit extends _FromSSOInit {}
5+
export interface FromSSOInit extends Omit<_FromSSOInit, "client"> {
6+
clientConfig?: SSOClientConfig;
7+
}
58

69
/**
710
* Creates a credential provider function that reads from the _resolved_ access token from local disk then requests
@@ -38,11 +41,11 @@ export interface FromSSOInit extends _FromSSOInit {}
3841
* // Optional. The name of the AWS role to assume. Required if any of the `sso*` options(except for `ssoClient`) is
3942
* // provided.
4043
* ssoRoleName: "SampleRole",
41-
* // Optional. The SSO Client used to request AWS credentials with the SSO access token. If not specified, a default
42-
* // SSO client will be created with the region specified in the profile `sso_region` entry.
43-
* ssoClient,
44+
* // Optional. Overwrite the configuration used construct the SSO service client.
45+
* clientConfig: { region },
4446
* }),
4547
* });
4648
* ```
4749
*/
48-
export const fromSSO = (init?: FromSSOInit): CredentialProvider => _fromSSO(init);
50+
export const fromSSO = (init: FromSSOInit = {}): CredentialProvider =>
51+
_fromSSO({ ...{ ssoClient: init.clientConfig ? new SSOClient(init.clientConfig) : undefined }, ...init });

0 commit comments

Comments
 (0)