Pyjo.GCP.Credentials.Metadata - Credentials of the metadata server

from Pyjo.GCP.Credentials.Metadata import Metadata

credentials = Metadata().credentials()
print(credentials.account, credentials.project)

# Or import the module, as in Google Python Style Guide

import Pyjo.GCP.Credentials.Metadata

provider = Pyjo.GCP.Credentials.Metadata.new(scopes=['https://www.googleapis.com/auth/devstorage.read_only'])
provider.credentials_p().then(lambda credentials: print(credentials.expiration)).wait()

Pyjo.GCP.Credentials.Metadata is the provider of the credentials of the service account of the metadata server of Compute Engine, GKE, Cloud Run and Cloud Functions, of AIP-4115: its token, with the scopes, its email, the project and the universe domain of the server.

The provider asks the server once whether it is there, as google-auth does: a request to the IP of the server, 169.254.169.254, whose response has Metadata-Flavor: Google, or, without it, on Linux, the product of the machine, Google Compute Engine, of /sys/class/dmi/id/product_name. Without the server it has no credentials, as on a machine that is not of Google Cloud, and with NO_GCE_CHECK=true it does not ask.

The environment variables of google-auth change the server:

  • GCE_METADATA_HOST, or GCE_METADATA_ROOT, the host of the requests, metadata.google.internal by default,

  • GCE_METADATA_IP, the host of the request that finds the server, 169.254.169.254 by default,

  • GCE_METADATA_TIMEOUT, the timeout of a request, 3 seconds by default,

  • GCE_METADATA_DETECT_RETRIES, the attempts of the request that finds the server, 3 by default.

See also

Pyjo.GCP.Credentials.Provider, Pyjo.GCP.Credentials.Chain, https://google.aip.dev/auth/4115, https://cloud.google.com/compute/docs/metadata/overview.

Classes

Pyjo.GCP.Credentials.Metadata.HOST = 'metadata.google.internal'

The host of the metadata server.

Pyjo.GCP.Credentials.Metadata.IP = '169.254.169.254'

The IP of the metadata server, which finds it.

Pyjo.GCP.Credentials.Metadata.PRODUCT_NAME_FILE = '/sys/class/dmi/id/product_name'

The file of the product of a machine of Linux, Google Compute Engine of one of Google Cloud.

class Pyjo.GCP.Credentials.Metadata.Pyjo_GCP_Credentials_Metadata(*, host: str | None = None, ip: str | None = None, timeout: float | None = None, attempts: int | None = None, scopes: Sequence[str] | None = None, service_account: str = 'default', product_name_file: str = '/sys/class/dmi/id/product_name', ua: Pyjo_UserAgent | None = None, **kwargs: Any)[source]

Pyjo.GCP.Credentials.Metadata inherits all attributes and methods from Pyjo.GCP.Credentials.Provider and implements the following new ones.

__init__(*, host: str | None = None, ip: str | None = None, timeout: float | None = None, attempts: int | None = None, scopes: Sequence[str] | None = None, service_account: str = 'default', product_name_file: str = '/sys/class/dmi/id/product_name', ua: Pyjo_UserAgent | None = None, **kwargs: Any) → None[source]
provider = Pyjo.GCP.Credentials.Metadata.new()
provider = Pyjo.GCP.Credentials.Metadata.new(host='127.0.0.1:8080', timeout=1, attempts=1)

Construct a new Pyjo.GCP.Credentials.Metadata object, with the options of Pyjo.GCP.Credentials.Provider, and those of the environment by default.

attempts
attempts = provider.attempts

The number of the attempts of the request that finds the server, of the GCE_METADATA_DETECT_RETRIES environment variable, or 3, and of a request that fails with a status of 5xx or without a connection, at least 1.

async detect() → bool[source]
boolean = await provider.detect()

Whether the server is there, of the response to the request to ip with Metadata-Flavor: Google, or of the product of the machine on Linux.

detected: bool | None
boolean = provider.detected

Whether the server is there, once the provider has asked, or None.

disabled
boolean = provider.disabled

Whether the provider does not ask for the server, of the NO_GCE_CHECK environment variable true.

async fetch() → Pyjo_GCP_Credentials | None[source]
credentials = await provider.fetch()

The token of the service account of the server, with its email, the project and the universe domain of the server, or None without the server.

async get(path: str, query: Mapping[str, str] | None = None) → Pyjo_Transaction_HTTP[source]
tx = await provider.get('project/project-id')

The transaction of a request of a path of /computeMetadata/v1/ of host, with Metadata-Flavor: Google, tried again for a status of 5xx or without a connection, or a Pyjo.Exception of the last connection that fails.

host
host = provider.host

The host of the requests, of the GCE_METADATA_HOST or GCE_METADATA_ROOT environment variable, or metadata.google.internal, with a port too.

ip
ip = provider.ip

The host of the request that finds the server, of the GCE_METADATA_IP environment variable, or 169.254.169.254.

product_name_file
path = provider.product_name_file

The file of the product of the machine on Linux, /sys/class/dmi/id/product_name.

scopes
scopes = provider.scopes

The scopes of the token, or none for the scopes of the service account of the machine.

service_account
name = provider.service_account

The service account of the machine, default by default, or its email.

async text(path: str) → str | None[source]
project = await provider.text('project/project-id')

The text of a path of the server, or None of one that is not there, or a Pyjo.Exception of another status.

timeout
seconds = provider.timeout

The timeout of a request, of the GCE_METADATA_TIMEOUT environment variable, or 3 seconds, of a new ua of the provider, which keeps no connections, as a token comes once an hour.