
    MiR                        S SK r S SKrS SKrS SKJrJrJrJr  S SKrS SK	J
r  S SKJrJr  S SKJrJr  S SKJr  SSKJr  S rSS	 jrSS
 jrS rS rS\S\4S jr SS\S\\   S\4S jjrS rS rS\R@                  4S jr!S\RD                  4S jr#S\RD                  4S jr$S r% SS\\RL                     S\\RD                  \R@                  4   4S jjr'S r(S\)SS4S jr*g)    N)CallableListOptionalUnion)
get_secretget_secret_str)REDIS_CONNECTION_POOL_TIMEOUTREDIS_SOCKET_TIMEOUT)SensitiveDataMasker   )verbose_loggerc                      [         R                  " [        R                  5      n 1 Skn/ SQnU R                   Vs/ s H  o3U;  d  M
  UPM     snU-   nU$ s  snf )N>   selfretryconnection_pool)urlredis_connect_funcgcp_service_accountgcp_ssl_ca_certs)inspectgetfullargspecredisRedisargs)arg_specexclude_argsinclude_argsxavailable_argss        h/var/www/eduai.edurigo.com/storigo/production/storigo_env/lib/python3.13/site-packages/litellm/_redis.py_get_redis_kwargsr!      sS    %%ekk2HL \L!)HA<2GaH<WN Is   	AAc                    U c  [         R                  R                  n [        R                  " [         R                  R                  5      n1 SknS/nUR
                   Vs/ s H  oDU;  d  M
  UPM     snU-   nU$ s  snf )N>   r   r   r   r   )r   r   from_urlr   r   r   )clientr   r   r   r   r   s         r    _get_redis_url_kwargsr%   ,   sn    ~%%%%ekk&:&:;HL 7L!)HA<2GaH<WN Is   !	A<.A<c                 4   U c  [         R                  R                  n [        R                  " [         R
                  5      n1 SknUR                   Vs/ s H  o3U;  d  M
  UPM     nnUR                  S5        UR                  S5        UR                  S5        UR                  S5        UR                  S5        UR                  S5        UR                  S5        UR                  S	5        UR                  S
5        UR                  S5        U$ s  snf )N>   hostportr   r   startup_nodesr   passwordusernamesslssl_cert_reqsssl_check_hostnamessl_ca_certsr   r   r   max_connections)r   r   r#   r   r   RedisClusterr   append)r$   r   r   r   r   s        r    _get_redis_cluster_kwargsr3   ?   s    ~%%%%e&8&89H YL!)HA<2GaNH*%*%% /*./.).//0,-+, Is   	D!Dc                  j    Sn [        5        Vs0 s H  o UR                  5        3U_M     sn$ s  snf )NREDIS_)r!   upper)PREFIXr   s     r    _get_redis_env_kwarg_mappingr8   V   s6    F/@/BC/B!hqwwyk"A%/BCCCs   0c                  v    [        5       n 0 nU R                  5        H  u  p#[        US S9nUc  M  XAU'   M     U$ )N)default_value)r8   itemsr   )mappingreturn_dictkvvalues        r    _redis_kwargs_from_environmentrA   \   sB    *,GK1D1"N       service_accountreturnc                      SSK Jn  UR                  5       nUR	                  U S/S9nUR                  US9n[        UR                  5      $ ! [         a    [        S5      ef = f)z
Generate GCP IAM access token for Redis authentication.

Args:
    service_account: GCP service account in format 'projects/-/serviceAccounts/name@project.iam.gserviceaccount.com'

Returns:
    Access token string for GCP IAM authentication
r   )iam_credentials_v1zlgoogle-cloud-iam is required for GCP IAM Redis authentication. Install it with: pip install google-cloud-iamz.https://www.googleapis.com/auth/cloud-platform)namescope)request)google.cloudrF   ImportErrorIAMCredentialsClientGenerateAccessTokenRequestgenerate_access_tokenstraccess_token)rC   rF   r$   rI   responses        r    _generate_gcp_iam_access_tokenrR   g   s    
3  446F ;;?@ < G ++G+<Hx$$%%  
<
 	

s   A A#r/   c                    ^  U 4S jnU$ )aU  
Creates a custom Redis connection function for GCP IAM authentication.

Args:
    service_account: GCP service account in format 'projects/-/serviceAccounts/name@project.iam.gserviceaccount.com'
    ssl_ca_certs: Path to SSL CA certificate file for secure connections

Returns:
    A connection function that can be used with Redis clients
