Skip to content

feat: Add --headers/--verify to Ray State CLI and document State REST API - #61958

Closed
Anarion-zuo wants to merge 8 commits into
ray-project:masterfrom
Anarion-zuo:aaronzuo/state-cli-auth-headers-verify
Closed

Anarion-zuo wants to merge 8 commits into
ray-project:masterfrom
Anarion-zuo:aaronzuo/state-cli-auth-headers-verify

Conversation

@Anarion-zuo

Copy link
Copy Markdown
Contributor

Summary

This PR makes Ray’s State CLI usable on authenticated / TLS-secured clusters by adding --headers and --verify flags (similar to ray job), and documents the State REST API endpoints and query parameters.

Resolved issue #50256.

Changes

  • CLI: Add --headers and --verify to:
    • ray list, ray get, ray summary (tasks|actors|objects), and ray logs (...)
    • Support RAY_STATE_HEADERS env var as a default for --headers
  • Client: Plumb headers/verify into State API client usage and log REST calls
    • StateApiClient(..., headers=..., verify=...)
    • get_log() / list_logs() accept headers and verify
  • Docs: Add a “State REST API” section documenting endpoints and parameters; add CLI auth notes

Tests

  • Added unit tests covering:
    • --headers parsing and propagation
    • RAY_STATE_HEADERS env var behavior
    • invalid JSON header handling
    • verify flag propagation to list/summary/logs paths
    • Tests: test_state_api.py

Run:

python -m pytest -q python/ray/tests/test_state_api.py -k "test_state_cli_"

Usage example

ray list actors --address https://<head>:8265 \
  --headers '{"Authorization": "Bearer <token>"}' \
  --verify /path/to/ca-bundle.pem

@Anarion-zuo
Anarion-zuo requested review from a team as code owners March 22, 2026 15:44

@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 adds authentication support to the Ray State CLI via --headers and --verify flags, and also documents the State REST API. The changes are well-implemented, with corresponding updates to documentation and new unit tests. I've identified a couple of minor opportunities for refactoring to reduce code duplication, which would improve maintainability. Overall, this is a solid contribution that enhances the usability of Ray in secure environments.

Comment thread python/ray/util/state/api.py Outdated
Comment on lines +1300 to +1303
request_headers: Dict[str, Any] = {}
if headers:
request_headers.update(headers)
request_headers.update(get_auth_headers_if_auth_enabled(request_headers))

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.

medium

This logic for preparing request headers is duplicated in list_logs (lines 1369-1372). To improve maintainability and avoid code duplication, consider extracting this logic into a private helper function.

For example, you could add:

def _prepare_request_headers(headers: Optional[Dict[str, Any]]) -> Dict[str, Any]:
    """Prepares request headers by adding auth headers if not present."""
    request_headers = headers.copy() if headers else {}
    auth_headers = get_auth_headers_if_auth_enabled(request_headers)
    request_headers.update(auth_headers)
    return request_headers

And then call request_headers = _prepare_request_headers(headers) in both get_log and list_logs.

Comment thread python/ray/util/state/state_cli.py Outdated
Comment on lines +1101 to +1102
headers=_handle_headers(headers),
verify=verify,

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.

medium

The _handle_headers function is called here and again on line 1133 when calling _print_log. This is slightly inefficient as it involves parsing the JSON string twice.

Consider calling _handle_headers once at the beginning of the log_cluster function and reusing the result:

def log_cluster(
    ctx,
    glob_filter: str,
    address: Optional[str],
    headers: Optional[str],
    ...
):
    parsed_headers = _handle_headers(headers)
    if node_id is None and node_ip is None:
        node_ip = _get_head_node_ip(address)

    logs = list_logs(
        ...,
        headers=parsed_headers,
        ...
    )
    ...
    _print_log(
        ...,
        headers=parsed_headers,
        ...
    )

Comment thread python/ray/util/state/state_cli.py Resolved
Signed-off-by: aaronzuo <[email protected]>
Signed-off-by: aaronzuo <[email protected]>
Signed-off-by: aaronzuo <[email protected]>
@ray-gardener ray-gardener Bot added core Issues that should be addressed in Ray Core observability Issues related to the Ray Dashboard, Logging, Metrics, Tracing, and/or Profiling community-contribution Contributed by the community labels Mar 22, 2026

@edoakes edoakes left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Look good. I don't love the --verify type overloading, but given it matches the job CLI no reason to revisit it here.

Can you also add one full integration test for the CLI running against an actual HTTP server? Either the real state API or a small fake one

Comment thread python/ray/tests/test_state_api.py Outdated


