
    &`i                         d Z ddlZddlmZ ddlmZmZmZ ddlm	Z	 ddl
mZ ddlmZmZmZ ddlmZ  ej        e          Zdd
Zd	efdZ	 ddeeeef                  ded	dfdZdS )zAuthentication token setup for Ray.

This module provides functions to generate and save authentication tokens
for Ray's token-based authentication system. Token loading and caching is
handled by the C++ AuthenticationTokenLoader.
    N)Path)AnyDictOptional)3TOKEN_AUTH_ENABLED_BUT_NO_TOKEN_FOUND_ERROR_MESSAGE)!generate_new_authentication_token)AuthenticationModeAuthenticationTokenLoaderget_authentication_mode)AuthenticationErrorreturnc                  J   t                      } t                      }	 |j                            dd           t	          |d          5 }|                    |            ddd           n# 1 swxY w Y   t                              d|            dS # t          $ r  w xY w)zGenerate a new random token and save it in the default token path.

    Returns:
        The newly generated authentication token.
    T)parentsexist_okwNz0Generated new authentication token and saved to )	r   _get_default_token_pathparentmkdiropenwriteloggerinfo	Exception)token
token_pathfs      /home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/_private/authentication/authentication_token_setup.pygenerate_and_save_tokenr      s     .//E(**J
t<<< *c"" 	aGGENNN	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	SzSSTTTTT   s/   ,B 
A, B ,A00B 3A04 B B"c                  4    t          j                    dz  dz  S )zyGet the default token file path (~/.ray/auth_token).

    Returns:
        Path object pointing to ~/.ray/auth_token
    z.ray
auth_token)r   home     r   r   r   3   s     9;;,..r#   Tsystem_configcreate_token_if_missingc                 J   t                      t          j        k    r#| rd| v r| d         dk    rt          d          dS t	          j                    }|                    d          s:|r$t                       |                                 dS t          t                    dS )a  Check authentication settings and set up token resources if authentication is enabled.

    Ray calls this early during ray.init() to do the following for token-based authentication:
    1. Check whether you enabled token-based authentication.
    2. Make sure a token is available if authentication is enabled.
    3. Generate and save a default token for new local clusters if one doesn't already exist.

    Args:
        system_config: Ray raises an error if you set AUTH_MODE in system_config instead of the environment.
        create_token_if_missing: Generate a new token if one doesn't already exist.

    Raises:
        RuntimeError: Ray raises this error if authentication is enabled but no token is found when connecting
            to an existing cluster.
    	AUTH_MODEdisabledzsSet authentication mode can only be set with the `RAY_AUTH_MODE` environment variable, not using the system_config.NT)ignore_auth_mode)r   r	   TOKENRuntimeErrorr
   instance	has_tokenr   reset_cacher   r   )r$   r%   token_loaders      r   ensure_token_if_auth_enabledr0   <   s    (   $6$<<<	},,k*j88 F   	,577L!!4!88 
" 		#%%% $$&&&&&%C  
 
r#   )r   N)NT)__doc__loggingpathlibr   typingr   r   r   4ray._private.authentication.authentication_constantsr   :ray._private.authentication.authentication_token_generatorr   ray._rayletr	   r
   r   ray.exceptionsr   	getLogger__name__r   r   r   strboolr0   r"   r#   r   <module>r=      s\           & & & & & & & & & &                   
 / . . . . .		8	$	$   ./ / / / / UY+ +DcN++MQ+	+ + + + + +r#   