Pyjo.GCP.Credentials.IdentityPool - Credentials of an identity pool of a file, a URL or a certificate

from Pyjo.GCP.Credentials.IdentityPool import IdentityPool

provider = IdentityPool(
    audience='//iam.googleapis.com/projects/123/locations/global/workloadIdentityPools/pool/providers/oidc',
    subject_token_type='urn:ietf:params:oauth:token-type:jwt',
    file='/var/run/secrets/tokens/gcp-token',
)
print(provider.credentials().token)

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

import Pyjo.GCP.Credentials.IdentityPool

provider = Pyjo.GCP.Credentials.IdentityPool.IdentityPool.from_info(info)
provider.credentials_p().then(lambda credentials: print(credentials.expiration)).wait()

Pyjo.GCP.Credentials.IdentityPool is the provider of the credentials of an external account of Workload Identity Federation or Workforce Identity Federation whose subject token is in a file, as the token of a service account of Kubernetes, or the response of a URL, as the token of the managed identity of Azure, of a file of the type external_account with credential_source of file or url, as gcloud iam workload-identity-pools create-cred-config writes it:

{
  "type": "external_account",
  "audience": "//iam.googleapis.com/projects/123/locations/global/workloadIdentityPools/pool/providers/azure",
  "subject_token_type": "urn:ietf:params:oauth:token-type:jwt",
  "token_url": "https://sts.googleapis.com/v1/token",
  "service_account_impersonation_url": "https://iamcredentials.googleapis.com/v1/projects/-/serviceAccounts/reader@myproject.iam.gserviceaccount.com:generateAccessToken",
  "credential_source": {
    "url": "http://169.254.169.254/metadata/identity/oauth2/token?api-version=2018-02-01&resource=api://pool",
    "headers": {"Metadata": "True"},
    "format": {"type": "json", "subject_token_field_name": "access_token"}
  }
}

The subject token is the whole content of the file or of the response, as text of format, or a field of its JSON object, as json. The file is read again for each token, as a token that the platform rotates.

The subject token of certificate is the certificate of X.509 of the workload, of Workload Identity Federation with mTLS, with the certificates of its trust chain, which the requests of the provider present with the private key of the workload, to https://sts.mtls.googleapis.com/v1/token of the file:

{
  "type": "external_account",
  "audience": "//iam.googleapis.com/projects/123/locations/global/workloadIdentityPools/pool/providers/x509",
  "subject_token_type": "urn:ietf:params:oauth:token-type:mtls",
  "token_url": "https://sts.mtls.googleapis.com/v1/token",
  "credential_source": {
    "certificate": {
      "use_default_certificate_config": true,
      "trust_chain_path": "/etc/workload/chain.pem"
    }
  }
}

The certificate and its key are the files of cert_configs.workload of the configuration of certificate_config_location, or of GOOGLE_API_CERTIFICATE_CONFIG, of CLOUDSDK_CONTEXT_AWARE_CERTIFICATE_CONFIG_FILE_PATH, or of ~/.config/gcloud/certificate_config.json, as gcloud and Cloud Run 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"
    }
  }
}

The token of the certificate of an agent identity is bound to it, of Pyjo.GCP.X509, for the calls of mTLS of Pyjo.GCP.Client with it.

See also

Pyjo.GCP.Credentials.ExternalAccount, Pyjo.GCP.Credentials.File, https://google.aip.dev/auth/4117.

Classes

Pyjo.GCP.Credentials.IdentityPool.MTLS = 'urn:ietf:params:oauth:token-type:mtls'

The type of the subject token of a certificate of X.509, of mTLS.

class Pyjo.GCP.Credentials.IdentityPool.Pyjo_GCP_Credentials_IdentityPool(*, file: str | None = None, url: str | None = None, headers: Mapping[str, str] | None = None, format: Format = 'text', subject_token_field_name: str | None = None, certificate: bool = False, certificate_config_location: str | None = None, trust_chain_path: str | None = None, **kwargs: Any)[source]

