
­­­­­­­­­­­­­­­­­­
<!DOCTYPE html>
<html>
3
\me9                 @   s  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mZmZ d dlmZ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#G dd  d Z$d!d" Z%efd#d$Z&dS )&    N)MIMEBase)MIMEMultipart)MIMENonMultipart)MIMEText)featureshandlersutil)UrlErrorread_file_or_urlz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   /usr/lib/python3.6/user_data.py_replace_header;   s    r   c             C   s   | d= | j ddt|d d S )NzContent-DispositionZ
attachment)filename)
add_headerstr)r   r   r   r   r   _set_filename@   s    r   c             C   s    t jrt| |n
tj|  d S )N)r   ZERROR_ON_USER_DATA_FAILURERuntimeErrorLOGZwarning)error_messageZsource_exceptionr   r   r   _handle_errorE   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   Zfetch_ssl_detailsssl_details)selfr   r   r   r   __init__M   s    zUserDataProcessor.__init__c             C   sD   t  }t|tr0x.|D ]}| jt|| qW n| jt|| |S )N)r   
isinstancelist_process_msgconvert_string)r   ZblobZaccumulating_msgbr   r   r   processQ   s    

zUserDataProcessor.processc             C   s  dd }x|j  D ]}t|r$qd }|j }tj|}d}|tkrytj|dd}d }d}W nB tjk
r }	 z$dj|t	||	}
t
|
|	 wW Y d d }	~	X nX |st}|tdg kr||}|d kr|}|rB|jdd	\}}t||}|j| |j rt||j  x,dD ]$}||krt||t||  qW |}||krXt|t| |tkrp| j|| q|tkr| j|| qt|t| | j|| qW d 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   Zfully_decoded_payloadDECOMP_TYPESdecomp_gzipZ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&   partZctypeZ
ctype_origr%   Zwas_compresseder   maintypesubtypeZn_parthr   r   r   r    Z   sX    









zUserDataProcessor._process_msgc             C   s   |j dd }d }|j tkrVy$tj|jdd}|r>|j d}W n tk
rT   Y nX |d k	rb|}|d krn|}|d k	ry|jdtt	| W n t
tfk
r   Y nX d S )NzLaunch-IndexT)decodezlaunch-index)getr-   EXAMINE_FOR_LAUNCH_INDEXr   	load_yamlZget_payload	Exceptionr   r   int
ValueError	TypeError)r   r   Z
header_idxZpayload_idxr%   r   r   r   _attach_launch_index   s$    z&UserDataProcessor._attach_launch_indexc             C   s&   t j|dd}tjj| jjdd|S )NZmd5@   dataZurlcache)r   Z	hash_blobospathjoinr   Z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)r6   r   PART_FN_TPLrK   )r   r   Zattached_idr   r   r   _process_before_attach   s    z(UserDataProcessor._process_before_attachc             C   s  d}x|j  D ]}|j }|jdrB|tdd  j }d}n"|jdrd|tdd  j }d}|jdrpq|j }|s~qd }d }|r| j|}|rtjj	|rt
j|}nyZt|dd| jd}|r|j rt
j||jd	d
 |j r|j}ndj||j}	t|	 W n tk
rX }
 z0t|
}||kr>|dj|7 }t||
 W Y d d }
~
X n: tk
r } zdj||}	t|	| W Y d d }~X nX |d k	rt|}| j|| qW d S )NFz#include-onceTz#include#   
   )ZtimeoutZretriesr   i  )modez6Fetching from {} resulted in a invalid http code of {}z for url: {0}zFetching from {} resulted in {})
splitlineslower
startswithr1   lstripstriprR   rN   rO   isfiler   Z	load_filer
   r   okZ
write_filecontentsr0   coder   r	   r   IOErrorr!   r    )r   contentr=   Zinclude_once_onlineZlc_lineZinclude_urlZinclude_once_fnZrespr   ZurlemessageZioeZnew_msgr   r   r   r9      s\    






zUserDataProcessor._do_includec          
   C   sZ  t j|g ttfd}x>|D ]4}t|tr4d|i}t|ts@q|jdd}|jd}|sxt}t|t	rlt
}tj||}|jdd\}}	|dkrt|t	r|j }t||	d}
nt||	}
|
j| d	|krt|
|d	  d
|kr|
jdt|d
  xNt|j D ]>}|j dd	dd
dtj tj fkr0q|
j|||  qW | j||
 qW d S )N)defaultZallowedrc    typer(   r)   text)Z_subtyper   zlaunch-indexzLaunch-Indexzcontent-disposition)r   rF   r   setr   r   dictrD   ARCHIVE_UNDEF_TYPEbytesARCHIVE_UNDEF_BINARY_TYPEr   r$   r4   rC   r   r   r5   r   r   keysrZ   ATTACHMENT_FIELDr7   r<   )r   archiver=   ZentriesZentrc   Zmtyperf   r@   rA   r   headerr   r   r   r;     sH    






z"UserDataProcessor._explode_archiveNc             C   sl   t |krd|t < |dk	r(t|t t| d}yt|jt }W n( ttfk
rf   t|t t| Y nX |S )z
        Return the number of attachments to this MIMEMultipart by looking
        at its 'Number-Attachments' header.
        0Nr   )rp   r   r   rH   rD   rI   rJ   )r   	outer_msgZ	new_countZfetched_countr   r   r   _multi_part_countF  s    z#UserDataProcessor._multi_part_countc             C   s8   | 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)ru   rT   Zattach)r   rt   r>   Z
part_countr   r   r   r<   X  s    

zUserDataProcessor._attach_part)N)__name__
__module____qualname__r   r#   r    rK   rR   rT   r9   r;   ru   r<   r   r   r   r   r   L   s   	PE4
r   c             C   s    | j  p
d}|j dkrdS dS )Nrg   Z	multipartTF)Zget_content_maintyperZ   )r>   Zpart_maintyper   r   r   r,   d  s    r,   c             C   sl   | sd} dd }t | tr&| 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   s(   |j dd\}}t||}|j|  |S )Nr(   r)   )r4   r   r5   )rM   content_typer@   rA   r   r   r   r   create_binmsgs  s    

z%convert_string.<locals>.create_binmsgzutf-8F)rC   s   mime-version:r   i   )r   r   encoder   r/   rZ   Zmessage_from_stringrC   )Zraw_datarz   r{   Zbdatar   r   r   r   r!   m  s    

r!   )N)'ZloggingrN   Zemail.mime.baser   Zemail.mime.multipartr   Zemail.mime.nonmultipartr   Zemail.mime.textr   Z	cloudinitr   r   r   Zcloudinit.url_helperr	   r
   Z	getLoggerrv   r   ZNOT_MULTIPART_TYPErS   Z
OCTET_TYPEr7   r3   r8   r:   r2   rl   rn   r.   rp   rE   r   r   r   r   r,   r!   r   r   r   r   <module>   sH   

  	