
"T`09                 @   s  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 l	 m Z d  d l	 m Z d  d	 l m Z m Z d  d
 l	 m Z e j e  Z e
 j Z e
 j Z e
 j Z d Z d d g Z d d g Z d g Z d Z d Z d Z d d d d d d d d g Z d Z  d g Z! d d   Z" d d   Z# d d  d!  Z$ Gd" d#   d# e%  Z& d$ d%   Z' e d& d'  Z( d S)(    N)MIMEBase)MIMEMultipart)MIMENonMultipart)MIMEText)handlers)log)features)read_file_or_urlUrlError)utilzContent-Typez
text/plainztext/x-not-multipartztext/x-include-urlztext/x-include-once-urlztext/cloud-config-archiveztext/cloud-configzapplication/octet-streamzapplication/gzipzapplication/gzip-compressedzapplication/gzippedzapplication/x-compresszapplication/x-compressedzapplication/x-gunzipzapplication/x-gzipzapplication/x-gzip-compressedzNumber-Attachmentsc             C   s   |  | =| |  | <d  S)N )msgkeyvaluer   r   5/usr/lib/python3/dist-packages/cloudinit/user_data.py_replace_header=   s    r   c             C   s'   |  d =|  j  d d d t |  d  S)NzContent-Disposition
attachmentfilename)
add_headerstr)r   r   r   r   r   _set_filenameB   s    	r   c             C   s,   t  j r t |   |  n t j |   d  S)N)r   ZERROR_ON_USER_DATA_FAILURE	ExceptionLOGwarning)error_messageZsource_exceptionr   r   r   _handle_errorH   s    	r   c               @   s   e  Z d  Z d d   Z d d   Z d d   Z d d   Z d	 d
   Z d d   Z d d   Z	 d d   Z
 d d d  Z d d   Z d S)UserDataProcessorc             C   s   | |  _  t j |  |  _ d  S)N)pathsr   fetch_ssl_detailsssl_details)selfr   r   r   r   __init__P   s    	zUserDataProcessor.__init__c             C   s\   t    } t | t  rB x= | D] } |  j t |  |  q Wn |  j t |  |  | S)N)r   
isinstancelist_process_msgconvert_string)r    blobZaccumulating_msgbr   r   r   processT   s    	zUserDataProcessor.processc             C   s1  d d   } x| j    D]} t |  r. q d  } | j   } t j |  } d } | t k r y% t j | d d } d  } d } WnS t j k
 r }	 z0 d j | t	 |  |	  }
 t
 |
 |	  w WYd  d  }	 ~	 Xn X| s t } | t d g k r| |  } | d  k r| } | r| j d d	  \ } } t | |  } | j |  | j   rrt | | j    x4 d D], } | | k ryt | | t | |   qyW| } | | k rt | t |  | t k r|  j | |  q | t k r	|  j | |  q t | t |  |  j | |  q Wd  S)Nc             S   s   t  j |   S)N)r   type_from_starts_with)payloadr   r   r   
find_ctype_   s    z2UserDataProcessor._process_msg.<locals>.find_ctypeFquietTz<Failed decompressing payload from {} of length {} due to: {}ztext/x-shellscript/   Launch-Index)r/   )walkis_skippableget_content_typer   fully_decoded_payloadDECOMP_TYPESdecomp_gzipDecompressionErrorformatlenr   
UNDEF_TYPETYPE_NEEDEDsplitr   set_payloadget_filenamer   r   r   CONTENT_TYPEINCLUDE_TYPES_do_includeARCHIVE_TYPES_explode_archive_attach_part)r    Zbase_msg
append_msgr+   partctypeZ
ctype_origr*   Zwas_compresseder   maintypesubtypeZn_parthr   r   r   r$   ]   sX    
zUserDataProcessor._process_msgc             C   s   | j  d d   } d  } | j   t k rs y4 t j | j d d   } | r] | j  d  } Wn t k
 rr Yn X| d  k	 r | } | d  k r | } | d  k	 r y  | j d t t	 |    Wn t
 t f k
 r Yn Xd  S)NzLaunch-IndexdecodeTzlaunch-index)getr2   EXAMINE_FOR_LAUNCH_INDEXr   	load_yamlget_payloadr   r   r   int
