Skip to content
Open
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
4 changes: 3 additions & 1 deletion src/handlers/gateway/invoke/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
resolveGatewayInvokeSources,
resolveGatewayInvokeTuiBearerToken,
} from "./request";
import { writeGatewayInvokeResponse } from "./response";
import { gatewayAccessDeniedHint, writeGatewayInvokeResponse } from "./response";
import { GatewayInvokeLaunchContextKey } from "./launchContext";

export const invokeGatewayFlags = [
Expand Down Expand Up @@ -134,6 +134,8 @@ export const createInvokeGatewayHandler = (
beforeOutput,
});
if (response.statusCode < 200 || response.statusCode >= 300) {
const hint = gatewayAccessDeniedHint(response.statusCode, gateway.authorizerType);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Nit: this fires for every 403 from any AWS_IAM gateway, Box or not, because the command only sees the live GetGateway response. The wording is conditional, so this is fine. If you want the Box sentence only for Box gateways, you could check whether the local project has a bound gateway whose deployed ID matches --id.

if (hint) io.stderr.write(`${hint}\n`);
throw new GatewayInvokeResponseError(`HTTP ${response.statusCode}`);
}
};
Expand Down
38 changes: 38 additions & 0 deletions src/handlers/gateway/invoke/invoke.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,44 @@ describe("gateway invoke", () => {
expect(output.stderr()).toHaveLength(0);
});

test("explains an HTTP 403 from an AWS_IAM Gateway", async () => {
const core = configuredCore({ authorizerType: "AWS_IAM" });
core.gateway.setInvokeResponse({
statusCode: 403,
contentType: "application/json",
requestId: "request-403",
body: body(Buffer.from('{"message":"AccessDeniedException"}')),
});
const output = captureIO();

const code = await runWithExitCode(async () =>
runCommand(core, output.io, ["gateway", "invoke", "--id", GATEWAY_ID, "--payload", "{}"]),
);

expect(code).toBe(ExitCode.FAILURE);
expect(output.stderr().toString()).toContain(
"If this is a Box Gateway (bound to one runtime in a Box project), its resource policy " +
"lets only that runtime's execution role invoke it.",
);
});

test("adds no access hint to an HTTP 403 from a Gateway without AWS_IAM", async () => {
const core = configuredCore();
core.gateway.setInvokeResponse({
statusCode: 403,
contentType: "application/json",
requestId: "request-403",
body: body(Buffer.from('{"message":"Forbidden"}')),
});
const output = captureIO();

await runWithExitCode(async () =>
runCommand(core, output.io, ["gateway", "invoke", "--id", GATEWAY_ID, "--payload", "{}"]),
);

expect(output.stderr().toString()).not.toContain("Box Gateway");

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Nit: consider also asserting ExitCode.FAILURE here, and adding a small gatewayAccessDeniedHint unit test for CUSTOM_JWT/403 and AWS_IAM/401 or 500, both of which should return undefined.

});

