o
    ưi                     @   sV   d 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 G dd dZdS )	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.
    )DictListOptional)verbose_proxy_logger)RouteChecks)PolicyPolicyMatchContextPolicyScopec                   @   s   e Zd ZdZedee dee defddZ	ede
dedefd	d
Zededee fddZededee fddZe	ddee dedeeeef  dee fddZdS )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.
    valuepatternsreturnc                 C   s2   | du rd|v S |D ]}t j| |dr dS q
dS )aw  
        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
        N*)ZroutepatternTF)r   Z_route_matches_wildcard_pattern)r   r   r    r   a/home/app/Keep/.python/lib/python3.10/site-packages/litellm/proxy/policy_engine/policy_matcher.pymatches_pattern   s   zPolicyMatcher.matches_patternscopecontextc                    sx   t |j|  sdS t |j|  sdS t |j|  s!dS |    r:|j	s,dS t
 fdd|j	D s:dS dS )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   s    | ]	}t | V  qd S N)r
   r   ).0tagZ
scope_tagsr   r   	<genexpr>[   s
    

z.PolicyMatcher.scope_matches.<locals>.<genexpr>T)r
   r   Z
team_aliasZ	get_teamsZ	key_aliasZget_keysmodelZ
get_modelsget_tagstagsany)r   r   r   r   r   scope_matches7   s   zPolicyMatcher.scope_matchesc                 C   s2   ddl m} | }| std g S || 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)Z/litellm.proxy.policy_engine.attachment_registryr   is_initializedr   debugZget_attached_policies)r   r   registryr   r   r   get_matching_policiesc   s   
z#PolicyMatcher.get_matching_policiesc                 C   s   t j| d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
   r#   r$   r   r   r   #get_matching_policies_from_registry}   s   z1PolicyMatcher.get_matching_policies_from_registryNpolicy_namespoliciesc           	      C   s   ddl m} ddlm} |du r| }| sg S | }g }| D ]}||}|du r-q!|jdu s9||j|r>|	| q!|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_registryN)
Z/litellm.proxy.policy_engine.condition_evaluatorr(   Z+litellm.proxy.policy_engine.policy_registryr)   r    Zget_all_policiesget	conditionevaluateappend)	r&   r   r'   r(   r)   r"   Zmatching_policiesZpolicy_namepolicyr   r   r   %get_policies_with_matching_conditions   s$   

z3PolicyMatcher.get_policies_with_matching_conditionsr   )__name__
__module____qualname____doc__staticmethodr   strr   boolr   r	   r   r   r#   r%   r   r   r/   r   r   r   r   r
      s:    
 +r
   N)r3   typingr   r   r   Zlitellm._loggingr   Zlitellm.proxy.auth.route_checksr   Z!litellm.types.proxy.policy_enginer   r   r	   r
   r   r   r   r   <module>   s    	