HOME


Mini Shell 1.0
Redirecting to https://devs.lapieza.net/iniciar-sesion Redirecting to https://devs.lapieza.net/iniciar-sesion.
DIR: /lib/python3/dist-packages/cloudinit/sources/helpers/__pycache__/
Upload File :
Current File : //lib/python3/dist-packages/cloudinit/sources/helpers/__pycache__/azure.cpython-311.pyc
�

܍~c����ddlZddlZddlZddlZddlZddlZddlZddlZddlZddl	Z	ddl
mZddlmZddl
mZddlmZmZddlmZmZmZddlmZddlmZdd	lmZmZmZmZmZmZm Z dd
l!m"Z"ddl#m$Z$ej%e&��Z'dZ(d
Z)dZ*dZ+dZ,dZ-dZ.e"j/ddd���Z0dZ1d�Z2d�Z3e2d���Z4e2d���Z5dd�de6de"j7fd�Z8d �Z9e2e$d!fd"���Z:e2d#���Z;ed$���Z<d%�Z=e2dd&d'd(�d)e6d*e>d+ee6d,e?d-e?dej@fd.���ZAd/e6d0e6d1e6deBfd2�ZCGd3�d4��ZDGd5�d6eE��ZFGd7�d8��ZGGd9�d:��ZHGd;�d<��ZIGd=�d>��ZJe2		dNd?e6d@eee6dAee6fdB���ZKe2dOd?e6dCee6fdD���ZLdE�ZMGdF�dGeE��ZNGdH�dIeE��ZOGdJ�dKejP��ZQGdL�dM��ZRdS)P�N)�contextmanager)�datetime)�ENOENT)�sleep�time)�List�Optional�Union)�ElementTree)�escape)�distros�dmi�subp�
temp_utils�
url_helper�util�version)�events)�CFG_BUILTINz
168.63.129.16�boot-telemetryzsystem-info�
diagnostic�
compressedi�z'/run/cloud-init/log_pushed_to_kvp_indexzazure-dsz initialize reporter for azure dsT)�name�description�reporting_enabledz�The VM encountered an error during deployment. Please visit https://aka.ms/linuxprovisioningerror for more information on remediation.c����fd�}|S)Nc���tj�j�jt���5�|i|��cddd��S#1swxYwYdS)N�rr�parent)r�ReportEventStack�__name__�azure_ds_reporter)�args�kwargs�funcs  ��A/usr/lib/python3/dist-packages/cloudinit/sources/helpers/azure.py�implz)azure_ds_telemetry_reporter.<locals>.impl7s����
�
$����
�$�
�
�
�	)�	)�
�4��(��(�(�	)�	)�	)�	)�	)�	)�	)�	)�	)�	)�	)�	)����	)�	)�	)�	)�	)�	)s�=�A�A�)r%r's` r&�azure_ds_telemetry_reporterr)6s#���)�)�)�)�)��K�c��||krdSd	d�}|�d��}d�||d��||d��||d��|d|dg��}||kS)
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��d�tj|d��D��}|���d�|��S)Nc��g|]}|��Sr(r()�.0�bytes  r&�
<listcomp>z<is_byte_swapped.<locals>.swap_bytestring.<locals>.<listcomp>Ls��
3�
3�
3�t�d�
3�
3�
3r*r,�)�textwrap�wrap�reverse�join)�s�width�dds   r&�swap_bytestringz(is_byte_swapped.<locals>.swap_bytestringKs>��
3�
3�x�}�Q��2�2�
3�
3�
3��
�
�
�����w�w�r�{�{�r*�-r���)r,)�splitr6)�previous_id�
current_idr:�parts�
swapped_ids     r&�is_byte_swappedrDBs����j� � ��u�����

���S�!�!�E�����O�E�!�H�%�%��O�E�!�H�%�%��O�E�!�H�%�%��!�H��!�H�	
���J��*�$�$r*c�n�tj��std���t�d��	tt
����ttj����z
}n"#t$r}td��|�d}~wwxYw	tj
gd�d���\}}d}|rd|vr|�d��d	}|std
���|t|��dzz}nJ#tj$r}td|z��|�d}~wt$r}td
|z��|�d}~wwxYw	tj
gd�d���\}}d}|rd|vr|�d��d	}|std���|t|��dzz}nJ#tj$r}td|z��|�d}~wt$r}td|z��|�d}~wwxYwtjtddt!j|�����dz�dt!j|�����dz�dt!j|�����dz��tj��}tj|��|S)z[Report timestamps related to kernel initialization and systemd
    activation of cloud-initz1distro not using systemd, skipping boot telemetryzCollecting boot telemetryz*Failed to determine kernel start timestampN)�	systemctl�show�-p�UserspaceTimestampMonotonicT)�capture�=r<z8Failed to parse UserspaceTimestampMonotonic from systemdi@Bz-Failed to get UserspaceTimestampMonotonic: %sz<Failed to parse UserspaceTimestampMonotonic from systemd: %s)rFrGzcloud-init-localrH�InactiveExitTimestampMonotonicz;Failed to parse InactiveExitTimestampMonotonic from systemdz0Failed to get InactiveExitTimestampMonotonic: %sz?Failed to parse InactiveExitTimestampMonotonic from systemd: %srz
