diff --git a/src/core/project/boxNetworkWriters.ts b/src/core/project/boxNetworkWriters.ts index 9a2d48c88..117110bac 100644 --- a/src/core/project/boxNetworkWriters.ts +++ b/src/core/project/boxNetworkWriters.ts @@ -26,8 +26,13 @@ import { boxBedrockPolicy, boxMemoryPolicy, } from "./boxMemory"; -import { planBoxProfileCredentials, type BoxKeySources } from "./boxProfileCredentials"; -import type { BoxEnvRoute } from "./boxProfileEgress"; +import type { ReadEnvFileOptions } from "../../io/envFile"; +import { + boxKeyVariables, + planBoxProfileCredentials, + type BoxKeySources, +} from "./boxProfileCredentials"; +import { hasValue, type BoxEnvRoute } from "./boxProfileEgress"; import { boxTelemetryPolicy, boxTelemetryPolicyFile, @@ -72,6 +77,8 @@ export interface BoxNetworkEdits { readonly files?: readonly BoxNetworkFile[]; /** Entries for `.env.local`; each replaces the value it holds under that key. */ readonly envLocal?: readonly EnvLocalEntry[]; + /** Messages about what the writer could not do, for the user to act on. */ + readonly warnings?: readonly string[]; } /** What a writer reads besides the models: the project, and where build looks for each key. */ @@ -101,7 +108,9 @@ export interface CollectedEdits { readonly changedFields: readonly (keyof ProjectSpec)[]; readonly files: readonly BoxNetworkFile[]; readonly envLocal: readonly EnvLocalEntry[]; + /** The step messages of the changes to agentcore.json. */ readonly messages: readonly string[]; + readonly warnings: readonly string[]; } /** @@ -129,11 +138,13 @@ export function boxNetworkEdits( const messages: string[] = []; const files: BoxNetworkFile[] = []; const envLocal: EnvLocalEntry[] = []; + const warnings: string[] = []; for (const writer of writers) { const edits = writer.edits(networks, context); for (const change of edits.spec ?? []) messages.push(...change(spec)); files.push(...(edits.files ?? [])); envLocal.push(...(edits.envLocal ?? [])); + warnings.push(...(edits.warnings ?? [])); } const changedFields = (Object.keys(spec) as (keyof ProjectSpec)[]).filter( (field) => !isDeepStrictEqual(spec[field], context.spec[field]), @@ -145,6 +156,7 @@ export function boxNetworkEdits( files, envLocal, messages, + warnings, }; } @@ -186,10 +198,30 @@ export const credentialsWriter: BoxNetworkWriter = { }, ], envLocal: plan.entries, + warnings: plan.warnings, }; }, }; +/** + * How build reads `.env.local` for {@link credentialsWriter}, the only writer that reads it: only + * the variables its routes use, and none that `env` sets to a value, because a value set in `env` + * wins. An entry that is not read is not decrypted, so it cannot stop the build. With no routes, + * no entry is read. + */ +export function boxKeyReads( + networks: readonly BoxNetwork[], + spec: ProjectSpec, + env: NodeJS.ProcessEnv, +): ReadEnvFileOptions { + const variables = boxKeyVariables(spec.credentials, uniqueRoutes(networks)); + return { + select: (name) => variables.has(name), + // A blank value in `env` does not win, so build reads that variable from the file instead. + overrides: Object.fromEntries(Object.entries(env).filter(([, value]) => hasValue(value))), + }; +} + function uniqueRoutes(networks: readonly BoxNetwork[]): BoxEnvRoute[] { const routes: BoxEnvRoute[] = []; for (const route of networks.flatMap((network) => network.envRoutes)) { diff --git a/src/core/project/boxProfileCredentials.test.ts b/src/core/project/boxProfileCredentials.test.ts index 6c5ec7081..37c38ea25 100644 --- a/src/core/project/boxProfileCredentials.test.ts +++ b/src/core/project/boxProfileCredentials.test.ts @@ -1,12 +1,17 @@ import { describe, expect, test } from "bun:test"; -import { planBoxProfileCredentials, type BoxKeySources } from "./boxProfileCredentials"; +import { + boxKeyVariables, + planBoxProfileCredentials, + type BoxKeySources, +} from "./boxProfileCredentials"; import type { BoxEnvRoute } from "./boxProfileEgress"; import { ENV_LOCAL_RELATIVE_PATH } from "./envLocal"; import { credentialEnvVarName } from "../../projectSchemas/credential"; -import type { ProjectSpec } from "../../projectSchemas/project"; +import { ProjectSpecSchema, type ProjectSpec } from "../../projectSchemas/project"; -function spec(credentials: ProjectSpec["credentials"] = []): ProjectSpec { - return { credentials } as unknown as ProjectSpec; +/** A project spec with `credentials`, as the schema parses it. */ +function spec(credentials: readonly object[] = []): ProjectSpec { + return ProjectSpecSchema.parse({ name: "example", version: 2, credentials }); } const route = (name: string, variable: string): BoxEnvRoute => ({ route: name, variable }); @@ -50,7 +55,7 @@ describe("planBoxProfileCredentials", () => { expect(s.credentials).toEqual([]); expect(plan.added).toEqual([]); expect(plan.entries).toEqual([]); - expect(plan.messages).toEqual([ + expect(plan.warnings).toEqual([ "not adding credential 'github' for box egress 'env://GITHUB_TOKEN': GITHUB_TOKEN is not set " + `here or in ${ENV_LOCAL_RELATIVE_PATH}, so deploy could not provision it. Set GITHUB_TOKEN, or ${RESERVED}, ` + "and build again", @@ -60,7 +65,7 @@ describe("planBoxProfileCredentials", () => { test("names the variable once when the route reads its own credential's variable", () => { const plan = planBoxProfileCredentials(spec(), [route("github", RESERVED)], shell({})); - expect(plan.messages[0]).toEndWith(`Set ${RESERVED} and build again`); + expect(plan.warnings[0]).toEndWith(`Set ${RESERVED} and build again`); }); test("takes the key from .env.local under the box variable when the shell has none", () => { @@ -119,6 +124,19 @@ describe("planBoxProfileCredentials", () => { ]); }); + test("fills in a blank stored key, and says so", () => { + const plan = planBoxProfileCredentials(spec([API_KEY]), [GITHUB], { + env: { GITHUB_TOKEN: "ghp_new" }, + envLocal: { [RESERVED]: "" }, + }); + + expect(plan.entries.map(({ key, value }) => [key, value])).toEqual([[RESERVED, "ghp_new"]]); + expect(plan.messages).toEqual([ + `filling in the blank ${RESERVED} in ${ENV_LOCAL_RELATIVE_PATH} with the key in ` + + "GITHUB_TOKEN, so deploy provisions it", + ]); + }); + test("leaves a stored key alone when the variable holds it, or when nothing sets the variable", () => { for (const env of [{ GITHUB_TOKEN: "ghp_same" }, {}]) { const plan = planBoxProfileCredentials(spec([API_KEY]), [GITHUB], { @@ -141,11 +159,43 @@ describe("planBoxProfileCredentials", () => { expect(plan.entries).toEqual([]); }); + test("warns when the shell's reserved variable and the route's variable hold different keys", () => { + // Deploy provisions the reserved variable, but agentcore dev hands box the route's variable. + const plan = planBoxProfileCredentials(spec([API_KEY]), [GITHUB], { + env: { [RESERVED]: "ghp_explicit" }, + envLocal: { GITHUB_TOKEN: "ghp_dev" }, + }); + + expect(plan.warnings).toEqual([ + `box egress 'github': deploy provisions ${RESERVED} from the environment, but agentcore ` + + "dev uses env://GITHUB_TOKEN, which holds a different key, so dev and deploy can differ. " + + `Unset ${RESERVED}, or set it to the key in GITHUB_TOKEN`, + ]); + expect(plan.messages).toEqual([]); + }); + + test.each([ + ["holds the same key", { GITHUB_TOKEN: "ghp_explicit" }], + ["is not set", {}], + ["is blank", { GITHUB_TOKEN: " " }], + ])( + "does not warn about the shell's reserved variable when the route's variable %s", + (_label, routeEnv: NodeJS.ProcessEnv) => { + const plan = planBoxProfileCredentials( + spec([API_KEY]), + [GITHUB], + shell({ ...routeEnv, [RESERVED]: "ghp_explicit" }), + ); + + expect(plan.warnings).toEqual([]); + }, + ); + test("keeps an earlier build's credential and says what to set when its key has gone", () => { const plan = planBoxProfileCredentials(spec([API_KEY]), [GITHUB], shell({})); expect(plan.added).toEqual([]); - expect(plan.messages).toEqual([ + expect(plan.warnings).toEqual([ "box egress 'github' reads env://GITHUB_TOKEN, which is not set here or in " + `${ENV_LOCAL_RELATIVE_PATH}: set GITHUB_TOKEN, or ${RESERVED}, before deploy`, ]); @@ -153,11 +203,18 @@ describe("planBoxProfileCredentials", () => { test("stores nothing for an API key credential that keeps its key in Secrets Manager", () => { const s = spec([ - { ...API_KEY, secretRef: "arn:aws:secretsmanager:us-west-2:111122223333:secret:gh" }, - ] as unknown as ProjectSpec["credentials"]); + { + ...API_KEY, + secretRef: { + secretId: "arn:aws:secretsmanager:us-west-2:111122223333:secret:gh", + jsonKey: "token", + }, + }, + ]); expect(planBoxProfileCredentials(s, [GITHUB], shell({ GITHUB_TOKEN: "ghp_x" }))).toEqual({ messages: [], + warnings: [], added: [], entries: [], }); @@ -172,7 +229,7 @@ describe("planBoxProfileCredentials", () => { shell({ OPENAI_API_KEY: "sk-x", ANTHROPIC_API_KEY: "sk-ant-x" }), ); - expect(plan).toEqual({ messages: [], added: [], entries: [] }); + expect(plan).toEqual({ messages: [], warnings: [], added: [], entries: [] }); expect(s.credentials).toEqual([]); }); @@ -185,7 +242,7 @@ describe("planBoxProfileCredentials", () => { shell({}), ); - expect(plan).toEqual({ messages: [], added: [], entries: [] }); + expect(plan).toEqual({ messages: [], warnings: [], added: [], entries: [] }); }); test("reports a route that uses a model-provider name or variable in another way", () => { @@ -196,7 +253,7 @@ describe("planBoxProfileCredentials", () => { ); expect(plan.added).toEqual([]); - expect(plan.messages).toEqual([ + expect(plan.warnings).toEqual([ "skipping box egress 'openai' (env://MY_KEY): 'openai' is a model-provider route; " + "agentcore create declares its key", "skipping box egress 'my-llm' (env://GEMINI_API_KEY): GEMINI_API_KEY is a model provider's " + @@ -204,75 +261,103 @@ describe("planBoxProfileCredentials", () => { ]); }); - test("reports, but does not add, a name that is not a valid credential name", () => { - const plan = planBoxProfileCredentials( - spec(), - [route("bad name!", "SOME_KEY")], - shell({ SOME_KEY: "k" }), - ); - - expect(plan.added).toEqual([]); - expect(plan.messages).toEqual([ - "skipping box egress 'bad name!' (env://SOME_KEY): 'bad name!' is not a valid credential name", - ]); - }); - - test("reports, but does not add, a name that ends in a credential field suffix", () => { + test.each<{ + label: string; + credentials: object[]; + routes: BoxEnvRoute[]; + added: string[]; + warning: string; + }>([ + { + label: "a name that is not a valid credential name", + credentials: [], + routes: [route("bad name!", "SOME_KEY")], + added: [], + warning: + "skipping box egress 'bad name!' (env://SOME_KEY): 'bad name!' is not a valid credential name", + }, + { + label: "a name that ends in a credential field suffix", + credentials: [], + routes: [route("github_client_id", "GITHUB_ID")], + added: [], + warning: + "skipping box egress 'github_client_id' (env://GITHUB_ID): 'github_client_id' ends in " + + "'_CLIENT_ID', the suffix of a credential field, so its variable " + + `${credentialEnvVarName("github_client_id")} would read as a field of another credential`, + }, + { + label: "a name that derives an existing credential's variable", + credentials: [{ authorizerType: "ApiKeyCredentialProvider", name: "git-hub" }], + routes: [route("git_hub", "GITHUB_TOKEN")], + added: [], + warning: + "skipping box egress 'git_hub' (env://GITHUB_TOKEN): credential 'git_hub' would derive the " + + "same variable, AGENTCORE_CREDENTIAL_GIT_HUB, as credential 'git-hub'", + }, + { + label: "the second of two routes in one profile that derive the same variable", + credentials: [], + routes: [route("git-hub", "GITHUB_TOKEN"), route("git_hub", "GITHUB_TOKEN")], + added: ["git-hub"], + warning: + "skipping box egress 'git_hub' (env://GITHUB_TOKEN): credential 'git_hub' would derive the " + + "same variable, AGENTCORE_CREDENTIAL_GIT_HUB, as credential 'git-hub'", + }, + { + label: "a route named for an OAuth credential", + credentials: [ + { + authorizerType: "OAuthCredentialProvider", + name: "github", + discoveryUrl: "https://example.com/.well-known/openid-configuration", + }, + ], + routes: [GITHUB], + added: [], + warning: + "skipping box egress 'github' (env://GITHUB_TOKEN): credential 'github' is an " + + "OAuthCredentialProvider credential, not an API key", + }, + { + label: "a route named for a payment credential", + credentials: [ + { authorizerType: "PaymentCredentialProvider", name: "github", provider: "StripePrivy" }, + ], + routes: [GITHUB], + added: [], + warning: + "skipping box egress 'github' (env://GITHUB_TOKEN): credential 'github' is a " + + "PaymentCredentialProvider credential, not an API key", + }, + ])("reports, but does not add, $label", ({ credentials, routes, added, warning }) => { const plan = planBoxProfileCredentials( - spec(), - [route("github_client_id", "GITHUB_ID")], - shell({ GITHUB_ID: "k" }), + spec(credentials), + routes, + shell({ SOME_KEY: "k", GITHUB_ID: "k", GITHUB_TOKEN: "k" }), ); - expect(plan.added).toEqual([]); - expect(plan.messages).toHaveLength(1); - expect(plan.messages[0]).toContain("ends in '_CLIENT_ID'"); - }); - - test("reports, but does not add, a name that derives an existing credential's variable", () => { - const s = spec([{ authorizerType: "ApiKeyCredentialProvider", name: "git-hub" }]); - - const plan = planBoxProfileCredentials( - s, - [route("git_hub", "GITHUB_TOKEN")], - shell({ GITHUB_TOKEN: "k" }), + expect(plan.added.map(({ name }) => name)).toEqual(added); + expect(plan.entries.map(({ key }) => key)).toEqual( + added.map((name) => credentialEnvVarName(name)), ); - - expect(plan.added).toEqual([]); - expect(plan.entries).toEqual([]); - expect(plan.messages).toEqual([ - "skipping box egress 'git_hub' (env://GITHUB_TOKEN): credential 'git_hub' would derive the " + - "same variable, AGENTCORE_CREDENTIAL_GIT_HUB, as credential 'git-hub'", - ]); + expect(plan.warnings).toEqual([warning]); }); +}); - test("reports two routes in one profile that derive the same variable, and adds the first", () => { - const plan = planBoxProfileCredentials( - spec(), - [route("git-hub", "GITHUB_TOKEN"), route("git_hub", "GITHUB_TOKEN")], - shell({ GITHUB_TOKEN: "k" }), - ); - - expect(plan.added.map(({ name }) => name)).toEqual(["git-hub"]); - expect(plan.messages[1]).toContain("as credential 'git-hub'"); +describe("boxKeyVariables", () => { + test("holds the variables of each route the planner looks up a key for", () => { + expect(boxKeyVariables([], [GITHUB])).toEqual(new Set(["GITHUB_TOKEN", RESERVED])); }); - test("reports, but does not use, a route named for a credential that is not an API key", () => { - const s = spec([ - { - authorizerType: "OAuthCredentialProvider", - name: "github", - discoveryUrl: "https://example.com/.well-known/openid-configuration", - }, - ] as unknown as ProjectSpec["credentials"]); + test("skips a model-provider route and a route whose variable a credential reserves", () => { + const reserved = credentialEnvVarName("agentOpenAIApiKey"); - const plan = planBoxProfileCredentials(s, [GITHUB], shell({ GITHUB_TOKEN: "k" })); + const variables = boxKeyVariables( + [{ authorizerType: "ApiKeyCredentialProvider", name: "agentOpenAIApiKey" }], + [route("openai", "OPENAI_API_KEY"), route("my-llm", reserved)], + ); - expect(plan.added).toEqual([]); - expect(plan.entries).toEqual([]); - expect(plan.messages).toEqual([ - "skipping box egress 'github' (env://GITHUB_TOKEN): credential 'github' is a " + - "OAuthCredentialProvider credential, not an API key", - ]); + expect(variables).toEqual(new Set()); }); }); diff --git a/src/core/project/boxProfileCredentials.ts b/src/core/project/boxProfileCredentials.ts index b289357d4..f5dc1bdca 100644 --- a/src/core/project/boxProfileCredentials.ts +++ b/src/core/project/boxProfileCredentials.ts @@ -22,8 +22,13 @@ export type BoxKeySources = { /** What {@link planBoxProfileCredentials} did with the box profile's `env://` routes. */ export type BoxCredentialPlan = { - /** One message for each credential added, and for each route skipped for a reason to report. */ + /** One step message for each credential added, and for each stored key replaced or filled in. */ messages: string[]; + /** + * One warning for each route skipped for a reason to report, and for each route whose key is in + * no key source, so deploy could not provision its credential. + */ + warnings: string[]; /** The credentials added to `spec.credentials`. */ added: Credential[]; /** The `.env.local` entries that store each key under its credential's reserved variable. */ @@ -46,6 +51,42 @@ export function boxEgressCredentialName( return holder?.name ?? route; } +/** + * Whether {@link planBoxProfileCredentials} skips `route` before it looks up any key, because + * something else holds the key: a credential whose variables `reserved` holds reserves the route's + * variable, or the route is a model-provider route as `agentcore create` writes it (`openai`, + * `anthropic`, `gemini` or `openai-compatible`, reading the provider variable such as + * `OPENAI_API_KEY`), whose key is the variable LiteLLM reads. + */ +function boxRouteKeyHeldElsewhere( + { route, variable }: BoxEnvRoute, + reserved: { has(variable: string): boolean }, +): boolean { + return ( + reserved.has(variable) || + (MODEL_PROVIDER_EGRESS_ROUTES.has(route) && MODEL_PROVIDER_KEY_VARIABLES.has(variable)) + ); +} + +/** + * The variables {@link planBoxProfileCredentials} can look up in its key sources for `routes` in a + * project with `credentials`: for each route it does not skip first, the route's `env://` variable + * and the reserved variable of the credential named for the route. Build reads only these from + * `.env.local`, so an entry no route uses cannot stop it. A credential that the planner adds for + * one route can reserve the variable of a later route, which the set then still holds. + */ +export function boxKeyVariables( + credentials: readonly Credential[], + routes: readonly BoxEnvRoute[], +): Set { + const reserved = new Set(credentials.flatMap(credentialEnvironmentVariableNames)); + return new Set( + routes + .filter((route) => !boxRouteKeyHeldElsewhere(route, reserved)) + .flatMap(({ route, variable }) => [variable, credentialEnvVarName(route)]), + ); +} + /** * For each `[egress.] secret.ref = "env://VAR"` route of the box profile, make sure the * project declares an `ApiKeyCredentialProvider` named ``, and that `.env.local` holds its @@ -55,17 +96,14 @@ export function boxEgressCredentialName( * The key comes from the credential's reserved variable when it is already set, else from `VAR`, * in `sources.env` first and then in `sources.envLocal`. Deploy provisions every declared * credential and fails on one with no key, so a route whose key is in neither place gets no - * credential: only a message that names the variable to set. A credential an earlier build added - * is kept, and gets the same message if its key has gone. + * credential: only a warning that names the variable to set. A credential an earlier build added + * is kept, and gets the same warning if its key has gone. * * Mutates `spec.credentials` in place, and is idempotent. It skips, with no message: - * - a route whose variable an existing credential reserves: that credential holds the key; - * - a model-provider route as `agentcore create` writes it (`openai`, `anthropic`, `gemini` or - * `openai-compatible`, reading the provider variable such as `OPENAI_API_KEY`): its key is - * the variable LiteLLM reads, not a credential; + * - a route whose key something else holds ({@link boxRouteKeyHeldElsewhere}); * - a route named for an API key credential that keeps its key in Secrets Manager. * - * It skips, with a message, a route that uses a model-provider name or variable in any other way, + * It skips, with a warning, a route that uses a model-provider name or variable in any other way, * and a route whose name `agentcore credentials add` would refuse: a name that is not a valid * credential name, that ends in a credential field suffix, that derives the same variable as an * existing credential, or that names a credential which is not an API key credential. @@ -89,6 +127,7 @@ export function planBoxProfileCredentials( ); const messages: string[] = []; + const warnings: string[] = []; const added: Credential[] = []; const entries: EnvLocalEntry[] = []; /** @@ -96,14 +135,23 @@ export function planBoxProfileCredentials( * route's variable from the environment, else from `.env.local`. A stored key that differs is * replaced, so dev and deploy use the same key after a rotation. False when there is none. */ - const seed = ({ variable }: BoxEnvRoute, credential: string): boolean => { + const seed = ({ route, variable }: BoxEnvRoute, credential: string): boolean => { const key = credentialEnvVarName(credential); - // Set in the environment itself, it is what deploy provisions; nothing to store. - if (hasValue(sources.env[key])) return true; - if (entries.some((entry) => entry.key === key)) return true; const value = hasValue(sources.env[variable]) ? sources.env[variable] : sources.envLocal[variable]; + // Set in the environment itself, it is what deploy provisions; nothing to store. + if (hasValue(sources.env[key])) { + if (hasValue(value) && value !== sources.env[key]) { + warnings.push( + `box egress '${route}': deploy provisions ${key} from the environment, but agentcore ` + + `dev uses env://${variable}, which holds a different key, so dev and deploy can ` + + `differ. Unset ${key}, or set it to the key in ${variable}`, + ); + } + return true; + } + if (entries.some((entry) => entry.key === key)) return true; const stored = sources.envLocal[key]; if (!hasValue(value)) return hasValue(stored); if (value === stored) return true; @@ -112,6 +160,11 @@ export function planBoxProfileCredentials( `updating ${key} in ${ENV_LOCAL_RELATIVE_PATH}: ${variable} holds a different key, which ` + "agentcore dev uses, so deploy provisions it too", ); + } else if (stored !== undefined) { + messages.push( + `filling in the blank ${key} in ${ENV_LOCAL_RELATIVE_PATH} with the key in ${variable}, ` + + "so deploy provisions it", + ); } entries.push({ key, @@ -127,13 +180,12 @@ export function planBoxProfileCredentials( for (const entry of routes) { const { route, variable } = entry; - if (reservedVars.has(variable)) continue; + if (boxRouteKeyHeldElsewhere(entry, reservedVars)) continue; const providerName = MODEL_PROVIDER_EGRESS_ROUTES.has(route); const providerVariable = MODEL_PROVIDER_KEY_VARIABLES.has(variable); - if (providerName && providerVariable) continue; const skip = (reason: string) => - messages.push(`skipping box egress '${route}' (env://${variable}): ${reason}`); + warnings.push(`skipping box egress '${route}' (env://${variable}): ${reason}`); if (providerName || providerVariable) { skip( providerName @@ -145,9 +197,12 @@ export function planBoxProfileCredentials( const existing = spec.credentials.find((credential) => credential.name === route); if (existing !== undefined) { if (existing.authorizerType !== "ApiKeyCredentialProvider") { - skip(`credential '${route}' is a ${existing.authorizerType} credential, not an API key`); + const article = /^[AEIOU]/.test(existing.authorizerType) ? "an" : "a"; + skip( + `credential '${route}' is ${article} ${existing.authorizerType} credential, not an API key`, + ); } else if (credentialEnvironmentVariableNames(existing).length > 0 && !seed(entry, route)) { - messages.push( + warnings.push( `box egress '${route}' reads env://${variable}, which is not set here or in ` + `${ENV_LOCAL_RELATIVE_PATH}: set ${toSet(variable, route)} before deploy`, ); @@ -178,7 +233,7 @@ export function planBoxProfileCredentials( continue; } if (!seed(entry, route)) { - messages.push( + warnings.push( `not adding credential '${route}' for box egress 'env://${variable}': ${variable} is not ` + `set here or in ${ENV_LOCAL_RELATIVE_PATH}, so deploy could not provision it. ` + `Set ${toSet(variable, route)} and build again`, @@ -195,5 +250,5 @@ export function planBoxProfileCredentials( "so deploy provisions it in AgentCore Identity", ); } - return { messages, added, entries }; + return { messages, warnings, added, entries }; } diff --git a/src/core/project/manager.boxCredentials.test.ts b/src/core/project/manager.boxCredentials.test.ts index 8cefab80b..125bfd964 100644 --- a/src/core/project/manager.boxCredentials.test.ts +++ b/src/core/project/manager.boxCredentials.test.ts @@ -1,5 +1,5 @@ import { afterEach, describe, expect, test } from "bun:test"; -import { mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises"; +import { appendFile, mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { FsProjectManager } from "./manager"; @@ -10,6 +10,7 @@ import { boxLocalDir, boxLocalProfileFile } from "./templates/boxProfile"; import { boxShowOf, FakeBoxProfileResolver } from "./boxProfileResolver-test-support"; import { resolveRuntimeTemplateShortcut } from "../../handlers/project/shortcuts"; import { credentialEnvVarName } from "../../projectSchemas/credential"; +import { ENC_PREFIX } from "../../secrets/cipher"; import { createSilentLogger, TestIdentityClient } from "../../testing"; // A box template: only it writes the box profile whose env:// secrets build provisions. @@ -23,6 +24,8 @@ const GITHUB_SHOWN = boxShowOf({ egress: { github: { destinations: ["api.github.com"], secret: { ref: "env://GITHUB_TOKEN" } } }, }); const RESERVED = credentialEnvVarName("github"); +/** A stored secret in the encrypted format that no key on this machine decrypts. */ +const UNDECRYPTABLE = `${ENC_PREFIX}${Buffer.alloc(48).toString("base64")}`; const originalCwd = process.cwd(); const directories: string[] = []; @@ -32,7 +35,10 @@ afterEach(async () => { }); /** A manager whose CDK backend records the project it builds, and runs nothing. */ -function manager(processEnv: NodeJS.ProcessEnv): { subject: FsProjectManager; built: Project[] } { +function manager( + processEnv: NodeJS.ProcessEnv, + shown = GITHUB_SHOWN, +): { subject: FsProjectManager; built: Project[] } { const built: Project[] = []; const backend: ProjectBackend = { async *build(project) { @@ -58,7 +64,7 @@ function manager(processEnv: NodeJS.ProcessEnv): { subject: FsProjectManager; bu runner: async () => {}, checkTool: async () => {}, backends: { CDK: backend }, - boxProfileResolver: new FakeBoxProfileResolver(GITHUB_SHOWN), + boxProfileResolver: new FakeBoxProfileResolver(shown), }); return { subject, built }; } @@ -82,10 +88,28 @@ async function project(subject: FsProjectManager, profile: string): Promise): Promise { - const messages: string[] = []; - for await (const event of generator) if (event.type === "step") messages.push(event.message); - return messages; +/** The step messages of `generator`, then its warnings. */ +async function drain( + generator: AsyncGenerator, +): Promise<{ steps: string[]; warnings: string[] }> { + const steps: string[] = []; + const warnings: string[] = []; + for await (const event of generator) { + if (event.type === "step") steps.push(event.message); + else if (event.type === "warning") warnings.push(event.message); + } + return { steps, warnings }; +} + +/** Appends `line` to the project's `.env.local` as written, so no value in it is encrypted. */ +async function appendEnvLocal(root: string, line: string): Promise { + await mkdir(join(root, "agentcore"), { recursive: true }); + await appendFile(join(root, ENV_LOCAL_RELATIVE_PATH), `\n${line}\n`); +} + +/** The key build stored under the credential's reserved variable; reads no other entry. */ +async function storedKey(root: string): Promise { + return (await new EnvLocalFile(root).read({ select: (name) => name === RESERVED }))[RESERVED]; } async function specOf(root: string): Promise> { @@ -98,7 +122,7 @@ describe("FsProjectManager.build: box env:// credentials", () => { const created = await project(subject, GITHUB_PROFILE); const before = await specOf(created.rootPath); - const messages = await drain(subject.build(created)); + const { steps } = await drain(subject.build(created)); const after = await specOf(created.rootPath); expect(after.credentials).toEqual([ @@ -120,7 +144,7 @@ describe("FsProjectManager.build: box env:// credentials", () => { ]); const stored = await new EnvLocalFile(created.rootPath).read(); expect(stored[RESERVED]).toBe("ghp_shell"); - expect(messages).toContain(`writing ${RESERVED} to ${ENV_LOCAL_RELATIVE_PATH}`); + expect(steps).toContain(`writing ${RESERVED} to ${ENV_LOCAL_RELATIVE_PATH}`); expect(built[0]?.spec.credentials).toContainEqual({ authorizerType: "ApiKeyCredentialProvider", name: "github", @@ -134,14 +158,38 @@ describe("FsProjectManager.build: box env:// credentials", () => { const spec = await readFile(join(created.rootPath, "agentcore", "agentcore.json"), "utf8"); const reread = await subject.resolve({ filePath: created.rootPath }); - const messages = await drain(subject.build(reread!)); + const { steps, warnings } = await drain(subject.build(reread!)); - expect(messages.filter((message) => message.includes("github"))).toEqual([]); + expect([...steps, ...warnings].filter((message) => message.includes("github"))).toEqual([]); expect(await readFile(join(created.rootPath, "agentcore", "agentcore.json"), "utf8")).toBe( spec, ); }); + test("a second build finds the key stored only under the credential's reserved variable", async () => { + const { subject: first } = manager({ GITHUB_TOKEN: "ghp_shell" }); + const created = await project(first, GITHUB_PROFILE); + await drain(first.build(created)); + const { subject } = manager({}); + const reread = await subject.resolve({ filePath: created.rootPath }); + + const { steps, warnings } = await drain(subject.build(reread!)); + + expect( + [...steps, ...warnings].filter((message) => message.includes("env://GITHUB_TOKEN")), + ).toEqual([]); + expect(await storedKey(created.rootPath)).toBe("ghp_shell"); + }); + + test("warns when the shell's reserved variable holds a key other than the route's", async () => { + const { subject } = manager({ GITHUB_TOKEN: "ghp_dev", [RESERVED]: "ghp_explicit" }); + const created = await project(subject, GITHUB_PROFILE); + + const { warnings } = await drain(subject.build(created)); + + expect(warnings).toContainEqual(expect.stringContaining("so dev and deploy can differ")); + }); + test("seeds the key that agentcore dev stored in .env.local under the box variable", async () => { const { subject } = manager({}); const created = await project(subject, GITHUB_PROFILE); @@ -162,17 +210,113 @@ describe("FsProjectManager.build: box env:// credentials", () => { .credentials; const before = await credentials(); - const messages = await drain(subject.build(created)); + const { steps, warnings } = await drain(subject.build(created)); - expect(messages).toContain( + expect(warnings).toContain( "not adding credential 'github' for box egress 'env://GITHUB_TOKEN': GITHUB_TOKEN is not " + `set here or in ${ENV_LOCAL_RELATIVE_PATH}, so deploy could not provision it. ` + `Set GITHUB_TOKEN, or ${RESERVED}, and build again`, ); + expect(steps.filter((message) => message.includes("not adding credential"))).toEqual([]); // Build still adds the model's target, so only the credentials stay as they were. expect(await credentials()).toEqual(before); }); + test("builds when .env.local holds an undecryptable entry that no route uses", async () => { + const { subject, built } = manager({ GITHUB_TOKEN: "ghp_shell" }); + const created = await project(subject, GITHUB_PROFILE); + await appendEnvLocal(created.rootPath, `OTHER_API_KEY='${UNDECRYPTABLE}'`); + + await drain(subject.build(created)); + + expect(built[0]?.spec.credentials).toContainEqual({ + authorizerType: "ApiKeyCredentialProvider", + name: "github", + }); + expect(await storedKey(created.rootPath)).toBe("ghp_shell"); + }); + + test("seeds a blank reserved variable when .env.local also holds an unused undecryptable entry", async () => { + const { subject } = manager({ GITHUB_TOKEN: "ghp_shell" }); + const created = await project(subject, GITHUB_PROFILE); + await writeFile(join(created.rootPath, ENV_LOCAL_RELATIVE_PATH), `${RESERVED}=\n`); + await appendEnvLocal(created.rootPath, `OTHER_API_KEY='${UNDECRYPTABLE}'`); + + const { steps } = await drain(subject.build(created)); + + expect(await storedKey(created.rootPath)).toBe("ghp_shell"); + expect(steps).toContain( + `filling in the blank ${RESERVED} in ${ENV_LOCAL_RELATIVE_PATH} with the key in ` + + "GITHUB_TOKEN, so deploy provisions it", + ); + }); + + test("builds when .env.local holds an undecryptable key of a model-provider route", async () => { + const { subject, built } = manager( + {}, + boxShowOf({ + egress: { + openai: { destinations: ["api.openai.com"], secret: { ref: "env://OPENAI_API_KEY" } }, + }, + }), + ); + const created = await project( + subject, + '[egress.openai]\ndestinations = ["api.openai.com"]\nsecret.ref = "env://OPENAI_API_KEY"\n', + ); + await appendEnvLocal(created.rootPath, `OPENAI_API_KEY='${UNDECRYPTABLE}'`); + + await drain(subject.build(created)); + + expect(built).toHaveLength(1); + }); + + test("builds with no box routes when .env.local holds an undecryptable entry", async () => { + const { subject, built } = manager({}, boxShowOf({})); + const created = await project(subject, ""); + await appendEnvLocal(created.rootPath, `GITHUB_TOKEN='${UNDECRYPTABLE}'`); + + await drain(subject.build(created)); + + expect(built).toHaveLength(1); + }); + + test("builds when the shell sets the variable of an undecryptable .env.local entry", async () => { + const { subject, built } = manager({ GITHUB_TOKEN: "ghp_shell" }); + const created = await project(subject, GITHUB_PROFILE); + await appendEnvLocal(created.rootPath, `GITHUB_TOKEN='${UNDECRYPTABLE}'`); + + await drain(subject.build(created)); + + expect(built[0]?.spec.credentials).toContainEqual({ + authorizerType: "ApiKeyCredentialProvider", + name: "github", + }); + expect(await storedKey(created.rootPath)).toBe("ghp_shell"); + }); + + test.each([ + ["the shell does not set it", {}], + ["the shell sets it blank", { GITHUB_TOKEN: " " }], + ])( + "stops on an undecryptable .env.local entry that a route uses when %s", + async (_label, env: NodeJS.ProcessEnv) => { + const { subject, built } = manager(env); + const created = await project(subject, GITHUB_PROFILE); + await appendEnvLocal(created.rootPath, `GITHUB_TOKEN='${UNDECRYPTABLE}'`); + const spec = await readFile(join(created.rootPath, "agentcore", "agentcore.json"), "utf8"); + + await expect(drain(subject.build(created))).rejects.toThrow( + "Could not decrypt a stored secret in agentcore/.env.local. " + + "Restore the original encryption key or re-add the credential.", + ); + expect(built).toEqual([]); + expect(await readFile(join(created.rootPath, "agentcore", "agentcore.json"), "utf8")).toBe( + spec, + ); + }, + ); + test("stops on a box profile that does not parse, and changes nothing", async () => { const { subject } = manager({ GITHUB_TOKEN: "ghp_shell" }); const created = await project(subject, "[egress.github\n"); diff --git a/src/core/project/manager.tsx b/src/core/project/manager.tsx index 480709beb..4e9fe39b5 100644 --- a/src/core/project/manager.tsx +++ b/src/core/project/manager.tsx @@ -58,7 +58,12 @@ import { boxGatewayIamWarnings, type BoxNetwork } from "./boxNetwork"; import { boxNetworkFailureMessage, readBoxNetworks } from "./boxNetworkSource"; import { DEFAULT_REGION, regionFromConfigFile } from "../region"; import { BoxCliProfileResolver, type BoxProfileResolver } from "./boxProfileResolver"; -import { BOX_NETWORK_WRITERS, boxNetworkEdits, writeBoxNetworkFiles } from "./boxNetworkWriters"; +import { + BOX_NETWORK_WRITERS, + boxKeyReads, + boxNetworkEdits, + writeBoxNetworkFiles, +} from "./boxNetworkWriters"; import { createProjectTree } from "./templates/project"; import { BOX_CODEZIP_ENTRYPOINT, @@ -1684,7 +1689,7 @@ export class FsProjectManager implements ProjectManager { spec: project.spec, projectRoot: project.rootPath, env: this.processEnv, - envLocal: networks.length > 0 ? await envLocal.read() : {}, + envLocal: await envLocal.read(boxKeyReads(networks, project.spec, this.processEnv)), }); const messages = [...edits.messages]; let result = project; @@ -1722,10 +1727,13 @@ export class FsProjectManager implements ProjectManager { const { written } = await envLocal.upsert([...edits.envLocal]); for (const key of written) messages.push(`writing ${key} to ${ENV_LOCAL_RELATIVE_PATH}`); } - const warnings = networks.flatMap((network) => [ - ...network.warnings, - ...boxGatewayIamWarnings(edits.spec, network), - ]); + const warnings = [ + ...edits.warnings, + ...networks.flatMap((network) => [ + ...network.warnings, + ...boxGatewayIamWarnings(edits.spec, network), + ]), + ]; const events: ProjectEvent[] = [ ...messages.map((message) => ({ type: "step" as const, message })), ...warnings.map((message) => ({ type: "warning" as const, message })),