
    g                         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   Z ej2                   e
j4                  e            Z G d de      Zy)    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                      e Zd ZdZdZdefdZe ej                  e
j                  g d      deeef   fd              Z ej                  e
j                  g d      defd       Zedefd	       Zd
 Z ej                  e
j                  g d      d        Zedefd       Zedefd       ZdefdZdedefdZy)AWSAutoAttachInstanceNurlc                     t        j                  ||d      }|j                  dk(  r|j                  S t	        j
                  |j                  |j                        )N   )headerstimeout   codebody)r   readurlr   r   r   CloudMetadataError)selfr   r   responses       5/usr/lib/python3/dist-packages/uaclient/clouds/aws.py_get_imds_url_responsez,AWSAutoAttachInstance._get_imds_url_response   sJ    <<Wa@==C== //]]     )g      ?r   r   )retry_sleepsreturnc                     | j                         }t        j                  | j                        }| j	                  ||      }t
        j                  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_responses       r   identity_docz"AWSAutoAttachInstance.identity_doc*   s]     557ood../ 77W7M))*;<*++r   c                 "   | j                         }t        j                  | j                        }	 t	        j
                  | j                  ||            }|S # t        j                  $ r"}t        j                  d|       i cY d}~S d}~ww xY 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es        r   _get_ii_docz!AWSAutoAttachInstance._get_ii_doc3   s     557""4#3#34	ZZ++C+AF  ## 	IIEqII	s   &A B,B	B	Bc                     | j                         }|j                  dd      }|j                  dd      }t        |      xs 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)r4   getbool)r   r2   billing_productsmarketplace_product_codess       r   is_likely_proz#AWSAutoAttachInstance.is_likely_proI   sM     !!#!::&7>$*JJ/H$$O!$%H.G)HHr   c                 ,   t         D ]  }	 | j                  |      }|| _         n | j                  )t        j                  dj                  t                     S # t        $ r,}t        j                  d|t        |dd             Y d }~d }~ww xY w)N)
ip_addressz*Could not reach AWS IMDS at http://%s: %s
reason z, )	addresses)
IMDS_IP_ADDRESS_get_imds_v2_token_headersr&   	Exceptionr0   warninggetattrr   AWSNoValidIMDSjoin)r   addressr   r3   s       r   r#   z4AWSAutoAttachInstance._request_imds_v2_token_headersX   s    & 	G
99W9M $+ 	 #++))O4    AAx, s   A	B'"BB)r         c                    | j                   dk(  ry | j                   rt        | j                   iS t        j                  t        j                  |      dt        t        id      }|j                  dk(  rL|j                  | _         t        j                  j                  | j                          t        | j                   iS |j                  dk(  rd| _         y t        j                  |j                  |j                        )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   r>   r   s      r   rC   z0AWSAutoAttachInstance._get_imds_v2_token_headersk   s    ??h&__($//::<<$$Z0)+@A	
 ==C&mmDO""--doo>($//::==C&DO++X]]
 	
r   c                      y)Naws r   s    r   
cloud_typez AWSAutoAttachInstance.cloud_type       r   c                 .   	 t        j                  t              }d|dd k(  ry	 t        j                  t              j                         }t        j                  t              j                         }d|dd cxk(  r|dd k(  ry yy# t        $ r Y nw xY w)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_serials       r   	is_viablezAWSAutoAttachInstance.is_viable   s    	$../JKO!,, - ##$45;;=%%&89??A
HQqM4Z!_4 5 ! 	 	s   !B 	BBc                      y)UnsupportedFrW   rX   s    r   should_poll_for_pro_licensez1AWSAutoAttachInstance.should_poll_for_pro_license   rZ   r   wait_for_changec                *    t        j                         )N)r   InPlaceUpgradeNotSupportedError)r   rk   s     r   is_pro_license_presentz,AWSAutoAttachInstance.is_pro_license_present   s    88::r   )__name__
__module____qualname__rP   r&   strr   propertyr	   retryr   r   r   r   r*   r4   r9   r<   r#   rC   rY   rg   rj   rn   rW   r   r   r   r      s&   JK#  TZZ
--KH,d38n , I , TZZ
--KHT  I* It I I& TZZ
--IF
 G
0 C   4  "T ; ;$ ;r   r   )r-   loggingtypingr   r   uaclientr   r   r   r   r	   uaclient.cloudsr
   IMDS_IPV4_ADDRESSIMDS_IPV6_ADDRESSrB   rR   r$   r,   r_   rc   ra   rT   rQ   rS   	getLoggerreplace_top_level_logger_namero   r0   r   rW   r   r   <module>r}      s       C C 9% % $&780 =E4 7 3  1 +n< g:::8DED;9 D;r   