U
    Ad(                  	   @   s  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mZ d dlmZmZmZmZmZ d dlmZ d dlmZ d d	lmZmZmZmZm Z m!Z!m"Z" d d
l#m$Z$ d dl%m&Z& e'e(Z)dZ*dZ+dZ,dZ-dZ.dZ/dZ0e$j1ddddZ2dZ3edZ4ede4f ede4f dddZ5dd Z6e5dd  Z7e5d!d" Z8dd#e9e$j:d$d%d&Z;d'd( Z<e5e&d) fd*d+Z=e5d,d- Z>ed.d/ Z?d0d1 Z@e5dd2d3d4e9eAeeB eCeCe jDd5d6d7ZEe9e9e9eBd8d9d:ZFG d;d< d<ZGG d=d> d>eHZIG d?d@ d@ZJG dAdB dBZKG dCdD dDZLG dEdF dFZMe5dWe9eee9  ee9 dGdHdIZNe5e9dJdKdLZOdMdN ZPG dOdP dPeHZQG dQdR dReHZRG dSdT dTejSZTG dUdV dVZUdS )X    N)contextmanager)datetime)ENOENT)sleeptime)CallableListOptionalTypeVarUnion)ElementTree)escape)distrosdmisubp
temp_utils
url_helperutilversion)events)CFG_BUILTINz168.63.129.16boot-telemetryzsystem-infoZ
diagnosticZ
compressedi  z'/run/cloud-init/log_pushed_to_kvp_indexzazure-dsz initialize reporter for azure dsT)namedescriptionZreporting_enabledzThe VM encountered an error during deployment. Please visit https://aka.ms/linuxprovisioningerror for more information on remediation.T.)funcreturnc                    s    fdd}|S )Nc               
      s6   t j j jtd  | |W  5 Q R  S Q R X d S )Nr   r   parent)r   ReportEventStack__name__azure_ds_reporter)argskwargsr    A/usr/lib/python3/dist-packages/cloudinit/sources/helpers/azure.pyimpl9   s    z)azure_ds_telemetry_reporter.<locals>.implr%   )r   r'   r%   r$   r&   azure_ds_telemetry_reporter8   s    r(   c                 C   s\   | |krdS d
dd}| d}d||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   s(   dd t | dD }|  d|S )Nc                 S   s   g | ]}|qS r%   r%   ).0Zbyter%   r%   r&   
<listcomp>N   s     z<is_byte_swapped.<locals>.swap_bytestring.<locals>.<listcomp>r)    )textwrapZwrapreversejoin)swidthZddr%   r%   r&   swap_bytestringM   s    z(is_byte_swapped.<locals>.swap_bytestring-r            )r)   )splitr/   )Zprevious_idZ
current_idr2   partsZ
swapped_idr%   r%   r&   is_byte_swappedD   s    





r9   c               
   C   s8  t  stdtd ztt tt  } W n, t	k
