
    K                        d Z ddlmZ ddlmZ ddlmZ ddlmZ ddlZddlZddlZddl	Z	ddl
Z
ddlZddlZddlZddlmZ ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlZddl Z ddl!m"Z# dZ$dZ%dZ&dZ'ejP                  Z)dZ*ejV                  d        Z, G d d e jZ                  ej\                              Z/d Z0 G d de/      Z1 G d de/      Z2d Z3 G d d e/      Z4 G d! d"e/      Z5d'd$Z6	 	 d(d%Z7ejp                  jr                  ejt                  jv                  dddd#fd&Z<y))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              #      K   t        t        j                  j                  |       }|j	                         }	 |j                  |       d |j                  |       y# |j                  |       w xY ww)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   se      j//FF&("&&(,((#	,','s   5A5A A5A22A5c                      e Zd ZdZd Zed        Zed        Zed        Ze	j                  d        Ze	j                  d        Ze	j                  d        Ze	j                  d	        Ze	j                  d
        Zd Zd Zd Zy)ConnectionInfoz1Information useful in constructing an API client.c                 X    d| _         d| _        d| _        d| _        || _        || _        y)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__zConnectionInfo.__init__M   s.     DMDMDKDHDNDM    c                     | j                   S N)r)   r+   s    r    r   zConnectionInfo.api_name[   s    >>r.   c                     | j                   S r0   )r*   r1   s    r    api_versionzConnectionInfo.api_version_   s    ==r.   c                     | j                   S r0   )_activer1   s    r    activezConnectionInfo.activec   s    <<r.   c                      y r0    r1   s    r    ConnectzConnectionInfo.Connectg       r.   c                      y r0   r8   r1   s    r    operatorzConnectionInfo.operatork   r:   r.   c                      y r0   r8   r1   s    r    ns_labelzConnectionInfo.ns_labelo   r:   r.   c                      y r0   r8   r1   s    r    supports_one_platformz$ConnectionInfo.supports_one_platforms   r:   r.   c                      y r0   r8   r1   s    r    location_labelzConnectionInfo.location_labelw   r:   r.   c                      y)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.
    Nr8   r1   s    r    
HttpClientzConnectionInfo.HttpClient{   s     r.   c                 n    d| _         | j                         | _        | j                  j                         S NT)r5   r9   r(   	__enter__r1   s    r    rG   zConnectionInfo.__enter__   s)    DL||~DH88r.   c                 J    d| _         | j                  j                  |||      S NF)r5   r(   __exit__)r+   typvalue	tracebacks       r    rJ   zConnectionInfo.__exit__   s"    DL88S%33r.   N)__name__
__module____qualname____doc__r-   propertyr   r3   r6   abcabstractmethodr9   abstractpropertyr<   r>   r@   rB   rD   rG   rJ   r8   r.   r    r#   r#   J   s    9       	 	 	 	 	 	 	 	 	 	 
4r.   r#   c                     t        j                  dt        j                        r2t	        j
                  dj                  t        j                              t        t        d      st        t        d      st        j                  j                  dk(  rdnd} t	        j
                  dj                  t        j                  j                  t        j                  j                  t        j                  j                  |             y	y	)
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    _CheckTLSSupportrh      s    XXnc112

*
*	 vc112	4 4 #~
&'#7I*J'*'7'7'='='BG

*
*	228&""C$4$4$:$:""$83:; ; +K
&r.   c                        e Zd ZdZ fdZej                  d        Zed        Z	d Z
ed        Zed        Zed        Zed	        Zed
        Zed        Z xZS )GKEConnectionContextz7Context manager to connect to the GKE Cloud Run add-in.c                 <    t         t        |   ||       || _        y r0   )superrj   r-   cluster_ref)r+   rm   r   r,   	__class__s       r    r-   zGKEConnectionContext.__init__   s    	
.xA"Dr.   c              #     K   t                t        j                  | j                        5 \  }}|| _        t        j
                  d|      5 }dj                  |      | _        t        | j                  | j                        5  |  d d d        d d d        d d d        y # 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   y xY ww)Nkubernetes.defaultzhttps://{}/)
