o
    (f"                     @   s~   d dl Z d dlmZ d dlmZmZmZ d dlmZmZm	Z	m
Z
 d dlmZ d dlmZ daG dd dZdd	efd
dZdS )    N)datetime)AnyDictOptional)defaults
exceptionssystemutil)PublicMachineTokenData)UAFilec                   @   sV  e Zd Zejdfdedee fddZdefddZ	d	d
 Z
dee fddZedd Zedd Zedee fddZdd Zedeeef fddZd0dee fddZe	d0deeef dee fddZedee fddZed d! Zedee fd"d#Zed1d%d&Zed1d'd(Zed2d*d+Zed,d- Zed.d/ Z dS )3MachineTokenFileN	directorymachine_token_overlay_pathc                 C   sF   t j}t||d t j | _t||d| _|| _d | _d | _d | _	d S )N/F)
r   MACHINE_TOKEN_FILEr   PRIVATE_SUBDIRprivate_filepublic_filer   _machine_token_entitlements_contract_expiry_datetime)selfr   r   	file_name r   >/usr/lib/python3/dist-packages/uaclient/files/machine_token.py__init__   s   
zMachineTokenFile.__init__private_contentc                 C   sp   t  r4tj|t jd}| j| t|j	dd}tj|t jd}| j
| d| _d| _d| _dS t )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_dictr   r   r   r   r   NonRootUserError)r   r   private_content_strpublic_contentpublic_content_strr   r   r   r$      s$   
zMachineTokenFile.writec                 C   s:   t  r| j  | j  d| _d| _d| _dS t	 )z!Delete both pub and private filesN)
r	   r    r   deleter   r   r   r   r   r'   r   r   r   r   r+   4   s   


zMachineTokenFile.deletereturnc                 C   sR   t  r| j}n| j}| }|sd S ztj|t jd}W |S  ty(   Y |S w Nr   )	r	   r    r   r   readr!   loadsDatetimeAwareJSONDecoder	Exception)r   file_handlercontentr   r   r   r/   @   s   zMachineTokenFile.readc                 C   s    t  r| jjo| jjS | jjS N)r	   r    r   
is_presentr   r,   r   r   r   r6   N   s   zMachineTokenFile.is_presentc                 C   sB   | j s|  }|r| jr| | j}|rtj||d || _ | j S )zAReturn the machine-token if cached in the machine token response.)	base_dictoverlay_dict)r   r/   r   parse_machine_token_overlayr	   depth_first_merge_overlay_dict)r   r4   machine_token_overlayr   r   r   machine_tokenU   s   
zMachineTokenFile.machine_tokenc                 C   &   | j r| j di di dS d S )NmachineTokenInfocontractInfonamer<   getr,   r   r   r   contract_nameg   s   zMachineTokenFile.contract_namec                 C   s   t |}tj|tjdS r.   )r   	load_filer!   r0   r	   r1   )r   r   machine_token_overlay_contentr   r   r   r9   r   s   z,MachineTokenFile.parse_machine_token_overlayc                 C   s   t | jr| jd d S i S )Nr>   accountInfoboolr<   r,   r   r   r   account{   s   
zMachineTokenFile.accountseriesc                 C   s,   | j r| j S | jsi S | | j|| _ | j S )z9Return configured entitlements keyed by entitlement named)r   r<   get_entitlements_from_token)r   rJ   r   r   r   entitlements   s   zMachineTokenFile.entitlementsr<   c           
      C   s   ddl m} | s
i S i }| di d}|si S tdd | dg D }tdd |d	g D }| D ]\}}d
|i}	||v rI|| |	d< ||	|d |	||< q7|S )zReturn a dictionary of entitlements keyed by entitlement name.

        Return an empty dict if no entitlements are present.
        r   )apply_contract_overridesr>   r?   c                 s   s$    | ]}| d | dfV  qdS )typetokenNrB   .0er   r   r   	<genexpr>   s
    
z?MachineTokenFile.get_entitlements_from_token.<locals>.<genexpr>resourceTokensc                 s   s    | ]
}| d |fV  qdS )rN   NrP   rQ   r   r   r   rT      s
    
resourceEntitlementsentitlementresourceToken)rJ   )uaclient.contractrM   rB   dictitems)
r<   rJ   rM   rL   r?   tokens_by_nameent_by_nameentitlement_name	ent_valueentitlement_cfgr   r   r   rK      s0   


z,MachineTokenFile.get_entitlements_from_tokenc                 C   s2   | j s| jr| jdi di dd| _ | j S )z6Return a datetime of the attached contract expiration.r>   r?   effectiveToN)r   is_attachedr<   rB   r,   r   r   r   contract_expiry_datetime   s   z)MachineTokenFile.contract_expiry_datetimec                 C   s
   t | jS )z<Report whether this machine configuration is attached to UA.rG   r,   r   r   r   rb      s   
zMachineTokenFile.is_attachedc                 C   s*   | j du rdS | j  t   }|jS )a  Report 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)rc   dater   utcnowdays)r   deltar   r   r   contract_remaining_days   s   
z(MachineTokenFile.contract_remaining_daysOptional[str]c                 C      | j r| j di dS d S )NactivityInfoactivityTokenrA   r,   r   r   r   activity_token   
   zMachineTokenFile.activity_tokenc                 C   rj   )Nrk   
activityIDrA   r,   r   r   r   activity_id   s   zMachineTokenFile.activity_idOptional[int]c                 C   rj   )Nrk   activityPingIntervalrA   r,   r   r   r   activity_ping_interval   rn   z'MachineTokenFile.activity_ping_intervalc                 C   r=   )Nr>   r?   idrA   r,   r   r   r   contract_id   s   zMachineTokenFile.contract_idc                 C   s   | j r
| j dg S d S )NrU   rA   r,   r   r   r   resource_tokens   s   z MachineTokenFile.resource_tokensr5   )r-   ri   )r-   rq   )!__name__
__module____qualname__r   DEFAULT_DATA_DIRstrr   r   rZ   r$   r+   r/   propertyr6   r<   rC   r9   r   r   rI   rL   staticmethodrK   r   rc   rb   intrh   rm   rp   rs   ru   rv   r   r   r   r   r      sX    



	
%

	r   r-   c                 C   s4   ddl m} ts| s| } t| j| jddatS )Nr   )UAConfigr;   )r   r   )uaclient.configr   _machine_token_filer   data_dirfeaturesrB   )cfgr   r   r   r   get_machine_token_file   s   r   r5   )r!   r   typingr   r   r   uaclientr   r   r   r	   uaclient.contract_data_typesr
   uaclient.files.filesr   r   r   r   r   r   r   r   <module>   s     m