r` } ztd|W 5 d}~X Y nX zXt
j
ddddgd	d
\}}d}|rd|kr|dd }|std| t|d  }W nb t
jk
r } ztd| |W 5 d}~X Y n2 t	k
r } ztd| |W 5 d}~X Y nX z`t
j
dddddgd	d
\}}d}|r^d|kr^|dd }|sltd| t|d  }W nd t
jk
r } ztd| |W 5 d}~X Y n2 t	k
r } ztd| |W 5 d}~X Y nX ttddt|  d t| d t| d f tj}t| |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	systemctlZshowz-pZUserspaceTimestampMonotonicT)capture=r4   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: %sr   z5kernel_start=%s user_start=%s cloudinit_activation=%sZ)r   Zuses_systemdRuntimeErrorLOGdebugfloatr   r   Zuptime
ValueErrorr   r7   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_telemetry`   s    





rJ   c                  C   sb   t  } ttddt | d | d | d d | d d | d d | d	 f tj}t| |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   r4   r)   python)	r   Zsystem_infor   rB   SYSTEMINFO_EVENT_TYPEr   Zversion_stringrD   rE   )inforI   r%   r%   r&   get_system_info   s$    



rO   logger_func)msgr   c                C   s6   t |r||  ttd| tj}tj|dhd |S )zReport a diagnostic eventzdiagnostic messagelogZexcluded_handler_types)callabler   rB   DIAGNOSTIC_EVENT_TYPErD   rE   )rR   rQ   rI   r%   r%   r&   report_diagnostic_event   s    rW   c                 C   sP   t t|}d|dd}tt| t	|tj
}tj|dddhd |S )zReport a compressed eventzgz+b64ascii)encodingdatarS   printZwebhookrT   )base64Zencodebyteszlibcompressdecoder   rB   COMPRESSED_EVENT_TYPEjsondumpsrD   rE   )Z
event_nameZevent_contentZcompressed_dataZ
event_datarI   r%   r%   r&   report_compressed_event   s     rc   Zdef_log_filec              
   C   s@  t  }td zt| dt}|dtj t| t	 |}t
d| | |tjd ||tj td|  ttt|  W 5 Q R X W n8 tk
r } zt
dt| tjd W 5 d}~X Y nX td	 z$tjd
gddd\}}td
| W n: tk
r: } zt
dt| tjd W 5 d}~X Y nX d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}rP   zcloud-init.logz#Exception when dumping log file: %sNzDumping dmesg log to KVPZdmesgFT)r_   r:   z$Exception when dumping dmesg log: %s)%get_last_log_byte_pushed_to_kvp_indexr>   r?   openseekosSEEK_ENDmaxtellMAX_LOG_TO_KVP_LENGTHrW   formatSEEK_SETrc   readr   Z
write_fileLOG_PUSHED_TO_KVP_INDEX_FILEstr	Exceptionreprwarningr   )	file_nameZstart_indexfZ
seek_indexexrG   rH   r%   r%   r&   push_log_to_kvp   s<    	

 "


rx   c               
   C   s   z0t td} t|  W  5 Q R  W S Q R X W n tk
rr } z$|jtkrbtdt| t	j
d W 5 d }~X Y nn tk
r } ztdt| t	j
d W 5 d }~X Y n8 tk
r } ztdt| t	j
d W 5 d }~X Y nX dS )Nrz0Reading LOG_PUSHED_TO_KVP_INDEX_FILE failed: %s.rP   z2Invalid value in LOG_PUSHED_TO_KVP_INDEX_FILE: %s.z2Failed to get the last log byte pushed to KVP: %s.r   )rf   rp   intro   IOErrorerrnor   rW   rs   r>   rt   rA   rr   )rv   rF   r%   r%   r&   re     s(    $



re   c              	   c   s6   t  }t t j|  z
d V  W 5 t | X d S N)rh   getcwdchdirpath
expanduser)ZnewdirZprevdirr%   r%   r&   cd7  s
    
r   c                 C   sx   |  dd}t|dkrdd}|dD ] }t|dkr>d| }||7 }q&tdt| ddd}n
|d	}t|S )
N\r,   r6   :r4   0z>L   utf-8)	replacelenr7   structZpackrz   encodesocketZ	inet_ntoa)Zfallback_lease_valueZunescaped_valueZ
hex_stringZhex_pairZpacked_bytesr%   r%   r&   get_ip_from_lease_valueA  s    

r         )rZ   retry_sleeptimeout_minutes)urlheadersrZ   r   r   r   c          	   
   C   s   |d t   }d}d}|s|d7 }ztj| ||dd}W qW n` tjk
r } z@td| |||j|jf tjd t  | |ksd	t	|kr W 5 d}~X Y nX t
| qtd
| |f tjd |S )zReadurl wrapper for querying wireserver.

    :param retry_sleep: Time to sleep before retrying.
    :param timeout_minutes: Retry up to specified number of minutes.
    :raises UrlError: on error fetching data.
    <   r   Nr4   )r   r   )r   rZ   timeoutzdFailed HTTP request with Azure endpoint %s during attempt %d with exception: %s (code=%r headers=%r)rP   zNetwork is unreachablez@Successful HTTP request with Azure endpoint %s after %d attempts)r   r   ZreadurlZUrlErrorrW   coder   r>   r?   rq   r   )	r   r   rZ   r   r   r   ZattemptresponserF   r%   r%   r&   http_with_retriesO  sB       

r   )usernamehostnamedisableSshPwdr   c                 C   s$   t d}|j| ||d}|dS )Na.          <ns0:Environment xmlns:ns0="http://schemas.dmtf.org/ovf/environment/1"
         xmlns:ns1="http://schemas.microsoft.com/windowsazure"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
          <ns1:ProvisioningSection>
            <ns1:Version>1.0</ns1:Version>
            <ns1:LinuxProvisioningConfigurationSet>
              <ns1:ConfigurationSetType>LinuxProvisioningConfiguration
              </ns1:ConfigurationSetType>
              <ns1:UserName>{username}</ns1:UserName>
              <ns1:DisableSshPasswordAuthentication>{disableSshPwd}
              </ns1:DisableSshPasswordAuthentication>
              <ns1:HostName>{hostname}</ns1:HostName>
            </ns1:LinuxProvisioningConfigurationSet>
          </ns1:ProvisioningSection>
          <ns1:PlatformSettingsSection>
            <ns1:Version>1.0</ns1:Version>
            <ns1:PlatformSettings>
              <ns1:ProvisionGuestAgent>true</ns1:ProvisionGuestAgent>
            </ns1:PlatformSettings>
          </ns1:PlatformSettingsSection>
        </ns0:Environment>
        )r   r   r   r   )r-   dedentrm   r   )r   r   r   ZOVF_ENV_TEMPLATEZretr%   r%   r&   build_minimal_ovf  s      r   c                   @   sH   e Zd ZdddZdd Zdejddd	Zdee	 ejdddZ
d
S )AzureEndpointHttpClientZWALinuxAgentz
2012-11-30)zx-ms-agent-namezx-ms-versionc                 C   s   d|d| _ d S )NZDES_EDE3_CBC)zx-ms-cipher-namez!x-ms-guest-agent-public-x509-cert)extra_secure_headers)selfcertificater%   r%   r&   __init__  s    z AzureEndpointHttpClient.__init__Fr   c                 C   s,   | j }|r | j  }|| j t||dS )N)r   )r   copyupdater   r   )r   r   securer   r%   r%   r&   get  s
    
zAzureEndpointHttpClient.getN)rZ   r   c                 C   s0   | j }|d k	r"| j  }|| t|||dS )N)rZ   r   )r   r   r   r   )r   r   rZ   extra_headersr   r%   r%   r&   post  s
    

zAzureEndpointHttpClient.post)F)NN)r    
__module____qualname__r   r   r   UrlResponser   r	   bytesr   r%   r%   r%   r&   r     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     s   r   c                   @   s2   e Zd Zdeeef eeddddZdd Z	dS )		GoalStateTN)unparsed_xmlazure_endpoint_clientneed_certificater   c              
   C   s  || _ zt|| _W n8 tjk
rN } ztd| tjd  W 5 d}~X Y nX | d| _	| d| _
| d| _dD ]0}t| |dkrxd| }t|tjd t|qxd| _| d	}|dk	r|rtjd
dtd. | j j|ddj| _| jdkrtdW 5 Q R X d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: %srP   Nz./Container/ContainerIdz4./Container/RoleInstanceList/RoleInstance/InstanceIdz./Incarnation)container_idinstance_idincarnationzMissing %s in GoalState XMLzD./Container/RoleInstanceList/RoleInstance/Configuration/Certificateszget-certificates-xmlzget certificates xmlr   T)r   z/Azure endpoint returned empty certificates xml.)r   r   
fromstringroot
ParseErrorrW   r>   rt   _text_from_xpathr   r   r   getattrr   certificates_xmlr   r   r!   r   contents)r   r   r   r   rF   attrrR   r   r%   r%   r&   r     sJ    
 
