
    J                     X   S r SSKrSSKrSSKrSSK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rSr Sr!Sr"\RF                  r$Sr%\RL                  S 5       r' " S S\RP                  " \RR                  5      5      r*S r+ " S S\*5      r, " S S\*5      r-S r. " S S\*5      r/ " S S\*5      r0S#S  jr1  S$S! jr2\Rf                  Rh                  \Rj                  Rl                  SSSS4S" jr7g)%z,Dynamic context for connection to Cloud Run.    N)gke)global_methods)apis)base)
exceptions)flags)	platforms)
properties)	transport)files)parseanthoseventsv1beta1eventsv1alpha1c              #      #    [        [        R                  R                  U 5      nUR	                  5       n UR                  U5        Sv   UR                  U5        g! UR                  U5        f = f7f)zContext manager to override an API's endpoint overrides for a while.

Args:
  api_name: str, Name of the API to modify.
  override: str, New value for the endpoint.

Yields:
  None.
N)getattrr
   VALUESapi_endpoint_overridesGetSet)api_nameoverrideendpoint_propertyold_endpoints       8lib/googlecloudsdk/command_lib/run/connection_context.py_OverrideEndpointOverridesr   1   sd      j//FF&("&&(,((#	,','s   5A5A A5A22A5c                      \ rS rSrSrS r\S 5       r\S 5       r\S 5       r	\
R                  S 5       r\
R                  S 5       r\
R                  S	 5       r\
R                  S
 5       r\
R                  S 5       rS rS rS rSrg)ConnectionInfoF   z1Information useful in constructing an API client.c                 T    SU l         SU l        SU l        SU l        Xl        X l        g)zInitialize a connection context.

Args:
  api_name: str, api name to use for making requests.
  version: str, api version to use for making requests.
N)endpointca_certsregion_cm	_api_name_version)selfr   versions      r   __init__ConnectionInfo.__init__I   s*     DMDMDKDHNM    c                     U R                   $ N)r&   r(   s    r   r   ConnectionInfo.api_nameW   s    >>r,   c                     U R                   $ r.   )r'   r/   s    r   api_versionConnectionInfo.api_version[   s    ==r,   c                     U R                   $ r.   )_activer/   s    r   activeConnectionInfo.active_   s    <<r,   c                     g r.    r/   s    r   ConnectConnectionInfo.Connectc       r,   c                     g r.   r9   r/   s    r   operatorConnectionInfo.operatorg   r<   r,   c                     g r.   r9   r/   s    r   ns_labelConnectionInfo.ns_labelk   r<   r,   c                     g r.   r9   r/   s    r   supports_one_platform$ConnectionInfo.supports_one_platformo   r<   r,   c                     g r.   r9   r/   s    r   location_labelConnectionInfo.location_labels   r<   r,   c                     g)zThe HTTP client to use to connect.

May only be called inside the context represented by this ConnectionInfo

Returns: An HTTP client specialized to connect in this context, or None if
a standard HTTP client is appropriate.
Nr9   r/   s    r   
HttpClientConnectionInfo.HttpClientw   s     r,   c                 n    SU l         U R                  5       U l        U R                  R                  5       $ NT)r5   r:   r%   	__enter__r/   s    r   rN   ConnectionInfo.__enter__   s)    DL||~DH88r,   c                 H    SU l         U R                  R                  XU5      $ NF)r5   r%   __exit__)r(   typvalue	tracebacks       r   rR   ConnectionInfo.__exit__   s     DL88S33r,   )r5   r&   r%   r'   r#   r"   r$   N)__name__
__module____qualname____firstlineno____doc__r*   propertyr   r2   r6   abcabstractmethodr:   abstractpropertyr>   rA   rD   rG   rJ   rN   rR   __static_attributes__r9   r,   r   r   r   F   s    9       	 	 	 	 	 	 	 	 	 	 
4r,   r   c                  8   [         R                  " S[        R                  5      (       a3  [        R
                  " SR                  [        R                  5      5      e[        [        S5      (       d  [        [        S5      (       d  [        R                  R                  S:X  a  SOSn [        R
                  " SR                  [        R                  R                  [        R                  R                  [        R                  R                  U 5      5      eg	g	)
zBProvide a useful error message if the user's doesn't have TLS 1.2.zOpenSSL 0\.zYour Python installation is using the SSL library {}, which does not support TLS 1.2. TLS 1.2 is required to connect to Cloud Run on Kubernetes Engine. Please use python with OpenSSL >1.0PROTOCOL_TLSPROTOCOL_TLSv1_2   z2.7.9z3.4zYour Python {}.{}.{} installation does not support TLS 1.2, which is required to connect to Cloud Run on Kubernetes Engine. Please upgrade to Python {} or greater.N)rematchsslOPENSSL_VERSIONserverless_exceptions
NoTLSErrorformathasattrsysversion_infomajorminormicro)min_required_versions    r   _CheckTLSSupportrs      s    XXnc1122

