o
    !dY                     @   s   d dl Z d dlmZ d dlZd dlZd dlZd dlmZ d dlm	Z	 e 
dZdZdZdZd	Zd
Zdd Zdd ZdZdd Zdd Zdd ZdefdefdefgZdd ZG dd de	ZdS )    N)sha1)six)CustomArgumentZec2bundleinstancezThe bucket in which to store the AMI.  You can specify a bucket that you already own or a new bucket that Amazon EC2 creates on your behalf.  If you specify a bucket that belongs to someone else, Amazon EC2 returns an error.zCThe prefix for the image component names being stored in Amazon S3.z7The access key ID of the owner of the Amazon S3 bucket.ac  An Amazon S3 upload policy that gives Amazon EC2 permission to upload items into Amazon S3 on the user's behalf. If you provide this parameter, you must also provide your secret access key, so we can create a policy signature for you (the secret access key is not passed to Amazon EC2). If you do not provide this parameter, we generate an upload policy for you automatically. For more information about upload policies see the sections about policy construction and signatures in the <a href="http://docs.aws.amazon.com/AmazonS3/latest/dev/HTTPPOSTForms.html">Amazon Simple Storage Service Developer Guide</a>.zThe AWS secret access key for the owner of the Amazon S3 bucket specified in the --bucket parameter. This parameter is required so that a signature can be computed for the policy.c                 K   s   | d }d|_ tddtd}|| d< tddtd}|| d< tdd	td}|| d	< td
dtd}|| d< tddtd}|| d< d S )NstorageFBucketbucket)storage_paramnameZ	help_textPrefixprefixZAWSAccessKeyIdz
owner-akid_SAKz	owner-sakUploadPolicypolicy)requiredBundleArgumentBUCKET_DOCSPREFIX_DOCSOWNER_AKID_DOCSOWNER_SAK_DOCSPOLICY_DOCS)Zargument_tablekwargsZstorage_argarg r   Lusr/lib/python3.10/site-packages/awscli/customizations/ec2/bundleinstance.py_add_paramsB   s6   r   c                 K   s@   t |  t| }|d rdD ]}|| rd}t|qd S d S )Nr   )r   r   Z
owner_akidZ	owner_sakr   zOMixing the --storage option with the simple, scalar options is not recommended.)loggerdebugvars
ValueError)Zparsed_argsr   Zarg_dictkeymsgr   r   r   _check_args^   s   
r!   z{{"expiration": "{expires}","conditions": [{{"bucket": "{bucket}"}},{{"acl": "ec2-bundle-read"}},["starts-with", "$key", "{prefix}"]]}}c                 C   sH   t jdd}t j  | }|d}tj|| d | d d}|| d< d S )N   )hoursz%Y-%m-%dT%H:%M:%S.%fZr   r
   )expiresr   r   r   )datetime	timedeltautcnowstrftimePOLICYformat)paramsdeltar$   Zexpires_isor   r   r   r   _generate_policyv   s   
r-   c                 C   s   |  d}|  d}|r@|rBtt|d}tj|dt	d}|
t| t|  d}|| d< | d= d S d S d S )Nr   r   zutf-8)Z	digestmodUploadPolicySignature)getbase64	b64encoder   bdecodehmacnewencoder   updateencodebytesdigeststrip)r+   r   ZsakZnew_hmacZpsr   r   r   _generate_signature   s   


r;   c                 K   s4   | d d }d|vrt | d|vrt| d S d S )NStorageS3r   r.   )r-   r;   )r+   r   r   r   r   r   _check_params   s   r>   z+building-argument-table.ec2.bundle-instancez)operation-args-parsed.ec2.bundle-instancez)before-parameter-build.ec2.BundleInstancec                 C   s   t D ]
\}}| || qd S N)EVENTSregister)Zevent_handlereventhandlerr   r   r   register_bundleinstance   s   rD   c                       s,   e Zd Z fddZdd Zdd Z  ZS )r   c                    s    t t| j|i | || _d S r?   )superr   __init___storage_param)selfr   argsr   	__class__r   r   rF      s   
zBundleArgument.__init__c                 C   s*   d|vr
di i|d< ||d d | j < d S )Nr<   r=   )rG   )rH   r+   valuer   r   r   _build_storage   s   zBundleArgument._build_storagec                 C   s   |r
|  || d S d S r?   )rM   )rH   
parametersrL   r   r   r   add_to_params   s   zBundleArgument.add_to_params)__name__
__module____qualname__rF   rM   rO   __classcell__r   r   rJ   r   r      s    r   )loggingZhashlibr   r4   r0   r%   Zawscli.compatr   Zawscli.argumentsr   	getLoggerr   r   r   r   r   r   r   r!   r)   r-   r;   r>   r@   rD   r   r   r   r   r   <module>   s4   
	