
    I+                     (   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SKJr  SSK	r	SS	K
Jr  SS
KJr  SSKJr  \" / SQ5      r\" / SQ5      rSr\" SS/5      r\" SSS/5      r\" SSS/5      rSrS rS rS rS rS rS rS rS r S r!S r"g) z"Helper module for the IAM command.    )absolute_import)print_function)division)unicode_literals)defaultdict)
namedtupleN)	protojson)CommandException)storage_v1_messages)
userzdeleted:userserviceAccountzdeleted:serviceAccountgroupzdeleted:groupdomain	principalprincipalSetprincipalHierarchy)projectOwnerprojectEditorprojectViewera  Assigning roles (e.g. objectCreator, legacyBucketOwner) for project convenience groups is not supported by gsutil, as it goes against the principle of least privilege. Consider creating and using more granular groups with which to assign permissions. See https://cloud.google.com/iam/docs/using-iam-securely for more information. Assigning a role to a project group can be achieved by setting the IAM policy directly (see gsutil help iam for specifics).allUsersallAuthenticatedUsersBindingsTupleis_grantbindingsBindingsDictTuple c                     U R                   U R                   Vs/ s H  n[        R                  " U5      PM     sn4$ s  snf )aE  Serializes the BindingsValueListEntry instances in a BindingsTuple.

This is necessary when passing instances of BindingsTuple through
Command.Apply, as apitools_messages classes are not by default pickleable.

Args:
  bindings_tuple: A BindingsTuple instance to be serialized.

Returns:
  A serialized BindingsTuple object.
)r   r   r	   encode_message)bindings_tuplets     )platform/gsutil/gslib/utils/iam_helper.pySerializeBindingsTupler"   N   sA     
!
!0>0G0G
H0G19##A&0G
H
J J
Hs    >c           
          U u  p[        UU Vs/ s H2  n[        R                  " [        R                  R
                  U5      PM4     snS9$ s  snf )Nr   r   )r   r	   decode_messageapitools_messagesPolicyBindingsValueListEntry)serialized_bindings_tupler   r   r    s       r!   DeserializeBindingsTupler*   ^   sZ    28	 ,4! ,4a %33077NN "+3!
 !s   9A
