Ë
    i©¹dô  ã                   óh   — 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e
e«      Zy)	é    N)ÚHTTPResponse)Ú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e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Ú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                 óX   — | j                  d«      }|d   dk7  ry| 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)Úexec_cmdÚ
GOOGLE_DMI)Úselfr   s     ú8/usr/lib/python3/dist-packages/sos/report/plugins/gcp.pyÚcheck_enabledzGCP.check_enabled(   s4   € ð —‘˜gÓ&ˆØ�‰?˜aÒØØ�‰ %¨¡/Ð1Ð1ó    c                 óT   — | j                  ddg¬«       | j                  ddg¬«       y)z²
        Collect the following info:
         * Metadata from the Metadata server
         * `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.setup3   s1   € ð 	×ÑÐ.°e°WÐÔ=ð 	×Ñ˜y°¨wÐÕ7r   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.collectA   s”   € à×!Ñ! /¸¸Ð!Ó@ð 	&ÀEð&Ø $× 1Ñ 1Ó 3�”Ø×#Ñ#Ô%Ø—‘œDŸJ™J t§}¡}¸QÔ?Ô@÷		&ð 	&øô
  ò &Ø—‘œC ›H×%Ñ%ûð&ú÷	&ð 	&ús/   •B%—AA5Á5	B"Á>BÂB%ÂB"Â"B%Â%B.Úreturnc                 óž   — | j                  | j                  «      }|j                  «       j                  «       }t	        j
                  |«      S )zq
        Retrieves metadata from the Metadata Server and transforms it into a
        dictionary object.
        )Ú_query_addressÚMETADATA_QUERYÚreadÚdecoder*   Úloads)r   ÚresponseÚresponse_bodys      r   r&   zGCP.get_metadataK   s=   € ð
 ×&Ñ& t×':Ñ':Ó;ˆØ Ÿ™›×.Ñ.Ó0ˆÜ�z‰z˜-Ó(Ð(r   Úurlc                 óB  — 	 t        j                  | ddi¬«      }t        j                  |«      }|j                  dk7  r8t	        d|j                  › d�|j                  «       j                  «       z   «      ‚|S # 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)Úheadersz,Failed to communicate with Metadata Server: NéÈ   z2Failed to communicate with Metadata Server (code: z): )	r   ÚRequestÚurlopenr   r,   r-   Úcoder5   r6   )r:   Úreqr8   r/   s       r   r3   zGCP._query_addressT   s¬   € ð	KÜ—/‘/ #Ð0AÀ8Ð/LÔMˆCÜ—‘ sÓ+ˆHð �=‰=˜CÒÜðØ"Ÿ-™-˜¨ð-Ø/7¯}©}«×/EÑ/EÓ/GñHóIð Ið ˆøô ò 	KÜØ>ÄÀSÃÑIóKð Kûð	Kús   ‚.A9 Á9	BÂBÂB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Údatar1   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)	rG   ÚkÚvÚvalueÚ
project_idÚproject_numberÚproject_number_intÚscrubr   s	       €€€€€r   rW   z!GCP.scrub_metadata.<locals>.scrubv   sÊ   ø€ Ü˜$¤Ô%Ø˜d‘?ð %)§M¡M�D˜‘MØ7;·z±z³|×D©t¨q°!™˜a›¡%¨£(Ñ*ÓDÐDÜ˜D¤$Ô'Ø26Ö7¨™˜e�Ò7Ð7Ü˜D¤#Ô&Ø—|‘| N°D·M±MÓBß#™G J°·±Ó>ð?ä˜D¤#Ô&Ø(,Ð0BÒ(B�t—}‘}ÐLÈÐLØˆKùó Eùâ7s   ¸C&Á)C,Ú
attributeszssh-keysÚsshKeys)Ú
get_optionr'   r-   r   Úsafe_redact_key)r   rT   rU   rV   rW   s   `@@@@r   r(   zGCP.scrub_metadataf   s³   ü€ ð �?‰?˜:Ô&Øà—]‘] 9Ñ-¨kÑ:ˆ
Ø!Ÿ]™]¨9Ñ5Ð6HÑIÐÜÐ/Ó0ˆð	œð 	¤÷ 	ñ 	ñ  ˜dŸm™mÓ,ˆŒà×Ñ˜TŸ]™]¨9Ñ5°lÑCØ'ô	)à×Ñ˜TŸ]™]¨9Ñ5°lÑCØ&õ	(r   Údict_objÚkeyc                 ó,   — ||v r| j                   ||<   y y )N)rL   )Úclsr\   r]   s      r   r[   zGCP.safe_redact_key�   s   € à�(‰?ØŸL™LˆH�SŠMð r   N)Ú__name__Ú
__module__Ú__qualname__Ú
short_descÚplugin_nameÚprofilesr	   Úoption_listÚMETADATA_ROOTr4   rL   r   r   r!   r0   rK   r&   Ústaticmethodr-   r   r3   r(   Úclassmethodr[   © r   r   r   r      s«   „ à(€JØ€KØ€Hñ 	�* eð*ô	+ð€Kð J€Mð'€Nà€Hð/€Jò	2ò8ò&ð)˜dó )ð ð˜Cð  Lò ó ðò"%(ðN ð) tð )°#ò )ó ñ)r   r   )r*   Úhttp.clientr   Útypingr   Úurllibr   Úurllib.errorr   Úsos.report.pluginsr   r   r	   r   rj   r   r   ú<module>rp      s-   ðó Ý $Ý Ý Ý !ç CÑ Cô)ˆ&Ð#õ )r   