Your IP : 216.73.217.13


Current Path : /usr/lib/python3/dist-packages/uaclient/files/__pycache__/
Upload File :
Current File : //usr/lib/python3/dist-packages/uaclient/files/__pycache__/files.cpython-38.pyc

U

8�-d5&�@s�ddlZddlZddlZddlmZddlmZmZmZddlm	Z	m
Z
mZmZm
Z
mZddlmZe
��Ze�e�ZGdd�d�ZGdd	�d	�ZdS)
�N)�datetime)�Any�Dict�Optional)�defaults�event_logger�
exceptions�messages�system�util)�PublicMachineTokenDatac@s~eZdZejdfeeed�dd�Zeed�dd��Z	eed�dd	��Z
ed
d��Zed�d
d�Ze
ed�dd�Zdd�ZdS)�UAFileT)�name�	directory�privatecCs*||_||_||_tj�|j|j�|_dS�N)�
_directoryZ
_file_name�_is_private�os�path�join�_path)�selfrrr�r�6/usr/lib/python3/dist-packages/uaclient/files/files.py�__init__szUAFile.__init__��returncCs|jSr)r�rrrrrszUAFile.pathcCs|jSr)rrrrr�
is_privateszUAFile.is_privatecCstj�|j�Sr)rr�existsrrrr�
is_present"szUAFile.is_present)�contentcCsb|jrtjntj}tj�|j�sNt�|j�tj�	|j�tj
krNt�|jd�t�
|j||�dS)Ni�)rrZROOT_READABLE_MODEZWORLD_READABLE_MODErrr r�makedirs�basename�PRIVATE_SUBDIR�chmodr
Z
write_file)rr"Z	file_moderrr�write&s��zUAFile.writecCs@d}zt�|j�}Wn&tk
r:t�d�|j��YnX|S)NzFile does not exist: {})r
�	load_filer�FileNotFoundError�LOG�debug�format)rr"rrr�read2szUAFile.readcCst�|j�dSr)r
Zensure_file_absentrrrrr�delete:sz
UAFile.deleteN)�__name__�
__module__�__qualname__r�DEFAULT_DATA_DIR�str�boolr�propertyrrr!r'rr-r.rrrrr
s ��
r
c@seZdZejdfeeed�dd�Zed�dd�Z	dd	�Z
eed
�dd�Zed
d��Z
edd��Zdd�Zeeed
�dd��Zedd��Zeed�dd��Zeeed
�dd��Zedd��Zeeed
�dd��Zed d
�d!d"��Zed d
�d#d$��Zed%d
�d&d'��Zed(d)��ZdS)*�MachineTokenFileN)r�machine_token_overlay_pathcCsBtj}t||tj�|_t||d�|_||_d|_d|_d|_	dS)NF)
rZMACHINE_TOKEN_FILEr
r%�private_file�public_filer7�_machine_token�
_entitlements�_contract_expiry_datetime)rrr7�	file_namerrrr?s�zMachineTokenFile.__init__)�private_contentcCsrt��rftj|tjd�}|j�|�t�|�j	dd�}tj|tjd�}|j
�|�d|_d|_d|_
nt���dS)z8Update the machine_token file for both pub/private files��clsF)Z	keep_noneN)r�we_are_currently_root�json�dumpsZDatetimeAwareJSONEncoderr8r'rZ	from_dictZto_dictr9r:r;r<r�NonRootUserError)rr>Zprivate_content_strZpublic_contentZpublic_content_strrrrr'Ns(����zMachineTokenFile.writecCs<t��r0|j��|j��d|_d|_d|_nt�	��dS)z!Delete both pub and private filesN)
rrAr9r.r8r:r;r<rrDrrrrr.fs