Pyjo.GCP.Credentials.IdentityPool inherits all attributes and methods from Pyjo.GCP.Credentials.ExternalAccount and implements the following new ones.

__init__(*, file: str | None = None, url: str | None = None, headers: Mapping[str, str] | None = None, format: Format = 'text', subject_token_field_name: str | None = None, certificate: bool = False, certificate_config_location: str | None = None, trust_chain_path: str | None = None, **kwargs: Any) → None[source]
provider = Pyjo.GCP.Credentials.IdentityPool.new(audience=audience, subject_token_type=token_type,
                                                 file='/var/run/secrets/tokens/gcp-token')
provider = Pyjo.GCP.Credentials.IdentityPool.new(audience=audience, subject_token_type=token_type,
                                                 url=url, headers={'Metadata': 'True'}, format='json',
                                                 subject_token_field_name='access_token')
provider = Pyjo.GCP.Credentials.IdentityPool.new(audience=audience, subject_token_type=MTLS,
                                                 certificate=True, trust_chain_path='/etc/chain.pem')

Construct a new Pyjo.GCP.Credentials.IdentityPool object, of a file, a URL or a certificate of X.509, with the options of Pyjo.GCP.Credentials.ExternalAccount, or a Pyjo.Exception of none or more than one of them, of a format of JSON without a field, or of a certificate without its configuration.

certificate
boolean = provider.certificate

Whether the subject token is the certificate of X.509 of the workload, of the configuration of certificate_config_location, which the requests of the provider present, of mTLS.

certificate_config_location
path = provider.certificate_config_location

The file of the configuration of the certificate, or None, for the one of GOOGLE_API_CERTIFICATE_CONFIG, of CLOUDSDK_CONTEXT_AWARE_CERTIFICATE_CONFIG_FILE_PATH, or ~/.config/gcloud/certificate_config.json.

certificate_paths() → tuple[str, str][source]
cert, key = provider.certificate_paths()

The files of the certificate and of the private key of the workload, of workload_certificate() of the configuration of certificate_config_location, or of certificate_config(), or a Pyjo.Exception without them.

certificate_token() → str[source]
token = provider.certificate_token()

The subject token of the certificate, a JSON array of the certificate and of the ones of trust_chain_path after it, each one in Base64 of DER, as google-auth makes it, or a Pyjo.Exception of a file of the certificate without one, or of a trust chain whose certificate is not first.

exchange_options() → dict[str, str][source]
options = provider.exchange_options()

The options of the exchange, with bindCertFingerprint of the fingerprint of the certificate of an agent identity, of certificate, which binds the token to it, as google-auth has it, unless GOOGLE_API_PREVENT_AGENT_TOKEN_SHARING_FOR_GCP_SERVICES is false.

file
path = provider.file

The file of the subject token, or None.

format: Format
format = provider.format

The format of the subject token, text, the whole content, or json, a field of a JSON object.

headers
headers = provider.headers

The headers of the request of url.

classmethod options_of(info: Mapping[str, Any]) → dict[str, Any][source]
options = IdentityPool.options_of(info)

The options of file or url, headers and format, or of certificate, of credential_source of the JSON object of a file, or a Pyjo.Exception of a source of another kind, as an environment_id of AWS, or of a certificate with both or none of certificate_config_location and use_default_certificate_config.

async subject_token() → str[source]
token = await provider.subject_token()

The subject token of file or url, or of the certificate, or a Pyjo.Exception of a file that is not there, of a response that is not successful, or without the token.

subject_token_field_name
name = provider.subject_token_field_name

The field of the subject token in the JSON object, or None.

trust_chain_path
path = provider.trust_chain_path

The file of the certificates of the trust chain of the certificate, after it in the subject token, or None.

url
url = provider.url

The URL of the subject token, or None.