HEX
Server: nginx/1.24.0
System: Linux ip-10-50-20-85 6.5.0-1022-aws #22~22.04.1-Ubuntu SMP Fri Jun 14 16:31:00 UTC 2024 x86_64
User: usefuloffices.org (1001)
PHP: 8.1.2-1ubuntu2.23
Disabled: NONE
Upload Files
File: //lib/python3/dist-packages/uaclient/__pycache__/livepatch.cpython-310.pyc
o

8�-d,�
@s�ddlZddlZddlZddlZddlmZddlmZmZm	Z	ddl
mZmZm
Z
mZmZmZddlmZmZmZmZmZmZddlmZdZdZd	Zd
Ze��ZGdd�de�Z Gd
d�de�Z!Gdd�de�Z"Gdd�de�Z#dee"fdd�Z$Gdd�dej%�Z&dee'fdd�Z(de)de)de)de)de	e'ee'ff
dd�Z*de)de)de)de)dee'f
d d!�Z+edd"�dee'fd#d$��Z,	d2d%e)d&eee-ddfd'd(�Z.			d3d)ee)d*ee)d&eee-ddfd+d,�Z/d-e)dee)fd.d/�Z0de'fd0d1�Z1dS)4�N)�	lru_cache)�List�Optional�Tuple)�event_logger�
exceptions�messages�
serviceclient�system�util)�
BoolDataValue�
DataObject�Field�IncorrectTypeError�StringDataValue�	data_list)�state_filesz
http-proxyzhttps-proxyz/snap/bin/canonical-livepatchz/v1/api/kernels/supportedc@sFeZdZededdd�ededdd�gZdeedee	fdd�Z
d	S)
�LivepatchPatchFixStatus�nameF�Name��required�dict_key�patched�PatchedcC�||_||_dS�N)rr)�selfrr�r�4/usr/lib/python3/dist-packages/uaclient/livepatch.py�__init__*�
z LivepatchPatchFixStatus.__init__N)�__name__�
__module__�__qualname__rrr�fieldsr�str�boolr rrrrr$s���rc@sNeZdZededdd�edee�ddd�gZdee	dee
efdd�Zd	S)
�LivepatchPatchStatus�stateF�Stater�fixes�FixescCrr)r)r+)rr)r+rrrr >r!zLivepatchPatchStatus.__init__N)r"r#r$rrrrr%rr&rr rrrrr(3s��
�
�r(c@s\eZdZededdd�ededdd�ededdd�gZdeedeedeefd	d
�Z	dS)�LivepatchStatusStatus�kernelF�Kernelr�	livepatch�	Livepatch�	supported�	SupportedcCs||_||_||_dSr)r.r0r2)rr.r0r2rrrr Xs
zLivepatchStatusStatus.__init__N)
r"r#r$rrr(r%rr&r rrrrr-Gs,������r-c@s8eZdZedee�ddd�gZdeeefdd�Z	dS)�LivepatchStatus�statusF�StatusrcCs
||_dSr)r5)rr5rrrr ms
zLivepatchStatus.__init__N)
r"r#r$rrr-r%rrr rrrrr4cs��	
�r4�returncCsnt�s
t�d�dSz
t�tdddg�\}}Wn&tjy=t�	��t�
d�Wd�YdS1s5wYYdSwzt�|�}Wn)tj
ynt�	��t�
d�|��Wd�YdS1sfwYYdSwzt�|�}Wn(ty�t�	��t�
d�|��Wd�YdS1s�wYYdSw|jdus�t|j�dkr�t�d	�dS|jd
S)Nz$canonical-livepatch is not installedr5z--format�jsonz7canonical-livepatch returned error when checking statusz4canonical-livepatch status returned invalid json: {}z<canonical-livepatch status returned unexpected structure: {}�z!canonical-livepatch has no statusr)�is_livepatch_installed�logging�debugr
�subp�
LIVEPATCH_CMDr�ProcessExecutionErrorr�disable_log_to_console�warningr8�loads�JSONDecodeError�formatr4�	from_dictrr5�len)�out�_�status_json�status_rootrrrr5ts\

�
���
��
���	
�
���

r5c@s8eZdZdZejZdededededee	f
dd�Z
d	S)
�UALivepatchClient�
livepatch_url�version�flavor�arch�codenamer7c
Cs�||||d�}|��}z|jt||d�\}}Wn1tyH}	z%t���t�d�t�|	�Wd�n1s8wYWYd}	~	dSd}	~	wwt|t	�sUt�d�dSt
|�dd��S)z�
        :returns: True if supported
                  False if unsupported
                  None if API returns error or ambiguous response
        )zkernel-version�flavour�architecturerP)�query_params�headersz4error while checking livepatch supported kernels APINz2livepatch api returned something that isn't a dictr3F)rT�request_url�"LIVEPATCH_API_V1_KERNELS_SUPPORTED�	Exceptionrr@r;rA�
