Ë
    uPêj?  ã                   óö   — U d dl Z d dlZd dlZd dlmZmZ d dlmZ dZee	d<   dZ
ee	d<   i Zeeeeeef   f      e	d<    ej                   «       Zee	d	<   d
edefd„Zd
edefd„Z G d„ de«      Z G d„ de«      Zy)é    N)ÚAnyÚFinal)ÚCredentialProviderz https://redis.azure.com/.defaultÚAZURE_REDIS_SCOPEiä  Ú_GCP_IAM_TOKEN_TTL_SECONDSÚ_token_cacheÚ_token_cache_lockÚservice_accountÚreturnc                 óÖ   — 	 ddl m} |j                  «       }|j	                  | dg¬«      }|j                  |¬«      }t        |j                  «      S # t        $ r t        d«      ‚w xY w)a
  
    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.cloudr   ÚImportErrorÚIAMCredentialsClientÚGenerateAccessTokenRequestÚgenerate_access_tokenÚstrÚaccess_token)r
   r   Úclientr   Úresponses        úg/home/oem/CyberBrain26-unified/.venv/lib/python3.12/site-packages/litellm/_redis_credential_provider.pyÚ_generate_gcp_iam_access_tokenr      s‚   € ð
Ý3ð '×;Ñ;Ó=€FØ'×BÑBØØ?Ð@ð Có €Gð ×2Ñ2¸7Ð2ÓC€HÜˆx×$Ñ$Ó%Ð%øô ò 
Üð<ó
ð 	
ð
ús   ‚A ÁA(c                 ó‚  — t         j                  | «      }|�|\  }}t        j                  «       |k  r|S t        5  t         j                  | «      }|�'|\  }}t        j                  «       |k  r|cddd«       S t        | «      }|t        j                  «       t        z   ft         | <   |cddd«       S # 1 sw Y   yxY w)ay  
    Return a cached GCP IAM token, refreshing only when expired.

    Uses a module-level cache shared across all GCPIAMCredentialProvider
    instances for the same service account. The threading.Lock ensures only
    one thread performs the network round-trip on expiry; all others wait
    briefly and read the fresh token (double-checked locking pattern).

    This avoids N concurrent blocking IAM refreshes when N Redis connections
    are established simultaneously (e.g. during health checks or pool warm-up),
    which would otherwise serialise inside Python's async event loop and cause
    cascading request latency.
    N)r   ÚgetÚtimeÚ	monotonicr	   r   r   )r
   ÚcachedÚtokenÚexpirys       r   Ú_get_cached_gcp_iam_tokenr#   0   sº   € ô ×Ñ˜oÓ.€FØÐØ‰ˆˆvÜ�>‰>Ó˜fÒ$ØˆLä	ñ ä×!Ñ! /Ó2ˆØÐØ"‰MˆE�6Ü�~‰~Ó &Ò(Ø÷ñ ô /¨Ó?ˆàÜ�N‰NÓÔ9Ñ9ð)
Œ�_Ñ%ð ÷÷ ò ús   ¼5B5Á;0B5Â5B>c                   óD   — e Zd ZdZdeddfd„Zdee   fd„Zdee   fd„Zy)ÚGCPIAMCredentialProvideruß  
    redis.credentials.CredentialProvider implementation that supplies GCP IAM tokens
    for Redis authentication, with module-level caching per service account.

    Tokens are cached for _GCP_IAM_TOKEN_TTL_SECONDS (55 min) so that repeated
    connection establishments â€” e.g. during connection pool warm-up or health checks â€”
    do not each trigger a synchronous network round-trip that would block Python's
    async event loop and cause cascading request latency.
    Úgcp_service_accountr   Nc                 ó   — || _         y ©N)Ú_gcp_service_account)Úselfr&   s     r   Ú__init__z!GCPIAMCredentialProvider.__init___   s
   € Ø$7ˆÕ!ó    c                 ó2   — t        | j                  «      }|fS r(   )r#   r)   ©r*   r!   s     r   Úget_credentialsz(GCPIAMCredentialProvider.get_credentialsb   s   € Ü0°×1JÑ1JÓKˆØˆxˆr,   c              ƒ   ól   K  — t        j                  t        | j                  «      ƒ d {  –—† }|fS 7 Œ­wr(   )ÚasyncioÚ	to_threadr#   r)   r.   s     r   Úget_credentials_asyncz.GCPIAMCredentialProvider.get_credentials_asyncf   s/   è ø€ Ü$×.Ñ.Ô/HÈ$×JcÑJcÓd×dˆØˆxˆð eús   ‚(4ª2«4)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r+   Útupler/   r3   © r,   r   r%   r%   T   s:   „ ñð8¨Cð 8°Dó 8ð  s¡ó ð¨U°3©Zô r,   r%   c                   óp   — e Zd ZdZd	dededz  ddfd„Zdee   eeef   z  fd„Zdee   eeef   z  fd„Z	y)
ÚAzureADCredentialProvidera°  
    redis.credentials.CredentialProvider implementation that supplies Azure AD
    tokens for Redis authentication.

    Wraps an azure-identity credential object so the Azure SDK's internal token
    cache and silent refresh are honoured on every Redis connection. This avoids
    the static-token-baked-in-pool issue where pool-managed connections would
    fail authentication after the initial token expired (~1 hour TTL).
    NÚ
credentialÚusernamer   c                 ó    — || _         || _        y r(   )Ú_credentialÚ	_username)r*   r<   r=   s      r   r+   z"AzureADCredentialProvider.__init__v   s   € Ø%ˆÔØ!ˆ�r,   c                 óŽ   — | j                   j                  t        «      j                  }| j                  r| j                  |fS |fS r(   )r?   Ú	get_tokenr   r!   r@   r.   s     r   r/   z)AzureADCredentialProvider.get_credentialsz   s<   € Ø×'Ñ'×1Ñ1Ô2CÓD×JÑJˆØ�>Š>Ø—N‘N EÐ*Ð*Øˆxˆr,   c              ƒ   óÜ   K  — t        j                  | j                  j                  t        «      ƒ d {  –—† }| j
                  r| j
                  |j                  fS |j                  fS 7 Œ5­wr(   )r1   r2   r?   rB   r   r@   r!   )r*   Ú	token_objs     r   r3   z/AzureADCredentialProvider.get_credentials_async€   sU   è ø€ Ü!(×!2Ñ!2°4×3CÑ3C×3MÑ3MÔO`Ó!a×aˆ	Ø�>Š>Ø—N‘N I§O¡OÐ4Ð4Ø—‘Ð!Ð!ð bús   ‚2A,´A*µ6A,r(   )
r4   r5   r6   r7   r   r   r+   r8   r/   r3   r9   r,   r   r;   r;   k   s_   „ ññ" 3ð "°#¸±*ð "Èó "ð  s¡¨e°C¸°H©oÑ!=ó ð"¨U°3©Z¸%ÀÀSÀ¹/Ñ-Iô "r,   r;   )r1   Ú	threadingr   Útypingr   r   Úredis.credentialsr   r   Ú__annotations__r   r   Údictr   r8   ÚfloatÚLockr	   r   r#   r%   r;   r9   r,   r   ú<module>rL      s§   ðÜ Û Û ß å 0ð >Ð �5Ó =ð %)Ð ˜EÓ (ð
 57€ˆe�D˜˜e C¨ JÑ/Ð/Ñ0Ñ1Ó 6Ø)˜9Ÿ>™>Ó+Ð �5Ó +ð&°Cð &¸Có &ð6!¨sð !°só !ôHÐ1ô ô."Ð 2õ "r,   