Pyjo.GCP.Credentials.Impersonated - Credentials of an impersonated service account

from Pyjo.GCP.Credentials.Chain import Chain
from Pyjo.GCP.Credentials.Impersonated import Impersonated

provider = Impersonated(source=Chain(), target_principal='reader@myproject.iam.gserviceaccount.com',
                        scopes=['https://www.googleapis.com/auth/devstorage.read_only'])
print(provider.credentials().account)

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

import Pyjo.GCP.Credentials.Chain
import Pyjo.GCP.Credentials.Impersonated

provider = Pyjo.GCP.Credentials.Impersonated.new(
    source=Pyjo.GCP.Credentials.Chain.new(),
    target_principal='reader@myproject.iam.gserviceaccount.com',
    delegates=['projects/-/serviceAccounts/broker@myproject.iam.gserviceaccount.com'],
    lifetime=600,
)
provider.credentials_p().then(lambda credentials: print(credentials.expiration)).wait()

Pyjo.GCP.Credentials.Impersonated is the provider of the credentials of a service account that the credentials of another provider, its source, impersonate, with the role Service Account Token Creator on it: the access token of generateAccessToken of the IAM Service Account Credentials API, as gcloud --impersonate-service-account gets it.

A file of the type impersonated_service_account, as gcloud auth application-default login --impersonate-service-account writes it, has the credentials of the source, of the type authorized_user, service_account or external_account_authorized_user, which Pyjo.GCP.Credentials.File makes the provider of:

{
  "type": "impersonated_service_account",
  "service_account_impersonation_url": "https://iamcredentials.googleapis.com/v1/projects/-/serviceAccounts/reader@myproject.iam.gserviceaccount.com:generateAccessToken",
  "delegates": [],
  "source_credentials": {
    "type": "authorized_user",
    "client_id": "764086051850-....apps.googleusercontent.com",
    "client_secret": "...",
    "refresh_token": "1//0g..."
  }
}

The token of the source needs a scope of IAM, as https://www.googleapis.com/auth/cloud-platform, the default one of Pyjo.GCP.Credentials.Chain, or https://www.googleapis.com/auth/iam, of the key of a service account of a file. The endpoint of the API is the one of the universe domain of the source, as google-auth has it, or the emulator of IAMCREDENTIALS_EMULATOR_HOST, which the provider calls without a token, as Pyjo.GCP.Client calls an emulator, so that a program tested against emulators does not call Google. The token of the source comes of its own provider, as the token endpoint of a key of a service account, which no emulator replaces.

With a subject, the token is the one of the domain-wide delegation of Google Workspace: the JWT of the grant of RFC 7523 that signJwt of the API signs with the key of the service account that Google keeps.

See also

Pyjo.GCP.Credentials.Provider, Pyjo.GCP.Credentials.File, Pyjo.GCP.Client, https://cloud.google.com/iam/docs/service-account-impersonation, https://cloud.google.com/iam/docs/reference/credentials/rest.

Classes

Pyjo.GCP.Credentials.Impersonated.IAM_SCOPE = 'https://www.googleapis.com/auth/iam'

The scope of IAM, of the token of the key of a service account of a file that is the source.

Pyjo.GCP.Credentials.Impersonated.LIFETIME = 3600

The number of seconds that a token is valid, an hour, the most of the API without an organization policy.

class Pyjo.GCP.Credentials.Impersonated.Pyjo_GCP_Credentials_Impersonated(*, source: Pyjo_GCP_Credentials_Provider, target_principal: str, scopes: Sequence[str] | None = None, delegates: Sequence[str] | None = None, lifetime: int | None = 3600, subject: str | None = None, quota_project: str | None = None, endpoint: str | None = None, url: str | None = None, token_uri: str = 'https://oauth2.googleapis.com/token', **kwargs: Any)[source]

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

__init__(*, source: Pyjo_GCP_Credentials_Provider, target_principal: str, scopes: Sequence[str] | None = None, delegates: Sequence[str] | None = None, lifetime: int | None = 3600, subject: str | None = None, quota_project: str | None = None, endpoint: str | None = None, url: str | None = None, token_uri: str = 'https://oauth2.googleapis.com/token', **kwargs: Any) → None[source]
provider = Pyjo.GCP.Credentials.Impersonated.new(source=source, target_principal=email)
provider = Pyjo.GCP.Credentials.Impersonated.new(source=source, target_principal=email,
                                                 subject='admin@example.com')

Construct a new Pyjo.GCP.Credentials.Impersonated object, with the options of Pyjo.GCP.Credentials.Provider.

delegates
delegates = provider.delegates

The service accounts of a chain of delegation, as projects/-/serviceAccounts/broker@myproject.iam.gserviceaccount.com, each with the role Service Account Token Creator on the next one, and the last one on target_principal, or none, as the source has it on target_principal itself.

endpoint
url = provider.endpoint

The URL of the endpoint of the IAM Service Account Credentials API, which comes over the emulator of IAMCREDENTIALS_EMULATOR_HOST and url, or None, for https://iamcredentials.<universe domain> of the universe domain of the source.

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

The access token of the service account, with the credentials of the source, or of the grant of the JWT of a subject.

classmethod from_info(info: Mapping[str, Any], *, source: Pyjo_GCP_Credentials_Provider, **kwargs: Any) → Self[source]
provider = Impersonated.from_info(info, source=source)
provider = Impersonated.from_info(info, source=source, scopes=scopes, ua=ua)

A new provider of the JSON object of a file of the type impersonated_service_account, with the provider of its source_credentials, with the service account of service_account_impersonation_url, delegates, scopes and quota_project_id of the file, and the arguments over them, or a Pyjo.Exception of a file without a service account in the URL. The endpoint of the URL is not the one of the provider, as google-auth has it.

lifetime
seconds = provider.lifetime

The number of seconds that the token is valid, 3600 by default.

quota_project
project = provider.quota_project

The quota project of the credentials, or None.

scopes
scopes = provider.scopes

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

source
provider = provider.source

The provider of the credentials that impersonate the service account.

subject
email = provider.subject

The email of a user that the service account acts as, of the domain-wide delegation of Google Workspace, or None.

target_principal
email = provider.target_principal

The email of the service account that is impersonated.

token_uri
url = provider.token_uri

The token endpoint of OAuth 2.0 of the grant of a subject, https://oauth2.googleapis.com/token.

url
url = provider.url

The URL of generateAccessToken of the service account, as service_account_impersonation_url of the credentials of an external account has it, without an endpoint and an emulator of IAMCREDENTIALS_EMULATOR_HOST, or None.