ó
Òbc           @@  sÂ  d  d l  m Z d  d l Z d  d l Z y d  d l m Z Wn! e k
 r_ d  d l m Z n Xd d l m	 Z	 d  d l
 m Z d d l m Z m Z m Z d d	 l m Z m Z m Z d d
 l m Z d  d l Z d  d l m Z m Z g  Z e d d „ Z e j d ƒ d „  Z e j d ƒ d d d d d „ Z e j d ƒ d „  Z  e j d ƒ d „  Z! d „  Z" d „  Z# d d d d d d d d „ Z$ e j d ƒ d  d d „ Z% d  d d „ Z& d S(   i    (   t   absolute_importN(   t   encodebytes(   t   encodestringi   (   t   Config(   t   debug(   t   encode_to_s3t   decode_from_s3t   s3_quote(   t   time_to_epocht   deunicodiset   check_bucket_name_dns_support(   t
   SortedDict(   t   sha1t   sha256c         C@  sÆ   |  s
 d Sd } | r d p d } x t  |  j ƒ  ƒ D]{ } | rS | | k rS q5 n  |  | } | d	 k rŒ | d t | d t ƒ| f 7} q5 | d | t |  | d t ƒf 7} q5 W| oÅ d | d S(
   sª  
    Format URL parameters from a params dict and returns
    ?parm1=val1&parm2=val2 or an empty string if there
    are no parameters.  Output of this function should
    be appended directly to self.resource['uri']
    - Set "always_have_equal" to always have the "=" char for a param even when
    there is no value for it.
    - Set "limited_keys" list to restrict the param string to keys that are
    defined in it.
    t    u   =s   &%s%st   unicode_outputs   &%s=%st   ?i   N(   NR   (   t   sortedt   keyst   NoneR   t   True(   t   paramst   always_have_equalt   limited_keyst	   param_strt	   equal_strt   keyt   value(    (    sI   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/Crypto.pyt   format_param_str    s    
#(R   c         C@  s@   t  j  ƒ  j } t t j t | ƒ |  t ƒ j ƒ  ƒ j ƒ  } | S(   s{  Sign a string with the secret key, returning base64 encoded results.
    By default the configured secret key is used, but may be overridden as
    an argument.

    Useful for REST authentication. See http://s3.amazonaws.com/doc/s3-developer-guide/RESTAuthentication.html
    string_to_sign should be utf-8 "bytes".
    and returned signature will be utf-8 encoded "bytes".
    (	   R   t
   secret_keyR   t   hmact   newR   R   t   digestt   strip(   t   string_to_signR   t	   signature(    (    sI   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/Crypto.pyt   sign_string_v2<   s    	-R$   t   GETt   /c         C@  sØ  d d d d d d d d d	 d
 d d d d d d d d g } | d" k rZ t d t ƒ } n  t j ƒ  j } |  d } | | j d d ƒ d 7} | | j d d ƒ d 7} | | j d d ƒ d 7} xr t | j ƒ  ƒ D]^ } | j d ƒ r| | d | | d 7} n  | j d ƒ rÔ | | d | | d 7} qÔ qÔ Wt	 | d t
 d t ƒ} t | d | ƒ} | | 7} | | 7} t d t | ƒ ƒ t t t | ƒ ƒ ƒ }	 t t | j ƒ  ƒ d t ƒ}
 d  | d |	 |
 d! <|
 S(#   sA  Sign a string with the secret key, returning base64 encoded results.
    By default the configured secret key is used, but may be overridden as
    an argument.

    Useful for REST authentication. See http://s3.amazonaws.com/doc/s3-developer-guide/RESTAuthentication.html
    string_to_sign should be utf-8 "bytes".
    t   aclt	   lifecyclet   locationt   loggingt   notificationt
   partNumbert   policyt   requestPaymentt   torrentt   uploadIdt   uploadst	   versionIdt
   versioningt   versionst   websitet   deletet   corst   restoret   ignore_cases   
s   content-md5R   s   content-typet   dates   x-amz-t   :s   x-emc-t   quote_backslashesR   R   s   SignHeaders: s   AWS t   AuthorizationN(   R   R   R   R   t
   access_keyt   getR   R   t
   startswithR   t   FalseR   R   t   reprR   R$   R   t   listt   items(   t   methodt   canonical_uriR   t   cur_headerst   SUBRESOURCES_TO_INCLUDER>   R"   t   headert   canonical_querystringR#   t   new_headers(    (    sI   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/Crypto.pyt   sign_request_v2J   s6    				
!

RL   c         C@  s%   t  d |  j ƒ  d |  j ƒ  d | ƒ S(   sE  Sign a URL in s3://bucket/object form with the given expiry
    time. The object will be accessible via the signed URL until the
    AWS key and secret are revoked or the expiry time is reached, even
    if the object is otherwise private.

    See: http://s3.amazonaws.com/doc/s3-developer-guide/RESTAuthentication.html
    t   buckett   objectt   expiry(   t   sign_url_base_v2RM   RN   (   t   url_to_signRO   (    (    sI   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/Crypto.pyt   sign_url_v2}   s    RR   c          K@  sÊ  t  j  ƒ  j } t  j  ƒ  j } t |  d ƒ |  d <t  j  ƒ  j |  d <t  j  ƒ  j |  d <t |  d d t d t ƒ|  d <d |  d <t  j  ƒ  j	 rž d	 |  d <n  t
 d
 |  d ƒ d |  } d } | rà | | d | 7} d } n  | r| | d | 7} d } n  t
 d | ƒ t t t | ƒ ƒ d t ƒ|  d <t
 d |  d ƒ t t  j  ƒ  j |  d ƒ rfd } n d } | d 7} | |  } | r£| d t | d t ƒ7} n  | rÆ| d t | d t ƒ7} n  | S(   sc   Shared implementation of sign_url methods. Takes a hash of 'bucket', 'object' and 'expiry' as args.RO   R>   t	   host_baseRN   R<   R   t   httpt   protot   httpss#   Expiry interpreted as epoch time %ss'   GET


%(expiry)d
/%(bucket)s/%(object)sR   s   response-content-disposition=t   &s   response-content-type=s   Signing plaintext: %rt   sigs   Urlencoded signature: %sRM   s/   %(proto)s://%(bucket)s.%(host_base)s/%(object)ss/   %(proto)s://%(host_base)s/%(bucket)s/%(object)ssC   ?AWSAccessKeyId=%(access_key)s&Expires=%(expiry)d&Signature=%(sig)ss   &response-content-disposition=s   &response-content-type=(   R   t   content_dispositiont   content_typeR   R>   RS   R   RA   R   t   signurl_use_httpsR   R$   R   R
   t   host_bucket(   t   parmsRY   RZ   t   signtextt   param_separatort   url(    (    sI   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/Crypto.pyRP   Œ   s>     

		"	

c         C@  s   t  j |  t | ƒ t ƒ j ƒ  S(   N(   R   R   R   R   R    (   R   t   msg(    (    sI   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/Crypto.pyt   sign¯   s    c         C@  sJ   t  t d |  ƒ | ƒ } t  | | ƒ } t  | | ƒ } t  | d ƒ } | S(   s1   
    Input: unicode params
    Output: bytes
    t   AWS4t   aws4_request(   Rb   R   (   R   t	   dateStampt
   regionNamet   serviceNamet   kDatet   kRegiont   kServicet   kSigning(    (    sI   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/Crypto.pyt   getSignatureKey²   s
    R   s	   us-east-1c         C@  s;  d } | d  k r$ t d t ƒ } n  t j ƒ  } | j }	 | j }
 t j j ƒ  } | j d ƒ } | j d ƒ } t	 |
 | | | ƒ } t
 | d t d t ƒ} t | d t ƒj d ƒ } t | ƒ t t d	 ƒ ƒ k rê t | j ƒ  ƒ } n t t t | ƒ ƒ j ƒ  ƒ } i | d
 6| d 6| d 6} d } xi | j ƒ  D][ } | d k s6| | j d ƒ k rcq6n  | | j ƒ  | | j ƒ  <| d | j ƒ  7} q6Wd	 } x6 t | j ƒ  ƒ D]" \ } } | | d | d 7} q®W| } t d | ƒ d j t | j d ƒ ƒ ƒ } |  d | d | d | d | d | } t d | ƒ d } | d | d | d d } | d | d | d t t t | ƒ ƒ j ƒ  ƒ } t t j | t | ƒ t ƒ j ƒ  ƒ } | d d |	 d | d d | d d | } t | j ƒ  ƒ } | j i | d 6| d 6| d 6ƒ t d | ƒ | S(   Nt   s3R9   s   %Y%m%dT%H%M%SZs   %Y%m%dR<   R   R   R   R   t   hosts   x-amz-content-sha256s
   x-amz-dates$   host;x-amz-content-sha256;x-amz-dateR=   t   ;R;   s   
