Spamworldpro Mini Shell
Spamworldpro


Server : Apache/2.4.52 (Ubuntu)
System : Linux webserver 6.8.0-49-generic #49~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Wed Nov 6 17:42:15 UTC 2 x86_64
User : www-data ( 33)
PHP Version : 8.1.2-1ubuntu2.21
Disable Function : NONE
Directory :  /lib/python3/dist-packages/uaclient/__pycache__/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : //lib/python3/dist-packages/uaclient/__pycache__/contract.cpython-310.pyc
o

���f}�@s�ddlZddlZddlZddlmZddlmZmZmZm	Z	m
Z
ddlmm
ZddlmZmZmZmZmZmZmZmZmZddlmZddlmZddlmZddlm Z dd	l!m"Z"m#Z#dd
l$m%Z%ddl&m'Z'dZ(d
Z)d
Z*dZ+dZ,dZ-dZ.dZ/dZ0dZ1dZ2ddddd�Z3e�4�Z5e�6e�7e8��Z9edddg�Z:Gdd�de%j;�Z<de=fdd �Z>	!dJd"ed#ee?efd$ee?efd%e@d&e@d'dfd(d)�ZA	*	!dKd"ed+ee?efd,ee?efd%e@d&e@d'e
ee@ffd-d.�ZBd/ejCd'ejDfd0d1�ZEd"efd2d3�ZFd"ed'eefd4d5�ZGd"ed6e?d'ee?effd7d8�ZHd"ed'e@fd9d:�ZId;ee?e?fd<ee?e?fd'eJfd=d>�ZK	dLd?ee?efd@e?dAe?de	e?d'eeJee?efff
dBdC�ZL		dMd+ee?efdDe	e?de	e?d'dfdEdF�ZMd"edGee?efd'ee:fdHdI�ZNdS)N�N)�
namedtuple)�Any�Dict�List�Optional�Tuple)	�clouds�event_logger�
exceptions�http�messages�secret_manager�system�util�version)�_enabled_services)�_is_attached)�UAConfig)�ATTACH_FAIL_DATE_FORMAT)�attachment_data_file�machine_id_file)�
serviceclient)�get_user_or_root_log_file_pathz/v1/context/machines/tokenz3/v1/contracts/{contract}/context/machines/{machine}z
/v1/resourcesz3/v1/resources/{resource}/context/machines/{machine}z/v1/clouds/{cloud_type}/tokenz3/v1/contracts/{contract}/machine-activity/{machine}z/v1/contractz/v1/magic-attach����)�series_overrides�series�cloud�variant�EnableByDefaultService�namer csjeZdZdZ	d(deeddf�fdd�
Zeje	j
gd�d�	d(d	d
��Zdee
effdd�Zd
e
dee
effdd�Zeje	j
gd�d�dejfdd��Z	d(de
de
dee
dee
effdd�Zdd�Zde
dee
effdd�Zdee
effdd�Zde
fdd �Z	d(de
d!e
dee
dee
effd"d#�Z	d(de
d!e
dee
defd$d%�Zd&d'�Z�ZS))�UAContractClient�contract_urlN�cfg�returncst�j|d�t��|_dS)N�r%)�super�__init__�mtf�get_machine_token_file�machine_token_file)�selfr%��	__class__��3/usr/lib/python3/dist-packages/uaclient/contract.pyr)EszUAContractClient.__init__)rrr)�retry_sleepscCs�|st�|j�}|��}|�dd�|�i�|��}|��|d<||d�}t|�}|j	t
||d�}|jdkr:t�
��|jdkrCt|�|jdkrRtjt
|j|jd	��|j}	tj�|	�d
d��|	�dg�D]}
tj�|
�d
d��qe|	S)a}Requests machine attach to the provided machine_id.

        @param contract_token: Token string providing authentication to
            ContractBearer service endpoint.
        @param machine_id: Optional unique system machine id. When absent,
            contents of /etc/machine-id will be used.

        @return: Dict of the JSON response containing the machine-token.
        �
Authorization�	Bearer {}�lastAttachment��	machineId�activityInfo)�data�headers�i�����url�code�body�machineToken��resourceTokens�token)r�get_machine_idr%r:�update�format�_get_activity_info�	isoformat�_support_old_machine_info�request_url�API_V1_ADD_CONTRACT_MACHINEr?r
�AttachInvalidTokenError�_raise_attach_forbidden_message�ContractAPIErrorr@�	json_dictr
�secrets�
add_secret�get)r-�contract_token�
attachment_dt�
machine_idr:�
activity_infor9�backcompat_data�response�
response_jsonrDr0r0r1�add_contract_machineLs8