zGoalState.__init__c                 C   s   | j |}|d k	r|jS d S r}   )r   findtext)r   Zxpathelementr%   r%   r&   r     s    zGoalState._text_from_xpath)T)
r    r   r   r   rq   r   r   boolr   r   r%   r%   r%   r&   r     s    
7r   c                   @   s   e Zd Zddd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 )OpenSSLManagerzTransportPrivate.pemzTransportCert.pem)private_keyr   c                 C   s   t  | _d | _|   d S r}   )r   Zmkdtemptmpdir_certificategenerate_certificater   r%   r%   r&   r     s    
zOpenSSLManager.__init__c                 C   s   t | j d S r}   )r   Zdel_dirr   r   r%   r%   r&   clean_up  s    zOpenSSLManager.clean_upc                 C   s   | j S r}   r   r   r%   r%   r&   r     s    zOpenSSLManager.certificatec                 C   s
   || _ d S r}   r   )r   valuer%   r%   r&   r     s    c                 C   s   t d | jd k	r"t d d S t| jj tddddddd	d
ddd| jd d| jd g d}t| jd D ]}d|krr|| 7 }qr|| _W 5 Q R X t 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.)	r>   r?   r   r   r   r   certificate_namesrf   rstrip)r   r   liner%   r%   r&   r      s8    


z#OpenSSLManager.generate_certificatec                 C   s"   ddd| g}t j ||d\}}|S )Nr   Zx509z-nooutrZ   )r   )actionZcertcmdresultrH   r%   r%   r&   _run_x509_action@  s    zOpenSSLManager._run_x509_actionc                 C   s2   |  d|}ddddddg}tj||d\}}|S )	Nz-pubkeyz
ssh-keygenz-iz-mZPKCS8z-fz
/dev/stdinr   )r   r   )r   r   Zpub_keyZ
keygen_cmdssh_keyrH   r%   r%   r&   _get_ssh_key_from_certG  s    z%OpenSSLManager._get_ssh_key_from_certc                 C   s6   |  d|}|d}||d d d}d|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-fingerprintr;   r4   r   r,   )r   r   r7   r/   )r   r   Zraw_fpeqZoctetsr%   r%   r&   _get_fingerprint_from_certN  s    	
z)OpenSSLManager._get_fingerprint_from_certc              	   C   sj   t |d}|j}ddddd|dg}t| j* tjdjf | j	d	d

|d\}}W 5 Q R X |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    r   zuopenssl cms -decrypt -in /dev/stdin -inkey {private_key} -recip {certificate} | openssl pkcs12 -nodes -password pass:T   
)shellrZ   )r   r   r   r   r   r   r   r   rm   r   r/   )r   r   tagZcertificates_contentlinesrG   rH   r%   r%   r&   _decrypt_certs_from_xml\  s$    z&OpenSSLManager._decrypt_certs_from_xmlc           	      C   sv   |  |}g }i }| D ]V}|| td|r:g }qtd|rd|}| |}| |}|||< g }q|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   rG   Zcurrentkeysr   r   r   fingerprintr%   r%   r&   parse_certificatesu  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dZe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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   endpointr   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     s    z GoalStateHealthReporter.__init__r   c              
   C   s   | j | jj| jj| jj| jd}td z| j|d W n6 t	k
rp } zt
d| tjd  W 5 d }~X Y nX td d S )N)r   r   r   statusz Reporting ready to Azure fabric.documentz#exception while reporting ready: %srP   zReported ready to Azure fabric.)build_reportr   r   r   r   PROVISIONING_SUCCESS_STATUSr>   r?   _post_health_reportrr   rW   errorrN   )r   r   rF   r%   r%   r&   send_ready_signal  s     
z)GoalStateHealthReporter.send_ready_signalr   r   c              
   C   s   | j | jj| jj| jj| j| j|d}z| j|d W n: tk
