%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /lib/python3/dist-packages/uaclient/files/__pycache__/
Upload File :
Create Path :
Current File : //lib/python3/dist-packages/uaclient/files/__pycache__/files.cpython-312.pyc

�

�Hcf�(�� �ddlZddlZddlZddlmZddlmZmZmZddlm	Z	m
Z
mZmZm
Z
mZddlmZe
j"�Zej&ej(e��ZGd�d�ZGd�d	�ZGd
�de�ZGd�d
�Zy)�N)�datetime)�Any�Dict�Optional)�defaults�event_logger�
exceptions�secret_manager�system�util)�PublicMachineTokenDatac��eZdZejdfdededefd�Zedefd��Z	edefd��Z
ed	��Zd
efd�Zde
efd�Zd
�Zy)�UAFileT�name�	directory�privatec��||_||_||_tjj|j|j�|_y�N)�
_directory�
_file_name�_is_private�os�path�join�_path)�selfrrrs    �6/usr/lib/python3/dist-packages/uaclient/files/files.py�__init__zUAFile.__init__s8��$������"����W�W�\�\�$�/�/�4�?�?�C��
��returnc��|jSr)r�rs rrzUAFile.path!s���z�z�rc��|jSr)rr"s r�
is_privatezUAFile.is_private%s�����rc�T�tjj|j�Sr)rr�existsr"s r�
is_presentzUAFile.is_present)s���w�w�~�~�d�i�i�(�(r�contentc��|jrtjntj}	tj
j
|j�tjk(r"t	j|jd��nt	j|j�tj|j
||�y#t$rY�-wxYw)Ni�)�mode)
r$r�ROOT_READABLE_MODE�WORLD_READABLE_MODErr�basenamer�PRIVATE_SUBDIR�makedirs�OSErrorr�
write_file)rr(�	file_modes   r�writezUAFile.write-s������
�'�'��-�-�	�	��w�w������0�H�4K�4K�K����D�O�O�%�8����D�O�O�,�	���$�)�)�W�i�8���	��	�s�A;C�	C�Cc��d}	tj|j�}|S#t$r$tjd|j�Y|SwxYw)Nz(Tried to load %s but file does not exist)r�	load_filer�FileNotFoundError�LOG�debug�rr(s  r�readzUAFile.read>sS����	M��&�&�t�y�y�1�G����!�	M��I�I�@�$�)�)�L���	M�s�%�)A�Ac�B�tj|j�yr)r�ensure_file_absentrr"s r�deletez
UAFile.deleteFs���!�!�$�)�)�,rN)�__name__�
__module__�__qualname__r�DEFAULT_DATA_DIR�str�boolr�propertyrr$r'r3rr:r=�rrrrs���"�2�2��		D��	D��	D��		D���c����� �D� �� ��)��)�9�S�9�"�h�s�m��-rrc�`�eZdZdefd�Zdeeeffd�Zde	eeeffd�Z
d�Zed��Z
y	)
�ProJSONFile�pro_filec��||_yr�rH)rrHs  rrzProJSONFile.__init__Ks��!��
rr(c��|jjtj|tj
����y)N��cls)r()rHr3�json�dumpsr�DatetimeAwareJSONEncoderr9s  rr3zProJSONFile.writeQs-���
�
����J�J�w�D�,I�,I�J�	�	
rr c�,�|jj�}|r&	tj|tj
��Sy#tj$r<}tj|jjdt|�z���d}~wwxYw)NrL�
)�source�out)rHr:rN�loadsr�DatetimeAwareJSONDecoder�JSONDecodeErrorr	�InvalidJsonrrB)rr(�es   rr:zProJSONFile.readVsz���-�-�$�$�&���
��z�z�'�t�/L�/L�M�M����'�'�
� �,�,��=�=�-�-�4�#�a�&�=����
�s�$A�B�7B�Bc�6�|jj�Sr)rHr=r"s rr=zProJSONFile.deletecs���}�}�#�#�%�%rc�.�|jjSr)rHr'r"s rr'zProJSONFile.is_presentfs���}�}�'�'�'rN)r>r?r@rrrrBrr3rr:r=rDr'rErrrGrGJsU��!��!�
�T�#�s�(�^�
�
�h�t�C��H�~�.��&��(��(rrGc�$��eZdZdef�fd�Z�xZS)�
UserCacheFilerc�N��t�|�|tj�d��y)NF)rr)�superrr�get_user_cache_dir)rr�	__class__s  �rrzUserCacheFile.__init__ls%���
����F�5�5�7��	�	
r)r>r?r@rBr�
__classcell__)ras@rr]r]ks���
�S�
�
rr]c�p�eZdZejdfdedeefd�Zdeee	ffd�Z
d�Zdeefd	�Z
ed
��Zed��Zd�Zedeefd
��Zed��Zedefd��Zedeefd��Zed��Zedeefd��Zedd��Zedd��Zedd��Zed��Zed��Zy)�MachineTokenFileNr�machine_token_overlay_pathc	�"�tj}tt|tj
j
|tj����|_tt||d���|_	||_
d|_d|_d|_
y)NrJF)r�MACHINE_TOKEN_FILErGrrrrr.�private_file�public_filere�_machine_token�
_entitlements�_contract_expiry_datetime)rrre�	file_names    rrzMachineTokenFile.__init__ss|��
�/�/�	�'���2�7�7�<�<�	�8�3J�3J�K��
���
'��I�y�%�8�
���+E��'�"���!���)-��&r�private_contentc�4�tj�rq|jj|�t	j
|�j
d��}|jj|�d|_d|_	d|_
ytj��)z8Update the machine_token file for both pub/private filesF)�	keep_noneN)
r�we_are_currently_rootrhr3r
�	from_dict�to_dictrirjrkrlr	�NonRootUserError)rrn�public_contents   rr3zMachineTokenFile.write�s����%�%�'����#�#�O�4�4�=�=����g��g�&�
�
���"�"�>�2�"&�D��!%�D��-1�D�*��-�-�/�/rc���tj�rJ|jj�|jj�d|_d|_d|_ytj��)z!Delete both pub and private filesN)
rrqrir=rhrjrkrlr	rtr"s rr=zMachineTokenFile.delete�sY���%�%�'����#�#�%����$�$�&�"&�D��!%�D��-1�D�*��-�-�/�/rr c�n�tj�r
|j}n|j}|j	�}|rut
jj|jdd��|jdg�D]1}t
jj|jdd���3|S)N�machineToken��resourceTokens�token)	rrqrhrir:r
�secrets�
add_secret�get)r�file_handlerr(r{s    rr:zMachineTokenFile.read�s����%�%�'��,�,�L��+�+�L��#�#�%����"�"�-�-�g�k�k�.�"�.M�N� ���%5�r�:�
J���&�&�1�1�%�)�)�G�R�2H�I�
J��rc��tj�r.|jjxr|jjS|jjSr)rrqrir'rhr"s rr'zMachineTokenFile.is_present�sC���%�%�'��#�#�.�.�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)rjr:re�parse_machine_token_overlayr�depth_first_merge_overlay_dict)rr(�machine_token_overlays   r�
machine_tokenzMachineTokenFile.machine_token�sl���"�"��i�i�k�G��4�:�:�(,�(H�(H��3�3�)�%�)��7�7�")�%:��#*�D���"�"�"rc�v�tj|�}tj|tj
��S)NrL)rr5rNrUrrV)rre�machine_token_overlay_contents   rr�z,MachineTokenFile.parse_machine_token_overlay�s5��(.�(8�(8�&�)
�%��z�z�)��-�-�
�	
rc�R�t|j�r|jddSy)N�machineTokenInfo�accountInfo�rCr�r"s r�accountzMachineTokenFile.account�s*����"�"�#��%�%�&8�9�-�H�H�rc��|jr|jS|jsiS|j|j�|_|jS)z9Return configured entitlements keyed by entitlement named)rkr��get_entitlements_from_tokenr"s r�entitlementszMachineTokenFile.entitlements�sQ������%�%�%��!�!��I�!�=�=����
����!�!�!rr�c�Z�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_overridesr��contractInfoc3�`K�|]&}|jd�|jd�f���(y�w)�typer{N�r~��.0rYs  r�	<genexpr>z?MachineTokenFile.get_entitlements_from_token.<locals>.<genexpr>�s-����
���U�U�6�]�A�E�E�'�N�+�
�s�,.rzc3�BK�|]}|jd�|f���y�w)r�Nr�r�s  rr�z?MachineTokenFile.get_entitlements_from_token.<locals>.<genexpr>�s%����
���U�U�6�]�A��
�s��resourceEntitlements�entitlement�
resourceToken)�uaclient.contractr�r~�dict�items)	r�r�r�r��tokens_by_name�ent_by_name�entitlement_name�	ent_value�entitlement_cfgs	         rr�z,MachineTokenFile.get_entitlements_from_token�s���	?���I���$�(�(�);�R�@�D�D��
����I��
�"�&�&�'7��<�
�
���
�!�%�%�&<�b�A�
�
��,7�+<�+<�+>�	=�'��i�,�i�8�O��>�1�3A�$�4���0�
%�_�5�-<�L�)�*�	=��rc��|jsA|jjdi�jdi�jdd�|_|jS)z6Return a datetime of the attached contract expiration.r�r��effectiveToN)rlr�r~r"s r�contract_expiry_datetimez)MachineTokenFile.contract_expiry_datetimesR���-�-��"�"�&�&�'9�2�>���^�R�(���]�D�)�
�*��-�-�-rc�,�t|j�S)z<Report whether this machine configuration is attached to UA.r�r"s r�is_attachedzMachineTokenFile.is_attacheds���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)r��dater�utcnow�days)r�deltas  r�contract_remaining_daysz(MachineTokenFile.contract_remaining_dayssF���(�(�0���-�-�2�2�4�x���7H�7M�7M�7O�O���z�z�rc�r�|jr+|jjdi�jd�Sy)N�activityInfo�
activityToken�r�r~r"s r�activity_tokenzMachineTokenFile.activity_token&s9������%�%�)�)�.�"�=�A�A���
�rc�r�|jr+|jjdi�jd�Sy)Nr��
activityIDr�r"s r�activity_idzMachineTokenFile.activity_id.s2������%�%�)�)�.�"�=�A�A�,�O�O�rc�r�|jr+|jjdi�jd�Sy)Nr��activityPingIntervalr�r"s r�activity_ping_intervalz'MachineTokenFile.activity_ping_interval4s9������%�%�)�)�.�"�=�A�A�&��
�rc��|jr;|jjdi�jdi�jd�Sy)Nr�r��idr�r"s r�contract_idzMachineTokenFile.contract_id<sA������"�"�&�&�'9�2�>���^�R�(���T��
�
rc�T�|jr|jjdg�Sy)Nrzr�r"s r�resource_tokensz MachineTokenFile.resource_tokensFs(������%�%�)�)�*:�B�?�?�r)r z
Optional[str])r z
Optional[int]) r>r?r@rrArBrrrrr3r=r�r:rDr'r�r�r�r��staticmethodr�rr�r��intr�r�r�r�r�r�rErrrdrdrs���"�2�2�48�.��.�%-�S�M�.�&0�T�#�s�(�^�0�$
0�
�h�t�n�
��/��/��#��#�"
����$�����
�	"��	"��!�4�!��!�F�	.�(�8�*<�	.��	.��(��(��
��#��
��
���������
�����������rrd)rN�loggingrr�typingrrr�uaclientrrr	r
rr�uaclient.contract_data_typesr
�get_event_logger�event�	getLogger�replace_top_level_logger_namer>r7rrGr]rdrErr�<module>r�s�����	��&�&���@�%��%�%�'���g���:��:�:�8�D�E��2-�2-�j(�(�B
�F�
�Y�Yr

Zerion Mini Shell 1.0