def test_state_cli_list_supports_headers_and_verify(monkeypatch):
import ray.util.state.state_cli as state_cli

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

should be no need for lazy import

Comment thread python/ray/tests/test_state_api.py Outdated
Comment on lines +503 to +510
class DummyStateApiClient:
def __init__(self, address=None, headers=None, verify=True, **kwargs):
captured["address"] = address
captured["headers"] = headers
captured["verify"] = verify

def list(self, *args, **kwargs):
return []

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

no need to duplicate this in each test, use one shared FakeStateApiClient that can be reused and evolved over time

@github-actions

github-actions Bot commented Apr 9, 2026

Copy link
Copy Markdown

This pull request has been automatically marked as stale because it has not had
any activity for 14 days. It will be closed in another 14 days if no further activity occurs.
Thank you for your contributions.

You can always ask for help on our discussion forum or Ray's public slack channel.

If you'd like to keep this open, just leave any comment, and the stale label will be removed.

@github-actions github-actions Bot added the stale The issue is stale. It will be closed within 7 days unless there are further conversation label Apr 9, 2026
Signed-off-by: aaronzuo <[email protected]>
@Anarion-zuo

Copy link
Copy Markdown
Contributor Author

@edoakes Resolved issues you mentioned.

  • Removed lazy imports.
  • Removed the duplicated per-test DummyStateApiClient stubs.
  • Added a real CLI integration test that runs ray list actors against a live in-test HTTP server and verifies the auth header arrives at the server in test_state_api.py .
  • Kept the existing summary/logs tests but removed their lazy module imports in test_state_api.py.

To run the targeted tests:

python -m pytest \
    "./tests/test_state_api.py::test_state_cli_list_supports_headers_and_verify" \
    "./tests/test_state_api.py::test_state_cli_list_supports_headers_env_var" \
    "./tests/test_state_api.py::test_state_cli_list_rejects_invalid_headers" \
    "./tests/test_state_api.py::test_state_cli_list_integration_with_http_server" \
    "./tests/test_state_api.py::test_state_cli_summary_supports_headers_and_verify" \
    "./tests/test_state_api.py::test_state_cli_logs_supports_headers_and_verify" \
    -q

@github-actions github-actions Bot added unstale A PR that has been marked unstale. It will not get marked stale again if this label is on it. and removed stale The issue is stale. It will be closed within 7 days unless there are further conversation labels Apr 11, 2026
@edoakes edoakes added the go add ONLY when ready to merge, run all tests label Apr 13, 2026
@edoakes

edoakes commented Apr 16, 2026

Copy link
Copy Markdown
Collaborator

@Anarion-zuo I don't think we're willing to support the state API as a public REST API at the moment. Could you please remove that from this PR and I can merge the other changes?

It makes sense in general, but I'd want to do a round of auditing the APIs before we advertise them.

Signed-off-by: aaronzuo <[email protected]>
If your cluster requires authentication, pass HTTP headers such as an
``Authorization`` token to these endpoints. When using the State CLI, you can
use ``--headers`` for custom headers and ``--verify`` to control TLS verification.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

REST API docs still present despite reviewer requesting removal

Low Severity

The "State REST API" documentation section (endpoints, query parameters, authentication notes) remains in this PR despite the reviewer explicitly requesting its removal with: "I don't think we're willing to support the state API as a public REST API at the moment. Could you please remove that from this PR and I can merge the other changes?" Publishing undocumented/unaudited API endpoints as official documentation could mislead users into depending on unstable interfaces.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 2ec51cb. Configure here.

Comment thread python/ray/tests/test_state_api.py Resolved
Signed-off-by: aaronzuo <[email protected]>

@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 and found 1 potential issue.

There are 2 total unresolved issues (including 1 from previous review).

Fix All in Cursor

Reviewed by Cursor Bugbot for commit f725488. Configure here.

Comment thread python/ray/tests/test_state_api.py Resolved
Signed-off-by: aaronzuo <[email protected]>
@Anarion-zuo

Copy link
Copy Markdown
Contributor Author

@edoakes did as you commented

@edoakes

edoakes commented Apr 20, 2026

Copy link
Copy Markdown
Collaborator

@edoakes

edoakes commented Jul 23, 2026

Copy link
Copy Markdown
Collaborator

closing as stale, please re-open if you are still interested in contributing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

community-contribution Contributed by the community core Issues that should be addressed in Ray Core go add ONLY when ready to merge, run all tests observability Issues related to the Ray Dashboard, Logging, Metrics, Tracing, and/or Profiling unstale A PR that has been marked unstale. It will not get marked stale again if this label is on it.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants