
    K                        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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'\RP                  r)Sr*\RV                  S 5       r, " S S\ RZ                  " \R\                  5      5      r/S r0 " S S\/5      r1 " S S\/5      r2S r3 " S S \/5      r4 " S! S"\/5      r5S'S$ jr6  S(S% jr7\Rp                  Rr                  \Rt                  Rv                  SSSS#4S& jr<g))z,Dynamic context for connection to Cloud Run.    )absolute_import)division)print_function)unicode_literalsN)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!   5   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)ConnectionInfoJ   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__M   s*     DMDMDKDHNM    c                     U R                   $ N)r*   r,   s    r    r   ConnectionInfo.api_name[   s    >>r0   c                     U R                   $ r2   )r+   r3   s    r    api_versionConnectionInfo.api_version_   s    ==r0   c                     U R                   $ r2   )_activer3   s    r    activeConnectionInfo.activec   s    <<r0   c                     g r2    r3   s    r    ConnectConnectionInfo.Connectg       r0   c                     g r2   r=   r3   s    r    operatorConnectionInfo.operatork   r@   r0   c                     g r2   r=   r3   s    r    ns_labelConnectionInfo.ns_labelo   r@   r0   c                     g r2   r=   r3   s    r    supports_one_platform$ConnectionInfo.supports_one_platforms   r@   r0   c                     g r2   r=   r3   s    r    location_labelConnectionInfo.location_labelw   r@   r0   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.
Nr=   r3   s    r    
HttpClientConnectionInfo.HttpClient{   s     r0   c                 n    SU l         U R                  5       U l        U R                  R                  5       $ NT)r9   r>   r)   	__enter__r3   s    r    rR   ConnectionInfo.__enter__   s)    DL||~DH88r0   c                 H    SU l         U R                  R                  XU5      $ NF)r9   r)   __exit__)r,   typvalue	tracebacks       r    rV   ConnectionInfo.__exit__   s     DL88S33r0   )r9   r*   r)   r+   r'   r&   r(   N)__name__
__module____qualname____firstlineno____doc__r.   propertyr   r6   r:   abcabstractmethodr>   abstractpropertyrB   rE   rH   rK   rN   rR   rV   __static_attributes__r=   r0   r    r#   r#   J   s    9       	 	 	 	 	 	 	 	 	 	 
4r0   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    _CheckTLSSupportrw      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
&r0   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 r2   )superry   r.   cluster_ref)r,   r}   r   r-   	__class__s       r    r.   GKEConnectionContext.__init__   s    	
.xA"r0   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://{}/)
rw   r   ClusterConnectionInfor}   r'   MonkeypatchAddressCheckingro   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 Anthosr=   r3   s    r    rB   GKEConnectionContext.operator       !r0   c                     U R                   (       d   eSSKJn  UR                  [        R
                  U R                  S9nU$ )Nr   
