-
Notifications
You must be signed in to change notification settings - Fork 578
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(credential-providers): expose node.js default credential provide…
…r chain
- Loading branch information
1 parent
22870a0
commit c9f0f74
Showing
7 changed files
with
152 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
packages/credential-providers/src/fromNodeJsProviderChain.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
const ROLE_ASSUMER = "ROLE_ASSUMER"; | ||
const ROLE_ASSUMER_WITH_WEB_IDENTITY = "ROLE_ASSUMER_WITH_WEB_IDENTITY"; | ||
|
||
jest.mock("@aws-sdk/client-sts", () => ({ | ||
getDefaultRoleAssumer: jest.fn().mockReturnValue(ROLE_ASSUMER), | ||
getDefaultRoleAssumerWithWebIdentity: jest.fn().mockReturnValue(ROLE_ASSUMER_WITH_WEB_IDENTITY), | ||
})); | ||
|
||
import { getDefaultRoleAssumer, getDefaultRoleAssumerWithWebIdentity } from "@aws-sdk/client-sts"; | ||
import { defaultProvider } from "@aws-sdk/credential-provider-node"; | ||
|
||
import { fromNodeJsProviderChain } from "./fromNodeJsProviderChain"; | ||
|
||
jest.mock("@aws-sdk/credential-provider-node", () => ({ | ||
defaultProvider: jest.fn(), | ||
})); | ||
|
||
describe(fromNodeJsProviderChain.name, () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it("should inject default role assumers", () => { | ||
const profile = "profile"; | ||
fromNodeJsProviderChain({ profile }); | ||
expect(defaultProvider).toBeCalledWith({ | ||
profile, | ||
roleAssumer: ROLE_ASSUMER, | ||
roleAssumerWithWebIdentity: ROLE_ASSUMER_WITH_WEB_IDENTITY, | ||
}); | ||
expect(getDefaultRoleAssumer).toBeCalled(); | ||
expect(getDefaultRoleAssumerWithWebIdentity).toBeCalled(); | ||
}); | ||
|
||
it("should use supplied role assumers", () => { | ||
const profile = "profile"; | ||
const roleAssumer = jest.fn(); | ||
const roleAssumerWithWebIdentity = jest.fn(); | ||
fromNodeJsProviderChain({ profile, roleAssumer, roleAssumerWithWebIdentity }); | ||
expect(defaultProvider).toBeCalledWith({ | ||
profile, | ||
roleAssumer, | ||
roleAssumerWithWebIdentity, | ||
}); | ||
expect(getDefaultRoleAssumer).not.toBeCalled(); | ||
expect(getDefaultRoleAssumerWithWebIdentity).not.toBeCalled(); | ||
}); | ||
|
||
it("should use supplied sts options", () => { | ||
const profile = "profile"; | ||
const clientConfig = { | ||
region: "US_BAR_1", | ||
}; | ||
fromNodeJsProviderChain({ profile, clientConfig }); | ||
expect(getDefaultRoleAssumer).toBeCalledWith(clientConfig); | ||
expect(getDefaultRoleAssumerWithWebIdentity).toBeCalledWith(clientConfig); | ||
}); | ||
}); |
40 changes: 40 additions & 0 deletions
40
packages/credential-providers/src/fromNodeJsProviderChain.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { getDefaultRoleAssumer, getDefaultRoleAssumerWithWebIdentity, STSClientConfig } from "@aws-sdk/client-sts"; | ||
import { defaultProvider, DefaultProviderInit } from "@aws-sdk/credential-provider-node"; | ||
import { CredentialProvider } from "@aws-sdk/types"; | ||
|
||
export interface FromNodeJsProviderChainInit extends DefaultProviderInit { | ||
clientConfig?: STSClientConfig; | ||
} | ||
|
||
/** | ||
* This is the same credential provider as {@link defaultProvider|the default provider for Node.js SDK}, | ||
* but with default role assumers so you don't need to import them from | ||
* STS client and supply them manually. | ||
* | ||
* You normally don't need to use this explicitly in the client constructor. | ||
* It is useful for utility functions requiring credentials like S3 presigner, | ||
* or RDS signer. | ||
* | ||
* ```js | ||
* import { fromNodeJsProviderChain } from "@aws-sdk/credential-providers"; // ES6 import | ||
* // const { fromNodeJsProviderChain } = require "@aws-sdk/credential-providers" // CommonJS import | ||
* | ||
* const credentialProvider = fromNodeJsProviderChain({ | ||
* //...any input of fromEnv(), fromSSO(), fromTokenFile(), fromIni(), | ||
* // fromProcess(), fromInstanceMetadata(), fromContainerMetadata() | ||
* | ||
* // Optional. Custom STS client configurations overriding the default ones. | ||
* clientConfig: { region }, | ||
* }) | ||
* ``` | ||
* | ||
* @param init | ||
* @returns | ||
*/ | ||
export const fromNodeJsProviderChain = (init: FromNodeJsProviderChainInit = {}): CredentialProvider => | ||
defaultProvider({ | ||
...init, | ||
roleAssumer: init.roleAssumer ?? getDefaultRoleAssumer(init.clientConfig), | ||
roleAssumerWithWebIdentity: | ||
init.roleAssumerWithWebIdentity ?? getDefaultRoleAssumerWithWebIdentity(init.clientConfig), | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters