Your IP : 216.73.217.13


Current Path : /lib/python3/dist-packages/sos/report/plugins/__pycache__/
Upload File :
Current File : //lib/python3/dist-packages/sos/report/plugins/__pycache__/gcp.cpython-38.pyc

U

���bZ�@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
ed�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_enabledc
Cs�|jddgd�z6|��|_|��|jtj|jdd�dddgd�Wn:tk
r�}z|jt|�dddgd�W5d	}~XYnX|j	d
dgd�d	S)z�
        Collect the following info:
         * Metadata from the Metadata server
         * `gcloud auth list` output
         * Any google services output from journal
        zgcloud auth listr
)�tags�)�indentz
metadata.jsonT)Zplug_dirrNzgoogle*)Zunitsr)
Zadd_cmd_output�get_metadata�metadata�scrub_metadataZadd_string_as_file�json�dumps�RuntimeError�strZadd_journal)r�errrrr�setup3s	
�
�z	GCP.setup)�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�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): )	rZRequestZurlopenrrr�coder#r$)r'Zreqr&rrrrr!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|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)r,�valuer/rr�
<listcomp>sz5GCP.scrub_metadata.<locals>.scrub.<locals>.<listcomp>)�
isinstance�dict�REDACTED�items�listr�replace�int)r*�Z
project_idZproject_numberZproject_number_intr0rrrr0ws



�
z!GCP.scrub_metadata.<locals>.scrubZ
attributeszssh-keysZsshKeys)Z
get_optionrrr�safe_redact_key)rrr;rrgs	
��zGCP.scrub_metadata)�dict_obj�keycCs||kr|j||<dS)N)r6)�clsr=r>rrrr<�szGCP.safe_redact_keyN)�__name__�
__module__�__qualname__Z
short_descZplugin_nameZprofilesrZoption_listZ
METADATA_ROOTr"r6rrrr5r�staticmethodrrr!r�classmethodr<rrrrr	s&��	'r	)rZhttp.clientr�typingrZurllibrZurllib.errorrZsos.report.pluginsrrrr	rrrr�<module>s