
    6                     x   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rSSK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Jr  SSKJr  \	R,                  " \	R.                  R0                  \	R.                  R2                  \	R.                  R4                  5       " S S\	R6                  5      5       rg)z5Generate RBAC policy files for Connect Gateway users.    )absolute_import)division)print_function)unicode_literalsN)base)	kube_util)	rbac_util)
exceptions)log)
properties)
console_io)filesc                   .    \ rS rSrSr\S 5       rS rSrg)GenerateGatewayRbac#   ar  Generate RBAC policy files for connected clusters by the user.

{command} generates RBAC policies to be used by Connect Gateway API.

Upon success, this command will write the output RBAC policy to the designated
local file in dry run mode.

Override RBAC policy: Y to override previous RBAC policy, N to stop. If
overriding the --role, Y will clean up the previous RBAC policy and then apply
the new one.

## EXAMPLES
  The current implementation supports multiple modes:

  Dry run mode to generate the RBAC policy file, and write to local directory:

    $ {command} --membership=my-cluster
    --users=foo@example.com,test-acct@test-project.iam.gserviceaccount.com
    --role=clusterrole/cluster-admin --rbac-output-file=./rbac.yaml

  Dry run mode to generate the RBAC policy, and print on screen:

    $ {command} --membership=my-cluster
    --users=foo@example.com,test-acct@test-project.iam.gserviceaccount.com
    --role=clusterrole/cluster-admin

  Anthos support mode, generate the RBAC policy file with read-only permission
  for TSE/Eng to debug customers' clusters:

    $ {command} --membership=my-cluster --anthos-support

  Apply mode, generate the RBAC policy and apply it to the specified cluster:

    $ {command} --membership=my-cluster
    --users=foo@example.com,test-acct@test-project.iam.gserviceaccount.com
    --role=clusterrole/cluster-admin --context=my-cluster-context
    --kubeconfig=/home/user/custom_kubeconfig --apply

  Revoke mode, revoke the RBAC policy for the specified users:

    $ {command} --membership=my-cluster
    --users=foo@example.com,test-acct@test-project.iam.gserviceaccount.com
    --role=clusterrole/cluster-admin --context=my-cluster-context
    --kubeconfig=/home/user/custom_kubeconfig --revoke

  The role to be granted to the users can either be cluster-scoped or
  namespace-scoped. To grant a namespace-scoped role to the users in dry run
  mode, run:

    $ {command} --membership=my-cluster
    --users=foo@example.com,test-acct@test-project.iam.gserviceaccount.com
    --role=role/mynamespace/namespace-reader

  The users provided can be using a Google identity (only email) or using
  external identity providers (starting with
  "principal://iam.googleapis.com"):

    $ {command} --membership=my-cluster
    --users=foo@example.com,principal://iam.googleapis.com/locations/global/workforcePools/pool/subject/user
    --role=clusterrole/cluster-admin --context=my-cluster-context
    --kubeconfig=/home/user/custom_kubeconfig --apply

  The groups can be provided as a Google identity (only email) or an
  external identity (starting with
  "principalSet://iam.googleapis.com"):

    $ {command} --membership=my-cluster
    --groups=group@example.com,principalSet://iam.googleapis.com/locations/global/workforcePools/pool/group/ExampleGroup
    --role=clusterrole/cluster-admin --context=my-cluster-context
    --kubeconfig=/home/user/custom_kubeconfig --apply