test("streams a non-2xx body before returning a failure exit code", async () => {
const core = configuredCore();
core.gateway.setInvokeResponse({
Expand Down
18 changes: 18 additions & 0 deletions src/handlers/gateway/invoke/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,24 @@ function summary(
);
}

/**
* The hint for an HTTP 403 from an AWS_IAM Gateway. In a Box project, each Box Gateway has a
* resource policy that denies every principal except its runtime's execution role, so even an
* administrator gets AccessDenied.
*/
export function gatewayAccessDeniedHint(
statusCode: number,
authorizerType: string | undefined,
): string | undefined {
if (statusCode !== 403 || authorizerType !== "AWS_IAM") return undefined;
return (
"Access denied. If this is a Box Gateway (bound to one runtime in a Box project), its " +

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Nit: the hint says what fails but not how to test the gateway. You could add a clause like "To exercise it, invoke the runtime (agentcore invoke); the runtime's code calls the Gateway with its execution role." I would leave out "assume the role": the execution role usually trusts only the service principal, and #476's ArnNotEquals aws:PrincipalArn means only that role gets through.

"resource policy lets only that runtime's execution role invoke it. Your credentials " +
"cannot invoke it, even with administrator access. Otherwise, check that your " +
"credentials allow bedrock-agentcore:InvokeGateway on this Gateway."
);
}

export async function writeGatewayInvokeResponse(
response: GatewayInvokeResponse,
output: StreamingResponseOutput,
Expand Down
9 changes: 8 additions & 1 deletion src/handlers/gateway/invoke/screen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { coreOptsFromCtx } from "../../utils";
import type { GatewayInvokeRequest, GatewayInvokeResponse } from "../types";
import { GatewayInvokeLaunchContextKey, type GatewayInvokeLaunchContext } from "./launchContext";
import { normalizeGatewayInvokeRequest } from "./request";
import { gatewayAccessDeniedHint } from "./response";

const theme = darkTheme;
const ACCEPT = "application/json, text/event-stream, */*;q=0.1";
Expand Down Expand Up @@ -320,7 +321,13 @@ export function GatewayInvokeConsole({
if (response.mcpSessionId) setMcpSessionId(response.mcpSessionId);
if (response.mcpProtocolVersion) setMcpProtocolVersion(response.mcpProtocolVersion);
updateExchange({
...(success ? {} : { note: `HTTP ${response.statusCode}` }),
...(success
? {}
: {
note:
gatewayAccessDeniedHint(response.statusCode, detail.data?.authorizerType) ??
`HTTP ${response.statusCode}`,
}),
state: success ? "complete" : "failed",
});
} catch (error) {
Expand Down
21 changes: 21 additions & 0 deletions src/projectSchemas/project.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,27 @@ describe("gateway boundRuntime and discoveryPrefix", () => {
test.each(["my-prefix", "", "1BOX"])("rejects discoveryPrefix %j", (discoveryPrefix) => {
expect(parse({ boundRuntime: "agent", discoveryPrefix }).success).toBe(false);
});

test("rejects a bound NONE gateway in a Box project, and accepts one outside it", () => {
const spec = (projectType?: string) => ({
...minimalProject,
...(projectType && { projectType }),
runtimes: [runtime],
agentCoreGateways: [gateway({ boundRuntime: "agent", authorizerType: "NONE" })],
});
const box = ProjectSpecSchema.safeParse(spec("box"));
expect(box.success).toBe(false);
expect(box.error?.issues.map(({ message }) => message)).toContain(
'Gateway "agent-box-egress" has boundRuntime "agent" in a Box project and authorizerType "NONE". Anyone with its URL could call it and use its credentials. Use "AWS_IAM".',
);
expect(ProjectSpecSchema.safeParse(spec()).success).toBe(true);
expect(
ProjectSpecSchema.safeParse({
...spec("box"),
agentCoreGateways: [gateway({ boundRuntime: "agent" })],
}).success,
).toBe(true);
});
});

describe("Box project gateways", () => {
Expand Down
13 changes: 13 additions & 0 deletions src/projectSchemas/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,19 @@ export const ProjectSpecSchema = z
path: ["agentCoreGateways"],
});
}
// A Box gateway holds its runtime's outbound credentials. With NONE, anyone who has its URL
// can call it and use them. The L3 schema has the same check.
if (
isBoxProject(spec) &&
gateway.boundRuntime !== undefined &&
gateway.authorizerType === "NONE"
) {
ctx.addIssue({
code: "custom",
message: `Gateway "${gateway.name}" has boundRuntime "${gateway.boundRuntime}" in a Box project and authorizerType "NONE". Anyone with its URL could call it and use its credentials. Use "AWS_IAM".`,
path: ["agentCoreGateways"],
});
}
}
// A Box project keeps one Box gateway for each runtime. Without it, a deploy would delete the
// deployed gateway, so every command stops here and prints the entry to add back.
Expand Down
Loading