Viewing File: /usr/lib/python2.7/site-packages/cloudinit/sources/helpers/azure.pyc


oB]c@s@ddlZddlZddlZddlZddlZddlZddlZddlZddlm	Z	ddl
mZddl
mZddl
mZddlmZddl
mZddl
mZdd	l
mZdd
l
mZddlmZddlmZdd
lmZejeZdZdZdZdZ ej!ddddde"Z#dZ$dZ%e$dZ&e$dZ'dZ(edZ)dZ*de+fdYZ,d e+fd!YZ-d"e+fd#YZ.d$e+fd%YZ/e$dddd&Z1d'e+fd(YZ2dS()iN(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_enabledcsfd}|S(Ncs<tjdjdjdt||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|dD]}|^q}|jdj|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}tjstdntjdy&ttjttj}Wnt	k
rmtdnXytj
ddddgdt\}}d}|rd	|kr|j
d	d
}n|stdn|t|d}WnHtjk
r}td
|n#t	k
r:}td|nXytj
dddddgdt\}}d}|rd	|kr|j
d	d
}n|stdn|t|d}WnHtjk
r}td|n#t	k
r
}td|nXtjtddtj|jdtj|jdtj|jdftj}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_infos!
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_events

ccsCtj}tjtjj|z	dVWdtj|XdS(N(tostgetcwdtchdirtpatht
expanduser(tnewdirtprevdir((sC/usr/lib/python2.7/site-packages/cloudinit/sources/helpers/azure.pytcds
	cCstjrd}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_names	tAzureEndpointHttpClientcBs@eZidd6dd6ZdZedZdddZRS(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|jntj|d|ddddS(Ntheadersttimeoutitretriesi
(RjtcopytupdateRfRtread_file_or_url(RgturltsecureRj((sC/usr/lib/python2.7/site-packages/cloudinit/sources/helpers/azure.pytgets	c
CsY|j}|dk	r4|jj}|j|ntj|d|d|ddddS(NtdataRjRkiRli
(RjR;RmRnRRo(RgRpRst
extra_headersRj((sC/usr/lib/python2.7/site-packages/cloudinit/sources/helpers/azure.pytposts	N(Rt
__module__RjRiR$RrR;Ru(((sC/usr/lib/python2.7/site-packages/cloudinit/sources/helpers/azure.pyRcs
	t	GoalStatecBsVeZdZdZedZedZedZedZRS(cCs(||_tj||_d|_dS(N(thttp_clientRt
fromstringtrootR;t_certificates_xml(RgtxmlRx((sC/usr/lib/python2.7/site-packages/cloudinit/sources/helpers/azure.pyRis	cCs)|jj|}|dk	r%|jSdS(N(RztfindR;ttext(Rgtxpathtelement((sC/usr/lib/python2.7/site-packages/cloudinit/sources/helpers/azure.pyt_text_from_xpathscCs
|jdS(Ns./Container/ContainerId(R(Rg((sC/usr/lib/python2.7/site-packages/cloudinit/sources/helpers/azure.pytcontainer_idscCs
|jdS(Ns
./Incarnation(R(Rg((sC/usr/lib/python2.7/site-packages/cloudinit/sources/helpers/azure.pytincarnationscCs
|jdS(Ns4./Container/RoleInstanceList/RoleInstance/InstanceId(R(Rg((sC/usr/lib/python2.7/site-packages/cloudinit/sources/helpers/azure.pytinstance_idscCsU|jdkrN|jd}|dk	rN|jj|dtj|_qNn|jS(NsD./Container/RoleInstanceList/RoleInstance/Configuration/CertificatesRq(R{R;RRxRrR:tcontents(RgRp((sC/usr/lib/python2.7/site-packages/cloudinit/sources/helpers/azure.pytcertificates_xmls		(	RRvRiRtpropertyRRRR(((sC/usr/lib/python2.7/site-packages/cloudinit/sources/helpers/azure.pyRws		tOpenSSLManagercBseZidd6dd6ZdZdZedZeedZedZ	ed	Z
ed
ZedZRS(sTransportPrivate.pemtprivate_keysTransportCert.pemRhcCs&tj|_d|_|jdS(N(RtmkdtempttmpdirR;Rhtgenerate_certificate(Rg((sC/usr/lib/python2.7/site-packages/cloudinit/sources/helpers/azure.pyRis	cCstj|jdS(N(Rtdel_dirR(Rg((sC/usr/lib/python2.7/site-packages/cloudinit/sources/helpers/azure.pytclean_upscCstjd|jdk	r-tjddSt|jtjddddddd	d
ddd
|jdd|jdgd}x:t	|jdD]%}d|kr||j
7}qqW||_WdQXtjddS(Ns7Generating certificate for communication with fabric...sCertificate already generated.topenssltreqs-x509s-nodess-subjs/CN=LinuxTransports-dayst32768s-newkeysrsa:2048s-keyoutRs-outRhRtCERTIFICATEsNew certificate generated.(R3R4RhR;R_RRR9tcertificate_namestopentrstrip(RgRhtline((sC/usr/lib/python2.7/site-packages/cloudinit/sources/helpers/azure.pyRs 


cCs1ddd|g}tj|d|\}}|S(NRtx509s-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(RRR9(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.iit:R(RR}R%R(RgRhtraw_fpteqtoctets((sC/usr/lib/python2.7/site-packages/cloudinit/sources/helpers/azure.pyt_get_fingerprint_from_cert8s	cCstj|jd}|j}ddddd|jdg}t|j;tjdj	|j
d	td
dj|\}}WdQX|S(
sDecrypt 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_RRR9tformatRR:R(RgRttagtcertificates_contenttlinesRDRE((sC/usr/lib/python2.7/site-packages/cloudinit/sources/helpers/azure.pyt_decrypt_certs_from_xmlFs	$c	Cs|j|}g}i}x|jD]}|j|tjd|rVg}q(tjd|r(dj|}|j|}|j|}|||<g}q(q(W|S(sGiven the Certificates XML document, return a dictionary of
           fingerprints and associated SSH keys derived from the certs.s[-]+END .*?KEY[-]+$s[-]+END .*?CERTIFICATE[-]+$s
(Rt
splitlinestappendtretmatchRRR(	RgRRDtcurrenttkeysRRhRtfingerprint((sC/usr/lib/python2.7/site-packages/cloudinit/sources/helpers/azure.pytparse_certificates]s
	

(
RRvRRiRRRtstaticmethodRRRRR(((sC/usr/lib/python2.7/site-packages/cloudinit/sources/helpers/azure.pyRs
		tWALinuxAgentShimcBs%eZdjddddddddd	d
ddd
ddgZdddZdZedZe	dZ
edZeeddZ
eedZeedZeedZeedddZeddZdZedZRS(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.pyRis				cCs#|jdk	r|jjndS(N(RR;R(Rg((sC/usr/lib/python2.7/site-packages/cloudinit/sources/helpers/azure.pyRscCs(tj}tjj|jjdS(Nsdhclient.hooks(RtInitRXR[Rtpathstget_runpath(t_paths((sC/usr/lib/python2.7/site-packages/cloudinit/sources/helpers/azure.pyt_get_hooks_dirscCs4|jdkr-|j|j|j|_n|jS(N(RR;t
find_endpointRR(Rg((sC/usr/lib/python2.7/site-packages/cloudinit/sources/helpers/azure.pytendpointscCs|jdd}t|dkrd}x@|jdD]/}t|dkr_d|}n||7}q:Wtjdt|jddd}n|jd	}tj|S(
Ns\RiRit0s>Lisutf-8(	treplacetlenR%tstructtpacktintRtsockett	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_values
$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_leasesscCsg}ytj|}Wn'tk
rB}tjd||dSXtjd|t}xR|jD]D}||kri|j	|j
djdddj
dqiqiWt|dkrdS|dSdS(NsFailed to read %s: %ss
content is %st iis;
"i(
Rt	load_filetIOErrorR3terrorR;R4RbRRtstripR%R(Rtleasestcontenttextoption_nameR((sC/usr/lib/python2.7/site-packages/cloudinit/sources/helpers/azure.pyt_get_value_from_leases_files	6cCsi}tj}tjj|s8tjd|dSgtj|D]}tjj	||^qH}xx|D]p}y>tjj
|jdd}tj
tj|||<Wqptk
rtdjd|qpXqpW|S(Ns
%s not found.s.jsonRs{_file} is not valid JSON datat_file(RRRXR[texistsR3R4R;tlistdirRtbasenameRtjsontloadsRRR8R(Rt	hooks_dirtxt
hook_filest	hook_fileR
((sC/usr/lib/python2.7/site-packages/cloudinit/sources/helpers/azure.pyt_load_dhclient_jsons.
 
cCs^|dkrdSd}xA|D]9}||jdd}|dk	rtjdPqqW|S(Ntunknown_245s)Endpoint server found in dhclient options(R;RrR3R4(Rt_valuet	interface((sC/usr/lib/python2.7/site-packages/cloudinit/sources/helpers/azure.pyt_get_value_from_dhcpoptionss

cCshd}|dk	r(|}tjdn|dkrZtdtjdtj}n|dkrtdtjdtj}tj|}n|dkrtdtjd|dkrtjdd}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;R3R4RWRRRRtwarningRtDEFAULT_WIRESERVER_ENDPOINTR(Rtdhcp245tvalueRtmsgtendpoint_ip_address((sC/usr/lib/python2.7/site-packages/cloudinit/sources/helpers/azure.pyRsB






		

	


c	Csd|jdkrt|_nt|jj}tjdd}x{try|jdj	|j
}WnDtk
r}|dkrtj
|dqtd|nXP|d7}qFWtjdt|j|}td|jg}|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(RR;RRcRhR3RSR:RrRRt	ExceptionR6tsleepRWR4RwRRRRt_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
	

	cCsg}x|D]}d|kr=|dr=|j|dq
d|kr|dr|d}||kr}|j||qtjd|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.
        RRsIovf-env.xml specified PublicKey fingerprint %s not found in goalstate XMLsFovf-env.xml specified PublicKey with neither value nor fingerprint: %s(RR3R(RgRRRtpubkeyR((sC/usr/lib/python2.7/site-packages/cloudinit/sources/helpers/azure.pyR7s

	
	cCstjd|jjd|jd|jd|j}tjdy0|j	dj|j
d|did	d
6Wn$tk
r}td|nXtj
ddS(
Ns Reporting ready to Azure fabric.RRRishttp://{0}/machine?comp=healthRsRtstext/xml; charset=utf-8sContent-Types#exception while reporting ready: %ssReported ready to Azure fabric.(R3R4tREPORT_READY_XML_TEMPLATERRRRR6RRuRRRWRS(RgRRxtdocumentRH((sC/usr/lib/python2.7/site-packages/cloudinit/sources/helpers/azure.pyRNs
		
N(RRvRRR;RiRRRRRRRRRRRRRRR(((sC/usr/lib/python2.7/site-packages/cloudinit/sources/helpers/azure.pyRrsJ	
) 	cCs;td|d|}z|jd|SWd|jXdS(NRRR(RRR(Rt	dhcp_optsRtshim((sC/usr/lib/python2.7/site-packages/cloudinit/sources/helpers/azure.pytget_metadata_from_fabricgs
	tEphemeralDHCPv4WithReportingcBs&eZddZdZdZRS(cCs||_td||_dS(Ntiface(treporterR
tephemeralDHCPv4(RgR	tnic((sC/usr/lib/python2.7/site-packages/cloudinit/sources/helpers/azure.pyRiss	cCs9tjddddd|j|jjSWdQXdS(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	(3RtloggingRXRRRR6Rt
cloudinit.netRt	cloudinitRRt
contextlibRt	xml.etreeRRRRRtcloudinit.reportingR	tcloudinit.net.dhcpR
Rt	getLoggerRR3RR>RQRURR:RRR*RKRTRWR_RbtobjectRcRwRRR;RR(((sC/usr/lib/python2.7/site-packages/cloudinit/sources/helpers/azure.pyt<module>sV			
	F	
	&k	
Back to Directory File Manager