c                   > SSK JnJn  SSKJn  U R
                  R                  U 5        [        T5      4nU R                  " S/UQ7SS06   U R                  5       nU" U5      S	:w  a  U" S
5      eg! U aQ    [        U S5      (       a>  U R                  (       a-  U R                  SU R                  SS9  U R                  5       n Nje f = f)z8Initialize the connection and authenticate using GCP IAMr   )AuthenticationError$AuthenticationWrongNumberOfArgsError)str_if_bytesAUTHcheck_healthFr*   )rY   OKzGCP IAM authentication failedN)redis.exceptionsrU   rV   redis.utilsrW   _parser
on_connectrR   send_commandread_responsehasattrr*   )r   rU   rV   rW   	auth_argsauth_responserC   s         r    iam_connect6create_gcp_iam_redis_connect_func.<locals>.iam_connect   s    	
 	-%3ODF	&A9A5A	 ..0M &$.%&EFF / 4 	tZ((T]]!!&$--e!L $ 2 2 4	s   A3 3AC
C
 )rC   r/   rd   s   `  r    !create_gcp_iam_redis_connect_funcrg      s    G4 rB   c                     S[         R                  ;   a  [         R                  S   $ S[         R                  ;  d  S[         R                  ;  a  [        S5      eS[         R                  ;   a(  [         R                  S   R                  5       S:X  a  Sn OSn S	nS
[         R                  ;   a?  S[         R                  ;   a+  [         R                  S
    S[         R                  S    S3nO*S[         R                  ;   a  [         R                  S    S3nU  SU [         R                  S    S[         R                  S    3$ )N	REDIS_URL
REDIS_HOST
REDIS_PORTzUEither 'REDIS_URL' or both 'REDIS_HOST' and 'REDIS_PORT' must be specified for Redis.	REDIS_SSLtrueredissr    REDIS_USERNAMEREDIS_PASSWORD:@z://)osenviron
ValueErrorlower)redis_protocol	auth_parts     r    get_redis_url_from_environmentrz      s   bjj zz+&&2::%RZZ)Gc
 	
 bjj RZZ%<%B%B%D%N!  I2::%*:bjj*Hzz"234AbjjAQ6R5SSTU		RZZ	'zz"234A6	 
#i[L)A(B!BJJ|D\C]^rB   c                     U R                  5        HU  u  p[        U[        5      (       d  M  UR                  S5      (       d  M4  UR	                  SS5      n[        U5      nX0U'   MW     0 [        5       EU EnUR                  SS5      =(       d    [        S5      nUb.  [        U[        5      (       a  [        R                  " U5      US'   UR                  SS5      =(       d    [        S5      nUb.  [        U[        5      (       a  [        R                  " U5      US'   UR                  SS5      =(       d    [        S	5      nUb  XtS'   UR                  S
S5      =(       d    [        S5      nUb  XS
'   UR                  S5      =(       d    [        S5      n	UR                  S5      =(       d    [        S5      n
U	br  [        R                  " S5        [        U	U
S9US'   XS   l        UR                  SS5        UR                  SS5        U
(       a  UR                  SS5      (       a  XS'   SU;   aP  US   bJ  UR                  SS5        UR                  SS5        UR                  SS5        UR                  SS5        U$ SU;   a	  US   b   U$ SU;   a	  US   b   U$ SU;  d  US   c  [!        S5      eU$ )zG
Common functionality across sync + async redis client implementations
zos.environ/ro   r)   NREDIS_CLUSTER_NODESsentinel_nodesREDIS_SENTINEL_NODESsentinel_passwordREDIS_SENTINEL_PASSWORDservice_nameREDIS_SERVICE_NAMEr   REDIS_GCP_SERVICE_ACCOUNTr   REDIS_GCP_SSL_CA_CERTSzASetting up GCP IAM authentication for Redis with service account.)rC   r/   r   r,   Fr/   r   r'   r(   dbr*   z3Either 'host' or 'url' must be specified for redis.)r;   
isinstancerO   
startswithreplacer   rA   getjsonloadsr   r   debugrg   _gcp_service_accountpoprv   )env_overridesr>   r?   r@   redis_kwargs_startup_nodes_sentinel_nodes_sentinel_password_service_namer   _gcp_ssl_ca_certss              r    _get_redis_client_logicr      s   
 ##%a!,,}"="=		-,AqME$!	 &
(
*
L
 2>1A1A/SW1X 2\f]N !j&E&E(,

>(B_%2>2B2BCSUY2Z 3^h_O "z/3'G'G)-O)D%&(4(8(8T) )3	1	2  %,>()#/#3#3ND#I $ZNM  '4^$ (++,ABqnUpFq$(();<hOg@h'`a-N0*.
)*
 CW)*? 	.5+T2 !1!1%!?!?+<(e!4!@&&t$T*  
L	(\/-J-V  	L(\:J-K-W
 	 
|	#|F';'CNOO rB   c                    [        S5      nUb   [        R                  " U5      U S'   [
        R                  " S5        SSKJn  [        5       n0 nU  H  nXS;   d  M
  X   XE'   M     / nU S    H  nUR                  U" S0 UD65        M     UR                  SS 5        [        R                  " SSU0UD6$ ! [        R                   a    [	        S5      ef = f)Nr|   r)   zbREDIS_CLUSTER_NODES environment variable is not valid JSON. Please ensure it's properly formatted.z8init_redis_cluster: startup nodes are being initialized.r   ClusterNoderf   )r   r   r   JSONDecodeErrorrv   r   r   redis.clusterr   r3   r2   r   r   r1   )r   _redis_cluster_nodes_in_envr   r   cluster_kwargsargnew_startup_nodesitems           r    init_redis_clusterr     s    1;<Q1R".	,0JJ7R,SL) ST)$&DN;"."3N  ,._-  !4t!45 . -P,=PPP) ## 	t 	s   B6 6 Cc                    U R                  S5      nU R                  S5      nU R                  S5      nU(       a  U(       d  [        S5      e[        R                  " S5        [        R
                  " U[        US9nUR                  U5      $ Nr}   r   r   zIBoth 'sentinel_nodes' and 'service_name' are required for Redis Sentinel.z:init_redis_sentinel: sentinel nodes are being initialized.)socket_timeoutr*   )r   rv   r   r   r   Sentinelr
   
master_forr   r}   r   r   sentinels        r    _init_redis_sentinelr   6  s    !%%&67N$(()<=##N3LW
 	
 UV ~~+"H |,,rB   c                    U R                  S5      nU R                  S5      nU R                  S5      nU(       a  U(       d  [        S5      e[        R                  " S5        [        R
                  " U[        US9nUR                  U5      $ r   )r   rv   r   r   async_redisr   r
   r   r   s        r    _init_async_redis_sentinelr   N  s    !%%&67N$(()<=##N3LW
 	
 UV ##+"H |,,rB   c                  H   [        S0 U D6nSU;   aH  US   bB  [        5       n0 nU H  nXB;   d  M
  X   X4'   M     [        R                  R                  " S0 UD6$ SU;   d  [        S5      b  [        U5      $ SU;   a  SU;   a  [        U5      $ [        R                  " S0 UD6$ )Nr   r)   r|   r}   r   rf   )r   r%   r   r   r#   r   r   r   )r   r   r   
url_kwargsr   s        r    get_redis_clientr   f  s    *;];Le!4!@$&
C{"."3
   {{##1j11,&*5J*K*W!,// <'Nl,J#L11;;&&&rB   r   c                    [        S0 UD6nSU;   a  US   b~  [        [        R                  R                  S9n0 nU H5  nXS;   a  X%   XE'   M  [
        R                  " SR                  U5      5        M7     [        R                  R                  " S0 UD6$ SU;   Gac  SSKJ	n  [        5       n0 nU H  nXS;   d  M
  X%   Xu'   M     UR                  SS 5      nSSKJn	  S n
U(       a  [        US	5      (       a  UR                  n
O UR!                  S
5      =(       d    U	" S5      n
[
        R                  " SUS L SU
S L 35        U(       aD  U
(       a=  [
        R                  " S5         [#        U
5      nXS'   [
        R                  " S5        O [
        R                  " SUS L SU
S L 35        / nUS    H  nUR-                  U" S0 UD65        M     UR                  SS 5        [        R.                  " SSU0UD6nU$ SU;   a  SU;   a  [1        U5      $ [3        US9  U b  XS'   [        R                  " S0 UD6$ ! [$         a,  n[
        R&                  " SU 35        SSKJn  U" S5      eS nAff = f)Nr   )r$   zLREDIS: ignoring argument: {}. Not an allowed async_redis.Redis.from_url arg.r)   r   r   r   )r   r   r   r   z0DEBUG: Redis cluster kwargs: redis_connect_func=z, gcp_service_account_provided=zWDEBUG: Generating IAM token for service account (value not logged for security reasons)r*   zJDEBUG: Successfully generated GCP IAM access token for async Redis clusterz)Failed to generate GCP IAM access token: )rU   z'Failed to generate GCP IAM access tokenz3DEBUG: Not using GCP IAM auth - redis_connect_func=r}   r   )r   r   rf   )r   r%   r   r   r#   r   r   formatr   r   r3   r   litellmr   ra   r   r   rR   	Exceptionerrorr[   rU   r2   r1   r   _pretty_print_redis_config)r   r   r   r   r   r   r   r   r   r   r   rP   erU   r   r   cluster_clients                    r    get_redis_async_clientr   {  s    +;];Le!4!@$K,=,=,F,FG
C{"."3
$$bii	     ))7J77,&-(*C{&2&7#  
 ,//0DdK* #'*<>T"U"U"4"I"I"."2"23H"I"x^\wMxOPbjnPnOo  pO  Pc  ko  Po  Op  q  	r "5  !z{U=>QR-9z*$$%qr   #VWiquWuVv  wV  Wj  rv  Wv  Vw  "x  y/1 1D$$[%84%89 2?D1 %11 
+
/=
  <'Nl,J),77L9"*9&' 
 9  U$$'PQRPS%TU@)*STTUs   )%H= =
I3'I..I3c                  |   [        S0 U D6n[        R                  " SU5        SU;   a+  US   b%  [        R                  R                  [        US   S9$ [        R                  nSU;   a&  [        R                  nUR                  SS 5        X!S'   UR                  SS 5        [        R                  " SS[        0UD6$ )	Nz'get_redis_connection_pool: redis_kwargsr   )timeoutr   r,   connection_classr)   r   rf   )
r   r   r   r   BlockingConnectionPoolr#   r	   
ConnectionSSLConnectionr   )r   r   r   s      r    get_redis_connection_poolr     s    *;];LBLQe!4!@11::1|E7J ; 
 	
 #--&44%+;'(_d+-- -1= rB   r   c                     SSK nSSKJn  SSKJn  SSKJn  SSKJn  [        R                  " UR                  5      (       d  gU" 5       n[        5       nUR                  U 5      nU" SSS	9n	U" S
SSSS9n
U
R                  SSSS9  U
R                  SSS	9  UR                  5        H  u  pUc  M
  [!        U["        5      (       aF  US:X  a  U(       a  S[%        U5       S3nO4US:X  a  U(       a  S[%        U5       S3nO['        U5      nO['        U5      nU
R)                  X5        M     SnUR+                  S5      (       a  SnO1UR+                  S5      (       a  SnOUR+                  S5      (       a  SnU" SSSSS9nUR                  S SSS9  UR                  SSS	9  UR)                  S!U5        UR-                  S"5        UR-                  U" U	S#S$95        UR-                  U5        UR-                  U
5        UR-                  S"5        g! [.         a7    [        5       nUR                  U 5      n[        R0                  " S%U 35         g[2         a#  n[        R4                  " S&U 35         SnAgSnAff = f)'zKPretty print the Redis configuration using rich with sensitive data maskingr   N)Console)Panel)Table)TextzRedis Configurationz	bold blue)styleu    🔧 Redis Connection ParametersTzbold magentaleft)titleshow_headerheader_styletitle_justify	Parametercyan)r   no_wrapValueyellowr)   [z cluster nodes]r}   z sentinel nodes]zStandard RediszRedis ClusterzRedis Sentinelr   zRedis (URL-based)u   📊 Connection Infoz
bold greenPropertyzConnection Type
blue)border_stylezRedis configuration: z+Error pretty printing Redis configuration: )loggingrich.consoler   
rich.panelr   
rich.tabler   	rich.textr   r   isEnabledForDEBUGr   	mask_dict
add_columnr;   r   listlenrO   add_rowr   printrK   infor   r   )r   r   r   r   r   r   consolemaskermasked_redis_kwargsr   config_tablekeyr@   	value_strconnection_type
info_tabler   s                    r    r   r     sp   RP($$"**7==99) %& %..|< *+> 4' 	
 	64Hx8 .335JC eT**o-%&'E
|?$C	 00U&'E
|3C$D	$'J	 #E
I$$S4 6$ +""?33-O $$%566.O $$U++1O (% 	

 	jEgX6,o> 	deE78j!l#d K$&$..|<34G3HIJ PJ1#NOOPs,   AH A)H 1EH >I;	I;I66I;)N)+r   r   rt   typingr   r   r   r   r   redis.asyncioasyncior   r   r   r   litellm.constantsr	   r
   0litellm.litellm_core_utils.sensitive_data_maskerr   _loggingr   r!   r%   r3   r8   rA   rO   rR   rg   rz   r   r1   r   r   r   r   r   r   r   r   dictr   rf   rB   r    <module>r      s&     
 2 2  # . Q P $"&.D&C &C &: #'((3-( (V4PfQ(:(: Q8-%++ -0-0A0A -0', EINk@@AN
;k6667Nb"TPT TPd TPrB   