rp } zd| }t	|t
jd  W 5 d }~X Y nX t
d d S )N)r   r   r   r   	substatusr   r   z%exception while reporting failure: %srP   z!Reported failure to Azure fabric.)r   r   r   r   r   PROVISIONING_NOT_READY_STATUSPROVISIONING_FAILURE_SUBSTATUSr   rr   rW   r>   r   rt   )r   r   r   rF   rR   r%   r%   r&   send_failure_signal  s    z+GoalStateHealthReporter.send_failure_signal)r   r   r   r   r   c           	      C   sb   d}|d k	r.| j jt|t|d | j d}| jjtt|t|t|t||d}|dS )Nr,   )Zhealth_substatusZhealth_description)r   r   r   Zhealth_statusZhealth_detail_subsectionr   )%HEALTH_DETAIL_SUBSECTION_XML_TEMPLATErm   r   "HEALTH_REPORT_DESCRIPTION_TRIM_LENHEALTH_REPORT_XML_TEMPLATErq   r   )	r   r   r   r   r   r   r   Zhealth_detailZhealth_reportr%   r%   r&   r     s     	
z$GoalStateHealthReporter.build_report)r   r   c                 C   sH   t   td td d| j}| jj||ddid td d S )Nr   z&Sending health report to Azure fabric.zhttp://{}/machine?comp=healthzContent-Typeztext/xml; charset=utf-8)rZ   r   z/Successfully sent health report to Azure fabric)rx   r   r>   r?   rm   r   r   r   )r   r   r   r%   r%   r&   r   	  s    
z+GoalStateHealthReporter._post_health_report)NN)r    r   r   r-   r   r   r   r   r   r   r   r   r   rq   r   r(   r   r   r   r   r   r%   r%   r%   r&   r     s<   	  r   c                   @   s   e Zd ZedddZdd Zedddd	Zedee	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f eedddZeeeedddZeeeedddZdS )WALinuxAgentShimr   c                 C   s   || _ d | _d | _d S r}   )r   openssl_managerr   )r   r   r%   r%   r&   r   ,  s    zWALinuxAgentShim.__init__c                 C   s   | j d k	r| j   d S r}   )r  r   r   r%   r%   r&   r   1  s    
zWALinuxAgentShim.clean_upNr   c              
   C   sV   zt d td|g W n4 tk
rP } ztd| t jd W 5 d }~X Y nX d S )NzEjecting the provisioning isoZejectz(Failed ejecting the provisioning iso: %srP   )r>   r?   r   rr   rW   )r   iso_devrF   r%   r%   r&   	eject_iso5  s    
zWALinuxAgentShim.eject_isoc                 C   s   d}| j dkr&|dk	r&t | _ | j j}| jdkr:t|| _| j|dk	d}d}|dk	rb| ||}t|| j| j}|dk	r| 	| |
  |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.
        Nr   )r  r   r   r   r   _fetch_goal_state_from_azure_get_user_pubkeysr   r   r  r   )r   pubkey_infor  Zhttp_client_certificater   ssh_keyshealth_reporterr%   r%   r&   "register_with_azure_and_fetch_data@  s.    
  
z3WALinuxAgentShim.register_with_azure_and_fetch_datar   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.
        NFr  r   )r   r   r  r   r   r   )r   r   r   r  r%   r%   r&   &register_with_azure_and_report_failuref  s    

  z7WALinuxAgentShim.register_with_azure_and_report_failure)r   r   c                 C   s   |   }| ||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  u  s
    
 z-WALinuxAgentShim._fetch_goal_state_from_azurec              
   C   s   t d d| j}z,tjddtd | j|}W 5 Q R X W n6 t	k
rx } zt
d| t jd  W 5 d}~X Y nX t 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=goalstatezgoalstate-retrievalzretrieve goalstater   z9failed to register with Azure and fetch GoalState XML: %srP   Nz#Successfully fetched GoalState XML.)r>   rN   rm   r   r   r   r!   r   r   rr   rW   rt   r?   r   )r   r   r   rF   r%   r%   r&   r    s&    

z3WALinuxAgentShim._get_raw_goal_state_xml_from_azure)r  r   r   c              
   C   s~   zt || j|}W n6 tk
rH } ztd| tjd  W 5 d}~X Y nX dd|j d|j d|j	 g}t|tj
d |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: %srP   Nz, zGoalState XML container id: %szGoalState XML instance id: %szGoalState XML incarnation: %s)r   r   rr   rW   r>   rt   r/   r   r   r   r?   )r   r  r   r   rF   rR   r%   r%   r&   r    s(    z*WALinuxAgentShim._parse_raw_goal_state_xml)r   r	  r   c                 C   sH   g }|j dk	rD|dk	rD| jdk	rDtd | 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   r  r>   r?   r   _filter_pubkeys)r   r   r	  r
  keys_by_fingerprintr%   r%   r&   r    s    