c                 >   UR                  S[        [        R                  " S5      S9  UR                  S[        [        R                  " S5      S9  UR                  S[        [        R                  " S5      S9  UR                  SS	[        R                  " S
5      S9  UR                  S[        [        R                  " S5      S9  UR                  S[        [        R                  " S5      S9  UR                  SS	[        R                  " S5      S9  UR	                  SS9nUR                  S[        [        R                  " S5      S9  UR                  S[        [        R                  " S5      S9  UR                  SS	[        R                  " S5      S9  g )Nz--membershipz>          Membership name to assign RBAC policy with.
        )typehelpz--rolez9          Namespace scoped role or cluster role.
        z--rbac-output-filea            If specified, this command will execute in dry run mode and write to
          the file specified with this flag: the generated RBAC policy will not
          be applied to Kubernetes clusters,instead it will be written to the
          designated local file.
        z--apply
store_truezu          If specified, this command will generate RBAC policy and apply to the
          specified cluster.
        )actionr   z	--contextz          The cluster context as it appears in the kubeconfig file. You can get
        this value from the command line by running command:
        `kubectl config current-context`.
        z--kubeconfigz            The kubeconfig file containing an entry for the cluster. Defaults to
            $KUBECONFIG if it is set in the environment, otherwise defaults to
            $HOME/.kube/config.
          z--revokezl          If specified, this command will revoke the RBAC policy for the
          specified users.
        T)requiredz--groupszJ          Group email address or third-party IAM group principal.
        z--userszm          User's email address, service account email address, or third-party IAM subject principal.
        z--anthos-supportzj          If specified, this command will generate RBAC policy
          file for anthos support.
        )add_argumentstrtextwrapdedentadd_mutually_exclusive_group)clsparser
identitiess      @lib/surface/container/fleet/memberships/generate_gateway_rbac.pyArgsGenerateGatewayRbac.Argsp   s   
__     __     __    	 __     __     __     __     44d4CJ__     __     __        c                    [         R                  R                  S5        [        R                  R
                  R                  R                  5       n[        R                  " U5        UR                  (       Ga?  [        R                  R                  SR                  UR                  UR                   5      5        ["        R$                  " ['        USS 5      ['        USS 5      S9 nUR)                  5         [+        5       nUR,                  (       a-  UR,                  R/                  S5       Vs/ s H  oUS4PM     nnOvUR0                  (       a(  UR3                  [        R4                  " U5      S45        O=UR6                  (       a,  UR6                  R/                  S5       Vs/ s H  ofS4PM     nnU H  u  pxS	R                  U5      n	[8        R:                  " U	SS
9  [         R                  R                  S5        [         R                  R                  SR                  U5      5        UR=                  UR>                  UR@                  UUUUR0                  5      n
URC                  U
5      (       d  M  [         R                  R                  SR                  U5      5        M      S S S 5        g [        RD                  " X5      nURF                  (       a9  [        R                  R                  SR                  URF                  5      5        O>[        R                  R                  S5        [        R                  R                  S5        Sn[I        URK                  5       5       H  nXRM                  U5      -  nM     [         RN                  " URF                  (       a  URF                  OSUSSSS9  URP                  (       Ga5  [        R                  R                  SR                  UR                  UR                   5      5        ["        R$                  " ['        USS 5      ['        USS 5      S9 nUR)                  5         URK                  5        GH  u  px[R        RT                  " 5        nUS-   nURM                  Xx45      n[R        RV                  " X5        UR=                  UR>                  UR@                  UUUUR0                  5      n
URY                  U
5      (       Gd  SnSnUR[                  U5      u  nnUb0  Sn[         R                  R                  SR                  U5      5        Ub  SU;   al  UR]                  SX!R>                  U5      nUR_                  UUR@                  5      n[         R                  R                  SR                  U5      5        SnSnO&[`        Rb                  " SR                  UU5      5      eU(       a  Sn	[8        R:                  " U	SS
9  U(       a  [         R                  R                  S5        [         R                  R                  SR                  U5      5        URC                  U
5      (       a.  [         R                  R                  SR                  U5      5         [         R                  R                  SR                  U5      5        URe                  U5         S S S 5        [         R                  R                  S5        GM     S S S 5        g g s  snf s  snf ! , (       d  f       GNi= f! [f         a4  n[         R                  R                  SR                  U5      5        e S nAff = f! , (       d  f       N= f! , (       d  f       g = f) NzValidating input arguments.zGRevoking the RBAC policy from cluster with kubeconfig: {}, context: {}

kubeconfigcontext)r%   r&   ,TFz*The RBAC policy for {} will be cleaned up.)messagecancel_on_noz,--------------------------------------------z%Start cleaning up RBAC policy for: {}z5Finished cleaning up the previous RBAC policy for: {}z.Generated RBAC policy is written to file: {} 
zGenerated RBAC policy is: 
z---------------------------------------------
 -)	overwritebinaryprivatezNApplying the generate RBAC policy to cluster with kubeconfig: {}, context: {}
z
/rbac.yamlz0The new RBAC policy has diff with previous: 
 {}zInvalid value
permissionz<The existing RBAC policy has conflicts with proposed one:
{}zEError when getting diff for RBAC policy files for: {}, with error: {}z!The RBAC file will be overridden.z.Start cleaning up previous RBAC policy for: {}z,Writing RBAC policy for user: {} to cluster.z0Error in applying the RBAC policy to cluster: {}z0Successfully applied the RBAC policy to cluster.)4r   statusPrintr   VALUEScoreproject	GetOrFailr	   ValidateArgsrevokesysstdoutwriteformatr%   r&   r   KubernetesClientgetattrCheckClusterAdminPermissionslistuserssplitanthos_supportappendGetAnthosSupportUsergroupsr   PromptContinueGetRBACForOperations
membershiproleCleanUpRbacPolicyGenerateRBACrbac_output_filesortedkeysgetWriteToFileOrStdoutapply
file_utilsTemporaryDirectoryWriteFileContentsGetRbacPolicyGetRbacPolicyDiffRbacPolicyNameGetRbacPermissionPolicyr
   ErrorApplyRbacPolicy	Exception)selfargs
project_idkube_clientidentities_listusergroupidentityis_userr(   rbacgenerated_rbacfinal_rbac_policytmp_dirfilecurrent_rbac_policyneed_clean_upoverride_checkdifferrrbac_policy_namerbac_permission_policyes                          r    RunGenerateGatewayRbac.Run   s%   JJ23""''//99;J 4  {{{	jj&$,,7
 %%T<6$	40 002&::6:jj6F6Fs6KL6KdD\6K/L/  

 
 --j94@ [[9=9J9J39OP9OU^9O/P!0H@GG' 
#
#G$
G
**

I
J
**

5<<XF 11ooii!!$ **400JJGNN% "1. 	M R ++D=N	jj
;
B
B## 
jj56	jjGH ~**,---d33 .!%!6!6C zzz	jj!6$//4<<@
 %%T<6$	40 002!/!4!4!6H,,.'\)D"0"4"4h5H"I((C33		##&D ,,T22#m$n%77=idC!!%

  GNN  #c)%0%?%?"J&" ,7+N+N&		,( **""""(&)?"@ #'-#'."((,,2F8S,A 
  =))'M

  !OP

  DKK 
 0066**""Mvh'
	jj@GGQ ))$/Y /d **

M
Ng "7  w M Q z  jjDKKAN 	[ /. sv   (A	\1\	>A*\(\5C\=3\<]7=H]&?\%,]7	
\
\"%
]#	//]	]#	#]&&
]40]77
^ N)	__name__
__module____qualname____firstlineno____doc__classmethodr!   rr   __static_attributes__rt   r#   r    r   r   #   s'    
FP Q QfpOr#   r   )ry   
__future__r   r   r   r   r8   r   googlecloudsdk.callioper   *googlecloudsdk.command_lib.container.fleetr   r	   googlecloudsdk.corer
   r   r   googlecloudsdk.core.consoler   googlecloudsdk.core.utilr   rR   ReleaseTracksReleaseTrackALPHABETAGACommandr   rt   r#   r    <module>r      s    < &  % ' 
  ( @ @ * # * 2 8 T..33T5F5F5I5INO$,, NONOr#   