*
*	 vc112	4 4 #~
&
&'#7I*J*J'*'7'7'='='BG

*
*	228&""C$4$4$:$:""$83:; ; +K
&r,   c                      ^  \ rS rSrSrU 4S jr\R                  S 5       r\	S 5       r
S r\	S 5       r\	S 5       r\	S	 5       r\	S
 5       r\	S 5       r\	S 5       rSrU =r$ )GKEConnectionContext   z7Context manager to connect to the GKE Cloud Run add-in.c                 8   > [         [        U ]  X#5        Xl        g r.   )superru   r*   cluster_ref)r(   ry   r   r)   	__class__s       r   r*   GKEConnectionContext.__init__   s    	
.xA"r,   c              #     #    [        5         [        R                  " U R                  5       u  pX l        [        R
                  " SU5       nSR                  U5      U l        [        U R                  U R                  5         U v   S S S 5        S S S 5        S S S 5        g ! , (       d  f       N= f! , (       d  f       N(= f! , (       d  f       g = f7f)Nkubernetes.defaultzhttps://{}/)
rs   r   ClusterConnectionInfory   r#   MonkeypatchAddressCheckingrk   r"   r   r&   )r(   ipr#   r"   s       r   r:   GKEConnectionContext.Connect   s     		"	"4#3#3	4m))*>Cx%,,X6'F* G D 
5	4 GF DC 
5	4sL   +C C7B3B"	B3C	C"
B0,B33
C	=C
CCc                     gNzCloud Run for Anthosr9   r/   s    r   r>   GKEConnectionContext.operator       !r,   c                     U R                   (       d   eSSKJn  UR                  [        R
                  U R                  S9nU$ )Nr   
transportsresponse_encodingr#   )r6   googlecloudsdk.core.credentialsr   GetApitoolsTransportr   ENCODINGr#   r(   r   http_clients      r   rJ   GKEConnectionContext.HttpClient   s>     ;;;:11#,,t}} 2 FKr,   c                 8    SR                  U R                  5      $ )Nz) of cluster [{{{{bold}}}}{}{{{{reset}}}}])rk   cluster_namer/   s    r   rG   #GKEConnectionContext.location_label   s    6==d>O>OPPr,   c                 6    U R                   R                  5       $ r.   )ry   Namer/   s    r   r   !GKEConnectionContext.cluster_name   s      ""r,   c                 .    U R                   R                  $ r.   )ry   zoner/   s    r   cluster_location%GKEConnectionContext.cluster_location   s       r,   c                 .    U R                   R                  $ r.   )ry   	projectIdr/   s    r   cluster_project$GKEConnectionContext.cluster_project   s    %%%r,   c                     grQ   r9   r/   s    r   rD   *GKEConnectionContext.supports_one_platform       r,   c                     gN	namespacer9   r/   s    r   rA   GKEConnectionContext.ns_label       r,   )r#   ry   r"   )rW   rX   rY   rZ   r[   r*   
contextlibcontextmanagerr:   r\   r>   rJ   rG   r   r   r   rD   rA   r`   __classcell__rz   s   @r   ru   ru      s    ?#   " " Q Q # # ! ! & &    r,   ru   c                     ^  \ rS rSrSrSU 4S jjr\R                  S 5       rS r	\
S 5       r\
S 5       r\
S 5       r\
S	 5       r\
S
 5       r\
S 5       r\R                  S 5       r\R                  S 5       rSrU =r$ )KubeconfigConnectionContext   zEContext manager to connect to a cluster defined in a Kubeconfig file.c                    > [         [        U ]  X#5        Xl        U R                  R	                  U=(       d    UR
                  5        SU l        SU l        SU l        SU l	        g)a  Initialize connection context based on kubeconfig file.

Args:
  kubeconfig: googlecloudsdk.api_lib.container.kubeconfig.Kubeconfig object
  api_name: str, api name to use for making requests
  version: str, api version to use for making requests
  context: str, current context name
N)
rx   r   r*   
kubeconfigSetCurrentContextcurrent_contextclient_cert_dataclient_cert
client_keyclient_cert_domain)r(   r   r   r)   contextrz   s        r   r*   $KubeconfigConnectionContext.__init__   sV     

%t5hH OOO%%g&K1K1KL DDDO"Dr,   c              #     #    [        5         U R                  5           U R                  (       aK  [        R                  " SU R
                  5       nSR                  XR                  5      U l        S S S 5        O+SR                  U R
                  U R                  5      U l        [        U R                  U R                  5         U v   S S S 5        S S S 5        g ! , (       d  f       ND= f! , (       d  f       N(= f! [        R                  [        R                  R                  4 a6  nS[        R                   " U5      ;   a  [        R"                  " S5      ee S nAff = f! , (       d  f       g = f7f)Nr}   zhttps://{}/{}CERTIFICATE_VERIFY_FAILEDzdMissing or invalid [certificate-authority] or [certificate-authority-data] field in kubeconfig file.)rs   _LoadClusterDetailsca_datar   r   raw_hostnamerk   raw_pathr"   r   r&   rg   SSLErrorrequestsr   six	text_typeNoCaCertError)r(   r"   es      r   r:   #KubeconfigConnectionContext.Connect   s"    		!	!	#<<--.B.2.?.?ADL+228]]KDMA A *001B1B15@$-'F* G 
$	#A A GF llH//889 &#--*::!!GH H  
$	#sv   E6E%2C>!C2AC>C-C>	E6
C*	&C>-
C;	7C>:E%;C>>.E",1EE""E%%
E3/E6c                    U R                   (       d   eU R                  (       d-  U R                  (       a  U R                  (       a  [	        S5      eU R                  (       aF  SSKJn  UR                  [        R                  U R                  U R                  U R                  S9nU$ SSKJn  UR                  [        R                  U R                  S9nU$ )NzNKubeconfig authentication requires a client certificate authentication method.r   r   )r   r#   client_certificater   r   )r6   r   r   r   
ValueErrorgooglecloudsdk.corer   r   r   r   r#   r   r   s      r   rJ   &KubeconfigConnectionContext.HttpClient  s    ;;;??t//D4K4K#$ $  133%..==!--__	 4 &k
 :11#,,t}} 2 FKr,   c                     gr   r9   r/   s    r   r>   $KubeconfigConnectionContext.operator  r   r,   c                 h    SR                  U R                  S   U R                  R                  5      $ )Nza of context [{{{{bold}}}}{}{{{{reset}}}}] referenced by config file [{{{{bold}}}}{}{{{{reset}}}}]name)rk   curr_ctxr   filenamer/   s    r   rG   *KubeconfigConnectionContext.location_label  s2    GGMvf%t'?'?HABr,   c                      U R                   S   $ )Nr   )clusterr/   s    r   r   (KubeconfigConnectionContext.cluster_name"  s    <<r,   c                     g r.   r9   r/   s    r   r   ,KubeconfigConnectionContext.cluster_location&      r,   c                     grQ   r9   r/   s    r   rD   1KubeconfigConnectionContext.supports_one_platform*  r   r,   c                     gr   r9   r/   s    r   rA   $KubeconfigConnectionContext.ns_label.  r   r,   c              #   V  #    U(       a  Uv   g U(       as  [         R                  " 5       u  p1[        R                  " U5         [        R
                  " U[        R                  " U5      SS9  Uv   [        R                  " U5        g S v   g ! [        R                  " U5        f = f7f)NT)private)	tempfilemkstemposcloser   WriteBinaryFileContentsbase64	b64decoderemove)r(   fdfds       r   _WriteDataIfNoFile.KubeconfigConnectionContext._WriteDataIfNoFile2  sp     g	
 ebhhrl%%a)9)9!)<dK
		!j 			!s   AB).B 2B)B&&B)c              #     #     U R                   R                  U R                   R                     U l        U R                   R                  U R                  S   S      U l        U R
                  S   R                  SS5      U l        U R                  (       d$  U R
                  S   R                  SS5      U l        [        R                  " U R
                  S   S   5      nUR                  U l        UR                  (       a$  UR                  R                  S5      S-   U l        OSU l        U R                   R                  U R                  S   S	      U l        U R                   S	   R                  S
S5      U l        SU l        SU l        U R"                  (       d$  U R                   S	   R                  SS5      U l        U R                   S	   R                  SS5      U l        U R(                  (       d$  U R                   S	   R                  SS5      U l        U R5                  U R                  U R                  5       nU R5                  U R"                  U R$                  5       nU R5                  U R(                  U R&                  5       nX0l        X@l        XPl        U R(                  (       a.  [6        R8                  (       a  SU l        OU R                  U l        Sv   SSS5        SSS5        SSS5        g! [*         a7  n[,        R.                  " SR1                  UR2                  S   5      5      eSnAff = f! , (       d  f       Nc= f! , (       d  f       Nl= f! , (       d  f       g= f7f)zGet the current cluster and its connection info from the kubeconfig.

Yields:
  None.
Raises:
  flags.KubeconfigError: if the config file has missing keys or values.
r   r   zcertificate-authorityNzcertificate-authority-dataserver/ userz
client-keyzclient-key-datazclient-certificatezclient-certificate-datazMissing key `{}` in kubeconfig.r   r}   )r   contextsr   r   clustersr   getr#   r   urlparsehostnamer   pathstripr   usersr   r   client_key_datar   r   KeyErrorr   KubeconfigErrorrk   argsr   r   PY2r   )r(   parsed_serverr   r#   r   r   s         r   r   /KubeconfigConnectionContext._LoadClusterDetailsA  s    oo..t/N/NOdm__--dmmI.F/8/: ;dlll9-112I4Pdm]]||I.223O379 ''Y(?(IJm'00d
 
		%**005;//''i(@(HIdi		&)--lDAdo!d"d__#yy0445FM6*../CTJd $		& 1 5 56O6:!<
 
	 	 	=1E1EF* 0 0$2G2GHKm"o$			 77$8$
!$($5$5$
! 	I 	G 
>	=  !!"C"J"J
&&)#  
 	IH 	GF 
>	=sr   M8HL &M8'M'+'MAM(M0M'8	M8
M2L==MM8
MM
M$	 M''
M51M8)r#   r   r   r   r   r   r   r   r   r"   r   r   r   r   r.   )rW   rX   rY   rZ   r[   r*   r   r   r:   rJ   r\   r>   rG   r   r   rD   rA   r   r   r`   r   r   s   @r   r   r      s    M#"  ,* " " B B
             6 6r,   r   c                     [         R                   " U 5      u  p#pEpgSR                  X5      n[         R                  " X#XEXg45      $ )Nz{}-{})r   rk   
urlunparse)r"   r$   schemenetlocr   paramsqueryfragments           r   DeriveRegionalEndpointr  {  s?    2:2C2CH2M/&$>>&)&			fdEL	MMr,   c                      ^  \ rS rSrSrU 4S jr\S 5       r\S 5       r\S 5       r	\S 5       r
\R                  S 5       rS	 rS
rU =r$ )MultiRegionConnectionContexti  zAContext manager to connect to the multi-region endpoint (global).c                 F   > [         [        U ]  X5        SU l        X0l        g )N-)rx   r  r*   r$   region_list)r(   r   r)   r
  rz   s       r   r*   %MultiRegionConnectionContext.__init__  s     	
&6xIDK"r,   c                     gNprojectr9   r/   s    r   rA   %MultiRegionConnectionContext.ns_label      r,   c                     gNz	Cloud Runr9   r/   s    r   r>   %MultiRegionConnectionContext.operator  r   r,   c                 8    SR                  U R                  5      $ )Nz& regions [{{{{bold}}}}{}{{{{reset}}}}])rk   r
  r/   s    r   rG   +MultiRegionConnectionContext.location_label  s    3::4;K;KLLr,   c                     grM   r9   r/   s    r   rD   2MultiRegionConnectionContext.supports_one_platform  r   r,   c              #      #    [         R                  " U R                  U R                  5      U l        [        U R                  U R                  5         U v   S S S 5        g ! , (       d  f       g = f7fr.   )r   GetEffectiveApiEndpointr&   r'   r"   r   r/   s    r   r:   $MultiRegionConnectionContext.Connect  sD     00ODM	#DNNDMM	Bj 
C	B	Bs   AA2A!	A2!
A/+A2c                 d    XR                   :X  a  U $ [        XR                  U R                  5      $ r.   r$   RegionalConnectionContextr&   r'   r(   r$   s     r   GetContextWithRegionOverride9MultiRegionConnectionContext.GetContextWithRegionOverride  &    k&v~~t}}MMr,   )r"   r$   r
  )rW   rX   rY   rZ   r[   r*   r\   rA   r>   rG   rD   r   r   r:   r  r`   r   r   s   @r   r  r    s    I#     M M    
N Nr,   r  c                      ^  \ rS rSrSrU 4S jr\S 5       r\S 5       r\S 5       r	\
R                  S 5       r\S 5       rS	 rS
rU =r$ )r  i  z9Context manager to connect a particular Cloud Run region.c                 8   > [         [        U ]  X#5        Xl        g r.   )rx   r  r*   r$   )r(   r$   r   r)   rz   s       r   r*   "RegionalConnectionContext.__init__  s    	
#T3HFKr,   c                     gr  r9   r/   s    r   rA   "RegionalConnectionContext.ns_label  r  r,   c                     gr  r9   r/   s    r   r>   "RegionalConnectionContext.operator  r   r,   c                 8    SR                  U R                  5      $ )Nz% region [{{{{bold}}}}{}{{{{reset}}}}])rk   r$   r/   s    r   rG   (RegionalConnectionContext.location_label  s    299$++FFr,   c              #     #    [         R                  " U R                  U R                  5      n[	        XR
                  5      U l        [        U R                  U R                  5         U v   S S S 5        g ! , (       d  f       g = f7fr.   )r   r  r&   r'   r  r$   r"   r   )r(   global_endpoints     r   r:   !RegionalConnectionContext.Connect  sV     224>>37==BO*?KKHDM	#DNNDMM	Bj 
C	B	Bs   A&B(A6-	B6
B Bc                     grM   r9   r/   s    r   rD   /RegionalConnectionContext.supports_one_platform  r   r,   c                 d    XR                   :X  a  U $ [        XR                  U R                  5      $ r.   r  r  s     r   r  6RegionalConnectionContext.GetContextWithRegionOverride  r!  r,   )r"   r$   )rW   rX   rY   rZ   r[   r*   r\   rA   r>   rG   r   r   r:   rD   r  r`   r   r   s   @r   r  r    s    A     G G    N Nr,   r  Fc                 L   U [         R                  R                  :X  a  [        R                  $ U [         R                  R
                  :X  a7  U(       a  [        $ U[        R                  R                  :X  a  [        $ [        $ [        S[        R                  " U 5      -   5      e)z=Returns the api name to use depending on the current context.Unrecognized product: )r   ProductRUNr   SERVERLESS_API_NAMEEVENTS_CLUSTER_EVENTS_API_NAMEr   ReleaseTrackALPHA_MANAGED_EVENTS_ALPHA_API_NAME_MANAGED_EVENTS_API_NAMEr   r   u)productrelease_track
is_clusters      r   _GetApiNamerA    st    !!!---%--&&&%%	$++11	1++%%
-g>
??r,   c                 V   Ub  U$ U [         R                  R                  :X  a  [        R                  $ U [         R                  R
                  :X  a7  U(       a  [        $ U[        R                  R                  :X  a  [        $ [        $ [        S[        R                  " U 5      -   5      e)z@Returns the api version to use depending on the current context.r3  )r   r4  r5  r   SERVERLESS_API_VERSIONr7  _CLUSTER_EVENTS_API_VERSIONr   r9  r:  !_MANAGED_EVENTS_ALPHA_API_VERSION_MANAGED_EVENTS_API_VERSIONr   r   r=  )r>  r?  r@  version_overrides       r   _GetApiVersionrH    s    
 !!!!000%--&&&((	$++11	1..((
-g>
??r,   c                    Uc  [         R                  " 5       nU[         R                  :X  aN  [        R                  " [        U SS5      5      n[        XSS9n[        UUSUS9n	[        XxU	U R                  5      $ U[         R                  :X  ac  U R                  R                  R                  5       n
U
(       d  [        R                  " S5      e[        XSS9n[        UUSUS9n	[!        XU	5      $ U[         R"                  :X  a}  [        X5      n[        XUS9n	U(       d?  [        R$                  " U SUS9nU(       d  [        R                  " S	5      e['        XU	5      $ [        R(                  " U 5      n[+        XU5      $ g)
a  Gets the regional, kubeconfig, or GKE connection context.

Args:
  args: Namespace, the args namespace.
  product: Which product is requesting connection context.
  release_track: Release track of the command being run.
  version_override: If specified, the given api version will be used no matter
    the other parameters.
  platform: 'gke', 'kubernetes', or 'managed'. If not specified, the value of
    the --platform flag will be used instead.
  region_label: A k8s label representing the intended region.
  is_multiregion: Whether we will use the managed Multi-region API.

Raises:
  ArgumentError if region or cluster is not specified.

Returns:
  A GKE or regional ConnectionInfo object.
Nr   T)r@  )r@  rG  zYou must specify a cluster in a given location. Either use the `--cluster` and `--cluster-location` flags or set the run/cluster and run/cluster_location properties.)rG  )promptregion_labelzYYou must specify a region. Either use the `--region` flag or set the run/region property.)r	   GetPlatformPLATFORM_KUBERNETESr   GetKubeconfigr   rA  rH  r   r   PLATFORM_GKECONCEPTSr   Parseri   ArgumentErrorru   PLATFORM_MANAGED	GetRegionr  GetMultiRegionr  )r   r>  r?  rG  platformrK  is_multiregionr   r   r2   ry   r$   r
  s                r   GetConnectionContextrX    s~   8 $$&H...$$WT<%FGJ7dCH )	+K
 'z['+||5 5 '''--''--/K!//HI I 7dCH )	+K
  {CC+++72H 1ACKtD|Lf#11.
 	
 'vEE((.k)(MM ,r,   )F)FN)8r[   r]   r   r   r   re   rg   rm   r   googlecloudsdk.api_lib.runr   r   googlecloudsdk.api_lib.utilr   googlecloudsdk.callioper   googlecloudsdk.command_lib.runr   ri   r   r	   r   r
   r   googlecloudsdk.core.utilr   r   r   six.moves.urllibr   r   r8  rD  r<  rF  r6  r;  rE  r   r   with_metaclassABCMetar   rs   ru   r   r  r  r  rA  rH  r4  r5  r9  GArX  r9   r,   r   <module>rb     s7   3    	 	 
 
  * 5 , ( N 0 4 * ) *  
 .) ' # ' !/!C!C $. ! ( ((B4S''4 B4J;,4> 4n`. `FN#N> #NL#N #NL@" $$(@. MM##&&FNr,   