z"WALinuxAgentShim._get_user_pubkeys)r  r	  r   c                 C   s|   g }|D ]n}d|kr,|d r,| |d  qd|krj|d rj|d }|| kr\| | |  qvtd| qtd| q|S )a8  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   r>   rt   )r  r	  r   Zpubkeyr   r%   r%   r&   r    s"    z WALinuxAgentShim._filter_pubkeys)NN)r    r   r   rq   r   r   r(   r  r	   r   r  r  r   r   r  r   r  r   r  listr  r   dictr  r%   r%   r%   r&   r  +  s<   
   
%
! )r  )r   r	  r  c                 C   s,   t | d}z|j||dW S |  X d S )Nr  )r	  r  )r  r   r  )r   r	  r  shimr%   r%   r&   get_metadata_from_fabric  s    
 
r  r  c                 C   s.   t | d}t}z|j|d W 5 |  X d S )Nr  r  )r  +DEFAULT_REPORT_FAILURE_USER_VISIBLE_MESSAGEr   r  )r   r  r   r%   r%   r&   report_failure_to_fabric  s
    
r  c                 C   s(   t d|  tjd t d| tjd d S )Nzdhclient output stream: %srP   zdhclient error stream: %s)rW   r>   r?   )rG   errr%   r%   r&   dhcp_log_cb)  s      r  c                   @   s   e Zd ZdS )BrokenAzureDataSourceNr    r   r   r%   r%   r%   r&   r  2  s   r  c                   @   s   e Zd ZdS )NonAzureDataSourceNr  r%   r%   r%   r&   r  6  s   r  c                   @   s&   e Zd ZdZeed  dddZdS )ChassisAssetTagz 7783-7084-3265-9085-8269-3286-77r   c                 C   s\   t d}z| |}W n( tk
r>   td| tjd Y dS X td||jf tjd |S )zCheck platform environment to report if this datasource may run.

        :returns: ChassisAssetTag if matching tag found, else None.
        zchassis-asset-tagzNon-Azure chassis asset tag: %rrP   Nz Azure chassis asset tag: %r (%s))r   Zread_dmi_datarA   rW   r>   r?   r   )clsZ	asset_tagr   r%   r%   r&   query_system=  s    
zChassisAssetTag.query_systemN)r    r   r   ZAZURE_CLOUDclassmethodr	   r!  r%   r%   r%   r&   r  :  s   r  c                   @   s   e Zd ZdddZdddddddddee ee ee ee ee eee	  eee dd	dd	Z
ed
ddZeed dddZdeeedddZdeeeedddZdd Zdd Zdd ZdS )	OvfEnvXmlz)http://schemas.dmtf.org/ovf/environment/1z)http://schemas.microsoft.com/windowsazure)ZovfwaNFr   passwordr   custom_datadisable_ssh_password_authpublic_keyspreprovisioned_vmpreprovisioned_vm_type)	r   r&  r   r'  r(  r)  r*  r+  r   c          	      C   s8   || _ || _|| _|| _|| _|p$g | _|| _|| _d S r}   r%  )	r   r   r&  r   r'  r(  r)  r*  r+  r%   r%   r&   r   Z  s    
zOvfEnvXml.__init__r   c                 C   s   | j |j kS r}   )__dict__)r   otherr%   r%   r&   __eq__o  s    zOvfEnvXml.__eq__)ovf_env_xmlr   c              
   C   sz   zt |}W n6 t jk
rD } zd| }t||W 5 d}~X Y nX |d| js\tdt }|| |	| |S )zParser for ovf-env.xml data.

        :raises NonAzureDataSource: if XML is not in Azure's format.
        :raises BrokenAzureDataSource: if XML is unparseable or invalid.
        zInvalid ovf-env.xml: %sNz./wa:ProvisioningSectionz=Ignoring non-Azure ovf-env.xml: ProvisioningSection not found)