kernel_start=�Zz user_start=z cloudinit_activation=)r
�uses_systemd�RuntimeError�LOG�debug�floatrr�uptime�
ValueErrorrr?�ProcessExecutionErrorr�ReportingEvent�BOOT_EVENT_TYPEr�utcfromtimestamp�	isoformat�DEFAULT_EVENT_ORIGIN�report_event)�kernel_start�e�out�_�tsm�
user_start�cloudinit_activation�evts        r&�get_boot_telemetryrd^sX����!�!�P��N�O�O�O��I�I�)�*�*�*�P��T�V�V�}�}�u�T�[�]�]�';�';�;�����P�P�P��G�H�H�a�O�����P�������F�F�F��
�
�
���Q����	$�3�#�:�:��)�)�C�.�.��#�C��	��J���
�"�U�3�Z�Z�'�%9�:�
�
���%�����;�a�?�
�
��	����������J�Q�N�
�
��	���������
���
�
�
��	
�	
�	
���Q����	$�3�#�:�:��)�)�C�.�.��#�C��	��M���
� ,�u�S�z�z�G�/C�D�����%�����>��B�
�
��	����������M��
�
�
��	����������
����
�%�l�3�3�=�=�?�?�#�E�E�E��%�j�1�1�;�;�=�=��C�C�C��%�&:�;�;�E�E�G�G�#�M�M�		
�	�#�
�
�C��������Jsa�<A;�;
B�B�B�A$D�E
�D%�%
E
�2E�E
�A$F3�3G:�G�
G:�"G5�5G:c�X�tj��}tjtddtj���d|d�d|d�d|dd	�d
|dd�d|dd
�d|d��tj��}tj|��|S)z%Collect and report system informationzsystem informationzcloudinit_version=z, kernel_version=�releasez
, variant=�variantz, distro_name=�distrz, distro_version=r<z	, flavor=r,z, python_version=�python)	r�system_inforrV�SYSTEMINFO_EVENT_TYPEr�version_stringrZr[)�inforcs  r&�get_system_inforn�s�������D�
�
����

�"�$�$�$�$���O�O�O���O�O�O���L��O�O�O���L��O�O�O���L��O�O�O���N�N�	
�	�#���C�"�������Jr*��logger_func�msg�returnc��t|��r||��tjtd|tj��}tj|dh���|S)zReport a diagnostic eventzdiagnostic message�log��excluded_handler_types)�callablerrV�DIAGNOSTIC_EVENT_TYPErZr[)rqrprcs   r&�report_diagnostic_eventry�sf���������C����
�
�����#�	��C����U�G�<�<�<�<��Jr*c�"�tjtj|����}d|�d��d�}tjt|tj	|��t
j
��}tj|hd����|S)zReport a compressed eventzgz+b64�ascii)�encoding�data>rt�print�webhookru)�base64�encodebytes�zlib�compress�decoderrV�COMPRESSED_EVENT_TYPE�json�dumpsrZr[)�
event_name�
event_content�compressed_data�
event_datarcs     r&�report_compressed_eventr��s����(���}�)E�)E�F�F�O���&�&�w�/�/���J��
����
�:����#�	��C����$?�$?�$?�����
�Jr*�def_log_filec�R�t��}t�d��	t|d��5}|�dt
j��t|���tz
|��}td�|���|z
|��tj���|�|t
j��td|�����tjt"t%|�������ddd��n#1swxYwYnB#t&$r5}tdt)|��ztj���Yd}~nd}~wwxYwt�d	��	t-jd
gdd�
��\}}td
|��dS#t&$r6}tdt)|��ztj���Yd}~dSd}~wwxYw)azPush 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�rbrzMDumping last {0} bytes of cloud-init.log file to KVP starting from index: {1}rozcloud-init.logNz#Exception when dumping log file: %szDumping dmesg log to KVP�dmesgFT)r�rJz$Exception when dumping dmesg log: %s)�%get_last_log_byte_pushed_to_kvp_indexrPrQ�open�seek�os�SEEK_END�max�tell�MAX_LOG_TO_KVP_LENGTHry�format�SEEK_SETr��readr�
write_file�LOG_PUSHED_TO_KVP_INDEX_FILE�str�	Exception�repr�warningr)�	file_name�start_index�f�
seek_index�exr^r_s       r&�push_log_to_kvpr��ss��8�9�9�K��I�I�2�3�3�3�
�
�)�T�
"�
"�
	I�a�
�F�F�1�b�k�"�"�"��Q�V�V�X�X�(=�=�{�K�K�J�#�#�#)�6�!�&�&�(�(�Z�*?��#L�#L��I�
�
�
�
�

�F�F�:�r�{�+�+�+�#�$4�a�f�f�h�h�?�?�?��O�8�#�a�f�f�h�h�-�-�H�H�H�
	I�
	I�
	I�
	I�
	I�
	I�
	I�
	I�
	I�
	I�
	I����
	I�
	I�
	I�
	I����
�
�
��1�D��H�H�<���	
�	
�	
�	
�	
�	
�	
�	
�	
�����
�����I�I�(�)�)�)�
���G�9�U�D�A�A�A���Q����-�-�-�-�-���
�
�
��2�T�"�X�X�=���	
�	
�	
�	
�	
�	
�	
�	
�	
�	
�����
���sS�E�D
E�E�E�E�E�E�
F�&+F�F�9+G&�&
H&�0+H!�!H&c�>�	ttd��5}t|�����cddd��S#1swxYwYn�#t$rE}|jtkr+tdt|��ztj
���Yd}~n�d}~wt$r5}tdt|��ztj
���Yd}~nEd}~wt$r5}tdt|��ztj
���Yd}~nd}~wwxYwdS)N�rz0Reading LOG_PUSHED_TO_KVP_INDEX_FILE failed: %s.roz2Invalid value in LOG_PUSHED_TO_KVP_INDEX_FILE: %s.z2Failed to get the last log byte pushed to KVP: %s.r)
r�r��intr��IOError�errnorryr�rPr�rTr�)r�r]s  r&r�r�s���
�
�.��
4�
4�	!���q�v�v�x�x�=�=�	!�	!�	!�	!�	!�	!�	!�	!�	!�	!�	!�	!����	!�	!�	!�	!�	!�������7�f���#�B�T�!�W�W�L��K�
�
�
�
�����������
�
�
��@�4��7�7�J���	
�	
�	
�	
�	
�	
�	
�	
�	
������
�
�
��@�4��7�7�J���	
�	
�	
�	
�	
�	
�	
�	
�	
�����
����

�1sQ�A�!A�A�A	�	A�A	�
A�
D�;B�
D�(+C�
D�%+D�Dc#�K�tj��}tjtj�|����	dV�tj|��dS#tj|��wxYw�N)r��getcwd�chdir�path�
expanduser)�newdir�prevdirs  r&�cdr�5sk�����i�k�k�G��H�R�W�
�
��
'�
'�(�(�(��
����
������������������s�A"�"A8c	��|�dd��}t|��dkrod}|�d��D]}t|��dkrd|z}||z
}� tjdt|�dd��d����}n|�d	��}tj|��S)
N�\r2r>�:r<�0z>L��utf-8)	�replace�lenr?�struct�packr��encode�socket�	inet_ntoa)�fallback_lease_value�unescaped_value�
hex_string�hex_pair�packed_bytess     r&�get_ip_from_lease_valuer�?s���*�2�2�4��<�<�O�
�?���a����
�'�-�-�c�2�2�	#�	#�H��8�}�}��!�!���>���(�"�J�J��{�4��Z�-?�-?��R�-H�-H�"�)M�)M�N�N���&�-�-�g�6�6����L�)�)�)r*��)r}�retry_sleep�timeout_minutes�url�headersr}r�r�c	��|dzt��z}d}d}|s�|dz
}	tj|||d���}n�#tj$r^}t	d||||j|jfztj���t��|z|ksd	t|��vr�Yd}~nd}~wwxYwt|��|��t	d
||fztj���|S)z�Readurl 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.
    �<rNr<)r�r�)r�r}�timeoutzdFailed HTTP request with Azure endpoint %s during attempt %d with exception: %s (code=%r headers=%r)rozNetwork is unreachablez@Successful HTTP request with Azure endpoint %s after %d attempts)rr�readurl�UrlErrorry�coder�rPrQr�r)	r�r�r}r�r�r��attempt�responser]s	         r&�http_with_retriesr�MsD����"�T�V�V�+�G��G��H����1���	�!�)��W�4�����H�
