
    $                         d 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 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y)z7IAM-related helpers for working with the Cloud KMS API.    )absolute_import)division)unicode_literals)base)iam_utilc                     t        j                         }t        j                         }|j                  t        j
                  |       }|j                  j                  |      S )zFetch the IAM Policy attached to the EkmConfig.

  Args:
      ekm_config_name: A string name of the EkmConfig.

  Returns:
      An apitools wrapper for the IAM Policy.
  options_requestedPolicyVersionresource)r   GetClientInstanceGetMessagesModule5CloudkmsProjectsLocationsEkmConfigGetIamPolicyRequestr   !MAX_LIBRARY_IAM_SUPPORTED_VERSIONprojects_locations_ekmConfigGetIamPolicy)ekm_config_nameclientmessagesreqs       *lib/googlecloudsdk/api_lib/cloudkms/iam.pyGetEkmConfigIamPolicyr      s[     !!#&##%(FF%-%O%O 	G 	 # 
	,	,	9	9#	>>    c                    t        j                         }t        j                         }t        j                  |_        |sd}n	d|vr|dz  }|j                  | |j                  ||            }|j                  j                  |      S )a  Set the IAM Policy attached to the named EkmConfig to the given policy.

  If 'policy' has no etag specified, this will BLINDLY OVERWRITE the IAM policy!

  Args:
      ekm_config_name:  A string name of the EkmConfig.
      policy: An apitools wrapper for the IAM Policy.
      update_mask: str, FieldMask represented as comma-separated field names.

  Returns:
      The IAM Policy.
  version,versionpolicy
updateMaskr   setIamPolicyRequest)
r   r   r   r   r   r   5CloudkmsProjectsLocationsEkmConfigSetIamPolicyRequestSetIamPolicyRequestr   SetIamPolicy)r   r   update_maskr   r   r   s         r   SetEkmConfigIamPolicyr%   ,   s     !!#&##%(==&.	K#:KFF"66K 7 1 	G 	2#
 
	,	,	9	9#	>>r   c                     t        j                         }t        |       }t        j                  |j
                  |||       t        | |d      S z@Does an atomic Read-Modify-Write, adding the member to the role.bindings,etagr$   )r   r   r   r   AddBindingToIamPolicyBindingr%   )r   memberroler   r   s        r   AddPolicyBindingToEkmConfigr.   J   sH    ##%( 1&
  !1!1664H	v?
< <r   c                 b    t        |       }t        j                  |||       t        | |d      S zDDoes an atomic Read-Modify-Write, removing the member from the role.r(   r)   )r   r   RemoveBindingFromIamPolicyr%   )r   r,   r-   r   s       r    RemovePolicyBindingFromEkmConfigr2   T   s2     1&
%%ffd;	v?
< <r   c                     t        j                         }t        j                         }|j                  t        j
                  | j                               }|j                  j                  |      S )zFetch the IAM Policy attached to the named KeyRing.

  Args:
      key_ring_ref: A resources.Resource naming the KeyRing.

  Returns:
      An apitools wrapper for the IAM Policy.
  r	   )	r   r   r   4CloudkmsProjectsLocationsKeyRingsGetIamPolicyRequestr   r   RelativeNameprojects_locations_keyRingsr   )key_ring_refr   r   r   s       r   GetKeyRingIamPolicyr8   \   sd     !!#&##%(EE%-%O%O((* 	F 	,# 
	+	+	8	8	==r   c                 2   t        j                         }t        j                         }t        j                  |_        |sd}n	d|vr|dz  }|j                  | j                         |j                  ||            }|j                  j                  |      S )a  Set the IAM Policy attached to the named KeyRing to the given policy.

  If 'policy' has no etag specified, this will BLINDLY OVERWRITE the IAM policy!

  Args:
      key_ring_ref: A resources.Resource naming the KeyRing.
      policy: An apitools wrapper for the IAM Policy.
      update_mask: str, FieldMask represented as comma-separated field names.

  Returns:
      The IAM Policy.
  r   r   r   r   )r   r   r   r   r   r   4CloudkmsProjectsLocationsKeyRingsSetIamPolicyRequestr5   r"   r6   r#   )r7   r   r$   r   r   r   s         r   SetKeyRingIamPolicyr;   o   s     !!#&##%(==&.	K#:KEE((*"66K 7 1 	F 	2#
 
	+	+	8	8	==r   c                     t        j                         }t        |       }t        j                  |j
                  |||       t        | |d      S r'   )r   r   r8   r   r*   r+   r;   )r7   r,   r-   r   r   s        r   AddPolicyBindingToKeyRingr=      sC    ##%(|,&
  !1!1664H	\6	OOr   c                 b    t        |       }t        j                  |||       t        | |d      S r0   )r8   r   r1   r;   )r7   r,   r-   r   s       r   RemovePolicyBindingFromKeyRingr?      s-    |,&
