Pyjo.GCP.Config - Configuration of gcloud and the environment of Google Cloud

from Pyjo.GCP.Config import Config

config = Config()
print(config.config_dir, config.active_configuration(), config.get('core/project'))

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

import Pyjo.GCP.Config

config = Pyjo.GCP.Config.new(configuration='dev')
print(config.project() or config.get('core/project'))
print(config.endpoint_url('storage'))

Pyjo.GCP.Config reads what the client libraries of Google and the gcloud CLI read of the environment and of the configuration directory of gcloud, as the Application Default Credentials find them: the file of the credentials of GOOGLE_APPLICATION_CREDENTIALS, the one of gcloud auth application-default login, the properties of the configurations of gcloud, the project, the quota project, the universe domain, the endpoint of a service, and the certificate of the client of mTLS.

The configuration directory is the one of the CLOUDSDK_CONFIG environment variable, or ~/.config/gcloud, or %APPDATA%\gcloud on Windows. A configuration of gcloud is the file configurations/config_<name> of the directory, whose sections have the properties, as project of [core]:

[core]
project = myproject
account = me@example.com

[auth]
impersonate_service_account = reader@myproject.iam.gserviceaccount.com

The files are read when a method needs them, once, and a file that does not exist has no properties.

The certificate of the client is the one of the workload of the configuration of the certificate, of GOOGLE_API_CERTIFICATE_CONFIG, as gcloud, Cloud Run and GKE write it:

{
  "cert_configs": {
    "workload": {
      "cert_path": "/var/run/secrets/workload-spiffe-credentials/certificates.pem",
      "key_path": "/var/run/secrets/workload-spiffe-credentials/private_key.pem"
    }
  }
}

which the client presents with GOOGLE_API_USE_CLIENT_CERTIFICATE=true, or without it when GOOGLE_API_CERTIFICATE_CONFIG has one, to the endpoint of mTLS of a service, as https://storage.mtls.googleapis.com, of GOOGLE_API_USE_MTLS_ENDPOINT, auto, always or never, as the client libraries of Google have them.

See also

Pyjo.GCP, https://google.aip.dev/auth/4110, https://google.aip.dev/auth/4113, https://cloud.google.com/sdk/docs/configurations.

Classes

Pyjo.GCP.Config.ADC_FILE = 'application_default_credentials.json'

The name of the file of the credentials of gcloud auth application-default login in the configuration directory.

Pyjo.GCP.Config.CERTIFICATE_CONFIG = '~/.config/gcloud/certificate_config.json'

The file of the configuration of the certificate of the workload, as gcloud writes it, by default.

Pyjo.GCP.Config.CLOUD_RUN_CERT = '/var/run/secrets/workload-spiffe-credentials/certificates.pem'

The certificate of the workload of Cloud Run.

Pyjo.GCP.Config.CLOUD_RUN_KEY = '/var/run/secrets/workload-spiffe-credentials/private_key.pem'

The private key of the workload of Cloud Run.

class Pyjo.GCP.Config.Pyjo_GCP_Config(*, config_dir: str | None = None, configuration: str | None = None, credentials_file: str | None = None)[source]

Pyjo.GCP.Config inherits all attributes and methods from Pyjo.Base and implements the following new ones.

__init__(*, config_dir: str | None = None, configuration: str | None = None, credentials_file: str | None = None) → None[source]
config = Pyjo.GCP.Config.new()
config = Pyjo.GCP.Config.new(config_dir='/etc/gcloud', configuration='dev')

Construct a new Pyjo.GCP.Config object.

__weakref__

list of weak references to the object

active_configuration() → str[source]
name = config.active_configuration()

The name of the configuration of gcloud, configuration, the one of the file active_config of the directory, which gcloud config configurations activate writes, or default.

adc_file() → str[source]
path = config.adc_file()

The path of the file of the credentials of gcloud auth application-default login in config_dir, which may not exist.

certificate_config() → str | None[source]
path = config.certificate_config()

The file of the configuration of the certificate of the workload, of the GOOGLE_API_CERTIFICATE_CONFIG or CLOUDSDK_CONTEXT_AWARE_CERTIFICATE_CONFIG_FILE_PATH environment variable, or ~/.config/gcloud/certificate_config.json, or None of one that does not exist.

client_certificate() → tuple[str, str] | None[source]
cert, key = config.client_certificate() or (None, None)

