From 555f19c69235c6aa1b57b91d1b563546ea5b94bf Mon Sep 17 00:00:00 2001 From: Sai Abhishek Date: Fri, 20 Nov 2026 14:49:38 -0800 Subject: [PATCH 1/3] fix(build): Keep the Bedrock grant of the Box gateway in its region and account ## Problem agentcore/box-bedrock-policy.json lets the role of the Box gateway use bedrock:InvokeModel and bedrock:InvokeModelWithResponseStream on all inference profiles and all foundation models, in all regions and in all accounts: arn:aws:bedrock:*:*:inference-profile/* arn:aws:bedrock:*::foundation-model/* The agent uses the model only through the `bedrock` target of the gateway. `build` writes that target for one region. The agent uses only the inference profiles of the account of the gateway. A code inspection found this problem. ## Solution boxBedrockPolicy(spec, region) gets the region of the `bedrock` target from the default targets writer. boxMemoryPolicy gets the region of the `agentcore` target from the same writer. When the writer has no region, it writes no Bedrock policy and no Memory policy. The policy has 3 statements: 1. BoxedAgentInferenceProfilesThroughGateway: the inference profiles in the region. One file is for all deploy targets, thus the ARN keeps "*" for the account. The condition of [e96cbdb9](https://github.com/aws/agentcore-cli/commit/e96cbdb9f782a51b6490ce779b6936af74e74067) keeps the inference profile in the account of the gateway role: "aws:ResourceAccount": "${aws:PrincipalAccount}". 2. BoxedAgentModelsInRegionThroughGateway: the foundation models in the region, when the agent names the model ID. 3. BoxedAgentModelsThroughInferenceProfiles: the foundation models in all regions, only when the agent uses an inference profile of that account in the region: "StringLike": {"bedrock:InferenceProfileArn": "arn:aws:bedrock::${aws:PrincipalAccount}:inference-profile/*"}. IAM does not accept that value in an ArnLike condition. CreatePolicy and the stack give "The policy failed legacy parsing". The Bedrock example in the AWS documents also uses StringLike. A `us.` or `global.` inference profile can send the model operation to other regions. For a `global.` inference profile, Bedrock also examines arn:aws:bedrock:::foundation-model/, which has no region. The "*" region agrees with that ARN. Each ARN has the partition of the region (aws, aws-cn, or aws-us-gov), from the partition data of the AWS SDK. boxTelemetryPolicy uses the same data. The policy file cannot contain the AWS::Partition value of the stack, because the L3 reads the file with PolicyDocument.fromJson, and that function does not replace such a value. boxGatewayIamWarnings counts the Bedrock file only for the Bedrock runtime host of the region that `build` writes. It counts the Memory file only for the Memory host of that region. Thus a target that signs Bedrock operations for a different region with the gateway role gets a warning, because the new policy does not let the role do these operations. The new boxGatewayBedrockHost(region) gives the host. The policy keeps the same 2 operations. The policy does not let the role use an `application-inference-profile`. The policy before this change did not let the role use one, and the CLI does not make one. ## Tests Before this change, each of these tests gives an incorrect result. After this change, each result is satisfactory: - boxMemory.test.ts, "boxBedrockPolicy": 1 test for each statement, and 1 test for the partition of cn-north-1 and us-gov-west-1. - boxNetworkWriters.test.ts, "writes the Bedrock policy for the region of the bedrock target it writes". - manager.boxNetwork.test.ts, "moves the Memory and Bedrock policies with their targets to the region of the targets". A deploy to a target in eu-west-1 moves the `bedrock` target from us-west-2, and the Bedrock policy moves with it. - network.test.ts, "a Bedrock runtime target needs bedrock:InvokeModel, which the Bedrock file grants in the build region only". Each of 9 mutations gives an incorrect result in 1 or more of these tests: no account condition, a wildcard region for the inference profiles, a wildcard region for the models, no inference profile condition, a wildcard account in that condition, ArnLike and not StringLike, a constant `arn:aws`, a constant region in the writer, and no host check in the warning. IAM: `aws iam simulate-custom-policy` on the us-west-2 policy gives `allowed` for these 7 items: the model in us-west-2 by its ID, the `us.` and `global.` inference profiles of the account in us-west-2, and the model in us-west-2, us-east-1, us-east-2, and in the region-less global ARN through such a profile. It gives `implicitDeny` for these 7 items: an inference profile of a different account, an inference profile in us-east-1, the model in us-east-1 and the global model with no inference profile, the model in us-east-1 through a profile of a different account or of us-east-1, and an `application-inference-profile`. `simulate-principal-policy` on the gateway role after the deploy gives the same 14 results. CreatePolicy accepts the policy. Live test in a dev account in us-west-2: a new project from the agent-python-strands-box-container template, with the L3 at [08f8b230](https://github.com/aws/agentcore-l3-cdk-constructs/commit/08f8b23002f9357b7607b9d073dfd77929629e6a). With the default model, global.anthropic.claude-sonnet-5-5, 6 invokes gave 391, 5555 (through the add_numbers tool), and Paris. Then amazon.nova-lite-v1:0 in `app/agent/model/load.py`, a deploy, and 3 invokes gave the same answers. CloudTrail shows 12 ConverseStream operations of the gateway role on these 2 model IDs, with no error code. The gateway log group shows 12 operations through the `bedrock` target, with no error. CloudTrail gives `"inferenceRegion": "us-west-2"` for each operation, thus the live test did not show a model operation in a different region. Only the simulation shows it. The teardown removed the stack and the 3 log groups that stay after the stack. Not done: `bun run test:e2e`, and a live test in aws-cn or aws-us-gov. `bun test` gives `5833 pass, 7 skip, 0 fail`. The typecheck, lint, and format checks are satisfactory. --- src/core/project/boxMemory.test.ts | 70 ++++++++++++++++----- src/core/project/boxMemory.ts | 43 ++++++++++--- src/core/project/boxNetwork/network.test.ts | 5 +- src/core/project/boxNetwork/network.ts | 15 +++-- src/core/project/boxNetworkWriters.test.ts | 16 +++++ src/core/project/boxNetworkWriters.ts | 5 +- src/core/project/manager.boxNetwork.test.ts | 23 +++++-- src/handlers/project/shortcuts.ts | 10 +-- 8 files changed, 150 insertions(+), 37 deletions(-) diff --git a/src/core/project/boxMemory.test.ts b/src/core/project/boxMemory.test.ts index 34fb1090b..acd28378c 100644 --- a/src/core/project/boxMemory.test.ts +++ b/src/core/project/boxMemory.test.ts @@ -182,7 +182,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 +195,65 @@ describe("addBoxBedrockTarget", () => { expect(targets(project)).toEqual(["bedrock"]); expect(gateway(project).additionalPolicies).toBeUndefined(); }); +}); + +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("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/*", - ], + 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..8842c4c24 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", @@ -114,22 +118,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..c0c31315f 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([]); } 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.ts b/src/handlers/project/shortcuts.ts index 4fc90438e..dabe02412 100644 --- a/src/handlers/project/shortcuts.ts +++ b/src/handlers/project/shortcuts.ts @@ -38,6 +38,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}.amazonaws.com`; +} + /** 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 +50,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 }, }; } From 0d8c8d387ff873f1a75378adfe9a1fc4e4b5edc2 Mon Sep 17 00:00:00 2001 From: Sai Abhishek Date: Fri, 20 Nov 2026 16:14:11 -0800 Subject: [PATCH 2/3] fix(build): Use the partition of the region in the Memory policy of the Box gateway The comment of leo237 on boxMemory.ts: boxMemoryPolicy wrote "arn:aws:bedrock-agentcore:" for each region. Thus `build` for cn-north-1 or us-gov-west-1 wrote a Bedrock policy with the correct partition, and a Memory policy with an incorrect partition. boxMemoryPolicy gets the partition from the region, with the partition data of the AWS SDK, the same as boxBedrockPolicy. Test: boxMemory.test.ts, "boxMemoryPolicy" > "names the partition of the region it is given", for cn-north-1 (aws-cn) and us-gov-west-1 (aws-us-gov). Before this change, the result of this test is incorrect. --- src/core/project/boxMemory.test.ts | 12 ++++++++++++ src/core/project/boxMemory.ts | 9 +++++---- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/core/project/boxMemory.test.ts b/src/core/project/boxMemory.test.ts index acd28378c..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(); diff --git a/src/core/project/boxMemory.ts b/src/core/project/boxMemory.ts index 8842c4c24..fe0533d70 100644 --- a/src/core/project/boxMemory.ts +++ b/src/core/project/boxMemory.ts @@ -90,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, @@ -108,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}" } }, }, From 0b0b2b3189c8d5a5cbfa030f2017dec213a89ff0 Mon Sep 17 00:00:00 2001 From: Sai Abhishek Date: Fri, 20 Nov 2026 16:18:46 -0800 Subject: [PATCH 3/3] fix(build): Use the DNS suffix of the partition in the Box gateway hosts The comment of leo237 on shortcuts.ts: boxGatewayBedrockHost wrote "bedrock-runtime..amazonaws.com" for each region, and boxGatewayAgentCoreHost wrote "bedrock-agentcore..amazonaws.com". In the aws-cn partition, the DNS suffix is "amazonaws.com.cn". Thus, for `build` in cn-north-1, the `bedrock` and `agentcore` targets had an incorrect endpoint. Also, boxGatewayIamWarnings compared a target host with an incorrect host, and gave a warning for a correct China host. This problem was on box-integration before this PR. The 2 functions get the DNS suffix from the partition data of the AWS SDK, as boxBedrockPolicy gets the partition. Tests. Before this change, the result of each test is incorrect: - shortcuts.test.ts, "Box gateway targets" > "take the DNS suffix of the partition of the region": cn-north-1 gives amazonaws.com.cn, us-gov-west-1 and us-west-2 give amazonaws.com. - network.test.ts, "the Bedrock and Memory files grant the amazonaws.com.cn hosts of a build for China": no warning. A constant "amazonaws.com" in each of the 2 functions gives an incorrect result in the 2 tests. --- src/core/project/boxNetwork/network.test.ts | 24 +++++++++++++++++++++ src/handlers/project/shortcuts.test.ts | 24 +++++++++++++++++++++ src/handlers/project/shortcuts.ts | 5 +++-- 3 files changed, 51 insertions(+), 2 deletions(-) diff --git a/src/core/project/boxNetwork/network.test.ts b/src/core/project/boxNetwork/network.test.ts index c0c31315f..04e2b6dc9 100644 --- a/src/core/project/boxNetwork/network.test.ts +++ b/src/core/project/boxNetwork/network.test.ts @@ -1164,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/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 dabe02412..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, @@ -40,7 +41,7 @@ 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}.amazonaws.com`; + return `bedrock-runtime.${region}.${partition(region).dnsSuffix}`; } /** The Box gateway's passthrough target for the Bedrock model endpoint, signed with its role. */ @@ -63,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}`; } /**