
    A                        S 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\R:                  R<                  R>                  S\R:                  R@                  R>                  S\R:                  RB                  R>                  \RD                  RG                  SSSS5      0r$S%S jr% " S S\RL                  5      r'S r( " S S\RR                  5      r*S&S jr+ " S S\RR                  5      r,S r-S  r.S! r/S'S" jr0   S(S# jr1S&S$ jr2g))zHAnthos command library functions and utilities for the anthoscli binary.    N)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   2   sM    bjj!#**JJz	ggaj 	*    c                       \ rS rSrSrSrg)AnthosAuthException>   z?Base Exception for auth issues raised by gcloud anthos surface. N)__name__
__module____qualname____firstlineno____doc____static_attributes__r$   r    r   r"   r"   >   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,   r-   rel_path
parent_dirs       r   RelativePkgPathFromFullPathr2   B   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$ )AnthosCliWrapperJ   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superr4   __init__selfkwargsr=   	__class__s      r   rA   AnthosCliWrapper.__init__M   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)rC   repo_uri
local_destfile_patternrD   	exec_argss         r   _ParseGetArgsAnthosCliWrapper._ParseGetArgsT   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)rJ   append)rC   	local_dirrK   strategydry_runverboserD   rN   s           r   _ParseUpdateArgs!AnthosCliWrapper._ParseUpdateArgs]   se     	9%I(+,{#h/0{#r    c                     ASU/$ )Ndescr$   )rC   rT   rD   s      r   _ParseDescribeArgs#AnthosCliWrapper._ParseDescribeArgsu   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	iteritemsr?   )rC   tagsxys       r   
_ParseTagsAnthosCliWrapper._ParseTagsy   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)endswithrJ   rf   )	rC   rT   descriptionnamerc   info_urlrD   package_pathrN   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$   )rC   	apply_dirprojectrD   rN   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-crt   z
--locationz--output-directoryrI   )rC   clusterrv   location
output_dirrD   rN   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)	NrH   r   r[   rj   rs   rz   z$Invalid Operation [{}] for anthosclir$   )	rO   rX   r\   rp   rw   r~   r   InvalidOperationForBinaryr?   rC   commandrD   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)   rA   rO   rX   r\   rf   rp   rw   r~   r   r*   __classcell__rE   s   @r   r4   r4   J   sX    8C !% $$$0L
 "&"4
@ @r    r4   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"   r?   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   r6   c                 r   > S[         R                  R                  SS90n[        [        U ]  " SSUS.UD6  g )Nr8   zkubectl-anthosr:   r<   r$   )r   MISSING_AUTH_BINARYr?   r@   r   rA   rB   s      r   rA   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)rC   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--userrR   z--ldap-usernamez--ldap-passwordz--preferred-authz--serverz--remote-loginz--remote-bootstraprI   )rC   r{   kube_configlogin_configlogin_config_certuser	ldap_user	ldap_passrV   preferred_auth
server_url
no_browserremote_bootstraprD   rN   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-fromrI   )rC   r   output_file
merge_fromrD   rN   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   rI   )rC   
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   rD   rN   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   r?   r   s      r   r   &AnthosAuthWrapper._ParseArgsForCommand7  s    '!!+F++	)	)--777	I	[	G	!!+F++77
5
<
<W
EG Gr    r$   )NNNNNNNNNNNNN)r%   r&   r'   r(   r)   rA   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"   r?   	file_pathr   )all_configsr{   found_clusterss      r   _GetClusterConfigr   E  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   )ra   ensure_textbase64	b64encodeensure_binary)ss    r   <lambda>#_Base64EncodeLdap.<locals>.<lambda>Q  s#    #//&"2"233D3DQ3G"HIr    r$   )usernamepasswdencs      r   _Base64EncodeLdapr   O  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   rd   s    r   r   %_GetLdapUserAndPass.<locals>.<lambda>a  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   e  s    #a&1*r    )validation_callable)IsLdapr?   r	   PromptWithValidatorPromptPasswordr   )cluster_config	auth_namer{   r   r   user_messagepass_messages          r   _GetLdapUserAndPassr   U  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requestsrH   status_codecodesokr"   r?   textr   ExpandLocalDirAndVersionr   ReadFileContents)r   certificate_file
config_urlis_urlresponseexpanded_config_pathcontentss          r   GetFileOrURLr  i  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"   r?   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_MESSAGEr?   LOGIN_CONFIG_SUCCESS_MESSAGE)r  list_clusters_onlys     r   LoginResponseHandlerr1    s    __JJX__%__JJX__%__IIh2299(//JK	JJX::;	r    r   )F)T)NFF)3r)   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  ra   	six.movesr   r   r   LINUXr   MACOSXWINDOWSr,   r`   r   r   Errorr"   r2   StreamingBinaryBackedOperationr4   r   r   r   r   r   r  r)  r1  r$   r    r   <module>r@     s-   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    