Skip to content

[serve] Require the auth token on HAProxy's exporter and stats page - #67418

Merged
rueian merged 4 commits into
ray-project:masterfrom
harshit-anyscale:serve-haproxy-exporter-token-auth
Oct 29, 2026
Merged

rueian merged 4 commits into
ray-project:masterfrom
harshit-anyscale:serve-haproxy-exporter-token-auth

Conversation

@harshit-anyscale

@harshit-anyscale harshit-anyscale commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

Description

Ray 2.61 turns token auth on by default, and since #67128 every Ray metrics endpoint requires the token. Serve's HAProxy ingress was left out. HAProxy runs two pages of its own: a Prometheus metrics page on port 9101 and a stats page on port 8404. HAProxy is a separate program and knows nothing about Ray's token, so both pages answer anyone who can reach the node. #67211 reports the metrics page. The stats page shows the same backend and server names and the same request and error counts, so this PR covers both.

The fix takes HAProxy off those ports and puts Ray's own token check in front of it. When token auth is on and RAY_AUTH_SKIP_METRICS is unset:

  • HAProxy serves the metrics page and the stats page only on a unix socket with mode 600, which only the Ray user can open. Nothing listens on port 8404.
  • HAProxyManager, which already starts and manages HAProxy on each node, listens on port 9101 instead. Every request goes through get_token_auth_wsgi_app, the same check [Core/Auth] Require the auth token on Ray's Prometheus metrics endpoints #67128 put on the node, dashboard head and autoscaler metrics servers. A missing token gets 401, a wrong one gets 403, and a request with the right token is passed to HAProxy over the socket and gets HAProxy's answer back.
before:  Prometheus -> :9101 -> HAProxy                                          (no check)
         browser    -> :8404 -> HAProxy                                          (no check)
after:   Prometheus -> :9101 -> HAProxyManager token check -> 0600 socket -> HAProxy
         :8404      -> nothing listening

With token auth off, or with RAY_AUTH_SKIP_METRICS=1, nothing changes. HAProxy serves both pages directly, and the rendered HAProxy config is byte-for-byte the same as on master.

Why not have HAProxy check the token itself, the other option in #67211? It can't cover every mode. In Kubernetes token mode, Ray doesn't compare against a fixed token: it asks the Kubernetes API (TokenReview and SubjectAccessReview), and HAProxy can't make those calls. For a fixed token, HAProxy would have to get the token from its environment, which #67047 rules out, or read the token file on its own terms, which differ from Ray's. HAProxy keeps trailing spaces, skips a line that starts with # and accepts every line in the file, where Ray takes only the first non-blank line, trimmed. Reusing Ray's check avoids all of that, and the HAProxy pages follow any later change to the auth rules.

A few smaller pieces. start_metrics_http_server now calls a new start_metrics_wsgi_server, which serves any WSGI app behind the same check, so existing callers don't change. With SERVE_SOCKET_REUSE_PORT_ENABLED=1, co-located EveryNode proxies share the metrics port, so HAProxyManager passes reuse_port and the server sets SO_REUSEPORT the way HAProxy's own listeners do. It sets the option in server_bind() because socketserver's allow_reuse_port only exists from Python 3.11. The socket path is per node, like the admin socket (RAY_SERVE_HAPROXY_EXPORTER_SOCKET_PATH, default /tmp/haproxy-serve/<node_id>/exporter.sock). A request is retried once if a reload resets its connection to the old HAProxy worker. If HAProxy is down, the answer is 503.

Related issues

Closes #67211

Additional information

What users will notice with token auth on: whoever scrapes :9101/metrics must send the token, as for Ray's other metrics endpoints since #67128. The stats page moves from :8404/stats to :9101/stats, also behind the token. Nothing in Ray reads port 8404 outside tests. RAY_AUTH_SKIP_METRICS=1 keeps both pages open, and reverting this PR brings back the old listeners with no state to migrate.

