o
    `j6                     @  s  d Z ddlmZ ddl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mZmZ er6ddlmZ eeZG d	d
 d
ZG dd deZG dd deZG dd deZG dd deZG dd dZG dd deZG dd dZG dd deZdd ZdS )zACL management mixin for KafkaAdminClient.

Also defines ACL data types: ResourceType, ACLOperation, ACLPermissionType,
ACLResourcePatternType, ACLFilter, ACL, ResourcePatternFilter, ResourcePattern.
    )annotationsN)IntEnum)TYPE_CHECKING)IllegalArgumentError)CreateAclsRequestDeleteAclsRequestDescribeAclsRequest)KafkaConnectionManagerc                   @  s~   e Zd ZU dZded< ded< dd Zdd	 Zed
d Zedd Z	edd Z
dd Zedd Zedd Zdd ZdS )ACLAdminMixinz<Mixin providing ACL management methods for KafkaAdminClient.r	   _managerdictconfigc                 C  s2   | dd d urttdd t|d |d< |S )Nauthorized_operationsc                 S  s   | j S N)name)acl r   O/home/djax/ivt_ai_plugin/venv/lib/python3.10/site-packages/kafka/admin/_acls.py<lambda>   s    z7ACLAdminMixin._process_acl_operations.<locals>.<lambda>)getlistmapvalid_acl_operations)selfobjr   r   r   _process_acl_operations   s   z%ACLAdminMixin._process_acl_operationsc              
   C  s^   |j jtjkr	dnd}t||j j|j j|j j|j|j|j	|j
d}| j| jj|}| |S )a  Describe a set of ACLs

        Used to return a set of ACLs matching the supplied ACLFilter.
        The cluster must be configured with an authorizer for this to work, or
        you will get a SecurityDisabledError

        Arguments:
            acl_filter: an ACLFilter object

        Returns:
            tuple of a list of matching ACL objects and a KafkaError (NoError if successful)
           r   )min_versionresource_type_filterresource_name_filterpattern_type_filterprincipal_filterhost_filter	operationpermission_type)resource_patternresource_typeResourceTypeUSERr   resource_namepattern_type	principalhostr#   r$   r   runsend'_convert_describe_acls_response_to_acls)r   
acl_filterr   requestresponser   r   r   describe_acls"   s   

zACLAdminMixin.describe_aclsc                 C  s   t | j}|t jur|| jg }| jD ](}|jD ]"}|t|j	|j
t|jt|jtt|j|jt|jdd qq|t jfS )aR  Convert a DescribeAclsResponse into a list of ACL objects and a KafkaError.

        Arguments:
            describe_response: The response object from the DescribeAclsRequest.

        Returns:
            A tuple of (list_of_acl_objects, error) where error is an instance
                 of KafkaError (NoError if successful).
        r&   r)   r*   r+   r,   r#   r$   r%   )Errorsfor_code
error_codeNoErrorerror_message	resourcesaclsappendACLr+   r,   ACLOperationr#   ACLPermissionTyper$   ResourcePatternr'   r&   r)   ACLResourcePatternTyper*   )describe_response
error_typeacl_listresourcer   r   r   r   r/   =   s*   




z5ACLAdminMixin._convert_describe_acls_response_to_aclsc              	   C  0   t j}|| jj| jj| jj| j| j| j| j	dS )z8Convert an ACL object into the CreateAclsRequest format.)r&   r)   resource_pattern_typer+   r,   r#   r$   )
r   AclCreationr%   r&   r)   r*   r+   r,   r#   r$   )r   
_AclCreater   r   r   %_convert_create_acls_resource_requestZ      z3ACLAdminMixin._convert_create_acls_resource_requestc                 C  sZ   g g d}t |jD ] \}}| | }|jdkr|d | q
|d t|j q
|S )zGParse a CreateAclsResponse, returning a dict of successes and failures.)	succeededfailedr   rM   rN   )	enumerateresultsr8   r=   r6   r7   )r<   create_responserP   iresultr   r   r   r   %_convert_create_acls_response_to_aclsh   s   

z3ACLAdminMixin._convert_create_acls_response_to_aclsc                   r   |D ]}t |tstdq fdd|D }tdd |D r"dnd}t||d} j jj|} ||S )	a.  Create a list of ACLs

        This endpoint only accepts a list of concrete ACL objects, no ACLFilters.
        Throws TopicAlreadyExistsError if topic is already present.

        Arguments:
            acls: a list of ACL objects

        Returns:
            dict of successes and failures
        zacls must contain ACL objectsc                      g | ]}  |qS r   )rK   .0r   r   r   r   
<listcomp>       z-ACLAdminMixin.create_acls.<locals>.<listcomp>c                 s      | ]	}|j tjkV  qd S r   )r&   r'   r(   )rX   creationr   r   r   	<genexpr>       z,ACLAdminMixin.create_acls.<locals>.<genexpr>r   r   )	creationsr   )	
isinstancer>   r   anyr   r   r-   r.   rT   )r   r<   r   r`   r   r1   r2   r   rY   r   create_aclst      
zACLAdminMixin.create_aclsc              	   C  rG   )z>Convert an ACLFilter object into the DeleteAclsRequest format.)r   r   r    r!   r"   r#   r$   )
r   DeleteAclsFilterr%   r&   r)   r*   r+   r,   r#   r$   )r   _AclsFilterr   r   r   %_convert_delete_acls_resource_request   rL   z3ACLAdminMixin._convert_delete_acls_resource_requestc           
      C  s   g }t |jD ]D\}}| | }t|j}g }|jD ](}t|j}	|t|j|j	t
