Ë
    uPêj‡'  ã                   ó‚  — U d Z ddlmZmZmZmZ ddlmZ ddlm	Z	 ddl
mZ ddlmZ ddlmZ erddlmZ d	Zeed
<   dZeed<   dZeed<   dZeed<   	 ddeeef   deeeef      dz  ddfd„Z	 	 	 	 ddeeef   deeeef      dz  ded   dededefd„Z	 	 ddeeef   deeeef      dz  deddfd„Zdeeef   fd„Zy) zþ
Policy Initialization - Loads policies from config and validates on startup.

Configuration structure:
- policies: Define WHAT guardrails to apply (with inheritance and conditions)
- policy_attachments: Define WHERE policies apply (teams, keys, models)
é    )ÚTYPE_CHECKINGÚAnyÚFinalÚOptional)Úverbose_proxy_logger)Úget_attachment_registry)Úget_policy_registry)ÚPolicyValidator)ÚPolicyValidationResponse)ÚPrismaClientz[92mÚ_green_color_codez[94mÚ_blue_color_codez[93mÚ_yellow_color_codez[0mÚ_reset_color_codeNÚpolicies_configÚpolicy_attachments_configÚreturnc                 óz  — ddl }t        t        › dt        | «      › dt        › d�«       |j
                  j                  «        | j                  «       D �]!  \  }}|j                  di «      }|j                  d«      }|j                  d«      }|j                  d	«      }t        |t        «      r|j                  d
g «      ng }	t        |t        «      r|j                  dg «      ng }
|rd|› d�nd}t        t        › d|› |› t        › �«       |rt        d|› �«       |	rt        d|	› �«       |
rt        d|
› �«       |sŒít        |t        «      r|j                  d«      nd}|s�Œt        d|› �«       �Œ$ |�rt        dt        › dt        |«      › dt        › �«       |D ]Ø  }|j                  dd«      }|j                  d«      }|j                  d«      }|j                  d«      }|j                  d«      }g }|dk(  r|j                  d«       |r|j                  d|› �«       |r|j                  d |› �«       |r|j                  d!|› �«       |rd"j                  |«      nd#}t        d|› d$|› �«       ŒÚ nt        dt        › d%t        › �«       t        «        |j
                  j                  «        y)&zN
    Print loaded policies to console on startup (similar to model list).
    r   Nz
