o
    x[hÀ9  ã                   @   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  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g d¢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-stream)z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t|ƒd� d S )NzContent-DispositionÚ
attachment)Úfilename)Ú
add_headerÚstr)r   r   r   r   r   Ú_set_filename@   s   r   c                 C   s   t jrt| ƒ|‚t | ¡ d S r   )r   ÚERROR_ON_USER_DATA_FAILUREÚRuntimeErrorÚLOGÚwarning)Úerror_messageÚ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 |¡| _d S r   )Úpathsr   Úfetch_ssl_detailsÚssl_details)Úselfr   r   r   r   Ú__init__M   s   zUserDataProcessor.__init__c                 C   sB   t ƒ }t|tƒr|D ]
}|  t|ƒ|¡ q
|S |  t|ƒ|¡ |S r   )r   Ú
isinstanceÚlistÚ_process_msgÚconvert_string)r"   ÚblobÚaccumulating_msgÚbr   r   r   ÚprocessQ   s   
ÿzUserDataProcessor.processc                 C   sŽ  dd„ }|  ¡ D ]¼}t|ƒrqd }| ¡ }t |¡}d}|tv rOztj|dd�}d }d}W n! tjyN }	 zd |t	|ƒ|	¡}
t
|
|	ƒ W Y d }	~	qd }	~	ww |sSt}|tdg v r^||ƒ}|d u rd|}|r˜| dd	¡\}}t||ƒ}| |¡ | ¡ rƒt|| ¡ ƒ d
D ]}||v r•t||t|| ƒƒ q…|}||kr¢t|t|ƒ |tv r­|  ||¡ q|tv r¸|  ||¡ qt|t|ƒ |  ||¡ qd S )Nc                 S   s
   t  | ¡S r   )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)Ú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"   Úbase_msgÚ
append_msgr.   ÚpartÚctypeÚ
ctype_origr-   Úwas_compressedÚer   ÚmaintypeÚsubtypeÚn_partÚhr   r   r   r&   Z   sf   

ÿþ
€ø

€¶zUserDataProcessor._process_msgc              	   C   s¬   |  dd ¡}d }| ¡ tv r*zt |jdd�¡}|r|  d¡}W n	 ty)   Y nw |d ur0|}|d u r6|}|d urTz| dtt	|ƒƒ¡ W d S  t
tfyS   Y d S w d S )Nr2   T©Údecodeúlaunch-index)Úgetr5   ÚEXAMINE_FOR_LAUNCH_INDEXr   Ú	load_yamlÚget_payloadÚ	Exceptionr   r   ÚintÚ
ValueErrorÚ	TypeError)r"   r   Ú
header_idxÚpayload_idxr-   r   r   r   Ú_attach_launch_indexª   s,   
€ÿÿýz&UserDataProcessor._attach_launch_indexc                 C   s&   t  |dd¡}tj | j d¡d|¡S )NÚmd5é@   ÚdataÚurlcache)r   Ú	hash_blobÚosÚpathÚjoinr   Úget_ipath_cur)r"   ÚentryÚentry_fnr   r   r   Ú_get_include_once_filenameÂ   s   ÿz,UserDataProcessor._get_include_once_filenamec                 C   s$   |  ¡ st|t| ƒ |  |¡ d S r   )r@   r   ÚPART_FN_TPLr_   )r"   r   Úattached_idr   r   r   Ú_process_before_attachÈ   s   z(UserDataProcessor._process_before_attachc                 C   s¬  d}|  ¡ D ]Í}| ¡ }| d¡r|tdƒd …  ¡ }d}n| d¡r/|tdƒd …  ¡ }d}| d¡r5q| ¡ }|s<qd }d }|rG|  |¡}|rUtj 	|¡rUt
 |¡}npz-t|dd| jd�}|rn| ¡ rnt
j||jd	d
� | ¡ rv|j}nd ||j¡}	t|	ƒ W nB ty¨ }
 zt|
ƒ}||vr™|d |¡7 }t||
ƒ W Y d }
~
n!d }
~
w tyÄ } zd ||¡}	t|	|ƒ W Y d }~nd }~ww |d urÓt|ƒ}|  ||¡ qd S )NFz#include-onceTz#includeú#é   é
   )ÚtimeoutÚretriesr!   i€  )Úmodez6Fetching from {} resulted in a invalid http code of {}z for url: {0}zFetching from {} resulted in {})Ú
splitlinesÚlowerÚ
startswithr;   ÚlstripÚstriprk   re   rf   Úisfiler   Úload_text_filer
   r!   ÚokÚ
write_fileÚcontentsr:   Úcoder   r	   r   ÚIOErrorr'   r&   )r"   ÚcontentrH   Úinclude_once_onÚlineÚlc_lineÚinclude_urlÚinclude_once_fnÚrespr   ÚurleÚmessageÚioeÚnew_msgr   r   r   rC   Í   st   



üÿÿþ€€ÿ€ü€ÂzUserDataProcessor._do_includec                 C   sH  t j|g ttfd�}|D ]•}t|tƒrd|i}t|tƒsq| dd¡}| d¡}|s9t}t|t	ƒr3t
}t ||¡}| dd¡\}}	|dkrUt|t	ƒrN| ¡ }t||	d�}
n
t||	ƒ}
|
 |¡ d	|v rjt|
|d	 ƒ d
|v rx|
 dt|d
 ƒ¡ t| ¡ ƒD ]}| ¡ dd	dd
dt ¡ t ¡ fv r’q~|
 ||| ¡ q~|  ||
¡ qd S )N)ÚdefaultÚallowedr�   Ú Útyper0   r1   Útext)Ú_subtyper   rT   r2   zcontent-disposition)r   rW   r%   Úsetr$   r   ÚdictrU   ÚARCHIVE_UNDEF_TYPEÚbytesÚARCHIVE_UNDEF_BINARY_TYPEr   r,   r>   rS   r   r   r?   r   r   Úkeysrv   ÚATTACHMENT_FIELDrA   rF   )r"   ÚarchiverH   ÚentriesÚentr�   ÚmtyperŒ   rN   rO   r   Úheaderr   r   r   rE     sL   






ù	Ðz"UserDataProcessor._explode_archiveNc              	   C   sj   t |vrd|t < |durt|t t|ƒƒ d}z
t| t ¡ƒ}W |S  ttfy4   t|t t|ƒƒ Y |S w )z
        Return the number of attachments to this MIMEMultipart by looking
        at its 'Number-Attachments' header.
        Ú0Nr   )r˜   r   r   rZ   rU   r[   r\   )r"   Ú	outer_msgÚ	new_countÚfetched_countr   r   r   Ú_multi_part_countF  s   þþz#UserDataProcessor._multi_part_countc                 C   s8   |   |¡}|  ||d ¡ | |¡ |   ||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.
        r1   N)r¢   rn   Úattach)r"   rŸ   rI   Ú
part_countr   r   r   rF   X  s   

zUserDataProcessor._attach_partr   )Ú__name__Ú
__module__Ú__qualname__r#   r+   r&   r_   rk   rn   rC   rE   r¢   rF   r   r   r   r   r   L   s    	PE
4r   c                 C   s    |   ¡ pd}| ¡ dkrdS dS )NrŽ   Ú	multipartTF)Úget_content_maintyperv   )rI   Úpart_maintyper   r   r   r4   d  s   r4   c                 C   sn   | sd} dd„ }t | tƒr|  d¡}n| }tj|dd�}d|dd	…  ¡ v r0t | d¡¡}|S |||ƒ}|S )
zJconvert a string (more likely bytes) or a message into
    a mime message.ó    c                 S   s(   |  dd¡\}}t||ƒ}| | ¡ |S )Nr0   r1   )r>   r   r?   )rb   Úcontent_typerN   rO   r   r   r   r   Úcreate_binmsgs  s   

z%convert_string.<locals>.create_binmsgzutf-8FrR   s   mime-version:r   i   )r$   r   Úencoder   r8   rv   Úmessage_from_stringrS   )Úraw_datar¬   r­   Úbdatar   r   r   r   r'   m  s   

þr'   r   )'Úloggingre   Úemail.mime.baser   Úemail.mime.multipartr   Úemail.mime.nonmultipartr   Úemail.mime.textr   Ú	cloudinitr   r   r   Úcloudinit.url_helperr	   r
   Ú	getLoggerr¥   r   ÚNOT_MULTIPART_TYPErl   Ú
OCTET_TYPErA   r=   rB   rD   r<   r”   r–   r7   r˜   rV   r   r   r   r   r4   r'   r   r   r   r   Ú<module>   s<   


  	