c                     [        [        5      nU  H*  nXR                     R                  UR                  5        M,     U$ )zReformats policy bindings metadata.

Args:
  bindings: A list of BindingsValueListEntry instances.

Returns:
  A {role: set(members)} dictionary.
)r   setroleupdatemembersr   tmp_bindingsbindings      r!   BindingsMessageToUpdateDictr3   h   s7     S!,g%%goo6 	    c                 h    [        [        5      nU  H  nXS      R                  US   5        M     U$ )a  Reformats policy bindings metadata.

Args:
  bindings: List of dictionaries representing BindingsValueListEntry
    instances. e.g.:
    {
      "role": "some_role",
      "members": ["allAuthenticatedUsers", ...]
    }

Returns:
  A {role: set(members)} dictionary.
r-   r/   )r   r,   r.   r0   s      r!   BindingsDictToUpdateDictr6   x   s8     S!,g!(();< 	r4   c                 n    [        X5      u  p#UR                  (       + =(       a    UR                  (       + $ )N)DiffBindingsr   )abgrantedremoveds       r!   IsEqualBindingsr=      s,    #A)7		6g&6&6"66r4   c           
         [        U 5      n[        U5      n[        / 5      n[        / 5      n[        R                  " U5       H)  u  pgXV   R                  UR	                  X6   5      5        M+     [        R                  " U5       H)  u  pgXF   R                  UR	                  X&   5      5        M+     [        R                  " U5       VV	s/ s H5  u  pU	(       d  M  [
        R                  R                  U[        U	5      S9PM7     nnn	[        R                  " U5       VV	s/ s H5  u  pU	(       d  M  [
        R                  R                  U[        U	5      S9PM7     nnn	[        SU5      [        SU5      4$ s  sn	nf s  sn	nf )aO  Computes the difference between two BindingsValueListEntry lists.

Args:
  old: The original list of BindingValuesListEntry instances
  new: The updated list of BindingValuesListEntry instances

Returns:
  A pair of BindingsTuple instances, one for roles granted between old and
    new, and one for roles removed between old and new.
r-   r/   TF)
r3   six	iteritemsr.   
differencer&   r'   r(   listr   )
oldnewtmp_oldtmp_newr;   r<   r-   r/   rms
             r!   r8   r8      sQ    (,'',''+''+'w/otM++GM:; 0w/otM++GM:; 0
 MM'**&1	
 O551d1g5N* 
  MM'**&1	
 O551d1g5N* 
  g
&eW(E	FF
s   E;+E;$F5+Fc                 ~   U(       aE  [         R                  " U5       H*  u  p4U(       d  [        S5      eX   R                  U5        M,     O8U  H2  nX   R	                  X   5        X   R	                  U[
           5        M4     [         R                  " U 5       VVs0 s H  u  p4U(       d  M  X4_M     snn$ s  snnf )a  Patches a diff list of BindingsValueListEntry to the base.

Will remove duplicate members for any given role on a grant operation.

Args:
  base (dict): A dictionary returned by BindingsMessageToUpdateDict or
    BindingsDictToUpdateDict representing a resource's current
    IAM policy.
  diff (dict): A dictionary returned by BindingsMessageToUpdateDict or
    BindingsDictToUpdateDict representing the IAM policy bindings to
    add/remove from `base`.
  is_grant (bool): True if `diff` should be added to `base`, False
    if it should be removed from `base`.

Returns:
  A {role: set(members)} dictionary created by applying `diff` to `base`.
z+Role must be specified for a grant request.)r@   rA   r
   r.   difference_updateDROP_ALL)basediffr   r-   r/   s        r!   PatchBindingsrO      s    & ==.LMM
j  /
 
j""4:.
j""4>2 
 .1]]4-@	L-@MDG-$--@	LL	Ls   B9/B9c                    UR                  S5      (       d  US-  nUR                  S5      n[         Vs0 s H  o3R                  5       U_M     nn[         Vs0 s H  o3R                  5       U_M     nn[
         Vs0 s H  o3R                  5       U_M     nnUS   R                  5       nUS   R                  5       < SUS   R                  5       < 3nXt;   a  XG   US'   O;Xu;   a  XW   US'   O.Xv;   a  Xg   US'   O!X;   a  XX   R                  S5      u  US'   US'   SR                  U5      nU (       + =(       a    US   [
        ;   n	UR                  S5      S:X  ab  US   < SUS   < 3[        ;   a  [        SU-  5      eUS   [        ;   a  Uu  pOUS   [        ;   d  U	(       a	  Un
[        nO[        SU-  5      eUR                  S5      S:X  aP  US   < SUS   < 3[        ;   a	  Un
[        nOU	(       a  Uu  pnU< SU< 3n
OpUu  pn[        X5        U< SU< 3n
OVUR                  S5      S:X  a3  UR                  S5      u  nnpU< SU< 3n[        X5        U< SU< 3n
O[        SU-  5      eU (       a  U(       d  [        S5      eUR                  S	5       Vs/ s H  n[        U5      PM     nn[        U5       Vs/ s H	  nUU
/S
.PM     nn[        U US9$ s  snf s  snf s  snf s  snf s  snf )a  Parses an iam ch bind string to a list of binding tuples.

Args:
  is_grant: If true, binding is to be appended to IAM policy; else, delete
            this binding from the policy.
  input_str: A string representing a member-role binding.
             e.g. user:foo@bar.com:objectAdmin
                  user:foo@bar.com:objectAdmin,objectViewer
                  user:foo@bar.com
                  allUsers
                  deleted:user:foo@bar.com?uid=123:objectAdmin,objectViewer
                  deleted:serviceAccount:foo@bar.com?uid=123

Raises:
  CommandException in the case of invalid input.

Returns:
  A BindingsDictTuple instance.
:r      z+Incorrect public member type for binding %s      zInvalid ch format %szMust specify a role to grant.,r?   r$   )countsplitPUBLIC_MEMBERSlowerTYPESDISCOURAGED_TYPESjoinr
   rL   _check_member_typeResolveRoler,   r   )r   	input_strtokensspublic_memberstypesdiscouraged_typespossible_public_member_or_typepossible_typeremoving_discouraged_typememberrolesmember_type
project_id	member_idmember_type_p1member_type_p2rH   r   s                      r!   BindingStringToTuplero      s   * 
		I ??3&*89.QGGIqL..9!&
'A779a<%
'->?->wwy!|->?#)!9??#4 #AY__.q	0AB-#5>F1I%.5F1I%:!AF1I"177<VAYq	hhv) #+lMvay<M/M__SQ)VAY'50J&' ( (	n	$ovu	e	8feJ&' ( (sq )VAY'50fe	")/&{%z2f(.%{u0%y1fsq 9B9M6^^Y+^<K{.#Y/F
1I=
>>e
:
;;#(;;s#3
4#3a;q>#3%
48;E
C
1qfX.
(C	Hx	@@u :
'?j 5Cs   K(K-=K2(K7K<c                 f    U [         ;   a  [        [        5      eU [        ;  a  [        SU-  5      eg )Nz$Incorrect member type for binding %s)r[   r
   DISCOURAGED_TYPES_MSGrZ   )rj   r_   s     r!   r]   r]   /  s5    %%
0
11%
AIM
NN  r4   c                 6    U (       d  [         $ SU ;   a  U $ SU -  $ )Nzroles/zroles/storage.%s)rL   )r-   s    r!   r^   r^   6  s"    	OK	d	""r4   )#__doc__
__future__r   r   r   r   collectionsr   r   r@   apitools.base.protorpcliter	   gslib.exceptionr
   "gslib.third_party.storage_apitoolsr   r&   r,   rZ   r[   rq   rX   r   r   rL   r"   r*   r3   r6   r=   r8   rO   ro   r]   r^    r4   r!   <module>rz      s    ) & %  ' # " 
 0 , W  	
    K    ?Z,DE2Z4LM  J  *7
!GHMBUApO#r4   