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
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Build stopped: 10 problems in the Box profile and agentcore.json. Build wrote no
.box/policy.dw:7:1 This rule allows requests to every host, and each gateway target needs one fixed host. Add a condition that names each host, for example context.input.host == "api.example.com".
.box/profile.toml:4:1 The rule "pack_mirrors", which a pack in `packs` adds, allows each host that matches "*.mirror.example.com", and each gateway target needs one fixed host. Name each host in the rule, for example context.input.host == "api.example.com".
.box/profile.toml:4:1 [egress.legacy], which a pack in `packs` adds, reads its secret from credsd://prod, and the gateway cannot read credsd. Use an env:// secret, which build stores as an API key credential.
.box/profile.toml:40:1 [egress.signed] signs requests to api.example.com with aws://default, and the gateway signs only for Bedrock, AgentCore, and Lambda function URL hosts. Use an env:// secret for this host, or remove the secret.
.box/profile.toml:44:1 [egress.wide] has the wildcard destination "*.wide.example.com", and each gateway target needs one fixed host. Name each host in `destinations`.
.box/profile.toml:48:1 [mcp.docs] has the destination "docs.example.com/mcp", and the gateway route of an MCP server takes a host with no path. Remove /mcp from the destination. The agent's MCP client keeps the path in its URL.
.box/profile.toml:47:1 [egress.signed] signs requests to api.example.com with aws://default, and the gateway signs only for Bedrock, AgentCore, and Lambda function URL hosts. Use an env:// secret for this host, or remove the secret.
.box/profile.toml:51:1 [egress.wide] has the wildcard destination "*.wide.example.com", and each gateway target needs one fixed host. Name each host in `destinations`.
.box/profile.toml:55:1 [mcp.docs] has the destination "docs.example.com/mcp", and the gateway route of an MCP server takes a host with no path. Remove /mcp from the destination. The agent's MCP client keeps the path in its URL.
agentcore/agentcore.json The gateway target 'gh' for api.github.com sends the API key credential 'github' in the header Authorization with the prefix Bearer, and [egress.github] sends no credential. Make the two the same, in agentcore.json or in .box/profile.toml.
10 changes: 8 additions & 2 deletions src/core/project/manager.boxNetwork.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,10 @@ const gatewayRoute = (target: string, host: string) => ({
backends: [
{
url: `\${{ inputs.gateway.url }}/${target}`,
auth: { ref: "aws://", aws: { service: "bedrock-agentcore", region: "us-west-2" } },
auth: {
ref: "aws://",
aws: { service: "bedrock-agentcore", region: "${{ inputs.aws.region }}" },
},
},
],
});
Expand Down Expand Up @@ -843,7 +846,10 @@ describe("build writes the Bedrock and Memory targets for the region it builds f
expect(profile.options).toEqual({ bedrock: { region: "eu-central-1" } });
const egress = profile.egress as Record<string, { destinations: string[]; backends: unknown }>;
expect(egress.bedrock!.destinations).toEqual(["bedrock-runtime.eu-central-1.amazonaws.com"]);
expect(JSON.stringify(egress.bedrock!.backends)).toContain('"region":"eu-central-1"');
// The backend signs for the Runtime's own region, which box reads from AWS_REGION there.
expect(JSON.stringify(egress.bedrock!.backends)).toContain(
'"region":"${{ inputs.aws.region }}"',
);
});