|jt|jtt|j|jt|jdd q||||f q|S )zUParse a DeleteAclsResponse, returning a list of (filter, matched ACLs, error) tuples.r4   r5   )rO   filter_resultsr6   r7   r8   matching_aclsr=   r>   r+   r,   r?   r#   r@   r$   rA   r'   r&   r)   rB   r*   )
acl_filtersdelete_responserP   rR   rS   r0   rD   ri   r   errorr   r   r   ._convert_delete_acls_response_to_matching_acls   s,   

z<ACLAdminMixin._convert_delete_acls_response_to_matching_aclsc                   rU   )	a]  Delete a set of ACLs

        Deletes all ACLs matching the list of input ACLFilter

        Arguments:
            acl_filters: a list of ACLFilter

        Returns:
            a list of 3-tuples corresponding to the list of input filters.
                 The tuples hold (the input ACLFilter, list of affected ACLs, KafkaError instance)
        z/acl_filters must contain ACLFilter type objectsc                   rV   r   )rg   rW   rY   r   r   rZ      r[   z-ACLAdminMixin.delete_acls.<locals>.<listcomp>c                 s  r\   r   )r   r'   r(   )rX   _filterr   r   r   r^      r_   z,ACLAdminMixin.delete_acls.<locals>.<genexpr>r   r   )filtersr   )	ra   	ACLFilterr   rb   r   r   r-   r.   rm   )r   rj   r   ro   r   r1   r2   r   rY   r   delete_acls   rd   zACLAdminMixin.delete_aclsN)__name__
__module____qualname____doc____annotations__r   r3   staticmethodr/   rK   rT   rc   rg   rm   rq   r   r   r   r   r
      s$   
 




r
   c                   @  s0   e Zd ZdZdZdZdZdZdZdZ	dZ
d	Zd
S )r'   zaType of kafka resource to set ACL for.

    The ANY value is only valid in a filter context.
    r         r               N)rr   rs   rt   ru   UNKNOWNANYTOPICGROUPCLUSTERTRANSACTIONAL_IDDELEGATION_TOKENr(   r   r   r   r   r'      s    r'   c                   @  sL   e Zd ZdZdZdZdZdZdZdZ	dZ
d	Zd
ZdZdZdZdZdZdZdS )r?   zMType of operation.

    The ANY value is only valid in a filter context.
    r   rx   ry   r   rz   r{   r|   r}      	   
               N)rr   rs   rt   ru   r~   r   ALLREADWRITECREATEDELETEALTERDESCRIBECLUSTER_ACTIONDESCRIBE_CONFIGSALTER_CONFIGSIDEMPOTENT_WRITECREATE_TOKENSDESCRIBE_TOKENSr   r   r   r   r?      s"    r?   c                   @  s    e Zd ZdZdZdZdZdZdS )r@   z]An enumerated type of permissions.

    The ANY value is only valid in a filter context.
    r   rx   ry   r   N)rr   rs   rt   ru   r~   r   DENYALLOWr   r   r   r   r@      s    r@   c                   @  s$   e Zd ZdZdZdZdZdZdZdS )rB   zAn enumerated type of resource patterns.

    More details on the pattern types and how they work
    can be found in KIP-290 (Support for prefixed ACLs).
    r   rx   ry   r   rz   N)	rr   rs   rt   ru   r~   r   MATCHLITERALPREFIXEDr   r   r   r   rB      s    rB   c                   @  s4   e Zd Zdd Zdd Zdd Zdd Zd	d
 ZdS )ResourcePatternFilterc                 C  s   || _ || _|| _|   d S r   )r&   r)   r*   validater   r&   r)   r*   r   r   r   __init__	  s   zResourcePatternFilter.__init__c                 C  s,   t | jts