���"�	�	�	�#�E����A�F�A�I�6�7� �I�	
�
�
�
�����$��/�/�+�s�1�v�v�5�5��6�5�5�5�5�����	����&	�k����5��8�	��g��	'��I�����
�Os�:�B'�	AB"�"B'�username�hostname�
disableSshPwdc��tjd��}|�|||���}|�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�)r3�dedentr�r�)r�r�r��OVF_ENV_TEMPLATE�rets     r&�build_minimal_ovfr��sR�� ��	����2�
!�
!��H�M�"���C��:�:�g���r*c�P�eZdZddd�Zd�Zd
dejfd�Z	ddejfd	�ZdS)�AzureEndpointHttpClient�WALinuxAgentz
2012-11-30)zx-ms-agent-namezx-ms-versionc��d|d�|_dS)N�DES_EDE3_CBC)zx-ms-cipher-namez!x-ms-guest-agent-public-x509-cert)�extra_secure_headers)�self�certificates  r&�__init__z AzureEndpointHttpClient.__init__�s�� .�1<�%
�%
��!�!�!r*Frrc��|j}|r3|j���}|�|j��t	||���S)N)r�)r��copy�updater�r�)r�r��securer�s    r&�getzAzureEndpointHttpClient.get�sL���,���	6��l�'�'�)�)�G��N�N�4�4�5�5�5� ��g�6�6�6�6r*Nc��|j}|�.|j���}|�|��t|||���S)N)r}r�)r�r�r�r�)r�r�r}�
extra_headersr�s     r&�postzAzureEndpointHttpClient.post�sK���,���$��l�'�'�)�)�G��N�N�=�)�)�)� ��4��A�A�A�Ar*)F�NN)	r!�
__module__�__qualname__r�r�r�UrlResponser�r�r(r*r&r�r��s�������*�$���G�

�
�
�7�7�
�(>�7�7�7�7�-1�B�B�	�	�B�B�B�B�B�Br*r�c��eZdZdZdS)�InvalidGoalStateXMLExceptionz9Raised when GoalState XML is invalid or has missing data.N)r!r�r��__doc__r(r*r&r�r��s������C�C�C�Cr*r�c	�@�eZdZ	d	deeefdededdfd�Zd�Z	dS)
�	GoalStateT�unparsed_xml�azure_endpoint_client�need_certificaterrNc��||_	tj|��|_n6#tj$r$}td|ztj����d}~wwxYw|�d��|_	|�d��|_
|�d��|_dD]A}t||���/d|z}t|tj���t|����Bd|_|�d	��}|�v|rvtjd
dt"���5|j�|d
���j|_|j�td���	ddd��dS#1swxYwYdSdSdS)ahParses 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: %sroNz./Container/ContainerIdz4./Container/RoleInstanceList/RoleInstance/InstanceIdz
./Incarnation)�container_id�instance_id�incarnationzMissing %s in GoalState XMLzD./Container/RoleInstanceList/RoleInstance/Configuration/Certificateszget-certificates-xmlzget certificates xmlrT)r�z/Azure endpoint returned empty certificates xml.)r�r�
fromstring�root�
ParseErrorryrPr��_text_from_xpathr�r�r�getattrr��certificates_xmlrr r"r��contents)r�r�r�r�r]�attrrqr�s        r&r�zGoalState.__init__�s ��&;��"�	�#�.�|�<�<�D�I�I���%�	�	�	�#�3�a�7��K�
�
�
�
�
�����	����!�1�1�2K�L�L����0�0�B�
�
��� �0�0��A�A���B�	8�	8�D��t�T�"�"�*�3�d�:��'����E�E�E�E�2�3�7�7�7�+�
!%����#�#�
*�
�
���?�/�?��(�+�2�(����
�
�
)-�(B�(F�(F���)G�)�)���%��(�0�6�I����1�
�
�
�
�
�
�
�
�
�
�
�
����
�
�
�
�
�
��?�?�?s&�#�A�A�A�'=E2�2E6�9E6c�L�|j�|��}|�|jSdSr�)r�find�text)r��xpath�elements   r&rzGoalState._text_from_xpaths(���)�.�.��'�'�����<���tr*)T)
r!r�r�r
r��bytesr��boolr�rr(r*r&r�r��sq������
"&�	5�5��C��J�'�5� 7�5��	5�

�5�5�5�5�n����r*r�c���eZdZddd�Zd�Zd�Zed���Zejd���Ze	d���Z
ee	d	�����Ze	d
���Z
e	d���Ze	d���Ze	d
���ZdS)�OpenSSLManagerzTransportPrivate.pemzTransportCert.pem)�private_keyr�c�l�tj��|_d|_|���dSr�)r�mkdtemp�tmpdir�_certificate�generate_certificate�r�s r&r�zOpenSSLManager.__init__s2�� �(�*�*��� ����!�!�#�#�#�#�#r*c�8�tj|j��dSr�)r�del_dirrrs r&�clean_upzOpenSSLManager.clean_ups����T�[�!�!�!�!�!r*c��|jSr��rrs r&r�zOpenSSLManager.certificates��� � r*c��||_dSr�r)r��values  r&r�zOpenSSLManager.certificates��!����r*c��t�d��|j�t�d��dSt|j��5tjddddddd	d
ddd
|jdd|jdg��d}t|jd��D]}d|vr||���z
}�||_ddd��n#1swxYwYt�d��dS)Nz7Generating certificate for communication with fabric...zCertificate already generated.�openssl�reqz-x509z-nodesz-subjz/CN=LinuxTransportz-days�32768z-newkeyzrsa:2048z-keyoutrz-outr�r2�CERTIFICATEzNew certificate generated.)	rPrQr�r�rr�certificate_namesr��rstrip)r�r��lines   r&rz#OpenSSLManager.generate_certificatesS���	�	�K�L�L�L���'��I�I�6�7�7�7��F�
���_�_�	+�	+��I������(�������*�=�9���*�=�9��
�
�
�$�K��T�3�M�B�C�C�
1�
1�� ��,�,��4�;�;�=�=�0�K��*�D��/	+�	+�	+�	+�	+�	+�	+�	+�	+�	+�	+����	+�	+�	+�	+�0	�	�	�.�/�/�/�/�/s�A:C�C�Cc�D�ddd|g}tj||���\}}|S)Nr!�x509z-noout�r})r)�action�cert�cmd�resultr_s     r&�_run_x509_actionzOpenSSLManager._run_x509_action>s/���&�(�F�3���I�c��-�-�-�	����
r*c�l�|�d|��}gd�}tj||���\}}|S)Nz-pubkey)z
ssh-keygenz-iz-m�PKCS8z-fz
/dev/stdinr*)r/r)r�r��pub_key�
keygen_cmd�ssh_keyr_s      r&�_get_ssh_key_from_certz%OpenSSLManager._get_ssh_key_from_certEs?���'�'�	�;�?�?��L�L�L�
��Y�z��8�8�8�
����r*c���|�d|��}|�d��}||dzd��d��}d�|��S)aopenssl 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-fingerprintrKr<���r�r2)r/r
r?r6)r�r��raw_fp�eq�octetss     r&�_get_fingerprint_from_certz)OpenSSLManager._get_fingerprint_from_certLsZ���&�&�~�{�C�C��
�[�[��
�
����Q����$�*�*�3�/�/���w�w�v���r*c�j�tj|���d��}|j}ddddd|�d��g}t|j��5tjdjd
i|j	��d	d
�
|�����\}}ddd��n#1swxYwY|S)z�Decrypt the certificates XML document using the our private key;
        return the list of certs and private keys contained in the doc.
        z.//DatasMIME-Version: 1.0s<Content-Disposition: attachment; filename="Certificates.p7m"s?Content-Type: application/x-pkcs7-mime; name="Certificates.p7m"s!Content-Transfer-Encoding: base64r*r�zuopenssl cms -decrypt -in /dev/stdin -inkey {private_key} -recip {certificate} | openssl pkcs12 -nodes -password pass:T�
)�shellr}Nr()rrr
rr�r�rrr�r%r6)r�r�tag�certificates_content�linesr^r_s       r&�_decrypt_certs_from_xmlz&OpenSSLManager._decrypt_certs_from_xmlZs��
�$�%5�6�6�;�;�I�F�F��"�x�� �K�N�0�� �'�'��0�0�

�����_�_�	�	��Y�*�#�#)�D�D�,0�,B�D�D���Z�Z��&�&����F�C��	�	�	�	�	�	�	�	�	�	�	����	�	�	�	��
s�>B(�(B,�/B,c�v�|�|��}g}i}|���D]�}|�|��tjd|��rg}�/tjd|��rFd�|��}|�|��}|�|��}|||<g}��|S)z�Given the Certificates XML document, return a dictionary of
        fingerprints and associated SSH keys derived from the certs.z[-]+END .*?KEY[-]+$z[-]+END .*?CERTIFICATE[-]+$�
)rB�
splitlines�append�re�matchr6r5r;)	r�rr^�current�keysr'r�r4�fingerprints	         r&�parse_certificatesz!OpenSSLManager.parse_certificatesss����*�*�+;�<�<�������N�N�$�$�
	�
	�D��N�N�4� � � ��x�.��5�5�
������8�$�?�?�
�"�i�i��0�0���5�5�k�B�B��"�=�=�k�J�J��$+��[�!�����r*N)r!r�r�r%r�r�propertyr��setterr)r�staticmethodr/r5r;rBrLr(r*r&rrs3������.�*����
$�$�$�
"�"�"��!�!��X�!���"�"���"�!�0�0�!� �0�>� ���!� ��\��
!���!� ��!���!� ��!���!� ��0!���!� ���r*rc���eZdZejd��Zejd��ZdZdZdZ	dZ
deded	e
d
dfd�Zedd
���Zede
d
dfd���Z		dde
de
de
de
d
e
f
d�Zede
d
dfd���ZdS)�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>
        �Ready�NotReady�ProvisioningFailedi�
