o
    !d                     @   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)Zargument_tableoperation_modelsessionkwargs r	   Musr/lib/python3.10/site-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   ZShapeZ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_argumentZcli_nameZ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   Z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: %sZ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rsaZ
PrivateKeyZ
load_pkcs1r   bbase64	b64decodeZdecryptdecode	Exceptionr,   )r   parsedr   r   Zpk_fileZpk_contentsZprivate_keyr/   r	   r	   r
   r+   `   s*   



&z(LaunchKeyArgument._decrypt_password_dataN)__name__
__module____qualname__r   propertyr   r   setterr   r#   r0   r+   r	   r	   r	   r
   r   +   s    



r   )loggingr$   r9   r7   Zawscli.compatr   Zbotocorer   Zawscli.argumentsr   	getLoggerr>   r2   r   r   r   r	   r	   r	   r
   <module>   s   
