
    g#                         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      Zdd	efd
Zy)    N)datetime)AnyDictOptional)defaults
exceptionssystemutil)PublicMachineTokenData)UAFilec                      e Zd Zej                  dfdedee   fdZdefdZ	d Z
dee   fd	Zed
        Zed        Zedee   fd       Zd Zedeeef   fd       Zddee   fdZe	 ddeeef   dee   fd       Zedee   fd       Zed        Zedee   fd       Zedd       Zedd       Zedd       Zed        Zed        Z ed        Z!y)MachineTokenFileN	directorymachine_token_overlay_pathc                     t         j                  }t        ||dz   t         j                  z         | _        t        ||d      | _        || _        d | _        d | _        d | _	        y )N/F)
r   MACHINE_TOKEN_FILEr   PRIVATE_SUBDIRprivate_filepublic_filer   _machine_token_entitlements_contract_expiry_datetime)selfr   r   	file_names       >/usr/lib/python3/dist-packages/uaclient/files/machine_token.py__init__zMachineTokenFile.__init__   sc    
 //	"y3)@)@@
 ")Y>*D'"!)-&    private_contentc                    t        j                         rt        j                  |t         j                        }| j
                  j                  |       t        j                  |      j                  d      }t        j                  |t         j                        }| j                  j                  |       d| _        d| _        d| _        yt        j                         )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_strs        r   r(   zMachineTokenFile.write   s    %%'"&**T%B%B# ##$78 4==gg&  "&D$A$A" ""#56"&D!%D-1D*--//r   c                     t        j                         rJ| j                  j                          | j                  j                          d| _        d| _        d| _        yt        j                         )z!Delete both pub and private filesN)
r
   r$   r   deleter   r   r   r   r   r+   r   s    r   r0   zMachineTokenFile.delete4   sY    %%'##%$$&"&D!%D-1D*--//r   returnc                     t        j                         r| j                  }n| j                  }|j	                         }|sy 	 t        j                  |t         j                        }|S # t        $ r Y |S w xY wNr!   )	r
   r$   r   r   readr%   loadsDatetimeAwareJSONDecoder	Exception)r   file_handlercontents      r   r5   zMachineTokenFile.read@   ss    %%',,L++L##%	jjd.K.KLG   		s   %A) )	A65A6c                     t        j                         r.| j                  j                  xr | j                  j                  S | j                  j                  S N)r
   r$   r   
is_presentr   r1   s    r   r=   zMachineTokenFile.is_presentN   sC    %%'##..O43D3D3O3OO##...r   c                     | j                   sY| j                         }|r@| j                  r4| j                  | j                        }|rt	        j
                  ||       || _         | j                   S )zAReturn the machine-token if cached in the machine token response.)	base_dictoverlay_dict)r   r5   r   parse_machine_token_overlayr
   depth_first_merge_overlay_dict)r   r:   machine_token_overlays      r   machine_tokenzMachineTokenFile.machine_tokenU   sl     ""iikG4::(,(H(H33)% )77")%: #*D"""r   c                     | j                   r;| j                   j                  di       j                  di       j                  d      S y )NmachineTokenInfocontractInfonamerD   getr1   s    r   contract_namezMachineTokenFile.contract_nameg   sA    ""&&'92>^R(V r   c                 v    t        j                  |      }t        j                  |t        j
                        S r4   )r	   	load_filer%   r6   r
   r7   )r   r   machine_token_overlay_contents      r   rA   z,MachineTokenFile.parse_machine_token_overlayr   s5    (.(8(8&)
% zz)--
 	
r   c                 T    t        | j                        r| j                  d   d   S i S )NrF   accountInfoboolrD   r1   s    r   accountzMachineTokenFile.account{   s,    ""#%%&89-HH	r   seriesc                     | j                   r| j                   S | j                  si S | j                  | j                  |      | _         | j                   S )z9Return configured entitlements keyed by entitlement named)r   rD   get_entitlements_from_token)r   rT   s     r   entitlementszMachineTokenFile.entitlements   sQ    %%%!!I!==
 !!!r   rD   c                 ^   ddl m} | si S i }| j                  di       j                  d      }|si S t        d | j                  dg       D              }t        d |j                  dg       D              }|j	                         D ]$  \  }}d	|i}	||v r||   |	d
<    ||	|       |	||<   & |S )zReturn a dictionary of entitlements keyed by entitlement name.

        Return an empty dict if no entitlements are present.
        r   )apply_contract_overridesrF   rG   c              3   `   K   | ]&  }|j                  d       |j                  d      f ( yw)typetokenNrJ   .0es     r   	<genexpr>z?MachineTokenFile.get_entitlements_from_token.<locals>.<genexpr>   s-      
 UU6]AEE'N+
s   ,.resourceTokensc              3   B   K   | ]  }|j                  d       |f  yw)r[   Nr]   r^   s     r   ra   z?MachineTokenFile.get_entitlements_from_token.<locals>.<genexpr>   s%      
 UU6]A
s   resourceEntitlementsentitlementresourceToken)rT   )uaclient.contractrY   rJ   dictitems)
rD   rT   rY   rW   rG   tokens_by_nameent_by_nameentitlement_name	ent_valueentitlement_cfgs
             r   rV   z,MachineTokenFile.get_entitlements_from_token   s     	?I$(();R@DD
 I 
"&&'7<
 
  
!%%&<bA
 
 ,7+<+<+> 	='i,i8O>13A$40 %_VD-<L)*	= r   c                     | j                   sM| j                  rA| j                  j                  di       j                  di       j                  dd      | _         | j                   S )z6Return a datetime of the attached contract expiration.rF   rG   effectiveToN)r   is_attachedrD   rJ   r1   s    r   contract_expiry_datetimez)MachineTokenFile.contract_expiry_datetime   sZ     --$2B2B""&&'92>^R(]D) * ---r   c                 ,    t        | j                        S )z<Report whether this machine configuration is attached to UA.rQ   r1   s    r   rq   zMachineTokenFile.is_attached   s     D&&''r   c                     | j                   y| j                   j                         t        j                         j                         z
  }|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)rr   dater   utcnowdays)r   deltas     r   contract_remaining_daysz(MachineTokenFile.contract_remaining_days   sF     ((0--224x7H7M7M7OOzzr   c                 r    | j                   r+| j                   j                  di       j                  d      S y )NactivityInfoactivityTokenrI   r1   s    r   activity_tokenzMachineTokenFile.activity_token   s9    %%))."=AA  r   c                 r    | j                   r+| j                   j                  di       j                  d      S y )Nr{   
activityIDrI   r1   s    r   activity_idzMachineTokenFile.activity_id   s2    %%))."=AA,OOr   c                 r    | j                   r+| j                   j                  di       j                  d      S y )Nr{   activityPingIntervalrI   r1   s    r   activity_ping_intervalz'MachineTokenFile.activity_ping_interval   s9    %%))."=AA&  r   c                     | j                   r;| j                   j                  di       j                  di       j                  d      S y )NrF   rG   idrI   r1   s    r   contract_idzMachineTokenFile.contract_id   sA    ""&&'92>^R(T
 r   c                 T    | j                   r| j                   j                  dg       S y )Nrb   rI   r1   s    r   resource_tokensz MachineTokenFile.resource_tokens   s(    %%))*:B??r   c                     | j                         j                  di       j                  di       j                  di       j                  dd       }|S )Nsupportre   affordances
onlySeries)rW   rJ   )r   only_seriess     r   r   zMachineTokenFile.only_series   sM     SBS#S#St$ 	 r   r<   )r2   zOptional[str])r2   zOptional[int])"__name__
__module____qualname__r   DEFAULT_DATA_DIRstrr   r   rh   r(   r0   r5   propertyr=   rD   rK   rA   r   r   rS   rW   staticmethodrV   r   rr   rq   intry   r}   r   r   r   r   r    r   r   r   r      s    "2248.. %-SM.0T 00
0htn  / / # #" x}  
 c3h  
	"8C= 	" ?C#CH~#/7}# #J 	.(8*< 	. 	. ( ( 
# 
 
    
        r   r   r2   c                     ddl m} t        s9| s |       } t        | j                  | j
                  j                  d            at        S )Nr   )UAConfigrC   )r   r   )uaclient.configr   _machine_token_filer   data_dirfeaturesrJ   )cfgr   s     r   get_machine_token_filer     sC    ( *C.ll'*||'7'7'(
 r   r<   )r%   r   typingr   r   r   uaclientr   r   r	   r
   uaclient.contract_data_typesr   uaclient.files.filesr   r   r   r   r   r   r   <module>r      s=      & & 7 7 ? ' t tn(8 r   