
    }f9                     D   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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&y)    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                     | |= || |<   y N )msgkeyvalues      5/usr/lib/python3/dist-packages/cloudinit/user_data.py_replace_headerr   ;   s    CCH    c                 D    | d= | j                  ddt        |             y )NzContent-Disposition
attachment)filename)
add_headerstr)r   r   s     r   _set_filenamer   @   s"    !"NN(,XNOr   c                 f    t         j                  rt        |       |t        j	                  |        y r   )r   ERROR_ON_USER_DATA_FAILURERuntimeErrorLOGwarning)error_messagesource_exceptions     r   _handle_errorr"   E   s%    **=)/??M"r   c                   J    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y	)UserDataProcessorc                 F    || _         t        j                  |      | _        y r   )pathsr	   fetch_ssl_detailsssl_details)selfr&   s     r   __init__zUserDataProcessor.__init__M   s    
11%8r   c                     t               }t        |t              r$|D ]  }| j                  t	        |      |        |S | j                  t	        |      |       |S r   )r   
isinstancelist_process_msgconvert_string)r)   blobaccumulating_msgbs       r   processzUserDataProcessor.processQ   sa    (?dD! G!!."35EFG   nT24DEr   c           
         d }|j                         D ]r  }t        |      rd }|j                         }t        j                  |      }d}|t
        v r	 t        j                  |d      }d }d}|st        }|t        dgz   v r ||      }||}|r|j                  dd      \  }}t        ||      }|j                  |       |j!                         rt#        ||j!                                d	D ]   }||v st%        ||t'        ||                " |}||k7  rt%        |t(        |       |t*        v r| j-                  ||       4|t.        v r| j1                  ||       Pt%        |t(        |       | j3                  ||       u y # t        j                  $ r3}	dj                  |t        |      |	      }
t        |
|	       Y d }	~	d }	~	ww xY w)
Nc                 ,    t        j                  |       S r   )r   type_from_starts_with)payloads    r   
find_ctypez2UserDataProcessor._process_msg.<locals>.find_ctype[   s    1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_msgr8   partctype
ctype_origr7   was_compresseder    maintypesubtypen_parths                  r   r.   zUserDataProcessor._process_msgZ   s   	; MMO J	0DD!E..0J006G"N
 \)"..weDG "&J%)N '
 [,@+AAA"7+}" $)KKQ$7!')(G<""7+ $$&!&$*;*;*=>* AADy'3tAw<@A 
"lE:%  *5%%%gz: HlE:j$/UJ	0& .. 006&Ga1 " "-3s   FG(GGc                 r   |j                  dd       }d }|j                         t        v r9	 t        j                  |j                  d            }|r|j                  d      }||}||}|&	 |j                  dt        t        |                   y y # t        $ r Y <w xY w# t        t        f$ r Y y w xY w)Nr<   Tdecodelaunch-index)getr?   EXAMINE_FOR_LAUNCH_INDEXr	   	load_yamlget_payload	Exceptionr   r   int
ValueError	TypeError)r)   r   
header_idxpayload_idxr7   s        r   _attach_launch_indexz&UserDataProcessor._attach_launch_index   s    WW^T2
!%== ..)EF")++n"=K !$K$K"~s3{3C/DE #   	* s#   8B /$B$ 	B! B!$B65B6c                     t        j                  |dd      }t        j                  j	                  | 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   s?    >>%3ww||JJ$$V,j(
 	
r   c                 l    |j                         st        |t        |z         | j                  |       y r   )rJ   r   PART_FN_TPLrj   )r)   r   attached_ids      r   _process_before_attachz(UserDataProcessor._process_before_attach   s+    !#{k:;!!#&r   c                 H   d}|j                         D ]  }|j                         }|j                  d      r|t        d      d  j	                         }d}n/|j                  d      r|t        d      d  j	                         }d}|j                  d      r|j                         }|sd }d }|r| j                  |      }|r5t        j                  j                  |      rt        j                  |      }n	 t        |dd| j                        }|r2|j                         r"t        j                  ||j                   d	
       |j                         r|j                   }n'dj#                  ||j$                        }	t'        |	       ||t/        |      }| j1                  ||        y # t(        $ r9}
t+        |
      }||vr|dj#                  |      z  }t'        ||
       Y d }