test("has box resolve the profile for that region, from a copy it then removes", async () => {
Expand Down
30 changes: 29 additions & 1 deletion src/core/project/manager.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { afterEach, describe, expect, test } from "bun:test";
import { existsSync } from "node:fs";
import { existsSync, readdirSync } from "node:fs";
import { mkdir, mkdtemp, readdir, readFile, rm, writeFile } from "node:fs/promises";
import { writeMigrationState } from "./migrate/journal";
import { join, relative } from "node:path";
Expand Down Expand Up @@ -624,6 +624,34 @@ describe("FsProjectManager.create", () => {
expect(skipInstall.checkedTools).toEqual(["git"]);
});

test("with --skip-install, makes an empty .venv only for a CodeZip box agent", async () => {
const directory = await inTempDirectory();
const venv = (name: string, runtime: { runtimeName: string }) =>
join(directory, name, "app", runtime.runtimeName, ".venv");

// box drops the profile's optional .venv deny_write while the folder is missing, and then
// refuses the profile, as the venv's Python would be under the project's write grant.
await runCreate(manager().manager, {
name: "codezip",
scaffoldRuntimeInput: AGENT_PYTHON_STRANDS_BOX,
skipInstall: true,
});
expect(readdirSync(venv("codezip", AGENT_PYTHON_STRANDS_BOX))).toEqual([]);

// A container's image builds its own venv, and an agent outside a box has no profile.
for (const [name, runtime] of [
["container", AGENT_PYTHON_STRANDS_BOX_CONTAINER],
["plain", AGENT_PYTHON],
] as const) {
await runCreate(manager().manager, {
name,
scaffoldRuntimeInput: runtime,
skipInstall: true,
});
expect(existsSync(venv(name, runtime))).toBe(false);
}
});

test("fails before writing files when box is not on PATH, with the install hint", async () => {
const directory = await inTempDirectory();
const subject = new FsProjectManager({
Expand Down
9 changes: 9 additions & 0 deletions src/core/project/manager.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,15 @@ export class FsProjectManager implements ProjectManager {
const appDir = join(destination, "app", scaffoldRuntimeInput.runtimeName);
yield* this.ensureLockFileExists(appDir);
}
if (input.skipInstall && scaffoldRuntimeInput && startsInBoxOnCodeZip(scaffoldRuntimeInput)) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small one: the empty .venv could be part of the template tree in withBoxStartScript, next to start.py, rather than a --skip-install branch in create. uv sync fills an existing empty .venv, and it keeps the box layout in one place.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I have this in later commit, will include it herer

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in #2886 (08fb943): the empty .venv is part of boxCodeZipFiles with start.py in templates/project.ts, which both create and add runtime write. The --skip-install branch in create and the copy in add runtime are gone.

// The box profile keeps the venv out of the project's write grant with an optional
// deny_write, which box drops while `.venv` is missing; box then refuses the profile, as the
// venv's Python would be writable. An empty `.venv` keeps the profile valid, and `uv sync`
// fills it later.
await mkdir(join(destination, "app", scaffoldRuntimeInput.runtimeName, ".venv"), {
recursive: true,
});
}

if (scaffoldRuntimeInput) {
yield* this.writeBoxProfile(destination, input.name, scaffoldRuntimeInput, input.region);
Expand Down
28 changes: 23 additions & 5 deletions src/core/project/templates/boxProfile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,15 @@ describe("renderBoxProfile", () => {
expect(parse(renderBoxProfile("V3Demo", STRANDS)!) as unknown).toEqual({
packs: ["box/base", "box/bedrock"],
profile: { name: "V3Demo" },
project: { access: "write" },
options: { bedrock: { region: "us-west-2" } },
agent: {
command: UVICORN,
env: { LOCAL_DEV: "1", PORT: "8081", PYTHONUNBUFFERED: "1", PYTHONUTF8: "1" },
filesystem: VENV_GRANTS,
ingress: [{ listen: "127.0.0.1:8080", workload_port: 8081 }],
},
tool: { uname: { filesystem: { deny_write: VENV_GRANTS.deny_write } } },
egress: { bedrock: { secret: { ref: "aws://default" } } },
});
});
Expand Down Expand Up @@ -374,6 +376,7 @@ describe("renderBoxProfile for AgentCore Runtime", () => {
expect(parse(text)).toEqual({
packs: ["box/base", "box/bedrock", "box/agentcore", "box/agentcore-codezip"],
profile: { name: "V3Demo" },
project: { access: "write" },
options: { bedrock: { region: "us-west-2" } },
agent: {
command: [
Expand Down Expand Up @@ -416,6 +419,7 @@ describe("renderBoxProfile for AgentCore Runtime", () => {
// The container reads the deployed resources from the environment through box/agentcore.
packs: ["box/base", "box/bedrock", "box/agentcore"],
profile: { name: "V3Demo" },
project: { access: "read" },
options: { bedrock: { region: "us-west-2" } },
agent: {
command: [
Expand Down Expand Up @@ -511,7 +515,11 @@ describe("renderRuntimeBoxProfile", () => {
backends: [
{
url: `\${{ inputs.gateway.url }}/${target}`,
auth: { ref: "aws://", aws: { service: "bedrock-agentcore", region: "us-west-2" } },
// Signed for the Runtime's own region, which box reads from AWS_REGION there.
auth: {
ref: "aws://",
aws: { service: "bedrock-agentcore", region: "${{ inputs.aws.region }}" },
},
},
],
});
Expand All @@ -528,7 +536,7 @@ describe("renderRuntimeBoxProfile", () => {
url: "${{ inputs.gateway.url }}/agentcore",
auth: {
ref: "aws://",
aws: { service: "bedrock-agentcore", region: "us-west-2" },
aws: { service: "bedrock-agentcore", region: "${{ inputs.aws.region }}" },
},
},
],
Expand Down Expand Up @@ -573,7 +581,7 @@ describe("renderRuntimeBoxProfile", () => {
});
});

test("signs each route for the gateway in the region build builds for", () => {
test("signs each route for the gateway in the Runtime's own region", () => {
const project = spec([], {}, [getBoxGatewayBedrockTargetSpec("eu-central-1")]);
const profile = parse(
renderRuntimeBoxProfile(project, project.runtimes[0]!, {
Expand All @@ -588,9 +596,10 @@ describe("renderRuntimeBoxProfile", () => {
backends: [
{
url: "${{ inputs.gateway.url }}/bedrock",
// The gateway is in the Runtime's region, which box reads from AWS_REGION there.
auth: {
ref: "aws://",
aws: { service: "bedrock-agentcore", region: "eu-central-1" },
aws: { service: "bedrock-agentcore", region: "${{ inputs.aws.region }}" },
},
},
],
Expand Down Expand Up @@ -702,7 +711,7 @@ describe("renderRuntimeBoxProfile", () => {
'secret.phantom_prefix = "tk-"\n\n' +
"[[mcp.tools.backends]]\n" +
'url = "${{ inputs.gateway.url }}/egress-tools"\nauth.ref = "aws://"\n' +
'auth.aws.service = "bedrock-agentcore"\nauth.aws.region = "us-west-2"\n',
'auth.aws.service = "bedrock-agentcore"\nauth.aws.region = "${{ inputs.aws.region }}"\n',
);
});

Expand Down Expand Up @@ -837,8 +846,17 @@ describe("startsInBoxOnCodeZip", () => {
test("starts only the CodeZip box template in a box", () => {
const plain = (name: RuntimeTemplateShortcutName) => resolveRuntimeTemplateShortcut(name);
expect(startsInBoxOnCodeZip(plain("agent-python-strands-box"))).toBe(true);
expect(startsInBoxOnCodeZip(plain("agent-python-minimal-box"))).toBe(true);
expect(startsInBoxOnCodeZip(plain("agent-python-minimal"))).toBe(false);
expect(startsInBoxOnCodeZip(plain("agent-python-langchain-box"))).toBe(true);
expect(startsInBoxOnCodeZip(plain("agent-python-langchain"))).toBe(false);
expect(startsInBoxOnCodeZip(plain("a2a-python-strands-box"))).toBe(true);
expect(startsInBoxOnCodeZip(plain("a2a-python-strands"))).toBe(false);
expect(startsInBoxOnCodeZip(plain("agui-python-strands-box"))).toBe(true);
expect(startsInBoxOnCodeZip(plain("agui-python-strands"))).toBe(false);
expect(startsInBoxOnCodeZip(plain("agent-python-strands"))).toBe(false);
expect(startsInBoxOnCodeZip(plain("mcp-python-fastmcp"))).toBe(false);
expect(startsInBoxOnCodeZip(plain("mcp-python-fastmcp-box"))).toBe(true);
expect(startsInBoxOnCodeZip(plain("agent-python-strands-box-container"))).toBe(false);
expect(startsInBoxOnCodeZip(plain("agent-typescript-strands"))).toBe(false);
});
Expand Down
61 changes: 39 additions & 22 deletions src/core/project/templates/boxProfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,14 @@ const RUNTIME_AWS_STAND_IN_LINES = [
'AWS_SECRET_ACCESS_KEY = "box-stand-in"',
].join("\n");

/**
* The tools of box/base that get the project's write grant: box gives each one its own grant, so
* each lists the agent's `deny_write` folders as well. Taken from box/base in the box release that
* `boxRelease.json` pins (0.1.11-linux-preview.6). When the pin moves, check box/base's tools: box
* refuses a CodeZip box profile if a tool it misses gets the project write grant.
*/
const BOX_BASE_PROJECT_WRITE_TOOLS = ["uname"];
Comment thread
mquang-amazon marked this conversation as resolved.
Resolved

type Protocol = NonNullable<ScaffoldRuntimeInput["protocol"]>;

/**
Expand Down Expand Up @@ -443,6 +451,12 @@ export function renderBoxProfile(
const workloadPort = boxWorkloadPort(protocol);
const onRuntime = target === "runtime";
const container = isBoxedContainer(runtime);
// The image's agent reads /app and writes nothing there. Build resolves the profile on the host,
// where the image's /app/.venv does not exist, so a write grant would leave its Python unprotected
// and box would refuse the profile. A CodeZip agent writes in the folder box starts in.
const projectSection = container

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Every local CodeZip profile now gets [project] access = "write", and under agentcore dev box starts in the project root, so the agent can read and write the whole project. That includes agentcore/.env.local with the real model provider key, which the keyed route is meant to keep away from it, and .git/hooks, agentcore/cdk/ and agentcore.json, which would run outside the box on the next commit or deploy. A sandboxed Python process with the pinned box release can read a dummy credential from that file. Could this be "read" like the container profile, with agentcore/ and .git in deny_read and deny_write, or start box in app/<runtime> instead? A check that the agent can't read .env.local would keep it that way.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would do start box in app/runtime instead, since ever read can still have .env.local. THis is simpler

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed on box-integration: agentcore dev now starts box in the runtime's folder, app/<runtime>, so the [project] access = "write" grant covers only the code folder, not agentcore/.env.local, .git or agentcore/cdk. box.test.ts:192 checks the start folder.

? '# The agent, and the tools its packs add, can read the folder box starts in.\n[project]\naccess = "read"\n'
: '# The agent, and the tools its packs add, can read and write the folder box starts in.\n[project]\naccess = "write"\n';
const codeZipOnRuntime = onRuntime && !container;
const runner = container
? containerRunner(workloadPort, !onRuntime)
Expand Down Expand Up @@ -479,6 +493,14 @@ export function renderBoxProfile(
const execComment = runner.execComment ?? "";
const denyWrite =
runner.denyWrite.length > 0 ? `deny_write = [${runner.denyWrite.join(", ")}]\n` : "";
// box refuses to run a program from a folder that any process in the box can write, so each
// tool that gets the project's write grant lists the venv too.
const toolDenyWrite =
!container && runner.denyWrite.length > 0
? BOX_BASE_PROJECT_WRITE_TOOLS.map(
(tool) => `\n[tool.${tool}.filesystem]\ndeny_write = [${runner.denyWrite.join(", ")}]\n`,
).join("")
: "";
// Only the Runtime agent calls AgentCore: locally, LOCAL_DEV=1 skips Identity, and no memory ID
// reaches the box, so Memory is off.
const routes = onRuntime ? runtimeRoutes : "";
Expand Down Expand Up @@ -520,6 +542,7 @@ ${policies}
[profile]
name = ${str(projectName)}

${projectSection}
${options}[agent]
command = ${list(runner.command)}

Expand All @@ -533,28 +556,23 @@ ${denyWrite}
[[agent.ingress]]
listen = ${str(`${listenHost}:${ingressPort}`)}
workload_port = ${workloadPort}
${routes}${bedrockRoute}${keyedRoute}`;
${toolDenyWrite}${routes}${bedrockRoute}${keyedRoute}`;
}

/**
* A route the agent's calls take on the Runtime: the box sends calls to `host`
* to the runtime's Box gateway target `target`, signing them for the gateway in
* `region` with the execution role; the gateway calls `host` with the target's
* the Runtime's region with the execution role; the gateway calls `host` with the target's
* own auth. Every `[egress.<name>]` on the Runtime is one of these, so every
* outbound call goes through the Box gateway. Starts with a blank line.
*/
function runtimeGatewayRouteSection(
gatewayName: string,
target: string,
host: string,
region: string,
): string {
function runtimeGatewayRouteSection(gatewayName: string, target: string, host: string): string {
const key = tomlKey(target);
return `
# Calls to ${host}, through the '${gatewayName}' gateway's '${target}' target.
[egress.${key}]
destinations = [${str(host)}]
${runtimeGatewayBackend(`egress.${key}`, target, region)}`;
${runtimeGatewayBackend(`egress.${key}`, target)}`;
}

/** An HTTP MCP server of the box profile that a Box gateway target serves. */
Expand All @@ -574,7 +592,6 @@ function runtimeMcpServerSection(
server: RuntimeMcpServer,
target: string,
host: string,
region: string,
): string {
const key = tomlKey(server.name);
const secret = server.secret?.ref.startsWith("env://")
Expand All @@ -588,23 +605,23 @@ function runtimeMcpServerSection(
[mcp.${key}]
type = "http"
destinations = [${str(host)}]
${secret}${runtimeGatewayBackend(`mcp.${key}`, target, region)}`;
${secret}${runtimeGatewayBackend(`mcp.${key}`, target)}`;
}

/**
* The backend of a Runtime route or MCP server: the runtime's Box gateway
* target, signed for the gateway in `region` with the execution role. Both
* Runtime profiles read the gateway URL from the environment through the
* box/agentcore pack's input, and sign through a bare `aws://`, so no start
* script fills the profile in. Starts with a blank line.
* target. Both Runtime profiles read the gateway URL and the region from the
* environment through the agentcore pack's inputs, and sign with the execution
* role through a bare `aws://`, so the backend names no region. Starts with a
* blank line.
*/
function runtimeGatewayBackend(table: string, target: string, region: string): string {
function runtimeGatewayBackend(table: string, target: string): string {
return `
[[${table}.backends]]
url = ${str(`\${{ inputs.gateway.url }}/${target}`)}
auth.ref = ${str(RUNTIME_AWS_SIGNER)}
auth.aws.service = "bedrock-agentcore"
auth.aws.region = ${str(region)}
auth.aws.region = "\${{ inputs.aws.region }}"
`;
}

Expand All @@ -619,7 +636,6 @@ auth.aws.region = ${str(region)}
function runtimeGatewayRoutes(
spec: ProjectSpec,
runtime: ProjectRuntime,
region: string,
servers: ReadonlyMap<string, RuntimeMcpServer>,
): string {
if (!isBoxProject(spec)) return "";
Expand All @@ -646,8 +662,8 @@ function runtimeGatewayRoutes(
.map((target) => {
const server = servers.get(target.name);
return server
? runtimeMcpServerSection(gateway.name, server, target.name, hostOf(target), region)
: runtimeGatewayRouteSection(gateway.name, target.name, hostOf(target), region);
? runtimeMcpServerSection(gateway.name, server, target.name, hostOf(target))
: runtimeGatewayRouteSection(gateway.name, target.name, hostOf(target));
})
.join("");
}
Expand Down Expand Up @@ -721,7 +737,7 @@ export function renderRuntimeBoxProfile(
input,
region,
"runtime",
runtimeGatewayRoutes(spec, runtime, region, runtimeMcpServers(network?.hosts ?? [])),
runtimeGatewayRoutes(spec, runtime, runtimeMcpServers(network?.hosts ?? [])),
network?.policyFiles,
)!;
return profile + (network?.telemetry ?? []).map(runtimeTelemetrySection).join("");
Expand Down Expand Up @@ -812,7 +828,8 @@ description = "The runtime's Box gateway URL."
from = ${str(`env:${BOX_GATEWAY_URL_ENV}`)}
pattern = ${str(GATEWAY_URL_PATTERN)}

# The Runtime's region. The Runtime sets AWS_REGION; the profile passes it to the agent.
# The Runtime's region, which box signs each Box gateway backend for and passes to the agent. The
# Runtime sets AWS_REGION.
[inputs.aws.region]
description = "The AWS region the runtime runs in."
from = "env:AWS_REGION"
Expand Down
Loading
Loading