rh   r   ClusterConnectionInform   r&   MonkeypatchAddressCheckingr`   r%   r!   r)   )r+   ipr&   r%   s       r    r9   zGKEConnectionContext.Connect   s     		"	"4#3#3	4Xdm))*>Cx%,,X6'F* G D 
5	4 GF DC 
5	4sL   *C!B:7B.B"	B.B:	C"B+'B..B7	3B::C?Cc                      yNzCloud Run for Anthosr8   r1   s    r    r<   zGKEConnectionContext.operator       !r.   c                     | j                   sJ ddlm} |j                  t        j
                  | j                        }|S )Nr   
transportsresponse_encodingr&   )r6   googlecloudsdk.core.credentialsry   GetApitoolsTransportr   ENCODINGr&   r+   ry   http_clients      r    rD   zGKEConnectionContext.HttpClient   s>     ;;;:11#,,t}} 2 FKr.   c                 8    dj                  | j                        S )Nz) of cluster [{{{{bold}}}}{}{{{{reset}}}}])r`   cluster_namer1   s    r    rB   z#GKEConnectionContext.location_label   s    6==d>O>OPPr.   c                 6    | j                   j                         S r0   )rm   Namer1   s    r    r   z!GKEConnectionContext.cluster_name   s      ""r.   c                 .    | j                   j                  S r0   )rm   zoner1   s    r    cluster_locationz%GKEConnectionContext.cluster_location   s       r.   c                 .    | j                   j                  S r0   )rm   	projectIdr1   s    r    cluster_projectz$GKEConnectionContext.cluster_project   s    %%%r.   c                      yrI   r8   r1   s    r    r@   z*GKEConnectionContext.supports_one_platform       r.   c                      yN	namespacer8   r1   s    r    r>   zGKEConnectionContext.ns_label       r.   )rN   rO   rP   rQ   r-   
contextlibcontextmanagerr9   rR   r<   rD   rB   r   r   r   r@   r>   __classcell__rn   s   @r    rj   rj      s    ?#   " " Q Q # # ! ! & &    r.   rj   c                        e Zd ZdZd fd	Zej                  d        Zd Ze	d        Z
e	d        Ze	d        Ze	d        Ze	d	        Ze	d
        Zej                  d        Zej                  d        Z xZS )KubeconfigConnectionContextzEContext manager to connect to a cluster defined in a Kubeconfig file.c                     t         t        |   ||       || _        | j                  j	                  |xs |j
                         d| _        d| _        d| _        d| _	        y)a4  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)
rl   r   r-   
kubeconfigSetCurrentContextcurrent_contextclient_cert_dataclient_cert
client_keyclient_cert_domain)r+   r   r   r,   contextrn   s        r    r-   z$KubeconfigConnectionContext.__init__   sZ     

%t5hH DOOO%%g&K1K1KL DDDO"Dr.   c              #     K   t                | j                         5  	 | j                  rKt        j                  d| j
                        5 }dj                  || j                        | _        d d d        n+dj                  | j
                  | j                        | _        t        | j                  | j                        5  |  d d d        d d d        y # 1 sw Y   ?xY w# 1 sw Y   xY w# t        j                  t        j                  j                  f$ r2}dt        j                   |      v rt        j"                  d       d }~ww xY w# 1 sw Y   y xY ww)Nrp   zhttps://{}/{}CERTIFICATE_VERIFY_FAILEDzdMissing or invalid [certificate-authority] or [certificate-authority-data] field in kubeconfig file.)rh   _LoadClusterDetailsca_datar   rr   raw_hostnamer`   raw_pathr%   r!   r)   r\   SSLErrorrequestsr   six	text_typeNoCaCertError)r+   r%   es      r    r9   z#KubeconfigConnectionContext.Connect   s%    		!	!	#<<--.B.2.?.?ADL+228T]]KDMA A *001B1B15@$-'F* G 
$	#A A GF llH//889 &#--*::!!GH H  
$	#sj   EE,C/"C-AC/C#C/	EC 	C/#C,	(C//-E-E		EEEEc                    | j                   sJ | j                  s#| j                  r| j                  rt	        d      | j                  rTddlm} |j                  t        j                  | j                  | j                  | j                  | j                        }|S ddlm} |j                  t        j                  | j                        }|S )NzNKubeconfig authentication requires a client certificate authentication method.r   rx   )r{   r&   client_certificater   r   rz   )r6   r   r   r   
ValueErrorgooglecloudsdk.corery   r}   r   r~   r&   r|   r   s      r    rD   z&KubeconfigConnectionContext.HttpClient  s    ;;;??t//D4K4K#$ $  133%..==!--__!44 4 6k :11#,,t}} 2 FKr.   c                      yru   r8   r1   s    r    r<   z$KubeconfigConnectionContext.operator  rv   r.   c                 h    dj                  | j                  d   | j                  j                        S )Nza of context [{{{{bold}}}}{}{{{{reset}}}}] referenced by config file [{{{{bold}}}}{}{{{{reset}}}}]name)r`   curr_ctxr   filenamer1   s    r    rB   z*KubeconfigConnectionContext.location_label!  s2    GGMvf%t'?'?HABr.   c                      | j                   d   S )Nr   )clusterr1   s    r    r   z(KubeconfigConnectionContext.cluster_name'  s    <<r.   c                      y r0   r8   r1   s    r    r   z,KubeconfigConnectionContext.cluster_location+      r.   c                      yrI   r8   r1   s    r    r@   z1KubeconfigConnectionContext.supports_one_platform/  r   r.   c                      yr   r8   r1   s    r    r>   z$KubeconfigConnectionContext.ns_label3  r   r.   c              #   >  K   |r| y |rrt        j                         \  }}t        j                  |       	 t	        j
                  |t        j                  |      d       | t        j                  |       y d  y # t        j                  |       w xY ww)NT)private)	tempfilemkstemposcloser   WriteBinaryFileContentsbase64	b64decoderemove)r+   fdfds       r    _WriteDataIfNoFilez.KubeconfigConnectionContext._WriteDataIfNoFile7  ss     g	
 eb!hhrl%%a)9)9!)<dK
		!j 			!s   6B/B (BBBc              #     K   	 | j                   j                  | j                   j                     | _        | j                   j                  | j                  d   d      | _        | j
                  d   j                  dd      | _        | j                  s$| j
                  d   j                  dd      | _        t        j                  | j
                  d   d         }|j                  | _        |j                  r$|j                  j                  d      dz   | _        nd| _        | j                   j                  | j                  d   d	      | _        | j                   d	   j                  d
