o
    *bZ                     @   sb   d dl Z d dlmZ d dlmZ d dlmZ d dlmZ d dl	m
Z
mZmZ G dd de
eZdS )	    N)HTTPResponse)Any)request)URLError)PluginIndependentPlugin	PluginOptc                   @   s   e Zd ZdZdZdZeddddgZdZd	Z	d
Z
dZdd Zdd ZdefddZededefddZdd ZededefddZdS )GCPzGoogle Cloud Platformgcp)Zvirtkeep-piiFzyStop the plugin from removing PIIs like project name or organization ID from the metadata retrieved from Metadata server.)defaultZdescz3http://metadata.google.internal/computeMetadata/v1/zBhttp://metadata.google.internal/computeMetadata/v1/?recursive=truez[--REDACTED--]zDDMI: Google Google Compute Engine/Google Compute Engine, BIOS Googlec                 C   s(   |  d}|d dkrdS | j|d v S )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statusr   Foutput)Zexec_cmd
GOOGLE_DMI)selfr    r   8/usr/lib/python3/dist-packages/sos/report/plugins/gcp.pycheck_enabled(   s   
zGCP.check_enabledc              
   C   s   | j ddgd z|  | _|   | jtj| jdddddgd W n tyA } z| jt|dddgd W Y d	}~nd	}~ww | j	d
dgd d	S )z
        Collect the following info:
         * Metadata from the Metadata server
         * `gcloud auth list` output
         * Any google services output from journal
        zgcloud auth listr
   )tags   )indentzmetadata.jsonT)Zplug_dirr   Nzgoogle*)Zunitsr   )
Zadd_cmd_outputget_metadatametadatascrub_metadataZadd_string_as_filejsondumpsRuntimeErrorstrZadd_journal)r   errr   r   r   setup3   s   	

z	GCP.setupreturnc                 C   s"   |  | 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_bodyr   r   r   r   L   s   
zGCP.get_metadataurlc              
   C   sv   zt j| ddid}t |}W n ty$ } ztdt| d}~ww |jdkr9td|j d|   |S )	zf
        Query the given url address with headers required by Google Metadata
        Server.
        zMetadata-FlavorZGoogle)Zheadersz,Failed to communicate with Metadata Server: N   z2Failed to communicate with Metadata Server (code: z): )	r   ZRequestZurlopenr   r   r   coder$   r%   )r(   Zreqr'   r   r   r   r   r"   U   s$   


zGCP._query_addressc                    s    drdS jd d  jd d tdtdtf fdd	j_jd d
 d jd d
 d dS )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.
        r   NZprojectZ	projectIdZnumericProjectIddatar!   c                    s   t | trd| v rj| d< fdd|  D S t | tr'fdd| D S t | tr8| j jS t | trF| krDjS | S | S )Ntokenc                    s   i | ]\}} | |qS r   r   ).0kvscrubr   r   
<dictcomp>}   s    z5GCP.scrub_metadata.<locals>.scrub.<locals>.<dictcomp>c                    s   g | ]} |qS r   r   )r-   valuer0   r   r   
<listcomp>   s    z5GCP.scrub_metadata.<locals>.scrub.<locals>.<listcomp>)
isinstancedictREDACTEDitemslistr   replaceint)r+   Z
project_idZproject_numberZproject_number_intr1   r   r   r   r1   w   s   





z!GCP.scrub_metadata.<locals>.scrubZ
attributeszssh-keysZsshKeys)Z
get_optionr   r   r   safe_redact_key)r   r   r<   r   r   g   s   
	zGCP.scrub_metadatadict_objkeyc                 C   s   ||v r| j ||< d S d S )N)r7   )clsr>   r?   r   r   r   r=      s   zGCP.safe_redact_keyN)__name__
__module____qualname__Z
short_descZplugin_nameZprofilesr   Zoption_listZMETADATA_ROOTr#   r7   r   r   r    r6   r   staticmethodr   r   r"   r   classmethodr=   r   r   r   r   r	      s(    	'r	   )r   Zhttp.clientr   typingr   Zurllibr   Zurllib.errorr   Zsos.report.pluginsr   r   r   r	   r   r   r   r   <module>   s   