o
    .&ßaõ  ã                   @   sn   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	 e  
e¡ZdZdd„ ZG dd	„ d	e	ƒZdS )
é    N)Úsix)Úmodel)ÚBaseCLIArgumentz½<p>The file that contains the private key used to launch
the instance (e.g. windows-keypair.pem).  If this is supplied, the
password data sent from EC2 will be decrypted before display.</p>c                 K   s   t ||dƒ| d< dS )zž
    This handler gets called after the argument table for the
    operation has been created.  It's job is to add the
    ``priv-launch-key`` parameter.
    zpriv-launch-keyN)ÚLaunchKeyArgument)Úargument_tableÚoperation_modelÚsessionÚkwargs© r
   úK/usr/lib/python3/dist-packages/awscli/customizations/ec2/decryptpassword.pyÚec2_add_priv_launch_key    s   ÿr   c                   @   s^   e Zd Zdd„ Zedd„ ƒZedd„ ƒZejdd„ ƒZedd	„ ƒZd
d„ Z	dd„ Z
dd„ ZdS )r   c                 C   s4   || _ t dddi¡| _|| _|| _d | _d| _d S )Nr   ÚtypeÚstringF)Ú_sessionr   ÚShapeÚargument_modelÚ_operation_modelÚ_nameÚ	_key_pathÚ	_required)Úselfr   r   Únamer
   r
   r   Ú__init__-   s   
zLaunchKeyArgument.__init__c                 C   s   dS )Nr   r
   ©r   r
   r
   r   Úcli_type_name5   ó   zLaunchKeyArgument.cli_type_namec                 C   s   | j S ©N©r   r   r
   r
   r   Úrequired9   s   zLaunchKeyArgument.requiredc                 C   s
   || _ d S r   r   )r   Úvaluer
   r
   r   r   =   s   
c                 C   s   t S r   )ÚHELPr   r
   r
   r   ÚdocumentationA   r   zLaunchKeyArgument.documentationc                 C   s   |j | j| jdd� d S )NzSSH Private Key file)ÚdestÚhelp)Úadd_argumentÚcli_nameÚpy_name)r   Úparserr
   r
   r   Úadd_to_parserE   s   
ÿzLaunchKeyArgument.add_to_parserc                 C   sl   |r4t j |¡}t j |¡}t j |¡r.|| _| jjj}d|| jj	f }| j
 || j¡ dS d}t|ƒ‚dS )a  
        This gets called with the value of our ``--priv-launch-key``
        if it is specified.  It needs to determine if the path
        provided is valid and, if it is, it stores it in the instance
        variable ``_key_path`` for use by the decrypt routine.
        zafter-call.%s.%sz_priv-launch-key should be a path to the local SSH private key file used to launch the instance.N)ÚosÚpathÚ
expandvarsÚ
expanduserÚisfiler   r   Úservice_modelÚendpoint_prefixr   r   ÚregisterÚ_decrypt_password_dataÚ
ValueError)r   Ú
parametersr   r*   r/   ÚeventÚmsgr
   r
   r   Úadd_to_paramsI   s   ÿÿòzLaunchKeyArgument.add_to_paramsc                 K   sÞ   | j durmt d| j ¡ | d¡}|sdS zCt| j ƒ�3}| ¡ }tj t	 
|¡¡}t |¡}t ||¡}t |¡ | d¡|d< t |¡ W d  ƒ W dS 1 sQw   Y  W dS  tyl   tjddd� d}t|ƒ‚w dS )	aq  
        This handler gets called after the GetPasswordData command has been
        executed.  It is called with the and the ``parsed`` data.  It checks to
        see if a private launch key was specified on the command.  If it was,
        it tries to use that private key to decrypt the password data and
        replace it in the returned data dictionary.
        Nz"Decrypting password data using: %sÚPasswordDatazutf-8zUnable to decrypt PasswordDataT)Úexc_infoz@Unable to decrypt password data using provided private key file.)r   ÚloggerÚdebugÚgetÚopenÚreadÚrsaÚ
PrivateKeyÚ
load_pkcs1r   ÚbÚbase64Ú	b64decodeÚdecryptÚdecodeÚ	Exceptionr2   )r   Úparsedr	   r   Úpk_fileÚpk_contentsÚprivate_keyr5   r
   r
   r   r1   `   s*   



&ùüòz(LaunchKeyArgument._decrypt_password_dataN)Ú__name__Ú
__module__Ú__qualname__r   Úpropertyr   r   Úsetterr!   r(   r6   r1   r
   r
   r
   r   r   +   s    



r   )Úloggingr)   rB   r>   Úawscli.compatr   Úbotocorer   Úawscli.argumentsr   Ú	getLoggerrK   r9   r    r   r   r
   r
   r
   r   Ú<module>   s   