zMachineTokenFile.deletercCsTt��r|j}n|j}|��}|s&dSztj|tjd�}Wntk
rNYnX|S)Nr?)	rrAr8r9r-rB�loads�DatetimeAwareJSONDecoder�	Exception)rZfile_handlerr"rrrr-rszMachineTokenFile.readcCs$t��r|jjo|jjS|jjSdSr)rrAr9r!r8rrrrr!�szMachineTokenFile.is_presentcCsB|js<|��}|r6|jr6|�|j�}|r6tj||d�||_|jS)zAReturn the machine-token if cached in the machine token response.)Z	base_dictZoverlay_dict)r:r-r7�parse_machine_token_overlayrZdepth_first_merge_overlay_dict)rr"Zmachine_token_overlayrrr�
machine_token�s
��zMachineTokenFile.machine_tokenc
Cs~tj�|�s t�tjj|d���zt�	|�}t
j|tj
d�WStk
rx}zt�tjjt|�|d���W5d}~XYnXdS)N)�	file_pathr?)�errorrJ)rrr rZUserFacingErrorr	Z&INVALID_PATH_FOR_MACHINE_TOKEN_OVERLAYr,r
r(rBrErrF�
ValueErrorZERROR_JSON_DECODING_IN_FILEr3)rr7Zmachine_token_overlay_content�errrrH�s*������z,MachineTokenFile.parse_machine_token_overlaycCst|j�r|jddSdS)N�machineTokenInfoZaccountInfo�r4rIrrrr�account�s
zMachineTokenFile.accountcCs*|jr|jS|jsiS|�|j�|_|jS)z9Return configured entitlements keyed by entitlement named)r;rI�get_entitlements_from_tokenrrrr�entitlements�s�zMachineTokenFile.entitlements)rIc	Cs�ddlm}|siSi}|�di��d�}|s2iStdd�|�dg�D��}tdd�|�d	g�D��}|��D]4\}}d
|i}||kr�|||d<||�|||<qn|S)z�Return a dictionary of entitlements keyed by entitlement name.

        Return an empty dict if no entitlements are present.
        r)�apply_contract_overridesrN�contractInfocss"|]}|�d�|�d�fVqdS)�type�tokenN��get��.0rMrrr�	<genexpr>�s�z?MachineTokenFile.get_entitlements_from_token.<locals>.<genexpr>ZresourceTokenscss|]}|�d�|fVqdS)rUNrWrYrrrr[�s�ZresourceEntitlementsZentitlementZ
resourceToken)Zuaclient.contractrSrX�dict�items)	rIrSrRrTZtokens_by_nameZent_by_nameZentitlement_nameZ	ent_valueZentitlement_cfgrrrrQ�s0�
�
��
z,MachineTokenFile.get_entitlements_from_tokencCs,|js&|j�di��di��dd�|_|jS)z6Return a datetime of the attached contract expiration.rNrTZeffectiveToN)r<rIrXrrrr�contract_expiry_datetime�s���z)MachineTokenFile.contract_expiry_datetimecCs
t|j�S)z<Report whether this machine configuration is attached to UA.rOrrrr�is_attached�szMachineTokenFile.is_attachedcCs*|jdkrdS|j��t����}|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^�daterZutcnowZdays)rZdeltarrr�contract_remaining_days�s
z(MachineTokenFile.contract_remaining_daysz
Optional[str]cCs|jr|j�di��d�SdS)N�activityInfoZ
activityToken�rIrXrrrr�activity_tokens
�zMachineTokenFile.activity_tokencCs|jr|j�di��d�SdS)NrbZ
activityIDrcrrrr�activity_id
szMachineTokenFile.activity_idz
Optional[int]cCs|jr|j�di��d�SdS)NrbZactivityPingIntervalrcrrrr�activity_ping_intervals
�z'MachineTokenFile.activity_ping_intervalcCs&|jr"|j�di��di��d�SdS)NrNrT�idrcrrrr�contract_ids���zMachineTokenFile.contract_id)r/r0r1rr2r3rrr\r'r.r-r5r!rIrHrPrR�staticmethodrrQrr^r_�intrardrerfrhrrrrr6>sD��


#
r6)rBZloggingrr�typingrrrZuaclientrrrr	r
rZuaclient.contract_data_typesrZget_event_loggerZeventZ	getLoggerr/r*r
r6rrrr�<module>s 
0