~
bd }
~
wt,        $ r(}dj#                  ||      }	t'        |	|       Y d }~d }~ww xY w)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
startswithrE   lstripstriprw   rq   rr   isfiler	   load_text_filer   r(   ok
write_filecontentsrD   coder"   r
   r   IOErrorr/   r.   )r)   contentrR   include_once_onlinelc_lineinclude_urlinclude_once_fnrespr    urlemessageioenew_msgs                 r   rM   zUserDataProcessor._do_include   s     &&( >	7DjjlG!!/2C023::< #'##J/C
O-.557 #(s#**,K"OG"&"A"A+"N277>>/#B--o> 6+# ! "$($4$4	D '4779+T]] wwy"&--99? +TYY: & &m4 "(1!!':6}>	7^   1!$iG #'1?#9#9+#FF!'400 6$E$L$L#S%M "-55	6s%   :BF00	H!9/G--H!9HH!c                    t        j                  |g t        t        f      }|D ]  }t	        |t
              rd|i}t	        |t              s)|j                  dd      }|j                  d      }|s2t        }t	        |t              rt        }t        j                  ||      }|j                  dd      \  }}	|dk(  r.t	        |t              r|j                         }t        ||	      }
nt!        ||	      }
|
j#                  |       d	|v rt%        |
|d	          d
|v r|
j'                  dt        |d
                t        |j)                               D ]U  }|j+                         dd	dd
dt,        j+                         t.        j+                         fv rA|
j'                  |||          W | j1                  ||
        y )N)defaultallowedr    typer:   r;   text)_subtyper   r_   r<   zcontent-disposition)r	   rb   r-   setr,   r   dictr`   ARCHIVE_UNDEF_TYPEbytesARCHIVE_UNDEF_BINARY_TYPEr   r6   rH   r^   r   r   rI   r   r   keysr   ATTACHMENT_FIELDrK   rP   )r)   archiverR   entriesentr   mtyper   rX   rY   r   headers               r   rO   z"UserDataProcessor._explode_archive  s   .."tSkJ 0	/C #s# #&cD*ggi,GGGFOE,gu-7G 66wH %C 3Hg6!gu-%nn.Gw9x1(S c3z?3$~s3~3F/GHsxxz* 4<<>")$**, &&(&  vs6{34 j#.a0	/r   Nc                    t         |vr	d|t         <   |t        |t         t        |             d}	 t        |j	                  t                     }|S # t
        t        f$ r t        |t         t        |             Y |S w xY w)z
        Return the number of attachments to this MIMEMultipart by looking
        at its 'Number-Attachments' header.
        0r   )r   r   r   re   r`   rf   rg   )r)   	outer_msg	new_countfetched_counts       r   _multi_part_countz#UserDataProcessor._multi_part_countF  s    
 9,*-I&' I'7YH	M	.> ?@M  I& 	MI'7]9KL	Ms   A )A>=A>c                     | j                  |      }| j                  ||dz          |j                  |       | j                  ||dz          y)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   rS   
part_counts       r   rP   zUserDataProcessor._attach_partX  sJ     ++I6
##D*q.9y*q.9r   r   )__name__
__module____qualname__r*   r3   r.   rj   rw   r{   rM   rO   r   rP   r   r   r   r$   r$   L   s9    9 N0`0
'
C7J2/h$	:r   r$   c                 T    | j                         xs d}|j                         dk(  ryy)Nr   	multipartTF)get_content_maintyper   )rS   part_maintypes     r   r>   r>   d  s,    --/52M+r   c                    | sd} d }t        | t              r| j                  d      }n| }t        j                  |d      }d|dd j                         v r&t        j                  |j                  d            }|S  |||      }|S )	zJconvert a string (more likely bytes) or a message into
    a mime message.r   c                 j    |j                  dd      \  }}t        ||      }|j                  |        |S )Nr:   r;   )rH   r   rI   )rn   content_typerX   rY   r   s        r   create_binmsgz%convert_string.<locals>.create_binmsgs  s6    (..sA6'x)
r   zutf-8Fr]   s   mime-version:r   i   )r,   r   encoder	   rB   r   message_from_stringr^   )raw_datar   r   bdatar   s        r   r/   r/   m  s      (C (U51E54=..00&&u||G'<= J E<0Jr   r   )'loggingrq   email.mime.baser   email.mime.multipartr   email.mime.nonmultipartr   email.mime.textr   	cloudinitr   r   r	   cloudinit.url_helperr
   r   	getLoggerr   r   NOT_MULTIPART_TYPEry   
OCTET_TYPErK   rG   rL   rN   rF   r   r   rA   r   ra   r   r   r"   r$   r>   r/   r   r   r   <module>r      s     	 $ . 4 $ . . ;g! 00 ""  
  34%'@A,-
( 6 	 (  00 
P
#U: U:p += r   