Repository navigation
Conversation
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]>
There was a problem hiding this comment.
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.
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: |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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") |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
| # 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. |
There was a problem hiding this comment.
Could we describe the routing more directly?
| # 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. |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
Could we drop this comment? metrics_auth_required already describes the field, and the listener setup documents the routing.
| if metrics_auth_required: | ||
| from ray._private.authentication.http_token_authentication import ( | ||
| start_metrics_wsgi_server, | ||
| ) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Moved both to the module imports and dropped the comment.
| finally: | ||
| server.shutdown() | ||
| server.server_close() | ||
| assert len(connections) == 2 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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]>
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_METRICSis unset:HAProxyManager, which already starts and manages HAProxy on each node, listens on port 9101 instead. Every request goes throughget_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.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_servernow calls a newstart_metrics_wsgi_server, which serves any WSGI app behind the same check, so existing callers don't change. WithSERVE_SOCKET_REUSE_PORT_ENABLED=1, co-locatedEveryNodeproxies share the metrics port, soHAProxyManagerpassesreuse_portand the server setsSO_REUSEPORTthe way HAProxy's own listeners do. It sets the option inserver_bind()because socketserver'sallow_reuse_portonly 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/metricsmust send the token, as for Ray's other metrics endpoints since #67128. The stats page moves from:8404/statsto:9101/stats, also behind the token. Nothing in Ray reads port 8404 outside tests.RAY_AUTH_SKIP_METRICS=1keeps 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;csvthrough:9101, and nothing answering on:8404.test_haproxy.py::test_haproxy_metrics_and_stats_open_without_token: both pages open without a token, once withRAY_AUTH_SKIP_METRICS=1and once with auth disabled.test_haproxy.py::test_haproxy_metrics_token_auth_with_colocated_reuse_port_proxies: two nodes on one machine withEveryNodeproxies, token auth andSERVE_SOCKET_REUSE_PORT_ENABLED=1. Both proxies come up healthy, and/metricsanswers 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/metricsand/stats;csvare 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 withenable_hap_optimizationon (the-xsocket handoff) and off (plain-sf).test_haproxy_api.py::test_exporter_proxy_app_retries_after_dropped_connection: the retry.test_haproxy.py::test_haproxy_metricsnow sends the token.I ran both HAProxy test files on Linux in a container: the Ray nightly image at
fb22a8a266with 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 fromray-haproxy.test_haproxy.pypassed 37 of 37 andtest_haproxy_api.pypassed 60 of 60, each file in one pytest session. With thereuse_portchange reverted, the co-located test fails withOSError: [Errno 98] Address already in useand the second proxy is marked unhealthy.🤖 Generated with Claude Code