
    G%@i                     \    d dl 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ee	      Zy)    N)Any)request)URLError)PluginIndependentPlugin	PluginOptc                       e Zd ZdZdZdZ eddd      gZdZd	Z	d
Z
dZd Zd Zd ZdefdZdedefdZd Zededefd       Zy)GCPzGoogle Cloud Platformgcp)virtkeep-piiFzyStop the plugin from removing PIIs like project name or organization ID from the metadata retrieved from Metadata server.)defaultdescz(/sys/devices/virtual/dmi/id/product_namezBhttp://metadata.google.internal/computeMetadata/v1/?recursive=truez[--REDACTED--]Nc                     t        | j                  d      5 }d|j                         v cddd       S # 1 sw Y   yxY w)zo
        Checks if this plugin should be executed based on the presence of
        GCE entry in sysfs.
        zutf-8)encodingzGoogle Compute EngineN)openPRODUCT_PATHread)selfsys_files     8/usr/lib/python3/dist-packages/sos/report/plugins/gcp.pycheck_enabledzGCP.check_enabled$   s9    
 $##g6 	>(*hmmo=	> 	> 	>s   4=c                 T    | j                  ddg       | j                  ddg       y)z
        Collect the following info:
         * `gcloud auth list` output
         * Any google services output from journal
        zgcloud auth listr   tagszgoogle*)unitsr   N)add_cmd_outputadd_journal)r   s    r   setupz	GCP.setup,   s1     	.eW= 	yw7    c                 b   | j                  ddg      5 }	 | j                         | _        | j                          |j	                  t        j                  | j                  d             d d d        y # t        $ r$}|j	                  t        |             Y d }~1d }~ww xY w# 1 sw Y   y xY w)Nzmetadata.jsonr   r      )indent)	collection_fileget_metadatametadatascrub_metadatawritejsondumpsRuntimeErrorstr)r   mfileerrs      r   collectzGCP.collect9   s    !!/!@ 	&E& $ 1 1 3##%DJJt}}Q?@		& 	&
   &CH%%&	& 	&s/   B%AA55	B">BB%B""B%%B.returnc                 b    | j                  | j                        }t        j                  |      S )zq
        Retrieves metadata from the Metadata Server and transforms it into a
        dictionary object.
        )_query_addressMETADATA_QUERYr)   loads)r   response_bodys     r   r%   zGCP.get_metadataC   s)    
 ++D,?,?@zz-((r    urlc                    	 t        j                  |ddi      }t        j                  |      5 }|j                  dk7  r8t	        d|j                   d|j                         j                         z         |j                         j                         cddd       S # 1 sw Y   yxY w# t        $ r}t	        dt        |      z         |d}~ww xY w)	zf
        Query the given url address with headers required by Google Metadata
        Server.
        zMetadata-FlavorGoogle)headers   z2Failed to communicate with Metadata Server (code: z): Nz,Failed to communicate with Metadata Server: )	r   Requesturlopencoder+   r   decoder   r,   )r   r6   reqresponser.   s        r   r2   zGCP._query_addressK   s    
	//#0A8/LMC% 0==C'&""*--5 ..012 2  }}--/0 0 0  	>SIK	s5   .B+ A%B	B+ B($B+ (B+ +	C4CCc                 r     j                  d      ry j                  d   d    j                  d   d   t              dt        dt        f fd  j                         _         j	                   j                  d   d	   d
        j	                   j                  d   d	   d       y)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   Nproject	projectIdnumericProjectIddatar0   c                    t        | t              rCd| v rj                  | d<   | j                         D ci c]  \  }} |       |       c}}S t        | t              r| D cg c]
  } |       c}S t        | t
              r6| j                  j                        j                  j                        S t        | t              r| k(  rj                  S | S | S c c}}w c c}w )Ntoken)
isinstancedictREDACTEDitemslistr,   replaceint)	rE   kvvalue
project_idproject_numberproject_number_intscrubr   s	       r   rU   z!GCP.scrub_metadata.<locals>.scrubn   s    $%d? %)MMDM7;zz|Dtq!a%(*DD$%267e77$$||NDMMB#GJ>?$$(,0B(Bt}}LLK E7s   C&)C,
attributeszssh-keyssshKeys)
get_optionr&   r,   r   safe_redact_key)r   rR   rS   rT   rU   s   `@@@@r   r'   zGCP.scrub_metadata^   s     ??:&]]9-k:
!]]956HI/0	 	 	 	  dmm,T]]95lC'	)T]]95lC&	(r    dict_objkeyc                 ,    ||v r| j                   ||<   yy)z Redact keys N)rJ   )clsrZ   r[   s      r   rY   zGCP.safe_redact_key   s     (?LLHSM r    )__name__
__module____qualname__
short_descplugin_nameprofilesr   option_listr   r3   rJ   r&   r   r   r/   rI   r%   r,   r2   r'   classmethodrY    r    r   r
   r
      s    (JKH 	*e*	+K >L'NHH>8&)d )# # &%(N )t )# ) )r    r
   )r)   typingr   urllibr   urllib.errorr   sos.report.pluginsr   r   r   r
   rf   r    r   <module>rk      s*       ! C Cy)&# y)r    