Pyjo.GCP.Credentials.Chain - Application Default Credentials

from Pyjo.GCP.Credentials.Chain import Chain

credentials = Chain().credentials()
print(credentials.project)

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

import Pyjo.GCP.Config
import Pyjo.GCP.Credentials.Chain
import Pyjo.GCP.Credentials.File

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

chain = Pyjo.GCP.Credentials.Chain.new(providers=[Pyjo.GCP.Credentials.File.new(path='/etc/gcloud/key.json')])

Pyjo.GCP.Credentials.Chain is the provider of the Application Default Credentials, the credentials of the first of its providers that has them, in the order of AIP-4110 by default:

  1. the file of the GOOGLE_APPLICATION_CREDENTIALS environment variable, of Pyjo.GCP.Credentials.File,

  2. the file of gcloud auth application-default login, of Pyjo.GCP.Credentials.File,

  3. the metadata server of Compute Engine, GKE, Cloud Run and Cloud Functions, of Pyjo.GCP.Credentials.Metadata.

The project of the GOOGLE_CLOUD_PROJECT environment variable, or of GCLOUD_PROJECT, comes over the one of the credentials, as google-auth has it.

A provider that is not configured passes on to the next one, and the error of one that is configured ends the chain, as google-auth does, so that a broken file of GOOGLE_APPLICATION_CREDENTIALS is not hidden by other credentials.

See also

Pyjo.GCP.Credentials.Provider, Pyjo.GCP.Credentials, Pyjo.GCP.Config, https://google.aip.dev/auth/4110.

Classes

class Pyjo.GCP.Credentials.Chain.Pyjo_GCP_Credentials_Chain(*, providers: Sequence[Pyjo_GCP_Credentials_Provider] | None = None, config: Pyjo_GCP_Config | None = None, scopes: Sequence[str] | None = None, **kwargs: Any)[source]

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

__init__(*, providers: Sequence[Pyjo_GCP_Credentials_Provider] | None = None, config: Pyjo_GCP_Config | None = None, scopes: Sequence[str] | None = None, **kwargs: Any) → None[source]
chain = Pyjo.GCP.Credentials.Chain.new()
chain = Pyjo.GCP.Credentials.Chain.new(scopes=['https://www.googleapis.com/auth/devstorage.read_only'])
chain = Pyjo.GCP.Credentials.Chain.new(providers=[Pyjo.GCP.Credentials.File.new(path=path)])

Construct a new Pyjo.GCP.Credentials.Chain object, with the options of Pyjo.GCP.Credentials.Provider, and the providers of the Application Default Credentials, with config and scopes, by default.

config
config = chain.config

The configuration of the default providers and of the project, a new Pyjo.GCP.Config object by default.

default_providers() → list[Pyjo_GCP_Credentials_Provider][source]
providers = chain.default_providers()

New providers of the Application Default Credentials, with config, scopes and the user agent of the chain, in their order, but the metadata server, which has a user agent of its own, with a short timeout.

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

The credentials of the first provider that has them, with the project of the environment, or None.

providers: list[Pyjo_GCP_Credentials_Provider]
providers = chain.providers

The providers, in their order.

scopes
scopes = chain.scopes

The scopes of the token of the default providers that take scopes, https://www.googleapis.com/auth/cloud-platform by default.