HEX
Server: Apache
System: Linux 185.122.168.184.host.secureserver.net 5.14.0-570.60.1.el9_6.x86_64 #1 SMP PREEMPT_DYNAMIC Wed Nov 5 05:00:59 EST 2025 x86_64
User: barbeatleanalyti (1024)
PHP: 8.1.33
Disabled: NONE
Upload Files
File: //lib/python3.9/site-packages/sos/report/plugins/__pycache__/gcp.cpython-39.opt-1.pyc
a

\��h��@sbddlZddlmZddlmZddlmZddlmZddl	m
Z
mZmZGdd�de
e�Z
dS)	�N)�HTTPResponse)�Any)�request)�URLError)�Plugin�IndependentPlugin�	PluginOptc@s�eZdZdZdZdZedddd�gZdZd	Z	d
Z
dZdZd
d�Z
dd�Zdd�Zed�dd�Zeeed�dd��Zdd�Zeeed�dd��ZdS)�GCPzGoogle Cloud Platform�gcp)Zvirt�keep-piiFzyStop the plugin from removing PIIs like project name or organization ID from the metadata retrieved from Metadata server.)�defaultZdescz3http://metadata.google.internal/computeMetadata/v1/zBhttp://metadata.google.internal/computeMetadata/v1/?recursive=truez[--REDACTED--]NzDDMI: Google Google Compute Engine/Google Compute Engine, BIOS GooglecCs(|�d�}|ddkrdS|j|dvS)z�
        Checks if this plugin should be executed at all. In this case, it
        will check the `dmesg` command output to see if the system is
        running on a Google Cloud Compute instance.
        �dmesg�statusrF�output)Zexec_cmd�
GOOGLE_DMI)�selfr
�r�:/usr/lib/python3.9/site-packages/sos/report/plugins/gcp.py�
check_enabled)s
zGCP.check_enabledcCs$|jddgd�|jddgd�dS)z�
        Collect the following info:
         * Metadata from the Metadata server
         * `gcloud auth list` output
         * Any google services output from journal
        zgcloud auth listr
��tagszgoogle*)ZunitsrN)Zadd_cmd_outputZadd_journal�rrrr�setup4s	z	GCP.setupcCs�|jddgd��p}z,|��|_|��|�tj|jdd��Wn2typ}z|�t|��WYd}~n
d}~00Wd�n1s�0YdS)Nz
metadata.jsonr
r�)�indent)	Zcollection_file�get_metadata�metadata�scrub_metadata�write�json�dumps�RuntimeError�str)rZmfile�errrrr�collectBs
zGCP.collect)�returncCs"|�|j�}|����}t�|�S)zq
        Retrieves metadata from the Metadata Server and transforms it into a
        dictionary object.
        )�_query_address�METADATA_QUERY�read�decoder�loads)r�responseZ
response_bodyrrrrLszGCP.get_metadata)�urlr%c
Cs�zntj|ddid�}t�|��>}|jdkrHtd|j�d�|������|Wd�WS1sb0YWn6ty�}ztdt|��|�WYd}~n
d}~00dS)	zf
        Query the given url address with headers required by Google Metadata
        Server.
        zMetadata-FlavorZGoogle)Zheaders��z2Failed to communicate with Metadata Server (code: z): Nz,Failed to communicate with Metadata Server: )	rZRequestZurlopen�coder!r(r)rr")r,Zreqr+r#rrrr&Us$
�
��(
��zGCP._query_addresscs���d�rdS�jdd��jdd�t���ttd������fdd����j��_���jdd	d
����jdd	d�dS)a"
        Remove all PII information from metadata, unless a keep-pii option
        is specified.

        Note: PII information collected by this plugin, like
        project number, account names etc. might be required by Google
        Cloud Support for faster issue resolution.
        rNZprojectZ	projectIdZnumericProjectId)�datar%cs�t|t�r2d|vr�j|d<�fdd�|��D�St|t�rN�fdd�|D�St|t�rp|���j����j�St|t�r�|�kr��jS|S|S)N�tokencsi|]\}}�|��|��qSrr)�.0�k�v��scrubrr�
<dictcomp>�z5GCP.scrub_metadata.<locals>.scrub.<locals>.<dictcomp>csg|]}�|��qSrr)r1�valuer4rr�
<listcomp>�r7z5GCP.scrub_metadata.<locals>.scrub.<locals>.<listcomp>)�
isinstance�dict�REDACTED�items�listr"�replace�int)r/�Z
project_idZproject_numberZproject_number_intr5rrrr5ys



�
z!GCP.scrub_metadata.<locals>.scrubZ
attributeszssh-keysZsshKeys)Z
get_optionrr"r�safe_redact_keyrrrArris	
��zGCP.scrub_metadata)�dict_obj�keycCs||vr|j||<dS)z
 Redact keys N)r<)�clsrCrDrrrrB�szGCP.safe_redact_key)�__name__�
__module__�__qualname__Z
short_descZplugin_nameZprofilesrZoption_listZ
METADATA_ROOTr'r<rrrrr$r;r�staticmethodr"rr&r�classmethodrBrrrrr	s*��
	'r	)rZhttp.clientr�typingrZurllibrZurllib.errorrZsos.report.pluginsrrrr	rrrr�<module>s