
    ;B                        S 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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  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0r \RB                  RD                  RF                  S\RB                  RH                  RF                  S\RB                  RJ                  RF                  \RL                  RO                  SSSS5      0r(S(S jr) " S S\RT                  5      r+S r, " S S\RZ                  5      r.S)S jr/ " S  S!\RZ                  5      r0S" r1S# r2S$ r3S*S% jr4   S+S& jr5S)S' jr6g),zHAnthos command library functions and utilities for the anthoscli binary.    )absolute_import)division)unicode_literalsN)flags)file_parsers)messages)binary_operations)
exceptions)log)
console_io)store)files)	platforms)urllibCOBRA_SILENCE_USAGEtruez2~/.config/google/anthos/kubectl-anthos-config.yamlz>~/Library/Preferences/google/anthos/kubectl-anthos-config.yamlz	%APPDATA%googleanthoszkubectl-anthos-config.yamlc                     [         R                  " [        R                  5      nUR	                  [
        5        U (       a  UR	                  U 5        U(       a  U H  nUR                  U5        M     U$ )z6Return an env dict to be passed on command invocation.)copydeepcopyosenvironupdateDEFAULT_ENV_ARGSpop)
extra_varsexclude_varsenvks       :lib/googlecloudsdk/command_lib/anthos/anthoscli_backend.pyGetEnvArgsForCommandr"   5   sM    bjj!#**JJz	ggaj 	*    c                       \ rS rSrSrSrg)AnthosAuthExceptionA   z?Base Exception for auth issues raised by gcloud anthos surface. N)__name__
__module____qualname____firstlineno____doc____static_attributes__r'   r#   r!   r%   r%   A   s    Gr#   r%   c                     [         R                  R                  U 5      n[         R                  R                  U5      n[         R                  R	                  U5      =(       d    UnX#4$ )z=Splits full path into relative(basename) path and parent dir.)r   pathnormpathbasenamedirname)r/   r0   rel_path
parent_dirs       r!   RelativePkgPathFromFullPathr5   E   sJ    WWd#(WWh'(wwx(4H*		r#   c                   x   ^  \ rS rSrSrU 4S jrSS jr    SS jrS rS r	    SS jr
S	 rS
 rS rSrU =r$ )AnthosCliWrapperM   0Binary operation wrapper for anthoscli commands.c                 r   > S[         R                  R                  SS90n[        [        U ]  " SSUS.UD6  g )NMISSING_EXEC	anthosclibinaryr>   custom_errorsr'   )r   MISSING_BINARYformatsuperr7   __init__selfkwargsr@   	__class__s      r!   rD   AnthosCliWrapper.__init__P   sJ    //66k6JM 

D* C-C;ACr#   c                 D    ASX/nU(       a  UR                  SU/5        U$ )Ngetz	--patternextend)rF   repo_uri
local_destfile_patternrG   	exec_argss         r!   _ParseGetArgsAnthosCliWrapper._ParseGetArgsW   s+    -I\23r#   c                     ASU/nU(       a  UR                  SU/5        U(       a  UR                  S5        U(       a  UR                  SU/5        U(       a  UR                  S5        U$ )Nr   z--repo	--dry-runz
--strategyz	--verbose)rM   append)rF   	local_dirrN   strategydry_runverboserG   rQ   s           r!   _ParseUpdateArgs!AnthosCliWrapper._ParseUpdateArgs`   se     	9%I(+,{#h/0{#r#   c                     ASU/$ )Ndescr'   )rF   rW   rG   s      r!   _ParseDescribeArgs#AnthosCliWrapper._ParseDescribeArgsx   s    Ir#   c           
          SR                  [        R                  " U5       VVs/ s H  u  p#SR                  X#5      PM     snn5      $ s  snnf )N,z{}={})joinsix	iteritemsrB   )rF   tagsxys       r!   
_ParseTagsAnthosCliWrapper._ParseTags|   s9    88cmmD6IJ6IdaW^^A)6IJKKJs   A

c                 8   AUnUR                  S5      (       d  US-  nSU/nU(       a  UR                  SU/5        U(       a  UR                  SU/5        U(       a"  UR                  SU R                  U5      /5        U(       a  UR                  SU/5        U$ )N/initz--descriptionz--namez--tagz--url)endswithrM   ri   )	rF   rW   descriptionnamerf   info_urlrG   package_pathrQ   s	            r!   _ParseInitArgsAnthosCliWrapper._ParseInitArgs   s     	L  %%cl&I56$'(!678*+r#   c                     ASSUSU/nU$ )Napplyz-f	--projectr'   )rF   	apply_dirprojectrG   rQ   s        r!   _ParseApplyArgs AnthosCliWrapper._ParseApplyArgs   s    $	;@Ir#   c                 ~    ASSUSU/nU(       a  UR                  SU/5        U(       a  UR                  SU/5        U$ )Nexportz-crw   z
--locationz--output-directoryrL   )rF   clusterry   location
output_dirrG   rQ   s          r!   _ParseExportArgs!AnthosCliWrapper._ParseExportArgs   sH    4+w?Ih/0,j9:r#   c                 l   US:X  a  U R                   " S0 UD6$ US:X  a  U R                  " S0 UD6$ US:X  a  U R                  " S0 UD6$ US:X  a  U R                  " S0 UD6$ US:X  a  U R                  " S0 UD6$ US:X  a  U R
                  " S0 UD6$ [        R                  " SR                  U5      5      e)	NrK   r   r^   rm   rv   r}   z$Invalid Operation [{}] for anthosclir'   )	rR   r[   r_   rs   rz   r   r	   InvalidOperationForBinaryrB   rF   commandrG   s      r!   _ParseArgsForCommand%AnthosCliWrapper._ParseArgsForCommand   s    %)&))("",V,,&$$.v..&  *6**'!!+F++("",V,,

5
5.55g>@ @r#   r'   N)NNFF)NNNN)r(   r)   r*   r+   r,   rD   rR   r[   r_   ri   rs   rz   r   r   r-   __classcell__rH   s   @r!   r7   r7   M   sX    8C !% $$$0L
 "&"4
@ @r#   r7   c                      [         R                  " XS9nSU0n[
        R                  " USS9$ ! [         a  n[        SR	                  XS95      eSnAff = f)z@Generate a JSON object containing the current gcloud auth token.)allow_account_impersonation
auth_tokenz<Error retrieving auth credentials for {operation}: {error}. )	operationerrorNT)	sort_keys)c_storeGetFreshAccessToken	Exceptionr%   rB   jsondumps)accountr   impersonatedaccess_tokenoutputes         r!   GetAuthTokenr      sq    	+..;L 	lF 
Fd	++	 
 +
FMM 	N 	*+ ++s   / 
AAAc                   v   ^  \ rS rSrSrU 4S jr\S 5       r           S
S jr  SS jr	S r
S rS	rU =r$ )AnthosAuthWrapper   r9   c                 r   > S[         R                  R                  SS90n[        [        U ]  " SSUS.UD6  g )Nr;   zkubectl-anthosr=   r?   r'   )r   MISSING_AUTH_BINARYrB   rC   r   rD   rE   s      r!   rD   AnthosAuthWrapper.__init__   sL    ((//7G/HM 

T+ H}H@FHr#   c                     [         R                  " [        [        R                  R                  5       R                     5      $ r   )r   ExpandHomeAndVarsDEFAULT_LOGIN_CONFIG_PATHr   OperatingSystemCurrentid)rF   s    r!   default_config_path%AnthosAuthWrapper.default_config_path   s4    ""!)";";"C"C"E"H"HIK Kr#   c                 X   AS/nU(       a  UR                  SU/5        U(       a  UR                  SU/5        U(       a  UR                  SU/5        U(       a  UR                  SU/5        U(       a  UR                  SU/5        U(       a  UR                  S/5        U(       a  U(       a  UR                  SUS	U/5        U	(       a  UR                  S
U	/5        U
(       a  UR                  SU
/5        U(       a  UR                  S/5        U(       a  UR                  SU/5        U$ )Nlogin	--cluster--kubeconfigz--login-configz--login-config-cert--userrU   z--ldap-usernamez--ldap-passwordz--preferred-authz--serverz--remote-loginz--remote-bootstraprL   )rF   r~   kube_configlogin_configlogin_config_certuser	ldap_user	ldap_passrY   preferred_auth
server_url
no_browserremote_bootstraprG   rQ   s                  r!   _ParseLoginArgs!AnthosAuthWrapper._ParseLoginArgs   s     		IW-.45(,78-/@AB$'(}%Yi):I
FH*N;<
J/0()*,.>?@r#   c                     AS/nUR                  SU/5        U(       a  UR                  SU/5        U(       a  UR                  SU/5        U$ )Ncreate-login-configr   z--outputz--merge-fromrL   )rF   r   output_file
merge_fromrG   rQ   s         r!   _ParseCreateLoginConfigArgs-AnthosAuthWrapper._ParseCreateLoginConfigArgs  sT    
 	&'Ink23
K01
34r#   c                    AS/nU(       a  UR                  SU/5        U(       a  UR                  SU/5        U(       a  UR                  SU/5        U(       a  UR                  SU/5        U(       a  UR                  SU/5        U(       a  UR                  SU/5        U(       a  UR                  SU/5        U(       a  UR                  S	U/5        U	(       a  UR                  S
U	/5        U
(       a  UR                  SU
/5        U(       a  UR                  SU/5        U(       a  UR                  SU/5        U(       a  UR                  SU/5        U$ )Ntokenz--typer   z--aws-sts-regionz
--id-tokenz--access-tokenz--access-token-expiryz--refresh-tokenz--client-idz--client-secretz --idp-certificate-authority-dataz--idp-issuer-urlz--kubeconfig-pathr   rL   )rF   
token_typer~   aws_sts_regionid_tokenr   access_token_expiryrefresh_token	client_idclient_secretidp_certificate_authority_dataidp_issuer_urlkubeconfig_pathr   rG   rQ   s                   r!   _ParseTokenArgs!AnthosAuthWrapper._ParseTokenArgs  s?    		I*-.W-.*N;<h/0(,78/1DEF)=9:y12)=9:%-/M
NP*N;<+_=>$'(r#   c                     US:X  a  U R                   " S0 UD6$ US:X  a  U R                  " S0 UD6$ US:X  a  S/$ US:X  a  U R                  " S0 UD6$ [        R                  " SR                  U5      5      e)Nr   r   versionr   z)Invalid Operation [{}] for kubectl-anthosr'   )r   r   r   r	   r   rB   r   s      r!   r   &AnthosAuthWrapper._ParseArgsForCommand:  s    '!!+F++	)	)--777	I	[	G	!!+F++77
5
<
<W
EG Gr#   r'   )NNNNNNNNNNNNN)r(   r)   r*   r+   r,   rD   propertyr   r   r   r   r   r-   r   r   s   @r!   r   r      sj    8H K K *\ /3-1"HG Gr#   r   c                     U R                  [        R                  R                  U5      n[	        U5      S:w  a$  [        SR                  XR                  5      5      eUR                  5       $ )N   z+Cluster [{}] not found for config path [{}])	FindMatchingItemr   LoginConfigObjectCLUSTER_NAME_KEYlenr%   rB   	file_pathr   )all_configsr~   found_clusterss      r!   _GetClusterConfigr   H  sf    //$$55w@.A
5<<**	,- - 
			r#   c                 (    S nU" U 5      U" U5      4$ )z)Base64 Encode Ldap username and password.c                 ~    [         R                  " [        R                  " [         R                  " U 5      5      5      $ r   )rd   ensure_textbase64	b64encodeensure_binary)ss    r!   <lambda>#_Base64EncodeLdap.<locals>.<lambda>T  s#    #//&"2"233D3DQ3G"HIr#   r'   )usernamepasswdencs      r!   _Base64EncodeLdapr   R  s    I#	XF	##r#   c                     SnSnU R                  5       (       d  gSR                  X5      nSR                  X5      n[        R                  " S SUS9n[        R                  " US S	9n[        X45      $ )
z+Prompt User for Ldap Username and Password.Nr   z5Please enter the ldap user for [{}] on cluster [{}]: z9Please enter the ldap password for [{}] on cluster [{}]: c                     [        U 5      S:  $ Nr   r   rg   s    r!   r   %_GetLdapUserAndPass.<locals>.<lambda>d  s    #a&1*r#   z*Error: Invalid username, please try again.)	validatorerror_messageprompt_stringc                     [        U 5      S:  $ r   r   r   s    r!   r   r   h  s    #a&1*r#   )validation_callable)IsLdaprB   r   PromptWithValidatorPromptPasswordr   )cluster_config	auth_namer~   r   r   user_messagepass_messages          r!   _GetLdapUserAndPassr   X  s    ))				 	 ++16)+E ++16)+E ,,$@ ") ''(<>)	9	00r#   c                    U (       d  g[         R                  R                  U 5      nUR                  S:H  =(       d    UR                  S:H  nU(       a  [        R
                  " X=(       d    SS9nUR                  [        R                  R                  :w  a/  [        SR                  UR                  UR                  5      5      eXR                  U4$ [        R                  " U 5      n[        R                  " U5      nXVU4$ )af  Parses config input to determine whether URL or File logic should execute.

   Determines whether the cluster_config is a file or URL. If it's a URL, it
   then pulls the contents of the file using a GET request. If it's a
   file, then it expands the file path and returns its contents.

Args:
  cluster_config: str, A file path or URL for the login-config.
  certificate_file: str, Optional file path to the CA certificate to use with
    the GET request to the URL.

Raises:
  AnthosAuthException: If the data could not be pulled from the URL.

Returns:
  parsed_config_fileOrURL, config_contents, and is_url
  parsed_config_fileOrURL: str, returns either the URL that was passed or an
    expanded file path if a file was passed.
    config_contents: str, returns the contents of the file or URL.
  is_url: bool, True if the provided cluster_config input was a URL.
NNNhttphttpsT)verifyzIRequest to login-config URL failed withresponse code [{}] and text [{}]: )r   parseurlparseschemerequestsrK   status_codecodesokr%   rB   textr   ExpandLocalDirAndVersionr   ReadFileContents)r   certificate_file
config_urlis_urlresponseexpanded_config_pathcontentss          r!   GetFileOrURLr  l  s    . 
 ||$$^4*&F**;*;w*F&||N3KtLHx~~000 !EEKV$,$8$8(--FIJ J ==&00 77G##$89(		//r#   c                    U (       a  U(       d  gSnU(       aE  U(       d  [        SR                  U5      5      e[        R                  " U[        R                  S9nO$[        R                  " UU[        R                  S9n[        XP5      n UR                  5       nU(       a  U(       Ga  UR                  5       nU(       d  [        SR                  U5      5      e[        U5      S:X  a  UR                  5       nOTSR                  U 5      n	S	n
U(       a!  U(       a  U(       d  XR                  U5      -   n	[        R                  " XS
S9nX   n[        R                  R!                  SR                  U5      5        UR#                  U5        U(       a  U(       d  UR%                  5         ['        XgU 5      u  pX|U4$ ! [         a    Sn GN2[        R                   a     gf = f)z.Get preferredAuthentication value for cluster.r   Nz-Config contents were not passed with URL [{}])file_contents	item_type)r  r   r  z)No Authentication Providers found in [{}]r   zCPlease select your preferred authentication option for cluster [{}]zN. Note: This will overwrite current preferred auth method [{}] in config file.T)messagecancel_optionz/Setting Preferred Authentication option to [{}])r%   rB   r   YamlConfigFiler   r   GetPreferredAuthKeyErrorYamlConfigObjectFieldErrorGetAuthProvidersr   r   r   PromptChoicer   statusPrintSetPreferredAuthWriteToDiskr   )r~   r   config_contentsforce_updater  configsr   auth_method	providersprompt_messageoverride_warningindexr   r   s                 r!   GetPreferredAuthForClusterr,    s    l'
9
@
@
NP P))%1O1OQG ))%002G
 %W6. 113K 
//1I
5
<
<\
JL L
9~MMOk++16'? 8 
f'*A*A+*NN%%
4Ae$kJJ9@@MO##K0F,^-46)		**E 
 K		0	0  s   G G&G&%G&c                    U R                   (       a)  [        R                  R                  U R                   5        U R                  (       a)  [        R                  R                  U R                  5        U R
                  (       a>  [        R                  " [        R                  R                  U R                  5      5        gU(       d-  [        R                  R                  [        R                  5        U R                   $ )zHandle Login Responses.N)stdoutr   r   r!  stderrfailedr   r   LOGIN_CONFIG_FAILED_MESSAGErB   LOGIN_CONFIG_SUCCESS_MESSAGE)r  list_clusters_onlys     r!   LoginResponseHandlerr4    s    __JJX__%__JJX__%__IIh2299(//JK	JJX::;	r#   r   )F)T)NFF)7r,   
__future__r   r   r   r   r   r   r   !googlecloudsdk.command_lib.anthosr   (googlecloudsdk.command_lib.anthos.commonr   r   &googlecloudsdk.command_lib.util.anthosr	   googlecloudsdk.corer
   c_exceptr   googlecloudsdk.core.consoler   googlecloudsdk.core.credentialsr   r   googlecloudsdk.core.utilr   r   r  rd   	six.movesr   r   r   LINUXr   MACOSXWINDOWSr/   rc   r   r"   Errorr%   r5   StreamingBinaryBackedOperationr7   r   r   r   r   r   r  r,  r4  r'   r#   r!   <module>rD     s6   O &  '    	 3 A = D 6 # 2 < * .  
 )62  ##&&<$$''H%%((
[(H1	3 	H(.. Hm@(GG m@`,yG)HH yGx$1((0Z 04,1&+	>+Br#   