GOOD SHELL MAS BOY
Server: Apache/2.4.52 (Ubuntu)
System: Linux vmi1836763.contaboserver.net 5.15.0-130-generic #140-Ubuntu SMP Wed Dec 18 17:59:53 UTC 2024 x86_64
User: www-data (33)
PHP: 8.4.10
Disabled: NONE
Upload Files
File: //lib/python3/dist-packages/uaclient/__pycache__/actions.cpython-310.pyc
o

�P�f�8�@sPddlZddlZddlZddlZddlZddlZddlZddlmZm	Z	ddl
mZmZm
Z
mZmZmZmZmZddl
mZddl
mZmZddl
mZddl
mZmZmZddlmZmZmZm Z dd	l!m"Z"m#Z#dd
l$m%Z%m&Z&m'Z'm(Z(e�)�Z*e�+e�,e-��Z.dZ/dZ0d
e
j1dej2dejfdd�Z3	d8d
e
j1deej4dej2dejde5f
dd�Z6	d8d
e
j1de7de5de5ddf
dd�Z8	d9d
e
j1dej9ddfdd�Z:dd ddd!�d
e
j1d"e7d#e5d$e7de5d%e	ee7fd&d'�Z;ddd(�d
e
j1d)e	e7d*e5fd+d,�Zd-e7ddfd.d/�Z<	d:d-e7d0e	ee=ddfd1d2�Z>d
e
j1fd3d4�Z?d
e
j1d5e7fd6d7�Z@dS);�N)�List�Optional)�api�clouds�config�contract�entitlements�event_logger�
exceptions�	livepatch)�log)�messages�secret_manager)�status)�system�timer�util)�APPARMOR_PROFILES�CLOUD_BUILD_INFO�DEFAULT_CONFIG_FILE�DEFAULT_LOG_PREFIX)�
machine_token�notices)�AttachmentData�attachment_data_file�machine_id_file�timer_jobs_state_file)zapt-news.servicezesm-cache.servicezua-timer.servicezua-timer.timerzua-auto-attach.pathzua-auto-attach.servicezua-reboot-cmds.servicezubuntu-advantage.service�
�cfg�contract_client�attached_atcCs<ddlm}t�t|d��tj|d�||�|��dS)Nr��update_motd_messages�r �r)�uaclient.timer.update_messagingr"r�writer�	ua_statusr�update_activity_token)rrr r"�r)�2/usr/lib/python3/dist-packages/uaclient/actions.py�_handle_partial_attach8s
r+F�services_to_be_enabled�silentc

CsDd}g}g}z'|D]"}t||j|j|d�\}	}
||	M}|	s$|�|j�q	tj|jd�q	WnJtjyH}zt�|j�t	|||�|�d}~wtj
yY|�|j�d}Yntyw}zd}|�|j�|�|�WYd}~nd}~ww|s�t	|||�t�|�|r�tj
dd�t||�D�d��tjdd�|D�d��dS)	NT)r�name�variantr-)�serviceFcSs,g|]\}}|tjjt|�t��d�f�qS))�	error_msg�log_path)r
�UNEXPECTED_ERROR�format�str�pro_log�get_user_or_root_log_file_path)�.0r.�	exceptionr)r)r*�
<listcomp>qs����z,_enable_default_services.<locals>.<listcomp>)�failed_servicescSsg|]}|tjf�qSr))r
�!E_ATTACH_FAILURE_DEFAULT_SERVICES)r8r.r)r)r*r:�s��)�enable_entitlement_by_namer.r/�append�event�service_processedr
�ConnectivityError�service_failedr+�UbuntuProError�	Exception�services_failed�AttachFailureUnknownError�zip�AttachFailureDefaultServices)
rr,rr r-�retr;�unexpected_errors�enable_by_default_service�ent_ret�reason�exc�er)r)r*�_enable_default_servicesEs^
��
���
������rP�token�allow_enable�returnc
Cs�ddlm}ddlm}tj�|�t�|�}t	�
|�}tjjtj
jd�}|j||d�}	t��j}
|	�di��di�}tdd	�|�d
g�D��}|�di��di��d
d�}
|
rvt�|
�}|
|
krjtj|j|jd��tjtjj|j|jd�|�|	�z||�Wntjy�}z|� �|�d}~wwtj!�"�|	�di��dt�!|��}t#�|�|r�t	�$||�%��}t&|||||d�t'�t(|d��||�t)�*�dS)aC
    Common functionality to take a token and attach via contract backend
    :raise ConnectivityError: On unexpected connectivity issues to contract
        server or inability to access identity doc from metadata service.
    :raise ContractAPIError: On unexpected errors when talking to the contract
        server.
    r)�+check_entitlement_apt_directives_are_uniquer!)�tz)�contract_token�
attachment_dt�machineTokenInfo�contractInfocss,�|]}|�d�dkr|�d�|fVqdS)�type�supportN)�get)r8rOr)r)r*�	<genexpr>�s���z$attach_with_token.<locals>.<genexpr>�resourceEntitlementsr[�affordances�
onlySeriesN)�release�series_codename�	machineId)rr,rr r-r#)+�uaclient.entitlementsrTr%r"r�secrets�
add_secretr�get_machine_token_filer�UAContractClient�datetime�now�timezone�utc�add_contract_machiner�get_release_info�seriesr\�dict�get_distro_infor
�AttachFailureRestrictedReleaserarbr�add�Notice�LIMITED_TO_RELEASEr&�%EntitlementsAPTDirectivesAreNotUnique�delete�get_machine_id�cache_clearr�get_enabled_by_default_servicesrrPrrr�start)rrQrRr-rTr"�machine_token_filerr �new_machine_token�current_seriesrY�support_resource�only_series�allowed_releaserO�
machine_idr,r)r)r*�attach_with_token�sv


�
�
�
�
��
��

�
��r�T�cloudcCs0t�|�}|j|d�}|d}t|||d�dS)a\
    :raise ConnectivityError: On unexpected connectivity issues to contract
        server or inability to access identity doc from metadata service.
    :raise ContractAPIError: On unexpected errors when talking to the contract
        server.
    :raise NonAutoAttachImageError: If this cloud type does not have
        auto-attach support.
    )�instance�
contractToken)rQrRN)rrh�%get_contract_token_for_cloud_instancer�)rr�rRr�
tokenResponserQr)r)r*�auto_attach�s

�r��)�access_onlyr/r-�
extra_argsr.r�r/r�c	Csftj|||||d�}|st�tjj|jd��|�t	�
��\}}|r/|s/t�tjj|jd��||fS)z�
    Constructs an entitlement based on the name provided. Passes kwargs onto
    the entitlement constructor.
    :raise EntitlementNotFoundError: If no entitlement with the given name is
        found, then raises this error.
    )rr.r/r�r�)�title)r�entitlement_factoryr?�infor
�
ENABLING_TMPLr4r��enabler�ProgressWrapper�ENABLED_TMPL)	rr.r�r/r-r��entitlementrLrMr)r)r*r=�s�r=)�simulate_with_token�show_allr�r�cCs:|rtj|||d�\}}||fStj||d�}d}||fS)z6
    Construct the current Pro status dictionary.
    )rrQr��rr�r)r'�simulate_statusr)rr�r�rrIr)r)r*rs	
�	�r�filenamec
Cs�gd�}d}d}z	t�|�\}}Wn'tjy8}zt�dt|��t�d�|�t|��WYd}~dSd}~ww|r[g}|�	d�D]
}t
�||�rO|�|�qBt�|d�
|��dSdS)z�
    Helper which gets ubuntu_pro apparmor logs from the kernel from the last
    day and writes them to the specified filename.
    )�
journalctlz-bz-kz--since=1 day agoz7apparmor=\".*(profile=\"ubuntu_pro_|name=\"ubuntu_pro_)Nz!Failed to collect kernel logs:
%s�{}-error�
)r�subpr
�ProcessExecutionError�LOG�warningr5�
write_filer4�split�re�searchr>�join)r��cmd�apparmor_re�kernel_logs�_rO�
apparmor_logs�kernel_liner)r)r*�_write_apparmor_logs_to_file)s$$��
��r��return_codesc
Csrz
tj|��|d�\}}Wntjy,}zt�d�|�t|��WYd}~dSd}~wwt�|d�||��dS)zCHelper which runs a command and writes output or error to filename.)�rcsr�Nzstdout:
{}

stderr:
{})rr�r�r
r�r�r4r5)r�r�r��out�errrOr)r)r*�_write_command_output_to_fileBs$���r�cs,�jpt�jtjjtg�fdd�tjD��S)Nc3s(�|]}t|tjj�r|��jVqdS�N)�
issubclassr�repo�RepoEntitlement�	repo_file)r8�entitlement_clsr$r)r*r]Ws��
�z#_get_state_files.<locals>.<genexpr>)	�cfg_pathr�log_filer�ua_file�pathrr�ENTITLEMENT_CLASSESr$r)r$r*�_get_state_filesPs�
��r��
output_dirc
Cs�tdd�|��td�tj�d�|��tdd�|��tdd�|��td	�d
�dd�tD���d
�|��tD]}td�|�d�||�ddgd�q9t|dd�\}}t�d�|�t	j
|tjd��t�
�}t�d�|�t	�
|��t|�}t��r�t��dt�nt��g}t|�D]7\}}	zt�t�|	��}
t�tj�|d�|��|
�Wq�ty�}zt�d|	t|��WYd}~q�d}~ww|t�td�D]K}tj�|��rzt�|�}
Wnty�}zt�d|t|��WYd}~q�d}~wwt�|
�}
t���r	t�||
�t�tj�|tj� |��|
�q�t!d�|��t"D]1}tj�|��rRz	t#�$||�W�q"t�yQ}zt�d|t|��WYd}~�q"d}~ww�q"dS)zG
    Write all relevant Ubuntu Pro logs to the specified directory
    zcloud-idz{}/cloud-id.txtz	{} statusz{}/livepatch-status.txtzsystemctl list-timers --allz{}/systemd-timers.txtzujournalctl --boot=0 -o short-precise -u cloud-init-local.service -u cloud-init-config.service -u cloud-config.servicez{}/cloud-init-journal.txtzjournalctl -o short-precise {}� cSsg|]}d|vrd�|��qS)z.servicez-u {})r4)r8�sr)r)r*r:zsz collect_logs.<locals>.<listcomp>z{}/pro-journal.txtzsystemctl status {}z	{}/{}.txtr�)r�Fr�z{}/pro-status.json)�clsz{}/environment_vars.jsonNz
user{}.logz&Failed to collect user log file: %s
%s�*zFailed to load file: %s
%sz{}/apparmor_logs.txtzFailed to copy file: %s
%s)%r�r4r�
LIVEPATCH_CMDr��UA_SERVICESrrr��json�dumpsr�DatetimeAwareJSONEncoder�get_pro_environmentr��we_are_currently_rootr6�get_all_user_log_files�USER_LOG_COLLECTED_LIMIT�get_user_log_file�	enumerate�redact_sensitive_logs�	load_file�osr�rDr�r�r5�globr�isfile�basenamer�r�shutil�copy)
rr�r0�
pro_statusr��env_vars�state_files�user_log_files�log_file_idxr��contentrO�fr)r)r*�collect_logs_s�
�
���	���
������
�����

������r�)F)Tr�)Arir�r��loggingr�r�r��typingrr�uaclientrrrrrr	r
rrr6r
rrr'rrr�uaclient.defaultsrrrr�uaclient.filesrr�uaclient.files.state_filesrrrr�get_event_loggerr?�	getLogger�replace_top_level_logger_name�__name__r�r�r��UAConfigrhr+�EnableByDefaultService�boolrPr5r��AutoAttachCloudInstancer�r=r��intr�r�r�r)r)r)r*�<module>s�(
��
������
�F�����
�V���
�������

�%���
���
�
