
"T`:                 @   sp  d  d l  Z  d  d l Z d  d l Z d  d l Z d  d l Z d  d l Z d  d l Z d  d l Z 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 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 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! d  d l" m" Z" e j# e$  Z% d Z& d Z' d Z( d Z) d Z* d Z+ d Z, e j- d d d d d d  Z. d Z/ d  d!   Z0 d" d#   Z1 e0 d$ d%    Z2 e0 d& d'    Z3 d( d e4 e j5 d) d* d+   Z6 d, d-   Z7 e0 e d. d/ d0   Z8 e0 d1 d2    Z9 e d3 d4    Z: d5 d6   Z; e0 e4 d7 d8 d9     Z< Gd: d;   d;  Z= Gd< d=   d= e>  Z? Gd> d?   d?  Z@ Gd@ dA   dA  ZA GdB dC   dC  ZB GdD dE   dE  ZC e0 d d d dF dG   ZD e0 d d d dH dI   ZE dJ dK   ZF GdL dM   dM  ZG d S)N    N)ENOENT)CFG_BUILTIN)dhcp)stages)
temp_utils)contextmanager)ElementTree)escape)subp)
url_helper)util)version)distros)events)EphemeralDHCPv4)datetimeza8:3f:81:10zboot-telemetryzsystem-infoZ
diagnosticZ
compressedi  z'/run/cloud-init/log_pushed_to_kvp_indexnamezazure-dsdescriptionz initialize reporter for azure dsZreporting_enabledTzThe VM encountered an error during deployment. Please visit https://aka.ms/linuxprovisioningerror for more information on remediation.c                s     f d d   } | S)Nc           
      s=   t  j d   j d   j d t     |  |   SWd  QRXd  S)Nr   r   parent)r   ReportEventStack__name__azure_ds_reporter)argskwargs)func A/usr/lib/python3/dist-packages/cloudinit/sources/helpers/azure.pyimpl=   s
    			
z)azure_ds_telemetry_reporter.<locals>.implr   )r   r   r   )r   r   azure_ds_telemetry_reporter<   s    r   c             C   s|   |  | k r d Sd d d  } | j  d  } d j | | d  | | d  | | d  | d | d	 g  } |  | k S)
z
    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.
    F   c             S   s6   d d   t  j |  d  D } | j   d j |  S)Nc             S   s   g  |  ] } |  q Sr   r   ).0Zbyter   r   r   
<listcomp>P   s   	 z<is_byte_swapped.<locals>.swap_bytestring.<locals>.<listcomp>r    )textwrapZwrapreversejoin)swidthZddr   r   r   swap_bytestringO   s    
z(is_byte_swapped.<locals>.swap_bytestring-r            )splitr%   )Zprevious_idZ
current_idr(   partsZ
swapped_idr   r   r   is_byte_swappedF   s    r/   c           2   C   s  t  j   s t d   t j d  y& t t j    t t j    }  Wn4 t	 k
 r } z t d  |  WYd d } ~ Xn Xy| t
 j
 d d d d g d	 d
 \ } } d } | r d | k r | j d  d } | s t d   |  t |  d } Wnr t
 j k
 r;} z t d |  |  WYd d } ~ Xn8 t	 k
 rr} z t d |  |  WYd d } ~ Xn Xy t
 j
 d d d d d g d	 d
 \ } } d } | rd | k r| j d  d } | st d   |  t |  d } Wnr t
 j k
 r/} z t d |  |  WYd d } ~ Xn8 t	 k
 rf} z t d |  |  WYd d } ~ Xn Xt j t d d t j |   j   d t j |  j   d t j |  j   d f t j  } t j |  | S)z^Report timestamps related to kernel initialization and systemd
       activation of cloud-initz1distro not using systemd, skipping boot telemetryzCollecting boot telemetryz*Failed to determine kernel start timestampNz/bin/systemctlZshowz-pZUserspaceTimestampMonotoniccaptureT=r*   z8Failed to parse UserspaceTimestampMonotonic from systemdi@B z-Failed to get UserspaceTimestampMonotonic: %sz<Failed to parse UserspaceTimestampMonotonic from systemd: %szcloud-init-localZInactiveExitTimestampMonotonicz;Failed to parse InactiveExitTimestampMonotonic from systemdz0Failed to get InactiveExitTimestampMonotonic: %sz?Failed to parse InactiveExitTimestampMonotonic from systemd: %szboot-telemetryz5kernel_start=%s user_start=%s cloudinit_activation=%sZ)r   Zuses_systemdRuntimeErrorLOGdebugfloattimer   Zuptime
