Current File : //usr/lib/python2.7/site-packages/cloudinit/sources/helpers/azure.pyo
�
oB�]c@s@ddlZddlZddlZddlZddlZddlZddlZddlZddlm	Z	ddl
mZddl
mZddl
mZddlmZddl
mZddl
mZdd	l
mZdd
l
mZddlmZddlmZdd
lmZeje�ZdZdZdZdZ ej!ddddde"�Z#d�Z$d�Z%e$d��Z&e$d��Z'd�Z(ed��Z)d�Z*de+fd��YZ,d e+fd!��YZ-d"e+fd#��YZ.d$e+fd%��YZ/e$dddd&��Z1d'e+fd(��YZ2dS()i����N(tdhcp(tstages(t
temp_utils(tcontextmanager(tElementTree(t
url_helper(tutil(tversion(tdistros(tevents(tEphemeralDHCPv4(tdatetimesa8:3f:81:10sboot-telemetryssystem-infot
diagnostictnamesazure-dstdescriptions initialize reporter for azure dstreporting_enabledcs�fd�}|S(Ncs<tjd�jd�jdt���||�SWdQXdS(NR
Rtparent(R	tReportEventStackt__name__tazure_ds_reporter(targstkwargs(tfunc(sC/usr/lib/python2.7/site-packages/cloudinit/sources/helpers/azure.pytimpl+s
			
((RR((RsC/usr/lib/python2.7/site-packages/cloudinit/sources/helpers/azure.pytazure_ds_telemetry_reporter*scCsy||krtSdd�}|jd�}dj||d�||d�||d�|d|dg�}||kS(s�
    Azure stores the instance ID with an incorrect byte ordering for the
    first parts. This corrects the byte order such that it is consistent with
    that returned by the metadata service.
    icSs<gtj|d�D]}|^q}|j�dj|�S(Nit(ttextwraptwraptreversetjoin(tstwidthtbytetdd((sC/usr/lib/python2.7/site-packages/cloudinit/sources/helpers/azure.pytswap_bytestring=s%
t-iiii(tFalsetsplitR(tprevious_idt
current_idR"tpartst
swapped_id((sC/usr/lib/python2.7/site-packages/cloudinit/sources/helpers/azure.pytis_byte_swapped4s


c
Cs}tj�std��ntjd�y&ttj��ttj��}Wnt	k
rmtd��nXy�tj
ddddgdt�\}}d}|r�d	|kr�|j
d	�d
}n|s�td��n|t|�d}WnHtjk
r}td
|��n#t	k
r:}td|��nXy�tj
dddddgdt�\}}d}|r�d	|kr�|j
d	�d
}n|s�td��n|t|�d}WnHtjk
r�}td|��n#t	k
r
}td|��nXtjtddtj|�j�dtj|�j�dtj|�j�dftj�}tj|�|S(s^Report timestamps related to kernel initialization and systemd
       activation of cloud-inits1distro not using systemd, skipping boot telemetrysCollecting boot telemetrys*Failed to determine kernel start timestamps/bin/systemctltshows-ptUserspaceTimestampMonotonictcapturet=is8Failed to parse UserspaceTimestampMonotonic from systemdi@Bs-Failed to get UserspaceTimestampMonotonic: %ss<Failed to parse UserspaceTimestampMonotonic from systemd: %sscloud-init-localtInactiveExitTimestampMonotonics;Failed to parse InactiveExitTimestampMonotonic from systemds0Failed to get InactiveExitTimestampMonotonic: %ss?Failed to parse InactiveExitTimestampMonotonic from systemd: %ssboot-telemetrys5kernel_start=%s user_start=%s cloudinit_activation=%stZN(Rtuses_systemdtRuntimeErrortLOGtdebugtfloatttimeRtuptimet
ValueErrortsubptTruetNoneR%tProcessExecutionErrorR	tReportingEventtBOOT_EVENT_TYPERtutcfromtimestampt	isoformattDEFAULT_EVENT_ORIGINtreport_event(tkernel_starttoutt_ttsmt
user_starttetcloudinit_activationtevt((sC/usr/lib/python2.7/site-packages/cloudinit/sources/helpers/azure.pytget_boot_telemetryNsf
&
		
	

cCs{tj�}tjtddtj�|d|d|dd|dd|dd|d	ftj�}tj|�|S(
s%Collect and report system informationssystem informationstcloudinit_version=%s, kernel_version=%s, variant=%s, distro_name=%s, distro_version=%s, flavor=%s, python_version=%streleasetvarianttdistiiitpython(	Rtsystem_infoR	R=tSYSTEMINFO_EVENT_TYPERtversion_stringRARB(tinfoRJ((sC/usr/lib/python2.7/site-packages/cloudinit/sources/helpers/azure.pytget_system_info�s!
cCs,tjtd|tj�}tj|�|S(sReport a diagnostic eventsdiagnostic message(R	R=tDIAGNOSTIC_EVENT_TYPERARB(tstrRJ((sC/usr/lib/python2.7/site-packages/cloudinit/sources/helpers/azure.pytreport_diagnostic_event�s

ccsCtj�}tjtjj|��z	dVWdtj|�XdS(N(tostgetcwdtchdirtpatht
expanduser(tnewdirtprevdir((sC/usr/lib/python2.7/site-packages/cloudinit/sources/helpers/azure.pytcd�s
	cCstj�rd}nd}|S(Ns
option-245sunknown-245(Rt
is_FreeBSD(tazure_endpoint((sC/usr/lib/python2.7/site-packages/cloudinit/sources/helpers/azure.pyt_get_dhcp_endpoint_option_name�s	tAzureEndpointHttpClientcBs@eZidd6dd6Zd�Zed�Zddd�ZRS(tWALinuxAgentsx-ms-agent-names
2012-11-30sx-ms-versioncCsidd6|d6|_dS(NtDES_EDE3_CBCsx-ms-cipher-names!x-ms-guest-agent-public-x509-cert(textra_secure_headers(tselftcertificate((sC/usr/lib/python2.7/site-packages/cloudinit/sources/helpers/azure.pyt__init__�scCsP|j}|r1|jj�}|j|j�ntj|d|dddd�S(Ntheadersttimeoutitretriesi
(RjtcopytupdateRfRtread_file_or_url(RgturltsecureRj((sC/usr/lib/python2.7/site-packages/cloudinit/sources/helpers/azure.pytget�s	c
CsY|j}|dk	r4|jj�}|j|�ntj|d|d|dddd�S(NtdataRjRkiRli
(RjR;RmRnRRo(RgRpRst
extra_headersRj((sC/usr/lib/python2.7/site-packages/cloudinit/sources/helpers/azure.pytpost�s	N(Rt
__module__RjRiR$RrR;Ru(((sC/usr/lib/python2.7/site-packages/cloudinit/sources/helpers/azure.pyRc�s
	t	GoalStatecBsVeZd�Zd�Zed��Zed��Zed��Zed��ZRS(cCs(||_tj|�|_d|_dS(N(thttp_clientRt
fromstringtrootR;t_certificates_xml(RgtxmlRx((sC/usr/lib/python2.7/site-packages/cloudinit/sources/helpers/azure.pyRi�s	cCs)|jj|�}|dk	r%|jSdS(N(RztfindR;ttext(Rgtxpathtelement((sC/usr/lib/python2.7/site-packages/cloudinit/sources/helpers/azure.pyt_text_from_xpath�scCs
|jd�S(Ns./Container/ContainerId(R�(Rg((sC/usr/lib/python2.7/site-packages/cloudinit/sources/helpers/azure.pytcontainer_id�scCs
|jd�S(Ns
./Incarnation(R�(Rg((sC/usr/lib/python2.7/site-packages/cloudinit/sources/helpers/azure.pytincarnation�scCs
|jd�S(Ns4./Container/RoleInstanceList/RoleInstance/InstanceId(R�(Rg((sC/usr/lib/python2.7/site-packages/cloudinit/sources/helpers/azure.pytinstance_id�scCsU|jdkrN|jd�}|dk	rN|jj|dt�j|_qNn|jS(NsD./Container/RoleInstanceList/RoleInstance/Configuration/CertificatesRq(R{R;R�RxRrR:tcontents(RgRp((sC/usr/lib/python2.7/site-packages/cloudinit/sources/helpers/azure.pytcertificates_xml�s		(	RRvRiR�tpropertyR�R�R�R�(((sC/usr/lib/python2.7/site-packages/cloudinit/sources/helpers/azure.pyRw�s		tOpenSSLManagercBs�eZidd6dd6Zd�Zd�Zed��Zeed���Zed��Z	ed	��Z
ed
��Zed��ZRS(sTransportPrivate.pemtprivate_keysTransportCert.pemRhcCs&tj�|_d|_|j�dS(N(RtmkdtempttmpdirR;Rhtgenerate_certificate(Rg((sC/usr/lib/python2.7/site-packages/cloudinit/sources/helpers/azure.pyRis	cCstj|j�dS(N(Rtdel_dirR�(Rg((sC/usr/lib/python2.7/site-packages/cloudinit/sources/helpers/azure.pytclean_upscCs�tjd�|jdk	r-tjd�dSt|j���tjddddddd	d
ddd
|jdd|jdg�d}x:t	|jd�D]%}d|kr�||j
�7}q�q�W||_WdQXtjd�dS(Ns7Generating certificate for communication with fabric...sCertificate already generated.topenssltreqs-x509s-nodess-subjs/CN=LinuxTransports-dayst32768s-newkeysrsa:2048s-keyoutR�s-outRhRtCERTIFICATEsNew certificate generated.(R3R4RhR;R_R�RR9tcertificate_namestopentrstrip(RgRhtline((sC/usr/lib/python2.7/site-packages/cloudinit/sources/helpers/azure.pyR�s 


cCs1ddd|g}tj|d|�\}}|S(NR�tx509s-nooutRs(RR9(tactiontcerttcmdtresultRE((sC/usr/lib/python2.7/site-packages/cloudinit/sources/helpers/azure.pyt_run_x509_action*scCsI|jd|�}ddddddg}tj|d|�\}}|S(	Ns-pubkeys
ssh-keygens-is-mtPKCS8s-fs
/dev/stdinRs(R�RR9(RgRhtpub_keyt
keygen_cmdtssh_keyRE((sC/usr/lib/python2.7/site-packages/cloudinit/sources/helpers/azure.pyt_get_ssh_key_from_cert1scCsH|jd|�}|jd�}||dd!jd�}dj|�S(sopenssl x509 formats fingerprints as so:
        'SHA1 Fingerprint=07:3E:19:D1:4D:1C:79:92:24:C6:A0:FD:8D:DA:        B6:A8:BF:27:D4:73
'

        Azure control plane passes that fingerprint as so:
        '073E19D14D1C799224C6A0FD8DDAB6A8BF27D473'
        s-fingerprintR.ii����t:R(R�R}R%R(RgRhtraw_fpteqtoctets((sC/usr/lib/python2.7/site-packages/cloudinit/sources/helpers/azure.pyt_get_fingerprint_from_cert8s	cCs�tj|�jd�}|j}ddddd|jd�g}t|j��;tjdj	|j
�d	td
dj|��\}}WdQX|S(
s�Decrypt the certificates XML document using the our private key;
           return the list of certs and private keys contained in the doc.
        s.//DatasMIME-Version: 1.0s<Content-Disposition: attachment; filename="Certificates.p7m"s?Content-Type: application/x-pkcs7-mime; name="Certificates.p7m"s!Content-Transfer-Encoding: base64Rsutf-8suopenssl cms -decrypt -in /dev/stdin -inkey {private_key} -recip {certificate} | openssl pkcs12 -nodes -password pass:tshellRss
N(
RRyR}R~tencodeR_R�RR9tformatR�R:R(RgR�ttagtcertificates_contenttlinesRDRE((sC/usr/lib/python2.7/site-packages/cloudinit/sources/helpers/azure.pyt_decrypt_certs_from_xmlFs	$c	Cs�|j|�}g}i}x�|j�D]�}|j|�tjd|�rVg}q(tjd|�r(dj|�}|j|�}|j|�}|||<g}q(q(W|S(s�Given the Certificates XML document, return a dictionary of
           fingerprints and associated SSH keys derived from the certs.s[-]+END .*?KEY[-]+$s[-]+END .*?CERTIFICATE[-]+$s
(R�t
splitlinestappendtretmatchRR�R�(	RgR�RDtcurrenttkeysR�RhR�tfingerprint((sC/usr/lib/python2.7/site-packages/cloudinit/sources/helpers/azure.pytparse_certificates]s
	

(
RRvR�RiR�RR�tstaticmethodR�R�R�R�R�(((sC/usr/lib/python2.7/site-packages/cloudinit/sources/helpers/azure.pyR�s
		tWALinuxAgentShimcBs%eZdjddddddddd	d
ddd
ddg�Zddd�Zd�Zed��Ze	d��Z
ed��Zeedd���Z
eed���Zeed���Zeed���Zeeddd���Zedd��Zd�Zed��ZRS(s
s&<?xml version="1.0" encoding="utf-8"?>sk<Health xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">s<  <GoalStateIncarnation>{incarnation}</GoalStateIncarnation>s
  <Container>s-    <ContainerId>{container_id}</ContainerId>s    <RoleInstanceList>s      <Role>s.        <InstanceId>{instance_id}</InstanceId>s        <Health>s          <State>Ready</State>s        </Health>s
      </Role>s    </RoleInstanceList>s  </Container>s	</Health>cCs8tjd|�||_d|_d|_||_dS(Ns5WALinuxAgentShim instantiated, fallback_lease_file=%s(R3R4tdhcpoptionsR;t	_endpointtopenssl_managert
lease_file(Rgtfallback_lease_filetdhcp_options((sC/usr/lib/python2.7/site-packages/cloudinit/sources/helpers/azure.pyRi�s				cCs#|jdk	r|jj�ndS(N(R�R;R�(Rg((sC/usr/lib/python2.7/site-packages/cloudinit/sources/helpers/azure.pyR��scCs(tj�}tjj|jj�d�S(Nsdhclient.hooks(RtInitRXR[Rtpathstget_runpath(t_paths((sC/usr/lib/python2.7/site-packages/cloudinit/sources/helpers/azure.pyt_get_hooks_dir�scCs4|jdkr-|j|j|j�|_n|jS(N(R�R;t
find_endpointR�R�(Rg((sC/usr/lib/python2.7/site-packages/cloudinit/sources/helpers/azure.pytendpoint�scCs�|jdd�}t|�dkr�d}x@|jd�D]/}t|�dkr_d|}n||7}q:Wtjdt|jdd�d��}n|jd	�}tj|�S(
Ns\RiR�it0s>Lisutf-8(	treplacetlenR%tstructtpacktintR�tsockett	inet_ntoa(tfallback_lease_valuetunescaped_valuet
hex_stringthex_pairtpacked_bytes((sC/usr/lib/python2.7/site-packages/cloudinit/sources/helpers/azure.pytget_ip_from_lease_value�s
$cCstjdd|�S(Nt
OPTION_245tleases_d(Rtnetworkd_get_option_from_leases(R�((sC/usr/lib/python2.7/site-packages/cloudinit/sources/helpers/azure.pyt_networkd_get_value_from_leases�scCs�g}ytj|�}Wn'tk
rB}tjd||�dSXtjd|�t�}xR|j�D]D}||kri|j	|j
d�jdd�dj
d��qiqiWt|�dkr�dS|dSdS(NsFailed to read %s: %ss
content is %st ii����s;
"i(
Rt	load_filetIOErrorR3terrorR;R4RbR�R�tstripR%R�(R�tleasestcontenttextoption_nameR�((sC/usr/lib/python2.7/site-packages/cloudinit/sources/helpers/azure.pyt_get_value_from_leases_file�s	6cCs�i}tj�}tjj|�s8tjd|�dSgtj|�D]}tjj	||�^qH}xx|D]p}y>tjj
|�jdd�}tj
tj|��||<Wqptk
r�tdjd|���qpXqpW|S(Ns
%s not found.s.jsonRs{_file} is not valid JSON datat_file(R�R�RXR[texistsR3R4R;tlistdirRtbasenameR�tjsontloadsRR�R8R�(R�t	hooks_dirtxt
hook_filest	hook_fileR
((sC/usr/lib/python2.7/site-packages/cloudinit/sources/helpers/azure.pyt_load_dhclient_json�s.
 
cCs^|dkrdSd}xA|D]9}||jdd�}|dk	rtjd�PqqW|S(Ntunknown_245s)Endpoint server found in dhclient options(R;RrR3R4(R�t_valuet	interface((sC/usr/lib/python2.7/site-packages/cloudinit/sources/helpers/azure.pyt_get_value_from_dhcpoptions�s

cCshd}|dk	r(|}tjd�n|dkrZtd�tjd�tj�}n|dkr�td�tjd�tj�}tj|�}n|dkrtd�tjd�|dkr�tjd�d}qtjd	|�tj	|�}n|dkr4d
}t|�tj|�t
}ntj|�}d|}t|�tj|�|S(Ns&Using Azure Endpoint from dhcp optionss#No Azure endpoint from dhcp optionss'Finding Azure endpoint from networkd...sNo Azure endpoint from networkds(Finding Azure endpoint from hook json...s$No Azure endpoint from dhclient logssLUnable to find endpoint in dhclient logs.  Falling back to check lease filess%No fallback lease file was specified.s%Looking for endpoint in lease file %ss&No lease found; using default endpointsAzure endpoint found at %s(R;R3R4RWR�R�R�R�twarningR�tDEFAULT_WIRESERVER_ENDPOINTR�(R�tdhcp245tvalueR�tmsgtendpoint_ip_address((sC/usr/lib/python2.7/site-packages/cloudinit/sources/helpers/azure.pyR��sB






		

	


c	Csd|jdkrt�|_nt|jj�}tjd�d}x{tr�y|jdj	|j
��}WnDtk
r�}|dkr�tj
|d�q�td|��nXP|d7}qFWtjd�t|j|�}td|j�g}|jdk	rI|dk	rItjd	�|jj|j�}|j||�}n|j||�i|d
6S(NsRegistering with Azure...is"http://{0}/machine/?comp=goalstatei
is!failed to register with Azure: %ss#Successfully fetched GoalState XML.scontainer_id %ss/Certificate XML found; parsing out public keys.spublic-keys(R�R;R�RcRhR3RSR:RrR�R�t	ExceptionR6tsleepRWR4RwR�R�R�R�t_filter_pubkeyst
_report_ready(	Rgtpubkey_infoRxtattemptstresponseRHt
goal_statetssh_keystkeys_by_fingerprint((sC/usr/lib/python2.7/site-packages/cloudinit/sources/helpers/azure.pyt"register_with_azure_and_fetch_datas8
	

	cCs�g}x�|D]�}d|kr=|dr=|j|d�q
d|kr�|dr�|d}||kr}|j||�q�tjd|�q
tjd|�q
W|S(s$cloud-init expects a straightforward array of keys to be dropped
           into the user's authorized_keys file. Azure control plane exposes
           multiple public keys to the VM via wireserver. Select just the
           user's key(s) and return them, ignoring any other certs.
        R�R�sIovf-env.xml specified PublicKey fingerprint %s not found in goalstate XMLsFovf-env.xml specified PublicKey with neither value nor fingerprint: %s(R�R3R�(RgR�R�R�tpubkeyR�((sC/usr/lib/python2.7/site-packages/cloudinit/sources/helpers/azure.pyR�7s

	
	cCs�tjd�|jjd|jd|jd|j�}tjd�y0|j	dj|j
�d|did	d
6�Wn$tk
r�}td|��nXtj
d�dS(
Ns Reporting ready to Azure fabric.R�R�R�ishttp://{0}/machine?comp=healthRsRtstext/xml; charset=utf-8sContent-Types#exception while reporting ready: %ssReported ready to Azure fabric.(R3R4tREPORT_READY_XML_TEMPLATER�R�R�R�R6R�RuR�R�RWRS(RgR�RxtdocumentRH((sC/usr/lib/python2.7/site-packages/cloudinit/sources/helpers/azure.pyR�Ns
		
N(RRvRRR;RiR�R�R�R�R�R�RR�R�R�R�R�RR�R�(((sC/usr/lib/python2.7/site-packages/cloudinit/sources/helpers/azure.pyR�rsJ	
) 	cCs;td|d|�}z|jd|�SWd|j�XdS(NR�R�R�(R�RR�(R�t	dhcp_optsR�tshim((sC/usr/lib/python2.7/site-packages/cloudinit/sources/helpers/azure.pytget_metadata_from_fabricgs
	tEphemeralDHCPv4WithReportingcBs&eZdd�Zd�Zd�ZRS(cCs||_td|�|_dS(Ntiface(treporterR
tephemeralDHCPv4(RgR	tnic((sC/usr/lib/python2.7/site-packages/cloudinit/sources/helpers/azure.pyRiss	cCs9tjddddd|j��|jj�SWdQXdS(NR
sobtain-dhcp-leaseRsobtain dhcp leaseR(R	RR	R
t	__enter__(Rg((sC/usr/lib/python2.7/site-packages/cloudinit/sources/helpers/azure.pyRws
	
cCs|jj|||�dS(N(R
t__exit__(Rgt	excp_typet
excp_valuetexcp_traceback((sC/usr/lib/python2.7/site-packages/cloudinit/sources/helpers/azure.pyR
~s	N(RRvR;RiRR
(((sC/usr/lib/python2.7/site-packages/cloudinit/sources/helpers/azure.pyRrs	(3R�tloggingRXR�R�R�R6Rt
cloudinit.netRt	cloudinitRRt
contextlibRt	xml.etreeRRRRRtcloudinit.reportingR	tcloudinit.net.dhcpR
Rt	getLoggerRR3R�R>RQRURR:RRR*RKRTRWR_RbtobjectRcRwR�R�R;RR(((sC/usr/lib/python2.7/site-packages/cloudinit/sources/helpers/azure.pyt<module>sV			
	F	
	&k