HEX
Server: Apache
System: Linux scp1.abinfocom.com 5.4.0-216-generic #236-Ubuntu SMP Fri Apr 11 19:53:21 UTC 2025 x86_64
User: confeduphaar (1010)
PHP: 8.1.33
Disabled: exec,passthru,shell_exec,system
Upload Files
File: //usr/lib/python3/dist-packages/sos/report/plugins/__pycache__/gcp.cpython-38.pyc
U

i��d��@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d
�Zdd�Z
dd�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--]zDDMI: Google Google Compute Engine/Google Compute Engine, BIOS GooglecCs(|�d�}|ddkrdS|j|dkS)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.
        �dmesgZstatusrF�output)Zexec_cmd�
GOOGLE_DMI)�selfr
�r�8/usr/lib/python3/dist-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�setup3s	z	GCP.setupcCs~|jddgd��d}z,|��|_|��|�tj|jdd��Wn0tk
rn}z|�t|��W5d}~XYnXW5QRXdS)Nz
metadata.jsonr
r�)�indent)	Zcollection_file�get_metadata�metadata�scrub_metadata�write�json�dumps�RuntimeError�str)rZmfile�errrrr�collectAs
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_bodyrrrrKszGCP.get_metadata)�urlr$c
Cs�z tj|ddid�}t�|�}Wn2tk
rR}ztdt|���W5d}~XYnX|jdkr|td|j�d�|������|S)	zf
        Query the given url address with headers required by Google Metadata
        Server.
        zMetadata-FlavorZGoogle)Zheadersz,Failed to communicate with Metadata Server: N��z2Failed to communicate with Metadata Server (code: z): )	rZRequestZurlopenrr r!�coder'r()r+Zreqr*r"rrrr%Ts
�

��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|kr�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>|sz5GCP.scrub_metadata.<locals>.scrub.<locals>.<dictcomp>csg|]}�|��qSrr)r0�valuer3rr�
<listcomp>~sz5GCP.scrub_metadata.<locals>.scrub.<locals>.<listcomp>)�
isinstance�dict�REDACTED�items�listr!�replace�int)r.�Z
project_idZproject_numberZproject_number_intr4rrrr4vs



�
z!GCP.scrub_metadata.<locals>.scrubZ
attributeszssh-keysZsshKeys)Z
get_optionrr!r�safe_redact_keyrrr?rrfs	
��zGCP.scrub_metadata)�dict_obj�keycCs||kr|j||<dS)N)r:)�clsrArBrrrr@�szGCP.safe_redact_keyN)�__name__�
__module__�__qualname__Z
short_descZplugin_nameZprofilesrZoption_listZ
METADATA_ROOTr&r:rrrr#r9r�staticmethodr!rr%r�classmethodr@rrrrr	s(��
	'r	)rZhttp.clientr�typingrZurllibrZurllib.errorrZsos.report.pluginsrrrr	rrrr�<module>s