ValueErrorr
   r-   ZProcessExecutionErrorr   ReportingEventBOOT_EVENT_TYPEr   ZutcfromtimestampZ	isoformatDEFAULT_EVENT_ORIGINreport_event)Zkernel_starteout_ZtsmZ
user_startZcloudinit_activationevtr   r   r   get_boot_telemetryb   st    	&		

	
rA   c              C   s{   t  j   }  t j t d d t j   |  d |  d |  d d |  d d |  d d |  d	 f t j  } t j |  | S)
z%Collect and report system informationzsystem informationztcloudinit_version=%s, kernel_version=%s, variant=%s, distro_name=%s, distro_version=%s, flavor=%s, python_version=%sreleaseZvariantZdistr   r*   r   python)	r   Zsystem_infor   r9   SYSTEMINFO_EVENT_TYPEr   Zversion_stringr;   r<   )infor@   r   r   r   get_system_info   s    !rF   logger_func)msgreturnc            C   sK   t  |  r | |   t j t d |  t j  } t j | d d h | S)zReport a diagnostic eventzdiagnostic messageexcluded_handler_typeslog)callabler   r9   DIAGNOSTIC_EVENT_TYPEr;   r<   )rH   rG   r@   r   r   r   report_diagnostic_event   s    
rN   c             C   sw   t  j t j |   } d d d | j d  i } t j t |  t j	 |  t j
  } t j | d d d d h | S)	zReport a compressed eventencodingzgz+b64dataasciirJ   rK   printZwebhook)base64Zencodebyteszlibcompressdecoder   r9   COMPRESSED_EVENT_TYPEjsondumpsr;   r<   )Z
event_nameZevent_contentZcompressed_dataZ
event_datar@   r   r   r   report_compressed_event   s    rZ   Zdef_log_filec             C   s  t    } t j d  y t |  d   } | j d t j  t | j   t	 |  } t
 d j | j   | |  d t j | j | t j  t d | j    t j t t | j     Wd QRXWnB t k
 r} z" t
 d t |  d t j WYd d } ~ Xn Xt j d	  y5 t j d
 g d d d d \ } } t d
 |  WnB t k
 r} z" t
 d t |  d t j WYd d } ~ Xn Xd S)az  Push a portion of cloud-init.log file or the whole file to KVP
    based on the file size.
    The first time this function is called after VM boot, It will push the last
    n bytes of the log file such that n < MAX_LOG_TO_KVP_LENGTH
    If called again on the same boot, it continues from where it left off.
    In addition to cloud-init.log, dmesg log will also be collected.z"Dumping cloud-init.log file to KVPrbr   zMDumping last {0} bytes of cloud-init.log file to KVP starting from index: {1}rG   zcloud-init.logNz#Exception when dumping log file: %szDumping dmesg log to KVPZdmesgrV   Fr0   Tz$Exception when dumping dmesg log: %s)%get_last_log_byte_pushed_to_kvp_indexr4   r5   openseekosSEEK_ENDmaxtellMAX_LOG_TO_KVP_LENGTHrN   formatSEEK_SETrZ   readr   Z
write_fileLOG_PUSHED_TO_KVP_INDEX_FILEstr	Exceptionreprwarningr
   )	file_nameZstart_indexfZ
seek_indexexr>   r?   r   r   r   push_log_to_kvp   s2    		
'$ro   c              C   s  y- t  t d   }  t |  j    SWd  QRXWn t k
 r } z1 | j t k rn t d t |  d t	 j
 WYd  d  } ~ Xn t k
 r } z" t d t |  d t	 j
 WYd  d  } ~ XnB t k
 r} z" t d t |  d t	 j
 WYd  d  } ~ Xn Xd S)Nrz0Reading LOG_PUSHED_TO_KVP_INDEX_FILE failed: %s.rG   z2Invalid value in LOG_PUSHED_TO_KVP_INDEX_FILE: %s.z2Failed to get the last log byte pushed to KVP: %s.r   )r]   rg   intrf   IOErrorerrnor   rN   rj   r4   rk   r8   ri   )rm   r=   r   r   r   r\     s     *r\   c             c   sC   t  j   } t  j t  j j |    z	 d  VWd  t  j |  Xd  S)N)r_   getcwdchdirpath
expanduser)ZnewdirZprevdirr   r   r   cd  s
    	rx   c              C   s   t  j   r d }  n d }  |  S)Nz
