U
    *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edddZeeedddZdd Zeeed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 k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.
        dmesgZ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 z6|  | _|   | jtj| jdddddgd W n: tk
r } z| jt|dddgd W 5 d	}~X Y nX | 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)urlr    c              
   C   s   z t j| ddid}t |}W n2 tk
rR } ztdt| W 5 d}~X Y nX |jdkr|t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ttd 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r2d| krj| d< fdd|  D S t | trNfdd| D S t | trp| j jS t | tr| kr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,   valuer/   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_intr0   r   r   r   r0   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   ||kr| j ||< d S )N)r6   )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"   r6   r   r   r   r5   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   