| Current Path : /usr/lib/python3/dist-packages/uaclient/clouds/__pycache__/ |
| Current File : //usr/lib/python3/dist-packages/uaclient/clouds/__pycache__/gcp.cpython-38.pyc |
U
8�-dt � @ s� d dl Z d dlZd dlZd dlZd dlmZmZmZmZ d dl m
Z
d dlmZm
Z
mZmZ d dlmZ e�d�ZdZdZd Zd
ZdZdZd
dddd�ZG dd� de�ZdS )� N)�Any�Dict�List�Optional)� HTTPError)�
exceptions�messages�system�util)�AutoAttachCloudInstancezpro.clouds.gcpz�http://metadata/computeMetadata/v1/instance/service-accounts/default/identity?audience=contracts.canonical.com&format=full&licenses=TRUEzThttp://metadata.google.internal/computeMetadata/v1/instance/licenses/?recursive=truez&wait_for_change=truez&last_etag={etag}z/sys/class/dmi/id/product_namezGoogle Compute EngineZ8045211386737108299Z6022427724719891830Z599959289349842382Z2592866803419978320)ZxenialZbionicZfocalZjammyc @ s� e Zd Zdd� Zeejejdddgd�e e
ef d�dd���Zee
d�d d
��Z
eed�dd��Zee
d�d
d�Zed�dd�Zeed�dd�ZdS )�UAAutoAttachGCPInstancec C s
d | _ d S )N��etag��self� r �5/usr/lib/python3/dist-packages/uaclient/clouds/gcp.py�__init__% s z UAAutoAttachGCPInstance.__init__g �?� )Zretry_sleeps)�returnc
C s� z ddi}t jt|dd�\}}W n� tk
r� } z�t|dd �}d }|r�ztj|t jd�}W n tk
rr d }Y nX |r�|� dd �}|r�|n|j
}d } |r�d|�� kr�tj
jj|d �}tj
j} tj|| t|d
d�d��W 5 d }~X Y nX d
|iS )N�Metadata-Flavor�Googler )�headersZtimeout�body)�clsZerror_descriptionzservice account)Z error_msg�coder )�msg�msg_coder �
identityToken)r
�readurl� TOKEN_URLr �getattr�json�loadsZDatetimeAwareJSONDecoder�
ValueError�get�reason�lowerr Z"GCP_SERVICE_ACCT_NOT_ENABLED_ERRORr �format�namer �GCPProAccountError)
r r Zurl_responseZ_headers�er Z
error_descZ
error_detailsr r r r r �identity_doc, sB � �
�
�z$UAAutoAttachGCPInstance.identity_docc C s dS )NZgcpr r r r r �
cloud_typeL s z"UAAutoAttachGCPInstance.cloud_typec C s* t j�t�r&t�t�}t|�� kr&dS dS )z$This machine is a viable GCPInstanceTF)�os�path�exists�DMI_PRODUCT_NAMEr Z load_file�GCP_PRODUCT_NAME�strip)r Zproduct_namer r r � is_viableP s
z!UAAutoAttachGCPInstance.is_viablec C sN | j d }t�|�d�d d �}t�|�d��}|�di ��di ��dg �S ) z�Get a list of licenses from the GCP metadata.
Instance identity token (jwt) carries a list of licenses
associated with the instance itself.
Returns an empty list if licenses are not present in the metadata.
r �.r z===zutf-8ZgoogleZcompute_engineZ
license_id)r, �base64Zurlsafe_b64decode�splitr"