Skip to content

[Fix][Core/Auth] Pass auth token to image_uri containers and reuse it when ray.init() finds a local cluster - #66467

Merged
elliot-barn merged 5 commits into
ray-project:masterfrom
MortalHappiness:fix/flaky-test_runtime_env_container
Sep 29, 2026
Merged

elliot-barn merged 5 commits into
ray-project:masterfrom
MortalHappiness:fix/flaky-test_runtime_env_container

Conversation

@MortalHappiness

@MortalHappiness MortalHappiness commented Sep 24, 2026 •

Copy link
Copy Markdown
Member

Description

test_runtime_env_container has failed on master since #64755 enabled token auth by default for local clusters. That change exposed two bugs:

  1. Container workers can't find the token. The image_uri / container plugin forwards RAY_AUTH_MODE=token into the podman worker but doesn't mount the token file, so the worker crashes on startup. Fix: mount the token file read-only and set RAY_AUTH_TOKEN_PATH. The token isn't passed via --env, because the podman command is logged.
  2. ray.init() can't connect to a cluster that ray start --head started with token auth. With RAY_AUTH_MODE unset, the driver connects without a token and gets rejected. Fix: when ray.init() finds a running local cluster (no address, or address="auto") and a token exists, it enables token auth, reuses the token, and warns, just like ray start --head. An explicit address or RAY_ADDRESS keeps the current behavior, and nothing else in the [Feat][Core/Auth] Enable token authentication for local clusters by default #64755 behavior table changes.

Related issues

Related to #64755.

Additional information

N/A

…rs and in image_uri containers

Signed-off-by: Chi-Sheng Liu <[email protected]>
@MortalHappiness
MortalHappiness requested review from a team as code owners September 24, 2026 18:24

@gemini-code-assist gemini-code-assist Bot left a comment

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.

Code Review

This pull request updates ray.init() to automatically enable token authentication and reuse an existing token when connecting to a local cluster without an explicit address (or with address='auto'). It also improves security by mounting the token file read-only into container runtimes instead of passing the token via environment variables. The feedback suggests resolving RAY_AUTH_TOKEN_PATH to an absolute path using os.path.abspath(os.path.expanduser(...)) to ensure relative paths or paths containing tildes are correctly detected and mounted.