LiteLLM Policy Engine: Loaded z	 policiesú
Ú
guardrailsÚinheritÚ	conditionÚdescriptionÚaddÚremovez (inherits: ú)Ú z  - z      description: z      guardrails.add: z      guardrails.remove: Úmodelz      condition.model: zPolicy Attachments: z attachment(s)ÚpolicyÚunknownÚscopeÚteamsÚkeysÚmodelsÚ*zscope=* (global)zteams=zkeys=zmodels=z, Úallz -> zXWarning: No policy_attachments configured. Policies will not be applied to any requests.)ÚsysÚprintr   Úlenr   ÚstdoutÚflushÚitemsÚgetÚ
isinstanceÚdictr   r   ÚappendÚjoin)r   r   r'   Úpolicy_nameÚpolicy_datar   r   r   r   Úguardrails_addÚguardrails_removeÚinherit_strÚmodel_conditionÚ
attachmentr   r!   r"   r#   r$   Úscope_partsÚ	scope_strs                        ún/home/oem/CyberBrain26-unified/.venv/lib/python3.12/site-packages/litellm/proxy/policy_engine/init_policies.pyÚ_print_policies_on_startupr<      sË  € ó ä	ÜÐ
Ð=¼cÀ/Ó>RÐ=SÐS\Ô]nÐ\oÐoqÐrôð ‡J�J×ÑÔà$3×$9Ñ$9Ó$;ó CÑ ˆ�[Ø —_‘_ \°2Ó6ˆ
Ø—/‘/ )Ó,ˆØ—O‘O KÓ0ˆ	Ø!—o‘o mÓ4ˆä6@ÀÌTÔ6R˜Ÿ™¨¨rÔ2ÐXZˆÜ<FÀzÔSWÔ<X˜JŸN™N¨8°RÔ8Ð^`ÐÙ3:˜ W I¨QÑ/ÀˆäÜÐ   [ M°+°Ô?PÐ>QÐRô	
ñ ÜÐ'¨ }Ð5Ô6ÙÜÐ*¨>Ð*:Ð;Ô<ÙÜÐ-Ð.?Ð-@ÐAÔBÚÜ8BÀ9ÌdÔ8S˜iŸm™m¨GÔ4ÐY]ˆOÛÜÐ/°Ð/@ÐAÖBð-Cò2 !ÜØÔ#Ð$Ð$8¼Ð=VÓ9WÐ8XÐXfÔgxÐfyÐzô	
ð 4ò 	2ˆJØ—^‘^ H¨iÓ8ˆFØ—N‘N 7Ó+ˆEØ—N‘N 7Ó+ˆEØ—>‘> &Ó)ˆDØ—^‘^ HÓ-ˆFàˆKØ˜Š|Ø×"Ñ"Ð#5Ô6ÙØ×"Ñ" V¨E¨7Ð#3Ô4ÙØ×"Ñ" U¨4¨& >Ô2ÙØ×"Ñ" W¨V¨HÐ#5Ô6Ù2=˜Ÿ	™	 +Ô.À5ˆIä�D˜˜  Y KÐ0Õ1ñ%	2ô( 	ØÔ#Ð$Ð$|ô  ~Oð  }Pð  Qô	
ô 
„GØ‡J�J×ÑÕó    Úprisma_clientr   Úvalidate_dbÚfail_on_errorc              ƒ   óŒ  K  — t        j                  dt        | «      «       t        | |«       t	        «       }t        «       }t        |¬«      }|j                  | |¬«      ƒ d{  –—† }|j                  rG|j                  D ]8  }	t        j                  d|	j                  |	j                  |	j                  «       Œ: |j                  rG|j                  D ]8  }
t        j                  d|
j                  |
j                  |
j                  «       Œ: |j                  si|rg|j                  D �cg c]  }d|j                  › d|j                  › �‘Œ  }}t!        d	t        |j                  «      › d
�dj#                  |«      z   «      ‚	 |j%                  | «       t        j                  dt        | «      «       |r3	 |j)                  |«       t        j                  dt        |«      «       |S |S 7 �Œˆc c}w # t&        $ r}t        j                  d|«       ‚ d}~ww xY w# t&        $ r}t        j                  d|«       ‚ d}~ww xY w­w)aJ  
    Initialize policies from configuration.

    This function:
    1. Parses the policy configuration
    2. Validates policies (guardrails exist, teams/keys exist in DB)
    3. Loads policies into the global registry
    4. Loads attachments into the attachment registry (if provided)

    Args:
        policies_config: Dictionary mapping policy names to policy definitions
        policy_attachments_config: Optional list of policy attachment configurations
        prisma_client: Optional Prisma client for database validation
        validate_db: Whether to validate team/key aliases against database
        fail_on_error: If True, raise exception on validation errors

    Returns:
        PolicyValidationResponse with validation results

    Raises:
        ValueError: If fail_on_error is True and validation errors are found
    zInitializing %s policies...)r>   )r?   Nz(Policy validation error in '%s': [%s] %sz*Policy validation warning in '%s': [%s] %sú[z] zPolicy validation failed with z error(s):
r   zSuccessfully loaded %s policieszFailed to load policies: %sz)Successfully loaded %s policy attachmentsz%Failed to load policy attachments: %s)r   Úinfor)   r<   r	   r   r
   Úvalidate_policy_configÚerrorsÚerrorr2   Ú
error_typeÚmessageÚwarningsÚwarningÚvalidÚ
ValueErrorr1   Úload_policiesÚ	ExceptionÚload_attachments)r   r   r>   r?   r@   Úpolicy_registryÚattachment_registryÚ	validatorÚvalidation_resultrF   rJ   ÚeÚerror_messagess                r;   Úinit_policiesrV   b   s5  è ø€ ô: ×ÑÐ;¼SÀÓ=QÔRô ˜Ð0IÔJô 1Ó2€OÜ!8Ó!:Ðô '°]ÔC€Ið &/×%EÑ%EØØð &Fó &÷  Ðð ×ÒØ&×-Ñ-ò 	ˆEÜ ×&Ñ&Ø:¸E×<MÑ<MÈu×O_ÑO_Ðaf×anÑanõð	ð
 ×!Ò!Ø(×1Ñ1ò 	ˆGÜ ×(Ñ(Ø<¸g×>QÑ>QÐSZ×SeÑSeÐgn×gvÑgvõð	ð ×"Ò"¡}ØK\×KcÑKcÖ dÀa 1 Q§]¡] O°2°a·i±i°[Ò!AÐ dˆÐ dÜØ,¬SÐ1B×1IÑ1IÓ-JÐ,KÈ<ÐXÐ[_×[dÑ[dÐesÓ[tÑtó
ð 	
ð
Ø×%Ñ% oÔ6Ü×!Ñ!Ð"CÄSÈÓEYÔZñ !ð	Ø×0Ñ0Ð1JÔKÜ ×%Ñ%Ð&QÔSVÐWpÓSqÔrð
 ÐÐÐðU ûò( !eøô ò Ü×"Ñ"Ð#@À!ÔDØûðûô ò 	Ü ×&Ñ&Ð'NÐPQÔRØûð	üsm   ‚A"IÁ$G,Á%CIÄ+#G/Å6IÆ0G4 Æ5IÆ80H Ç(IÇ/IÇ4	HÇ=HÈHÈIÈ	IÈ%H<È<IÉIc           	      óÌ   — ddl }	 |j                  «       }|j                  t        | |dd|¬«      «       y# t        $ r$ |j                  «       }|j	                  |«       Y ŒLw xY w)a…  
    Synchronous version of init_policies (without DB validation).

    Use this when async is not available or DB validation is not needed.

    Args:
        policies_config: Dictionary mapping policy names to policy definitions
        policy_attachments_config: Optional list of policy attachment configurations
        fail_on_error: If True, raise exception on validation errors
    r   NF)r   r   r>   r?   r@   )ÚasyncioÚget_event_loopÚRuntimeErrorÚnew_event_loopÚset_event_loopÚrun_until_completerV   )r   r   r@   rX   Úloops        r;   Úinit_policies_syncr_   ¹   sn   € ó ð%Ø×%Ñ%Ó'ˆð
 	×ÑÜØ+Ø&?ØØØ'ô	
õøô	 ò %Ø×%Ñ%Ó'ˆØ×Ñ˜tÖ$ð%ús   †6 ¶*A#Á"A#c            	      ó*  — ddl m}  t        «       }t        «       }|j	                  «       sdi g dœS | j                  «       }dt        |«      t        |j                  «       «      i g dœ}|j                  «       D ]¶  \  }}|j                  |«      }|r|j                  nd|r|j                  nd|r|j                  j                  «       ng |r|j                  j                  «       ng |r&|j                  r|j                  j!                  «       nd|j                  |j"                  dœ|d	   |<   Œ¸ |j                  «       D ]N  }|d
   j%                  |j&                  |j(                  |j*                  |j,                  |j.                  dœ«       ŒP |S )zz
    Get a summary of loaded policies for debugging/display.

    Returns:
        Dictionary with policy information
    r   )ÚPolicyResolverF)ÚinitializedÚpoliciesÚattachmentsT)rb   Úpolicy_countÚattachment_countrc   rd   N)r   r   r4   r5   r   Úresolved_guardrailsÚinheritance_chainrc   rd   )r   r!   r"   r#   r$   )Ú+litellm.proxy.policy_engine.policy_resolverra   r	   r   Úis_initializedÚget_all_resolved_policiesr)   Úget_all_attachmentsr,   Ú
get_policyr   r   r   Úget_addÚ
get_remover   Ú
model_dumprh   r0   r   r!   r"   r#   r$   )	ra   rP   rQ   ÚresolvedÚsummaryr2   Úresolved_policyr   r8   s	            r;   Úget_policies_summaryrt   Ü   s‚  € õ Kä0Ó2€OÜ!8Ó!:Ðà×)Ñ)Ô+Ø$°"ÀRÑHÐHà$×>Ñ>Ó@€Hð Ü˜H›ÜÐ 3× GÑ GÓ IÓJØØñ&€Gð )1¯©Ó(8ò 

Ñ$ˆ�_Ø ×+Ñ+¨KÓ8ˆá)/�v—~’~°TÙ17˜6×-Ò-¸TÙ=C˜f×/Ñ/×7Ñ7Ô9ÈÙCI ×!2Ñ!2×!=Ñ!=Ô!?ÈrÙ;AÀf×FVÒFV˜&×*Ñ*×5Ñ5Ô7Ð\`Ø#2×#=Ñ#=Ø!0×!BÑ!Bñ,
ˆ�
Ñ˜KÒ(ð

ð *×=Ñ=Ó?ò 	
ˆ
Ø�Ñ×%Ñ%à$×+Ñ+Ø#×)Ñ)Ø#×)Ñ)Ø"Ÿ™Ø$×+Ñ+ñõ	
ð	
ð €Nr=   )N)NNTT)NT)Ú__doc__Útypingr   r   r   r   Úlitellm._loggingr   Ú/litellm.proxy.policy_engine.attachment_registryr   Ú+litellm.proxy.policy_engine.policy_registryr	   Ú,litellm.proxy.policy_engine.policy_validatorr
   Ú!litellm.types.proxy.policy_enginer   Úlitellm.proxy.utilsr   r   Ú__annotations__r   r   r   r/   ÚstrÚlistr<   ÚboolrV   r_   rt   © r=   r;   ú<module>r‚      sm  ðò÷ 7Ó 6å 1Ý SÝ KÝ HÝ FáÝ0ð &Ð �5Ó %Ø$Ð �%Ó $Ø&Ð �EÓ &Ø$Ð �5Ó $ð
 >BñDØ˜#˜s˜(‘^ðDà# D¨¨c¨¡NÑ3°dÑ:ðDð 
óDðR >BØ.2ØØñTØ˜#˜s˜(‘^ðTà# D¨¨c¨¡NÑ3°dÑ:ðTð ˜NÑ+ðTð ð	Tð
 ðTð óTðr >BØñ Ø˜#˜s˜(‘^ð à# D¨¨c¨¡NÑ3°dÑ:ð ð ð ð 
ó	 ðF1˜d 3¨ 8™nô 1r=   