The files of the certificate and of the private key of the client of mTLS, of workload_certificate(), with use_client_certificate(), or None.

config_dir
path = config.config_dir
config.config_dir = '/etc/gcloud'

The path of the configuration directory of gcloud, defaults to the value of the CLOUDSDK_CONFIG environment variable, ~/.config/gcloud, or %APPDATA%\gcloud on Windows.

configuration
name = config.configuration
config.configuration = 'dev'

The name of the configuration of gcloud, defaults to the value of the CLOUDSDK_ACTIVE_CONFIG_NAME environment variable, or None for the active one of the directory, see active_configuration().

credentials_file
path = config.credentials_file
config.credentials_file = '/etc/gcloud/key.json'

The path of the file of the credentials, defaults to the value of the GOOGLE_APPLICATION_CREDENTIALS environment variable, or None.

credentials_info(path: str) → dict[str, Any][source]
info = config.credentials_info(config.adc_file())

The JSON object of a file of credentials, which raises a Pyjo.Exception of a file that does not exist, is not JSON, or is not an object.

emulator_host(service: str) → str | None[source]
url = config.emulator_host('storage')

The URL of the emulator of a service, of the value of the <SERVICE>_EMULATOR_HOST environment variable, as STORAGE_EMULATOR_HOST, with http:// before a host without a scheme, as localhost:4443, or None.

endpoint_url(service: str) → str[source]
url = config.endpoint_url('storage')

The URL of the endpoint of a service, of emulator_host(), the one of mTLS of is_mtls(), as https://storage.mtls.googleapis.com, of googleapis.com only, or the one of the service in universe_domain(), as https://storage.googleapis.com, or a Pyjo.Exception of mTLS in another universe domain.

get(name: str, configuration: str | None = None) → str | None[source]
project = config.get('core/project')
account = config.get('account', 'dev')

The value of a property of gcloud, of its section and name, as core/project, or of its name in core, as project: the value of the CLOUDSDK_<SECTION>_<NAME> environment variable, as CLOUDSDK_CORE_PROJECT, or of the property of a configuration, active_configuration() by default, or None.

is_mtls() → bool[source]
boolean = config.is_mtls()

Whether the endpoints are the ones of mTLS, of use_mtls_endpoint() always, or auto with a client_certificate().

project() → str | None[source]
project = config.project()

The project of the environment, of the value of the GOOGLE_CLOUD_PROJECT environment variable, or of GCLOUD_PROJECT, or None, which comes over the project of the credentials.

properties(configuration: str | None = None) → dict[str, dict[str, str]][source]
properties = config.properties()
properties = config.properties('dev')

The sections of the properties of a configuration of gcloud, active_configuration() by default, without the environment, or an empty dict.

quota_project() → str | None[source]
project = config.quota_project()

The quota project of the environment, of the value of the GOOGLE_CLOUD_QUOTA_PROJECT environment variable, or None, which comes over the one of the credentials.

universe_domain() → str[source]
domain = config.universe_domain()

The universe domain of the endpoints, of the value of the GOOGLE_CLOUD_UNIVERSE_DOMAIN environment variable, or googleapis.com.

use_client_certificate() → bool[source]
boolean = config.use_client_certificate()

Whether the client presents a certificate of mTLS, of the GOOGLE_API_USE_CLIENT_CERTIFICATE or CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE environment variable true, or, without them, of the workload of the configuration of GOOGLE_API_CERTIFICATE_CONFIG or CLOUDSDK_CONTEXT_AWARE_CERTIFICATE_CONFIG_FILE_PATH, as check_use_client_cert of google-auth has it.

use_mtls_endpoint() → MtlsEndpoint[source]
mode = config.use_mtls_endpoint()

Whether the endpoints are the ones of mTLS, of the GOOGLE_API_USE_MTLS_ENDPOINT environment variable, auto by default, for a client with a certificate, always or never, or a Pyjo.Exception of another value.

workload_certificate(location: str) → tuple[str, str] | None[source]
cert, key = config.workload_certificate(config.certificate_config())

The files of the certificate and of the private key of cert_configs.workload of a configuration of the certificate, with the well-known files of Cloud Run for its paths of the configurations of gcloud before, or None of a configuration that does not exist, or a Pyjo.Exception of an invalid one.

Pyjo.GCP.Config.UNIVERSE_DOMAIN = 'googleapis.com'

The universe domain of Google Cloud, of the endpoints of its APIs.