�


�
�z%UAContractClient.add_contract_machinecCsT|��}|jt|d|d|d|dd�d�}|jdkr'tjt|j|jd��|jS)	z=Requests list of entitlements available to this machine type.�architecturer�kernel�virt�r\rr]r^)�query_paramsr<r=)rHrK�API_V1_AVAILABLE_RESOURCESr?r
rOr@rP)r-rWrYr0r0r1�available_resourcesws ��
	�z$UAContractClient.available_resourcesrTcCsN|��}|�dd�|�i�|jt|d�}|jdkr$tjt|j|jd��|j	S)Nr3r4�r:r<r=)
r:rFrGrK�API_V1_GET_CONTRACT_USING_TOKENr?r
rOr@rP)r-rTr:rYr0r0r1�get_contract_using_token�s�
�z)UAContractClient.get_contract_using_token�instancecCs~|jtj|jd�|jd�}|jdkr0|j�dd�}|r&t�	|�t
j|d��t
jt|j|j
d��|j}tj�|�dd��|S)	z�Requests contract token for auto-attach images for Pro clouds.

        @param instance: AutoAttachCloudInstance for the cloud.

        @return: Dict of the JSON response containing the contract-token.
        )�
cloud_type)r9r<�messagerB)�	error_msgr=�
contractToken)rK�,API_V1_GET_CONTRACT_TOKEN_FOR_CLOUD_INSTANCErGrg�identity_docr?rPrS�LOG�debugr
�InvalidProImagerOr@r
rQrR)r-rfrY�msgrZr0r0r1�%get_contract_token_for_cloud_instance�s*
��

�
�z6UAContractClient.get_contract_token_for_cloud_instance�
machine_token�resourcerVc	Cs�|st�|j�}|��}|�dd�|�i�tj||d�}|j||d�}|jdkr3t	j
t|j|jd��|j�d�rA|jd|j
d<|j
}|�dg�D]}tj�|�d	d
��qJ|S)a�Requests machine access context for a given resource

        @param machine_token: The authentication token needed to talk to
            this contract service endpoint.
        @param resource: Entitlement name.
        @param machine_id: Optional unique system machine id. When absent,
            contents of /etc/machine-id will be used.

        @return: Dict of the JSON response containing entitlement accessInfo.
        r3r4)rs�machinercr<r=�expiresrCrDrB)rrEr%r:rFrG�"API_V1_GET_RESOURCE_MACHINE_ACCESSrKr?r
rOr@rSrPr
rQrR)	r-rrrsrVr:r>rYrZrDr0r0r1�get_resource_machine_access�s(�
�z,UAContractClient.get_resource_machine_accesscCs�|jj}|jj�d�}t�|j�}|��}tj	||d�}|�
�}|�dd�	|�i�|j|||d�}|j
dkrAtj||j
|jd��|jrU|jj}|j|d<|j�|�d	Sd	S)
z�Report current activity token and enabled services.

        This will report to the contracts backend all the current
        enabled services in the system.
        rA��contractrtr3r4)r:r9r<r=r8N)r,�contract_idrrrSrrEr%rH�API_V1_UPDATE_ACTIVITY_TOKENrGr:rFrKr?r
rOr@rP�write)r-rzrrrV�request_datar>r:rYr0r0r1�update_activity_token�s*��

�
�z&UAContractClient.update_activity_token�magic_tokencCs�|��}|�dd�|�i�|jt|d�}|jdkrt���|jdkr't���|jdkr6tj	t|j|j
d��|j}gd�}|D]}tj
�|�|d	��q?|S)
z�Request magic attach token info.

        When the magic token is registered, it will contain new fields
        that will allow us to know that the attach process can proceed
        r3r4rcr;�r<r=�rD�userCoderjrB)r:rFrGrK�"API_V1_GET_MAGIC_ATTACH_TOKEN_INFOr?r
�MagicAttachTokenError�MagicAttachUnavailablerOr@rPr
rQrRrS)r-rr:rYrZ�
secret_fields�fieldr0r0r1�get_magic_attach_token_infos(�


�z,UAContractClient.get_magic_attach_token_infocCsx|��}|jt|dd�}|jdkrt���|jdkr$tjt|j|jd��|j}gd�}|D]}t	j
�|�|d��q-|S)z)Create a magic attach token for the user.�POST�r:�methodr�r<r=r�rB)
r:rK�API_V1_NEW_MAGIC_ATTACHr?r
r�rOr@rPr
rQrRrS)r-r:rYrZr�r�r0r0r1�new_magic_attach_token$s&�

�z'UAContractClient.new_magic_attach_tokencCs�|��}|�dd�|�i�|jt|dd�}|jdkrt���|jdkr(t���|jdkr1t�	��|jdkr@tj
t|j|jd	��d
S)z)Revoke a magic attach token for the user.r3r4�DELETEr�i�r;r�r<r=N)r:rFrGrK�API_V1_REVOKE_MAGIC_ATTACHr?r
� MagicAttachTokenAlreadyActivatedr�r�rOr@)r-rr:rYr0r0r1�revoke_magic_attach_token;s(�



��z*UAContractClient.revoke_magic_attach_tokenrzc	Cs�|st�|j�}|��}|�dd�|�i�tj||d�}|��}|j|d||d|d|d|dd	�d
�}|j	dkrFt
j||j	|jd��|j�
d
�rT|jd
|jd
<|jS)a|Get the updated machine token from the contract server.

        @param machine_token: The machine token needed to talk to
            this contract service endpoint.
        @param contract_id: Unique contract id provided by contract service
        @param machine_id: Optional unique system machine id. When absent,
            contents of /etc/machine-id will be used.
        r3r4rx�GETr\rr]r^r_)r�r:r`r<r=ru)rrEr%r:rFrG�API_V1_GET_CONTRACT_MACHINErHrKr?r
rOr@rSrP)r-rrrzrVr:r>rWrYr0r0r1�get_contract_machineRs4���

�z%UAContractClient.get_contract_machinec	Cs�|st�|j�}|��}|�dd�|�i�||��d�}t|�}tj||d�}|j	||d|d�}|j
dkr@tj||j
|j
d��|j�d	�rN|jd	|jd	<|jS)
a�Request machine token refresh from contract server.

        @param machine_token: The machine token needed to talk to
            this contract service endpoint.
        @param contract_id: Unique contract id provided by contract service.
        @param machine_id: Optional unique system machine id. When absent,
            contents of /etc/machine-id will be used.

        @return: Dict of the JSON response containing refreshed machine-token
        r3r4r6rxr�)r:r�r9r<r=ru)rrEr%r:rFrGrHrJ�API_V1_UPDATE_CONTRACT_MACHINErKr?r
rOr@rSrP)	r-rrrzrVr:r9rXr>rYr0r0r1�update_contract_machine}s*���

�z(UAContractClient.update_contract_machinecCs�t��jt��jt��jt��t��t��t	�
�d�}t|j�j
rOt|j�j}t��}|jjp4t�|j�|jjdd�|D�dd�|D�|rJ|j��ndd�}ni}i|�|�S)z9Return a dict of activity info data for contract requests)�distributionr]rr\�desktopr^�
clientVersioncSsg|]}|j�qSr0)r"��.0�servicer0r0r1�
<listcomp>�sz7UAContractClient._get_activity_info.<locals>.<listcomp>cSsi|]
}|jr|j|j�qSr0)�variant_enabledr"�variant_namer�r0r0r1�
<dictcomp>�s��z7UAContractClient._get_activity_info.<locals>.<dictcomp>N)�
activityID�
activityToken�	resources�resourceVariantsr5)r�get_release_infor��get_kernel_info�
uname_releaser�
get_dpkg_arch�
is_desktop�
get_virt_typer�get_versionrr%�is_attachedr�enabled_servicesr�readr,�activity_idrE�activity_token�attached_atrI)r-�machine_infor��attachment_datarWr0r0r1rH�s8�

�����z#UAContractClient._get_activity_info�N)�__name__�
__module__�__qualname__�cfg_url_base_attrrrr)r�retry�socket�timeoutr[r�strrrbrer�AutoAttachCloudInstancerqrwr~r�r�r�r�r�rH�
__classcell__r0r0r.r1r#Bsh����*�$����

�&(����

�/����
�(r#�request_bodyc	CsJ|�di�}|�d�||�d�|�d�|�d�|�d�dt��jd�d	�S)
a?
    Transforms a request_body that has the new activity_info into a body that
    includes both old and new forms of machineInfo/activityInfo

    This is necessary because there may be old ua-airgapped contract
    servers deployed that we need to support.
    This function is used for attach and refresh calls.
    r8r7r\r�r]r�Linux)r�r]r�type�release)r7r8r\�os)rSrr�r�)r�rWr0r0r1rJ�s	��rJTr%�past_entitlements�new_entitlements�allow_enablerr&cCsjddlm}d}g}g}||�D]|}	z||	}
Wn	ty!Yqwg}zt||�|	i�|
||d�\}}WnMtjy[}
zt�|
�d}|�	|	�t�
d|	|
�WYd}
~
qd}
~
wty�}
zt�|
�|�	|
�|�	|	�t�d|	|
�WYd}
~
qd}
~
ww|r�|r�t�
|	�qt�|�t|�dkr�tjd	d
�t||�D�d��|r�tjdd
�|D�d��dS)
a�Iterate over all entitlements in new_entitlement and apply any delta
    found according to past_entitlements.

    :param cfg: UAConfig instance
    :param past_entitlements: dict containing the last valid information
        regarding service entitlements.
    :param new_entitlements: dict containing the current information regarding
        service entitlements.
    :param allow_enable: Boolean set True if allowed to perform the enable
        operation. When False, a message will be logged to inform the user
        about the recommended enabled service.
    :param series_overrides: Boolean set True if series overrides should be
        applied to the new_access dict.
    r)�entitlements_enable_orderF)r%�orig_access�
new_accessr�rTz+Failed to process contract delta for %s: %rNz5Unexpected error processing contract delta for %s: %rcSs*g|]\}}|tjjt|�t�d�f�qS))ri�log_path)r�UNEXPECTED_ERRORrGr�r)r�r"�	exceptionr0r0r1r�/s����z.process_entitlements_delta.<locals>.<listcomp>)�failed_servicescSsg|]}|tjf�qSr0)r�!E_ATTACH_FAILURE_DEFAULT_SERVICES)r�r"r0r0r1r�<s��)�uaclient.entitlementsr��KeyError�process_entitlement_deltarSr
�UbuntuProErrorrmr��append�error�	Exception�event�service_processed�services_failed�len�AttachFailureUnknownError�zip�AttachFailureDefaultServices)r%r�r�r�rr��delta_error�unexpected_errorsr�r"�new_entitlement�deltas�service_enabled�er0r0r1�process_entitlements_delta�sr�
�

��


���
�
�����r�Fr�r�c
Cs�ddlm}|rt|�t�||�}d}|rh|�di��d�}|s*|�di��d�}|s3tj||d��|�di��di��d	d
�}	z	||||	d�}
Wntjy_}zt	�
d|�|�d
}~ww|
j|||d�}||fS)a,Process a entitlement access dictionary deltas if they exist.

    :param cfg: UAConfig instance
    :param orig_access: Dict with original entitlement access details before
        contract refresh deltas
    :param new_access: Dict with updated entitlement access details after
        contract refresh
    :param allow_enable: Boolean set True if allowed to perform the enable
        operation. When False, a message will be logged to inform the user
        about the recommended enabled service.
    :param series_overrides: Boolean set True if series overrides should be
        applied to the new_access dict.

    :raise UbuntuProError: on failure to process deltas.
    :return: A tuple containing a dict of processed deltas and a
             boolean indicating if the service was fully processed
    r��entitlement_factoryF�entitlementr�)�orig�new�entitlements�obligations�use_selectorrB�r%r"r z3Skipping entitlement deltas for "%s". No such classN�r�)r�r��apply_contract_overridesr�get_dict_deltasrSr
� InvalidContractDeltasServiceType�EntitlementNotFoundErrorrmrn�process_contract_deltas)r%r�r�r�rr�r��retr"r r��excr0r0r1r�CsD�
�
�����r�rYcCs�|j�d�}|rJ|d}|d}|dkr(|d�t�}tj|||d�d�d��|dkr@|d�t�}tj|||d�d�d	��|d
krJtj|d��t���)N�info�
contractId�reasonzno-longer-effective�timez%m-%d-%Y)rz�date�contract_expiry_dateznot-effective-yet)rzr��contract_effective_dateznever-effective)rz)	rPrS�strftimerr
�AttachForbiddenExpired�AttachForbiddenNotYet�AttachForbiddenNever�AttachExpiredToken)rYr�rzr�r�r0r0r1rN�s*��rNc	Cs�t�|�}|��}|j}|d}|ddd}t|d�}|j||d�}|�|�tj�	�|�
di��
dt�|��}t�|�t|||��dd	�d
S)z�Request contract refresh from ua-contracts service.

    :raise UbuntuProError: on failure to update contract or error processing
        contract deltas
    :raise ConnectivityError: On failure during a connection
    rA�machineTokenInfo�contractInfo�idr')rrrzr7Fr�N)
r*r+r�rrr#r�r|rrE�cache_clearrSrr�)	r%r,�orig_entitlements�
orig_tokenrrrz�contract_client�resprVr0r0r1�refresh�s*

�


�

�r	cCst|�}|��}|�dg�S)zDQuery available resources from the contract server for this machine.r�)r#rbrS)r%�clientr�r0r0r1�get_available_resources�srrDcCst|�}|�|�S)z/Query contract information for a specific token)r#re)r%rDr
r0r0r1�get_contract_information�s
rc
Cs�t|�}|jj}|j��}|�dd�}|�di��di��dd�}|s%dS|�||�}|�di��di��dd�}|r=|n|jj}|jj|krIdS|j�|�}	t|	�	��D]\}
}t
�|�|
i�|�}|rhdSqUdS)	NrArBrrrF�effectiveToT)r#r,rrr�rSr��contract_expiry_datetime�get_entitlements_from_token�sorted�itemsrr�)
r%rrrrrrzr�resp_expiry�
new_expiry�curr_entitlementsr"r�r�r0r0r1�is_contract_changed�sF

�
��������r�override_selector�selector_valuescCs<d}|��D]\}}||f|��vrdS|t|7}q|S)Nr)r�OVERRIDE_SELECTOR_WEIGHTS)rr�override_weight�selector�valuer0r0r1�_get_override_weight�srr��series_namergc
Cszi}||d�}|r
||d<|�di��|i�}|r||td<t�|�dg��}|D]}t|�d�|�}	|	r:|||	<q*|S)N)rrr rr�	overridesr)�popr�copy�deepcopyrSr)
r�rrgr rrr�general_overrides�override�weightr0r0r1�_select_overridess"

�
��r%rcCs�ddlm}tt|t�d|vg�std�|���|dur!t��j	n|}|�\}}|�
di�}t||||�}t|�
��D]%\}	}
|
�
�D]\}}|d�
|�}
t|
t�rY|
�|�qC||d|<qCq;dS)a�Apply series-specific overrides to an entitlement dict.

    This function mutates orig_access dict by applying any series-overrides to
    the top-level keys under 'entitlement'. The series-overrides are sparse
    and intended to supplement existing top-level dict values. So, sub-keys
    under the top-level directives, obligations and affordance sub-key values
    will be preserved if unspecified in series-overrides.

    To more clearly indicate that orig_access in memory has already had
    the overrides applied, the 'series' key is also removed from the
    orig_access dict.

    :param orig_access: Dict with original entitlement access details
    r)�get_cloud_typer�z?Expected entitlement access dict. Missing "entitlement" key: {}N)�uaclient.clouds.identityr&�all�
isinstance�dict�RuntimeErrorrGrr�rrSr%rrrF)r�rr r&rrg�_�orig_entitlementr�_weight�overrides_to_apply�keyr�currentr0r0r1r�#s*��
�
��r�r�c	Cs�ddlm}g}|��D]H\}}|�di��dd�}z	||||d�}Wn
tjy-Yqw|�di��di�}|�d�}	|�||	�rT|��\}
}|
rT|�t	||d	��q|S)
Nrr�r�r�rBr�r��
resourceToken)r"r )
r�r�rrSr
r��_should_enable_by_default�
can_enabler�r!)r%r�r��enable_by_default_services�ent_name�	ent_valuer �entr�r2r4r,r0r0r1�get_enabled_by_default_servicesTs,�
���r9)T)FTr�)NN)Or �loggingr��collectionsr�typingrrrrr�uaclient.files.machine_token�filesrrr*�uaclientrr	r
rrr
rrr�-uaclient.api.u.pro.status.enabled_services.v1r�(uaclient.api.u.pro.status.is_attached.v1r�uaclient.configr�uaclient.defaultsr�uaclient.files.state_filesrr�
uaclient.httpr�uaclient.logrrLr�r�rarvrkr{rdr�r�r�r�get_event_loggerr��	getLogger�replace_top_level_logger_namer�rmr!�UAServiceClientr#r*rJr��boolr�r��HTTPResponse�NamedMessagerNr	rrr�intrr%r�r9r0r0r0r1�<module>s�,������ ��
�
���
�^��
�
���

�?�
�!�
�-
�
�
��
����
��
���
�1�
��

Spamworldpro Mini