u   canonical_headers = %ss,   Canonical Request:
%s
----------------------s   AWS4-HMAC-SHA256R&   Rd   t    s   Credential=t   ,s   SignedHeaders=s
   Signature=s   signature-v4 headers: %s(   R   R   R   R   R>   R   t   datetimet   utcnowt   strftimeRl   R   RA   R   t   lstript   typeR   R   t	   hexdigestR   R   t   splitR!   R   RD   R   t   joinR   R   t   update(   RE   Rn   RF   R   t   regionRG   t   bodyt   servicet   cfgR>   R   t   tt   amzdatet	   datestampt   signing_keyRJ   t   payload_hasht   canonical_headerst   signed_headersRI   t   canonical_headers_strt   kt   vt   canonical_requestt	   algorithmt   credential_scopeR"   R#   t   authorization_headerRK   (    (    sI   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/Crypto.pyt   sign_request_v4½   sV    		

!.6'2R   c         @  só   y t  ƒ  } Wn t k
 r, t  j ƒ  } n Xt t |  ƒ d ƒ « ‰  | d  k r„ x• t ‡  f d †  d ƒ D] } | j | ƒ qj Wne ˆ  j | ƒ | } xO | d k rè ˆ  j	 t
 d | ƒ ƒ } | sÈ Pn  | t | ƒ 8} | j | ƒ qš WWd  QX| S(   Nt   rbc           @  s   ˆ  j  d ƒ S(   Ni    (   t   read(    (   t   f(    sI   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/Crypto.pyt   <lambda>  s    R   i    i    (   R   t	   ExceptionR   t   openR	   R   t   iterRz   t   seekR   t   mint   len(   t   filenamet   offsett   sizet   hasht   chunkt	   size_left(    (   R   sI   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/Crypto.pyt   checksum_sha256_fileþ   s"    c         C@  se   y t  ƒ  } Wn t k
 r, t  j ƒ  } n X| d  k rI | j |  ƒ n | j |  | | | !ƒ | S(   N(   R   R’   R   R   Rz   (   t   bufferR™   Rš   R›   (    (    sI   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/Crypto.pyt   checksum_sha256_buffer  s    ('   t
   __future__R    t   sysR   t   base64R   R   t   ImportErrorR   R   R*   R   t	   BaseUtilsR   R   R   t   UtilsR   R	   R
   R   Rr   t   hashlibR   R   t   __all__RA   R   R   t   appendR$   RL   RR   RP   Rb   Rl   R   Rž   R    (    (    (    sI   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/Crypto.pyt   <module>	   s<   	1		#		>