
    $                         S r SSKJr  SSKJr  SSKJr  SSKJr  SSKJr  S r	S r
S	 rS
 rS rS rS rS rS rS rS rS rS rS rg)z7IAM-related helpers for working with the Cloud KMS API.    )absolute_import)division)unicode_literals)base)iam_utilc                     [         R                  " 5       n[         R                  " 5       nUR                  [        R
                  U S9nUR                  R                  U5      $ )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                    [         R                  " 5       n[         R                  " 5       n[        R                  Ul        U(       d  SnOSU;  a  US-  nUR                  U UR                  XS9S9nUR                  R                  U5      $ )as  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 7 1 	G 	2#
 
	,	,	9	9#	>>r   c                     [         R                  " 5       n[        U 5      n[        R                  " UR
                  XAU5        [        XSS9$ 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   sD    ##%( 1&
  !1!164H	?
< <r   c                 Z    [        U 5      n[        R                  " X1U5        [        XSS9$ 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   s.     1&
%%fd;	?
< <r   c                     [         R                  " 5       n[         R                  " 5       nUR                  [        R
                  U R                  5       S9nUR                  R                  U5      $ )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                 6   [         R                  " 5       n[         R                  " 5       n[        R                  Ul        U(       d  SnOSU;  a  US-  nUR                  U R                  5       UR                  XS9S9nUR                  R                  U5      $ )av  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 7 1 	F 	2#
 
	+	+	8	8	==r   c                     [         R                  " 5       n[        U 5      n[        R                  " UR
                  XAU5        [        XSS9$ r'   )r   r   r8   r   r*   r+   r;   )r7   r,   r-   r   r   s        r   AddPolicyBindingToKeyRingr=      s?    ##%(|,&
  !1!164H	\	OOr   c                 Z    [        U 5      n[        R                  " X1U5        [        XSS9$ r0   )r8   r   r1   r;   )r7   r,   r-   r   s       r   RemovePolicyBindingFromKeyRingr?      s)    |,&
%%fd;	\	OOr   c                     [         R                  " 5       n[         R                  " 5       nUR                  [        R
                  U R                  5       S9nUR                  R                  U5      $ )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                 6   [         R                  " 5       n[         R                  " 5       n[        R                  Ul        U(       d  SnOSU;  a  US-  nUR                  U R                  5       UR                  XS9S9nUR                  R                  U5      $ )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 7 1 	P 	2#
 
	6	6	C	CC	HHr   c                     [         R                  " 5       n[         R                  " 5       nUR                  U R	                  5       UR                  US9S9nUR                  R                  U5      $ )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                     [        XU4/5      $ )a8  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                    [         R                  " 5       n[        U 5      n[        R                  Ul        SnU H0  u  pV[        R                  " UR                  X5U5      (       d  M.  SnM2     U(       a
  [        XSS9$ U$ )aP  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      sz     ##%( 0&==&."lf%%h&6&6MM #  O= = 
-r   c                     [        U 5      n[        R                  Ul        [        R                  " X1U5        [        XSS9$ r0   )rD   r   r   r   r1   rG   )rC   r,   r-   r   s       r    RemovePolicyBindingFromCryptoKeyrV     s;     0&==&.
%%fd;	/
; ;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   