File: //proc/thread-self/root/lib/python3/dist-packages/uaclient/clouds/__pycache__/aws.cpython-38.pyc
U
��Jh� � @ s� d dl Z d dlZd dlmZmZ d dlmZmZmZm Z m
Z
d dlmZ dZ
dZe
efZdZdZd Zd
ZdZdZd
ZdZed Ze�e
�e��ZG dd� de�ZdS )� N)�Any�Dict)�
exceptions�http�secret_manager�system�util)�PublicCloudAutoAttachInstancez169.254.169.254z[fd00:ec2::254]zhttp://{}/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_uuidZ21600zX-aws-ec2-metadata-tokenz-ttl-secondsc @ s� e Zd ZdZdZed�dd�Zeej e
jdddgd�eee
f d�d d
���Zej e
jdddgd�ed�dd��Zeed�d
d��Zdd� Zej e
jdddgd�dd� �Zeed�dd��Zeed�dd��Zed�dd�Zeed�dd�ZdS )�AWSAutoAttachInstanceN)�urlc C s6 t j||dd�}|jdkr |jS tj|j|jd��d S )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_responseg �?r )Zretry_sleeps)�returnc C s6 | � � }t�| j�}| j||d�}tj�|� d|iS )N�r
Zpkcs7)�_request_imds_v2_token_headers�IMDS_URL�format�_ip_addressr r �secrets�
add_secret)r r
r Zimds_url_responser r r �identity_doc* s
z"AWSAutoAttachInstance.identity_docc
C sj | � � }t�| j�}zt�| j||d��}W n8 tjk
rd } zt� d|� i W Y �S d}~X Y nX |S )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 ZJSONDecodeError�LOG�debug)r r
r �ii_doc�er r r �_get_ii_doc3 s �z!AWSAutoAttachInstance._get_ii_docc C s0 | � � }|�dd�}|�dd�}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.
ZbillingProductsNZmarketplaceProductCodes)r* �get�bool)r r( Zbilling_productsZmarketplace_product_codesr r r �
is_likely_proI s
z#AWSAutoAttachInstance.is_likely_proc C s~ t D ]X}z| j|d�}W n8 tk
rP } zt�d|t|dd�� W 5 d }~X Y qX || _ q^q| jd krztjd� t �d��|S )N)�
ip_addressz*Could not reach AWS IMDS at http://%s: %s
�reason� z, )Z addresses)
�IMDS_IP_ADDRESS�_get_imds_v2_token_headers� Exceptionr&