ValueError	TypeError)r    r   Z
header_idxZpayload_idxr*   r   r   r   _attach_launch_index   s$     z&UserDataProcessor._attach_launch_indexc             C   s7   t  j | d d  } t j j |  j j d  d |  S)Nmd5@   dataZurlcache)r   	hash_blobospathjoinr   get_ipath_cur)r    entryZentry_fnr   r   r   _get_include_once_filename   s    z,UserDataProcessor._get_include_once_filenamec             C   s.   | j    s t | t |  |  j |  d  S)N)r=   r   PART_FN_TPLrS   )r    r   Zattached_idr   r   r   _process_before_attach   s    z(UserDataProcessor._process_before_attachc             C   sV  d } xI| j    D];} | j   } | j d  rY | t d  d   j   } d } n1 | j d  r | t d  d   j   } d } | j d  r q | j   } | s q d  } d  } | r |  j |  } | r t j j	 |  r t
 j |  } n*y t | d d d d	 d
 |  j } | rK| j   rKt
 j | | j d d | j   rc| j } n d j | | j  }	 t |	  Wn t k
 r}
 z= t |
  } | | k r| d j |  7} t | |
  WYd  d  }
 ~
 XnD t k
 r%} z$ d j | |  }	 t |	 |  WYd  d  } ~ Xn X| d  k	 r t |  } |  j | |  q Wd  S)NFz#include-onceTz#include#timeout   retries
   r   modei  z6Fetching from {} resulted in a invalid http code of {}z for url: {0}zFetching from {} resulted in {})
splitlineslower
startswithr8   lstripstripr]   rX   rY   isfiler   	load_filer	   r   ok
write_filecontentsr7   coder   r
   r   IOErrorr%   r$   )r    contentrD   Zinclude_once_onlineZlc_lineZinclude_urlZinclude_once_fnrespr   ZurlemessageZioeZnew_msgr   r   r   r@      sV    	 zUserDataProcessor._do_includec          
   C   s  t  j | d g  d t t f } x| D]} t | t  rI d | i } t | t  s[ q( | j d d  } | j d  } | s t } t | t	  r t
 } t j | |  } | j d d  \ } }	 | d k rt | t	  r | j   } t | d	 |	 }
 n t | |	  }
 |
 j |  d
 | k r<t |
 | d
  d | k rb|
 j d t | d   xd t | j    D]P } | j   d d
 d d d t j   t j   f k rqu|
 j | | |  quW|  j | |
  q( Wd  S)Ndefaultallowedrr    typer-   r.   text_subtyper   zlaunch-indexzLaunch-Indexzcontent-disposition)r   rN   r#   setr"   r   dictrL   ARCHIVE_UNDEF_TYPEbytesARCHIVE_UNDEF_BINARY_TYPEr   r)   r;   rK   r   r   r<   r   r   keysrg   ATTACHMENT_FIELDr>   rC   )r    archiverD   entriesentrr   mtyperv   rH   rI   r   headerr   r   r   rB     s@    !	z"UserDataProcessor._explode_archiveNc             C   s   t  | k r d | t  <| d k	 r8 t | t  t |   d } y t | j t    } Wn. t t f k
 r t | t  t |   Yn X| S)z
        Return the number of attachments to this MIMEMultipart by looking
        at its 'Number-Attachments' header.
        0Nr   )r   r   r   rP   rL   rQ   rR   )r    	outer_msg	new_countZfetched_countr   r   r   _multi_part_count;  s    
z#UserDataProcessor._multi_part_countc             C   sH   |  j  |  } |  j | | d  | j |  |  j  | | d  d S)z
        Attach a message to an outer message. outermsg must be a MIMEMultipart.
        Modifies a header in the outer message to keep track of number of
        attachments.
        r.   N)r   r_   attach)r    r   rE   
part_countr   r   r   rC   M  s    zUserDataProcessor._attach_part)__name__
__module____qualname__r!   r(   r$   rS   r]   r_   r@   rB   r   rC   r   r   r   r   r   O   s   	P=/r   c             C   s,   |  j    p d } | j   d k r( d Sd S)Nrx   	multipartTF)get_content_maintyperg   )rE   Zpart_maintyper   r   r   r1   Y  s    r1   c             C   s   |  s d }  d d   } t  |  t  r9 |  j d  } n |  } t j | d d } d | d d	  j   k r t j | j d   } n | | |  } | S)
zJconvert a string (more likely bytes) or a message into
    a mime message.    c             S   s8   | j  d d  \ } } t | |  } | j |   | S)Nr-   r.   )r;   r   r<   )rV   content_typerH   rI   r   r   r   r   create_binmsgh  s    z%convert_string.<locals>.create_binmsgzutf-8rK   Fs   mime-version:r   i   )r"   r   encoder   r5   rg   message_from_stringrK   )Zraw_datar   r   Zbdatar   r   r   r   r%   b  s    r%   ))rX   Zemail.mime.baser   Zemail.mime.multipartr   Zemail.mime.nonmultipartr   Zemail.mime.textr   	cloudinitr   r   loggingr   Zcloudinit.url_helperr	   r
   r   	getLoggerr   r   ZNOT_MULTIPART_TYPEr^   Z
OCTET_TYPEr>   r:   r?   rA   r9   r~   r   r4   r   rM   r   r   r   objectr   r1   r%   r   r   r   r   <module>   sJ   						 	