oB]c@s@ddlZddlZddlZddlZddlZddlZddlZddlZddlm Z ddl m Z ddl m Z ddl mZddlmZddl mZddl mZdd l mZdd l mZdd lmZdd 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(t ElementTree(t url_helper(tutil(tversion(tdistros(tevents(tEphemeralDHCPv4(tdatetimes a8:3f:81:10sboot-telemetrys system-infot diagnostictnamesazure-dst descriptions 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(t previous_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|std n|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_TYPER tutcfromtimestampt 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( Rt system_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-245s unknown-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(tselft certificate((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(t http_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.pyt incarnationscCs |jdS(Ns4./Container/RoleInstanceList/RoleInstance/InstanceId(R(Rg((sC/usr/lib/python2.7/site-packages/cloudinit/sources/helpers/azure.pyt instance_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 Z ed Z RS( sTransportPrivate.pemt private_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 d d 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-outRhRt CERTIFICATEsNew 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 c Cstj|jd}|j}ddddd|jdg}t|j;tjdj |j d t d d j |\}}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|}|||sks< {incarnation}s s- {container_id}s s s. {instance_id}s s Readys s s s s cCs8tjd|||_d|_d|_||_dS(Ns5WALinuxAgentShim instantiated, fallback_lease_file=%s(R3R4t dhcpoptionsR;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[Rtpathst get_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 dj dddj dqiqiWt |dkrdS|dSdS(NsFailed to read %s: %ss content is %st iis; "i( Rt load_filetIOErrorR3terrorR;R4RbRRtstripR%R(Rtleasestcontenttext option_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 |j dd}t j tj|||RQRURR:RRR*RKRTRWR_RbtobjectRcRwRRR;RR(((sC/usr/lib/python2.7/site-packages/cloudinit/sources/helpers/azure.pytsV           F  &k