U
    h3                     @   s`   d Z ddlZddlZddlZddlZdZdZdZdZdZ	G dd	 d	e
ZG d
d dejjZdS )a6  Non-API-specific IAM policy definitions

For allowed roles / permissions, see:
https://cloud.google.com/iam/docs/understanding-roles

Example usage:

.. code-block:: python

   # ``get_iam_policy`` returns a :class:'~google.api_core.iam.Policy`.
   policy = resource.get_iam_policy(requested_policy_version=3)

   phred = "user:phred@example.com"
   admin_group = "group:admins@groups.example.com"
   account = "serviceAccount:account-1234@accounts.example.com"

   policy.version = 3
   policy.bindings = [
       {
           "role": "roles/owner",
           "members": {phred, admin_group, account}
       },
       {
           "role": "roles/editor",
           "members": {"allAuthenticatedUsers"}
       },
       {
           "role": "roles/viewer",
           "members": {"allUsers"}
           "condition": {
               "title": "request_time",
               "description": "Requests made before 2021-01-01T00:00:00Z",
               "expression": "request.time < timestamp("2021-01-01T00:00:00Z")"
           }
       }
   ]

   resource.set_iam_policy(policy)
    Nzroles/ownerzroles/editorzroles/viewerz_Assigning to '{}' is deprecated. Use the `policy.bindings` property to modify bindings instead.zWDict access is not supported on policies with version > 1 or with conditional bindings.c                   @   s   e Zd ZdZdS )InvalidOperationExceptionz1Raised when trying to use Policy class as a dict.N)__name__
__module____qualname____doc__ r   r   7/tmp/pip-unpacked-wheel-eraazoov/google/api_core/iam.pyr   M   s   r   c                   @   s(  e Zd ZdZefZefZefZ	d/ddZ
dd Zdd Zd	d
 Zdd Zdd Zdd Zdd Zedd Zejdd Zedd Zejdd Zedd Zejdd Zedd Zejdd Zedd  Zed!d" Zed#d$ Zed%d& Zed'd( Zed)d* Zed+d, Z d-d. Z!dS )0Policya1  IAM Policy

    Args:
        etag (Optional[str]): ETag used to identify a unique of the policy
        version (Optional[int]): The syntax schema version of the policy.

    Note:
        Using conditions in bindings requires the policy's version to be set
        to `3` or greater, depending on the versions that are currently supported.

        Accessing the policy using dict operations will raise InvalidOperationException
        when the policy's version is set to 3.

        Use the policy.bindings getter/setter to retrieve and modify the policy's bindings.

    See:
        IAM Policy https://cloud.google.com/iam/reference/rest/v1/Policy
        Policy versions https://cloud.google.com/iam/docs/policies#versions
        Conditions overview https://cloud.google.com/iam/docs/conditions-overview.
    Nc                 C   s   || _ || _g | _d S N)etagversion	_bindings)selfr   r   r   r   r   __init__r   s    zPolicy.__init__c                 C   s   |    dd | jD S )Nc                 s   s   | ]}|d  r|d V  qdS )membersroleNr   ).0bindingr   r   r   	<genexpr>z   s      z"Policy.__iter__.<locals>.<genexpr>)__check_version__r   r   r   r   r   __iter__w   s    zPolicy.__iter__c                 C   s   |    tt|  S r
   )r   lenlistr   r   r   r   r   __len__|   s    zPolicy.__len__c                 C   sL   |    | jD ]}|d |kr|d   S q|t d}| j| |d S Nr   r   r   r   )r   r   setappend)r   keybnew_bindingr   r   r   __getitem__   s    
zPolicy.__getitem__c                 C   sL   |    t|}| jD ]}|d |kr||d<  d S q| j||d d S r   )r   r   r   r   )r   r   valuer   r   r   r   __setitem__   s    
zPolicy.__setitem__c                 C   s>   |    | jD ]"}|d |kr| j|  d S qt|d S )Nr   )r   r   removeKeyError)r   r   r    r   r   r   __delitem__   s    
zPolicy.__delitem__c                 C   s,   | j dk	o| j dk}|s |  r(ttdS )z[Raise InvalidOperationException if version is greater than 1 or policy contains conditions.N   )r   _contains_conditionsr   _DICT_ACCESS_MSG)r   Zraise_versionr   r   r   r      s    zPolicy.__check_version__c                 C   s$   | j D ]}|dd k	r dS qdS )N	conditionTF)r   get)r   r    r   r   r   r)      s    
zPolicy._contains_conditionsc                 C   s   | j S )aE  The policy's list of bindings.

        A binding is specified by a dictionary with keys:

        * role (str): Role that is assigned to `members`.

        * members (:obj:`set` of str): Specifies the identities associated to this binding.

        * condition (:obj:`dict` of str:str): Specifies a condition under which this binding will apply.

          * title (str): Title for the condition.

          * description (:obj:str, optional): Description of the condition.

          * expression: A CEL expression.

        Type:
           :obj:`list` of :obj:`dict`

        See:
           Policy versions https://cloud.google.com/iam/docs/policies#versions
           Conditions overview https://cloud.google.com/iam/docs/conditions-overview.

        Example:

        .. code-block:: python

           USER = "user:phred@example.com"
           ADMIN_GROUP = "group:admins@groups.example.com"
           SERVICE_ACCOUNT = "serviceAccount:account-1234@accounts.example.com"
           CONDITION = {
               "title": "request_time",
               "description": "Requests made before 2021-01-01T00:00:00Z", # Optional
               "expression": "request.time < timestamp("2021-01-01T00:00:00Z")"
           }

           # Set policy's version to 3 before setting bindings containing conditions.
           policy.version = 3

           policy.bindings = [
               {
                   "role": "roles/viewer",
                   "members": {USER, ADMIN_GROUP, SERVICE_ACCOUNT},
                   "condition": CONDITION
               },
               ...
           ]
        r   r   r   r   r   bindings   s    2zPolicy.bindingsc                 C   s
   || _ d S r
   r-   )r   r.   r   r   r   r.      s    c                 C   s6   t  }| jD ] }| |dD ]}|| qqt|S )zLegacy access to owner role.

        Raise InvalidOperationException if version is greater than 1 or policy contains conditions.

        DEPRECATED:  use `policy.bindings` to access bindings instead.
        r   )r   _OWNER_ROLESr,   add	frozensetr   resultr   memberr   r   r   owners   s
    
