diff --git a/src/core/project/boxMemory.test.ts b/src/core/project/boxMemory.test.ts index 34fb1090b..96dfbfb43 100644 --- a/src/core/project/boxMemory.test.ts +++ b/src/core/project/boxMemory.test.ts @@ -153,6 +153,18 @@ describe("boxMemoryPolicy", () => { ]); }); + test("names the partition of the region it is given", () => { + const resources = (region: string) => + (boxMemoryPolicy(spec(), region) as Policy).Statement[0]!.Resource; + + expect(resources("cn-north-1")).toEqual([ + "arn:aws-cn:bedrock-agentcore:cn-north-1:*:memory/demo_*agentMemory-*", + ]); + expect(resources("us-gov-west-1")).toEqual([ + "arn:aws-us-gov:bedrock-agentcore:us-gov-west-1:*:memory/demo_*agentMemory-*", + ]); + }); + test("is undefined when no boxed runtime has its default Memory", () => { expect(boxMemoryPolicy(spec({ memories: ["ordersMemory"] }), "us-west-2")).toBeUndefined(); expect(boxMemoryPolicy(spec({ entrypoint: "main.py" }), "us-west-2")).toBeUndefined(); @@ -182,7 +194,7 @@ describe("addBoxBedrockTarget", () => { for (const project of [mcp, openai, plain]) { expect(addBoxBedrockTarget(project)).toEqual([]); - expect(boxBedrockPolicy(project)).toBeUndefined(); + expect(boxBedrockPolicy(project, "us-west-2")).toBeUndefined(); } }); @@ -195,23 +207,65 @@ describe("addBoxBedrockTarget", () => { expect(targets(project)).toEqual(["bedrock"]); expect(gateway(project).additionalPolicies).toBeUndefined(); }); +}); - test("grants the gateway role the model invoke actions in any region", () => { - expect(boxBedrockPolicy(spec())).toEqual({ - Version: "2012-10-17", - Statement: [ - { - Sid: "BoxedAgentModelCallsThroughGateway", - Effect: "Allow", - Action: ["bedrock:InvokeModel", "bedrock:InvokeModelWithResponseStream"], - Resource: [ - "arn:aws:bedrock:*:*:inference-profile/*", - "arn:aws:bedrock:*::foundation-model/*", - ], +describe("boxBedrockPolicy", () => { + type Statement = Record & { Resource: string[] }; + const statements = (region: string) => + (boxBedrockPolicy(spec(), region) as { Statement: Statement[] }).Statement; + const ACTIONS = ["bedrock:InvokeModel", "bedrock:InvokeModelWithResponseStream"]; + + test("allows the inference profiles of the gateway role's account in the region it is given, and no other", () => { + expect(statements("eu-central-1")).toHaveLength(3); + expect(statements("eu-central-1")[0]).toEqual({ + Sid: "BoxedAgentInferenceProfilesThroughGateway", + Effect: "Allow", + Action: ACTIONS, + Resource: ["arn:aws:bedrock:eu-central-1:*:inference-profile/*"], + Condition: { StringEquals: { "aws:ResourceAccount": "${aws:PrincipalAccount}" } }, + }); + }); + + test("allows a foundation model by its own ID only in the region it is given", () => { + expect(statements("eu-central-1")[1]).toEqual({ + Sid: "BoxedAgentModelsInRegionThroughGateway", + Effect: "Allow", + Action: ACTIONS, + Resource: ["arn:aws:bedrock:eu-central-1::foundation-model/*"], + }); + }); + + test("allows a foundation model in another region only through an inference profile of that account and region", () => { + expect(statements("eu-central-1")[2]).toEqual({ + Sid: "BoxedAgentModelsThroughInferenceProfiles", + Effect: "Allow", + Action: ACTIONS, + Resource: ["arn:aws:bedrock:*::foundation-model/*"], + Condition: { + StringLike: { + "bedrock:InferenceProfileArn": + "arn:aws:bedrock:eu-central-1:${aws:PrincipalAccount}:inference-profile/*", }, - ], + }, }); }); + + test("names the partition of the region it is given", () => { + const [profiles, inRegion, throughProfiles] = statements("cn-north-1"); + + expect(profiles!.Resource).toEqual(["arn:aws-cn:bedrock:cn-north-1:*:inference-profile/*"]); + expect(inRegion!.Resource).toEqual(["arn:aws-cn:bedrock:cn-north-1::foundation-model/*"]); + expect(throughProfiles!.Resource).toEqual(["arn:aws-cn:bedrock:*::foundation-model/*"]); + expect(throughProfiles!.Condition).toEqual({ + StringLike: { + "bedrock:InferenceProfileArn": + "arn:aws-cn:bedrock:cn-north-1:${aws:PrincipalAccount}:inference-profile/*", + }, + }); + expect(statements("us-gov-west-1")[1]!.Resource).toEqual([ + "arn:aws-us-gov:bedrock:us-gov-west-1::foundation-model/*", + ]); + }); }); describe("addBoxGatewayTargets", () => { diff --git a/src/core/project/boxMemory.ts b/src/core/project/boxMemory.ts index ce970bbb9..fe0533d70 100644 --- a/src/core/project/boxMemory.ts +++ b/src/core/project/boxMemory.ts @@ -1,4 +1,5 @@ import { isDeepStrictEqual } from "node:util"; +import { partition } from "@aws-sdk/util-endpoints"; import { InputValidationError } from "../../errors/errors"; import { BOX_GATEWAY_AGENTCORE_TARGET_NAME, @@ -32,6 +33,9 @@ export const BOX_BEDROCK_POLICY_FILE = "agentcore/box-bedrock-policy.json"; /** The Bedrock actions behind Converse and ConverseStream, which the agent's model calls make. */ const BEDROCK_INVOKE_ACTIONS = ["bedrock:InvokeModel", "bedrock:InvokeModelWithResponseStream"]; +/** The IAM policy variable for the account of the gateway role. */ +const PRINCIPAL_ACCOUNT = "${aws:PrincipalAccount}"; + /** The Memory data-plane actions the agent's session manager calls, as the L3 grants a runtime. */ const MEMORY_DATA_PLANE_ACTIONS = [ "bedrock-agentcore:ListMemoryRecords", @@ -86,9 +90,9 @@ function boxedRuntimesCallingBedrock(spec: ProjectSpec): ProjectRuntime[] { * no boxed runtime has its default Memory. The Memory ARN is known only after * deploy, so each resource matches the physical name the L3 gives the Memory, * `__-`, in any target, in `region`, the region of - * the `agentcore` target that build writes. One file serves every deploy target, so - * the ARN names no account, and the condition holds the Memory to the gateway role's - * own account. + * the `agentcore` target that build writes, and in the partition of `region`. One file + * serves every deploy target, so the ARN names no account, and the condition holds the + * Memory to the gateway role's own account. */ export function boxMemoryPolicy( spec: ProjectSpec, @@ -104,7 +108,8 @@ export function boxMemoryPolicy( Effect: "Allow", Action: MEMORY_DATA_PLANE_ACTIONS, Resource: memories.map( - (memory) => `arn:aws:bedrock-agentcore:${region}:*:memory/${spec.name}_*${memory}-*`, + (memory) => + `arn:${partition(region).name}:bedrock-agentcore:${region}:*:memory/${spec.name}_*${memory}-*`, ), Condition: { StringEquals: { "aws:ResourceAccount": "${aws:PrincipalAccount}" } }, }, @@ -114,22 +119,45 @@ export function boxMemoryPolicy( /** * The IAM policy document for {@link BOX_BEDROCK_POLICY_FILE}, or undefined when no boxed runtime - * calls a Bedrock model. A cross-region (`us.`, `global.`) inference profile invokes the model in - * other regions, so both the profiles and the foundation models match any region. + * calls a Bedrock model. Each ARN is in the partition of `region`, the region of the `bedrock` + * target that build writes. One file serves every deploy target, so the inference profile ARN + * names no account, and the condition holds the profile to the gateway role's own account. A + * cross-region (`us.`, `global.`) inference profile invokes the model in other regions, so a + * foundation model outside `region` matches only through an inference profile of that account in + * `region`. */ -export function boxBedrockPolicy(spec: ProjectSpec): Record | undefined { +export function boxBedrockPolicy( + spec: ProjectSpec, + region: string, +): Record | undefined { if (boxedRuntimesCallingBedrock(spec).length === 0) return undefined; + const bedrock = `arn:${partition(region).name}:bedrock`; return { Version: "2012-10-17", Statement: [ { - Sid: "BoxedAgentModelCallsThroughGateway", + Sid: "BoxedAgentInferenceProfilesThroughGateway", + Effect: "Allow", + Action: BEDROCK_INVOKE_ACTIONS, + Resource: [`${bedrock}:${region}:*:inference-profile/*`], + Condition: { StringEquals: { "aws:ResourceAccount": PRINCIPAL_ACCOUNT } }, + }, + { + Sid: "BoxedAgentModelsInRegionThroughGateway", + Effect: "Allow", + Action: BEDROCK_INVOKE_ACTIONS, + Resource: [`${bedrock}:${region}::foundation-model/*`], + }, + { + Sid: "BoxedAgentModelsThroughInferenceProfiles", Effect: "Allow", Action: BEDROCK_INVOKE_ACTIONS, - Resource: [ - "arn:aws:bedrock:*:*:inference-profile/*", - "arn:aws:bedrock:*::foundation-model/*", - ], + Resource: [`${bedrock}:*::foundation-model/*`], + Condition: { + StringLike: { + "bedrock:InferenceProfileArn": `${bedrock}:${region}:${PRINCIPAL_ACCOUNT}:inference-profile/*`, + }, + }, }, ], }; diff --git a/src/core/project/boxNetwork/network.test.ts b/src/core/project/boxNetwork/network.test.ts index 52d1f16d3..04e2b6dc9 100644 --- a/src/core/project/boxNetwork/network.test.ts +++ b/src/core/project/boxNetwork/network.test.ts @@ -1127,9 +1127,12 @@ describe("warnings", () => { expect(warningsFor(control)).toEqual([forService("egress-bedrock-control", "bedrock")]); }); - test("a Bedrock runtime target needs bedrock:InvokeModel, which the Bedrock file grants", () => { + test("a Bedrock runtime target needs bedrock:InvokeModel, which the Bedrock file grants in the build region only", () => { expect(warningsFor(runtime, [], [BEDROCK_FILE])).toEqual([]); expect(warningsFor(getBoxGatewayBedrockTargetSpec(), [], [BEDROCK_FILE])).toEqual([]); + expect(warningsFor(getBoxGatewayBedrockTargetSpec("us-east-1"), [], [BEDROCK_FILE])).toEqual([ + forHost("bedrock", "bedrock-runtime.us-east-1.amazonaws.com", "bedrock"), + ]); for (const granted of ["BEDROCK:invokemodel", "bedrock:Invoke*", "bedrock:InvokeMode?"]) { expect(warningsFor(runtime, [granted])).toEqual([]); } @@ -1161,5 +1164,29 @@ describe("warnings", () => { forService("egress-agentcore-control", "bedrock-agentcore"), ]); }); + + test("the Bedrock and Memory files grant the amazonaws.com.cn hosts of a build for China", () => { + const china = (name: string, host: string, service: string) => + passthrough(name, `https://${host}`, { + outboundAuth: { type: "GATEWAY_IAM_ROLE", service, region: "cn-north-1" }, + }); + const r = model( + "", + {}, + { + targets: [ + china("egress-models", "bedrock-runtime.cn-north-1.amazonaws.com.cn", "bedrock"), + china( + "egress-memory", + "bedrock-agentcore.cn-north-1.amazonaws.com.cn", + "bedrock-agentcore", + ), + ], + }, + ); + r.spec.agentCoreGateways[0]!.additionalPolicies = [BEDROCK_FILE, MEMORY_FILE]; + + expect(boxGatewayIamWarnings(r.spec, { ...r.network, region: "cn-north-1" })).toEqual([]); + }); }); }); diff --git a/src/core/project/boxNetwork/network.ts b/src/core/project/boxNetwork/network.ts index 534d8ba54..49d72b050 100644 --- a/src/core/project/boxNetwork/network.ts +++ b/src/core/project/boxNetwork/network.ts @@ -4,7 +4,10 @@ * the policy files to stage, and every warning and failure. Build, deploy, and staging write from * it. */ -import { boxGatewayAgentCoreHost } from "../../../handlers/project/shortcuts"; +import { + boxGatewayAgentCoreHost, + boxGatewayBedrockHost, +} from "../../../handlers/project/shortcuts"; import type { AgentCoreGateway, AgentCoreGatewayTarget } from "../../../projectSchemas/gateway"; import type { ProjectSpec } from "../../../projectSchemas/project"; import type { ProjectRuntime } from "../../../projectSchemas/runtime"; @@ -600,8 +603,9 @@ function sourceLabel(source: EntrySource): string { /** * A warning for each target of the network's Box gateway in `spec`, as build writes it, that the * gateway signs with its own role for an endpoint that no policy of that role allows. The Bedrock - * policy file that build writes allows the Bedrock runtime host, and the Memory policy file the - * Memory host; a gateway that imports its role is the user's to grant. + * policy file that build writes allows the Bedrock runtime host of the build region, and the Memory + * policy file the Memory host of that region; a gateway that imports its role is the user's to + * grant. */ export function boxGatewayIamWarnings( spec: ProjectSpec, @@ -625,7 +629,10 @@ export function boxGatewayIamWarnings( const allows = (api: AwsEndpointApi, host: string): boolean => { switch (api) { case "bedrock-runtime": - return bedrockListed || grants("bedrock:InvokeModel"); + return ( + (bedrockListed && host === boxGatewayBedrockHost(network.region)) || + grants("bedrock:InvokeModel") + ); case "bedrock-control": return granted.some(grantsBedrockControlPlane); case "bedrock-agentcore": diff --git a/src/core/project/boxNetworkWriters.test.ts b/src/core/project/boxNetworkWriters.test.ts index d63c5a501..c1d957ba7 100644 --- a/src/core/project/boxNetworkWriters.test.ts +++ b/src/core/project/boxNetworkWriters.test.ts @@ -313,6 +313,22 @@ describe("defaultTargetsWriter", () => { ]); }); + test("writes the Bedrock policy for the region of the bedrock target it writes", () => { + const edits = run([defaultTargetsWriter], [network({ region: "eu-central-1" })]); + + const bedrock = edits.spec.agentCoreGateways[0]!.targets.find(({ name }) => name === "bedrock"); + expect(bedrock?.outboundAuth?.region).toBe("eu-central-1"); + const policy = edits.files.find(({ path }) => path === BOX_BEDROCK_POLICY_FILE); + const resources = JSON.parse((policy as { text: string }).text).Statement.map( + ({ Resource }: { Resource: string[] }) => Resource, + ); + expect(resources).toEqual([ + ["arn:aws:bedrock:eu-central-1:*:inference-profile/*"], + ["arn:aws:bedrock:eu-central-1::foundation-model/*"], + ["arn:aws:bedrock:*::foundation-model/*"], + ]); + }); + test("writes the model and Memory targets for the region of the build", () => { const project = spec(); project.memories.push({ diff --git a/src/core/project/boxNetworkWriters.ts b/src/core/project/boxNetworkWriters.ts index cd8b28324..88bdbbe73 100644 --- a/src/core/project/boxNetworkWriters.ts +++ b/src/core/project/boxNetworkWriters.ts @@ -300,7 +300,10 @@ export const defaultTargetsWriter: BoxNetworkWriter = { return { spec: [(copy) => addBoxGatewayTargets(copy, region)], files: [ - { path: BOX_BEDROCK_POLICY_FILE, policy: boxBedrockPolicy(spec) }, + { + path: BOX_BEDROCK_POLICY_FILE, + policy: region === undefined ? undefined : boxBedrockPolicy(spec, region), + }, { path: BOX_MEMORY_POLICY_FILE, policy: region === undefined ? undefined : boxMemoryPolicy(spec, region), diff --git a/src/core/project/manager.boxNetwork.test.ts b/src/core/project/manager.boxNetwork.test.ts index 006c052d4..6a39cc9ee 100644 --- a/src/core/project/manager.boxNetwork.test.ts +++ b/src/core/project/manager.boxNetwork.test.ts @@ -1071,7 +1071,7 @@ describe("deploy writes the Box network before the backend deploys", () => { expect(await text(root, "agentcore/.env.local")).toMatch(/^AGENTCORE_CREDENTIAL_GITHUB=./m); }); - test("moves the Memory policy with the agentcore target to the region of its target", async () => { + test("moves the Memory and Bedrock policies with their targets to the region of the targets", async () => { const backend: ProjectBackend = { async *build() {}, async *deploy() { @@ -1088,8 +1088,12 @@ describe("deploy writes the Box network before the backend deploys", () => { const { manager, root } = await strandsProject({ backends: { CDK: backend } }); const memoryStatements = async () => JSON.parse(await text(root, "agentcore/box-memory-policy.json")).Statement as unknown[]; - const agentcoreAuth = async () => - (await boxGateway(root)).targets.find(({ name }) => name === "agentcore")?.outboundAuth; + const bedrockResources = async () => + JSON.parse(await text(root, "agentcore/box-bedrock-policy.json")).Statement.map( + ({ Resource }: { Resource: string[] }) => Resource, + ); + const authOf = async (target: string) => + (await boxGateway(root)).targets.find(({ name }) => name === target)?.outboundAuth; const statementsFor = (region: string) => [ expect.objectContaining({ Resource: [ @@ -1098,9 +1102,16 @@ describe("deploy writes the Box network before the backend deploys", () => { Condition: { StringEquals: { "aws:ResourceAccount": "${aws:PrincipalAccount}" } }, }), ]; + const bedrockFor = (region: string) => [ + [`arn:aws:bedrock:${region}:*:inference-profile/*`], + [`arn:aws:bedrock:${region}::foundation-model/*`], + ["arn:aws:bedrock:*::foundation-model/*"], + ]; await build(manager, root); - expect(await agentcoreAuth()).toMatchObject({ region: "us-west-2" }); + expect(await authOf("agentcore")).toMatchObject({ region: "us-west-2" }); + expect(await authOf("bedrock")).toMatchObject({ region: "us-west-2" }); expect(await memoryStatements()).toEqual(statementsFor("us-west-2")); + expect(await bedrockResources()).toEqual(bedrockFor("us-west-2")); await writeFile( path(root, "agentcore/aws-targets.json"), JSON.stringify([{ name: "primary", account: "111122223333", region: "eu-west-1" }]), @@ -1115,7 +1126,9 @@ describe("deploy writes the Box network before the backend deploys", () => { }), ); - expect(await agentcoreAuth()).toMatchObject({ region: "eu-west-1" }); + expect(await authOf("agentcore")).toMatchObject({ region: "eu-west-1" }); + expect(await authOf("bedrock")).toMatchObject({ region: "eu-west-1" }); expect(await memoryStatements()).toEqual(statementsFor("eu-west-1")); + expect(await bedrockResources()).toEqual(bedrockFor("eu-west-1")); }); }); diff --git a/src/handlers/project/shortcuts.test.ts b/src/handlers/project/shortcuts.test.ts index a262229cf..f22c389bb 100644 --- a/src/handlers/project/shortcuts.test.ts +++ b/src/handlers/project/shortcuts.test.ts @@ -2,6 +2,8 @@ import { describe, expect, test } from "bun:test"; import { EMPTY_TEMPLATE_NAME, formatTemplateParameterHelp, + getBoxGatewayAgentCoreTargetSpec, + getBoxGatewayBedrockTargetSpec, getDefaultMemorySpec, PROJECT_TEMPLATE_NAMES, RUNTIME_TEMPLATE_SHORTCUT_NAMES, @@ -84,3 +86,25 @@ test("default memory names fit the service limit for long runtime names", () => expect(memory.name).toBe(`${"a".repeat(42)}Memory`); expect(memory.name).toHaveLength(48); }); + +describe("Box gateway targets", () => { + test("take the DNS suffix of the partition of the region", () => { + const endpoints = (region: string) => + [getBoxGatewayBedrockTargetSpec(region), getBoxGatewayAgentCoreTargetSpec(region)].map( + ({ passthrough }) => passthrough?.endpoint, + ); + + expect(endpoints("cn-north-1")).toEqual([ + "https://bedrock-runtime.cn-north-1.amazonaws.com.cn", + "https://bedrock-agentcore.cn-north-1.amazonaws.com.cn", + ]); + expect(endpoints("us-gov-west-1")).toEqual([ + "https://bedrock-runtime.us-gov-west-1.amazonaws.com", + "https://bedrock-agentcore.us-gov-west-1.amazonaws.com", + ]); + expect(endpoints("us-west-2")).toEqual([ + "https://bedrock-runtime.us-west-2.amazonaws.com", + "https://bedrock-agentcore.us-west-2.amazonaws.com", + ]); + }); +}); diff --git a/src/handlers/project/shortcuts.ts b/src/handlers/project/shortcuts.ts index 4fc90438e..a1140608c 100644 --- a/src/handlers/project/shortcuts.ts +++ b/src/handlers/project/shortcuts.ts @@ -1,3 +1,4 @@ +import { partition } from "@aws-sdk/util-endpoints"; import z from "zod"; import { DEFAULT_EPISODIC_REFLECTION_NAMESPACE_TEMPLATES, @@ -38,6 +39,11 @@ export const DEFAULT_EGRESS_BEDROCK_REGION = "us-west-2"; /** The name of the Box gateway's Bedrock model target, which build and deploy add. */ export const BOX_GATEWAY_BEDROCK_TARGET_NAME = "bedrock"; +/** The Bedrock runtime host of `region`, which the Box gateway's Bedrock model target fronts. */ +export function boxGatewayBedrockHost(region: string = DEFAULT_EGRESS_BEDROCK_REGION): string { + return `bedrock-runtime.${region}.${partition(region).dnsSuffix}`; +} + /** The Box gateway's passthrough target for the Bedrock model endpoint, signed with its role. */ export function getBoxGatewayBedrockTargetSpec( region: string = DEFAULT_EGRESS_BEDROCK_REGION, @@ -45,10 +51,7 @@ export function getBoxGatewayBedrockTargetSpec( return { name: BOX_GATEWAY_BEDROCK_TARGET_NAME, targetType: "passthrough", - passthrough: { - endpoint: `https://bedrock-runtime.${region}.amazonaws.com`, - protocolType: "CUSTOM", - }, + passthrough: { endpoint: `https://${boxGatewayBedrockHost(region)}`, protocolType: "CUSTOM" }, outboundAuth: { type: "GATEWAY_IAM_ROLE", service: "bedrock", region }, }; } @@ -61,7 +64,7 @@ export const BOX_GATEWAY_AGENTCORE_TARGET_NAME = "agentcore"; /** The AgentCore data-plane host of `region`, which the Box gateway's AgentCore target fronts. */ export function boxGatewayAgentCoreHost(region: string = DEFAULT_EGRESS_BEDROCK_REGION): string { - return `bedrock-agentcore.${region}.amazonaws.com`; + return `bedrock-agentcore.${region}.${partition(region).dnsSuffix}`; } /**