option-245zunknown-245)r   Z
is_FreeBSD)Zazure_endpointr   r   r   _get_dhcp_endpoint_option_name!  s    	ry   )rI   c       	      K   s)  d } d } d } d } d | k r. | | d <| j  d d  rM t j d  | j  d d  rl t j d	  x t d
 | d
  D] } y7 t j |  |  } t d |  | f d t j | SWq t k
 r} z; | } | | d k rt d |  | | f d t j WYd d } ~ Xq Xq W|  d S)zvWrapper around url_helper.readurl() with custom telemetry logging
    that url_helper.readurl() does not provide.
    N         ZtimeoutZretrieszGIgnoring retries kwarg passed in for communication with Azure endpoint.ZinfinitezHIgnoring infinite kwarg passed in for communication with Azure endpoint.r*   z@Successful HTTP request with Azure endpoint %s after %d attemptsrG   r   zOFailed HTTP request with Azure endpoint %s during attempt %d with exception: %s)	popr4   rk   ranger   ZreadurlrN   r5   ri   )	urlr   excZmax_readurl_attemptsZdefault_readurl_timeoutZperiodic_logging_attemptsZattemptZretr=   r   r   r   http_with_retries)  s8    

!r   c               @   sO   e  Z d  Z d d d d i Z d d   Z d d d	  Z d
 d
 d d  Z d
 S)AzureEndpointHttpClientzx-ms-agent-nameZWALinuxAgentzx-ms-versionz