r   r   r   r  r   
NAMESPACESr  r#  &_parse_linux_configuration_set_section _parse_platform_settings_section)r   r/  r   rF   Z	error_strinstancer%   r%   r&   
parse_textr  s    

zOvfEnvXml.parse_textr$  )r   required	namespacec                 C   sl   | d||f tj}t|dkrDd| }t| |r@t|d S t|dkrdtd|t|f |d S )Nz./%s:%sr   #No ovf-env.xml configuration for %rr4   :Multiple configuration matches in ovf-exml.xml for %r (%d))findallr#  r0  r   r>   r?   r  )r   noder   r5  r6  matchesrR   r%   r%   r&   _find  s"    
 

zOvfEnvXml._find)r   r5  decode_base64
parse_boolc           
      C   s   | d| tj}t|dkr@d| }t| |r<t||S t|dkr`td|t|f |d j}	|	d krv|}	|r|	d k	rt	d
|	 }	|rt|	}	|	S )Nz./wa:r   r7  r4   r8  r,   )r9  r#  r0  r   r>   r?   r  r   r\   Z	b64decoder/   r7   r   Ztranslate_bool)
r   r:  r   r5  r=  r>  defaultr;  rR   r   r%   r%   r&   _parse_property  s*    	



zOvfEnvXml._parse_propertyc                 C   s   | j |ddd}| j |ddd}| j|dddd| _| j|ddd| _| j|d	dd| _| j|d
dd| _| j|dddd| _| | d S )NZProvisioningSectionTr5  Z!LinuxProvisioningConfigurationSetZ
CustomDataF)r=  r5  ZUserNameZUserPasswordZHostNameZ DisableSshPasswordAuthentication)r>  r5  )r<  r@  r'  r   r&  r   r(  _parse_ssh_section)r   r   Zprovisioning_section
config_setr%   r%   r&   r1    sL            z0OvfEnvXml._parse_linux_configuration_set_sectionc                 C   sL   | j |ddd}| j |ddd}| j|ddddd| _| j|ddd| _d S )	NZPlatformSettingsSectionTrA  ZPlatformSettingsZPreprovisionedVmF)r>  r?  r5  ZPreprovisionedVMType)r<  r@  r*  r+  )r   r   Zplatform_settings_sectionZplatform_settingsr%   r%   r&   r2    s,        z*OvfEnvXml._parse_platform_settings_sectionc           	      C   s   g | _ | j|ddd}|d kr"d S | j|ddd}|d kr>d S |dtjD ]N}| j|ddd}| j|ddd}| j|dd	dd
}|||d}| j | qLd S )NZSSHFrA  Z
PublicKeysz./wa:PublicKeyZFingerprintPathZValuer,   )r?  r5  )r   r   r   )r)  r<  r9  r#  r0  r@  r   )	r   rC  Zssh_sectionZpublic_keys_sectionZ
public_keyr   r   r   r   r%   r%   r&   rB    s@            zOvfEnvXml._parse_ssh_section)r$  )FFN)r    r   r   r0  r	   rq   r   r   r   r  r   r.  r"  r4  r<  r@  r1  r2  rB  r%   r%   r%   r&   r#  T  sV   
    $"r#  )NN)Vr\   enumra   Zloggingrh   r   r   r   r-   r]   
contextlibr   r   r|   r   r   r   typingr   r   r	   r
   r   Z	xml.etreer   Zxml.sax.saxutilsr   Z	cloudinitr   r   r   r   r   r   r   Zcloudinit.reportingr   Zcloudinit.settingsr   Z	getLoggerr    r>   ZDEFAULT_WIRESERVER_ENDPOINTrC   rM   rV   r`   rl   rp   r   r!   r  r   r(   r9   rJ   rO   rq   rB   rW   rc   rx   re   r   r   r  r   rz   r   r   r   r   rr   r   r   r   r   r  r  r  r  r  r  Enumr  r#  r%   r%   r%   r&   <module>   s   $
 
S
(

	7  "?  " f  
		