
    +                        d Z ddlmZ ddlmZ ddlm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mZ  ej.                  dg d      ZdZd Zej6                   ej8                  ej:                  j<                         G d dej>                                      Z ej6                   ej8                  ej:                  jB                         G d de                     Z"ej6                   ej8                  ej:                  jF                         G d de"                    Z$ e       e$_%         e       e"_%         e       e _%        y)z<Implements the command for starting a tunnel with Cloud IAP.    )absolute_import)division)unicode_literalsN)base_classes)iap_tunnel_websocket)arg_parsers)base)
exceptions)
iap_tunnel)scope)	ssh_utils)flags)log)
properties_TargetArgs
projectzoneinstance	interfaceportregionnetworkhost
dest_groupsecurity_gatewayz

To increase the performance of the tunnel, consider installing NumPy. For instructions,
please see https://cloud.google.com/iap/docs/using-tcp-forwarding#increasing_the_tcp_upload_bandwidth
c                      dddd} | S )z7Construct help text based on the command release track.$Starts an IAP TCP forwarding tunnel.a  Starts a tunnel to Cloud Identity-Aware Proxy for TCP forwarding through which
another process can create a connection (eg. SSH, RDP) to a Google Compute
Engine instance.

To learn more, see the
[IAP for TCP forwarding documentation](https://cloud.google.com/iap/docs/tcp-forwarding-overview).

If the `--region` and `--network` flags are provided, then an IP address or FQDN
must be supplied instead of an instance name. This is most useful for connecting
to on-prem resources.
a  To open a tunnel to the instances's RDP port on an arbitrary local port, run:

  $ {command} my-instance 3389

To open a tunnel to the instance's RDP port on a specific local port, run:

  $ {command} my-instance 3389 --local-host-port=localhost:3333

To use the IP address or FQDN of your remote VM (eg, for on-prem), you must also
specify the `--region` and `--network` flags:

  $ {command} 10.1.2.3 3389 --region=us-central1 --network=default
)briefDESCRIPTIONEXAMPLES )detailed_helps    'lib/surface/compute/start_iap_tunnel.py_DetailedHelpr%   /   s!     1#-B 
    c                   R    e Zd ZdZdZdZed        Zd Zd Z	d Z
d Zd	 Zd
 Zd Zy)StartIapTunnelr   TFc                    t        j                  |       t        j                  j	                  |       |j                  dt        j                  dd      d       d}|j                  dd	 d
|       |j                  dddd       |j                  dddd       t        j                  || j                         y )Ninstance_port   i  )lower_boundupper_boundz8The name or number of the instance's port to connect to.)typehelpaA  `LOCAL_HOST:LOCAL_PORT` on which gcloud should bind and listen for connections
that should be tunneled.

`LOCAL_PORT` may be omitted, in which case it is treated as 0 and an arbitrary
unused local port is chosen. The colon also may be omitted in that case.

If `LOCAL_PORT` is 0, an arbitrary unused local port is chosen.--local-host-portc                 D    t         j                  j                  | d      S )NT)ipv6_enabled)r   HostPortParse)args    r$   <lambda>z%StartIapTunnel.Args.<locals>.<lambda>p   s    --33Cd3Kr&   zlocalhost:0)r.   defaultr/   --listen-on-stdin
store_trueTzWhether to get/put local data on stdin/stdout instead of listening on a socket.  It is an error to specify --local-host-port with this, because that flag has no meaning with this.)actionhiddenr/   z%--iap-tunnel-disable-connection-checkFz/Disables the immediate check of the connection.)r7   r:   r/   )
r   AddProxyServerHelperArgsr   INSTANCE_ARGAddArgumentadd_argumentr   
BoundedIntAddHostBasedTunnelArgssupport_security_gateway)clsparserlocal_host_port_help_texts      r$   ArgszStartIapTunnel.Args]   s    ''/	""6*
##uEG  I
!C K&	  ( 	   />	  @ %%fc.J.JKr&   c                    |j                   r'|j                  d      rt        j                  dd      | j	                  |      }| j                  ||      }| j                          	 |j                          y # t        j                  $ r?}| j                  |j                        r|j                  s| j                  |       |d }~ww xY w)Nlocal_host_portr8   r0   )listen_on_stdinIsSpecifiedcalliope_exceptionsConflictingArgumentsException_GetTargetArgs_CreateIapTunnelHelper_CheckNumpyInstalledRunr   ConnectionCreationError%_ShouldFetchInstanceAfterConnectErrorr   r   _FetchInstance)selfargstargetiap_tunnel_helperes        r$   rP   zStartIapTunnel.Run   s     0 01B C==
24 4   &F33D&A
77 

4
4TYY
?kk 	D!gs   (A9 9C:CCc                 "    | j                   xr |S )N)"fetch_instance_after_connect_error)rT   r   s     r$   rR   z4StartIapTunnel._ShouldFetchInstanceAfterConnectError   s    22;t;r&   c           	          | j                   re|j                  rYt        j                  ||j                  |j
                  |j                  |j                  |j                  |j                        }n|j                  rYt        j                  ||j                  |j
                  |j                  |j                  |j                  |j                        }nMt        j                  ||j                  |j                  |j                  |j                  |j                        }|j                  rt        j                  |      }|S | j!                  |      \  }}d}t#        |d      r|j$                   }t        j&                  ||||      }|S )N)r   r   r   r   r   use_dest_group)r   r   r   r   r   )r   r   r   r   T#iap_tunnel_disable_connection_check)rB   r   r   SecurityGatewayTunnelHelperr   r   r   r   r   IAPWebsocketTunnelHelperr   r   r   r   rI   IapTunnelStdinHelper_GetLocalHostPorthasattrr]   IapTunnelProxyServerHelper)rT   rU   rV   tunnelerrW   
