-
Notifications
You must be signed in to change notification settings - Fork 85
feat: create credential providers before synthesizing a deploy #2123
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
notgitika
wants to merge
6
commits into
refactor
Choose a base branch
from
feat/deploy-credential-providers
base: refactor
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
6 commits
Select commit
Hold shift + click to select a range
3c09c23
feat: create credential providers before synthesizing a deploy
a5cd14b
test+fix: cover identity client, clear dropped credentials, honest en…
2c47266
test: cover the identity factory's found + secret-ARN mapping branches
0e76e9f
fix: use a valid Oauth2ProviderConfigInput in the no-secret-ARN test
10672be
fix: address review — env-key collisions, prereq order, no partial pr…
903abbc
test+fix: keep collision message wording, cover cross-type collision,…
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
192 changes: 192 additions & 0 deletions
192
src/core/project/backends/cdk/credentials.client.test.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,192 @@ | ||
| import { afterEach, describe, expect, mock, test } from "bun:test"; | ||
|
|
||
| // credentials.test.ts drives the provisioner with a fake client; this covers the | ||
| // real factory by mocking the AWS SDK it lazily imports. | ||
|
|
||
| class ResourceNotFoundException extends Error { | ||
| constructor() { | ||
| super("not found"); | ||
| this.name = "ResourceNotFoundException"; | ||
| } | ||
| } | ||
| class GetApiKeyCredentialProviderCommand { | ||
| constructor(readonly input: unknown) {} | ||
| } | ||
| class CreateApiKeyCredentialProviderCommand { | ||
| constructor(readonly input: unknown) {} | ||
| } | ||
| class GetOauth2CredentialProviderCommand { | ||
| constructor(readonly input: unknown) {} | ||
| } | ||
| class CreateOauth2CredentialProviderCommand { | ||
| constructor(readonly input: unknown) {} | ||
| } | ||
|
|
||
| const sent: unknown[] = []; | ||
| let send: (command: unknown) => Promise<unknown>; | ||
|
|
||
| class BedrockAgentCoreControlClient { | ||
| constructor(readonly config: unknown) {} | ||
| send(command: unknown) { | ||
| sent.push(command); | ||
| return send(command); | ||
| } | ||
| } | ||
|
|
||
| mock.module("@aws-sdk/client-bedrock-agentcore-control", () => ({ | ||
| BedrockAgentCoreControlClient, | ||
| GetApiKeyCredentialProviderCommand, | ||
| CreateApiKeyCredentialProviderCommand, | ||
| GetOauth2CredentialProviderCommand, | ||
| CreateOauth2CredentialProviderCommand, | ||
| ResourceNotFoundException, | ||
| })); | ||
|
|
||
| const { createIdentityProviderClient } = await import("./credentials"); | ||
| const credentials = async () => ({ accessKeyId: "a", secretAccessKey: "b" }); | ||
|
|
||
| afterEach(() => { | ||
| sent.length = 0; | ||
| }); | ||
|
|
||
| describe("createIdentityProviderClient", () => { | ||
| test("passes region and credentials to the SDK client", async () => { | ||
| send = async () => ({ credentialProviderArn: "arn:cp" }); | ||
| const client = await createIdentityProviderClient("eu-west-1", credentials); | ||
| await client.getApiKeyProvider("k"); | ||
|
|
||
| expect((sent[0] as GetApiKeyCredentialProviderCommand).input).toEqual({ name: "k" }); | ||
| }); | ||
|
|
||
| test("maps an API key provider, including its secret ARN", async () => { | ||
| send = async () => ({ | ||
| credentialProviderArn: "arn:cp", | ||
| apiKeySecretArn: { secretArn: "arn:secret" }, | ||
| }); | ||
| const client = await createIdentityProviderClient("us-east-1", credentials); | ||
|
|
||
| expect(await client.getApiKeyProvider("k")).toEqual({ | ||
| credentialProviderArn: "arn:cp", | ||
| clientSecretArn: "arn:secret", | ||
| }); | ||
| }); | ||
|
|
||
| test("maps an OAuth2 provider it finds, including its secret ARN", async () => { | ||
| send = async () => ({ | ||
| credentialProviderArn: "arn:cp", | ||
| clientSecretArn: { secretArn: "arn:secret" }, | ||
| }); | ||
| const client = await createIdentityProviderClient("us-east-1", credentials); | ||
|
|
||
| expect(await client.getOauth2Provider("o")).toEqual({ | ||
| credentialProviderArn: "arn:cp", | ||
| clientSecretArn: "arn:secret", | ||
| }); | ||
| }); | ||
|
|
||
| test("omits the secret ARN when Identity returns none", async () => { | ||
| send = async () => ({ credentialProviderArn: "arn:cp" }); | ||
| const client = await createIdentityProviderClient("us-east-1", credentials); | ||
|
|
||
| expect(await client.getApiKeyProvider("k")).toEqual({ credentialProviderArn: "arn:cp" }); | ||
| }); | ||
|
|
||
| test("returns undefined when the provider does not exist", async () => { | ||
| send = async () => { | ||
| throw new ResourceNotFoundException(); | ||
| }; | ||
| const client = await createIdentityProviderClient("us-east-1", credentials); | ||
|
|
||
| expect(await client.getApiKeyProvider("missing")).toBeUndefined(); | ||
| expect(await client.getOauth2Provider("missing")).toBeUndefined(); | ||
| }); | ||
|
|
||
| test("propagates errors other than not-found", async () => { | ||
| const failure = Object.assign(new Error("denied"), { name: "AccessDeniedException" }); | ||
| send = async () => { | ||
| throw failure; | ||
| }; | ||
| const client = await createIdentityProviderClient("us-east-1", credentials); | ||
|
|
||
| await expect(client.getApiKeyProvider("k")).rejects.toBe(failure); | ||
| }); | ||
|
|
||
| test("throws when Identity returns no provider ARN", async () => { | ||
| send = async () => ({}); | ||
| const client = await createIdentityProviderClient("us-east-1", credentials); | ||
|
|
||
| await expect(client.createApiKeyProvider({ name: "k", apiKey: "sk" })).rejects.toThrow( | ||
| /no credentialProviderArn/, | ||
| ); | ||
| }); | ||
|
|
||
| test("creates an API key provider from an inline key", async () => { | ||
| send = async () => ({ credentialProviderArn: "arn:cp" }); | ||
| const client = await createIdentityProviderClient("us-east-1", credentials); | ||
|
|
||
| await client.createApiKeyProvider({ name: "k", apiKey: "sk-live" }); | ||
|
|
||
| expect((sent[0] as CreateApiKeyCredentialProviderCommand).input).toEqual({ | ||
| name: "k", | ||
| apiKey: "sk-live", | ||
| }); | ||
| }); | ||
|
|
||
| test("creates an API key provider from an external secret reference", async () => { | ||
| send = async () => ({ credentialProviderArn: "arn:cp" }); | ||
| const client = await createIdentityProviderClient("us-east-1", credentials); | ||
|
|
||
| const secretRef = { secretId: "s", jsonKey: "apiKey" }; | ||
| await client.createApiKeyProvider({ name: "k", secretRef }); | ||
|
|
||
| expect((sent[0] as CreateApiKeyCredentialProviderCommand).input).toEqual({ | ||
| name: "k", | ||
| apiKeySecretConfig: secretRef, | ||
| apiKeySecretSource: "EXTERNAL", | ||
| }); | ||
| }); | ||
|
|
||
| test("returns the created API key provider's secret ARN", async () => { | ||
| send = async () => ({ | ||
| credentialProviderArn: "arn:cp", | ||
| apiKeySecretArn: { secretArn: "arn:secret" }, | ||
| }); | ||
| const client = await createIdentityProviderClient("us-east-1", credentials); | ||
|
|
||
| expect(await client.createApiKeyProvider({ name: "k", apiKey: "sk" })).toEqual({ | ||
| credentialProviderArn: "arn:cp", | ||
| clientSecretArn: "arn:secret", | ||
| }); | ||
| }); | ||
|
|
||
| test("creates an OAuth2 provider without a returned secret ARN", async () => { | ||
| send = async () => ({ credentialProviderArn: "arn:cp" }); | ||
| const client = await createIdentityProviderClient("us-east-1", credentials); | ||
|
|
||
| expect( | ||
| await client.createOauth2Provider({ | ||
| name: "o", | ||
| vendor: "CustomOauth2", | ||
| config: { customOauth2ProviderConfig: { oauthDiscovery: { discoveryUrl: "u" } } }, | ||
| }), | ||
| ).toEqual({ credentialProviderArn: "arn:cp" }); | ||
| }); | ||
|
|
||
| test("creates an OAuth2 provider with its vendor and config", async () => { | ||
| send = async () => ({ | ||
| credentialProviderArn: "arn:cp", | ||
| clientSecretArn: { secretArn: "arn:secret" }, | ||
| }); | ||
| const client = await createIdentityProviderClient("us-east-1", credentials); | ||
|
|
||
| const config = { customOauth2ProviderConfig: { oauthDiscovery: { discoveryUrl: "u" } } }; | ||
| const result = await client.createOauth2Provider({ name: "o", vendor: "CustomOauth2", config }); | ||
|
|
||
| expect((sent[0] as CreateOauth2CredentialProviderCommand).input).toEqual({ | ||
| name: "o", | ||
| credentialProviderVendor: "CustomOauth2", | ||
| oauth2ProviderConfigInput: config, | ||
| }); | ||
| expect(result).toEqual({ credentialProviderArn: "arn:cp", clientSecretArn: "arn:secret" }); | ||
| }); | ||
| }); |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could the local CDK prerequisite checks stay ahead of credential provisioning? I ran this with
agentcore/cdk/node_modulesmissing: the provisioner ran anddeployed-state.jsonwas written, then deploy failed with thenpm installguidance. Synthesis still has to happen after provisioning, but checkingnpmand dependencies separately first would avoid mutating AWS for a local setup error. Probably just good to have like for ux but not a blocker.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed, a local setup error shouldn't mutate AWS. I'll hoist the npm/node_modules prerequisite check ahead of credential provisioning so we fail fast before creating anything. Synth still runs after provisioning, but the local-only checks don't need to.