
    W!                        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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Jr  SSKJr  SrSrSr  " S S\!5      r"S r#S r$S r%S r&S r'g)z3Utilities for `app instances *` commands using SSH.    )absolute_import)division)unicode_literals)
exceptions)env)version_util)base_classes)lister)base)util)ssh)log)
properties)	resources)
console_iozyThis instance is serving live application traffic.  Any changes made could
result in downtime or unintended consequences.zgae.{project}.{instance_id}a  

*{command}* resolves the instance's IP address and pre-populates the
VM with a public key managed by gcloud. If the gcloud managed key pair
does not exist, it is generated the first time an SSH command is run,
which may prompt you for a passphrase for the private key encryption.

All SSH commands require the OpenSSH client suite to be installed on
Linux and Mac OS X. On Windows, the Google Cloud CLI comes with a bundled
PuTTY suite instead, so it has no external dependencies.c                   0    \ rS rSrSrS rS rS rS rSr	g)	ConnectionDetails9   z?Details about an SSH connection, for assembling an SSH command.c                     Xl         X l        g N)remoteoptions)selfr   r   s      0lib/googlecloudsdk/command_lib/app/ssh_common.py__init__ConnectionDetails.__init__<   s    KL    c                 j    [        XR                  5      (       a  U R                  UR                  :H  $ g)NF)
isinstance	__class____dict__r   others     r   __eq__ConnectionDetails.__eq__@   s'    %((]]enn,,r   c                 .    U R                  U5      (       + $ r   )r$   r"   s     r   __ne__ConnectionDetails.__ne__E   s    {{5!!!r   c                 8    SR                  U R                  5      $ )NzConnectionDetails(**{}))formatr!   )r   s    r   __repr__ConnectionDetails.__repr__H   s    $++DMM::r   )r   r   N)
__name__
__module____qualname____firstlineno____doc__r   r$   r'   r+   __static_attributes__ r   r   r   r   9   s    G
";r   r   c                 l   [         R                  " U 5      nUR                  n[        R                  " [
        R                  R                  R                  R                  5       5      nUR                  UR                  R                  SUR                  R                  UR                  S94/5      S   $ )NGet)projectr   )compute_base_classesComputeApiHolderclientprojects_utilParseProjectr   VALUEScorer6   	GetOrFailMakeRequestsapitools_clientprojectsmessagesComputeProjectsGetRequest	projectId)release_trackholderr9   project_refs       r   GetComputeProjectrH   L   s    00?&==&**$$..02+ 
		v55>>%HH+6+@+@  I  BC D 
