o
    ÙKÕi­  ã                   @   s¤   d dl Z d dl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ZdZeefZdZdZd	Zd
ZdZdZdZdZed Ze e
 e¡¡ZG dd„ deƒZdS )é    N)ÚAnyÚDict)Ú
exceptionsÚhttpÚsecret_managerÚsystemÚutil)ÚPublicCloudAutoAttachInstancez169.254.169.254z[fd00:ec2::254]zhttp://{}/latest/api/tokenz0http://{}/latest/dynamic/instance-identity/pkcs7z3http://{}/latest/dynamic/instance-identity/documentz/sys/hypervisor/uuidz /sys/class/dmi/id/product_serialz/sys/class/dmi/id/product_uuidÚ21600zX-aws-ec2-metadata-tokenz-ttl-secondsc                   @   sì   e Zd ZdZdZdefdd„Zeej	e
jg d¢d�deeef fdd	„ƒƒZej	e
jg d¢d�defd
d„ƒZedefdd„ƒZdd„ Zej	e
jg d¢d�dd„ ƒZedefdd„ƒZedefdd„ƒZdefdd„Zdedefdd„ZdS )ÚAWSAutoAttachInstanceNÚurlc                 C   s2   t j||dd�}|jdkr|jS tj|j|jd�‚)Né   )ÚheadersÚtimeoutéÈ   ©ÚcodeÚbody)r   Úreadurlr   r   r   ÚCloudMetadataError)Úselfr   r   Úresponse© r   ú5/usr/lib/python3/dist-packages/uaclient/clouds/aws.pyÚ_get_imds_url_response   s   
ÿz,AWSAutoAttachInstance._get_imds_url_response)g      à?r   r   )Úretry_sleepsÚreturnc                 C   s6   |   ¡ }t | j¡}| j||d�}tj |¡ d|iS )N©r   Úpkcs7)Ú_request_imds_v2_token_headersÚIMDS_URLÚformatÚ_ip_addressr   r   ÚsecretsÚ
add_secret)r   r   r   Úimds_url_responser   r   r   Úidentity_doc*   s
   z"AWSAutoAttachInstance.identity_docc              
   C   sf   |   ¡ }t | j¡}zt | j||d�¡}W |S  tjy2 } zt 	d|¡ i W  Y d}~S d}~ww )a  
        Get the instance identity doc associated with the current instance.

        See
        https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/retrieve-iid.html
        for more context.

        @return: Dict containing the instance identity document.
        r   z-Error decoding instance identity document: %sN)
r   Ú_IMDS_IID_URLr!   r"   ÚjsonÚloadsr   ÚJSONDecodeErrorÚLOGÚdebug)r   r   r   Úii_docÚer   r   r   Ú_get_ii_doc3   s   ÿý€þz!AWSAutoAttachInstance._get_ii_docc                 C   s0   |   ¡ }| dd¡}| dd¡}t|ƒpt|ƒS )a  
        Determines if the instance is likely Ubuntu Pro.

        Criteria: if any billing-product or marketplace-product-code is
        present, then is likely a Pro instance.

        @return: Boolean indicating if the instance is likely pro or not.
        ÚbillingProductsNÚmarketplaceProductCodes)r/   ÚgetÚbool)r   r-   Úbilling_productsÚmarketplace_product_codesr   r   r   Úis_likely_proI   s   
z#AWSAutoAttachInstance.is_likely_proc                 C   s|   t D ]+}z| j|d�}W n ty) } zt d|t|ddƒ¡ W Y d }~qd }~ww || _ | jd u r<tjd 	t ¡d�‚|S )N)Ú
ip_addressz*Could not reach AWS IMDS at http://%s: %s
ÚreasonÚ z, )Ú	addresses)
ÚIMDS_IP_ADDRESSÚ_get_imds_v2_token_headersÚ	Exceptionr+   ÚwarningÚgetattrr"   r   ÚAWSNoValidIMDSÚjoin)r   Úaddressr   r.   r   r   r   r   X   s$   
ý€ÿ
ÿz4AWSAutoAttachInstance._request_imds_v2_token_headers)r   é   é   c                 C   sŠ   | j dkrd S | j rt| j iS tjt |¡dttidd�}|jdkr2|j	| _ t
j | j ¡ t| j iS |jdkr<d| _ d S tj|j|j	d�‚)NÚIMDSv1ÚPUTr   )Úmethodr   r   r   i”  r   )Ú
_api_tokenÚAWS_TOKEN_PUT_HEADERr   r   ÚIMDS_V2_TOKEN_URLr!   ÚAWS_TOKEN_REQ_HEADERÚAWS_TOKEN_TTL_SECONDSr   r   r   r#   r$   r   r   )r   r7   r   r   r   r   r<   k   s(   

ü


ÿz0AWSAutoAttachInstance._get_imds_v2_token_headersc                 C   ó   dS )NÚawsr   ©r   r   r   r   Ú
cloud_type„   ó   z AWSAutoAttachInstance.cloud_typec                 C   s„   zt  t¡}d|dd… krW dS W n	 ty   Y nw t  t¡ ¡ }t  t¡ ¡ }d|dd…   kr=|dd… kr@dS  dS dS )z$This machine is a viable AWSInstanceÚec2r   é   TF)r   Ú	load_fileÚSYS_HYPERVISOR_PRODUCT_UUIDÚFileNotFoundErrorÚDMI_PRODUCT_UUIDÚlowerÚDMI_PRODUCT_SERIAL)r   Úhypervisor_uuidÚdmi_uuidÚ
dmi_serialr   r   r   Ú	is_viableˆ   s   
ÿý"ÿzAWSAutoAttachInstance.is_viablec                 C   rM   )ÚUnsupportedFr   rO   r   r   r   Úshould_poll_for_pro_licenseš   rQ   z1AWSAutoAttachInstance.should_poll_for_pro_licenseÚwait_for_changec                C   s   t  ¡ ‚)N)r   ÚInPlaceUpgradeNotSupportedError)r   r`   r   r   r   Úis_pro_license_presentž   s   z,AWSAutoAttachInstance.is_pro_license_present)Ú__name__Ú
__module__Ú__qualname__rH   r"   Ústrr   Úpropertyr   Úretryr   r   r   r   r&   r/   r3   r6   r   r<   rP   r]   r_   rb   r   r   r   r   r      s(    
r   )r(   ÚloggingÚtypingr   r   Úuaclientr   r   r   r   r   Úuaclient.cloudsr	   ÚIMDS_IPV4_ADDRESSÚIMDS_IPV6_ADDRESSr;   rJ   r    r'   rU   rY   rW   rL   rI   rK   Ú	getLoggerÚreplace_top_level_logger_namerc   r+   r   r   r   r   r   Ú<module>   s&    