Repository navigation
feat(ingress): ingress relay for agent tcp communication - #272
Conversation
|
Assessment: Request Changes The design is sound: the TCB binds the listener, PID 1 opens the connection inside the netns on request, and the relay checks the peer. Two problems block the merge. Callers that are not trusted can exhaust the trusted process's descriptors and keep it spinning (shown with a reproduction). And the new interface has no reference page. A Review notes
Checked and held:
The PID 1 connector protocol is small, and the peer check makes it easy to audit. |
ce37953 to
4123eaa
Compare
|
Assessment: Request Changes (re-review at 4123eaa) All four earlier findings are fixed: the accept backoff and connection bound, the Checked at this commit and held
The new socket-handoff protocol is simpler than the connect-in-PID-1 design and harder to stall. |
4123eaa to
e0faba0
Compare
|
Assessment: Approve (re-review at e0faba0) Both items from the last round are fixed, and I found nothing new that blocks the merge. The frozen-surface changes ( Checked at this commit and held
The handoff protocol and the child-limit restore are both small, and both are pinned by tests that fail without the fix. |
e0faba0 to
6689b59
Compare
|
Assessment: Approve (re-review at 6689b59) The only change since e0faba0 is the rebase onto One thing to fix before owner sign-off: the PR description is out of date in three places, including the frozen surface owners are asked to approve.
Checked at this commit and held
|
6689b59 to
595b0ed
Compare
|
Assessment: Approve (re-review at 595b0ed) This is another rebase onto The PR description is still out of date, and it now asks owners to approve the wrong version: it says " Checked at this commit and held
|
ferdingler
left a comment
There was a problem hiding this comment.
Two findings to address: connector readiness during Linux startup, and the caller-limit regression test.
1659d00 to
006ec00
Compare
|
Assessment: Approve (re-review at 006ec00) This push addresses all three of @ferdingler's findings, and I found no new defect. The PR description now matches the code: The review findings, checked at this commit
Tests on this Linux host: |
006ec00 to
4ffb4a2
Compare
|
Assessment: Approve (re-review at 4ffb4a2) I compared this push with
Suggestion (non-blocking): Checked and held
|
4ffb4a2 to
806e171
Compare
|
Assessment: Approve (re-review at 806e171) I compared this push with Checked and held
|
|
Addressed all comments. @ferdingler . Ready for another review. |
…agent.ingress]] An agent that serves HTTP, such as a `BedrockAgentCoreApp` or a Strands agent behind a web server, cannot be reached in a box. The agent's sandbox allows only outbound connections, through the egress gateway, so nothing outside the sandbox can connect to the port the agent listens on. On Linux the Namespace backend refuses a listen port, and on macOS the Seatbelt profile refuses one. Box binds a listener in its trusted process and relays each caller to the agent's port. The agent's code does not change, and the agent gains no reach. - `box.toml`: `[[agent.ingress]]` is a list of up to five entries, so an agent that serves HTTP, MCP, and A2A on separate ports gets one listener for each. Each entry holds `listen`, an IP address and port that Box binds, and `workload_port`, the port the agent listens on in its sandbox. Both are required. Box binds every `listen` before the agent starts, so a port in use refuses the run. Each entry needs its own `listen` port and its own `workload_port`, and on macOS no `listen` port may equal any entry's `workload_port`. A `[[tool.<name>.ingress]]` is refused; a tool's ingress is not built yet. Ingress is experimental: `[[agent.ingress]]` loads only when `[experimental-features]` sets `ingress = true`, and is refused otherwise. - New crate `strands-box-ingress` (`ingress`): `Ingress`, `IngressConfig`, `Source`, `Workload`, and `IngressError`. `Ingress::start` accepts each caller, connects to the agent, and copies bytes both ways. - Linux: the agent's port exists only in its network namespace. The sandbox's PID 1 keeps the socket pair that egress already uses, marked close-on-exec so the agent never holds it. For each caller the relay asks PID 1 for a socket, and PID 1 creates an unconnected TCP socket in the namespace and sends it back. The relay connects it to `127.0.0.1:workload_port` itself, without blocking, with a timeout for each caller. Every listener's relay shares the one channel (`Workload::in_namespace_ports`). PID 1 sends one ready byte when its connector starts, and Box starts no accept loop until that byte arrives, at most 60 s. The listeners are bound meanwhile, so an early caller waits in the listen queue. Without this wait, a caller that arrived while the sandbox was still being set up timed out, and the timeout closed the channel for every listener. A socket keeps the namespace it was created in, so the connection reaches the agent. PID 1 never blocks, so a stuck agent cannot stall other callers. - macOS: Seatbelt renders `network-bind` and `network-inbound` on `(local tcp "localhost:<workload_port>")`, which allows that port on any address of the Mac, and the relay connects to `127.0.0.1:workload_port` (`Workload::loopback`). Before the agent starts, Box refuses to start when another process already listens on `workload_port`. A local process can also reach the agent's port directly; that is an accepted risk on macOS. A profile with no listen port is byte-identical. - Both platforms: the relay connects to `127.0.0.1` only, and PID 1 creates IPv4 sockets only. A relay that also tried `::1` would let another local user on macOS take callers while the agent is down, and on Linux the sandbox creates no IPv6 socket. When nothing accepts, the relay closes the caller and prints one line until a caller is served again. - Box raises its own soft open-file limit to the hard limit (`run::descriptor_limit::raise`), and the relay holds at most an eighth of that limit in callers at once, between 16 and 10,000 (`IngressConfig::connection_limit`), split evenly across the listeners. An eighth leaves room for the egress descriptors that each caller's request also opens. After an accept error, the relay waits one second. - Every child that Box starts gets the soft limit that Box started with (`run::descriptor_limit::restore_in_child`): each process that the trampoline starts (the agent, each tool, each contained MCP leaf) and each stdio MCP server. So only the box process holds the raised limit. - The record stores the agent's ingress in `[agent]` only. A record that holds one is version 29, so an earlier build refuses the box. Versions 24 to 28 came first on `main`; an ingress takes 29, the highest, even beside them. Every other record keeps its version and its bytes. - Docs: `docs/design/decisions.md` says `listen` now renders on both backends, `docs/user/config.md` gains `[[agent.ingress]]`, `crates/box/README.md` lists the field, and `docs/user/native-filesystem-policy.md` names version 29. This changes frozen surfaces and needs owner approval: `[agent]` gains a field, `[experimental-features]` gains `ingress`, `Record` gains version 29, `containment` lowers `Network::Localhost.listen` on both backends, and the new crate adds a public facade. No policy decision covers a caller; `listen` decides who can reach the agent. - `relay::tests::an_exhausted_descriptor_limit_does_not_spin`: a child process with a 64-descriptor limit and every descriptor taken, while the parent holds callers in the queue. The workload server runs in the parent, so the child's CPU time is the relay's alone. On Linux the relay without the backoff fails this test. macOS drops a connection when `accept` fails, so it cannot spin there. - `run::descriptor_limit::tests::a_child_starts_with_the_limit_the_box_had_before_it_raised_it`: the child starts at 256 after Box raises its own limit. Without the restore, the child starts at 10,240 and the test fails. - `supervise::tests::the_ingress_connection_limit_is_an_eighth_of_the_descriptor_limit`: 1,024 gives 128 callers, 10,240 gives 1,280, and the floor and the ceiling hold. - `inherited_handles_linux::the_workload_does_not_inherit_the_channel_to_pid_one`: the real trampoline with a listen port and the Unix socket channel attached; the workload holds no descriptor above stderr. With the `close_on_exec` call removed, the test fails and names the channel's descriptor. - `relay::tests::a_caller_beyond_the_limit_waits_until_one_closes`: the workload serves each connection on its own thread and counts them. With the limit replaced by 10,000, the test fails, because a second connection reaches the workload. - `netns::tests::a_connector_that_starts_late_serves_the_caller_that_arrived_first`: the real `serve_inbound` starts 6 s late, which is more than one answer may take, and a caller connects before it starts. That caller and the next one are served. - `workload::linux::tests::the_workloads_wait_until_pid_one_is_ready`, `a_channel_that_closes_before_ready_is_refused`, and `a_first_byte_that_is_not_ready_is_refused`. - `record::config::tests::an_agent_ingress_needs_the_ingress_switch` (omitted and `false` are refused, `true` loads) and `a_stored_ingress_needs_the_stored_switch`. - `workload::linux::tests::*` (fake PID 1): the relay connects the socket PID 1 sends; nothing listening is not ready and the channel stays open; an IPv6 socket, a datagram socket, an undefined answer, and a closed channel are refused. - `workload::macos::tests::a_listener_on_the_ipv6_loopback_alone_is_not_reached`. - `record::config::tests::an_ingress_is_declared_on_the_agent` (two entries, in order; the single-table `[agent.ingress]` spelling is refused) and `an_agent_ingress_stores_a_version_no_earlier_build_reads`. - `record::config::ingress::tests`: several entries on distinct ports are accepted; six entries, two entries on one `listen` port, and two entries on one `workload_port` are refused; on macOS, a `listen` port that is another entry's `workload_port` is refused, and on Linux each port mapped to itself is accepted. - `workload::linux::tests::workloads_on_one_channel_each_reach_their_own_port`. - `boundary::tests::the_ingress_reaches_the_agent_and_no_tool`: two entries give the agent's boundary both `workload_port`s, and a tool gets none. - `record::config::tests::an_agent_ingress_beside_a_shared_program_stores_the_ingress_version` and `an_agent_ingress_beside_an_absolute_mcp_command_stores_the_ingress_version`. - `profile_conformance::a_localhost_listen_port_renders_one_bind_and_one_inbound_rule`, `listen_ports_render_beside_the_composition_ports`, and `preflight_new_cells_macos::a_listen_port_is_the_only_port_the_workload_can_listen_on` (real kernel). - On `main` (`9c0bf1d`). macOS: `strands-box` 1302 passed, `strands-box-containment` 330 passed, `strands-box-ingress` 7 passed. Linux, in Docker (`rust:1-bookworm`, `/proc` covers unmounted, run as uid 1000 because `main` refuses a root launch that grants a directory write): `strands-box` 1324 passed, `strands-box-ingress` 16 passed. One macOS test, `tool_grammars::a_push_is_denied_until_a_test_exits_0_and_stays_allowed_after_a_restart`, fails the same way on `main` without this change. - Rebased on `main` (`ec5ef77`), after strands-agents#377 took record version 28 for an absolute MCP command, so ingress moved to 29. Before that, on `f4180a0`, which fixes the seccomp entry count: Linux `strands-box-containment` 693 passed and `strands-box-ingress` 16 passed, 0 failed in each, and the macOS `record::` and ingress tests pass. clippy shows no new warning in `box`, `containment`, or `ingress` on either platform, and fmt is clean. - Live, macOS: a `BedrockAgentCoreApp` answers through the relay, 20 parallel calls return 200, a held `workload_port` refuses the run, and a stopped agent serves again when resumed. Live, Linux: an agent's server answers through PID 1's socket, its port is unreachable from the container, 20 parallel calls return 200, and a stopped agent serves again when resumed. An agent that binds `::1` on Linux fails to start, because the sandbox creates no IPv6 socket. - Live, two listeners into one agent, on macOS and on Linux: each listener reaches its own server port, 20 parallel calls across both return 200, and on Linux neither agent port is reachable from the container. On macOS, crossed ports and six entries are refused at startup. On Linux, from this revision, the same box without `ingress = true` is refused with the experimental message. - AgentCore Runtime, from the revision before the readiness wait and the switch, with one `[[agent.ingress]]` entry: the probe, a Bedrock prompt, a WebSocket session, and 10 parallel calls return 200. The runtime's open-file limit is 1,024, and its log shows that the relay holds up to 128 callers. - AgentCore Runtime, from this revision, with `ingress = true` and two `[[agent.ingress]]` entries (`0.0.0.0:8080` to 8081, an HTTP app; `0.0.0.0:8000` to 8000, an MCP server in the same agent) and `allowedPorts` 8080 and 8000: `InvokeAgentRuntime` returns 200, and the session endpoint reaches both listeners. On port 8000, signed MCP `initialize`, `tools/list`, and `tools/call` succeed, the tools run in the agent's sandbox, 10 parallel calls return correct results, and a tool's fetch goes through the egress gateway, which permits example.com and refuses google.com. A Bedrock prompt and a WebSocket session on 8080 also pass. Not run: the GitHub MCP call through an AgentCore Gateway, because the egress `proxy` route is not on `main`.
806e171 to
5e687cf
Compare
|
Assessment: Approve (re-review at 5e687cf) I compared this push with Checked and held
|
Description
An agent that serves HTTP, such as a
BedrockAgentCoreAppor a Strands agent behind a web server, cannot be reached in a box. The agent's sandbox allows only outbound connections, through the egress gateway, so nothing outside the sandbox can connect to the port the agent listens on. On Linux the Namespace backend refuses a listen port, and on macOS the Seatbelt profile refuses one.Solution
Box binds a listener in its trusted process and relays each caller to the agent's port. The agent's code does not change, and the agent gains no reach.
box.toml:[[agent.ingress]]is a list of up to five entries, so an agent that serves HTTP, MCP, and A2A on separate ports gets one listener for each. Each entry holdslisten, an IP address and port that Box binds, andworkload_port, the port the agent listens on in its sandbox. Both are required. Box binds everylistenbefore the agent starts, so a port in use refuses the run. Each entry needs its ownlistenport and its ownworkload_port, and on macOS nolistenport may equal any entry'sworkload_port. A[[tool.<name>.ingress]]is refused; a tool's ingress is not built yet. Ingress is experimental:[[agent.ingress]]loads only when[experimental-features]setsingress = true, and is refused otherwise.strands-box-ingress(ingress):Ingress,IngressConfig,Source,Workload, andIngressError.Ingress::startaccepts each caller, connects to the agent, and copies bytes both ways.127.0.0.1:workload_portitself, without blocking, with a timeout for each caller. Every listener's relay shares the one channel (Workload::in_namespace_ports). PID 1 sends one ready byte when its connector starts, and Box starts no accept loop until that byte arrives, at most 60 s. The listeners are bound meanwhile, so an early caller waits in the listen queue. Without this wait, a caller that arrived while the sandbox was still being set up timed out, and the timeout closed the channel for every listener. A socket keeps the namespace it was created in, so the connection reaches the agent. PID 1 never blocks, so a stuck agent cannot stall other callers.network-bindandnetwork-inboundon(local tcp "localhost:<workload_port>"), which allows that port on any address of the Mac, and the relay connects to127.0.0.1:workload_port(Workload::loopback). Before the agent starts, Box refuses to start when another process already listens onworkload_port. A local process can also reach the agent's port directly; that is an accepted risk on macOS. A profile with no listen port is byte-identical.127.0.0.1only, and PID 1 creates IPv4 sockets only. A relay that also tried::1would let another local user on macOS take callers while the agent is down, and on Linux the sandbox creates no IPv6 socket. When nothing accepts, the relay closes the caller and prints one line until a caller is served again.run::descriptor_limit::raise), and the relay holds at most an eighth of that limit in callers at once, between 16 and 10,000 (IngressConfig::connection_limit), split evenly across the listeners. An eighth leaves room for the egress descriptors that each caller's request also opens. After an accept error, the relay waits one second.run::descriptor_limit::restore_in_child): each process that the trampoline starts (the agent, each tool, each contained MCP leaf) and each stdio MCP server. So only the box process holds the raised limit.[agent]only. A record that holds one is version 29, so an earlier build refuses the box. Versions 24 to 28 came first onmain; an ingress takes 29, the highest, even beside them. Every other record keeps its version and its bytes.docs/design/decisions.mdsayslistennow renders on both backends,docs/user/config.mdgains[[agent.ingress]],crates/box/README.mdlists the field, anddocs/user/native-filesystem-policy.mdnames version 29.This changes frozen surfaces and needs owner approval:
[agent]gains a field,[experimental-features]gainsingress,Recordgains version 29,containmentlowersNetwork::Localhost.listenon both backends, and the new crate adds a public facade. No policy decision covers a caller;listendecides who can reach the agent.Files
[agent]gains[[agent.ingress]], up to five entries oflistenandworkload_port. Ingress is experimental: it loads only withingress = truein[experimental-features]. Box checks the count, unique ports, and on macOS that nolistenport equals aworkload_port. A tool's ingress is refused. A record with an ingress is version 29.record/config/ingress.rs(new),record/config/process.rs,record/config/mod.rs,ProcessSpecliterals;docs/user/config.md,crates/box/README.md,docs/user/native-filesystem-policy.md,docs/design/decisions.mdstrands-box-ingress)Ingress::startaccepts each caller up to a limit, connects to the agent's port, and copies bytes both ways. On Linux it waits until the sandbox's PID 1 reports ready, then asks PID 1 for a socket for each caller and connects it with a timeout; on macOS it connects over loopback.crates/ingress/**,Cargo.toml,Cargo.lockworkload_portonly.containment/.../namespace/{mod,netns,reaper}.rs,backend/macos/seatbelt.rs,model.rs,containment/Cargo.toml(test-only dependencies); testsinherited_handles_linux.rs,profile_conformance.rs,preflight_new_cells_macos.rsrunstarts the relaysrun/contain/supervise.rs,run/contain/boundary.rs,run/netns_relay.rs,error.rsrun/descriptor_limit.rs(new),run/contain/trampoline.rs,run/broker/mcp/server.rsPaths under
crates/box/src/are shortened torun/…andrecord/….Tests
relay::tests::an_exhausted_descriptor_limit_does_not_spin: a child process with a 64-descriptor limit and every descriptor taken, while the parent holds callers in the queue. The workload server runs in the parent, so the child's CPU time is the relay's alone. On Linux the relay without the backoff fails this test. macOS drops a connection whenacceptfails, so it cannot spin there.run::descriptor_limit::tests::a_child_starts_with_the_limit_the_box_had_before_it_raised_it: the child starts at 256 after Box raises its own limit. Without the restore, the child starts at 10,240 and the test fails.supervise::tests::the_ingress_connection_limit_is_an_eighth_of_the_descriptor_limit: 1,024 gives 128 callers, 10,240 gives 1,280, and the floor and the ceiling hold.inherited_handles_linux::the_workload_does_not_inherit_the_channel_to_pid_one: the real trampoline with a listen port and the Unix socket channel attached; the workload holds no descriptor above stderr. With theclose_on_execcall removed, the test fails and names the channel's descriptor.relay::tests::a_caller_beyond_the_limit_waits_until_one_closes: the workload serves each connection on its own thread and counts them. With the limit replaced by 10,000, the test fails, because a second connection reaches the workload.netns::tests::a_connector_that_starts_late_serves_the_caller_that_arrived_first: the realserve_inboundstarts 6 s late, which is more than one answer may take, and a caller connects before it starts. That caller and the next one are served.workload::linux::tests::the_workloads_wait_until_pid_one_is_ready,a_channel_that_closes_before_ready_is_refused, anda_first_byte_that_is_not_ready_is_refused.record::config::tests::an_agent_ingress_needs_the_ingress_switch(omitted andfalseare refused,trueloads) anda_stored_ingress_needs_the_stored_switch.workload::linux::tests::*(fake PID 1): the relay connects the socket PID 1 sends; nothing listening is not ready and the channel stays open; an IPv6 socket, a datagram socket, an undefined answer, and a closed channel are refused.workload::macos::tests::a_listener_on_the_ipv6_loopback_alone_is_not_reached.record::config::tests::an_ingress_is_declared_on_the_agent(two entries, in order; the single-table[agent.ingress]spelling is refused) andan_agent_ingress_stores_a_version_no_earlier_build_reads.record::config::ingress::tests: several entries on distinct ports are accepted; six entries, two entries on onelistenport, and two entries on oneworkload_portare refused; on macOS, alistenport that is another entry'sworkload_portis refused, and on Linux each port mapped to itself is accepted.workload::linux::tests::workloads_on_one_channel_each_reach_their_own_port.boundary::tests::the_ingress_reaches_the_agent_and_no_tool: two entries give the agent's boundary bothworkload_ports, and a tool gets none.record::config::tests::an_agent_ingress_beside_a_shared_program_stores_the_ingress_versionandan_agent_ingress_beside_an_absolute_mcp_command_stores_the_ingress_version.profile_conformance::a_localhost_listen_port_renders_one_bind_and_one_inbound_rule,listen_ports_render_beside_the_composition_ports, andpreflight_new_cells_macos::a_listen_port_is_the_only_port_the_workload_can_listen_on(real kernel).main(9c0bf1d). macOS:strands-box1302 passed,strands-box-containment330 passed,strands-box-ingress7 passed. Linux, in Docker (rust:1-bookworm,/proccovers unmounted, run as uid 1000 becausemainrefuses a root launch that grants a directory write):strands-box1324 passed,strands-box-ingress16 passed. One macOS test,tool_grammars::a_push_is_denied_until_a_test_exits_0_and_stays_allowed_after_a_restart, fails the same way onmainwithout this change.main(ec5ef77), after feat(mcp): support absolute executable commands #377 took record version 28 for an absolute MCP command, so ingress moved to 29. Before that, onf4180a0, which fixes the seccomp entry count: Linuxstrands-box-containment693 passed andstrands-box-ingress16 passed, 0 failed in each, and the macOSrecord::and ingress tests pass. clippy shows no new warning inbox,containment, oringresson either platform, and fmt is clean.BedrockAgentCoreAppanswers through the relay, 20 parallel calls return 200, a heldworkload_portrefuses the run, and a stopped agent serves again when resumed. Live, Linux: an agent's server answers through PID 1's socket, its port is unreachable from the container, 20 parallel calls return 200, and a stopped agent serves again when resumed. An agent that binds::1on Linux fails to start, because the sandbox creates no IPv6 socket.ingress = trueis refused with the experimental message.[[agent.ingress]]entry: the probe, a Bedrock prompt, a WebSocket session, and 10 parallel calls return 200. The runtime's open-file limit is 1,024, and its log shows that the relay holds up to 128 callers.ingress = trueand two[[agent.ingress]]entries (0.0.0.0:8080to 8081, an HTTP app;0.0.0.0:8000to 8000, an MCP server in the same agent) andallowedPorts8080 and 8000:InvokeAgentRuntimereturns 200, and the session endpoint reaches both listeners. On port 8000, signed MCPinitialize,tools/list, andtools/callsucceed, the tools run in the agent's sandbox, 10 parallel calls return correct results, and a tool's fetch goes through the egress gateway, which permits example.com and refuses google.com. A Bedrock prompt and a WebSocket session on 8080 also pass.Not run: the GitHub MCP call through an AgentCore Gateway, because the egress
proxyroute is not onmain.Type of Change
New feature
Testing
How have you tested the change? Verify that the changes do not break functionality or introduce new warnings.
just check, orcargo test --workspace --all-features)cargo fmt --allandcargo clippy --workspace --all-targets --all-features -- -D warningsChecklist
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.