2012-11-30c             C   s   d d d | i |  _  d  S)Nzx-ms-cipher-nameZDES_EDE3_CBCz!x-ms-guest-agent-public-x509-cert)extra_secure_headers)selfcertificater   r   r   __init__`  s    z AzureEndpointHttpClient.__init__Fc             C   s>   |  j  } | r. |  j  j   } | j |  j  t | d | S)Nheaders)r   copyupdater   r   )r   r   securer   r   r   r   getf  s
    	zAzureEndpointHttpClient.getNc             C   sG   |  j  } | d  k	 r1 |  j  j   } | j |  t | d | d | S)NrP   r   )r   r   r   r   )r   r   rP   extra_headersr   r   r   r   postm  s    	zAzureEndpointHttpClient.post)r   
__module____qualname__r   r   r   r   r   r   r   r   r   Y  s
   r   c               @   s   e  Z d  Z d Z d S)InvalidGoalStateXMLExceptionz9Raised when GoalState XML is invalid or has missing data.N)r   r   r   __doc__r   r   r   r   r   v  s   r   c               @   s=   e  Z d  Z d e e e d d d d   Z d d   Z d S)	GoalStateTN)unparsed_xmlazure_endpoint_clientneed_certificaterI   c             C   sv  | |  _  y t j |  |  _ WnB t j k
 rc } z t d | d t j   WYd d } ~ Xn X|  j d  |  _	 |  j d  |  _
 |  j d  |  _ xL d D]D } t |  |  d k r d
 | } t | d t j t |   q Wd |  _ |  j d  } | d k	 rr| rrt j d d d d d t  > |  j  j | d d j |  _ |  j d k rkt d   Wd QRXd S)ah  Parses a GoalState XML string and returns a GoalState object.

        @param unparsed_xml: string representing a GoalState XML.
        @param azure_endpoint_client: instance of AzureEndpointHttpClient.
        @param need_certificate: switch to know if certificates is needed.
        @return: GoalState object representing the GoalState XML string.
        z!Failed to parse GoalState XML: %srG   Nz./Container/ContainerIdz4./Container/RoleInstanceList/RoleInstance/InstanceIdz./Incarnationcontainer_idinstance_idincarnationzMissing %s in GoalState XMLzD./Container/RoleInstanceList/RoleInstance/Configuration/Certificatesr   zget-certificates-xmlr   zget certificates xmlr   r   Tz/Azure endpoint returned empty certificates xml.)zcontainer_idzinstance_idzincarnation)r   r   
fromstringrootZ
ParseErrorrN   r4   rk   _text_from_xpathr   r   r   getattrr   certificates_xmlr   r   r   r   contents)r   r   r   r   r=   attrrH   r   r   r   r   r   |  s<    	


			
	zGoalState.__init__c             C   s)   |  j  j |  } | d  k	 r% | j Sd  S)N)r   findtext)r   Zxpathelementr   r   r   r     s    zGoalState._text_from_xpath)r   r   r   rh   r   boolr   r   r   r   r   r   r   z  s   !-r   c               @   s   e  Z d  Z d d d d i Z d d   Z d d   Z e d	 d
    Z e j d d
    Z e	 d d    Z
 e e	 d d     Z e	 d d    Z e	 d d    Z e	 d d    Z e	 d d    Z d S)OpenSSLManagerprivate_keyzTransportPrivate.pemr   zTransportCert.pemc             C   s&   t  j   |  _ d  |  _ |  j   d  S)N)r   Zmkdtemptmpdir_certificategenerate_certificate)r   r   r   r   r     s    	zOpenSSLManager.__init__c             C   s   t  j |  j  d  S)N)r   Zdel_dirr   )r   r   r   r   clean_up  s    zOpenSSLManager.clean_upc             C   s   |  j  S)N)r   )r   r   r   r   r     s    zOpenSSLManager.certificatec             C   s   | |  _  d  S)N)r   )r   valuer   r   r   r     s    c             C   s   t  j d  |  j d  k	 r- t  j d  d  St |  j   t j d d d d d d d	 d
 d d d |  j d d |  j d g  d } x7 t |  j d  D]" } d | k r | | j   7} q W| |  _ Wd  QRXt  j d  d  S)Nz7Generating certificate for communication with fabric...zCertificate already generated.opensslZreqz-x509z-nodesz-subjz/CN=LinuxTransportz-daysZ32768z-newkeyzrsa:2048z-keyoutr   z-outr   r"   ZCERTIFICATEzNew certificate generated.)	r4   r5   r   rx   r   r
   certificate_namesr]   rstrip)r   r   liner   r   r   r     s     z#OpenSSLManager.generate_certificatec             C   s1   d d d |  g } t  j  | d | \ } } | S)Nr   Zx509z-nooutrP   )r
   )actionZcertcmdresultr?   r   r   r   _run_x509_action  s    zOpenSSLManager._run_x509_actionc             C   sI   |  j  d |  } d d d d d d g } t j | d | \ } } | S)	Nz-pubkeyz
ssh-keygenz-iz-mZPKCS8z-fz
/dev/stdinrP   )r   r
   )r   r   Zpub_keyZ
keygen_cmdssh_keyr?   r   r   r   _get_ssh_key_from_cert  s    z%OpenSSLManager._get_ssh_key_from_certc             C   sK   |  j  d |  } | j d  } | | d d  j d  } d j |  S)a   openssl 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'
        z-fingerprintr1   r*   :r"   )r   r   r-   r%   )r   r   Zraw_fpeqZoctetsr   r   r   _get_fingerprint_from_cert  s    	z)OpenSSLManager._get_fingerprint_from_certc             C   s   t  j |  j d  } | j } d d d d d | j d  g } t |  j  ; t j d j |  j	   d	 d
 d d j
 |  \ } } Wd QRX| S)zDecrypt the certificates XML document using the our private key;
           return the list of certs and private keys contained in the doc.
        z.//Datas   MIME-Version: 1.0s<   Content-Disposition: attachment; filename="Certificates.p7m"s?   Content-Type: application/x-pkcs7-mime; name="Certificates.p7m"s!   Content-Transfer-Encoding: base64    zutf-8zuopenssl cms -decrypt -in /dev/stdin -inkey {private_key} -recip {certificate} | openssl pkcs12 -nodes -password pass:shellTrP   s   
N)r   r   r   r   encoderx   r   r
   rd   r   r%   )r   r   tagZcertificates_contentlinesr>   r?   r   r   r   _decrypt_certs_from_xml  s    	%z&OpenSSLManager._decrypt_certs_from_xmlc       	      C   s   |  j  |  } g  } i  } x | j   D]} } | j |  t j d |  rV g  } q( t j d |  r( d j |  } |  j |  } |  j |  } | | | <g  } q( W| S)zGiven the Certificates XML document, return a dictionary of
           fingerprints and associated SSH keys derived from the certs.z[-]+END .*?KEY[-]+$z[-]+END .*?CERTIFICATE[-]+$
)r   
splitlinesappendrematchr%   r   r   )	r   r   r>   Zcurrentkeysr   r   r   fingerprintr   r   r   parse_certificates  s    	

z!OpenSSLManager.parse_certificatesN)r   r   r   r   r   r   propertyr   setterr   r   staticmethodr   r   r   r   r   r   r   r   r   r     s   r   c            
   @   s   e  Z d  Z e j d  Z e j d  Z d Z d Z d Z	 d Z
 e e e d d d	 d
    Z e d d d d     Z e e d d d d     Z d d e e e e e d d d   Z e e d d d d     Z d S)GoalStateHealthReportera          <?xml version="1.0" encoding="utf-8"?>
        <Health xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns:xsd="http://www.w3.org/2001/XMLSchema">
          <GoalStateIncarnation>{incarnation}</GoalStateIncarnation>
          <Container>
            <ContainerId>{container_id}</ContainerId>
            <RoleInstanceList>
              <Role>
                <InstanceId>{instance_id}</InstanceId>
                <Health>
                  <State>{health_status}</State>
                  {health_detail_subsection}
                </Health>
              </Role>
            </RoleInstanceList>
          </Container>
        </Health>
        z        <Details>
          <SubStatus>{health_substatus}</SubStatus>
          <Description>{health_description}</Description>
        </Details>
        ZReadyZNotReadyZProvisioningFailedi   N)
goal_stater   endpointrI   c             C   s   | |  _  | |  _ | |  _ d S)a?  Creates instance that will report provisioning status to an endpoint

        @param goal_state: An instance of class GoalState that contains
            goal state info such as incarnation, container id, and instance id.
            These 3 values are needed when reporting the provisioning status
            to Azure
        @param azure_endpoint_client: Instance of class AzureEndpointHttpClient
        @param endpoint: Endpoint (string) where the provisioning status report
            will be sent to
        @return: Instance of class GoalStateHealthReporter
        N)_goal_state_azure_endpoint_client	_endpoint)r   r   r   r   r   r   r   r   J  s    		z GoalStateHealthReporter.__init__)rI   c             C   s   |  j  d |  j j d |  j j d |  j j d |  j  } t j d  y |  j d |  Wn? t	 k
 r } z t
 d | d t j   WYd  d  } ~ Xn Xt j d	  d  S)
Nr   r   r   statusz Reporting ready to Azure fabric.documentz#exception while reporting ready: %srG   zReported ready to Azure fabric.)build_reportr   r   r   r   PROVISIONING_SUCCESS_STATUSr4   r5   _post_health_reportri   rN   errorrE   )r   r   r=   r   r   r   send_ready_signal]  s    	

z)GoalStateHealthReporter.send_ready_signal)r   rI   c             C   s   |  j  d |  j j d |  j j d |  j j d |  j d |  j d |  } y |  j d |  WnE t k
 r } z% d | } t	 | d	 t
 j   WYd  d  } ~ Xn Xt
 j d
  d  S)Nr   r   r   r   	substatusr   r   z%exception while reporting failure: %srG   z!Reported failure to Azure fabric.)r   r   r   r   r   PROVISIONING_NOT_READY_STATUSPROVISIONING_FAILURE_SUBSTATUSr   ri   rN   r4   r   rk   )r   r   r   r=   rH   r   r   r   send_failure_signalo  s    				
z+GoalStateHealthReporter.send_failure_signal)r   r   r   r   rI   c       	      C   s   d } | d  k	 rF |  j  j d t |  d t | d  |  j    } |  j j d t t |   d t |  d t |  d t |  d |  } | S)	Nr"   Zhealth_substatusZhealth_descriptionr   r   r   Zhealth_statusZhealth_detail_subsection)%HEALTH_DETAIL_SUBSECTION_XML_TEMPLATErd   r	   "HEALTH_REPORT_DESCRIPTION_TRIM_LENHEALTH_REPORT_XML_TEMPLATErh   )	r   r   r   r   r   r   r   Zhealth_detailZhealth_reportr   r   r   r     s    	z$GoalStateHealthReporter.build_report)r   rI   c             C   sf   t    t j d  t j d  d j |  j  } |  j j | d | d d d i t j d  d  S)	Nr   z&Sending health report to Azure fabric.zhttp://{}/machine?comp=healthrP   r   zContent-Typeztext/xml; charset=utf-8z/Successfully sent health report to Azure fabric)	ro   r7   Zsleepr4   r5   rd   r   r   r   )r   r   r   r   r   r   r     s    	z+GoalStateHealthReporter._post_health_report)r   r   r   r#   dedentr   r   r   r   r   r   r   r   rh   r   r   r   r   r   r   r   r   r   r   r   '  s$   		'r   c               @   s  e  Z d  Z d d d d  Z d d   Z e d d    Z e d d	    Z e d
 d    Z	 e e
 d d d    Z e e
 d d     Z e e
 d d     Z e e
 d d     Z e e
 d d d d    Z e
 d e d d d    Z e
 e d d d d     Z e
 e e d d d     Z e
 e d d d      Z e
 e e e d! d" d#     Z e
 e e e d$ d% d&     Z e e e e d' d( d)     Z d S)*WALinuxAgentShimNc             C   sA   t  j d |  | |  _ d  |  _ d  |  _ d  |  _ | |  _ d  S)Nz5WALinuxAgentShim instantiated, fallback_lease_file=%s)r4   r5   dhcpoptionsr   openssl_managerr   
lease_file)r   fallback_lease_filedhcp_optionsr   r   r   r     s    					zWALinuxAgentShim.__init__c             C   s    |  j  d  k	 r |  j  j   d  S)N)r   r   )r   r   r   r   r     s    zWALinuxAgentShim.clean_upc              C   s(   t  j   }  t j j |  j j   d  S)Nzdhclient.hooks)r   ZInitr_   rv   r%   pathsZget_runpath)Z_pathsr   r   r   _get_hooks_dir  s    zWALinuxAgentShim._get_hooks_dirc             C   s1   |  j  d  k r* |  j |  j |  j  |  _  |  j  S)N)r   find_endpointr   r   )r   r   r   r   r     s    zWALinuxAgentShim.endpointc             C   s   |  j  d d  } t |  d k r d } x= | j d  D], } t |  d k r\ d | } | | 7} q: Wt j d t | j  d d  d   } n | j d	  } t j |  S)
N\r"   r,   r   r*   0z>L   zutf-8)	replacelenr-   structZpackrq   r   socketZ	inet_ntoa)Zfallback_lease_valueZunescaped_valueZ
hex_stringZhex_pairZpacked_bytesr   r   r   get_ip_from_lease_value  s    
$z(WALinuxAgentShim.get_ip_from_lease_valuec             C   s   t  j d d |  S)NZ
OPTION_245leases_d)r   Znetworkd_get_option_from_leases)r   r   r   r   _networkd_get_value_from_leases  s    z0WALinuxAgentShim._networkd_get_value_from_leasesc             C   s   g  } y t  j |   } Wn< t k
 rW } z t j d |  |  d  SWYd  d  } ~ Xn Xt j d |  t   } xO | j   D]A } | | k r~ | j | j	 d  j
 d d  d j	 d   q~ Wt |  d k  r d  S| d Sd  S)	NzFailed to read %s: %szcontent is %s r   r*   z;
"r   r   )r   	load_filerr   r4   r   r5   ry   r   r   stripr-   r   )r   ZleasesZcontentrn   Zoption_namer   r   r   r   _get_value_from_leases_file  s    	3z,WALinuxAgentShim._get_value_from_leases_filec                 s   i  }  t  j     t j j    s8 t j d    d  S  f d d   t j    D } x | D] } y> t j j |  j	 d d  } t
 j t j |   |  | <Wqa t k
 r } z  t d j d |   |  WYd  d  } ~ Xqa Xqa W|  S)Nz%s not found.c                s%   g  |  ] } t  j j   |   q Sr   )r_   rv   r%   )r    x)	hooks_dirr   r   r!     s   	z8WALinuxAgentShim._load_dhclient_json.<locals>.<listcomp>z.jsonr"   z{_file} is not valid JSON dataZ_file)r   r   r_   rv   existsr4   r5   listdirbasenamer   rX   loadsr   r   r8   rd   )r   Z
hook_filesZ	hook_filer   r=   r   )r   r   _load_dhclient_json  s      z$WALinuxAgentShim._load_dhclient_jsonc             C   s[   |  d  k r d  Sd  } x> |  D]6 } |  | j  d d   } | d  k	 r t j d  Pq W| S)NZunknown_245z)Endpoint server found in dhclient options)r   r4   r5   )r   Z_valueZ	interfacer   r   r   _get_value_from_dhcpoptions  s    z,WALinuxAgentShim._get_value_from_dhcpoptionsc             C   sJ  d } | d k	 r% | } t  j d  | d k rP t d d t  j t j   } | d k r t d d t  j t j   } t j |  } | d k r t d d t  j |  d k r t d d t  j d } n& t d |  d t  j t j |   } | d k r t	 } t d	 | d t  j t j
 |  } t d
 | d t  j | S)aL  Finds and returns the Azure endpoint using various methods.

        The Azure endpoint is searched in the following order:
        1. Endpoint from dhcp options (dhcp option 245).
        2. Endpoint from networkd.
        3. Endpoint from dhclient hook json.
        4. Endpoint from fallback lease file.
        5. The default Azure endpoint.

        @param fallback_lease_file: Fallback lease file that will be used
            during endpoint search.
        @param dhcp245: dhcp options that will be used during endpoint search.
        @return: Azure endpoint IP address.
        Nz&Using Azure Endpoint from dhcp optionszLNo Azure endpoint from dhcp options. Finding Azure endpoint from networkd...rG   zINo Azure endpoint from networkd. Finding Azure endpoint from hook json...zqNo Azure endpoint from dhclient logs. Unable to find endpoint in dhclient logs. Falling back to check lease filesz%No fallback lease file was specified.z%Looking for endpoint in lease file %sz*No lease found; using default endpoint: %szAzure endpoint found at %s)r4   r5   rN   r   r   r   r   rk   r   DEFAULT_WIRESERVER_ENDPOINTr   )r   Zdhcp245r   r   Zendpoint_ip_addressr   r   r   r     sN    



		



zWALinuxAgentShim.find_endpoint)rI   c             C   s   d } |  j  d k r9 | d k	 r9 t   |  _  |  j  j } |  j d k rW t |  |  _ |  j d | d k	  } d } | d k	 r |  j | |  } t | |  j |  j  } | j	   d | i S)a  Gets the VM's GoalState from Azure, uses the GoalState information
        to report ready/send the ready signal/provisioning complete signal to
        Azure, and then uses pubkey_info to filter and obtain the user's
        pubkeys from the GoalState.

        @param pubkey_info: List of pubkey values and fingerprints which are
            used to filter and obtain the user's pubkey values from the
            GoalState.
        @return: The list of user's authorized pubkey values.
        Nr   zpublic-keys)
r   r   r   r   r   _fetch_goal_state_from_azure_get_user_pubkeysr   r   r   )r   pubkey_infoZhttp_client_certificater   ssh_keyshealth_reporterr   r   r   "register_with_azure_and_fetch_dataa  s     	
z3WALinuxAgentShim.register_with_azure_and_fetch_data)r   rI   c             C   s\   |  j  d k r t d  |  _  |  j d d  } t | |  j  |  j  } | j d |  d S)zGets the VM's GoalState from Azure, uses the GoalState information
        to report failure/send provisioning failure signal to Azure.

        @param: user visible error description of provisioning failure.
        Nr   Fr   )r   r   r   r   r   r   )r   r   r   r  r   r   r   &register_with_azure_and_report_failure  s    z7WALinuxAgentShim.register_with_azure_and_report_failure)r   rI   c             C   s   |  j    } |  j | |  S)a   Fetches the GoalState XML from the Azure endpoint, parses the XML,
        and returns a GoalState object.

        @param need_certificate: switch to know if certificates is needed.
        @return: GoalState object representing the GoalState XML
        )"_get_raw_goal_state_xml_from_azure_parse_raw_goal_state_xml)r   r   unparsed_goal_state_xmlr   r   r   r     s    
z-WALinuxAgentShim._fetch_goal_state_from_azurec             C   s   t  j d  d j |  j  } y< t j d d d d d t   |  j j |  } Wd QRXWn? t	 k
 r } z t
 d	 | d
 t  j   WYd d } ~ Xn Xt  j d  | j S)zFetches the GoalState XML from the Azure endpoint and returns
        the XML as a string.

        @return: GoalState XML string
        zRegistering with Azure...z!http://{}/machine/?comp=goalstater   zgoalstate-retrievalr   zretrieve goalstater   Nz9failed to register with Azure and fetch GoalState XML: %srG   z#Successfully fetched GoalState XML.)r4   rE   rd   r   r   r   r   r   r   ri   rN   rk   r5   r   )r   r   Zresponser=   r   r   r   r    s    	
z3WALinuxAgentShim._get_raw_goal_state_xml_from_azure)r  r   rI   c             C   s   y t  | |  j |  } Wn? t k
 rZ } z t d | d t j   WYd d } ~ Xn Xd j d | j d | j d | j	 g  } t | d t j
 | S)a  Parses a GoalState XML string and returns a GoalState object.

        @param unparsed_goal_state_xml: GoalState XML string
        @param need_certificate: switch to know if certificates is needed.
        @return: GoalState object representing the GoalState XML
        z"Error processing GoalState XML: %srG   Nz, zGoalState XML container id: %szGoalState XML instance id: %szGoalState XML incarnation: %s)r   r   ri   rN   r4   rk   r%   r   r   r   r5   )r   r  r   r   r=   rH   r   r   r   r    s     



z*WALinuxAgentShim._parse_raw_goal_state_xml)r   r  rI   c             C   sY   g  } | j  d k	 rU | d k	 rU t j d  |  j j | j   } |  j | |  } | S)a  Gets and filters the VM admin user's authorized pubkeys.

        The admin user in this case is the username specified as "admin"
        when deploying VMs on Azure.
        See https://docs.microsoft.com/en-us/cli/azure/vm#az-vm-create.
        cloud-init expects a straightforward array of keys to be dropped
        into the admin user's authorized_keys file. Azure control plane exposes
        multiple public keys to the VM via wireserver. Select just the
        admin user's key(s) and return them, ignoring any other certs.

        @param goal_state: GoalState object. The GoalState object contains
            a certificate XML, which contains both the VM user's authorized
            pubkeys and other non-user pubkeys, which are used for
            MSI and protected extension handling.
        @param pubkey_info: List of VM user pubkey dicts that were previously
            obtained from provisioning data.
            Each pubkey dict in this list can either have the format
            pubkey['value'] or pubkey['fingerprint'].
            Each pubkey['fingerprint'] in the list is used to filter
            and obtain the actual pubkey value from the GoalState
            certificates XML.
            Each pubkey['value'] requires no further processing and is
            immediately added to the return list.
        @return: A list of the VM user's authorized pubkey values.
        Nz/Certificate XML found; parsing out public keys.)r   r4   r5   r   r   _filter_pubkeys)r   r   r  r  keys_by_fingerprintr   r   r   r     s    	z"WALinuxAgentShim._get_user_pubkeys)r
  r  rI   c             C   s   g  } x | D] } d | k r= | d r= | j  | d  q d | k r | d r | d } | |  k r} | j  |  |  q t j d |  q t j d |  q W| S)a9   Filter and return only the user's actual pubkeys.

        @param keys_by_fingerprint: pubkey fingerprint -> pubkey value dict
            that was obtained from GoalState Certificates XML. May contain
            non-user pubkeys.
        @param pubkey_info: List of VM user pubkeys. Pubkey values are added
            to the return list without further processing. Pubkey fingerprints
            are used to filter and obtain the actual pubkey values from
            keys_by_fingerprint.
        @return: A list of the VM user's authorized pubkey values.
        r   r   zIovf-env.xml specified PublicKey fingerprint %s not found in goalstate XMLzFovf-env.xml specified PublicKey with neither value nor fingerprint: %s)r   r4   rk   )r
  r  r   Zpubkeyr   r   r   r   r	    s    
	
	z WALinuxAgentShim._filter_pubkeys)r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   dictr  rh   r  r   r   r   r  r  listr   r	  r   r   r   r   r     s@   	A"r   c          
   C   s;   t  d |  d |  } z | j d |  SWd  | j   Xd  S)Nr   r   r  )r   r  r   )r   	dhcp_optsr  shimr   r   r   get_metadata_from_fabric  s
    	r  c          
   C   sG   t  d |  d |  } | s! t } z | j d |  Wd  | j   Xd  S)Nr   r   r   )r   +DEFAULT_REPORT_FAILURE_USER_VISIBLE_MESSAGEr  r   )r   r  r   r  r   r   r   report_failure_to_fabric   s    		r  c             C   s2   t  d |  d t j t  d | d t j d  S)Nzdhclient output stream: %srG   zdhclient error stream: %s)rN   r4   r5   )r>   errr   r   r   dhcp_log_cb.  s    r  c               @   s7   e  Z d  Z d d d  Z d d   Z d d   Z d S)EphemeralDHCPv4WithReportingNc             C   s%   | |  _  t d | d t  |  _ d  S)NZifaceZdhcp_log_func)reporterr   r  ephemeralDHCPv4)r   r  Znicr   r   r   r   6  s    	z%EphemeralDHCPv4WithReporting.__init__c          	   C   s:   t  j d d d d d |  j   |  j j   SWd  QRXd  S)Nr   zobtain-dhcp-leaser   zobtain dhcp leaser   )r   r   r  r  	__enter__)r   r   r   r   r  ;  s
    	z&EphemeralDHCPv4WithReporting.__enter__c             C   s   |  j  j | | |  d  S)N)r  __exit__)r   Z	excp_typeZ
excp_valueZexcp_tracebackr   r   r   r  B  s    	z%EphemeralDHCPv4WithReporting.__exit__)r   r   r   r   r  r  r   r   r   r   r  5  s   r  )HrS   rX   Zloggingr_   r   r   r   r7   r#   rT   rs   r   Zcloudinit.settingsr   Zcloudinit.netr   Z	cloudinitr   r   
contextlibr   Z	xml.etreer   Zxml.sax.saxutilsr	   r
   r   r   r   r   Zcloudinit.reportingr   Zcloudinit.net.dhcpr   r   Z	getLoggerr   r4   r   r:   rD   rM   rW   rc   rg   r   r   r  r   r/   rA   rF   rh   r9   rN   rZ   ro   r\   rx   ry   r   r   ri   r   r   r   r   r   r  r  r  r  r   r   r   r   <module>   s   		
K%
/:s a	