ó
ùR‹_c           @   s–  d  Z  d d l Z d d l Z d d l Z d d l Z 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 e j e ƒ Z e j e j d ƒ Z e sÔ e j e j d ƒ Z n  d	 j e ƒ Z d
 j e j e j d ƒ ƒ Z d Z d Z i e e 6Z y e e j d d ƒ ƒ Z Wn e  k
 rOd Z n Xe d d „ Z! e e" d d „ Z# d „  Z$ d d „ Z% d d „ Z& d S(   s‘   Provides helper methods for talking to the Compute Engine metadata server.

See https://cloud.google.com/compute/docs/metadata for more details.
iÿÿÿÿN(   t   http_client(   t   parse(   t   _helpers(   t   environment_vars(   t
   exceptionss   metadata.google.internals   http://{}/computeMetadata/v1/s	   http://{}s   169.254.169.254s   metadata-flavort   Googlet   GCE_METADATA_TIMEOUTi   c      	   C   s­   d } x  | | k  r¨ yS |  d t  d d d t d | ƒ } | j j t ƒ } | j t j k of | t k SWq	 t	 j
 k
 r¤ } t j d | d | | ƒ | d 7} q	 Xq	 Wt S(	   s½  Checks to see if the metadata server is available.

    Args:
        request (google.auth.transport.Request): A callable used to make
            HTTP requests.
        timeout (int): How long to wait for the metadata server to respond.
        retry_count (int): How many times to attempt connecting to metadata
            server using above timeout.

    Returns:
        bool: True if the metadata server is reachable, False otherwise.
    i    t   urlt   methodt   GETt   headerst   timeoutsI   Compute Engine Metadata server unavailable onattempt %s of %s. Reason: %si   (   t   _METADATA_IP_ROOTt   _METADATA_HEADERSR
   t   gett   _METADATA_FLAVOR_HEADERt   statusR    t   OKt   _METADATA_FLAVOR_VALUER   t   TransportErrort   _LOGGERt   warningt   False(   t   requestR   t   retry_countt   retriest   responset   metadata_flavort   e(    (    s]   /var/www/syncserver/local/lib/python2.7/site-packages/google/auth/compute_engine/_metadata.pyt   ping?   s&    	i   c         C   s†  t  j | | ƒ } i  } | r+ d | d <n  t j | | ƒ } d } x… | | k  r² y  |  d | d d d t ƒ }	 PWqF t j k
 r® }
 t j d | d	 | |
 ƒ | d	 7} qF XqF Wt j d
 j	 | ƒ ƒ ‚ |	 j
 t j k r[t j |	 j ƒ } |	 j d d k rTy t j | ƒ SWqXt k
 rP} t j d j	 | ƒ ƒ } t j | | ƒ qXXq‚| Sn' t j d j	 | |	 j
 |	 j ƒ |	 ƒ ‚ d S(   s³  Fetch a resource from the metadata server.

    Args:
        request (google.auth.transport.Request): A callable used to make
            HTTP requests.
        path (str): The resource to retrieve. For example,
            ``'instance/service-accounts/default'``.
        root (str): The full path to the metadata server root.
        recursive (bool): Whether to do a recursive query of metadata. See
            https://cloud.google.com/compute/docs/metadata#aggcontents for more
            details.
        retry_count (int): How many times to attempt connecting to metadata
            server using above timeout.

    Returns:
        Union[Mapping, str]: If the metadata server returns JSON, a mapping of
            the decoded JSON is return. Otherwise, the response content is
            returned as a string.

    Raises:
        google.auth.exceptions.TransportError: if an error occurred while
            retrieving metadata.
    t   truet	   recursivei    R   R   R	   R
   sI   Compute Engine Metadata server unavailable onattempt %s of %s. Reason: %si   sp   Failed to retrieve {} from the Google Compute Enginemetadata service. Compute Engine Metadata server unavailables   content-types   application/jsonsL   Received invalid JSON from the Google Compute Enginemetadata service: {:.20}s]   Failed to retrieve {} from the Google Compute Enginemetadata service. Status: {} Response:
{}N(   t   urlparset   urljoinR   t   update_queryR   R   R   R   R   t   formatR   R    R   t
   from_bytest   dataR
   t   jsont   loadst
   ValueErrort   sixt
   raise_from(   R   t   patht   rootR   R   t   base_urlt   query_paramsR   R   R   R   t   contentt
   caught_exct   new_exc(    (    s]   /var/www/syncserver/local/lib/python2.7/site-packages/google/auth/compute_engine/_metadata.pyR   o   sF    c         C   s   t  |  d ƒ S(   sO  Get the Google Cloud Project ID from the metadata server.

    Args:
        request (google.auth.transport.Request): A callable used to make
            HTTP requests.

    Returns:
        str: The project ID

    Raises:
        google.auth.exceptions.TransportError: if an error occurred while
            retrieving metadata.
    s   project/project-id(   R   (   R   (    (    s]   /var/www/syncserver/local/lib/python2.7/site-packages/google/auth/compute_engine/_metadata.pyt   get_project_id»   s    t   defaultc         C   s   t  |  d j | ƒ d t ƒS(   sÉ  Get information about a service account from the metadata server.

    Args:
        request (google.auth.transport.Request): A callable used to make
            HTTP requests.
        service_account (str): The string 'default' or a service account email
            address. The determines which service account for which to acquire
            information.

    Returns:
        Mapping: The service account's information, for example::

            {
                'email': '...',
                'scopes': ['scope', ...],
                'aliases': ['default', '...']
            }

    Raises:
        google.auth.exceptions.TransportError: if an error occurred while
            retrieving metadata.
    s   instance/service-accounts/{0}/R   (   R   R#   t   True(   R   t   service_account(    (    s]   /var/www/syncserver/local/lib/python2.7/site-packages/google/auth/compute_engine/_metadata.pyt   get_service_account_infoÌ   s    c         C   sF   t  |  d j | ƒ ƒ } t j ƒ  t j d | d ƒ } | d | f S(   s-  Get the OAuth 2.0 access token for a service account.

    Args:
        request (google.auth.transport.Request): A callable used to make
            HTTP requests.
        service_account (str): The string 'default' or a service account email
            address. The determines which service account for which to acquire
            an access token.

    Returns:
        Union[str, datetime]: The access token and its expiration.

    Raises:
        google.auth.exceptions.TransportError: if an error occurred while
            retrieving metadata.
    s#   instance/service-accounts/{0}/tokent   secondst
   expires_int   access_token(   R   R#   R   t   utcnowt   datetimet	   timedelta(   R   R5   t
   token_jsont   token_expiry(    (    s]   /var/www/syncserver/local/lib/python2.7/site-packages/google/auth/compute_engine/_metadata.pyt   get_service_account_tokenê   s
    ('   t   __doc__R;   R&   t   loggingt   osR)   t	   six.movesR    t   six.moves.urllibR   R    t   google.authR   R   R   t	   getLoggert   __name__R   t   getenvt   GCE_METADATA_HOSTt   Nonet   _GCE_METADATA_HOSTt   GCE_METADATA_ROOTR#   t   _METADATA_ROOTt   GCE_METADATA_IPR   R   R   R   t   intt   _METADATA_DEFAULT_TIMEOUTR(   R   R   R   R2   R6   R?   (    (    (    s]   /var/www/syncserver/local/lib/python2.7/site-packages/google/auth/compute_engine/_metadata.pyt   <module>   s<   
0L	