local_host
local_portcheck_connections           r$   rN   z%StartIapTunnel._CreateIapTunnelHelper   sO   $$)>)>77
v}}!22{{V=N=NPh 
44
..{{{{&&(h 44
{{??$${{h $99(C   $55d;j*	<	=#GGG$??
j"2H> r&   c                 *   |j                  d      r|j                  d      rwt        t        j                  j                  j
                  j                         |j                  |j                  |j                  |j                  |j                  d d d d 
      S | j                  r|j                  rwt        t        j                  j                  j
                  j                         |j                  |j                  |j                  |j                  d |j                  d d d 
      S | j                  |j                         rct        t        j                  j                  j
                  j                         |j                   |j                  d|j                  d d d d d 
      S | j#                  |      \  }}t        |j
                  |j                   |j$                  t'        j(                  |      j$                  |j                  d d d d d 
      S )Nr   r   )
r   r   r   r   r   r   r   r   r   r   )
r   r   r   r   r   r   r   r   r   r   nic0r   )rJ   _CreateTargetArgsr   VALUEScorer   	GetOrFailr   r   instance_namer*   r   rB   r   r\   rR   r   rS   namer   GetInternalInterface)rT   rU   instance_refinstance_objs       r$   rM   zStartIapTunnel._GetTargetArgs   s   	"t'7'7'A##((00::<,,!!!!__
! 
! $$)>)>##((00::<!!!!00((
 
 11$))<##((00::<yy%%!!
! 
! "&!4!4T!:L,$$""00>CC
 
r&   c           	         t        j                  | j                               }|j                  }t	        j
                         }t        j                  j                  |j                  gt        j                  j                  |j                  |j                  t        j                  |            d   }||j!                  ||      fS )N)scope_listerr   )r   ComputeApiHolderReleaseTrackclientr   BaseSSHCLIHelperr   SSH_INSTANCE_RESOLVERResolveResourcesrn   r   	ScopeEnumZONEr   	resourcesGetInstanceZoneScopeListerGetInstance)rT   rU   holderrw   
ssh_helperrq   s         r$   rS   zStartIapTunnel._FetchInstance   s    **4+<+<+>?F]]F++-J..??					55f= @ ?
 @ABL //EEEr&   c                     |j                   j                  xs d}|j                   j                  rt        |j                   j                        nd}t	        j
                  |      }|s"t        j                  j                  d|z         ||fS )N	localhostr   )port_argzPicking local unused port [%d].)	rH   r   r   intr   DetermineLocalPortr   statusPrint)rT   rU   local_host_argr   rf   s        r$   ra   z StartIapTunnel._GetLocalHostPort  sw    ))..=+N*.*>*>*C*CD  %%& ..AJ	jj8:EF:%%r&   c                 ^    	 dd l }y # t        $ r t        j                  t               Y y w xY w)Nr   )numpyImportErrorr   warning_NUMPY_HELP_TEXT)rT   r   s     r$   rO   z#StartIapTunnel._CheckNumpyInstalled  s&    
$ $	kk"#$s    ",,N)__name__
__module____qualname____doc__rZ   rB   classmethodrF   rP   rR   rN   rM   rS   ra   rO   r"   r&   r$   r(   r(   U   sN     -'+$"(L (LT*<!F5nF&$r&   r(   c                       e Zd ZdZdZy)StartIapTunnelBeta+Starts an IAP TCP forwarding tunnel (Beta).TN)r   r   r   r   rZ   r"   r&   r$   r   r     s     4'+$r&   r   c                       e Zd ZdZdZy)StartIapTunnelAlphar   TN)r   r   r   r   rB   r"   r&   r$   r   r   $  s     4!r&   r   )&r   
__future__r   r   r   collectionsgooglecloudsdk.api_lib.computer   r   googlecloudsdk.callioper   r	   r
   rK   "googlecloudsdk.command_lib.computer   r   r   ,googlecloudsdk.command_lib.compute.instancesr   googlecloudsdk.corer   r   
namedtuplerj   r   r%   UniverseCompatibleReleaseTracksrv   GACommandr(   BETAr   ALPHAr   r#   r"   r&   r$   <module>r      sQ   C &  '  7 ? / ( E 9 4 8 > # **K**= ;  
 #L D%%(()B$T\\ B$ * B$J D%%**+, , , , D%%++,", " - "
 %2O  !#0?   , r&   