isinstance�dictr'�get)
rrMrNrOrPrSrT�result�_headers�errr�is_kernel_supported�s8	��
����
�z%UALivepatchClient.is_kernel_supportedN)r"r#r$�cfg_url_base_attrr�UrlError�
api_error_clsr&rr'r^rrrrrK�s�����rKcCs2t�}|dur	dS|jdkrdS|jdkrdSdS)Nr2T�unsupportedF)r5r2)�	lp_statusrrr�_on_supported_kernel_cli�s

rdrMrNrOrPcCs�ztj��}Wntyd}Ynw|dur`tj�tjj�tjdd�}t	|j
|k|j|k|j|k|j
|k|j|kg�r`|jdur[t���
t�d�Wd�n1sVwYd|jfSdS)zOCheck local cache of kernel support

    :return: (is_cache_valid, result)
    N�)�daysz-livepatch kernel support cache has None valueT)FN)r�livepatch_support_cache�readrW�datetime�now�timezone�utc�	timedelta�all�	cached_atrMrNrOrPr2rr@r;rA)rMrNrOrP�
cache_data�one_week_agorrr�_on_supported_kernel_cache�s6��
���
	
��
rrcCs~t�j||||d�}tj�tj|||||tj�tjj	�d��|dur=t
���t�
d�Wd�|S1s8wY|S)N)rMrNrOrP)rMrNrOrPr2roz3livepatch kernel support API response was ambiguous)rKr^rrg�write�LivepatchSupportCacheDatarirjrkrlrr@r;rA)rMrNrOrPr2rrr�_on_supported_kernel_api�s2���
�
��ru)�maxsizecCs�t�}|durt�d�|St��}|jdus!|jdus!|jdur(t�d�dSt	�
t���}t��d}dj
|j|jd�}t||j||�\}}|rQt�d�|St�d�t||j||�S)	z�
    Checks CLI, local cache, and API in that order for kernel support
    If all checks fail to return an authoritative answer, we return None
    Nzusing livepatch cli for supportzHunable to determine enough kernel information to check livepatch support�seriesz{major}.{minor})�major�minorzusing livepatch support cachezusing livepatch support api)rdr;r<r
�get_kernel_inforNrxryrAr�standardize_arch_name�
get_dpkg_arch�get_platform_inforDrrru)�cli_says�kernel_inforOrP�lp_api_kernel_ver�is_cache_valid�
cache_saysrrr�on_supported_kernels<
�����
�


�r��
protocol_type�retry_sleepscCs(t�sdStjtdd�|�g|d�dS)a�
    Unset livepatch configuration settings for http and https proxies.

    :param protocol_type: String either http or https
    :param retry_sleeps: Optional list of sleep lengths to apply between
        retries. Specifying a list of [0.5, 1] tells subp to retry twice
        on failure; sleeping half a second before the first retry and 1 second
        before the second retry.
    N�configz	{}-proxy=�r�)r:r
r=r>rD)r�r�rrr�unconfigure_livepatch_proxyDs
�r��
http_proxy�https_proxycCsnddlm}|s
|rt�tjj|jd��|r$tj	t
dd�|�g|d�|r5tj	t
dd�|�g|d�dSdS)	a�
    Configure livepatch to use http and https proxies.

    :param http_proxy: http proxy to be used by livepatch. If None, it will
                       not be configured
    :param https_proxy: https proxy to be used by livepatch. If None, it will
                        not be configured
    :@param retry_sleeps: Optional list of sleep lengths to apply between
                               snap calls
    r)�LivepatchEntitlement)�servicer�z
http-proxy={}r�zhttps-proxy={}N)�uaclient.entitlementsr��event�infor�SETTING_SERVICE_PROXYrD�titler
r=r>)r�r�r�r�rrr�configure_livepatch_proxyXs$���
��r��keycCs\t�tdg�\}}t�d�|�|tj�}|r|�d�nd}|r&t�dd|�}|r,|�	�SdS)z�
    Gets the config value from livepatch.
    :param key: can be any valid livepatch config option
    :return: the value of the livepatch config option, or None if not set
    r�z
^{}: (.*)$r9Nz\"(.*)\"z\g<1>)
r
r=r>�re�searchrD�	MULTILINE�group�sub�strip)r�rGrH�match�valuerrr�get_config_option_value}sr�cCst�t�duSr)r
�whichr>rrrrr:�sr:r)NNN)2rir8r;r��	functoolsr�typingrrr�uaclientrrrr	r
r�uaclient.data_typesrr
rrrr�uaclient.filesr�HTTP_PROXY_OPTION�HTTPS_PROXY_OPTIONr>rV�get_event_loggerr�rr(r-r4r5�UAServiceClientrKr'rdr&rrrur��floatr�r�r�r:rrrr�<module>s�  *+����
�"����
�0��
�
����
�
�%