
    ܍~c9                     6   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 d dl	mZ d dl	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g dZdZ dgZ!d Z"d Z#ddZ$ G d d          Z%d Z&efdZ'dS )    N)MIMEBase)MIMEMultipart)MIMENonMultipart)MIMEText)featureshandlers)log)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                     | |= || |<   d S N )msgkeyvalues      5/usr/lib/python3/dist-packages/cloudinit/user_data.py_replace_headerr   <   s    CCHHH    c                 V    | d= |                      ddt          |                     d S )NzContent-Disposition
attachment)filename)
add_headerstr)r   r   s     r   _set_filenamer   A   s0    !"NN(,XNOOOOOr   c                 r    t           j        rt          |           |t                              |            d S r   )r   ERROR_ON_USER_DATA_FAILURE	ExceptionLOGwarning)error_messagesource_exceptions     r   _handle_errorr#   F   s6    * #&&,<<M"""""r   c                   L    e Zd Zd Zd Zd Zd Zd Zd Zd Z	d Z
dd
Zd Zd	S )UserDataProcessorc                 F    || _         t          j        |          | _        d S r   )pathsr
   fetch_ssl_detailsssl_details)selfr'   s     r   __init__zUserDataProcessor.__init__N   s"    
1%88r   c                     t                      }t          |t                    r)|D ]%}|                     t	          |          |           &n#|                     t	          |          |           |S r   )r   
isinstancelist_process_msgconvert_string)r*   blobaccumulating_msgbs       r   processzUserDataProcessor.processR   s    (??dD!! 	F G G!!."3"35EFFFFG nT224DEEEr   c           
      H   d }|                                 D ]}t          |          rd }|                                }t          j        |          }d}|t
          v rl	 t          j        |d          }d }d}nP# t          j        $ r>}	d                    |t          |          |	          }
t          |
|	           Y d }	~	d }	~	ww xY w|st          }|t          dgz   v r ||          }||}|r|                    dd          \  }}t          ||          }|                    |           |                                r"t#          ||                                           d	D ]*}||v r$t%          ||t'          ||                              +|}||k    rt%          |t(          |           |t*          v r|                     ||           |t.          v r|                     ||           t%          |t(          |           |                     ||           
d S )
Nc                 *    t          j        |           S r   )r   type_from_starts_with)payloads    r   
find_ctypez2UserDataProcessor._process_msg.<locals>.find_ctype\   s    1':::r   F)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_msgr9   partctype
ctype_origr8   was_compresseder!   maintypesubtypen_parths                  r   r/   zUserDataProcessor._process_msg[   s   	; 	; 	; MMOO K	0 K	0DD!! E..00J066G"N
 \))".weDDDG "&J%)NN.   006&Ga1 1 " "-333HHHH  ('
 [,@+AAAA"
7++}"  $)KKQ$7$7!')(G<<""7+++ $$&& ?!&$*;*;*=*=>>>* A AADyy'3tAw<<@@@
""lE:::%%  *555%%%%gz::: HlE:::j$////WK	0 K	0s   !A<<C	4CC	c                    |                     dd           }d }|                                t          v rQ	 t          j        |                    d                    }|r|                     d          }n# t          $ r Y nw xY w||}||}|K	 |                    dt          t          |                               d S # t          t          f$ r Y d S w xY wd S )Nr=   Tdecodelaunch-index)getr@   EXAMINE_FOR_LAUNCH_INDEXr
   	load_yamlget_payloadr   r   r   int
ValueError	TypeError)r*   r   
header_idxpayload_idxr8   s        r   _attach_launch_indexz&UserDataProcessor._attach_launch_index   s   WW^T22
!!%=== .)E)EFF >")++n"="=K    !$K$K"~s3{3C3C/D/DEEEEE	*    #"s#   ?A5 5
BB0C CCc                     t          j        |dd          }t          j                            | j                            d          d|          S )Nmd5@   dataurlcache)r
   	hash_blobospathjoinr'   get_ipath_cur)r*   entryentry_fns      r   _get_include_once_filenamez,UserDataProcessor._get_include_once_filename   sD    >%33w||J$$V,,j(
 
 	
r   c                     |                                 st          |t          |z             |                     |           d S r   )rK   r   PART_FN_TPLrj   )r*   r   attached_ids      r   _process_before_attachz(UserDataProcessor._process_before_attach   sE    !! 	<#{k:;;;!!#&&&&&r   c                    d}|                                 D ]Z}|                                }|                    d          r,|t          d          d                                          }d}n@|                    d          r+|t          d          d                                          }d}|                    d          r|                                }|sd }d }|r|                     |          }|r5t          j        	                    |          rt          j        |          }n	 t          |dd| j                  }|r0|                                rt          j        ||j        d	
           |                                r|j        }n*d                    ||j                  }	t'          |	           n# t(          $ rE}
t+          |
          }||vr|d                    |          z  }t'          ||
           Y d }
~
n@d }
~
wt,          $ r0}d                    ||          }	t'          |	|           Y d }~nd }~ww xY w|%t/          |          }|                     ||           \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
startswithrF   lstripstriprw   rq   rr   isfiler
   	load_filer   r)   ok
write_filecontentsrE   coder#   r   r   IOErrorr0   r/   )r*   contentrS   include_once_onlinelc_lineinclude_urlinclude_once_fnrespr!   urlemessageioenew_msgs                 r   rN   zUserDataProcessor._do_include   s     &&(( >	7 >	7DjjllG!!/22 
(C00223::<< #'##J// (C
OO--.5577 #(s## **,,K "OG O"&"A"A+"N"N #627>>/#B#B #6.99 6+# ! "$($4	  D ' 47799 +T]    wwyy 	5"&-99? +TY: : & &m444 1 1 1!$iiG #'11?#9#9+#F#FF!'400000000 6 6 6$E$L$L#S% %M "-55555555	6 "(11!!':666}>	7 >	7s%   /BG  
I
;H

I&IIc                 R   t          j        |g t          t          f          }|D ]}t	          |t
                    rd|i}t	          |t                    s2|                    dd          }|                    d          }|s8t          }t	          |t                    rt          }t          j        ||          }|                    dd          \  }}	|dk    r;t	          |t                    r|                                }t          ||	          }
n%t!          ||	          }
|
                    |           d	|v rt%          |
|d	                    d
|v r)|
                    dt          |d
                              t          |                                          D ]j}|                                dd	dd
dt,                                          t.                                          fv rN|
                    |||                    k|                     ||
           d S )N)defaultallowedr    typer;   r<   text)_subtyper   r`   r=   zcontent-disposition)r
   rc   r.   setr-   r   dictra   ARCHIVE_UNDEF_TYPEbytesARCHIVE_UNDEF_BINARY_TYPEr   r7   rI   r_   r   r   rJ   r   r   keysr   ATTACHMENT_FIELDrL   rQ   )r*   archiverS   entriesentr   mtyper   rY   rZ   r   headers               r   rP   z"UserDataProcessor._explode_archive  s%   ."tSkJJJ 0	/ 0	/C #s## ' #&cD** ggi,,GGGFOOE I,gu-- 87G 6wHH %C 3 3Hg6!!gu-- /%nn..Gw999x11(((S  c3z?333$$~s3~3F/G/GHHHsxxzz** 4 4<<>>")$**,, &&((&   vs6{3333j#....a0	/ 0	/r   Nc                 @   t           |vr
d|t           <   |#t          |t           t          |                     d}	 t          |                    t                               }n:# t
          t          f$ r& t          |t           t          |                     Y nw xY w|S )z
        Return the number of attachments to this MIMEMultipart by looking
        at its 'Number-Attachments' header.
        0Nr   )r   r   r   re   ra   rf   rg   )r*   	outer_msg	new_countfetched_counts       r   _multi_part_countz#UserDataProcessor._multi_part_countH  s    
 9,,*-I&' I'7YHHH	M	.> ? ?@@MMI& 	M 	M 	MI'7]9K9KLLLLL	Ms   'A$ $4BBc                     |                      |          }|                     ||dz              |                    |           |                      ||dz              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   rT   
part_counts       r   rQ   zUserDataProcessor._attach_partZ  se     ++I66
##D*q.999y*q.99999r   r   )__name__
__module____qualname__r+   r4   r/   rj   rw   r{   rN   rP   r   rQ   r   r   r   r%   r%   M   s        9 9 9     O0 O0 O0b  0
 
 
' ' '
C7 C7 C7J2/ 2/ 2/h   $	: 	: 	: 	: 	:r   r%   c                 f    |                                  pd}|                                dk    rdS dS )Nr   	multipartTF)get_content_maintyper   )rT   part_maintypes     r   r?   r?   f  s9    --//52M++t5r   c                 >   | sd} d }t          | t                    r|                     d          }n| }t          j        |d          }d|dd                                         v r(t          j        |                    d                    }n |||          }|S )	zJconvert a string (more likely bytes) or a message into
    a mime message.r   c                     |                     dd          \  }}t          ||          }|                    |            |S )Nr;   r<   )rI   r   rJ   )rn   content_typerY   rZ   r   s        r   create_binmsgz%convert_string.<locals>.create_binmsgu  sB    (..sA66'x))
r   zutf-8Fr^   s   mime-version:r   i   )r-   r   encoder
   rC   r   message_from_stringr_   )raw_datar   r   bdatar   s        r   r0   r0   o  s         (C   ((U5111E54=..0000&u||G'<'<==mE<00Jr   r   )(rq   email.mime.baser   email.mime.multipartr   email.mime.nonmultipartr   email.mime.textr   	cloudinitr   r   r	   loggingr
   cloudinit.url_helperr   r   	getLoggerr   r   NOT_MULTIPART_TYPEry   
OCTET_TYPErL   rH   rM   rO   rG   r   r   rB   r   rb   r   r   r#   r%   r?   r0   r   r   r   <module>r      s   
			 $ $ $ $ $ $ . . . . . . 4 4 4 4 4 4 $ $ $ $ $ $ ( ( ( ( ( ( ( ( $ $ $ $ $ $       ; ; ; ; ; ; ; ;g!! 0 " 
  34%'@A,-
( 6 	 	 	 (  00   
P P P
# # # #V: V: V: V: V: V: V: V:r   +=      r   