HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux ip-10-0-8-47 6.8.0-1021-aws #23~22.04.1-Ubuntu SMP Tue Dec 10 16:31:58 UTC 2024 aarch64
User: ubuntu (1000)
PHP: 8.1.2-1ubuntu2.22
Disabled: NONE
Upload Files
File: //lib/python3/dist-packages/cloudinit/cmd/__pycache__/query.cpython-310.pyc
o

�Ad�(�@s�dZddlZddlZddlZddlmZddlmZmZddl	m
Z
mZddlm
Z
mZmZddlmZdZe�e�Zdd	d
�Zdd�Zd
efdd�Zdedededefdd�Zdd�Zdd�Zedkrie�dSdS)aTQuery standardized instance metadata provided to machine, returning a JSON
structure.

Some instance-data values may be binary on some platforms, such as userdata and
vendordata. Attempt to decompress and decode UTF-8 any binary values.

Any binary values in the instance metadata will be base64-encoded and prefixed
with "ci-b64:" in the output. userdata and, where applicable, vendordata may
be provided to the machine gzip-compressed (and therefore as binary data).
query will attempt to decompress these to a string before emitting the JSON
output; if this fails, they are treated as binary.
�N)�EACCES)�log�util)�addLogHandlerCLI�read_cfg_paths)�convert_jinja_instance_data�get_jinja_variable_alias�render_jinja_payload)�REDACT_SENSITIVE_VALUE�querycCs�|s	tjttd�}|jdddddd�|jdd	td
t��d���d�|jd
ddddd�|jddtdd�|jddtdd�|jdtddd�|jddddddd�|jdd td!d"d#�|S)$a#Build or extend an arg parser for query utility.

    @param parser: Optional existing ArgumentParser instance representing the
        query subcommand which will be extended to support the args of
        this utility.

    @returns: ArgumentParser with proper argument configuration.
    )�prog�descriptionz-dz--debug�
store_trueFz+Add verbose messages during template render)�action�default�helpz-iz--instance-dataz,Path to instance-data.json file. Default is �
instance_data)�typerz-lz--list-keyszBList query keys available at the provided instance-data <varname>.z-uz--user-datazHPath to user-data file. Default is /var/lib/cloud/instance/user-data.txtz-vz
--vendor-datazLPath to vendor-data file. Default is /var/lib/cloud/instance/vendor-data.txt�varname�?z�A dot-delimited specific variable to query from instance-data. For example: v1.local_hostname. If the value is not JSON serializable, it will be base64-encoded and will contain the prefix "ci-b64:". )r�nargsrz-az--all�dump_allz Dump all available instance-data)rr�destrz-fz--format�formatz�Optionally specify a custom output format string. Any instance-data variable can be specified between double-curly braces. For example -f "{{ v2.cloud_name }}")rrr)�argparse�ArgumentParser�NAME�__doc__�add_argument�strr�get_runpath��parser�r#�5/usr/lib/python3/dist-packages/cloudinit/cmd/query.py�
get_parser$st	�
��	�
�	�	���r%cCs>tj|dd�}z|�d�WStytj|ddd�YSw)z�Attempt to return a string of user-data from ud_file_path

    Attempt to decode or decompress if needed.
    If unable to decode the content, raw bytes will be returned.

    @returns: String of uncompressed userdata if possible, otherwise bytes.
    F)�decodezutf-8T)�quietr&)r�	load_filer&�UnicodeDecodeError�decomp_gzip)�ud_file_path�bdatar#r#r$�
load_userdata|s�r-�returnc
Cs6t��}t�}|r|}n#|�d�}|dkr-|�d�}tj�|�r#|}nt�d||�|}n|}|r4|}ntj�|j	d�}|rA|}	ntj�|j	d�}	zt
�|�}
Wn!tt
fyq}z|jtkrft�d|��t�d|��d	}~wwt
�|
�}|dkr�d
t|f|d<d
t|	f|d<|St|�|d<t|	�|d<|S)
a�Return a dict of merged instance-data, vendordata and userdata.

    The dict will contain supplemental userdata and vendordata keys sourced
    from default user-data and vendor-data files.

    Non-root users will have redacted INSTANCE_JSON_FILE content and redacted
    vendordata and userdata values.

    :raise: IOError/OSError on absence of instance-data.json file or invalid
        access perms.
    rr�instance_data_sensitivez4Missing root-readable %s. Using redacted %s instead.z
user-data.txtzvendor-data.txtz$No read permission on '%s'. Try sudozMissing instance-data file: %sNz<%s> file:%s�userdata�
vendordata)�os�getuidrr �path�exists�LOG�warning�join�
instance_linkrr(�IOError�OSError�errnor�error�	load_jsonr
r-)r�	user_data�vendor_data�uid�paths�instance_data_fn�redacted_data_fn�sensitive_data_fn�user_data_fn�vendor_data_fn�
instance_json�er#r#r$�_read_instance_data�s\

�
���

�
��rJ�jinja_vars_without_aliases�jinja_vars_with_aliasesr�	list_keysc
Cs�d}|}|�d�D]M}z||}Wn ty1}z|r#dj||d�}nd�|�}t|�|�d}~ww||vr;||}n|D]}	t|	�|krK||	}nq=|rR|d7}||7}q	|S)a�Return the value of the dot-delimited varname path in instance-data

    Split a dot-delimited jinja variable name path into components, walk the
    path components into the instance_data and look up a matching jinja
    variable name or cloud-init's underscore-delimited key aliases.

    :raises: ValueError when varname represents an invalid key name or path or
        if list-keys is provided by varname isn't a dict object.
    ��.z*instance-data '{key_path}' has no '{leaf}')�leaf�key_pathz Undefined instance-data key '{}'N)�split�KeyErrorr�
ValueErrorr)
rKrLrrM�walked_key_path�response�
key_path_partrI�msg�keyr#r#r$�(_find_instance_data_leaf_by_varname_path�s2�

��
�
rZc
Csvtt|jrtjntj�t|j|j|j	|j
g�s$t�d�t��
�dSzt|j|j|j�}Wnttfy;YdSw|j	r]dj	|j	d�}t|d||jrOdndd�}|r[t|�d	SdSt|�}|jr�t|dd
�}zt|||j|jd�}Wnttfy�}zt�|�WYd}~dSd}~ww|jr�t|t�s�t�d
|j�dSd�t|����}t|t�s�t �!|�}t|�d	S)z3Handle calls to 'cloud-init query' as a subcommand.zDExpected one of the options: --all, --format, --list-keys or varname�z## template: jinja
{fmt})�fmtzquery commandlineTF)�payload�
payload_fnr�debugr)�include_key_aliases)rKrLrrMNz+--list-keys provided but '%s' is not a dict�
)"rr6r_r�DEBUG�WARNING�anyrMrrrr=r%�
print_helprJrr?r@r:r;r	�printrrZrSrT�
isinstance�dictr8�sorted�keysrr�
json_dumps)�name�argsrr]�rendered_payloadrVrLrIr#r#r$�handle_args�sl�
����
�
��
�

rocCst�}t�tt|����dS)z,Tool to query specific instance-data values.N)r%�sys�exitror�
parse_argsr!r#r#r$�main4srs�__main__)N)rrr2rpr<r�	cloudinitrr�cloudinit.cmd.develrr�!cloudinit.handlers.jinja_templaterrr	�cloudinit.sourcesr
r�	getLoggerr6r%r-rhrJr�boolrZrors�__name__r#r#r#r$�<module>s8


XA���
�+=
