Skip to content

Commit 953439f

Browse files
author
gitikavj
committed
feat(compat): keep v0 credential provider names
v0 named Identity credential providers by the bare credential name, and the agent code it deployed looks them up by that name. For v0 projects, provision, update, record and remove providers under that bare name instead of the project/target-scoped v1 name, so a deploy never creates a second set of providers beside the ones in use. v0 providers can be shared between projects in an account, so teardown and orphan removal never delete a v0 project's API key or OAuth providers. Payment providers are still deleted, as v0's teardown did. v1 projects are unchanged.
1 parent c901a82 commit 953439f

2 files changed

Lines changed: 127 additions & 4 deletions

File tree

‎src/core/project/backends/cdk/credentials.test.ts‎

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import type { Project, ProjectEvent } from "../../../../handlers/project/types";
1212
import { ProjectSpecSchema } from "../../../../projectSchemas/project";
1313
import type { CoreOptions } from "../../../types";
1414
import { EnvLocalFile } from "../../envLocal";
15+
import { v0Fixture } from "../../legacy/fixtures-test-support";
16+
import { translateV0Spec } from "../../legacy/translate";
1517
import {
1618
createCredentialProvisioner,
1719
createCredentialRemover,
@@ -80,6 +82,15 @@ async function project(credentials: unknown[], envLocal?: string): Promise<Proje
8082
};
8183
}
8284

85+
/** The same project loaded from a v0 `agentcore.json`. */
86+
async function legacyProject(credentials: unknown[], envLocal?: string): Promise<Project> {
87+
const v0Spec = v0Fixture("baseline.json");
88+
return {
89+
...(await project(credentials, envLocal)),
90+
legacy: { format: "v0", v0Spec, extras: translateV0Spec(v0Spec).extras },
91+
};
92+
}
93+
8394
type ProviderKind = "apikey" | "oauth" | "payment";
8495

