o ckF[5z@sddlZddlZddlZddlmZmZddlmZddlm Z m Z ddl m Z m Z ddlmZddlmZmZddlmZGd d d eZGd d d eZGd ddeZGdddeZGdddeZGdddeZdS)N)sixjson)OriginAccessIdentity)ObjectStreamingObject)ActiveTrustedSignersTrustedSigners) LoggingInfo)S3Origin CustomOrigin)ACLc@s>eZdZ    dddZddZdd Zd d Zd d ZdS)DistributionConfigNFc CsX||_||_||_|r||_ntt|_g|_|r||_||_||_ | |_ ||_ dS)a+ :param origin: Origin information to associate with the distribution. If your distribution will use an Amazon S3 origin, then this should be an S3Origin object. If your distribution will use a custom origin (non Amazon S3), then this should be a CustomOrigin object. :type origin: :class:`boto.cloudfront.origin.S3Origin` or :class:`boto.cloudfront.origin.CustomOrigin` :param enabled: Whether the distribution is enabled to accept end user requests for content. :type enabled: bool :param caller_reference: A unique number that ensures the request can't be replayed. If no caller_reference is provided, boto will generate a type 4 UUID for use as the caller reference. :type enabled: str :param cnames: A CNAME alias you want to associate with this distribution. You can have up to 10 CNAME aliases per distribution. :type enabled: array of str :param comment: Any comments you want to include about the distribution. :type comment: str :param trusted_signers: Specifies any AWS accounts you want to permit to create signed URLs for private content. If you want the distribution to use signed URLs, this should contain a TrustedSigners object; if you want the distribution to use basic URLs, leave this None. :type trusted_signers: :class`boto.cloudfront.signers.TrustedSigners` :param default_root_object: Designates a default root object. Only include a DefaultRootObject value if you are going to assign a default root object for the distribution. :type comment: str :param logging: Controls whether access logs are written for the distribution. If you want to turn on access logs, this should contain a LoggingInfo object; otherwise it should contain None. :type logging: :class`boto.cloudfront.logging.LoggingInfo` N) connectionoriginenabledcaller_referencestruuiduuid4cnamescommenttrusted_signersloggingdefault_root_object) selfrrrrrrrrrr>/usr/lib/python3/dist-packages/boto/cloudfront/distribution.py__init__#s8 zDistributionConfig.__init__cC d|jS)NzDistributionConfig:%s)rrrrr__repr__j zDistributionConfig.__repr__cCsd}|d7}|jr||j7}|d|j7}|jD]}|d|7}q|jr-|d|j7}|d7}|jr9|d7}n|d7}|d 7}|jra|d 7}|jD]}|d krV|d 7}qK|d |7}qK|d7}|jr||d7}|d|jj7}|d|jj 7}|d7}|j r|j }|d|7}|d7}|S)N' zM ( %s  %s  %s truefalse  Selfz * %s   %s  %s z*%s z ) rto_xmlrrrrrrbucketprefixr)rscnamesignerdrorrrr3ms>     zDistributionConfig.to_xmlcCs\|dkr t|_|jS|dkrt|_|jS|dkr!t|_|jS|dkr,t|_|jSdS)NrLoggingr r )rrr rr rr rnameattrsrrrr startElementszDistributionConfig.startElementcCs|dkr |j|dS|dkr||_dS|dkr)|dkr$d|_dSd|_dS|dkr2||_dS|dkr;||_dSt|||dS) NCNAMECommentEnabledr(TFCallerReferenceDefaultRootObject)rappendrlowerrrrsetattrrr<valuerrrr endElements      zDistributionConfig.endElement) NNFrNrNNN)__name__ __module__ __qualname__rr!r3r>rIrrrrr !s G# r cs,eZdZ   dfdd ZddZZS) StreamingDistributionConfigNrFc s$tt|j||||||||ddS)N)rrrrrrrr)superrMr) rrrrrrrrr __class__rrrs  z$StreamingDistributionConfig.__init__cCsd}|d7}|jr||j7}|d|j7}|jD]}|d|7}q|jr-|d|j7}|d7}|jr9|d7}n|d7}|d 7}|jra|d 7}|jD]}|d krV|d 7}qK|d |7}qK|d7}|jr||d7}|d|jj7}|d|jj 7}|d7}|d7}|S)Nr#zV r$r%r&r'r(r)r*r+r,z r-r.r/r0r1r2z ) rr3rrrrrrr4r5)rr6r7r8rrrr3s8    z"StreamingDistributionConfig.to_xml)NrFrNrNN)rJrKrLrr3 __classcell__rrrOrrMs  rMc@s<eZdZ   dddZddZdd Zd d Zd d ZdS)DistributionSummaryNrFc Cs\||_||_||_||_||_||_| |_g|_|r |j|||_ d|_ d|_ d|_ dS)NF) r domain_nameidlast_modified_timestatusrrrrDrretag streaming) rrrSrTrUrVrr7rrrrrrs  zDistributionSummary.__init__cCr)NzDistributionSummary:%srSr rrrr!r"zDistributionSummary.__repr__cCsF|dkr t|_|jS|dkrt|_|jS|dkr!t|_|jSdS)Nrr r )rrr rr r;rrrr>sz DistributionSummary.startElementcCs|dkr ||_dS|dkr||_dS|dkr||_dS|dkr$||_dS|dkr-||_dS|dkr9|j|dS|dkrB||_dS|dkrV|d krQd |_ dSd |_ dS|d kr_d |_ dSt |||dS) NIdStatusLastModifiedTime DomainNameOriginr?r@rAr(TFStreamingDistributionSummary) rTrVrUrSrrrDrrErrXrFrGrrrrIs*          zDistributionSummary.endElementcC|j|jSN)rget_distribution_inforTr rrrget_distributionz$DistributionSummary.get_distribution) NrrNrNrrF)rJrKrLrr!r>rIrcrrrrrRs  rRc@seZdZddZdS)r_cCr`ra)rget_streaming_distribution_inforTr rrrrcrdz-StreamingDistributionSummary.get_distributionN)rJrKrLrcrrrrr_s r_c@seZdZ  d+ddZddZddZd d Zd,d d Zd dZddZ ddZ ddZ ddZ d-ddZ d-ddZd.ddZ   d/dd Z   d/d!d"Zed#d$Zed,d%d&Zed0d'd(Zed)d*ZdS)1 DistributionNrcCsF||_||_||_||_||_||_d|_d|_d|_d|_ t |_ dS)Nr) rconfigrSrTrUrV in_progress_invalidation_batchesactive_signersrW_bucketr _object_classrrrgrSrTrUrVrrrr"s zDistribution.__init__cCr)NzDistribution:%srYr rrrr!0r"zDistribution.__repr__cCs0|dkr t|_|jS|dkrt|_|jSdS)Nr r)r rgrrir;rrrr>3szDistribution.startElementcCsn|dkr ||_dS|dkr||_dS|dkr||_dS|dkr&t||_dS|dkr/||_dSt|||dS)NrZr\r[InProgressInvalidationBatchesr])rTrUrVintrhrSrFrGrrrrI=s    zDistribution.endElementc Cst|j|jj|jj|jj|jj|jj|jj|jj }|dur!||_|dur(||_|dur/||_|j |j |j ||_ ||_t |_dS)a Update the configuration of the Distribution. The only values of the DistributionConfig that can be directly updated are: * CNAMES * Comment * Whether the Distribution is enabled or not Any changes to the ``trusted_signers`` or ``origin`` properties of this distribution's current config object will also be included in the update. Therefore, to set the origin access identity for this distribution, set ``Distribution.config.origin.origin_access_identity`` before calling this update method. :type enabled: bool :param enabled: Whether the Distribution is active or not. :type cnames: list of str :param cnames: The DNS CNAME's associated with this Distribution. Maximum of 10 values. :type comment: str or unicode :param comment: The comment associated with the Distribution. N)r rrgrrrrrrrset_distribution_configrTrWrrkrrrr new_configrrrupdateKs    zDistribution.updatecC|jdddS)ze Activate the Distribution. A convenience wrapper around the update method. TrNrrr rrrenabletzDistribution.enablecCrs)zg Deactivate the Distribution. A convenience wrapper around the update method. FrtNrur rrrdisable{rwzDistribution.disablecCs|j|j|jdS)z Delete this CloudFront Distribution. The content associated with the Distribution is not deleted from the underlying Origin bucket in S3. N)rdelete_distributionrTrWr rrrdeleteszDistribution.deletecCst|jjtrE|jsB|jjj}|dd}ddlm}||j j |j j |j j |j j |j j|j jd}|||_||j_|j|j|jStd)Nz.s3.amazonaws.comrr) S3Connection)proxy proxy_port proxy_user proxy_passz%Unable to get_objects on CustomOrigin) isinstancergrr rjdns_namereplaceboto.s3.connectionr{raws_access_key_idaws_secret_access_keyr|r}r~r get_bucket distribution set_key_classrkNotImplementedError)rbucket_dns_name bucket_namer{s3rrr _get_buckets"    zDistribution._get_bucketcCs$|}g}|D]}||q|S)z Return a list of all content objects in this distribution. :rtype: list of :class:`boto.cloudfront.object.Object` :return: The content objects )rrD)rr4objskeyrrr get_objectss  zDistribution.get_objectsFcCs|t|jjtr<|jjjr5|jjjdd}|j|}|}|r&t |_ |j d|j | |dS|ddSdS)as Sets the S3 ACL grants for the given object to the appropriate value based on the type of Distribution. If the Distribution is serving private content the ACL will be set to include the Origin Access Identity associated with the Distribution. If the Distribution is serving public content the content will be set up with "public-read". :type object: :class:`boto.cloudfront.object.Object` :param enabled: The Object whose ACL is being set :type replace: bool :param replace: If False, the Origin Access Identity will be appended to the existing ACL for the object. If True, the ACL for the object will be completely replaced with one that grants READ permission to the Origin Access Identity. /READ public-readN)rrgrr origin_access_identitysplitrget_origin_access_identity_infoget_aclr acladd_user_grant s3_user_idset_aclset_canned_acl)robjectrrToaipolicyrrrset_permissionss  zDistribution.set_permissionscCs"|}|D]}|||qdS)a Sets the S3 ACL grants for all objects in the Distribution to the appropriate value based on the type of Distribution. :type replace: bool :param replace: If False, the Origin Access Identity will be appended to the existing ACL for the object. If True, the ACL for the object will be completely replaced with one that grants READ permission to the Origin Access Identity. N)rr)rrr4rrrrset_permissions_alls z Distribution.set_permissions_allTcCsP|jjjrd}nd}|}||}|j|||d|jjjr&||||S)a  Adds a new content object to the Distribution. The content for the object will be copied to a new Key in the S3 Bucket and the permissions will be set appropriately for the type of Distribution. :type name: str or unicode :param name: The name or key of the new object. :type content: file-like object :param content: A file-like object that contains the content for the new object. :type headers: dict :param headers: A dictionary containing additional headers you would like associated with the new object in S3. :rtype: :class:`boto.cloudfront.object.Object` :return: The newly created object. privater)headersr)rgrrrnew_keyset_contents_from_filer)rr<contentrrrr4rrrr add_objects    zDistribution.add_objectc  Csr|j||||||||d} d|vrd} nd} g} dD]} | | vr-d| | | f} | | q|| d| }|S)a Creates a signed CloudFront URL that is only valid within the specified parameters. :type url: str :param url: The URL of the protected object. :type keypair_id: str :param keypair_id: The keypair ID of the Amazon KeyPair used to sign theURL. This ID MUST correspond to the private key specified with private_key_file or private_key_string. :type expire_time: int :param expire_time: The expiry time of the URL. If provided, the URL will expire after the time has passed. If not provided the URL will never expire. Format is a unix epoch. Use int(time.time() + duration_in_sec). :type valid_after_time: int :param valid_after_time: If provided, the URL will not be valid until after valid_after_time. Format is a unix epoch. Use int(time.time() + secs_until_valid). :type ip_address: str :param ip_address: If provided, only allows access from the specified IP address. Use '192.168.0.10' for a single IP or use '192.168.0.0/24' CIDR notation for a subnet. :type policy_url: str :param policy_url: If provided, allows the signature to contain wildcard globs in the URL. For example, you could provide: 'http://example.com/media/\*' and the policy and signature would allow access to all contents of the media subdirectory. If not specified, only allow access to the exact url provided in 'url'. :type private_key_file: str or file object. :param private_key_file: If provided, contains the filename of the private key file used for signing or an open file object containing the private key contents. Only one of private_key_file or private_key_string can be provided. :type private_key_string: str :param private_key_string: If provided, contains the private key string used for signing. Only one of private_key_file or private_key_string can be provided. :rtype: str :return: The signed URL. )url keypair_id expire_timevalid_after_time ip_address policy_urlprivate_key_fileprivate_key_string?&)ExpiresPolicy Signature Key-Pair-Idz%s=%s)_create_signing_paramsrDjoin)rrrrrrrrrparamssepsigned_url_paramsrparam signed_urlrrrcreate_signed_urls"8 zDistribution.create_signed_urlc Csi} |r|s|s|s|||} t|| d<n|dur|}|j||||d} || } | | d<|| ||} || } | | d<|| d<| S)zG Creates the required URL parameters for a signed URL. rN)expires valid_afterrrrr)_canned_policyr_custom_policy_url_base64_encode _sign_string)rrrrrrrrrrrencoded_policy signatureencoded_signaturerrrrGs"   z#Distribution._create_signing_paramscCsdt}|S)z1 Creates a canned policy string. zf{"Statement":[{"Resource":"%(resource)s","Condition":{"DateLessThan":{"AWS:EpochTime":%(expires)s}}}]})locals)resourcerrrrrrhszDistribution._canned_policycCsri}|s ttd}d|i|d<|rd|i|d<|r*d|vr$|d7}d|i|d<d ||d gi}tj|d d S) zR Creates a custom policy string based on the supplied parameters. iQz AWS:EpochTime DateLessThanDateGreaterThanrz/32z AWS:SourceIp IpAddress Statement)Resource Condition),:) separators)rntimerdumps)rrrr conditionrrrrrrs    zDistribution._custom_policycCszddl}Wn tytdw|r|rtd|s"|s"td|durJt|tjrFt|d }|}Wdn1s@wYn|}|j |}| t ||d}|S)zo Signs a string for use with Amazon CloudFront. Requires the rsa library be installed. rNzMBoto depends on the python rsa library to generate signed URLs for CloudFrontzDOnly specify the private_key_file or the private_key_string not bothz>You must specify one of private_key_file or private_key_stringrzSHA-1) rsa ImportErrorr ValueErrorrr string_typesopenread PrivateKey load_pkcs1signr)messagerrr file_handle private_keyrrrrrs&      zDistribution._sign_stringcCs2t|}|dd}|dd}|dd}|S)zd Base64 encodes a string using the URL-safe characters specified by Amazon. +-=_r~)base64 b64encoder)msg msg_base64rrrrs    zDistribution._url_base64_encodeNNrrNrNNN)F)NT)NNNNNN)NN)rJrKrLrr!r>rIrrrvrxrzrrrrrrr staticmethodrrrrrrrrrf s@  ) " L !   rfcs@eZdZ  d fdd ZfddZd ddZd d ZZS) StreamingDistributionNrcs$tt|||||||t|_dSra)rNrrrrkrlrOrrrs zStreamingDistribution.__init__cs*|dkr t|_|jStt||||S)NrM)rMrgrNrr>r;rOrrr>s z"StreamingDistribution.startElementcCs~t|j|jj|jj|jj|jj|jj|jj}|dur||_|dur%||_|dur,||_|j |j |j ||_ ||_t |_ dS)a Update the configuration of the StreamingDistribution. The only values of the StreamingDistributionConfig that can be directly updated are: * CNAMES * Comment * Whether the Distribution is enabled or not Any changes to the ``trusted_signers`` or ``origin`` properties of this distribution's current config object will also be included in the update. Therefore, to set the origin access identity for this distribution, set ``StreamingDistribution.config.origin.origin_access_identity`` before calling this update method. :type enabled: bool :param enabled: Whether the StreamingDistribution is active or not. :type cnames: list of str :param cnames: The DNS CNAME's associated with this Distribution. Maximum of 10 values. :type comment: str or unicode :param comment: The comment associated with the Distribution. N)rMrrgrrrrrr!set_streaming_distribution_configrTrWrrkrprrrrrs(  zStreamingDistribution.updatecCs|j|j|jdSra)rdelete_streaming_distributionrTrWr rrrrzszStreamingDistribution.deleterr)rJrKrLrr>rrrzrQrrrOrrs  .r)rrr boto.compatrrboto.cloudfront.identityrboto.cloudfront.objectrrboto.cloudfront.signersrrboto.cloudfront.loggingr boto.cloudfront.originr r boto.s3.aclr rr rMrRr_rfrrrrrs(   +?