goal_stater��endpointrrNc�0�||_||_||_dS)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�rUr�rVs    r&r�z GoalStateHealthReporter.__init__�s��"&���&;��#�!����r*c�x�|�|jj|jj|jj|j���}t�d��	|�|���n1#t$r$}td|ztj����d}~wwxYwt�d��dS)N)rr�r��statusz Reporting ready to Azure fabric.��documentz#exception while reporting ready: %srozReported ready to Azure fabric.)
�build_reportrXrr�r��PROVISIONING_SUCCESS_STATUSrPrQ�_post_health_reportr�ry�errorrm)r�r^r]s   r&�send_ready_signalz)GoalStateHealthReporter.send_ready_signal�s����$�$��(�4��)�6��(�4��3�	%�
�
��	�	�	�4�5�5�5�	��$�$�h�$�7�7�7�7���	�	�	�#�5��9��I�
�
�
�
�
�����	����	���2�3�3�3�3�3s�A/�/
B�9B�Brc�V�|�|jj|jj|jj|j|j|���}	|�|���n3#t$r&}d|z}t|tj����d}~wwxYwt�d��dS)N)rr�r�r\�	substatusrr]z%exception while reporting failure: %sroz!Reported failure to Azure fabric.)
r_rXrr�r��PROVISIONING_NOT_READY_STATUS�PROVISIONING_FAILURE_SUBSTATUSrar�ryrPrbr�)r�rr^r]rqs     r&�send_failure_signalz+GoalStateHealthReporter.send_failure_signal�s����$�$��(�4��)�6��(�4��5��9�#�
%�
�
��	��$�$�h�$�7�7�7�7���	�	�	�9�A�=�C�#�C�S�Y�?�?�?�?������	����
	���7�8�8�8�8�8s�A�
B�&!B�Brr�r�r\c�T�d}|�C|j�t|��t|d|j������}|j�tt|����t|��t|��t|��|���}|S)Nr2)�health_substatus�health_description)rr�r��
health_status�health_detail_subsection)�%HEALTH_DETAIL_SUBSECTION_XML_TEMPLATEr�r�"HEALTH_REPORT_DESCRIPTION_TRIM_LEN�HEALTH_REPORT_XML_TEMPLATEr�)	r�rr�r�r\rer�
health_detail�
health_reports	         r&r_z$GoalStateHealthReporter.build_report�s����
�� � �F�M�M�!'�	�!2�!2�#)�� I�$�"I� I�J�$�$�N���M��7�>�>��s�;�/�/�0�0���-�-��{�+�+� ��.�.�%2�?�
�
�
��r*r^c��t��td��t�d��d�|j��}|j�||ddi���t�d��dS)Nrz&Sending health report to Azure fabric.zhttp://{}/machine?comp=healthzContent-Typeztext/xml; charset=utf-8)r}r�z/Successfully sent health report to Azure fabric)r�rrPrQr�rZrYr�)r�r^r�s   r&raz+GoalStateHealthReporter._post_health_reports�������(	�a�����	�	�:�;�;�;�-�4�4�T�^�D�D���#�(�(���)�+D�E�	)�	
�	
�	
�
	�	�	�C�D�D�D�D�Dr*�rrNr�)r!r�r�r3r�rprnr`rfrgror�r�r�r�r)rcrhr_rar(r*r&rQrQ�st������!0���	�"�"��,-<�H�O�	�-�-�)�#*��$.�!�%9�"�),�&�"��"� 7�"��	"�

�"�"�"�"�*!�4�4�4�!� �4�&!�9�s�9�t�9�9�9�!� �9�0���������	�
��

�����8!�E�C�E�D�E�E�E�!� �E�E�Er*rQc�H�eZdZdefd�Zd�Zedd���Ze	ddee	efd���Z
ededdfd	���Zed
ede
fd���Zedefd���Zed
eeefd
ede
fd���Zede
dedefd���Zedededefd���ZdS)�WALinuxAgentShimrVc�0�||_d|_d|_dSr�)rV�openssl_managerr�)r�rVs  r&r�zWALinuxAgentShim.__init__*s�� ��
�9=���HL��"�"�"r*c�J�|j�|j���dSdSr�)rxrrs r&rzWALinuxAgentShim.clean_up/s.����+�� �)�)�+�+�+�+�+�,�+r*rrNc���	t�d��tjd|g��dS#t$r)}t	d|ztj���Yd}~dSd}~wwxYw)NzEjecting the provisioning iso�ejectz(Failed ejecting the provisioning iso: %sro)rPrQrr�ry)r��iso_devr]s   r&�	eject_isozWALinuxAgentShim.eject_iso3s���	��I�I�5�6�6�6��I�w��(�)�)�)�)�)���	�	�	�#�:�Q�>��I�
�
�
�
�
�
�
�
�
�
�����	���s�04�
A'�A"�"A'c��d}|j�!|�t��|_|jj}|j�t	|��|_|�|du���}d}|�|�||��}t||j|j��}|�|�	|��|�
��|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�)rxrr�r�r��_fetch_goal_state_from_azure�_get_user_pubkeysrQrVr}rc)r��pubkey_infor|�http_client_certificaterU�ssh_keys�health_reporters       r&�"register_with_azure_and_fetch_dataz3WALinuxAgentShim.register_with_azure_and_fetch_data>s���#'����'�K�,C�#1�#3�#3�D� �&*�&:�&F�#��%�-�)@�'�*�*�D�&��6�6�4�D�@�7�
�
�
����"��-�-�j�+�F�F�H�1���2�D�M�
�
�����N�N�7�#�#�#��)�)�+�+�+��r*rc���|j�td��|_|�d���}t||j|j��}|�|���dS)z�Gets 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�rQrVrh)r�rrUr�s    r&�&register_with_azure_and_report_failurez7WALinuxAgentShim.register_with_azure_and_report_failuredsm���%�-�)@��)F�)F�D�&��6�6��6�N�N�
�1���2�D�M�
�
��	�+�+��+�D�D�D�D�Dr*r�c�V�|���}|�||��S)aFetches 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_xmls   r&r�z-WALinuxAgentShim._fetch_goal_state_from_azuress4��#'�"I�"I�"K�"K���-�-�#�%5�
�
�	
r*c��t�d��d�|j��}	t	jddt���5|j�|��}ddd��n#1swxYwYn1#t$r$}td|ztj����d}~wwxYwt�d	��|j
S)
z�Fetches the GoalState XML from the Azure endpoint and returns
        the XML as a string.

        @return: GoalState XML string
        zRegistering with Azure...z!http://{}/machine/?comp=goalstatezgoalstate-retrievalzretrieve goalstaterNz9failed to register with Azure and fetch GoalState XML: %sroz#Successfully fetched GoalState XML.)rPrmr�rVrr r"r�r�r�ryr�rQr)r�r�r�r]s    r&r�z3WALinuxAgentShim._get_raw_goal_state_xml_from_azure�s+��	���,�-�-�-�1�8�8���G�G��
	��(�*�0�(����
?�
?�
 �5�9�9�#�>�>��
?�
?�
?�
?�
?�
?�
?�
?�
?�
?�
?����
?�
?�
?�
?����	�	�	�#�K����K�
�
�
�
�

�����
	����	�	�	�7�8�8�8�� � s;�B�A9�-B�9A=�=B�A=�B�
B3�B.�.B3r�c�.�	t||j|��}n1#t$r$}td|ztj����d}~wwxYwd�d|jzd|jzd|j	zg��}t|tj
���|S)aParses 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: %sroNz, zGoalState XML container id: %szGoalState XML instance id: %szGoalState XML incarnation: %s)r�r�r�ryrPr�r6r�r�rrQ)r�r�r�rUr]rqs      r&r�z*WALinuxAgentShim._parse_raw_goal_state_xml�s���	�"�'��*� ���J�J��
�	�	�	�#�4�q�8��K�
�
�
�
�
�����	�����i�i�0�:�3J�J�/�*�2H�H�/�*�2H�H�
�
�
��	 ����;�;�;�;��s��
A�A�ArUr�c���g}|j�X|�V|j�Ot�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.)rrxrPrQrL�_filter_pubkeys)r�rUr�r��keys_by_fingerprints     r&r�z"WALinuxAgentShim._get_user_pubkeys�sp��:���'�3��'��$�0��I�I�G�H�H�H�"&�"6�"I�"I��+�#�#���+�+�,?��M�M�H��r*r�c�:�g}|D]�}d|vr$|dr|�|d���*d|vrL|drD|d}||vr|�||���^t�d|���zt�d|����|S)a8Filter 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.
        rrKzIovf-env.xml specified PublicKey fingerprint %s not found in goalstate XMLzFovf-env.xml specified PublicKey with neither value nor fingerprint: %s)rFrPr�)r�r�rJ�pubkeyrKs     r&r�z WALinuxAgentShim._filter_pubkeys�s�����!�	�	�F��&� � �V�G�_� ����F�7�O�,�,�,�,��&�(�(�V�M�-B�(�$�]�3���"5�5�5��K�K� 3�K� @�A�A�A�A��K�K�8�#��������0�������r*rtr�)r!r�r�r�r�rr)r}r	rr�r�rr�r�rr�r
r��listr�rO�dictr�r(r*r&rvrv)s�������M��M�M�M�M�
,�,�,�!����!� ��!�(,�#�#�	�$�s�)�	�#�#�#�!� �#�J!�E�#�E�$�E�E�E�!� �E�!�
� $�
�	�
�
�
�!� �
�!�!�E�!�!�!�!� �!�4!��!&�s�E�z�!2����
�	���!� ��B!�'�#�'�26�'�	
�'�'�'�!� �'�R�!�T�!��!��!�!�!��\�!�!�!r*rvrVr�r|c��t|���}	|�||���|���S#|���wxYw)N�rV)r�r|)rvr�r)rVr�r|�shims    r&�get_metadata_from_fabricr�sY���X�.�.�.�D���6�6�#�W�7�
�
�	
�
�
�������
�
�������s	�=�Arc��t|���}|st}	|�|���|���dS#|���wxYw)Nr�r�)rv�+DEFAULT_REPORT_FAILURE_USER_VISIBLE_MESSAGEr�r)rVrr�s   r&�report_failure_to_fabricr�s_���X�.�.�.�D��B�A����3�3��3�L�L�L��
�
��������
�
�������s�A�Ac�~�td|ztj���td|ztj���dS)Nzdhclient output stream: %srozdhclient error stream: %s)ryrPrQ)r^�errs  r&�dhcp_log_cbr�(sT���$�s�*��	������#�c�)�s�y������r*c��eZdZdS)�BrokenAzureDataSourceN�r!r�r�r(r*r&r�r�1��������Dr*r�c��eZdZdS)�NonAzureDataSourceNr�r(r*r&r�r�5r�r*r�c�:�eZdZdZededfd���ZdS)�ChassisAssetTagz 7783-7084-3265-9085-8269-3286-77rrc���tjd��}	||��}n/#t$r"td|ztj���YdSwxYwtd|�d|j�d�tj���|S)z�Check platform environment to report if this datasource may run.

        :returns: ChassisAssetTag if matching tag found, else None.
        zchassis-asset-tagzNon-Azure chassis asset tag: %rroNzAzure chassis asset tag: z (�))r�
read_dmi_datarTryrPrQr)�cls�	asset_tagr?s   r&�query_systemzChassisAssetTag.query_system<s����%�&9�:�:�	�	��#�i�.�.�C�C���	�	�	�#�1�I�=��I�
�
�
�
��4�4�	����	 ��2;�)�)�S�X�X�X�F��	�	
�	
�	
�	
��
s�"�(A�
AN)r!r�r��AZURE_CLOUD�classmethodr	r�r(r*r&r�r�9sD������4�K���X�&7�8�����[���r*r�c�.�eZdZddd�Zddddddddd�deedeed	eed
eedeedeee	d
edeeddfd�Z
defd�Zededdfd���Z
	ddededefd�Z			d dedededefd�Zd�Zd�Zd�ZdS)!�	OvfEnvXmlz)http://schemas.dmtf.org/ovf/environment/1z)http://schemas.microsoft.com/windowsazure)�ovf�waNF�r��passwordr��custom_data�disable_ssh_password_auth�public_keys�preprovisioned_vm�preprovisioned_vm_typer�r�r�r�r�r�r�r�rrc�z�||_||_||_||_||_|pg|_||_||_dSr�r�)	r�r�r�r�r�r�r�r�r�s	         r&r�zOvfEnvXml.__init__YsL��!��
� ��
� ��
�&���)B��&�'2�'8�b���!2���&<��#�#�#r*c�"�|j|jkSr�)�__dict__)r��others  r&�__eq__zOvfEnvXml.__eq__ns���}���.�.r*�ovf_env_xmlc�N�	tj|��}n,#tj$r}d|z}t|��|�d}~wwxYw|�d|j��st
d���t��}|�|��|�	|��|S)z�Parser for ovf-env.xml data.

        :raises NonAzureDataSource: if XML is not in Azure's format.
        :raises BrokenAzureDataSource: if XML is unparseable or invalid.
        zInvalid ovf-env.xml: %sNz./wa:ProvisioningSectionz=Ignoring non-Azure ovf-env.xml: ProvisioningSection not found)
rrrr�r
�
NAMESPACESr�r��&_parse_linux_configuration_set_section� _parse_platform_settings_section)r�r�rr]�	error_str�instances      r&�
parse_textzOvfEnvXml.parse_textqs���	:��)�+�6�6�D�D���%�	:�	:�	:�1�A�5�I�'�	�2�2��9�����	:����
�y�y�3�S�^�D�D�	�$�O���
��;�;���7�7��=�=�=��1�1�$�7�7�7��s��A�;�Ar�r�required�	namespacec�P�|�d|�d|��tj��}t|��dkr2d|z}t�|��|rt
|���dSt|��dkr!t
d|t|��fz���|dS)Nz./r�r�#No ovf-env.xml configuration for %rr<�:Multiple configuration matches in ovf-exml.xml for %r (%d))�findallr�r�r�rPrQr�)r��noderr�r��matchesrqs       r&�_findzOvfEnvXml._find�s����,�,�,�"���D�D�)�9�+?�
�
���w�<�<�1���7�$�>�C��I�I�c�N�N�N��
1�+�C�0�0�0��4�
��\�\�A�
�
�'�L���W���&�'���
�
�q�z�r*�
decode_base64�
parse_boolc��|�d|ztj��}t|��dkr2d|z}t�|��|rt
|���|St|��dkr!t
d|t|��fz���|dj}	|	�|}	|r;|	�9tj	d�
|	�������}	|rtj
|	��}	|	S)Nz./wa:rr�r<r�r2)r�r�r�r�rPrQr�rr��	b64decoder6r?r�translate_bool)
r�r�rr�r�r��defaultr�rqrs
          r&�_parse_propertyzOvfEnvXml._parse_property�s���,�,�w��~�y�/C�D�D���w�<�<�1���7�$�>�C��I�I�c�N�N�N��
1�+�C�0�0�0��N�
��\�\�A�
�
�'�L���W���&�'���
�
��
����=��E��	=�U�.��$�R�W�W�U�[�[�]�]�%;�%;�<�<�E��	/��'��.�.�E��r*c��|�|dd���}|�|dd���}|�|ddd���|_|�|dd���|_|�|d	d���|_|�|d
d���|_|�|ddd���|_|�|��dS)
N�ProvisioningSectionT�r��!LinuxProvisioningConfigurationSet�
CustomDataF)r�r��UserName�UserPassword�HostName� DisableSshPasswordAuthentication)r�r�)r�r�r�r�r�r�r��_parse_ssh_section)r�r�provisioning_section�
config_sets    r&r�z0OvfEnvXml._parse_linux_configuration_set_section�s'��#�z�z��'�$� *� 
� 
���Z�Z� �/�� �
�
�
� �/�/�����	0�
�
����,�,��
�T�-�
�
��
��,�,����-�
�
��
��,�,��
�T�-�
�
��
�*.�)=�)=��.���	*>�*
�*
��&�	
���
�+�+�+�+�+r*c���|�|dd���}|�|dd���}|�|dddd���|_|�|dd���|_dS)	N�PlatformSettingsSectionTr��PlatformSettings�PreprovisionedVmF)r�r�r��PreprovisionedVMType)r�r�r�r�)r�r�platform_settings_section�platform_settingss    r&r�z*OvfEnvXml._parse_platform_settings_section�s���$(�J�J��+�d�%/�%
�%
�!�!�J�J�%�'9�D�'�
�
��"&�!5�!5������"6�"
�"
���'+�&:�&:��"��';�'
�'
��#�#�#r*c��g|_|�|dd���}|�dS|�|dd���}|�dS|�dtj��D]k}|�|dd���}|�|dd���}|�|dd	d�
��}|||d�}|j�|���ldS)N�SSHFr��
PublicKeysz./wa:PublicKey�Fingerprint�Path�Valuer2)r�r�)rKr�r)r�r�r�r�r�r�rF)	r�r��ssh_section�public_keys_section�
public_keyrKr�rr4s	         r&r�zOvfEnvXml._parse_ssh_section�s������j�j��U�U�j�C�C�����F�"�j�j����)�
�
���&��F�-�5�5��i�2�
�
�	-�	-�J��.�.��M�E�/���K��'�'�
�F�U�'�K�K�D��(�(��G�R�%�)���E� +�����G�

��#�#�G�,�,�,�,�	-�	-r*)r�)FFN)r!r�r�r�r	r�rrrr�r�r�r�r�r�r�r�r�r�r(r*r&r�r�Ss�������:�9���J�#'�"&�"&�'+�48�,0�"'�04�=�=�=��3�-�=��3�-�	=�
�3�-�=��e�_�
=�$,�D�>�=��d�4�j�)�=� �=�!)��
�=�
�=�=�=�=�*/�t�/�/�/�/���S��[�����[��:������	�
�����:$� ��"�"��"��	"�
�"��
"�"�"�"�H ,� ,� ,�D
�
�
�*-�-�-�-�-r*r�r�r�)Sr��enumr��loggingr�rGr�r�r3r��
contextlibrrr�rrr�typingrr	r
�	xml.etreer�xml.sax.saxutilsr�	cloudinitr
rrrrrr�cloudinit.reportingr�cloudinit.settingsr�	getLoggerr!rP�DEFAULT_WIRESERVER_ENDPOINTrWrkrxr�r�r�r r"r�r)rDrdrnr�rVryr�r�r�r�r�r�r�r�r�rr�r�r�r�r�rrQrvr�r�r�r�r��Enumr�r�r(r*r&�<module>rs���
�
�
�
�������������	�	�	�	�	�	�	�	�
�
�
�
�
�
�
�
���������%�%�%�%�%�%���������������������(�(�(�(�(�(�(�(�(�(�!�!�!�!�!�!�#�#�#�#�#�#�O�O�O�O�O�O�O�O�O�O�O�O�O�O�O�O�O�O�&�&�&�&�&�&�*�*�*�*�*�*��g���!�!��.��"��%��$��$��
�� I��+�F�+�	�2������+�,�	�	�	�%�%�%�8�P�P���P�f������6"����	��������$���*�)�.�9�%
�%
�%
���%
�P�
�
���
�.������*�*�*��
���
3�3�3�	�3��3��3�-�	3�
�3��
3���3�3�3���3�l��� ��14��
�����DB�B�B�B�B�B�B�B�<D�D�D�D�D�9�D�D�D�<�<�<�<�<�<�<�<�~~�~�~�~�~�~�~�~�B^E�^E�^E�^E�^E�^E�^E�^E�Bb�b�b�b�b�b�b�b�J�(,�!������$�s�)�$���c�]����������s���#�����������	�	�	�	�	�I�	�	�	�	�	�	�	�	��	�	�	������d�i����4F-�F-�F-�F-�F-�F-�F-�F-�F-�F-r*