zPolicy.ownersc                 C   s    t tdtt || t< dS )zUpdate owners.

        Raise InvalidOperationException if version is greater than 1 or policy contains conditions.

        DEPRECATED:  use `policy.bindings` to access bindings instead.
        r5   N)warningswarn_ASSIGNMENT_DEPRECATED_MSGformat
OWNER_ROLEDeprecationWarningr   r#   r   r   r   r5      s
    
 c                 C   s6   t  }| jD ] }| |dD ]}|| qqt|S )zLegacy access to editor role.

        Raise InvalidOperationException if version is greater than 1 or policy contains conditions.

        DEPRECATED:  use `policy.bindings` to access bindings instead.
        r   )r   _EDITOR_ROLESr,   r0   r1   r2   r   r   r   editors   s
    
zPolicy.editorsc                 C   s    t tdtt || t< dS )zUpdate editors.

        Raise InvalidOperationException if version is greater than 1 or policy contains conditions.

        DEPRECATED:  use `policy.bindings` to modify bindings instead.
        r>   N)r6   r7   r8   r9   EDITOR_ROLEr;   r<   r   r   r   r>     s
    
c                 C   s6   t  }| jD ] }| |dD ]}|| qqt|S )zLegacy access to viewer role.

        Raise InvalidOperationException if version is greater than 1 or policy contains conditions.

        DEPRECATED:  use `policy.bindings` to modify bindings instead.
        r   )r   _VIEWER_ROLESr,   r0   r1   r2   r   r   r   viewers  s
    
zPolicy.viewersc                 C   s    t tdtt || t< dS )zUpdate viewers.

        Raise InvalidOperationException if version is greater than 1 or policy contains conditions.

        DEPRECATED:  use `policy.bindings` to modify bindings instead.
        rA   N)r6   r7   r8   r9   VIEWER_ROLEr;   r<   r   r   r   rA   (  s
    
c                 C   s
   d| f S )zFactory method for a user member.

        Args:
            email (str): E-mail for this particular user.

        Returns:
            str: A member string corresponding to the given user.
        zuser:%sr   emailr   r   r   user6  s    
zPolicy.userc                 C   s
   d| f S )zFactory method for a service account member.

        Args:
            email (str): E-mail for this particular service account.

        Returns:
            str: A member string corresponding to the given service account.

        zserviceAccount:%sr   rC   r   r   r   service_accountB  s    zPolicy.service_accountc                 C   s
   d| f S )zFactory method for a group member.

        Args:
            email (str): An id or e-mail for this particular group.

        Returns:
            str: A member string corresponding to the given group.
        zgroup:%sr   rC   r   r   r   groupO  s    
zPolicy.groupc                 C   s
   d| f S )zFactory method for a domain member.

        Args:
            domain (str): The domain for this member.

        Returns:
            str: A member string corresponding to the given domain.
        z	domain:%sr   )domainr   r   r   rH   [  s    
zPolicy.domainc                   C   s   dS )zFactory method for a member representing all users.

        Returns:
            str: A member string representing all users.
        ZallUsersr   r   r   r   r   	all_usersg  s    zPolicy.all_usersc                   C   s   dS )zFactory method for a member representing all authenticated users.

        Returns:
            str: A member string representing all authenticated users.
        ZallAuthenticatedUsersr   r   r   r   r   authenticated_usersp  s    zPolicy.authenticated_usersc                 C   sP   | d}| d}| ||}| dg |_|jD ]}t| dd|d< q2|S )zFactory: create a policy from a JSON resource.

        Args:
            resource (dict): policy resource returned by ``getIamPolicy`` API.

        Returns:
            :class:`Policy`: the parsed policy
        r   r   r.   r   r   )r,   r.   r   )clsresourcer   r   policyr   r   r   r   from_api_repry  s    




zPolicy.from_api_reprc                 C   s   i }| j dk	r| j |d< | jdk	r,| j|d< | jrt| jdkrg }| jD ]D}|d}|rJ|d t|d}|d}|r||d< || qJ|rtd}t||d	|d
< |S )zRender a JSON policy resource.

        Returns:
            dict: a resource to be passed to the ``setIamPolicy`` API.
        Nr   r   r   r   r   r   r+   )r   r.   )	r   r   r   r   r,   sortedr   operator
itemgetter)r   rL   r.   r   r   r!   r+   r   r   r   r   to_api_repr  s&    







zPolicy.to_api_repr)NN)"r   r   r   r   r:   r/   r?   r=   rB   r@   r   r   r   r"   r$   r'   r   r)   propertyr.   setterr5   r>   rA   staticmethodrE   rF   rG   rH   rI   rJ   classmethodrN   rR   r   r   r   r   r	   S   sV   
	
3













r	   )r   collectionscollections.abcrP   r6   r:   r?   rB   r8   r*   	Exceptionr   abcMutableMappingr	   r   r   r   r   <module>   s   (