Current File : //usr/lib/python3/dist-packages/uaclient/files/__pycache__/machine_token.cpython-312.pyc
�

��g�#���ddlZddlmZddlmZmZmZddlmZmZm	Z	m
Z
ddlmZddl
mZdaGd�d�Zdd	efd
�Zy)�N)�datetime)�Any�Dict�Optional)�defaults�
exceptions�system�util)�PublicMachineTokenData)�UAFilec��eZdZejdfdedeefd�Zdefd�Z	d�Z
deefd	�Zed
��Z
ed��Zedeefd��Zd
�Zedeeeffd��Zddeefd�Ze	ddeeefdeefd��Zedeefd��Zed��Zedeefd��Zedd��Zedd��Zedd��Zed��Zed��Z ed��Z!y)�MachineTokenFileN�	directory�machine_token_overlay_pathc���tj}t||dztjz�|_t||d�|_||_d|_d|_d|_	y)N�/F)
r�MACHINE_TOKEN_FILEr�PRIVATE_SUBDIR�private_file�public_filer�_machine_token�
_entitlements�_contract_expiry_datetime)�selfrr�	file_names    �>/usr/lib/python3/dist-packages/uaclient/files/machine_token.py�__init__zMachineTokenFile.__init__
sc��
�/�/�	�"��y�3���)@�)@�@�
���"�)�Y��>���*D��'�"���!���)-��&��private_contentc���tj�r�tj|tj��}|j
j
|�tj|�jd��}tj|tj��}|jj
|�d|_d|_d|_
ytj��)z8Update the machine_token file for both pub/private files��clsF)�	keep_noneN)r
�we_are_currently_root�json�dumps�DatetimeAwareJSONEncoderr�writer�	from_dict�to_dictrrrrr�NonRootUserError)rr�private_content_str�public_content�public_content_strs     rr(zMachineTokenFile.writes����%�%�'�"&�*�*��T�%B�%B�#��
���#�#�$7�8�4�=�=����g��g�&�
�"&����D�$A�$A�"��
���"�"�#5�6�"&�D��!%�D��-1�D�*��-�-�/�/rc���tj�rJ|jj�|jj�d|_d|_d|_ytj��)z!Delete both pub and private filesN)
r
r$r�deleterrrrrr+�rs rr0zMachineTokenFile.delete4sY���%�%�'����#�#�%����$�$�&�"&�D��!%�D��-1�D�*��-�-�/�/r�returnc���tj�r
|j}n|j}|j	�}|sy	tj|tj��}|S#t$rY|SwxYw�Nr!)	r
r$rr�readr%�loads�DatetimeAwareJSONDecoder�	Exception)r�file_handler�contents   rr5zMachineTokenFile.read@ss���%�%�'��,�,�L��+�+�L��#�#�%����	��j�j��d�.K�.K�L�G�����	����	�s�%A)�)	A6�5A6c��tj�r.|jjxr|jjS|jjS�N)r
r$r�
is_presentrr1s rr=zMachineTokenFile.is_presentNsC���%�%�'��#�#�.�.�O�4�3D�3D�3O�3O�O��#�#�.�.�.rc���|jsY|j�}|r@|jr4|j|j�}|rt	j
||��||_|jS)zAReturn the machine-token if cached in the machine token response.)�	base_dict�overlay_dict)rr5r�parse_machine_token_overlayr
�depth_first_merge_overlay_dict)rr:�machine_token_overlays   r�
machine_tokenzMachineTokenFile.machine_tokenUsl���"�"��i�i�k�G��4�:�:�(,�(H�(H��3�3�)�%�)��7�7�")�%:��#*�D���"�"�"rc��|jr;|jjdi�jdi�jd�Sy)N�machineTokenInfo�contractInfo�name�rD�getr1s r�
contract_namezMachineTokenFile.contract_namegsA������"�"�&�&�'9�2�>���^�R�(���V��
�rc�v�tj|�}tj|tj
��Sr4)r	�	load_filer%r6r
r7)rr�machine_token_overlay_contents   rrAz,MachineTokenFile.parse_machine_token_overlayrs5��(.�(8�(8�&�)
�%��z�z�)��-�-�
�	
rc�T�t|j�r|jddSiS)NrF�accountInfo��boolrDr1s r�accountzMachineTokenFile.account{s,����"�"�#��%�%�&8�9�-�H�H��	r�seriesc��|jr|jS|jsiS|j|j|�|_|jS)z9Return configured entitlements keyed by entitlement named)rrD�get_entitlements_from_token)rrTs  r�entitlementszMachineTokenFile.entitlements�sQ������%�%�%��!�!��I�!�=�=�����
����!�!�!rrDc�^�ddlm}|siSi}|jdi�jd�}|siStd�|jdg�D��}td�|jdg�D��}|j	�D]$\}}d	|i}	||vr|||	d
<||	|��|	||<�&|S)z�Return a dictionary of entitlements keyed by entitlement name.

        Return an empty dict if no entitlements are present.
        r)�apply_contract_overridesrFrGc3�`K�|]&}|jd�|jd�f���(y�w)�type�tokenN�rJ��.0�es  r�	<genexpr>z?MachineTokenFile.get_entitlements_from_token.<locals>.<genexpr>�s-����
���U�U�6�]�A�E�E�'�N�+�
�s�,.�resourceTokensc3�BK�|]}|jd�|f���y�w)r[Nr]r^s  rraz?MachineTokenFile.get_entitlements_from_token.<locals>.<genexpr>�s%����
���U�U�6�]�A��
�s��resourceEntitlements�entitlement�
resourceToken)rT)�uaclient.contractrYrJ�dict�items)
rDrTrYrWrG�tokens_by_name�ent_by_name�entitlement_name�	ent_value�entitlement_cfgs
          rrVz,MachineTokenFile.get_entitlements_from_token�s���	?���I���$�(�(�);�R�@�D�D��
����I��
�"�&�&�'7��<�
�
���
�!�%�%�&<�b�A�
�
��,7�+<�+<�+>�	=�'��i�,�i�8�O��>�1�3A�$�4���0�
%�_�V�D�-<�L�)�*�	=��rc���|jsM|jrA|jjdi�jdi�jdd�|_|jS)z6Return a datetime of the attached contract expiration.rFrG�effectiveToN)r�is_attachedrDrJr1s r�contract_expiry_datetimez)MachineTokenFile.contract_expiry_datetime�sZ���-�-�$�2B�2B��"�"�&�&�'9�2�>���^�R�(���]�D�)�
�*��-�-�-rc�,�t|j�S)z<Report whether this machine configuration is attached to UA.rQr1s rrqzMachineTokenFile.is_attached�s���D�&�&�'�'rc��|j�y|jj�tj�j�z
}|jS)aReport num days until contract expiration based on effectiveTo

        :return: A positive int representing the number of days the attached
            contract remains in effect. Return a negative int for the number
            of days beyond contract's effectiveTo date.
        N)rr�dater�utcnow�days)r�deltas  r�contract_remaining_daysz(MachineTokenFile.contract_remaining_days�sF���(�(�0���-�-�2�2�4�x���7H�7M�7M�7O�O���z�z�rc�r�|jr+|jjdi�jd�Sy)N�activityInfo�
activityTokenrIr1s r�activity_tokenzMachineTokenFile.activity_token�s9������%�%�)�)�.�"�=�A�A���
�rc�r�|jr+|jjdi�jd�Sy)Nr{�
activityIDrIr1s r�activity_idzMachineTokenFile.activity_id�s2������%�%�)�)�.�"�=�A�A�,�O�O�rc�r�|jr+|jjdi�jd�Sy)Nr{�activityPingIntervalrIr1s r�activity_ping_intervalz'MachineTokenFile.activity_ping_interval�s9������%�%�)�)�.�"�=�A�A�&��
�rc��|jr;|jjdi�jdi�jd�Sy)NrFrG�idrIr1s r�contract_idzMachineTokenFile.contract_id�sA������"�"�&�&�'9�2�>���^�R�(���T��
�
rc�T�|jr|jjdg�Sy)NrbrIr1s r�resource_tokensz MachineTokenFile.resource_tokens�s(������%�%�)�)�*:�B�?�?�rc��|j�jdi�jdi�jdi�jdd�}|S)N�supportre�affordances�
onlySeries)rWrJ)r�only_seriess  rr�zMachineTokenFile.only_series�sM��
����
�S��B�
�
�S���
#�
�S���
#�
�S��t�
$�	��rr<)r2z
Optional[str])r2z
Optional[int])"�__name__�
__module__�__qualname__r�DEFAULT_DATA_DIR�strrrrhr(r0r5�propertyr=rDrKrArrrSrW�staticmethodrVrrrrq�intryr}r�r�r�r�r��rrrrs���"�2�2�48�
.��
.�%-�S�M�
.�0�T�0�0
0��h�t�n���/��/��#��#�"��x��}����
����c�3�h�����
	"�8�C�=�	"��?C�#��C��H�~�#�/7��}�#��#�J�	.�(�8�*<�	.��	.��(��(��
��#��
��
���������
���������������rrr2c��ddlm}ts9|s|�}t|j|j
j
d���atS)Nr)�UAConfigrC)rr)�uaclient.configr��_machine_token_filer�data_dir�featuresrJ)�cfgr�s  r�get_machine_token_filer�sC��(����*�C�.��l�l�'*�|�|�'7�'7�'�(�
���rr<)r%r�typingrrr�uaclientrrr	r
�uaclient.contract_data_typesr�uaclient.files.filesrr�rr�r�rr�<module>r�s=����&�&�7�7�?�'���t�t�n�(8�r