@MortalHappiness MortalHappiness added the go add ONLY when ready to merge, run all tests label Sep 24, 2026
Signed-off-by: Chi-Sheng Liu <[email protected]>
@ray-gardener ray-gardener Bot added the core Issues that should be addressed in Ray Core label Sep 24, 2026
# logged and visible in `ps`.
if is_token_auth_enabled() and "RAY_AUTH_TOKEN" not in env_vars:
token_path = os.path.abspath(
env_vars.get("RAY_AUTH_TOKEN_PATH") or _get_default_token_path()

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.

what about the RAY_ENABLE_K8S_TOKEN_AUTH, which need some files under /var/run/secrets

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

No effect. In K8s auth mode KubeRay sets no RAY_AUTH_TOKEN(_PATH) or ~/.ray/auth_token, so isfile() check is false and this change adds nothing to the podman command.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Because this is an existing behavior, created another issue to track this. #66561

@MortalHappiness MortalHappiness changed the title [Fix][Core/Auth] Reuse existing token when connecting to local clusters and in image_uri containers [Fix][Core/Auth] Pass auth token to image_uri containers and reuse it when ray.init() finds a local cluster that requires auth Sep 26, 2026
@MortalHappiness MortalHappiness changed the title [Fix][Core/Auth] Pass auth token to image_uri containers and reuse it when ray.init() finds a local cluster that requires auth [Fix][Core/Auth] Pass auth token to image_uri containers and reuse it when ray.init() finds a local cluster that requires auth Sep 26, 2026
@MortalHappiness MortalHappiness changed the title [Fix][Core/Auth] Pass auth token to image_uri containers and reuse it when ray.init() finds a local cluster that requires auth [Fix][Core/Auth] Pass auth token to image_uri containers and reuse it when ray.init() finds a local cluster Sep 28, 2026
@MortalHappiness MortalHappiness changed the title [Fix][Core/Auth] Pass auth token to image_uri containers and reuse it when ray.init() finds a local cluster [Fix][Core/Auth] Pass auth token to image_uri containers and reuse it when ray.init() finds a local cluster Sep 28, 2026
Signed-off-by: Chi-Sheng Liu <[email protected]>
@MortalHappiness
MortalHappiness requested a review from a team as a code owner September 28, 2026 23:29

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.

Fix All in Cursor

Reviewed by Cursor Bugbot for commit b74912e. Configure here.

Comment thread python/ray/_private/worker.py Resolved
@rueian
rueian enabled auto-merge (squash) September 29, 2026 22:23
@elliot-barn
elliot-barn merged commit 5853ff1 into ray-project:master Sep 29, 2026
7 checks passed
elliot-barn added a commit that referenced this pull request Sep 29, 2026
…ntainers and reuse it when `ray.init()` finds a local cluster (#66467) (#66582)

Cherry-pick of #66467 onto `releases/2.59.0`.

#64755 (token auth enabled by default for local clusters) was
cherry-picked to 2.59.0 in #66248, so the two bugs it exposed exist on
the release branch too:

1. `image_uri` / `container` workers get `RAY_AUTH_MODE=token` but not
the token file, so they crash on startup. Fix: mount the token file
read-only and set `RAY_AUTH_TOKEN_PATH`.
2. `ray.init()` can't connect to a local cluster started via `ray start
--head` with token auth when `RAY_AUTH_MODE` is unset. Fix: reuse the
existing token (and warn) when connecting to a running local cluster.

Cherry-picked cleanly with `git cherry-pick -x` from merge commit
5853ff1; the diff is identical to the
upstream commit.

Not a duplicate: no existing PR against `releases/2.59.0` references
#66467.

Testing: relying on release-branch CI (`test_token_auth_integration`,
`unit/test_runtime_env_image_uri`, `test_runtime_env_container`); no
local tests run.

AI assistance was used to prepare this cherry-pick.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Signed-off-by: Chi-Sheng Liu <[email protected]>
Signed-off-by: elliot-barn <[email protected]>
Co-authored-by: Chi-Sheng Liu <[email protected]>
rueian pushed a commit that referenced this pull request Oct 2, 2026
…ation on Windows (#66623)

## Description

Two Windows tests have failed on every Windows postmerge build since
#66467 (builds 20012, 20016, 20026 and 20040). This PR fixes both.

- `//python/ray/tests/unit:test_runtime_env_image_uri`:
`test_mounts_default_token_file` fails because the `auth_env` fixture
redirects only `HOME`. On Windows, `Path.home()` reads `USERPROFILE` and
ignores `HOME`, so the code looks for the real `~/.ray/auth_token` and
never adds the mount. The fixture now sets `USERPROFILE` too.
- `//python/ray/tests:test_token_auth_integration`: the file times out
at the `medium` limit (300s). Before #66467 it already took 277-297s on
Windows. #66467 added `test_ray_init_connect_reuses_existing_token` with
3 parameters, and each one runs `ray start --head`, which pushes the
file past the limit. This PR moves the file to the `large` list (900s).
`test_streaming_generator.py` is already there for the same reason.

## Related issues

Related to #66467.

## Additional information

Both targets are now in the `FLAKY` state in the CI test database, so
the regular premerge jobs on this PR skip them. To check the fix, I ran
each target 3 times on Windows premerge, once on master and once on
master plus this fix, with retries off and the test database lookup
bypassed.

| Target | master | master + fix |
| --- | --- | --- |
| `test_runtime_env_image_uri` | 3/3 failed (same assertion as
postmerge) | 0/3 failed |
| `test_token_auth_integration` | 3/3 timed out at 300s | 0/3 failed,
324-345s per run |

- master: #66637,
https://buildkite.com/ray-project/premerge/builds/74235
- master + fix: #66638,
https://buildkite.com/ray-project/premerge/builds/74236

Signed-off-by: Chi-Sheng Liu <[email protected]>
Co-authored-by: Mengjin Yan <[email protected]>
nadongjun pushed a commit to nadongjun/ray that referenced this pull request Oct 6, 2026
… when `ray.init()` finds a local cluster (ray-project#66467)

## Description

`test_runtime_env_container` has failed on master since ray-project#64755 enabled
token auth by default for local clusters. That change exposed two bugs:

1. **Container workers can't find the token.** The `image_uri` /
`container` plugin forwards `RAY_AUTH_MODE=token` into the podman worker
but doesn't mount the token file, so the worker crashes on startup. Fix:
mount the token file read-only and set `RAY_AUTH_TOKEN_PATH`. The token
isn't passed via `--env`, because the podman command is logged.
2. **`ray.init()` can't connect to a cluster that `ray start --head`
started with token auth.** With `RAY_AUTH_MODE` unset, the driver
connects without a token and gets rejected. Fix: when `ray.init()` finds
a running local cluster (no `address`, or `address="auto"`) and a token
exists, it enables token auth, reuses the token, and warns, just like
`ray start --head`. An explicit `address` or `RAY_ADDRESS` keeps the
current behavior, and nothing else in the ray-project#64755 behavior table changes.

## Related issues

Related to ray-project#64755.

## Additional information

N/A

---------

Signed-off-by: Chi-Sheng Liu <[email protected]>
nadongjun pushed a commit to nadongjun/ray that referenced this pull request Oct 6, 2026
…ation on Windows (ray-project#66623)

## Description

Two Windows tests have failed on every Windows postmerge build since
ray-project#66467 (builds 20012, 20016, 20026 and 20040). This PR fixes both.

- `//python/ray/tests/unit:test_runtime_env_image_uri`:
`test_mounts_default_token_file` fails because the `auth_env` fixture
redirects only `HOME`. On Windows, `Path.home()` reads `USERPROFILE` and
ignores `HOME`, so the code looks for the real `~/.ray/auth_token` and
never adds the mount. The fixture now sets `USERPROFILE` too.
- `//python/ray/tests:test_token_auth_integration`: the file times out
at the `medium` limit (300s). Before ray-project#66467 it already took 277-297s on
Windows. ray-project#66467 added `test_ray_init_connect_reuses_existing_token` with
3 parameters, and each one runs `ray start --head`, which pushes the
file past the limit. This PR moves the file to the `large` list (900s).
`test_streaming_generator.py` is already there for the same reason.

## Related issues

Related to ray-project#66467.

## Additional information

Both targets are now in the `FLAKY` state in the CI test database, so
the regular premerge jobs on this PR skip them. To check the fix, I ran
each target 3 times on Windows premerge, once on master and once on
master plus this fix, with retries off and the test database lookup
bypassed.

| Target | master | master + fix |
| --- | --- | --- |
| `test_runtime_env_image_uri` | 3/3 failed (same assertion as
postmerge) | 0/3 failed |
| `test_token_auth_integration` | 3/3 timed out at 300s | 0/3 failed,
324-345s per run |

- master: ray-project#66637,
https://buildkite.com/ray-project/premerge/builds/74235
- master + fix: ray-project#66638,
https://buildkite.com/ray-project/premerge/builds/74236

Signed-off-by: Chi-Sheng Liu <[email protected]>
Co-authored-by: Mengjin Yan <[email protected]>
rueian pushed a commit that referenced this pull request Oct 6, 2026
… their own local cluster (#66778)

## Description

Since #64755, `ray start --head` enables token auth when `RAY_AUTH_MODE`
is unset and `~/.ray/auth_token` exists, for example after any earlier
Python `ray.init()`. C++ and Java drivers that start their own head
don't make the same decision: their auth mode stays `disabled`, so they
connect without a token and fail with `InvalidAuthToken`. The release
sanity check hits this. #66467 fixed the same mismatch for Python
`ray.init()`.

This PR adds `MaybeEnableTokenAuthIfTokenAvailable()` next to the token
loader. When `RAY_AUTH_MODE` is unset and a token exists, it sets
`RAY_AUTH_MODE=token` in the process environment so that the later
`RayConfig` re-initialization keeps it. Both drivers call it, only when
they started the head:

```diff
 ProcessHelper::RayStart (C++)
   if driver && no address
+    MaybeEnableTokenAuthIfTokenAvailable()
     StartRayNode()  # ray start --head

 RayNativeRuntime.start (Java)
   if driver && no address
     RunManager.startRayHead()  # ray start --head
   if driver
     loadLibrary()
+    if started the head
+      nativeMaybeEnableTokenAuthIfTokenAvailable()
     getGcsClient()
```

Drivers given an explicit address are unchanged. `//cpp:metric_example`
now runs without `RAY_AUTH_MODE` to cover this path.

## Related issues

N/A

## Additional information

N/A

Signed-off-by: Chi-Sheng Liu <[email protected]>
sai-miduthuri added a commit that referenced this pull request Oct 13, 2026
…va drivers that start their own local cluster (#66778) (#66801)

Cherry-pick of #66778 (master commit
8bf3d3e) onto `releases/2.60.0`.

`releases/2.60.0` already has #64755 (token auth enabled by default for
local clusters) and #66467, so C++ and Java drivers that start their own
head fail with `InvalidAuthToken` on this branch too: `ray start --head`
enables token auth when `RAY_AUTH_MODE` is unset and `~/.ray/auth_token`
exists, but the driver's auth mode stays `disabled`. The release sanity
check hits this. The fix adds `MaybeEnableTokenAuthIfTokenAvailable()`
next to the token loader and calls it from both drivers only when they
started the head.

Applied cleanly with `git cherry-pick -x`; the diff is identical to the
master commit. All 9 touched files, plus the APIs the new code calls
(`TryLoadToken(ignore_auth_mode)`, `SetEnv`), are byte-identical to
master at 8bf3d3e after the pick.

Not a duplicate: no existing PR against `releases/2.60.0` references
#66778.

Testing: relying on release-branch CI (`//cpp:metric_example`, which now
runs without `RAY_AUTH_MODE` to cover this path, plus the C++ and Java
driver tests); no local tests run.

AI assistance was used to prepare this cherry-pick.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Signed-off-by: Chi-Sheng Liu <[email protected]>
Signed-off-by: sai.miduthuri <[email protected]>
Co-authored-by: Chi-Sheng Liu <[email protected]>
elliot-barn pushed a commit that referenced this pull request Oct 21, 2026
… `test_token_auth_integration` on Windows (#66623) (#67213)

Cherry-pick of #66623 (master commit
43cb165) onto `releases/2.60.0`.

## Why

Both tests fail in the `:ray: core: :windows: python tests` job in all
11 `releases/2.60.0` postmerge builds so far, including build 20362 for
the current branch head:

- `test_runtime_env_image_uri.py::test_mounts_default_token_file` fails
its token-mount assertion. The `auth_env` fixture redirects only `HOME`,
but on Windows `Path.home()` reads `USERPROFILE`.
- `test_token_auth_integration` times out at the 300s `medium` limit.

Both failures started with #66467, which this branch has. #66623 sets
`USERPROFILE` in the fixture and moves `test_token_auth_integration.py`
to the `large` list (900s). It changes only tests and `BUILD.bazel`.

On master, both tests failed in every release-automation postmerge build
that ran them, from the branch point through build 20097. They have
passed in all 49 builds that ran them since build 20102, the first to
include #66623.

## Cherry-pick details

Applied cleanly with `git cherry-pick -x`. The change is identical to
the master commit:

- Same 2 files, 5 insertions and 1 deletion, and the same `git patch-id
--stable`.
- The diff text matches except for the second `BUILD.bazel` hunk header.
It starts 1 line earlier here, because this branch doesn't have the
`test_function_manager_import_error.py` entry that #66564 added above it
on master.

No prerequisite commits are missing:

- On master, only #66564 touched these files between the branch cut and
#66623. It added that one test entry to another list.
- After the pick, `test_runtime_env_image_uri.py` is identical to master
at 43cb165, and `BUILD.bazel` differs only by that entry.
- `test_token_auth_integration.py` is now in the same `large` list as on
master (tag `large_size_python_tests_shard_0`). The separate
`_client_mode` entry was already `large` and is unchanged.
- On this branch, no commit after the cut touched these files.

Not a duplicate: this branch doesn't contain #66623, and no PR against
`releases/2.60.0` references it.

## Testing

| Command | Result |
|---|---|
| `pre-commit run --files python/ray/tests/BUILD.bazel
python/ray/tests/unit/test_runtime_env_image_uri.py` | Passed (ruff,
black, buildifier, buildifier-lint, and the other applicable hooks) |

Both fixes only change behavior on Windows, so I didn't run the tests
locally on Linux. In CI they run in the `:ray: core: :windows: python
tests` job. On premerge and microcheck, that job waits on the "run
windows tests" block step.

AI assistance was used to prepare this cherry-pick.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Signed-off-by: Chi-Sheng Liu <[email protected]>
Signed-off-by: Sai Miduthuri <[email protected]>
Co-authored-by: Chi-Sheng Liu <[email protected]>
Co-authored-by: Mengjin Yan <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core Issues that should be addressed in Ray Core go add ONLY when ready to merge, run all tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants