o
    .&ßaY  ã                   @   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)ÚCustomArgumentÚec2bundleinstancezàThe 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>.z³The 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ÚnameÚ	help_textÚPrefixÚprefixÚAWSAccessKeyIdz
owner-akidÚ_SAKz	owner-sakÚUploadPolicyÚpolicy)ÚrequiredÚBundleArgumentÚBUCKET_DOCSÚPREFIX_DOCSÚOWNER_AKID_DOCSÚOWNER_SAK_DOCSÚPOLICY_DOCS)Úargument_tableÚkwargsÚstorage_argÚarg© r   úJ/usr/lib/python3/dist-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   Ú
owner_akidÚ	owner_sakr   zOMixing the --storage option with the simple, scalar options is not recommended.)ÚloggerÚdebugÚvarsÚ
ValueError)Úparsed_argsr   Ú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-   Úexpires_isor   r   r   r   Ú_generate_policyv   s   
þr7   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)Ú	digestmodÚUploadPolicySignature)ÚgetÚbase64Ú	b64encoder   ÚbÚdecodeÚhmacÚnewÚencoder   ÚupdateÚencodebytesÚdigestÚstrip)r4   r   ÚsakÚnew_hmacÚpsr   r   r   Ú_generate_signature‚   s   


úrI   c                 K   s4   | d d }d|vrt |ƒ d|vrt|ƒ d S d S )NÚStorageÚS3r   r9   )r7   rI   )r4   r   r   r   r   r   Ú_check_params�   s   ÿrL   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)Úevent_handlerÚeventÚhandlerr   r   r   Úregister_bundleinstance    s   ÿrS   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 rM   )Úsuperr   Ú__init__Ú_storage_param)Úselfr	   Úargsr   ©Ú	__class__r   r   rU   ¨   s   
zBundleArgument.__init__c                 C   s*   d|vr
di i|d< ||d d | j < d S )NrJ   rK   )rV   )rW   r4   Úvaluer   r   r   Ú_build_storage¬   s   zBundleArgument._build_storagec                 C   s   |r
|   ||¡ d S d S rM   )r\   )rW   Ú
parametersr[   r   r   r   Úadd_to_params²   s   ÿzBundleArgument.add_to_params)Ú__name__Ú
__module__Ú__qualname__rU   r\   r^   Ú__classcell__r   r   rY   r   r   ¦   s    r   )ÚloggingÚhashlibr   r?   r;   r.   Úawscli.compatr   Úawscli.argumentsr   Ú	getLoggerr"   r   r   r   r   r   r   r*   r2   r7   rI   rL   rN   rS   r   r   r   r   r   Ú<module>   s4   
ÿ	ý