
    %                        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 d Zej4                  ej6                   ej8                  ej:                  j<                  ej:                  j>                  ej:                  j@                         G d dejB                                             Z"y),Enable a personal auth session on a cluster.    )absolute_import)division)unicode_literalsN)dataproc)
exceptions)util)waiter)base)clusters)flags)log)
console_io)progress_tracker)filesc                     | j                   j                  ||      }| j                   j                  d|z   d|z   d|z   |      }| j                  j                  j                  |      S )a1  Inject credentials into the given cluster.

  The credentials must have already been encrypted before calling this method.

  Args:
    dataproc: The API client for calling into the Dataproc API.
    project: The project containing the cluster.
    region: The region where the cluster is located.
    cluster_name: The name of the cluster.
    cluster_uuid: The cluster UUID assigned by the Dataproc control plane.
    credentials_ciphertext: The (already encrypted) credentials to inject.

  Returns:
    An operation resource for the credential injection.
  )clusterUuidcredentialsCiphertextz	projects/zregions/z	clusters/)projectregionclusterinjectCredentialsRequest)messagesInjectCredentialsRequest7DataprocProjectsRegionsClustersInjectCredentialsRequestclientprojects_regions_clustersInjectCredentials)r   r   r   cluster_namecluster_uuidcredentials_ciphertextinject_credentials_requestrequests           =lib/surface/dataproc/clusters/enable_personal_auth_session.py_inject_encrypted_credentialsr%   /   s|    "  (00II6L  J  NUUG#& L(9	 V ;'
 
	2	2	D	DW	MM    c                   4    e Zd ZdZddiZed        Zd Zd Zy)EnablePersonalAuthSessionr   EXAMPLESzv
          To enable a personal auth session, run:

            $ {command} my-cluster --region=us-central1
          c                     t        j                  | j                               }t        j                  |d|j
                         t        j                  |       y)zMethod called by Calliope to register flags for this command.

    Args:
      parser: An argparser parser used to register flags.
    z!enable a personal auth session onN)dpDataprocReleaseTrackr   AddClusterResourceArgapi_versionAddPersonalAuthSessionArgs)clsparserr   s      r$   ArgszEnablePersonalAuthSession.Args\   sE     {{3++-.H	(K ( 4 46	$$V,r&   c
                    t        j                  |      }
|
s$t        j                  dj	                  |            t        j
                         j                  ||
|	      }t        ||||||      }|rt        j                  ||       y y )Nz-Failure getting credentials to inject into {})
r	   GetCredentialsr   PersonalAuthErrorformatPersonalAuthUtilsEncryptWithPublicKeyr%   r
   WaitFor)selfr   r   r   r   r    cluster_keyaccess_boundary_jsonoperation_polleropenssl_executabledownscoped_tokenr!   inject_operations                r$   inject_credentialsz,EnablePersonalAuthSession.inject_credentialsn   s     **+?@((
9
@
@
NP P!335JJ%'9;4Xw5A<5KM nn%'78 r&   c                 &   d}t        j                  |dd       t        j                  | j	                               }|j
                  j                  j                         }|j                  }|j                  }|j                  }|j                  j                  |||      }|j                  j                  j                  |      }	|	j                   }
|j"                  r9t%        j&                  |j"                        5 }|j)                         }d d d        nt+        j,                  |      }t/        j0                         j3                         rdnd}d }|dk(  rt5        j6                  |	|      }|sd}d }|dk(  r:t5        j6                  |	|      }|j8                  }|s	 t%        j:                  d      }tC        jD                  |j                  jF                  d
       }	 |s$tI        jJ                  djM                  |            tO        jP                  djM                  |      d      5  | jS                  |||||
|||	       d d d        |jT                  sy djM                  |      }tO        jP                  |d      5  	 |j                  j                  j                  |      }	t5        j6                  |	|      }|s$tI        jJ                  djM                  |            d}|dk  r7	 tW        jX                  d       | jS                  |||||
|||	       d}|dk  r7tI        jJ                  d      # 1 sw Y   xY w# t<        $ r t?        j@                  d	       Y w xY w# 1 sw Y   'xY w# t<        $ r$}t?        jZ                  |       |dz  }Y d }~d }~ww xY w# t         j\                  t^        f$ r Y d d d        y w xY w# 1 sw Y   y xY w# tH        jJ                  $ r}t?        jZ                  |       Y d }~y d }~ww xY w)NzA personal authentication session will propagate your personal credentials to the cluster, so make sure you trust the cluster and the user who created it.Tz Enabling session aborted by user)messagecancel_on_nocancel_string)	projectIdr   clusterNameECIESRSAopensslzcCould not find openssl on your system. The enable-session command requires openssl to be installed.c                     | j                   S )N)name)	operations    r$   <lambda>z/EnablePersonalAuthSession.Run.<locals>.<lambda>   s    )..r&   z.The cluster {} does not support personal auth.z1Injecting initial credentials into the cluster {})autotickzoPeriodically refreshing credentials for cluster {}. This will continue running until the command is interruptedr            z>Credential injection failed three times in a row, giving up...)0r   PromptContinuer+   r,   r-   CONCEPTSr   ParserG   r   rH   r   )DataprocProjectsRegionsClustersGetRequestr   r   Getr   access_boundaryr   
FileReaderreadr   ProjectGcsObjectsAccessBoundaryr	   r8   IsTinkLibraryInstalledr   
ClusterKeyopenssl_commandFindExecutableOnPath
ValueErrorr   fatalr
   CloudOperationPollerNoResourcesprojects_regions_operationsr   r6   r7   r   ProgressTrackerrB   refresh_credentialstimesleeperrorOperationCancelledErrorKeyboardInterrupt)r;   argsrD   r   cluster_refr   r   r   get_requestr   r    abfr=   cluster_key_typer<   r?   r>   update_messagefailure_counterrs                       r$   RunzEnablePersonalAuthSession.Run~   s   .G 8: {{4,,./H--''--/K##GF**L##MM&l N DKoo77;;KHG&&LD001S"xxz 21 #BB7K #'"8"8 ##w$)  K7" ''1ABk 5 ''1ABk//	A$99)D

 ==33(*3**<CC 	 ++
=
D
D 	'6< ,k;O 02D	F	 %%D| 
 ++NTJ	 OO==AA+N' ++G5EF+..@GG "# # -!	!jjn%%h&2K&:<L&8:  m ! ,,NP P[ 212  	A
)) @ A	A H  !iinq m!
 224EF 	
5 KJ2	3 KJ6 '' 	iins    L<"M	 #AO /M.	O (O OA(N+10M;!N+'N+<M	M+*M+.M83O ;	N(N#N+#N((N++OOO OOOO O P1PPN)	__name__
__module____qualname____doc__detailed_helpclassmethodr3   rB   rt    r&   r$   r(   r(   J   s7     5 - 	- 	-"9 gr&   r(   )#rx   
__future__r   r   r   rg   googlecloudsdk.api_lib.dataprocr   r+   r   r	   googlecloudsdk.api_lib.utilr
   googlecloudsdk.callioper   #googlecloudsdk.command_lib.dataprocr   r   googlecloudsdk.corer   googlecloudsdk.core.consoler   r   googlecloudsdk.core.utilr   r%   DefaultUniverseOnlyHiddenReleaseTracksr-   ALPHABETAGACommandr(   r{   r&   r$   <module>r      s    3 &  '  ; 6 0 . ( 8 5 # 2 8 *N6 T..33T5F5F5I5IV V  
Vr&   