-
Notifications
You must be signed in to change notification settings - Fork 615
feat(credential-providers): Add a cli v2 compatible credential provider #6863
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
smilkuri
wants to merge
21
commits into
aws:main
Choose a base branch
from
smilkuri:cliCredentialProvider
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
d0b6fa3
cliv2 compatible provider
smilkuri 0e94953
resolving comments
smilkuri 8ed9ab7
removed unrealated files
smilkuri ecc1845
undo the changes in clients/
smilkuri ee72b0c
Delete unwanted file from PR
smilkuri 9bc85a7
resolve comments
smilkuri b7cd141
revision 4
smilkuri ac46c94
add console.warn for IMDS region resolution
smilkuri 7fe4c51
add tests
smilkuri 97b4825
modifying test to ensure credential chain resolves in intended order
smilkuri dcb3939
feat(credential-providers): fix the failing tests for cliv2Compatible…
smilkuri b874052
chore(credential-providers): add the imports correctly
smilkuri 3eca517
feat(credential-providers): adding yarn file
smilkuri 775f11b
feat(credetial-providers): remove cyclic dependecies
smilkuri d8c82fd
feat(credential-providers): install latest versions
smilkuri e6f1a31
feat(credential-providers): remove unnecessary import
smilkuri e49a0f1
feat(credential-providers): update the cyclic dependency
smilkuri 4e1950f
feat(credential-providers): fix staging tests
smilkuri 4012c92
feat(credential-providers): fix ci test failures
smilkuri 46383e8
feat(credential-providers): update readme
smilkuri 8e5bae6
feat: defer to node chain in cliv2compat
kuhe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
86 changes: 86 additions & 0 deletions
86
packages/credential-providers/src/fromAwsCliV2CompatibleProviderChain.spec.ts
This file contains hidden or 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,86 @@ | ||
import type { Exact } from "@smithy/types"; | ||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; | ||
|
||
import { | ||
AwsCliV2CompatibleProviderOptions, | ||
fromAwsCliV2CompatibleProviderChain, | ||
} from "./fromAwsCliV2CompatibleProviderChain"; | ||
|
||
// the options type should have no required fields. | ||
type Assert = Exact<AwsCliV2CompatibleProviderOptions, Partial<AwsCliV2CompatibleProviderOptions>>; | ||
const typeAssertion: Assert = true as const; | ||
void typeAssertion; | ||
|
||
describe("fromAwsCliV2CompatibleProviderChain", () => { | ||
let mockFromIni: any; | ||
let mockFromNodeProviderChain: any; | ||
|
||
const mockLogger = { | ||
debug: vi.fn(), | ||
info: vi.fn(), | ||
warn: vi.fn(), | ||
error: vi.fn(), | ||
}; | ||
|
||
beforeEach(() => { | ||
vi.clearAllMocks(); | ||
vi.resetModules(); | ||
|
||
mockFromIni = vi.fn(() => | ||
vi.fn(async () => ({ | ||
accessKeyId: "PROFILE_ACCESS_KEY", | ||
secretAccessKey: "PROFILE_SECRET_KEY", | ||
})) | ||
); | ||
vi.doMock("@aws-sdk/credential-provider-ini", () => ({ | ||
fromIni: mockFromIni, | ||
})); | ||
|
||
mockFromNodeProviderChain = vi.fn(() => | ||
vi.fn(async () => ({ | ||
accessKeyId: "AWS_SDK_CHAIN_AK", | ||
secretAccessKey: "AWS_SDK_CHAIN_SK", | ||
})) | ||
); | ||
vi.doMock("@aws-sdk/credential-provider-node", () => ({ | ||
defaultProvider: mockFromNodeProviderChain, | ||
})); | ||
}); | ||
|
||
afterEach(() => { | ||
vi.restoreAllMocks(); | ||
}); | ||
|
||
it("should use profile credentials when profile is specified", async () => { | ||
const provider = fromAwsCliV2CompatibleProviderChain({ | ||
profile: "test-profile", | ||
logger: mockLogger, | ||
}); | ||
|
||
const result = await provider(); | ||
|
||
expect(result).toEqual({ | ||
accessKeyId: "PROFILE_ACCESS_KEY", | ||
secretAccessKey: "PROFILE_SECRET_KEY", | ||
}); | ||
|
||
expect(mockFromIni).toHaveBeenCalled(); | ||
expect(mockLogger.debug).toHaveBeenCalledWith( | ||
`@aws-sdk/credential-providers - fromAwsCliV2CompatibleProviderChain - Using fromIni with profile: test-profile` | ||
); | ||
}); | ||
|
||
it.only("should fall back to fromNodeProviderChain when no profile is specified", async () => { | ||
const provider = fromAwsCliV2CompatibleProviderChain({ | ||
logger: console, | ||
}); | ||
|
||
const result = await provider(); | ||
|
||
expect(result).toEqual({ | ||
accessKeyId: "AWS_SDK_CHAIN_AK", | ||
secretAccessKey: "AWS_SDK_CHAIN_SK", | ||
}); | ||
expect(mockFromNodeProviderChain).toHaveBeenCalled(); | ||
}); | ||
}); | ||
smilkuri marked this conversation as resolved.
Show resolved
Hide resolved
|
66 changes: 66 additions & 0 deletions
66
packages/credential-providers/src/fromAwsCliV2CompatibleProviderChain.ts
This file contains hidden or 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,66 @@ | ||
import type { DefaultProviderInit } from "@aws-sdk/credential-provider-node"; | ||
import type { RuntimeConfigAwsCredentialIdentityProvider } from "@aws-sdk/types"; | ||
import type { AwsCredentialIdentity } from "@smithy/types"; | ||
|
||
/** | ||
* @public | ||
*/ | ||
export type AwsCliV2CompatibleProviderOptions = Partial<AwsCredentialIdentity> & DefaultProviderInit; | ||
|
||
/** | ||
* Creates an alternate form of the AWS SDK for JavaScript's fromNodeProviderChain. | ||
* This differs in ways that makes it behave more like the AWS CLI v2: | ||
* 1. It allows inline static credentials. | ||
* 2. It checks AWS_DEFAULT_PROFILE in addition to AWS_PROFILE. | ||
* 3. It prioritizes fromIni if a profile is set. | ||
* Otherwise, it behaves as fromNodeProviderChain. | ||
* | ||
* @public | ||
* | ||
* @param _init - Configuration options for the provider chain. | ||
* @returns An AWS credential provider. | ||
*/ | ||
export const fromAwsCliV2CompatibleProviderChain = | ||
smilkuri marked this conversation as resolved.
Show resolved
Hide resolved
|
||
(_init: AwsCliV2CompatibleProviderOptions = {}): RuntimeConfigAwsCredentialIdentityProvider => | ||
smilkuri marked this conversation as resolved.
Show resolved
Hide resolved
|
||
async ({ callerClientConfig } = {}): Promise<AwsCredentialIdentity> => { | ||
// Merge init with caller's client config (profile/region). | ||
const init: AwsCliV2CompatibleProviderOptions = { | ||
..._init, | ||
profile: | ||
_init.profile ?? callerClientConfig?.profile ?? process.env.AWS_PROFILE ?? process.env.AWS_DEFAULT_PROFILE, | ||
logger: _init.logger ?? callerClientConfig?.logger, | ||
smilkuri marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}; | ||
const { profile, accessKeyId, secretAccessKey, sessionToken, expiration, accountId } = init; | ||
|
||
const debug = init.logger?.debug ?? (() => {}); | ||
|
||
debug("@aws-sdk/credential-providers - fromAwsCliV2CompatibleProviderChain - init"); | ||
|
||
// 1. If credentials are explicitly provided, return them. | ||
if (accessKeyId && secretAccessKey) { | ||
debug("@aws-sdk/credential-providers - fromAwsCliV2CompatibleProviderChain - static credentials from init"); | ||
return { | ||
accessKeyId, | ||
secretAccessKey, | ||
...(sessionToken && { sessionToken }), | ||
...(expiration && { expiration }), | ||
...(accountId && { accountId }), | ||
} as AwsCredentialIdentity; | ||
} | ||
|
||
// 2. If a profile is explicitly passed, use `fromIni`. | ||
if (profile) { | ||
debug( | ||
`@aws-sdk/credential-providers - fromAwsCliV2CompatibleProviderChain - Using fromIni with profile: ${profile}` | ||
); | ||
const { fromIni } = await import("@aws-sdk/credential-provider-ini"); | ||
return fromIni(init)({ callerClientConfig }); | ||
} | ||
|
||
// 3. Defer to AWS SDK credential chain. | ||
debug("@aws-sdk/credential-providers - fromAwsCliV2CompatibleProviderChain - defer to fromNodeProviderChain"); | ||
const { defaultProvider: fromNodeProviderChain } = await import("@aws-sdk/credential-provider-node"); | ||
return fromNodeProviderChain(init)({ | ||
// todo: fromNodeProviderChain should be changed to RuntimeConfigAwsCredentialIdentityProvider. | ||
}); | ||
}; |
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.