Repository navigation
[cherry-pick][2.60.0][core][sandbox] Require the API token on the gRPC facade (#66675) - #66897
Conversation
## Description The Ray Sandbox gRPC facade (`ray.experimental.sandbox.http.grpc_facade`) served sandbox create, exec, and file read/write to any client that could reach its port. Unlike the REST app, it never read `RAY_SANDBOX_API_TOKEN`, so even an operator who wanted to lock it down couldn't, and the documented quickstart binds `--host 0.0.0.0`. With this PR: - **The token is required on every RPC.** When `RAY_SANDBOX_API_TOKEN` is set, a call that doesn't present it fails with `UNAUTHENTICATED` before its handler reads the request. - A client presents the token the way the client SDK already sends credentials: as its token secret (`x-modal-token-secret`, from `MODAL_TOKEN_SECRET`), or as `authorization: Bearer <token>`, the REST app's form, which `MODAL_OVERRIDE_HEADERS` can also send. - `TaskGetCommandRouterAccess` hands out the token as the command router's credential. The SDK then sends it as a bearer token on every exec and file call, so an unmodified client only needs `MODAL_TOKEN_SECRET` set. - A bearer-only check wouldn't work with the SDK: its control-plane calls carry the token secret, and its router calls carry only the credential that `TaskGetCommandRouterAccess` returned. - **The check is bound to the servicers, not to one server.** `_TokenGate` wraps the handler table (`__mapping__`) that `grpclib.server.Server` dispatches through. So every server built from `build_servicers` is gated, including RPCs added later, such as #66359's `TaskSetNetworkAccess`. Without a token the table is returned unchanged. - **The CLI fails closed.** Without a token, `python -m ray.experimental.sandbox.http.grpc_facade` refuses a non-loopback `--host` unless `--allow-unauthenticated` is passed. - Keeping the facade on a private network isn't enough on its own. The facade's sandboxes get open egress by default and can reach any address their node can, including the facade's. So a tokenless facade on a network address would also serve the code running inside them. - Loopback binds stay tokenless, since `--disable-host-loopback` keeps sandboxes off the node's loopback. - **Docs.** The gRPC section now covers: - the token; - the trust model: one token controls every sandbox in the namespace, as with the REST token, with no per-client isolation; - the TLS requirement; - a fix for the stale network bullet: allowlists are refused, not widened. - **REST.** `require_bearer_token` compares bytes, so a non-ASCII `Authorization` header gets a 401 instead of a `TypeError` and a 500. **Behavior change:** a tokenless facade started with a non-loopback `--host`, as the old quickstart did, now exits with an error that names `RAY_SANDBOX_API_TOKEN` and `--allow-unauthenticated`. **This PR also fixes the failing `:ray: core: sandbox tests` job** (for example premerge build 74278 and microcheck build 55110). - `test_image_utils.py::test_seeding_stays_inside_the_tree` linked the fake image's `/tmp` to an absolute path under pytest's `tmp_path`, which is itself under `/tmp` in CI. - From the image's point of view that's a symlink loop. `_resolve_in_root` correctly reports it as `ELOOP`, so nothing was seeded. - The test now gives `tmp` its own tree with a relative link. The product code is unchanged. - The test only passed on macOS because `tmp_path` lives under `/private/var/folders` there. ## Related issues Related to #65839 (the facade) and #65352 (sandboxing umbrella). Not a duplicate: no open PR or issue covers facade authentication. I searched open PRs and issues for `grpc_facade`, `RAY_SANDBOX_API_TOKEN`, and sandbox authentication. ## Additional information **Tests run.** Environment: macOS, Python 3.12, the ray 2.58 wheel with this branch's sandbox package overlaid, grpclib 0.4.9. | Command | Result | | --- | --- | | `pytest --import-mode=importlib python/ray/experimental/sandbox/http/tests` | 130 passed, 8 skipped (modal not installed, `TEST_SANDBOX` unset) | | The same, with `RAY_SANDBOX_API_TOKEN=ambient` exported | 130 passed, 8 skipped | | `uv run --with modal==1.5.5 pytest .../http/tests/test_modal_client.py` | 6 passed | | `uv run --with modal==1.5.1 pytest .../http/tests/test_modal_client.py` | 5 passed, 1 skipped (1.5.1 can't send custom headers) | | The new auth tests against `origin/master`'s `grpc_facade.py` | 29 failed, as expected | | `test_image_utils.py::test_seeding_stays_inside_the_tree`, with the default `tmp_path` and with `tmp_path` under `/tmp` | Passed both ways (the old test fails with `tmp_path` under `/tmp`) | | `python -m ray.experimental.sandbox.http.grpc_facade --host 0.0.0.0` without a token | Exit 2, before connecting to the cluster | | Unauthenticated create path replayed against a local Ray cluster | Anonymous and wrong-token `AppGetOrCreate`, `ImageGetOrCreate`, and `SandboxCreate` return `UNAUTHENTICATED` and create no actor. With the token, a `SandboxHost` actor is created and then terminated. | | `pre-commit` (ruff, pydoclint, docstyle, import order), black 22.10.0, API param coverage check | Pass | `test_grpc_facade.py` and `test_modal_client.py` skip in CI because grpclib and modal aren't in the CI image, so the auth tests above ran locally only. AI assistance was used to write this change. Signed-off-by: xyuzh <[email protected]> Co-authored-by: Philipp Moritz <[email protected]> (cherry picked from commit 4f967ab) Signed-off-by: Sai Miduthuri <[email protected]>
There was a problem hiding this comment.
Code Review
This pull request introduces token-based authentication for the Ray Sandbox gRPC facade, aligning its security model with the REST API. When the RAY_SANDBOX_API_TOKEN environment variable is set, all RPCs on both the control and exec planes require the token, which can be presented as a token secret or a bearer token. By default, the CLI refuses to serve non-loopback addresses without a token unless the new --allow-unauthenticated flag is passed. Additionally, the REST API's token comparison was updated to handle non-ASCII headers gracefully by comparing them as bytes. Corresponding unit and integration tests have been added to verify these authentication mechanisms and prevent environment variable leaks. There are no review comments, so I have no feedback to provide.
|
LGTM |
Cherry-pick of #66675 (master commit 4f967ab) onto
releases/2.60.0.Why
releases/2.60.0includes the sandbox gRPC facade ([core][sandbox] Add a gRPC facade for external sandbox clients #65839) but not [core][sandbox] Require the API token on the gRPC facade #66675, which makes it require the API token. This pick brings the branch's facade in line with master.:ray: core: sandbox testsjob on this branch. For example, it fails in microcheck build 55893 for [cherry-pick][2.60.0][Fix][Core/Auth] Enable token auth in C++ and Java drivers that start their own local cluster (#66778) #66801.test_image_utils.py::test_seeding_stays_inside_the_treelinked the fake image's/tmpto an absolute path under pytest'stmp_path, which is itself under/tmpin CI. [core][sandbox] Require the API token on the gRPC facade #66675 fixes the test, and the product code is unchanged. The branch already has Added install runsc as standalone CI script #65815, which made the sandbox tests actually run in CI instead of skipping.Cherry-pick details
Applied cleanly with
git cherry-pick -x. The diff is byte-identical to the master commit: the same 10 files, 726 insertions and 29 deletions, with identical hunks, context lines and offsets.No prerequisite commits are missing:
python/ray/experimental/sandbox/ordoc/source/ray-core/sandboxes.md. Neither did any commit on this branch after the cut.python/ray/experimental/sandbox/package anddoc/source/ray-core/sandboxes.mdare identical to master at 4f967ab.ray.util.annotations, which is identical on both branches.Not a duplicate: no existing PR against
releases/2.60.0references #66675.Testing
test_grpc_facade.pyandtest_modal_client.pyskip in CI becausegrpclibandmodalaren't in the CI image, so I ran them locally.Environment: Linux, Python 3.10,
grpclib0.4.9 andmodal1.5.5 (the versions #66675 used). Other test dependencies were pinned frompython/requirements_compiled.txt. Ray was the CI-builtray-2.60.0wheel for this PR's parent commit (b7a4529), with the wheel'sray/experimental/sandbox/replaced by this branch's copy.pytest .../http/tests/test_grpc_facade.py .../http/tests/test_modal_client.pypytest .../http/teststest_http_integration.py,TEST_SANDBOXunset)releases/2.60.0releases/2.60.0(before this pick)test_image_utils.py::test_seeding_stays_inside_the_treeand the 2 skippedtest_http_integration.pycases run in CI's:ray: core: sandbox testsjob, which setsTEST_SANDBOX=1.AI assistance was used to prepare this cherry-pick.
🤖 Generated with Claude Code