Repository navigation
feat(box): Let a Box gateway call only the Bedrock model of its runtime - #2911
Conversation
The Box gateway's role could invoke every inference profile and every foundation model of the account: agentcore/box-bedrock-policy.json named `inference-profile/*` and `foundation-model/*`. - create records the model a box runtime's code calls in the runtime's existing `modelId` (the --model-id, or the template default), as it already does for LiteLLM. The minimal template calls no model and gets none. The LangChain template now renders `modelId` into model/load.py, as the other templates do, so the code and agentcore.json name the same model. - When every boxed runtime that calls Bedrock has a modelId, build writes a policy for those models only: an inference profile of the account in the region, and its foundation model only through that profile; or a foundation model ID in the region. - When a runtime has no modelId (a project created before this change), build keeps the every-model policy and warns, naming the runtime and the field to set.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## box-integration #2911 +/- ##
=================================================
Coverage 96.87% 96.87%
=================================================
Files 774 774
Lines 68744 68848 +104
=================================================
+ Hits 66593 66697 +104
Misses 2151 2151 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
leo237
left a comment
There was a problem hiding this comment.
Thanks, scoping the Box gateway role to the runtime's own model is the right fix, and the inference-profile pair (the profile, plus the foundation model in any region only through that profile ARN) is the correct IAM shape. It covers global. too, since * in the region segment matches the empty region of a global-profile foundation-model ARN, and the live test agrees. I'm requesting changes because of one blocker, a regression where legitimate calls get denied.
Blocker: an ARN or a LiteLLM route prefix in modelId writes a policy that matches nothing. modelBehindProfile splits on . and treats anything without a known prefix as a bare foundation-model ID. I ran boxBedrockPolicy (us-west-2) on a few inputs:
modelId |
Resource written |
|---|---|
arn:aws:bedrock:us-west-2:123456789012:inference-profile/us.anthropic.claude-sonnet-5-5 |
arn:aws:bedrock:us-west-2::foundation-model/arn:aws:bedrock:...:inference-profile/us.anthropic... |
arn:aws:bedrock:us-west-2::foundation-model/amazon.nova-pro-v1:0 |
...::foundation-model/arn:aws:bedrock:us-west-2::foundation-model/amazon.nova-pro-v1:0 |
LiteLLM bedrock/converse/us.anthropic.claude-sonnet-5-5 |
...::foundation-model/converse/us.anthropic.claude-sonnet-5-5 |
None of these come with a build warning. Strands' BedrockModel, Converse and LiteLLM all accept these forms, and --model-id / the schema only require min(1). The old policy allowed the first two (inference-profile/*, foundation-model/*), so this PR turns working agents into a Bedrock AccessDenied at invoke time. Application-inference-profile and provisioned-model ARNs were already denied before, but they now fail the same silent way. Suggested fix, inline below: normalize ARNs (inference-profile/X and foundation-model/X reduce to X; for an ARN that names another region, use that region). Strip LiteLLM's converse/ / invoke/ route. For anything you can't classify (application-inference-profile, provisioned-model, custom/imported model, or an ID that doesn't look like provider.model), keep the every-model policy and warn, the same way as for a missing modelId.
Should fix: a model-free box runtime silently turns scoping off for the whole project. callsBedrock treats an undefined modelProvider as Bedrock, so the minimal-box runtime, or an add runtime box runtime whose code is the user's, counts as calling Bedrock with no model ID. Then modelIds.every(...) fails and every gateway gets every model. boxBedrockScopeWarning skips exactly those runtimes (modelProvider !== undefined), so there is no warning. Probe: minimal-box + strands-box (global.anthropic.claude-sonnet-5-5) gives the three wildcard statements and warning: undefined. The two functions should agree. If a runtime with no provider "calls no model as created", leave it out of the scoping decision too. If it might call Bedrock, warn.
Non-blocking: a single box-bedrock-policy.json for all Box gateways means each gateway can call every boxed runtime's model. The PR calls a file per runtime a follow-up. I didn't find it in the squash plan, so please file it so it doesn't get lost.
Test coverage / mutation results (boxMemory, manager.box, manager.boxNetwork, manager tests):
- Caught, good: loosening the profile resource to
inference-profile/*, always returning the every-model policy, dropping thebedrock:InferenceProfileArncondition, widening the foundation-model resource to*:*, a no-oprecordBoxModelIdincreateProjectTree,foundation-model/${model}*changed tofoundation-model/*, and suppressing the warning. - Survived: (1) removing
recordBoxModelIdfromaddBoxRuntimeinmanager.tsx(the add-runtime path has no test), (2) removing thebedrock/strip inbedrockModelIdOf(the multi-runtime test checks only Sids), (3) hard-coding the oldMODEL_IDback into the LangChainload.py(the test uses the default model, which equals the old hard-coded value, so it can't tell the difference; use a non-default--model-id). - Missing cases: a user-set
--model-idthroughcreate, thea2a-python-strands-box/agui-python-strands-boxtemplates, an ARNmodelId, a project that mixes a model-free runtime with a model runtime, and amodelIdchanged between two builds (the file is rewritten from the spec, so stale grants should go, but nothing asserts it).
Checked and fine: the #2893 api-key route (box strips AWS_BEARER_TOKEN_BEDROCK and the gateway signs with its own role, so the role policy is what enforces this). Converse/ConverseStream are authorized as InvokeModel/InvokeModelWithResponseStream. The policy's region is the build region, which is also the bedrock target's endpoint region. Partitions work for aws-cn / aws-us-gov. The LangChain load.py now renders the same ID that create records. All box templates' load.py render {{safeJson modelId}}. There's no schema change, and existing projects keep the old policy and get the warning, so no migration is needed. The default model's e2e is unchanged.
Local: bun run typecheck passes. bun test: 6410 pass, 9 skip, 0 fail. CI: Analyze, CodeQL, and verify on Linux/Windows/macOS pass. e2e was skipped.
tejaskash
left a comment
There was a problem hiding this comment.
Changes requested. The scoped policy itself is right for global, geographic and plain model IDs: in the IAM policy simulator, modelId: global.anthropic.claude-sonnet-5-5 in us-west-2 allows the profile in the role's account and the model in us-west-2, eu-west-1 and the empty region through that profile, and denies another profile, another account, another model, and the model called directly. A plain amazon.nova-pro-v1:0 is allowed in the region only. The problems are the cases that fall back or produce a policy that can never match.
- A boxed runtime with no
modelProviderbrings back the every-model policy, and build says nothing (src/core/project/boxMemory.ts:137-138and:166). The fallback at:166triggers when any boxed runtime thatcallsBedrockhas nomodelId, andcallsBedrockModeltreats a missingmodelProvideras Bedrock. The warning at:138filters onmodelProvider !== undefined, so the two disagree. With the real CLI:create --template agent-python-strands-boxthenbuildwrites the scoped policy, but adding a second agent withadd runtime --name helper --language pythonand building again writesinference-profile/*andfoundation-model/*for both gateways, with no message.agent-python-minimal-boxgets the wide policy too, though the description says it gets none. Make the warning condition the same as the fallback condition and update the test atboxMemory.test.ts:223. - The
in.inference profile prefix is missing (boxMemory.ts:80). ap-south-1 has 7 system profiles with the prefixin(for examplein.anthropic.claude-sonnet-5). An unknown prefix is treated as a plain model ID, so build writesfoundation-model/in.anthropic.claude-sonnet-5, which does not exist, and every call is denied with no warning. The same will happen for the next prefix AWS adds. Suggest dropping the list: for every ID that is not an ARN, write the profile statement, the in-region model statement, and the through-profile model statement for the ID minus its first segment. For a plain model ID the profile statements can never match, so nothing widens. - A
modelIdthat is an ARN produces a policy that can never match (boxMemory.ts:102-109). An inference profile ARN becomesfoundation-model/arn:aws:bedrock:...:inference-profile/..., and the same for an application inference profile or a foundation model ARN. When the ID starts witharn:, use it as the resource (with the through-profile model statement for a system profile, andfoundation-model/*underbedrock:InferenceProfileArnfor an application profile), or refuse an ARN for a box runtime at build with a message. - The AccessDenied after changing the model in code is explained only in a source comment (
src/projectSchemas/runtime.ts:372). Add one sentence todocs/box-local-testing.mdand the box template README: the Box gateway may call only the model inmodelIdofagentcore.json, so change both together and deploy again.
Smaller:
manager.tsx:2293: therecordBoxModelIdcall inaddBoxRuntimeis always a no-op (that path never setsmodelProvider). Delete it and drop theexport.- Tests: removing the warning from the writer (
boxNetworkWriters.ts:398) or hardcoding the LangChain model again (agent-python-langchain/model/load.py:4) fails nothing. Assert the build warning inmanager.boxNetwork.test.ts, use a non-default model for the LangChain row, and adda2a-python-strands-boxandagui-python-strands-boxto the create table. - The description mentions
--model-id, whichcreatedoes not have, so the recorded value is always the template default. boxMemory.ts:72: LiteLLM also acceptsbedrock/converse/<model>andbedrock/invoke/<model>, which becomefoundation-model/converse/.... Strip those too.boxMemory.ts:143: with two runtimes the warning readsruntime "agent0", "agent1" has no modelId. Word it for a list.
- A modelId that build cannot scope to falls back, with a warning, instead of writing a policy that matches nothing: build reads a model ID or system inference profile ID (provider.model), a foundation model ARN or a system inference profile ARN (in the ARN's region), and LiteLLM's bedrock/, bedrock/converse/ and bedrock/invoke/ routes. An application inference profile, provisioned, custom or imported model ARN, or an ID that is not provider.model, keeps the every-model policy and the warning. - No list of geographic prefixes: every plain ID gets the profile statement, the model statement through that profile, and the in-region model statement. A model ID's profile statements and a profile ID's model statement name resources that cannot exist, so nothing widens, and in. or a future prefix needs no change. - The warning and the fallback agree: a boxed runtime with no modelProvider (minimal-box, or an added runtime) counts as calling Bedrock in both, so the project keeps every model and build says which runtime to give a modelId. The warning names several runtimes in one sentence. - The no-op recordBoxModelId call in add runtime is removed. - docs/box-local-testing.md and the strands and LangChain READMEs of a box runtime say that the Box gateway calls only the model in modelId, so the code and modelId change together. - The comment on foundation-model/<model>* says why it ends in *. - Tests: an in. profile, a plain model ID, a profile ARN in another region, a foundation model ARN, each LiteLLM route (asserting the resource), the three kinds that fall back, a mixed project, the plural warning; create with a non-default model for strands-box and langchain-box (the code and modelId match), a2a-python-strands-box and agui-python-strands-box; build rewrites the policy to a changed modelId and warns when it is missing.
|
Thanks @leo237 and @tejaskash, all addressed in 5e070e4 (inline replies on Leo's threads).
Smaller: the no-op Tests added: each ID and ARN form, the three kinds that fall back, each LiteLLM route (asserting @leo237: the policy-file-per-runtime follow-up is noted; I'll file it.
|
leo237
left a comment
There was a problem hiding this comment.
Approving. 5e070e4 fixes the blocker and the should-fix, and I found no new blocker. The policy is scoped in every case I probed, or it falls back to the every-model policy with a warning. No probe produced a resource that matches nothing, and no wildcard defeats the scoping.
Probes (boxBedrockPolicy / boxBedrockScopeWarning, us-west-2):
modelId / project |
Result |
|---|---|
arn:...:us-east-1:<acct>:inference-profile/us.anthropic.claude-sonnet-5-5 |
The profile in us-east-1 for the role's account, plus foundation-model/anthropic.claude-sonnet-5-5* only through that profile. No warning. |
arn:...:eu-west-1::foundation-model/amazon.nova-pro-v1:0 |
That model in eu-west-1 only. |
| application-inference-profile ARN, provisioned-model ARN | The every-model policy, plus the warning. |
arn:...::foundation-model/*, arn:aws:bedrock:*::..., anthropic.* |
The every-model policy, plus the warning. * never reaches a scoped resource. |
LiteLLM bedrock/converse/<id>, bedrock/invoke/<id> |
Scoped to <id>. |
minimal-box + strands-box; strands-box + a user-code add runtime box runtime |
The every-model policy, plus a warning naming the model-free runtime. |
Dropping the prefix list holds up. For a plain model ID, the profile statements name a profile that cannot exist. For a profile ID, the in-region statement names a model that cannot exist. Neither one widens anything.
Mutations (boxMemory, manager.box, manager.boxNetwork and runtime tests): The three that survived last time are now caught. The bedrock/ strip removal fails 4 tests, and the hard-coded LangChain MODEL_ID fails 1. The addBoxRuntime call was removed as a no-op, which is correct. Also caught:
- removing the
converse//invoke/strip - ignoring the ARN's region or kind
- dropping
MODEL_ID_PATTERNfor plain IDs - restoring the old warning filter
- dropping either statement group for plain IDs
- removing the strands README
{{#if box}}gate
Follow-ups (non-blocking):
- Removing
if (!MODEL_ID_PATTERN.test(rest!)) return undefined;for ARNs passes every test. This check is the only thing that stopsarn:aws:bedrock:us-west-2::foundation-model/*from writingfoundation-model/*as a "scoped" policy with no warning. Please add a wildcard-ARN case to the fallbacktest.each. - Two more mutations survive.
- Removing the dedupe in
boxBedrockPolicysurvives, because the multi-runtime test counts the...1and...2Sids but not the total. - Removing the LangChain README paragraph survives, because no test renders the langchain-box README.
- Removing the dedupe in
- The
a2a-python-strands-boxandagui-python-strands-boxREADMEs don't get the "changeload.pyandmodelIdtogether" paragraph. Their resolvers don't passbox. - The ARN's partition is ignored. An
arn:aws-cn:...modelIdin anawsbuild writesarn:aws:bedrock:cn-north-1::..., which never matches, and build gives no warning. It's an edge case. Falling back when the partition differs from the build's would close it. - A minimal-box-only project now warns on every build with "Set modelId to the model its code calls", but its code calls no model. That's acceptable given the agreed behavior. It may be worth a hint that a runtime calling no model can ignore the warning.
- The per-runtime policy file follow-up, as noted.
Local: bun run typecheck passes. bun test: 6425 pass, 9 skip, 0 fail.
CI: verify on Linux and Windows, Analyze and CodeQL pass. e2e was skipped. verify / macOS failed on project add config-bundle wizard > the example stays on screen while the user types over it, a 5 s timeout in a TUI test this PR doesn't touch. It passes locally, and the test before it took 5.6 s, so it looks flaky. A re-run should clear it.
|
Live results for 5e070e4 (test account, us-west-2, with the L3
The added runtime itself did not start on the Runtime, for a reason outside this PR: in a Box project with a Memory, its Runtime profile reads |
…ck policy - An ARN of another partition than the build's falls back to the every-model policy with the warning: it names a model the gateway's role can never call. - The warning says that a runtime whose code calls no model needs no modelId. - The A2A and AG-UI box READMEs get the paragraph that ties the model to modelId, as the strands and LangChain ones do. - Tests: a foundation model ARN with a wildcard, a wildcard model ID and an ARN of another partition fall back; the multi-runtime policy has the deduplicated statement count; create renders the modelId paragraph in the README of strands-box, langchain-box, a2a-strands-box and agui-strands-box. Removing the ARN ID check, the dedupe, or the LangChain README paragraph now fails a test.
leo237
left a comment
There was a problem hiding this comment.
Re-approving at b04d487. The follow-up commit closes the follow-ups from my last review, and I found no new blocker.
My follow-ups from 5e070e4:
- Wildcard-ARN test: done. Removing the
MODEL_ID_PATTERNcheck for ARNs now fails a test. - Dedupe and LangChain README mutations: both caught now. The multi-runtime test counts 6 statements, and the new README test renders the langchain-box README.
- a2a and agui READMEs: done. Both resolvers pass
box. I rendered the a2a README both ways: the paragraph appears only withbox, and the## Memoryspacing is right either way. - Partition: done. An
arn:aws-cn:model ID in anawsbuild now falls back to the every-model policy and warns. - Model-free hint: added to the warning.
- Per-runtime policy file: still a follow-up. Please make sure it's filed.
All my inline threads were already resolved.
tejaskash's review (b1c4fe4), as I read it:
- 1 (model-free runtime fallback and warning disagree), 2 (
in.prefix), 3 (ARNmodelId): addressed in 5e070e4. - 4 (docs sentence): addressed. It's in
docs/box-local-testing.mdand in all four box template READMEs. - Smaller items: the no-op
recordBoxModelIdwas removed. The tests he asked for were added: writer warning, non-default LangChain model, a2a/agui in the table. LiteLLMconverse//invoke/routes are stripped. The warning is worded for a list. - One item is open: the description still says the LangChain template "ignored
--model-id". That's for him to confirm.
Mutations (boxMemory, manager.box, manager.boxNetwork, manager and templates tests; 642 pass at baseline). Caught: dropping the partition check, inverting it, policy fallback ignoring the region, removing the dedupe, box: false in the a2a or agui resolver, removing the hint, removing the ARN pattern check, removing the LangChain README paragraph.
Nits (non-blocking):
- Survived:
defaultTargetsWritercallingboxBedrockScopeWarning(spec)withoutregion. The policy still falls back, but build would drop the warning for an ARN in another partition. Amanager.boxNetworkcase with anarn:aws-cn:modelIdwould pin it. - Survived: removing the
{{#if box}}gate in the a2a or agui README. No test checks that a non-box render leaves the paragraph out.
Local: bun run typecheck and oxlint are clean. The touched and related suites pass, 642 tests, 0 fail. CI: CodeQL and Analyze pass. verify on Linux, macOS and Windows was still pending when I posted.
Description
The role of a Box gateway could invoke every inference profile and every foundation model of the account.
agentcore/box-bedrock-policy.jsonnamedinference-profile/*andfoundation-model/*, and the Runtime profile's Bedrock route allows the wholebedrock-runtime.<region>.amazonaws.comhost, so a boxed agent could call any model, the most expensive ones too.This PR lets a Box gateway call only the model of its runtime, from the runtime's existing
modelIdinagentcore.json.What changes
createrecords the model. For a Box template that calls a Bedrock model,createwrites the model ID its code renders (the template default, or the scaffold input'smodelId) into the runtime'smodelId, as it already does for LiteLLM:agent-python-minimal-boxcalls no model as created and gets none, and neither does a runtime thatadd runtimeadds (its code is the user's). Both count as unscoped (see below).Build scopes the policy. When every boxed runtime that calls Bedrock has a
modelId,box-bedrock-policy.jsonallows only those models:{ "Sid": "BoxedAgentInferenceProfileThroughGateway", "Effect": "Allow", "Action": ["bedrock:InvokeModel", "bedrock:InvokeModelWithResponseStream"], "Resource": ["arn:aws:bedrock:us-west-2:*:inference-profile/global.anthropic.claude-sonnet-5-5"], "Condition": { "StringEquals": { "aws:ResourceAccount": "${aws:PrincipalAccount}" } } }, { "Sid": "BoxedAgentModelThroughItsInferenceProfile", "Effect": "Allow", "Action": ["bedrock:InvokeModel", "bedrock:InvokeModelWithResponseStream"], "Resource": ["arn:aws:bedrock:*::foundation-model/anthropic.claude-sonnet-5-5*"], "Condition": { "StringEquals": { "bedrock:InferenceProfileArn": "arn:aws:bedrock:us-west-2:${aws:PrincipalAccount}:inference-profile/global.anthropic.claude-sonnet-5-5" } } }plus the in-region statement on
foundation-model/<id>. A call through an inference profile is authorized twice: on the profile, and on the foundation model in the region the profile sends it to, so the model statement names any region but holds only through that profile. Every plain ID gets all three statements, with no list of geographic prefixes: for a model ID (amazon.nova-pro-v1:0) the profile statements name a profile that cannot exist, and for a profile ID the in-region statement a model that cannot exist. A system inference profile ARN or a foundation model ARN gets the statements of its kind, in the ARN's region. LiteLLM'sbedrock/,bedrock/converse/andbedrock/invoke/routes use the ID after them. Several runtimes give one statement set per model, each with its own Sid.No
modelIdbuild can scope to: no change, and a warning. A boxed runtime that calls Bedrock (one with nomodelProvidercounts) with nomodelId, or with one build cannot scope to (an application inference profile, provisioned, custom or imported model ARN, or an ID that is notprovider.model), keeps the whole project on the every-model policy, and build warns, naming each such runtime:The LangChain template renders
modelId.agent-python-langchainhardcodedMODEL_ID = "global.anthropic.claude-sonnet-5-5"and ignored--model-id. Itsmodel/load.pynow renders{{safeJson modelId}}, as the other templates do, so the code andagentcore.jsonname the same model.The
modelIdschema comment,docs/box-local-testing.md, and the box README of the strands and LangChain templates say that the Box gateway calls only the model inmodelId, so the code andmodelIdchange together.Decisions
modelIdfield, not a new one. It already records the model the scaffold rendered; this PR also writes it for box runtimes that call Bedrock.box-bedrock-policy.json, so it holds the models of all boxed runtimes. When one of them has nomodelId, the file keeps every model. A file per runtime is a follow-up.modelId; otherwise the gateway denies the call (Bedrock AccessDenied). The schema comment says so.Related Issue
Closes #
Strands Box bug bash, IAM finding F3 (security doc §2.4).
Documentation PR
Included:
docs/box-local-testing.mdand the template READMEs.Type of Change
Testing
How have you tested the change?
bun test: 6410 pass, 3 fail. The 3 (the box on PATH > ...inboxProfileResolver.test.tsandprojectGateways.test.ts) also fail onbox-integrationwithout this change; they run only with a box that has--format json --fullon PATH.bun run test:e2e, or explained why they are not applicable: not run. A live test in a test account (us-west-2) took its place:create --template agent-python-strands-boxwrote"modelId": "global.anthropic.claude-sonnet-5-5",deployattached the two-statement policy above to the Box gateway's role, andinvokeanswered through the Box gateway ("17 plus 25 is 42."), so Bedrock accepts the scoped policy for a global inference profile.bun run typecheckbun run lint:checkbun run format:checkbun run auditbun run secrets:checkbun run buildsrc/assets/, I updated affected snapshots withbun test <test-file> --update-snapshotsand committed them: no snapshot has the LangChainmodel/load.py.command.md(the release workflow regenerates it)New tests:
boxMemory.test.ts: an inference profile gives the profile and its model only through it; a model ID gives that model in the region; several runtimes give one statement set per model with distinct Sids, and LiteLLM'sbedrock/model is used; a runtime with a model provider and nomodelIdkeeps every model and gets the warning; a runtime with no model provider gets no warning.manager.box.test.ts:createrecordsmodelIdforagent-python-strands-box,agent-python-langchain-boxandagent-python-strands-box-container, none foragent-python-minimal-boxand the non-boxagent-python-strands; the LangChainmodel/load.pynames the same model.manager.boxNetwork.test.ts: the Bedrock policy of a created project names only its model.Checklist
modelId)