Ë
    uPêj  ã                   óT   — d Z ddlmZ ddlmZ ddlmZ ddlmZm	Z	m
Z
  G d„ d«      Zy)	a%  
Policy Matcher - Matches requests against policy attachments.

Uses existing wildcard pattern matching helpers to determine which policies
apply to a given request based on team alias, key alias, and model.

Policies are matched via policy_attachments which define WHERE each policy applies.
é    )ÚFinal)Úverbose_proxy_logger)ÚRouteChecks)ÚPolicyÚPolicyMatchContextÚPolicyScopec                   óÚ   — e Zd ZdZededz  dee   defd„«       Zede	de
defd	„«       Zede
dee   fd
„«       Zede
dee   fd„«       Ze	 ddee   de
deeef   dz  dee   fd„«       Zy)ÚPolicyMatchera  
    Matches incoming requests against policy attachments.

    Supports wildcard patterns:
    - "*" matches everything
    - "prefix-*" matches anything starting with "prefix-"

    Uses policy_attachments to determine which policies apply to a request.
    ÚvalueNÚpatternsÚreturnc                 óP   — | €d|v S |D ]  }t        j                  | |¬«      sŒ y y)av  
        Check if a value matches any of the given patterns.

        Uses the existing RouteChecks.route_matches_wildcard_pattern helper.

        Args:
            value: The value to check (e.g., team alias, key alias, model)
            patterns: List of patterns to match against

        Returns:
            True if value matches any pattern, False otherwise
        Ú*)ÚrouteÚpatternTF)r   Úroute_matches_wildcard_pattern)r   r   r   s      úo/home/oem/CyberBrain26-unified/.venv/lib/python3.12/site-packages/litellm/proxy/policy_engine/policy_matcher.pyÚmatches_patternzPolicyMatcher.matches_pattern   s:   € ð ˆ=Ø˜(�?Ð"àò 	ˆGä×9Ñ9ÀÈwÖWÙð	ð
 ó    ÚscopeÚcontextc                 óœ  ‡— t         j                  |j                  | j                  «       «      syt         j                  |j                  | j                  «       «      syt         j                  |j                  | j                  «       «      sy| j                  «       Š‰r,|j                  syt        ˆfd„|j                  D «       «      syy)a¦  
        Check if a policy scope matches the given context.

        A scope matches if ALL of its fields match:
        - teams matches context.team_alias
        - keys matches context.key_alias
        - models matches context.model

        Args:
            scope: The policy scope to check
            context: The request context

        Returns:
            True if scope matches context, False otherwise
        Fc              3   óJ   •K  — | ]  }t         j                  |‰«      –— Œ y ­w©N)r
   r   )Ú.0ÚtagÚ
scope_tagss     €r   ú	<genexpr>z.PolicyMatcher.scope_matches.<locals>.<genexpr>Y   s   øè ø€ Ò^È#”}×4Ñ4°S¸*×EÑ^ùs   ƒ #T)r
   r   Ú
team_aliasÚ	get_teamsÚ	key_aliasÚget_keysÚmodelÚ
get_modelsÚget_tagsÚtagsÚany)r   r   r   s     @r   Úscope_matcheszPolicyMatcher.scope_matches5   sŸ   ø€ ô$ ×,Ñ,¨W×-?Ñ-?ÀÇÁÓARÔSØô ×,Ñ,¨W×->Ñ->ÀÇÁÓ@PÔQØô ×,Ñ,¨W¯]©]¸E×<LÑ<LÓ<NÔOØð "ŸN™NÓ,ˆ
ÙØ—<’<ØäÓ^ÐQX×Q]ÑQ]Ô^Ô^Øàr   c                 óŒ   — ddl m}  |«       }|j                  «       st        j                  d«       g S |j                  | «      S )zë
        Get list of policy names that match the given context via attachments.

        Args:
            context: The request context to match against

        Returns:
            List of policy names that match the context
        r   )Úget_attachment_registryz8AttachmentRegistry not initialized, returning empty list)Ú/litellm.proxy.policy_engine.attachment_registryr*   Úis_initializedr   ÚdebugÚget_attached_policies)r   r*   Úregistrys      r   Úget_matching_policiesz#PolicyMatcher.get_matching_policies^   sB   € õ	
ñ 2Ó3ˆØ×&Ñ&Ô(Ü ×&Ñ&Ð'aÔbØˆIà×-Ñ-¨gÓ6Ð6r   c                 ó.   — t         j                  | ¬«      S )zô
        Get list of policy names that match the given context from the global registry.

        Args:
            context: The request context to match against

        Returns:
            List of policy names that match the context
        ©r   )r
   r0   r2   s    r   Ú#get_matching_policies_from_registryz1PolicyMatcher.get_matching_policies_from_registryv   s   € ô ×2Ñ2¸7Ð2ÓCÐCr   Úpolicy_namesÚpoliciesc                 ó"  — ddl m} ddlm} |€) |«       }|j	                  «       sg S |j                  «       }g }| D ]P  }|j                  |«      }|€Œ|j                  �|j                  |j                  |«      sŒ@|j                  |«       ŒR |S )a;  
        Filter policies to only those whose conditions match the context.

        A policy's condition matches if:
        - The policy has no condition (condition is None), OR
        - The policy's condition evaluates to True for the given context

        Args:
            policy_names: List of policy names to filter
            context: The request context to evaluate conditions against
            policies: Dictionary of all policies (if None, uses global registry)

        Returns:
            List of policy names whose conditions match the context
        r   )ÚConditionEvaluator)Úget_policy_registry)
Ú/litellm.proxy.policy_engine.condition_evaluatorr7   Ú+litellm.proxy.policy_engine.policy_registryr8   r,   Úget_all_policiesÚgetÚ	conditionÚevaluateÚappend)	r4   r   r5   r7   r8   r/   Úmatching_policiesÚpolicy_nameÚpolicys	            r   Ú%get_policies_with_matching_conditionsz3PolicyMatcher.get_policies_with_matching_conditions…   sœ   € õ* 	WÝSàÐÙ1Ó3ˆHØ×*Ñ*Ô,Ø�	Ø×0Ñ0Ó2ˆHà#%ÐØ'ò 	6ˆKØ—\‘\ +Ó.ˆFØˆ~Øà×ÑÐ'Ð+=×+FÑ+FÀv×GWÑGWÐY`Õ+aØ!×(Ñ(¨Õ5ð	6ð !Ð r   r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚstaticmethodÚstrÚlistÚboolr   r   r   r(   r0   r3   Údictr   rC   © r   r   r
   r
      s
  „ ñð ð˜s T™zð °T¸#±Yð À4ò ó ðð0 ð&˜[ð &Ð3Eð &È$ò &ó ð&ðP ð7Ø#ð7à	ˆc‰ò7ó ð7ð. ðDØ#ðDà	ˆc‰òDó ðDð ð .2ñ&!Ø˜3‘ið&!à#ð&!ð �s˜F�{Ñ# dÑ*ð&!ð 
ˆc‰ò	&!ó ñ&!r   r
   N)rG   Útypingr   Úlitellm._loggingr   Úlitellm.proxy.auth.route_checksr   Ú!litellm.types.proxy.policy_enginer   r   r   r
   rM   r   r   ú<module>rR      s'   ðñõ å 1Ý 7ß UÑ U÷[!ò [!r   