%%ffd;	\6	OOr   c                     t        j                         }t        j                         }|j                  t        j
                  | j                               }|j                  j                  |      S )zFetch the IAM Policy attached to the named CryptoKey.

  Args:
      crypto_key_ref: A resources.Resource naming the CryptoKey.

  Returns:
      An apitools wrapper for the IAM Policy.
  r	   )	r   r   r   >CloudkmsProjectsLocationsKeyRingsCryptoKeysGetIamPolicyRequestr   r   r5   &projects_locations_keyRings_cryptoKeysr   )crypto_key_refr   r   r   s       r   GetCryptoKeyIamPolicyrD      sd     !!#&##%(OO%-%O%O**, 	P 	.# 
	6	6	C	CC	HHr   c                 2   t        j                         }t        j                         }t        j                  |_        |sd}n	d|vr|dz  }|j                  | j                         |j                  ||            }|j                  j                  |      S )a  Set the IAM Policy attached to the named CryptoKey to the given policy.

  If 'policy' has no etag specified, this will BLINDLY OVERWRITE the IAM policy!

  Args:
      crypto_key_ref: A resources.Resource naming the CryptoKey.
      policy: An apitools wrapper for the IAM Policy.
      update_mask: str, FieldMask represented as comma-separated field names.

  Returns:
      The IAM Policy.
  r   r   r   r   )r   r   r   r   r   r   >CloudkmsProjectsLocationsKeyRingsCryptoKeysSetIamPolicyRequestr5   r"   rB   r#   )rC   r   r$   r   r   r   s         r   SetCryptoKeyIamPolicyrG      s     !!#&##%(==&.	K#:KOO**,"66K 7 1 	P 	2#
 
	6	6	C	CC	HHr   c                     t        j                         }t        j                         }|j                  | j	                         |j                  |            }|j                  j                  |      S )z>Return permissions that the caller has on the named CryptoKey.)permissions)r   testIamPermissionsRequest)r   r   r   DCloudkmsProjectsLocationsKeyRingsCryptoKeysTestIamPermissionsRequestr5   TestIamPermissionsRequestrB   TestIamPermissions)rC   rI   r   r   r   s        r   TestCryptoKeyIamPermissionsrN      sp    !!#&##%(UU**, ( B B! !C !# 	V 	$# 
	6	6	I	I#	NNr   c                      t        | ||fg      S )aH  Add an IAM policy binding on the CryptoKey.

  Does an atomic Read-Modify-Write, adding the member to the role.

  Args:
    crypto_key_ref: A resources.Resource naming the CryptoKey.
    member: Principal to add to the policy binding.
    role: List of roles to add to the policy binding.

  Returns:
    The new IAM Policy.
  )AddPolicyBindingsToCryptoKey)rC   r,   r-   s      r   AddPolicyBindingToCryptoKeyrQ      s     
&n~6F	GGr   c                     t        j                         }t        |       }t        j                  |_        d}|D ]*  \  }}t        j                  |j                  |||      s)d}, |rt        | |d      S |S )a`  Add IAM policy bindings on the CryptoKey.

  Does an atomic Read-Modify-Write, adding the members to the roles. Only calls
  SetIamPolicy if the policy would be different.

  Args:
    crypto_key_ref: A resources.Resource naming the CryptoKey.
    member_roles: List of 2-tuples in the form [(member, role), ...].

  Returns:
    The new IAM Policy.
  FTr(   r)   )	r   r   rD   r   r   r   r*   r+   rG   )rC   member_rolesr   r   policy_was_updatedr,   r-   s          r   rP   rP      s}     ##%( 0&==&."lfd%%h&6&6M #  O= = 
-r   c                     t        |       }t        j                  |_        t        j                  |||       t        | |d      S r0   )rD   r   r   r   r1   rG   )rC   r,   r-   r   s       r    RemovePolicyBindingFromCryptoKeyrV     s?     0&==&.
%%ffd;	f/
; ;r   N)__doc__
__future__r   r   r   googlecloudsdk.api_lib.cloudkmsr   googlecloudsdk.command_lib.iamr   r   r%   r.   r2   r8   r;   r=   r?   rD   rG   rN   rQ   rP   rV    r   r   <module>r\      se    > &  ' 0 3?&?<<<>&><PPI&I<	OH <;r   