EEF
H Hr   c                 X   U  H  n [        U[        5      (       d  [        S5      e US:X  a    gSU;   d  M5  [	        UR                  S5      S   5      n[	        UR                  S5      S   5      nUSs=::  a
  U::  d  M~     g  M     g	! [         a  n[        U5         SnAN|SnAff = f)
zChecks if the given list of allowed ports contains port 22.

Args:
  allowed_ports:

Returns:

Raises:
  ValueError:Port value must be of type string.
z!Port value must be of type stringN22T-r         F)r   str
ValueErrorprintintsplit)allowed_portsportestartends        r   _ContainsPort22rX   X   s     dc""<== # t|
d{$**S/!$%e

3"#c	"		 
  
  Ahhs    B


B)B$$B)c                     U R                  XS9n[        R                  R                  US5      nUR                  [        R                  La<  UR                  [        R                  L a  SnSn	OSnSn	[        R                  " X5      e[        R                  R!                  U["        R$                  R&                  R(                  R*                  UUUS.SS	9n
U
R-                  5       n U R/                  U
5      nUR2                  (       d/  [4        R6                  " [8        5        [:        R<                  " S
S
S9  UnU R>                  R@                  RB                  nURD                  RF                  RH                  URJ                  L a  URL                  OURN                  n[P        RR                  " XS9nU(       d]  SR                  XRU                  5       S9n[4        RV                  RY                  SR                  U5      5        U R[                  U
U5        S[P        R\                  R^                  S[`        R                  U R(                  US9S.n[c        UU5      $ ! [        R                   a&    [        R                  " SR                  X5      5      ef = f! [        R                   a    [        R0                  " U5      ef = f)a%  Enable debug mode on and send SSH keys to a flex instance.

Common method for SSH-like commands, does the following:
- Makes sure that the service/version/instance specified exists and is of the
  right type (Flexible).
- If not already done, prompts and enables debug on the instance.
- Populates the public key onto the instance.

Args:
  api_client: An appengine_api_client.AppEngineApiClient.
  service_id: str, The service ID.
  version_id: str, The version ID.
  instance_id: str, The instance ID.
  public_key: ssh.Keys.PublicKey, Public key to send.
  oslogin_state_user: str, The user to connect as.
  oslogin_state_enabled: bool, Whether OS Login is enabled.

Raises:
  InvalidInstanceTypeError: The instance is not supported for SSH.
  MissingVersionError: The version specified does not exist.
  MissingInstanceError: The instance specified does not exist.
  UnattendedPromptError: Not running in a tty.
  OperationCancelledError: User cancelled the operation.

Returns:
  ConnectionDetails, the details to use for SSH/SCP for the SSH
  connection.
)serviceversionz{}/{}NzManaged VMsz3Use `gcloud compute ssh` for Managed VMs instances.Standard)appsId
versionsIdinstancesId
servicesIdz*appengine.apps.services.versions.instances)params
collectionT)cancel_on_nothrow_if_unattended)hostuserz{user}:{key} {user})rf   keyz$Sending public key to instance [{}].yesno)r6   instance_id)IdentitiesOnlyUserKnownHostsFileCheckHostIPHostKeyAlias)2GetVersionResourceapitools_exceptionsHttpNotFoundErrorcommand_exceptionsMissingVersionErrorr*   r   VersionFromVersionResourceenvironmentr   FLEXMANAGED_VMSInvalidInstanceTypeErrorr   REGISTRYParser   r<   r=   r6   r>   RelativeNameGetInstanceResourceMissingInstanceErrorvmDebugEnabledr   warning_ENABLE_DEBUG_WARNINGr   PromptContinuerB   NetworkInstanceIpModeValueValuesEnumr[   networkinstanceIpModeINTERNALidvmIpr   RemoteToEntrystatusPrintDebugInstance
KnownHostsDEFAULT_PATH_HOST_KEY_ALIASr   )
api_client
service_id
version_idrj   
public_keyoslogin_state_useroslogin_state_enabledr[   rv   msgresrel_nameinstancerf   instance_ip_mode_enumre   r   ssh_keyr   s                      r   PopulatePublicKeyr   t   su   J0++ , 0G
   44WdC'(coo-!kAckc

5
5k
GG  %%**22<<"$"	 > 	! 	?# (<--c2H 
	 	 KK%&4TJ	$!!??  oo,,0E0N0NN kk==  ::4+&	#**:L:L:N*OGJJ;BB8LMS'*NN77%,,Z5G5G9D - F	G' 
67	++a 
	.	. 0

0
0z.0 000 
	.	. <

1
1(
;;<s   I" +J ":J+K
c                     [         R                  " [        R                  R                  5      n U R
                  n[        R                  " SS[        R                  " U R                  R                  [        R                  R                  R                  R                  5       SS9/5      5      n[        R                   " XR"                  R$                  5      n[        R&                  " X#5      nU$ )zZFetches the firewall rules for the current project.

Returns:
  A list of firewall rules.
Nzcompute.projects)rb   )r7   r8   r   ReleaseTrackGAr9   r
   	_FrontendGlobalScoper   r{   r   r<   r=   r6   r>   GlobalListerr@   	firewallsInvoke)rF   r9   request_datalist_implementationresults        r   FetchFirewallRulesr      s      001B1B1E1EF&==&!!





 
 $$,,668+ !  		, ++$$.. ==;&	-r   c                     / nU  Ht  nUR                  S5      S:X  d  M  UR                  S5      nU(       d  M4  US   R                  S5      nU(       d  MQ  [        U5      (       d  Mc  UR                  U5        Mv     U$ )z5Filters firewall rules that allow ingress to port 22.	directionINGRESSallowedr   ports)getrX   append)firewall_rulesfiltered_firewall_rulesfirewall_ruleallowed_dictrS   s        r   FilterFirewallRulesr      sv    %m%2"&&y1l"1o))'2m		'	'&&}5 & 
! r   N)(r1   
__future__r   r   r   apitools.base.pyr   rp   googlecloudsdk.api_lib.appr   r   googlecloudsdk.api_lib.computer	   r7   r
   googlecloudsdk.callioper   googlecloudsdk.command_lib.apprr   #googlecloudsdk.command_lib.projectsr   r:   #googlecloudsdk.command_lib.util.sshr   googlecloudsdk.corer   r   r   googlecloudsdk.core.consoler   r   r   DETAILED_HELPobjectr   rH   rX   r   r   r   r3   r   r   <module>r      sx     : &  ' > * 3 O 1 ( K E 3 # * ) 22  0	<; ;&	H8X,v4!r   