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 loginin 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.Configinherits all attributes and methods fromPyjo.Baseand 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.Configobject.
- __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 fileactive_configof the directory, whichgcloud config configurations activatewrites, ordefault.
- adc_file() str[source]¶
path = config.adc_file()
The path of the file of the credentials of
gcloud auth application-default logininconfig_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_CONFIGorCLOUDSDK_CONTEXT_AWARE_CERTIFICATE_CONFIG_FILE_PATHenvironment variable, or~/.config/gcloud/certificate_config.json, orNoneof 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(), withuse_client_certificate(), orNone.
- 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_CONFIGenvironment variable,~/.config/gcloud, or%APPDATA%\gcloudon Windows.
- configuration¶
name = config.configuration config.configuration = 'dev'
The name of the configuration of gcloud, defaults to the value of the
CLOUDSDK_ACTIVE_CONFIG_NAMEenvironment variable, orNonefor the active one of the directory, seeactive_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_CREDENTIALSenvironment variable, orNone.
- 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.Exceptionof 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_HOSTenvironment variable, asSTORAGE_EMULATOR_HOST, withhttp://before a host without a scheme, aslocalhost:4443, orNone.
- 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 ofis_mtls(), ashttps://storage.mtls.googleapis.com, ofgoogleapis.comonly, or the one of the service inuniverse_domain(), ashttps://storage.googleapis.com, or aPyjo.Exceptionof 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 incore, asproject: the value of theCLOUDSDK_<SECTION>_<NAME>environment variable, asCLOUDSDK_CORE_PROJECT, or of the property of a configuration,active_configuration()by default, orNone.
- is_mtls() bool[source]¶
boolean = config.is_mtls()
Whether the endpoints are the ones of mTLS, of
use_mtls_endpoint()always, orautowith aclient_certificate().
- project() str | None[source]¶
project = config.project()
The project of the environment, of the value of the
GOOGLE_CLOUD_PROJECTenvironment variable, or ofGCLOUD_PROJECT, orNone, 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 emptydict.
- quota_project() str | None[source]¶
project = config.quota_project()
The quota project of the environment, of the value of the
GOOGLE_CLOUD_QUOTA_PROJECTenvironment variable, orNone, 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_DOMAINenvironment variable, orgoogleapis.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_CERTIFICATEorCLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATEenvironment variabletrue, or, without them, of the workload of the configuration ofGOOGLE_API_CERTIFICATE_CONFIGorCLOUDSDK_CONTEXT_AWARE_CERTIFICATE_CONFIG_FILE_PATH, ascheck_use_client_certof 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_ENDPOINTenvironment variable,autoby default, for a client with a certificate,alwaysornever, or aPyjo.Exceptionof 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.workloadof a configuration of the certificate, with the well-known files of Cloud Run for its paths of the configurations of gcloud before, orNoneof a configuration that does not exist, or aPyjo.Exceptionof an invalid one.
- Pyjo.GCP.Config.UNIVERSE_DOMAIN = 'googleapis.com'¶
The universe domain of Google Cloud, of the endpoints of its APIs.