8596
/**
@@ -986,3 +997,99 @@ describe("createCredentialRemover", () => {
986997
expect(subject.contents()).toEqual({ [OPENAI_PROVIDER]: existing });
987998
});
988999
});
1000+
1001+
describe("v0 projects", () => {
1002+
// The v0 CLI named every provider by the bare credential name, and the agent code it
1003+
// deployed looks providers up by that name, so a v0 project keeps it.
1004+
const COINBASE_ENV =
1005+
"AGENTCORE_CREDENTIAL_WALLET_API_KEY_ID='key-1'\n" +
1006+
"AGENTCORE_CREDENTIAL_WALLET_API_KEY_SECRET='secret-1'\n" +
1007+
"AGENTCORE_CREDENTIAL_WALLET_WALLET_SECRET='wallet-1'\n";
1008+
1009+
test("creates providers under the bare credential name, recorded under the spec name", async () => {
1010+
const subject = account();
1011+
const input = await legacyProject(
1012+
[API_KEY, COINBASE],
1013+
"AGENTCORE_CREDENTIAL_OPENAI_KEY='sk-live'\n" + COINBASE_ENV,
1014+
);
1015+
1016+
const { events, result } = await run(subject.provision, input);
1017+
1018+
expect(stepMessages(events)).toEqual([
1019+
"Preparing credential provider 'openai-key'",
1020+
"Preparing credential provider 'wallet'",
1021+
]);
1022+
expect(Object.keys(subject.contents())).toEqual(["openai-key", "wallet"]);
1023+
expect(subject.configFor("openai-key")).toEqual({ name: "openai-key", apiKey: "sk-live" });
1024+
expect(result).toEqual({
1025+
"openai-key": recordedAs("ApiKeyCredentialProvider", {
1026+
credentialProviderArn: "arn:apikey:openai-key",
1027+
clientSecretArn: "arn:secret:openai-key",
1028+
}),
1029+
wallet: recordedAs("PaymentCredentialProvider", {
1030+
credentialProviderArn: "arn:payment:wallet",
1031+
}),
1032+
});
1033+
});
1034+
1035+
test("updates the provider v0 created rather than creating a scoped one", async () => {
1036+
const subject = account({ "my-oauth": { credentialProviderArn: "arn:oauth:my-oauth" } });
1037+
const input = await legacyProject(
1038+
[OAUTH],
1039+
"AGENTCORE_CREDENTIAL_MY_OAUTH_CLIENT_SECRET='rotated'\n",
1040+
);
1041+
1042+
const { result } = await run(subject.provision, input);
1043+
1044+
expect(Object.keys(subject.contents())).toEqual(["my-oauth"]);
1045+
expect(subject.contents()["my-oauth"]?.operation).toBe("updated");
1046+
expect(result["my-oauth"]?.credentialProviderArn).toBe("arn:oauth:my-oauth");
1047+
});
1048+
1049+
test("reuses the provider v0 created when no secret is available locally", async () => {
1050+
const existing = { credentialProviderArn: "arn:apikey:openai-key" };
1051+
const subject = account({ "openai-key": existing });
1052+
1053+
const { result } = await run(subject.provision, await legacyProject([API_KEY]));
1054+
1055+
expect(subject.contents()).toEqual({ "openai-key": existing });
1056+
expect(result).toEqual({ "openai-key": recordedAs("ApiKeyCredentialProvider", existing) });
1057+
});
1058+
1059+
test("rolls back a provider it created under the bare name", async () => {
1060+
const subject = account({}, { createFailsFor: "wallet" });
1061+
const input = await legacyProject(
1062+
[API_KEY, COINBASE],
1063+
"AGENTCORE_CREDENTIAL_OPENAI_KEY='sk-live'\n" + COINBASE_ENV,
1064+
);
1065+
1066+
await runFailing(subject.provision, input);
1067+
1068+
expect(subject.deleted).toEqual([{ kind: "apikey", name: "openai-key" }]);
1069+
});
1070+
1071+
test("never deletes API key or OAuth providers, which v0 projects may share", async () => {
1072+
const subject = account({
1073+
"openai-key": { credentialProviderArn: "arn:apikey:openai-key" },
1074+
"my-oauth": { credentialProviderArn: "arn:oauth:my-oauth" },
1075+
wallet: { credentialProviderArn: "arn:payment:wallet" },
1076+
});
1077+
const input = await legacyProject([]);
1078+
1079+
const { events } = await collect(
1080+
createCredentialRemover(subject.client)(input, {
1081+
...INPUT,
1082+
providers: [API_KEY, OAUTH, COINBASE],
1083+
}),
1084+
);
1085+
1086+
// Payment providers are removed as v0's teardown removed them, by the bare name.
1087+
expect(subject.deleted).toEqual([{ kind: "payment", name: "wallet" }]);
1088+
expect(Object.keys(subject.contents())).toEqual(["openai-key", "my-oauth"]);
1089+
expect(stepMessages(events)).toEqual([
1090+
"Leaving credential provider 'openai-key' in place: v0 projects can share it",
1091+
"Leaving credential provider 'my-oauth' in place: v0 projects can share it",
1092+
"Removing credential provider 'wallet'",
1093+
]);
1094+
});
1095+
});

‎src/core/project/backends/cdk/credentials.ts‎

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,14 @@ export type DeployedCredentials = Record<string, DeployedCredential>;
3939
/**
4040
* The Identity name a credential's provider is created under. Scoped to the project
4141
* and target so two targets in one account and region never share a provider.
42+
*
43+
* A v0 project keeps the bare credential name v0 created its providers under: the
44+
* agent code it deployed looks them up by that name, and a scoped name would create a
45+
* second set of providers beside the ones still in use.
4246
*/
43-
function providerName(projectName: string, targetName: string, credentialName: string): string {
44-
return `${projectName}_${targetName}_${credentialName}`;
47+
function providerName(project: Project, targetName: string, credentialName: string): string {
48+
if (project.legacy) return credentialName;
49+
return `${project.name}_${targetName}_${credentialName}`;
4550
}
4651

4752
const PROVIDER_NAME_MAX_LENGTH = 128;
@@ -133,6 +138,10 @@ export function orphanedCredentials(
133138
* nothing outside this target can be using it. A provider that is already gone is
134139
* not an error, and a provider that cannot be deleted is reported rather than
135140
* failing the deploy that already changed the stack.
141+
*
142+
* A v0 project's providers carry the bare credential name, so another project in the
143+
* account can be using the same API key or OAuth provider; those are never deleted.
144+
* Payment providers are deleted, as v0's own teardown deleted them.
136145
*/
137146
export function createCredentialRemover(identity: ProviderDeletes): CredentialRemover {
138147
return async function* removeCredentials(
@@ -141,7 +150,14 @@ export function createCredentialRemover(identity: ProviderDeletes): CredentialRe
141150
) {
142151
const options: CoreOptions = { region, credentials };
143152
for (const { name, authorizerType } of providers) {
144-
const provider = providerName(project.name, targetName, name);
153+
const provider = providerName(project, targetName, name);
154+
if (project.legacy && authorizerType !== "PaymentCredentialProvider") {
155+
yield {
156+
type: "step",
157+
message: `Leaving credential provider '${provider}' in place: v0 projects can share it`,
158+
};
159+
continue;
160+
}
145161
yield { type: "step", message: `Removing credential provider '${provider}'` };
146162
try {
147163
await deleteProvider(identity, authorizerType, provider, options);
@@ -202,7 +218,7 @@ export function createCredentialProvisioner(
202218
// Every name is composed and checked before the first lookup, so one that is
203219
// too long fails the deploy before it touches Identity.
204220
const named: NamedCredential[] = declared.map((credential) => {
205-
const provider = providerName(project.name, targetName, credential.name);
221+
const provider = providerName(project, targetName, credential.name);
206222
if (provider.length > PROVIDER_NAME_MAX_LENGTH) {
207223
throw new ProjectStateError(
208224
`Credential '${credential.name}' would create a credential provider named ` +

0 commit comments

Comments
 (0)