Skip to content

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

Open
Anarion-zuo wants to merge 4 commits intoray-project:masterfrom
Anarion-zuo:aaronzuo/state-cli-auth-headers-verify
Open

feat: Add --headers/--verify to Ray State CLI and document State REST API#61958
Anarion-zuo wants to merge 4 commits intoray-project:masterfrom
Anarion-zuo:aaronzuo/state-cli-auth-headers-verify

Conversation

@Anarion-zuo
Copy link
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
Copy link
Contributor

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

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 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
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 on lines +1101 to +1102
headers=_handle_headers(headers),
verify=verify,
Copy link
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,
        ...
    )

Copy link

@cursor cursor bot left a comment

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.

Fix All in Cursor

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
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 observability Issues related to the Ray Dashboard, Logging, Metrics, Tracing, and/or Profiling

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant