1212import yaml
1313
1414import ray
15- from ray ._private .authentication import authentication_constants
15+ from ray ._private .authentication .http_token_authentication import (
16+ format_authentication_http_error ,
17+ get_auth_headers_if_auth_enabled ,
18+ )
1619from ray ._private .runtime_env .packaging import (
1720 create_package ,
1821 get_uri_for_directory ,
2124from ray ._private .runtime_env .py_modules import upload_py_modules_if_needed
2225from ray ._private .runtime_env .working_dir import upload_working_dir_if_needed
2326from ray ._private .utils import split_address
24- from ray ._raylet import AuthenticationTokenLoader
2527from ray .autoscaler ._private .cli_logger import cli_logger
26- from ray .dashboard .authentication_utils import is_token_auth_enabled
2728from ray .dashboard .modules .job .common import uri_to_http_components
2829from ray .util .annotations import DeveloperAPI , PublicAPI
2930
@@ -226,7 +227,7 @@ def __init__(
226227 # Headers used for all requests sent to job server, optional and only
227228 # needed for cases like authentication to remote cluster.
228229 self ._headers = cluster_info .headers or {}
229- self ._headers .update (** self ._get_auth_headers ( ))
230+ self ._headers .update (** get_auth_headers_if_auth_enabled ( self ._headers ))
230231
231232 # Set SSL verify parameter for the requests library and create an ssl_context
232233 # object when needed for the aiohttp library.
@@ -247,36 +248,6 @@ def __init__(
247248 else :
248249 self ._ssl_context = None
249250
250- def _get_auth_headers (self ) -> Dict [str , str ]:
251- """Get authentication headers if token auth is enabled.
252-
253- Returns:
254- dict: Authentication headers to merge with request headers.
255- Empty dict if no auth needed or token unavailable.
256- """
257- if not is_token_auth_enabled ():
258- return {}
259-
260- # Check if user provided their own Authorization header (case-insensitive)
261- has_user_auth = any (
262- key .lower () == "authorization" for key in self ._headers .keys ()
263- )
264- if has_user_auth :
265- # User has provided their own auth header, don't override
266- return {}
267-
268- token_loader = AuthenticationTokenLoader .instance ()
269- auth_headers = token_loader .get_token_for_http_header ()
270-
271- if not auth_headers :
272- # Token auth enabled but no token found
273- logger .warning (
274- "Token authentication is enabled but no token was found. "
275- "Requests to authenticated clusters will fail."
276- )
277-
278- return auth_headers
279-
280251 def _check_connection_and_version (
281252 self , min_version : str = "1.9" , version_error_message : str = None
282253 ):
@@ -348,18 +319,11 @@ def _do_request(
348319 )
349320
350321 # Check for authentication errors and provide helpful messages
351- if response .status_code == 401 :
352- # Unauthorized - missing or no token provided
353- raise RuntimeError (
354- f"Authentication required: { response .text } \n \n "
355- + authentication_constants .HTTP_REQUEST_MISSING_TOKEN_ERROR_MESSAGE
356- )
357- elif response .status_code == 403 :
358- # Forbidden - invalid token
359- raise RuntimeError (
360- f"Authentication failed: { response .text } \n \n "
361- + authentication_constants .HTTP_REQUEST_INVALID_TOKEN_ERROR_MESSAGE
362- )
322+ formatted_error = format_authentication_http_error (
323+ response .status_code , response .text
324+ )
325+ if formatted_error :
326+ raise RuntimeError (formatted_error )
363327
364328 return response
365329
0 commit comments