Tests added:

  • test_haproxy.py::test_haproxy_metrics_and_stats_require_token: 401 and 403, 200 for each token source, /stats;csv through :9101, and nothing answering on :8404.
  • test_haproxy.py::test_haproxy_metrics_and_stats_open_without_token: both pages open without a token, once with RAY_AUTH_SKIP_METRICS=1 and once with auth disabled.
  • test_haproxy.py::test_haproxy_metrics_token_auth_with_colocated_reuse_port_proxies: two nodes on one machine with EveryNode proxies, token auth and SERVE_SOCKET_REUSE_PORT_ENABLED=1. Both proxies come up healthy, and /metrics answers 401 without the token and 200 with it.
  • test_haproxy_api.py::test_metrics_auth_required_moves_exporter_to_unix_socket: against a real HAProxy, the config binds only the socket, the socket's mode is 0600, both ports are closed and /metrics and /stats;csv are forwarded. It then reloads HAProxy, checks that a new process took over and that both pages still answer, and gets 503 once HAProxy stops. It runs with enable_hap_optimization on (the -x socket handoff) and off (plain -sf).
  • test_haproxy_api.py::test_exporter_proxy_app_retries_after_dropped_connection: the retry.
  • test_haproxy.py::test_haproxy_metrics now sends the token.

I ran both HAProxy test files on Linux in a container: the Ray nightly image at fb22a8a266 with this PR's files copied over it (the nightly's 15 commits past this branch's base touch none of them), and HAProxy 2.8.25 from ray-haproxy. test_haproxy.py passed 37 of 37 and test_haproxy_api.py passed 60 of 60, each file in one pytest session. With the reuse_port change reverted, the co-located test fails with OSError: [Errno 98] Address already in use and the second proxy is marked unhealthy.

🤖 Generated with Claude Code

