Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 68 additions & 14 deletions src/core/project/boxMemory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
}
});

Expand All @@ -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<string, unknown> & { 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", () => {
Expand Down
52 changes: 40 additions & 12 deletions src/core/project/boxMemory.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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,
* `<project>_<target>_<memory>-<suffix>`, 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,
Expand All @@ -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}" } },
},
Expand All @@ -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<string, unknown> | undefined {
export function boxBedrockPolicy(
spec: ProjectSpec,
region: string,
): Record<string, unknown> | undefined {
if (boxedRuntimesCallingBedrock(spec).length === 0) return undefined;
const bedrock = `arn:${partition(region).name}:bedrock`;
Comment thread
SaiAbhishek98 marked this conversation as resolved.
Resolved
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/*`,
},
},
},
],
};
Expand Down
29 changes: 28 additions & 1 deletion src/core/project/boxNetwork/network.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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([]);
}
Expand Down Expand Up @@ -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([]);
});
});
});
15 changes: 11 additions & 4 deletions src/core/project/boxNetwork/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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,
Expand All @@ -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":
Expand Down
16 changes: 16 additions & 0 deletions src/core/project/boxNetworkWriters.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
5 changes: 4 additions & 1 deletion src/core/project/boxNetworkWriters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
23 changes: 18 additions & 5 deletions src/core/project/manager.boxNetwork.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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: [
Expand All @@ -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" }]),
Expand All @@ -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"));
});
});
Loading
Loading