U
    (fw                  
   @   s  d dl Z d dlZd dlZd dlmZ d dlmZmZmZm	Z	m
Z
 d dlm  mZ d dlmZmZmZmZmZmZmZmZmZ d dlmZ d dlmZ d dlmZ d dlm Z  d d	l!m"Z"m#Z# d d
l$m%Z% d dl&m'Z' dZ(dZ)dZ*dZ+dZ,dZ-dZ.dZ/dZ0dZ1dZ2dddddZ3e4 Z5e6e7e8Z9edddgZ:G dd de%j;Z<e=ddd Z>dAeee?ef ee?ef e@e@dd"d#d$ZAdBeee?ef ee?ef e@e@e
ee@f d&d'd(ZBejCejDd)d*d+ZEed,d-d.ZFeee d/d0d1ZGee?ee?ef d2d3d4ZHee?e?f ee?e?f eId5d6d7ZJdCee?ef e?e?e	e? eeIee?ef f d8d9d:ZKdDee?ef e	e? e	e? dd;d<d=ZLeee?ef ee: d>d?d@ZMdS )E    N)
namedtuple)AnyDictListOptionalTuple)	cloudsevent_logger
exceptionshttpmessagessecret_managersystemutilversion)_enabled_services)_is_attached)UAConfig)ATTACH_FAIL_DATE_FORMAT)attachment_data_filemachine_id_file)serviceclient)get_user_or_root_log_file_pathz/v1/context/machines/tokenz3/v1/contracts/{contract}/context/machines/{machine}z/v1/resourcesz3/v1/resources/{resource}/context/machines/{machine}z/v1/clouds/{cloud_type}/tokenz3/v1/contracts/{contract}/machine-activity/{machine}z/v1/contractz/v1/magic-attach            )series_overridesseriescloudvariantEnableByDefaultServicenamer    c                       sL  e Zd ZdZd(ee dd fddZeje	j
dddgdd)d	d
Zeeef dddZeeeef dddZeje	j
dddgdejdddZd*eeee eeef dddZdd Zeeeef dddZeeef dddZeddd Zd+eeee eeef d!d"d#Zd,eeee ed!d$d%Zd&d' Z  ZS )-UAContractClientZcontract_urlNcfgreturnc                    s   t  j|d t | _d S )Nr%   )super__init__mtfget_machine_token_filemachine_token_file)selfr%   	__class__ 3/usr/lib/python3/dist-packages/uaclient/contract.pyr)   E   s    zUAContractClient.__init__r   r   )Zretry_sleepsc                 C   s   |st | j}|  }|dd|i |  }| |d< ||d}t|}| j	t
||d}|jdkrvt n|jdkrt| |jdkrtjt
|j|jd	|j}	tj|	d
d |	dg D ]}
tj|
dd q|	S )a}  Requests machine attach to the provided machine_id.

        @param contract_token: Token string providing authentication to
            ContractBearer service endpoint.
        @param machine_id: Optional unique system machine id. When absent,
            contents of /etc/machine-id will be used.

        @return: Dict of the JSON response containing the machine-token.
        Authorization	Bearer {}lastAttachment	machineIdactivityInfo)dataheaders  i     urlcodebodymachineToken resourceTokenstoken)r   get_machine_idr%   r9   updateformat_get_activity_info	isoformat_support_old_machine_inforequest_urlAPI_V1_ADD_CONTRACT_MACHINEr>   r
   ZAttachInvalidTokenError_raise_attach_forbidden_messageContractAPIErrorr?   	json_dictr   secrets
add_secretget)r-   contract_tokenZattachment_dt
machine_idr9   activity_infor8   backcompat_dataresponseresponse_jsonrC   r0   r0   r1   add_contract_machineL   s<    
  




z%UAContractClient.add_contract_machine)r&   c                 C   sT   |   }| jt|d |d |d |d dd}|jdkrNtjt|j|jd|jS )	z=Requests list of entitlements available to this machine type.architecturer   kernelvirtrY   r   rZ   r[   )query_paramsr;   r<   )rG   rJ   API_V1_AVAILABLE_RESOURCESr>   r
   rM   r?   rN   )r-   rT   rV   r0   r0   r1   available_resourcesw   s     	
z$UAContractClient.available_resources)rR   r&   c                 C   sN   |   }|dd|i | jt|d}|jdkrHtjt|j|jd|j	S )Nr2   r3   r9   r;   r<   )
r9   rE   rF   rJ   API_V1_GET_CONTRACT_USING_TOKENr>   r
   rM   r?   rN   )r-   rR   r9   rV   r0   r0   r1   get_contract_using_token   s     
z)UAContractClient.get_contract_using_token)instancec                C   s~   | j tj|jd|jd}|jdkr`|jdd}|rLt	| t
j|dt
jt|j|jd|j}tj|dd |S )	zRequests contract token for auto-attach images for Pro clouds.

        @param instance: AutoAttachCloudInstance for the cloud.

        @return: Dict of the JSON response containing the contract-token.
        )
cloud_type)r8   r;   messagerA   )	error_msgr<   contractToken)rJ   ,API_V1_GET_CONTRACT_TOKEN_FOR_CLOUD_INSTANCErF   rd   Zidentity_docr>   rN   rQ   LOGdebugr
   ZInvalidProImagerM   r?   r   rO   rP   )r-   rc   rV   msgrW   r0   r0   r1   %get_contract_token_for_cloud_instance   s*    



z6UAContractClient.get_contract_token_for_cloud_instance)machine_tokenresourcerS   r&   c           	      C   s   |st | j}|  }|dd|i tj||d}| j||d}|jdkrft	j
t|j|jd|jdr|jd |jd< |j}|dg D ]}tj|d	d
 q|S )a  Requests machine access context for a given resource

        @param machine_token: The authentication token needed to talk to
            this contract service endpoint.
        @param resource: Entitlement name.
        @param machine_id: Optional unique system machine id. When absent,
            contents of /etc/machine-id will be used.

        @return: Dict of the JSON response containing entitlement accessInfo.
        r2   r3   )rn   machiner`   r;   r<   expiresrB   rC   rA   )r   rD   r%   r9   rE   rF   "API_V1_GET_RESOURCE_MACHINE_ACCESSrJ   r>   r
   rM   r?   rQ   rN   r   rO   rP   )	r-   rm   rn   rS   r9   r=   rV   rW   rC   r0   r0   r1   get_resource_machine_access   s*     
z,UAContractClient.get_resource_machine_accessc                 C   s   | j j}| j jd}t| j}|  }tj	||d}| 
 }|dd	|i | j|||d}|jdkrtj||j|jd|jr| j j}|j|d< | j | d	S )
zReport current activity token and enabled services.

        This will report to the contracts backend all the current
        enabled services in the system.
        r@   Zcontractro   r2   r3   )r9   r8   r;   r<   r7   N)r,   contract_idrm   rQ   r   rD   r%   rG   API_V1_UPDATE_ACTIVITY_TOKENrF   r9   rE   rJ   r>   r
   rM   r?   rN   write)r-   rt   rm   rS   Zrequest_datar=   r9   rV   r0   r0   r1   update_activity_token   s.     
  
z&UAContractClient.update_activity_token)magic_tokenr&   c                 C   s   |   }|dd|i | jt|d}|jdkr<t |jdkrNt |jdkrltj	t|j|j
d|j}dd	d
g}|D ]}tj||d q|S )zRequest magic attach token info.

        When the magic token is registered, it will contain new fields
        that will allow us to know that the attach process can proceed
        r2   r3   r`   r:     r;   r<   rC   userCoderg   rA   )r9   rE   rF   rJ   "API_V1_GET_MAGIC_ATTACH_TOKEN_INFOr>   r
   MagicAttachTokenErrorMagicAttachUnavailablerM   r?   rN   r   rO   rP   rQ   )r-   rx   r9   rV   rW   secret_fieldsfieldr0   r0   r1   get_magic_attach_token_info  s*     



z,UAContractClient.get_magic_attach_token_infoc                 C   sz   |   }| jt|dd}|jdkr*t |jdkrHtjt|j|jd|j}dddg}|D ]}t	j
||d	 q\|S )
z)Create a magic attach token for the user.POSTr9   methodry   r;   r<   rC   rz   rg   rA   )r9   rJ   API_V1_NEW_MAGIC_ATTACHr>   r
   r}   rM   r?   rN   r   rO   rP   rQ   )r-   r9   rV   rW   r~   r   r0   r0   r1   new_magic_attach_token$  s&    


z'UAContractClient.new_magic_attach_token)rx   c                 C   s   |   }|dd|i | jt|dd}|jdkr>t |jdkrPt |jdkrbt	 |jdkrtj
t|j|jd	d
S )z)Revoke a magic attach token for the user.r2   r3   ZDELETEr   i  r:   ry   r;   r<   N)r9   rE   rF   rJ   API_V1_REVOKE_MAGIC_ATTACHr>   r
   Z MagicAttachTokenAlreadyActivatedr|   r}   rM   r?   )r-   rx   r9   rV   r0   r0   r1   revoke_magic_attach_token;  s&    



z*UAContractClient.revoke_magic_attach_token)rm   rt   rS   r&   c              	   C   s   |st | j}|  }|dd|i tj||d}|  }| j|d||d |d |d |d d	d
}|j	dkrt
j||j	|jd|jdr|jd |jd< |jS )a|  Get the updated machine token from the contract server.

        @param machine_token: The machine token needed to talk to
            this contract service endpoint.
        @param contract_id: Unique contract id provided by contract service
        @param machine_id: Optional unique system machine id. When absent,
            contents of /etc/machine-id will be used.
        r2   r3   rs   ZGETrY   r   rZ   r[   r\   )r   r9   r]   r;   r<   rp   )r   rD   r%   r9   rE   rF   API_V1_GET_CONTRACT_MACHINErG   rJ   r>   r
   rM   r?   rQ   rN   )r-   rm   rt   rS   r9   r=   rT   rV   r0   r0   r1   get_contract_machineR  s8    
  z%UAContractClient.get_contract_machinec           	      C   s   |st | j}|  }|dd|i ||  d}t|}tj||d}| j	||d|d}|j
dkrtj||j
|jd|jd	r|jd	 |jd	< |jS )
a  Request machine token refresh from contract server.

        @param machine_token: The machine token needed to talk to
            this contract service endpoint.
        @param contract_id: Unique contract id provided by contract service.
        @param machine_id: Optional unique system machine id. When absent,
            contents of /etc/machine-id will be used.

        @return: Dict of the JSON response containing refreshed machine-token
        r2   r3   r5   rs   r   )r9   r   r8   r;   r<   rp   )r   rD   r%   r9   rE   rF   rG   rI   API_V1_UPDATE_CONTRACT_MACHINErJ   r>   r
   rM   r?   rQ   rN   )	r-   rm   rt   rS   r9   r8   rU   r=   rV   r0   r0   r1   update_contract_machine}  s6        
  z(UAContractClient.update_contract_machinec                 C   s   t  jt  jt  jt  t  t  t	
 d}t| jjrt| jj}t }| jjpht | j| jjdd |D dd |D |r|j ndd}ni }||S )z9Return a dict of activity info data for contract requests)distributionrZ   r   rY   Zdesktopr[   ZclientVersionc                 S   s   g | ]
}|j qS r0   )r"   .0servicer0   r0   r1   
<listcomp>  s     z7UAContractClient._get_activity_info.<locals>.<listcomp>c                 S   s   i | ]}|j r|j|jqS r0   )Zvariant_enabledr"   Zvariant_namer   r0   r0   r1   
<dictcomp>  s    z7UAContractClient._get_activity_info.<locals>.<dictcomp>N)Z
activityIDZactivityToken	resourcesZresourceVariantsr4   )r   get_release_infor   Zget_kernel_infoZuname_releaser   Zget_dpkg_archZ
is_desktopZget_virt_typer   Zget_versionr   r%   Zis_attachedr   enabled_servicesr   readr,   Zactivity_idrD   Zactivity_tokenZattached_atrH   )r-   Zmachine_infor   Zattachment_datarT   r0   r0   r1   rG     s4    

z#UAContractClient._get_activity_info)N)N)N)N)N)__name__
__module____qualname__Zcfg_url_base_attrr   r   r)   r   ZretrysocketZtimeoutrX   r   strr   r_   rb   r   ZAutoAttachCloudInstancerl   rr   rw   r   r   r   r   r   rG   __classcell__r0   r0   r.   r1   r#   B   sP     *$ 
&( 
/ (r#   )request_bodyc              	   C   sJ   |  di }|  d|| d| d| d| ddt jdd	S )
a?  
    Transforms a request_body that has the new activity_info into a body that
    includes both old and new forms of machineInfo/activityInfo

    This is necessary because there may be old ua-airgapped contract
    servers deployed that we need to support.
    This function is used for attach and refresh calls.
    r7   r6   rY   r   rZ   r   ZLinux)r   rZ   r   typerelease)r6   r7   rY   os)rQ   r   r   r   )r   rT   r0   r0   r1   rI     s    	rI   T)r%   past_entitlementsnew_entitlementsallow_enabler   r&   c                 C   sv  ddl m} d}g }g }|| D  ]}	z||	 }
W n tk
rJ   Y q Y nX g }z"t| ||	i |
||d\}}W n tjk
r } z*t| d}|	|	 t
d|	|
 W 5 d}~X Y q  tk
r
 } z0t| |	| |	|	 td|	|
 W 5 d}~X Y q X |r |r t|	 q t| t|dkrVtjd	d
 t||D dn|rrtjdd
 |D ddS )a  Iterate over all entitlements in new_entitlement and apply any delta
    found according to past_entitlements.

    :param cfg: UAConfig instance
    :param past_entitlements: dict containing the last valid information
        regarding service entitlements.
    :param new_entitlements: dict containing the current information regarding
        service entitlements.
    :param allow_enable: Boolean set True if allowed to perform the enable
        operation. When False, a message will be logged to inform the user
        about the recommended enabled service.
    :param series_overrides: Boolean set True if series overrides should be
        applied to the new_access dict.
    r   )entitlements_enable_orderF)r%   orig_access
new_accessr   r   Tz+Failed to process contract delta for %s: %rNz5Unexpected error processing contract delta for %s: %rc                 S   s*   g | ]"\}}|t jjt|t d fqS ))rf   Zlog_path)r   ZUNEXPECTED_ERRORrF   r   r   )r   r"   	exceptionr0   r0   r1   r   /  s   z.process_entitlements_delta.<locals>.<listcomp>)failed_servicesc                 S   s   g | ]}|t jfqS r0   )r   Z!E_ATTACH_FAILURE_DEFAULT_SERVICES)r   r"   r0   r0   r1   r   <  s   )uaclient.entitlementsr   KeyErrorprocess_entitlement_deltarQ   r
   ZUbuntuProErrorri   r   appenderror	ExceptioneventZservice_processedZservices_failedlenZAttachFailureUnknownErrorzipZAttachFailureDefaultServices)r%   r   r   r   r   r   Zdelta_errorZunexpected_errorsr   r"   Znew_entitlementdeltasZservice_enableder0   r0   r1   process_entitlements_delta  sf    







r   F)r%   r   r   r   r   r&   c              
   C   s   ddl m} |rt| t||}d}|r|di d}|sT|di d}|sftj||d|di di d	d
}	z|| ||	d}
W n4 tjk
r } zt	
d| |W 5 d}~X Y nX |
j|||d}||fS )a,  Process a entitlement access dictionary deltas if they exist.

    :param cfg: UAConfig instance
    :param orig_access: Dict with original entitlement access details before
        contract refresh deltas
    :param new_access: Dict with updated entitlement access details after
        contract refresh
    :param allow_enable: Boolean set True if allowed to perform the enable
        operation. When False, a message will be logged to inform the user
        about the recommended enabled service.
    :param series_overrides: Boolean set True if series overrides should be
        applied to the new_access dict.

    :raise UbuntuProError: on failure to process deltas.
    :return: A tuple containing a dict of processed deltas and a
             boolean indicating if the service was fully processed
    r   entitlement_factoryFentitlementr   )Zorignewentitlementsobligationsuse_selectorrA   r%   r"   r    z3Skipping entitlement deltas for "%s". No such classNr   )r   r   apply_contract_overridesr   Zget_dict_deltasrQ   r
   Z InvalidContractDeltasServiceTypeEntitlementNotFoundErrorri   rj   Zprocess_contract_deltas)r%   r   r   r   r   r   r   Zretr"   r    r   excr0   r0   r1   r   C  sP       
   r   )rV   r&   c                 C   s   | j d}|r|d }|d }|dkrR|d t}tj|||d ddnF|dkr|d t}tj|||d dd	n|d
krtj|dt d S )NinfoZ
contractIdreasonzno-longer-effectivetimez%m-%d-%Y)rt   dateZcontract_expiry_dateznot-effective-yet)rt   r   Zcontract_effective_dateznever-effective)rt   )	rN   rQ   strftimer   r
   ZAttachForbiddenExpiredZAttachForbiddenNotYetZAttachForbiddenNeverZAttachExpiredToken)rV   r   rt   r   r   r0   r0   r1   rL     s*    rL   r'   c           	      C   s   t | }| }|j}|d }|d d d }t| d}|j||d}|| tj	  |
di 
dt| }t| t| || dd	 d
S )zRequest contract refresh from ua-contracts service.

    :raise UbuntuProError: on failure to update contract or error processing
        contract deltas
    :raise ConnectivityError: On failure during a connection
    r@   ZmachineTokenInfoZcontractInfoidr'   )rm   rt   r6   Fr   N)r*   r+   r   rm   r#   r   rv   r   rD   cache_clearrQ   r   r   )	r%   r,   Zorig_entitlementsZ
orig_tokenrm   rt   Zcontract_clientZresprS   r0   r0   r1   refresh  s.    

 

 
r   r$   c                 C   s   t | }| }|dg S )zDQuery available resources from the contract server for this machine.r   )r#   r_   rQ   )r%   clientr   r0   r0   r1   get_available_resources  s    r   )r%   rC   r&   c                 C   s   t | }||S )z/Query contract information for a specific token)r#   rb   )r%   rC   r   r0   r0   r1   get_contract_information  s    r   )override_selectorselector_valuesr&   c                 C   s<   d}|   D ]*\}}||f|  kr* dS |t| 7 }q|S )Nr   )itemsOVERRIDE_SELECTOR_WEIGHTS)r   r   Zoverride_weightselectorvaluer0   r0   r1   _get_override_weight  s    r   )r   series_namerd   r    r&   c           
      C   sz   i }||d}|r||d< |  di  |i }|r>||td < t| dg }|D ] }t| d|}	|	rT|||	< qT|S )N)r   r   r    r   r   	overridesr   )popr   copydeepcopyrQ   r   )
r   r   rd   r    r   r   r   Zgeneral_overridesoverrideZweightr0   r0   r1   _select_overrides  s"    

 
r   )r   r   r    r&   c                 C   s   ddl m} tt| td| kgs0td| |dkrBt j	n|}| \}}| 
di }t||||}t| D ]J\}	}
|
 D ]8\}}| d 
|}t|tr|| q|| d |< qqvdS )a  Apply series-specific overrides to an entitlement dict.

    This function mutates orig_access dict by applying any series-overrides to
    the top-level keys under 'entitlement'. The series-overrides are sparse
    and intended to supplement existing top-level dict values. So, sub-keys
    under the top-level directives, obligations and affordance sub-key values
    will be preserved if unspecified in series-overrides.

    To more clearly indicate that orig_access in memory has already had
    the overrides applied, the 'series' key is also removed from the
    orig_access dict.

    :param orig_access: Dict with original entitlement access details
    r   )get_cloud_typer   z?Expected entitlement access dict. Missing "entitlement" key: {}N)Zuaclient.clouds.identityr   all
isinstancedictRuntimeErrorrF   r   r   r   rQ   r   sortedr   rE   )r   r   r    r   r   rd   _Zorig_entitlementr   Z_weightZoverrides_to_applykeyr   Zcurrentr0   r0   r1   r     s.    
   
r   )r%   r   r&   c              	   C   s   ddl m} g }| D ]\}}|di dd}z|| ||d}W n tjk
r`   Y qY nX |di di }|d}	|||	r| \}
}|
r|t	||d	 q|S )
Nr   r   r   r   rA   r   r   resourceToken)r"   r    )
r   r   r   rQ   r
   r   Z_should_enable_by_default
can_enabler   r!   )r%   r   r   Zenable_by_default_servicesZent_nameZ	ent_valuer    Zentr   r   r   r   r0   r0   r1   get_enabled_by_default_services'  s(    

r   )T)FT)N)NN)Nr   Zloggingr   collectionsr   typingr   r   r   r   r   Zuaclient.files.machine_tokenfilesrm   r*   Zuaclientr   r	   r
   r   r   r   r   r   r   Z-uaclient.api.u.pro.status.enabled_services.v1r   Z(uaclient.api.u.pro.status.is_attached.v1r   Zuaclient.configr   Zuaclient.defaultsr   Zuaclient.files.state_filesr   r   Zuaclient.httpr   Zuaclient.logr   rK   r   r   r^   rq   rh   ru   ra   r{   r   r   r   Zget_event_loggerr   Z	getLoggerZreplace_top_level_logger_namer   ri   r!   ZUAServiceClientr#   r   rI   r   boolr   r   ZHTTPResponseZNamedMessagerL   r   r   r   intr   r   r   r   r0   r0   r0   r1   <module>   s   ,      

^  


@!
 
 
  
2 