transportsresponse_encodingr'   )r:   googlecloudsdk.core.credentialsr   GetApitoolsTransportr   ENCODINGr'   r,   r   http_clients      r    rN   GKEConnectionContext.HttpClient   s>     ;;;:11#,,t}} 2 FKr0   c                 8    SR                  U R                  5      $ )Nz) of cluster [{{{{bold}}}}{}{{{{reset}}}}])ro   cluster_namer3   s    r    rK   #GKEConnectionContext.location_label   s    6==d>O>OPPr0   c                 6    U R                   R                  5       $ r2   )r}   Namer3   s    r    r   !GKEConnectionContext.cluster_name   s      ""r0   c                 .    U R                   R                  $ r2   )r}   zoner3   s    r    cluster_location%GKEConnectionContext.cluster_location   s       r0   c                 .    U R                   R                  $ r2   )r}   	projectIdr3   s    r    cluster_project$GKEConnectionContext.cluster_project   s    %%%r0   c                     grU   r=   r3   s    r    rH   *GKEConnectionContext.supports_one_platform       r0   c                     gN	namespacer=   r3   s    r    rE   GKEConnectionContext.ns_label       r0   )r'   r}   r&   )r[   r\   r]   r^   r_   r.   
contextlibcontextmanagerr>   r`   rB   rN   rK   r   r   r   rH   rE   rd   __classcell__r~   s   @r    ry   ry      s    ?#   " " Q Q # # ! ! & &    r0   ry   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)
r|   r   r.   
kubeconfigSetCurrentContextcurrent_contextclient_cert_dataclient_cert
client_keyclient_cert_domain)r,   r   r   r-   contextr~   s        r    r.   $KubeconfigConnectionContext.__init__   sV     

%t5hH OOO%%g&K1K1KL DDDO"Dr0   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.)rw   _LoadClusterDetailsca_datar   r   raw_hostnamero   raw_pathr&   r!   r*   rk   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                  (       aQ  SSKJn  UR                  [        R                  U 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   r   )r:   r   r   r   
ValueErrorgooglecloudsdk.corer   r   r   r   r'   r   r   s      r    rN   &KubeconfigConnectionContext.HttpClient  s    ;;;??t//D4K4K#$ $  133%..==!--__!44 4 6k :11#,,t}} 2 FKr0   c                     gr   r=   r3   s    r    rB   $KubeconfigConnectionContext.operator  r   r0   c                 h    SR                  U R                  S   U R                  R                  5      $ )Nza of context [{{{{bold}}}}{}{{{{reset}}}}] referenced by config file [{{{{bold}}}}{}{{{{reset}}}}]name)ro   curr_ctxr   filenamer3   s    r    rK   *KubeconfigConnectionContext.location_label!  s2    GGMvf%t'?'?HABr0   c                      U R                   S   $ )Nr   )clusterr3   s    r    r   (KubeconfigConnectionContext.cluster_name'  s    <<r0   c                     g r2   r=   r3   s    r    r   ,KubeconfigConnectionContext.cluster_location+      r0   c                     grU   r=   r3   s    r    rH   1KubeconfigConnectionContext.supports_one_platform/  r   r0   c                     gr   r=   r3   s    r    rE   $KubeconfigConnectionContext.ns_label3  r   r0   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._WriteDataIfNoFile7  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   KubeconfigErrorro   argsr   r   PY2r   )r,   parsed_serverr   r'   r   r   s         r    r   /KubeconfigConnectionContext._LoadClusterDetailsF  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   r2   )r[   r\   r]   r^   r_   r.   r   r   r>   rN   r`   rB   rK   r   r   rH   rE   r   r   rd   r   r   s   @r    r   r      s    M#"  ,, " " B B
             6 6r0   r   c                     [         R                   " U 5      u  p#pEpgSR                  X5      n[         R                  " X#XEXg45      $ )Nz{}-{})r   ro   
urlunparse)r&   r(   schemenetlocr   paramsqueryfragments           r    DeriveRegionalEndpointr	    s?    2:2C2CH2M/&$>>&)&			fdEL	MMr0   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-)r|   r  r.   r(   region_list)r,   r   r-   r  r~   s       r    r.   %MultiRegionConnectionContext.__init__  s     	
&6xIDK"r0   c                     gNprojectr=   r3   s    r    rE   %MultiRegionConnectionContext.ns_label      r0   c                     gNz	Cloud Runr=   r3   s    r    rB   %MultiRegionConnectionContext.operator  r   r0   c                 8    SR                  U R                  5      $ )Nz& regions [{{{{bold}}}}{}{{{{reset}}}}])ro   r  r3   s    r    rK   +MultiRegionConnectionContext.location_label  s    3::4;K;KLLr0   c                     grQ   r=   r3   s    r    rH   2MultiRegionConnectionContext.supports_one_platform  r   r0   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r2   )r	   GetEffectiveApiEndpointr*   r+   r&   r!   r3   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      $ r2   r(   RegionalConnectionContextr*   r+   r,   r(   s     r    GetContextWithRegionOverride9MultiRegionConnectionContext.GetContextWithRegionOverride  &    k&v~~t}}MMr0   )r&   r(   r  )r[   r\   r]   r^   r_   r.   r`   rE   rB   rK   rH   r   r   r>   r#  rd   r   r   s   @r    r  r    s    I#     M M    
N Nr0   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 r2   )r|   r!  r.   r(   )r,   r(   r   r-   r~   s       r    r.   "RegionalConnectionContext.__init__  s    	
#T3HFKr0   c                     gr  r=   r3   s    r    rE   "RegionalConnectionContext.ns_label  r  r0   c                     gr  r=   r3   s    r    rB   "RegionalConnectionContext.operator  r   r0   c                 8    SR                  U R                  5      $ )Nz% region [{{{{bold}}}}{}{{{{reset}}}}])ro   r(   r3   s    r    rK   (RegionalConnectionContext.location_label  s    299$++FFr0   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r2   )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rQ   r=   r3   s    r    rH   /RegionalConnectionContext.supports_one_platform  r   r0   c                 d    XR                   :X  a  U $ [        XR                  U R                  5      $ r2   r   r"  s     r    r#  6RegionalConnectionContext.GetContextWithRegionOverride  r%  r0   )r&   r(   )r[   r\   r]   r^   r_   r.   r`   rE   rB   rK   r   r   r>   rH   r#  rd   r   r   s   @r    r!  r!    s    A     G G    N Nr0   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    _GetApiNamerE    st    !!!---%--&&&%%	$++11	1++%%
-g>
??r0   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.r7  )r   r8  r9  r   SERVERLESS_API_VERSIONr;  _CLUSTER_EVENTS_API_VERSIONr
   r=  r>  !_MANAGED_EVENTS_ALPHA_API_VERSION_MANAGED_EVENTS_API_VERSIONr   r   rA  )rB  rC  rD  version_overrides       r    _GetApiVersionrL    s    
 !!!!000%--&&&((	$++11	1..((
-g>
??r0   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)rD  )rD  rK  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.)rK  )promptregion_labelzYYou must specify a region. Either use the `--region` flag or set the run/region property.)r   GetPlatformPLATFORM_KUBERNETESr   GetKubeconfigr   rE  rL  r   r   PLATFORM_GKECONCEPTSr   Parserm   ArgumentErrorry   PLATFORM_MANAGED	GetRegionr!  GetMultiRegionr  )r   rB  rC  rK  platformrO  is_multiregionr   r   r6   r}   r(   r  s                r    GetConnectionContextr\    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 ,r0   )F)FN)=r_   
__future__r   r   r   r   ra   r   r   r   ri   rk   rq   r   googlecloudsdk.api_lib.runr   r   googlecloudsdk.api_lib.utilr	   googlecloudsdk.callioper
   googlecloudsdk.command_lib.runr   rm   r   r   r   r   r   googlecloudsdk.core.utilr   r   r   six.moves.urllibr   r   r<  rH  r@  rJ  r:  r?  rI  r   r!   with_metaclassABCMetar#   rw   ry   r   r	  r  r!  rE  rL  r8  r9  r=  GAr\  r=   r0   r    <module>rg     sA   3 &  % ' 
   	 	 
 
  * 5 , ( N 0 4 * ) *  
 .) ' # ' !/!C!C $. ! ( ((B4S''4 B4J;,4> 4na. aHN#N> #NL#N #NL@" $$(@. MM##&&FNr0   