d      | _        d| _        d| _        | j"                  s$| j                   d	   j                  dd      | _        | j                   d	   j                  dd      | _        | j(                  s$| j                   d	   j                  dd      | _        | j5                  | j                  | j                        5 }| j5                  | j"                  | j$                        5 }| j5                  | j(                  | j&                        5 }|| _        || _        || _        | j(                  r)t6        j8                  rd| _        n| j                  | _        d ddd       ddd       ddd       y# t*        $ r6}t-        j.                  dj1                  |j2                  d               d}~ww xY w# 1 sw Y   \xY w# 1 sw Y   `xY w# 1 sw Y   yxY ww)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   rp   )r   contextsr   r   clustersr   getr&   r   urlparsehostnamer   pathstripr   usersr   r   client_key_datar   r   KeyErrorr   KubeconfigErrorr`   argsr   r   PY2r   )r+   parsed_serverr   r&   r   r   s         r    r   z/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dm"do$d			 77$8$
!$($5$5$
! 	I 	G 
>	=  !!"C"J"J
&&)#  
 	IH 	GF 
>	=sr   MHK% 	&M/'L?'L3=AL'L3L?	M%	L$.1LL$$M'L0,L33L<	8L??MMr0   )rN   rO   rP   rQ   r-   r   r   r9   rD   rR   r<   rB   r   r   r@   r>   r   r   r   r   s   @r    r   r      s    M#"  ,, " " B B
             6 6r.   r   c                     t        j                   |       \  }}}}}}dj                  ||      }t        j                  ||||||f      S )Nz{}-{})r   r`   
urlunparse)r%   r'   schemenetlocr   paramsqueryfragments           r    DeriveRegionalEndpointr     sM    2:2C2CH2M/&&$x>>&&)&			ffdFE8L	MMr.   c                        e Zd ZdZ fdZed        Zed        Zed        Zed        Z	e
j                  d        Zd Z xZS )	MultiRegionConnectionContextzAContext manager to connect to the multi-region endpoint (global).c                 J    t         t        |   ||       d| _        || _        y )N-)rl   r   r-   r'   region_list)r+   r   r,   r   rn   s       r    r-   z%MultiRegionConnectionContext.__init__  s$    	
&6xIDK"Dr.   c                      yNprojectr8   r1   s    r    r>   z%MultiRegionConnectionContext.ns_label      r.   c                      yNz	Cloud Runr8   r1   s    r    r<   z%MultiRegionConnectionContext.operator  r   r.   c                 8    dj                  | j                        S )Nz& regions [{{{{bold}}}}{}{{{{reset}}}}])r`   r   r1   s    r    rB   z+MultiRegionConnectionContext.location_label  s    3::4;K;KLLr.   c                      yrF   r8   r1   s    r    r@   z2MultiRegionConnectionContext.supports_one_platform  r   r.   c              #      K   t        j                  | j                  | j                        | _        t        | j                  | j                        5  |  d d d        y # 1 sw Y   y xY wwr0   )r	   GetEffectiveApiEndpointr)   r*   r%   r!   r1   s    r    r9   z$MultiRegionConnectionContext.Connect  sE     00ODM	#DNNDMM	Bj 
C	B	Bs   AA,A 	A, A)%A,c                 f    || j                   k(  r| S t        || j                  | j                        S r0   r'   RegionalConnectionContextr)   r*   r+   r'   s     r    GetContextWithRegionOverridez9MultiRegionConnectionContext.GetContextWithRegionOverride  *    k&vt~~t}}MMr.   )rN   rO   rP   rQ   r-   rR   r>   r<   rB   r@   r   r   r9   r   r   r   s   @r    r   r     s}    I#     M M    
Nr.   r   c                        e Zd ZdZ fdZed        Zed        Zed        Ze	j                  d        Zed        Zd Z xZS )	r   z9Context manager to connect a particular Cloud Run region.c                 <    t         t        |   ||       || _        y r0   )rl   r   r-   r'   )r+   r'   r   r,   rn   s       r    r-   z"RegionalConnectionContext.__init__  s    	
#T3HgFDKr.   c                      yr   r8   r1   s    r    r>   z"RegionalConnectionContext.ns_label  r   r.   c                      yr   r8   r1   s    r    r<   z"RegionalConnectionContext.operator  r   r.   c                 8    dj                  | j                        S )Nz% region [{{{{bold}}}}{}{{{{reset}}}}])r`   r'   r1   s    r    rB   z(RegionalConnectionContext.location_label  s    299$++FFr.   c              #     K   t        j                  | j                  | j                        }t	        || j
                        | _        t        | j                  | j                        5  |  d d d        y # 1 sw Y   y xY wwr0   )r	   r   r)   r*   r   r'   r%   r!   )r+   global_endpoints     r    r9   z!RegionalConnectionContext.Connect  sY     224>>37==BO*?DKKHDM	#DNNDMM	Bj 