tdt | jtstdd S )Nz+resource_type must be a ResourceType objectz5pattern_type must be an ACLResourcePatternType object)ra   r&   r'   r   r*   rB   rY   r   r   r   r     s
   zResourcePatternFilter.validatec                 C  s   d | jj| j| jjS )Nz.<ResourcePattern type={}, name={}, pattern={}>)formatr&   r   r)   r*   rY   r   r   r   __repr__  s   zResourcePatternFilter.__repr__c                 C  s&   t | j|jk| j|jk| j|jkfS r   )allr&   r)   r*   r   otherr   r   r   __eq__  s
   


zResourcePatternFilter.__eq__c                 C  s   t | j| j| jfS r   )hashr&   r)   r*   rY   r   r   r   __hash__   s   zResourcePatternFilter.__hash__N)rr   rs   rt   r   r   r   r   r   r   r   r   r   r     s    r   c                      s.   e Zd ZdZejf fdd	Zdd Z  ZS )rA   z'A resource pattern to apply the ACL to.c                   s   t  ||| |   d S r   superr   r   r   	__class__r   r   r   &  s   zResourcePattern.__init__c                 C  s<   | j tjkr
td| jtjtjfv rtd| jjd S )Nzresource_type cannot be ANYz7pattern_type cannot be {} on a concrete ResourcePattern)	r&   r'   r   r   r*   rB   r   r   r   rY   r   r   r   r   *  s   zResourcePattern.validate)	rr   rs   rt   ru   rB   r   r   r   __classcell__r   r   r   r   rA   $  s    rA   c                   @  s8   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d ZdS )rp   z=Represents a filter to use with describing and deleting ACLs.c                 C  s*   || _ || _|| _|| _|| _|   d S r   )r+   r,   r#   r$   r%   r   r   r+   r,   r#   r$   r%   r   r   r   r   4  s   zACLFilter.__init__c                 C  s@   t | jts
tdt | jtstdt | jtstdd S )Nz;operation must be an ACLOperation object, and cannot be ANYzFpermission_type must be an ACLPermissionType object, and cannot be ANYz7resource_pattern must be a ResourcePatternFilter object)ra   r#   r?   r   r$   r@   r%   r   rY   r   r   r   r   <     zACLFilter.validatec                 C  s"   dj | j| j| jj| jj| jdS )Nza<ACL principal={principal}, resource={resource}, operation={operation}, type={type}, host={host}>)r+   r,   r#   typerF   )r   r+   r,   r#   r   r$   r%   rY   r   r   r   r   D  s   
zACLFilter.__repr__c                 C  s:   t | j|jk| j|jk| j|jk| j|jk| j|jkfS r   )r   r+   r,   r#   r$   r%   r   r   r   r   r   I  s
   
zACLFilter.__eq__c                 C  s   t | j| j| j| j| jfS r   )r   r+   r,   r#   r$   r%   rY   r   r   r   r   O  s   zACLFilter.__hash__N)	rr   rs   rt   ru   r   r   r   r   r   r   r   r   r   rp   2  s    rp   c                      s(   e Zd ZdZ fddZdd Z  ZS )r>   z9Represents a concrete ACL for a specific ResourcePattern.c                   s    t  ||||| |   d S r   r   r   r   r   r   r   U  s   zACL.__init__c                 C  s@   | j tjkr
td| jtjkrtdt| jtstdd S )Nzoperation cannot be ANYzpermission_type cannot be ANYz1resource_pattern must be a ResourcePattern object)	r#   r?   r   r   r$   r@   ra   r%   rA   rY   r   r   r   r   Y  r   zACL.validate)rr   rs   rt   ru   r   r   r   r   r   r   r   r>   S  s    r>   c                 C  s   t dd | D S )Nc                 S  s   g | ]
}|d vrt |qS ))r   rx   ry   )r?   )rX   vr   r   r   rZ   c  s    z(valid_acl_operations.<locals>.<listcomp>)set)int_valsr   r   r   r   b  s   r   )ru   
__future__r   loggingenumr   typingr   kafka.errorserrorsr6   r   kafka.protocol.adminr   r   r   kafka.net.managerr	   	getLoggerrr   logr
   r'   r?   r@   rB   r   rA   rp   r>   r   r   r   r   r   <module>   s,    
 5!