With token auth on, Ray's own metrics endpoints require the token
(ray-project#67128), but Serve's HAProxy ingress still served HAProxy's Prometheus
exporter on :9101/metrics and its stats page on *:8404 to anyone who
could reach them.

When token auth is on and RAY_AUTH_SKIP_METRICS is unset, HAProxy now
binds the exporter and stats page only to a 0600 unix socket, and
HAProxyManager serves /metrics and /stats on the metrics port through
get_token_auth_wsgi_app, the check Ray's other metrics endpoints use.
With auth off or RAY_AUTH_SKIP_METRICS=1, the rendered config is
unchanged.

Closes ray-project#67211

Signed-off-by: harshit-anyscale <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
@harshit-anyscale harshit-anyscale added serve Ray Serve Related Issue go add ONLY when ready to merge, run all tests labels Oct 26, 2026
@harshit-anyscale harshit-anyscale self-assigned this Oct 26, 2026
@harshit-anyscale
harshit-anyscale marked this pull request as ready for review October 26, 2026 12:25
@harshit-anyscale
harshit-anyscale requested review from a team as code owners October 26, 2026 12:25

@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 secures HAProxy's metrics and stats endpoints behind token authentication when enabled. When authentication is required, HAProxy is configured to expose these endpoints only via a local Unix socket, and a WSGI proxy server is started to serve them behind Ray's token check. The review feedback highlights a potential file descriptor leak in the Unix socket connection logic and points out that the tests use the unimported requests library, which will cause runtime NameErrors, suggesting a consistent use of httpx instead.

Comment thread python/ray/serve/_private/haproxy.py Resolved
Comment thread python/ray/serve/tests/test_haproxy.py Resolved
Comment thread python/ray/serve/tests/test_haproxy.py Resolved
A failed connect() never reached self.sock, so HTTPConnection.close()
left the socket for garbage collection: 200 failed connects raised 200
"unclosed socket" ResourceWarnings. Close it before re-raising.

Signed-off-by: harshit-anyscale <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
start_metrics_wsgi_server(port, make_wsgi_app(registry), addr)


def start_metrics_wsgi_server(port: int, app, addr: str = "0.0.0.0") -> WSGIServer:

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.

Could we set allow_reuse_port when SERVE_SOCKET_REUSE_PORT_ENABLED=1? Co-located EveryNode proxies share the metrics port through HAProxy’s SO_REUSEPORT, but this WSGI listener does not, so the second HAProxyManager fails with EADDRINUSE.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done in a3f4d0f. start_metrics_wsgi_server takes reuse_port, and HAProxyManager passes enable_so_reuseport. It sets SO_REUSEPORT in server_bind(), since allow_reuse_port only exists from Python 3.11 and Ray still supports 3.10. Without the fix, the new co-located test fails with OSError: [Errno 98] Address already in use.

assert stats.status_code == 200
assert stats.text.startswith("# pxname")
with pytest.raises(requests.ConnectionError):
_get_haproxy_endpoint(8404, "/stats")

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.

Could we add a co-located EveryNode test with token auth and SERVE_SOCKET_REUSE_PORT_ENABLED=1? It should assert both managers become healthy and authenticated /metrics succeeds, which would catch this listener regression.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Added test_haproxy_metrics_token_auth_with_colocated_reuse_port_proxies: two co-located nodes, EveryNode, token auth and SERVE_SOCKET_REUSE_PORT_ENABLED=1. It waits for both proxies to be HEALTHY, then checks /metrics returns 200 with the token and 401 without. It fails without the fix and passes with it.

Comment thread python/ray/serve/_private/constants.py Outdated
Comment on lines +866 to +869
# When metrics need the token (token auth on, RAY_AUTH_SKIP_METRICS unset),
# HAProxy serves its exporter and stats page only on this 0600 unix socket, and
# HAProxyManager serves them on the metrics port behind the token check that
# Ray's other metrics endpoints use.

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.

Could we describe the routing more directly?

Suggested change
# When metrics need the token (token auth on, RAY_AUTH_SKIP_METRICS unset),
# HAProxy serves its exporter and stats page only on this 0600 unix socket, and
# HAProxyManager serves them on the metrics port behind the token check that
# Ray's other metrics endpoints use.
# When metrics authentication is required, HAProxy serves the exporter and
# stats page on this owner-only Unix socket. HAProxyManager exposes them on the
# metrics port with Ray token authentication.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done, with your wording.

# True when token auth is on and RAY_AUTH_SKIP_METRICS is unset: the
# exporter and stats page move to exporter_socket_path, and HAProxyManager
# serves them on metrics_port behind the token check.
metrics_auth_required: bool = False

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.

Could we drop this comment? metrics_auth_required already describes the field, and the listener setup documents the routing.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Dropped.

Comment thread python/ray/serve/_private/haproxy.py Outdated
if metrics_auth_required:
from ray._private.authentication.http_token_authentication import (
start_metrics_wsgi_server,
)

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.

Could we hoist this and authentication_utils to the module imports and drop the preceding comment? Neither authentication module depends on Serve, and the condition and function names already describe the behavior.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Moved both to the module imports and dropped the comment.

finally:
server.shutdown()
server.server_close()
assert len(connections) == 2

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.

Could we cover a real HAProxy reload with metrics_auth_required=True and verify /metrics and /stats;csv before and after? The synthetic Unix-server test verifies the retry but not HAProxy listener transfer.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The test now reloads a real HAProxy, checks the PID changed, and re-checks /metrics and /stats;csv. It is parametrized on enable_hap_optimization, so it covers both the -x handoff and plain -sf.

…oads

With SERVE_SOCKET_REUSE_PORT_ENABLED=1, co-located EveryNode proxies share
the metrics port through HAProxy's SO_REUSEPORT. In token mode the Python
metrics server owns that port, so the second HAProxyManager failed with
EADDRINUSE. start_metrics_wsgi_server now takes reuse_port, and
HAProxyManager passes enable_so_reuseport. It sets the option in
server_bind() because socketserver's allow_reuse_port only exists from
Python 3.11.

Adds a co-located EveryNode test with token auth and reuse-port on, which
fails without the fix, and covers a real HAProxy reload with and without
enable_hap_optimization. Also hoists the auth imports and trims comments,
per review.

Signed-off-by: harshit-anyscale <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>

@eicherseiji eicherseiji 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.

🚢

@eicherseiji
eicherseiji enabled auto-merge (squash) October 29, 2026 19:09
@rueian
rueian disabled auto-merge October 29, 2026 22:49
@rueian
rueian merged commit 62a5cc1 into ray-project:master Oct 29, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

go add ONLY when ready to merge, run all tests serve Ray Serve Related Issue

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Serve][Core/Auth] HAProxy ingress serves /metrics without the auth token when token auth is on

4 participants