C	B	Bs   A&B(A6-	B6A?;Bc                      yrF   r8   r1   s    r    r@   z/RegionalConnectionContext.supports_one_platform  r   r.   c                 f    || j                   k(  r| S t        || j                  | j                        S r0   r   r   s     r    r   z6RegionalConnectionContext.GetContextWithRegionOverride  r   r.   )rN   rO   rP   rQ   r-   rR   r>   r<   rB   r   r   r9   r@   r   r   r   s   @r    r   r     s}    A     G G    Nr.   r   Fc                 :   | t         j                  j                  k(  rt        j                  S | t         j                  j
                  k(  r1|rt        S |t        j                  j                  k(  rt        S t        S t        dt        j                  |       z         )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    _GetApiNamer    st    !!!---%--&&&%%	$++11	1++%%
-g>
??r.   c                 B   ||S | t         j                  j                  k(  rt        j                  S | t         j                  j
                  k(  r1|rt        S |t        j                  j                  k(  rt        S t        S t        dt        j                  |       z         )z@Returns the api version to use depending on the current context.r  )r   r  r  r   SERVERLESS_API_VERSIONr  _CLUSTER_EVENTS_API_VERSIONr
   r	  r
  !_MANAGED_EVENTS_ALPHA_API_VERSION_MANAGED_EVENTS_API_VERSIONr   r   r  )r  r  r  version_overrides       r    _GetApiVersionr    s    
 !!!!000%--&&&((	$++11	1..((
-g>
??r.   c                 
   |t        j                         }|t         j                  k(  rUt        j                  t        | dd            }t        ||d      }t        ||d|      }	t        |||	| j                        S |t         j                  k(  re| j                  j                  j                         }
|
st        j                  d      t        ||d      }t        ||d|      }	t!        |
||	      S |t         j"                  k(  rzt        ||      }t        |||      }	|s<t        j$                  | d|      }|st        j                  d	      t'        |||	      S t        j(                  |       }t+        ||	|      S y)
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  r  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.)r  )promptregion_labelzYYou must specify a region. Either use the `--region` flag or set the run/region property.)r   GetPlatformPLATFORM_KUBERNETESr   GetKubeconfigr   r  r  r   r   PLATFORM_GKECONCEPTSr   Parser^   ArgumentErrorrj   PLATFORM_MANAGED	GetRegionr   GetMultiRegionr   )r   r  r  r  platformr  is_multiregionr   r   r3   rm   r'   r   s                r    GetConnectionContextr(    s   8 $$&H...$$WT<%FGJ7MdCH )	+K
 'z8['+||5 5 '''--''--/K!//HI I 7MdCH )	+K
  X{CC+++7M2H 1ACKtD|Lf#11.
 	
 'vxEE((.k)(KMM ,r.   )F)FN)=rQ   
__future__r   r   r   r   rS   r   r   r   rZ   r\   rb   r   googlecloudsdk.api_lib.runr   r   googlecloudsdk.api_lib.utilr	   googlecloudsdk.callioper
   googlecloudsdk.command_lib.runr   r^   r   r   r   r   r   googlecloudsdk.core.utilr   r   r   six.moves.urllibr   r   r  r  r  r  r  r  r  r   r!   with_metaclassABCMetar#   rh   rj   r   r   r   r   r  r  r  r  r	  GAr(  r8   r.   r    <module>r3     sD   3 &  % ' 
   	 	 
 
  * 5 , ( N 0 4 * ) *  
 .) ' # ' !/!C!C $. ! ( ((B4'S''4 B4J;,4> 4na. aHN#N> #NL#N #NL@" $$(@. MM##&&FNr.   