o
    !dR)                     @   s   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	 d dl
mZ d dlmZ dd Zdd
dZdd ZG dd de	ZG dd de	ZG dd deZG dd deZG dd deZdd ZG dd deZG dd deZdS )    N)parse_to_aware_datetime)CloudFrontSigner)CustomArgument)#validate_mutually_exclusive_handler)BasicCommandc                    s   |  dt |  dt |  dtdgdg |  dtddgd	g |  d
dd  |  d
dd  i  | j d jdd |  dtdgd	g |  d fdd d S )Nz!building-command-table.cloudfrontz6building-argument-table.cloudfront.create-invalidationz4operation-args-parsed.cloudfront.create-invalidationZinvalidation_batchpathsz4operation-args-parsed.cloudfront.create-distributionZdefault_root_objectZorigin_domain_nameZdistribution_configz6building-argument-table.cloudfront.create-distributionc                 [      |  dt| S )Norigin-domain-name)__setitem__OriginDomainNameargument_tablekwargs r   Dusr/lib/python3.10/site-packages/awscli/customizations/cloudfront.py<lambda>+       zregister.<locals>.<lambda>c                 [   r   )Ndefault-root-object)r
   CreateDefaultRootObjectr   r   r   r   r   /   r   ztop-level-args-parsed
cloudfront)Z	unique_idz4operation-args-parsed.cloudfront.update-distributionz6building-argument-table.cloudfront.update-distributionc                    s   |  dt | dS )Nr   )contextr   )r
   UpdateDefaultRootObjectr   r   r   r   r   ;   s
    )register	_add_sign
_add_pathsr   update)Zevent_handlerr   r   r   r      sJ   
r   clic                 C   s   d| t t tddf S )Nz%s-%s-%s   i@B )inttimerandomrandintprefixr   r   r   unique_string@   s   r%   c                 K   s   d| d _ t | d< d S )NFzinvalidation-batchr   )requiredPathsArgumentr   r   r   r   r   D   s   
r   c                       $   e Zd Z fddZdd Z  ZS )r'   c                    s   d}t t| jdd|d d S )NzkThe space-separated paths to be invalidated. Note: --invalidation-batch and --paths are mutually exclusive.r   +)nargs	help_text)superr'   __init__)selfdoc	__class__r   r   r-   K   s   zPathsArgument.__init__c                 C   s*   |d urt  t||dd|d< d S d S )NQuantityZItems)CallerReferencePathsZInvalidationBatch)r%   lenr.   
parametersvaluer   r   r   add_to_paramsR   s
   zPathsArgument.add_to_params__name__
__module____qualname__r-   r:   __classcell__r   r   r0   r   r'   I   s    r'   c                       s,   e Zd ZdZ	d fdd	Zdd Z  ZS )	ExclusiveArgumentz1%s This argument and --%s are mutually exclusive.distribution-config c                    s,   d|| _ tt| j|| j||f d d S )NFr+   )r&   r,   r@   r-   DOC)r.   namer   Zexclusive_tor+   r0   r   r   r-   ]   s   


zExclusiveArgument.__init__c                 C   s4   t  dg dddddiddddd	dd
dddS )Nr   r2   placeholderFForwardnone)ZQueryStringZCookies)Enabledr3   z	allow-all)TargetOriginIdZForwardedValuesZTrustedSignersZViewerProtocolPolicyZMinTTLTrB   )r4   OriginsDefaultCacheBehaviorrI   Comment)r%   )r.   r   r   r   distribution_config_templatec   s   z.ExclusiveArgument.distribution_config_template)rA   rB   )r<   r=   r>   rD   r-   rN   r?   r   r   r0   r   r@   Z   s
    r@   c                       r(   )r   c                    s   t t| jd|dd d S )Nr	   z The domain name for your origin.rC   )r,   r   r-   )r.   r   r0   r   r   r-   z   s   

zOriginDomainName.__init__c                 C   s   |d u rd S | d|   t|d}||dd}|d dr'ddi|d< nd	d
dd|d< d|gd|d d< ||d d d< d S )NDistributionConfigr#   rB   )Id
DomainNameZ
OriginPathrQ   z.s3.amazonaws.comZOriginAccessIdentityZS3OriginConfigP   i  z	http-only)ZHTTPPortZ	HTTPSPortZOriginProtocolPolicyZCustomOriginConfigr   r2   rK   rL   rJ   )
setdefaultrN   r%   endswith)r.   r8   r9   Z	origin_iditemr   r   r   r:      s$   


zOriginDomainName.add_to_paramsr;   r   r   r0   r   r   y   s    r   c                       s&   e Zd Zd fdd	Zdd Z  ZS )r   rB   c                    s   t t| jd||p
dd d S )Nr   zvThe object that you want CloudFront to return (for example, index.html) when a viewer request points to your root URL.rC   )r,   r   r-   )r.   r   r+   r0   r   r   r-      s   

z CreateDefaultRootObject.__init__c                 C   s,   |d ur| d|   ||d d< d S d S )NrO   DefaultRootObject)rS   rN   r7   r   r   r   r:      s   z%CreateDefaultRootObject.add_to_params)rB   r;   r   r   r0   r   r      s    r   c                       r(   )r   c                    s   t t| j|dd || _d S )NzThe object that you want CloudFront to return (for example, index.html) when a viewer request points to your root URL. CLI will automatically make a get-distribution-config call to load and preserve your other settings.rC   )r,   r   r-   r   )r.   r   r   r0   r   r   r-      s
   

z UpdateDefaultRootObject.__init__c                 C   st   |d ur8| j d jd| j d j| j d j| j d jd}|j|d d}|d |d< |d	 |d	< ||d	 d
< d S d S )Nsessionr   Zparsed_args)Zregion_nameendpoint_urlZverifyrP   )rP   ZETagZIfMatchrO   rV   )r   Zcreate_clientZregionrX   Z
verify_sslZget_distribution_config)r.   r8   r9   Zclientresponser   r   r   r:      s   



z%UpdateDefaultRootObject.add_to_paramsr;   r   r   r0   r   r      s    	r   c                 K   s   t || d< d S )Nsign)SignCommand)Zcommand_tablerW   r   r   r   r   r         r   c                   @   sf   e Zd ZdZdZdZdddddddd	d
dddd
ddde d
dde ddddgZdd ZdS )r[   rZ   zSign a given url.aO  Supported formats include:
        YYYY-MM-DD (which means 0AM UTC of that day),
        YYYY-MM-DDThh:mm:ss (with default timezone as UTC),
        YYYY-MM-DDThh:mm:ss+hh:mm or YYYY-MM-DDThh:mm:ss-hh:mm (with offset),
        or EpochTime (which always means UTC).
        Do NOT use YYYYMMDD, because it will be treated as EpochTime.urlTzThe URL to be signed)rE   Zno_paramfiler&   r+   zkey-pair-idz_The active CloudFront key pair Id for the key pair that you're using to generate the signature.)rE   r&   r+   zprivate-keyz#file://path/to/your/private-key.pemzdate-less-thanz*The expiration date and time for the URL. zdate-greater-thanz-An optional start date and time for the URL. )rE   r+   z
ip-addressztAn optional IP address or IP address range to allow client making the GET request from. Format: x.x.x.x/x or x.x.x.xc                 C   s   t |jt|jj}t|j}|j}|d urt|}|d us#|jd ur<|j	|j
|||jd}tj|j|j
|d dS tj|j|j
|d dS )N)date_greater_than
ip_address)policy)date_less_thanr   )r   Zkey_pair_id	RSASignerprivate_keyrZ   r   ra   r^   r_   Zbuild_policyr]   sysstdoutwriteZgenerate_presigned_url)r.   argsZparsed_globalsZsignerra   r^   r`   r   r   r   	_run_main   s(   


zSignCommand._run_mainN)r<   r=   r>   NAMEZDESCRIPTIONZDATE_FORMATZ	ARG_TABLErh   r   r   r   r   r[      s8    %r[   c                   @   s   e Zd Zdd Zdd ZdS )rb   c                 C   s   t j|d| _d S )Nutf8)rsaZ
PrivateKeyZ
load_pkcs1encodepriv_key)r.   rc   r   r   r   r-      s   zRSASigner.__init__c                 C   s   t || jdS )NzSHA-1)rk   rZ   rm   )r.   messager   r   r   rZ     r\   zRSASigner.signN)r<   r=   r>   r-   rZ   r   r   r   r   rb      s    rb   )r   )rd   r    r!   rk   Zbotocore.utilsr   Zbotocore.signersr   Zawscli.argumentsr   Zawscli.customizations.utilsr   Zawscli.customizations.commandsr   r   r%   r   r'   r@   r   r   r   r   r[   objectrb   r   r   r   r   <module>   s(   
&A