Google Cloud Impersonated credentials.
This module provides authentication for applications where local credentials impersonates a remote service account using IAM Credentials API.
This class can be used to impersonate a service account as long as the original Credential object has the “Service Account Token Creator” role on the target service account.
Bases: google.auth.credentials.Scoped
, google.auth.credentials.CredentialsWithQuotaProject
, google.auth.credentials.Signing
This module defines impersonated credentials which are essentially impersonated identities.
Impersonated Credentials allows credentials issued to a user or service account to impersonate another. The target service account must grant the originating credential principal the Service Account Token Creator IAM role:
For more information about Token Creator IAM role and IAMCredentials API, see Creating Short-Lived Service Account Credentials.
Usage:
First grant source_credentials the Service Account Token Creator role on the target account to impersonate. In this example, the service account represented by svc_account.json has the token creator role on impersonated-account@_project_.iam.gserviceaccount.com.
Enable the IAMCredentials API on the source project: gcloud services enable iamcredentials.googleapis.com.
Initialize a source credential which does not have access to list bucket:
from google.oauth2 import service_account target_scopes = [ 'https://www.googleapis.com/auth/devstorage.read_only'] source_credentials = ( service_account.Credentials.from_service_account_file( '/path/to/svc_account.json', scopes=target_scopes))
Now use the source credentials to acquire credentials to impersonate another service account:
from google.auth import impersonated_credentials target_credentials = impersonated_credentials.Credentials( source_credentials=source_credentials, target_principal='impersonated-account@_project_.iam.gserviceaccount.com', target_scopes = target_scopes, lifetime=500)
Resource access is granted:
client = storage.Client(credentials=target_credentials) buckets = client.list_buckets(project='your_project') for bucket in buckets: print(bucket.name)
source_credentials (google.auth.Credentials) – The source credential used as to acquire the impersonated credentials.
target_principal (str) – The service account to impersonate.
target_scopes (Sequence
str
) – Scopes to request during the authorization grant.
delegates (Sequence
str
) – The chained list of delegates required to grant the final access_token. If set, the sequence of identities must have “Service Account Token Creator” capability granted to the prceeding identity. For example, if set to [serviceAccountB, serviceAccountC], the source_credential must have the Token Creator role on serviceAccountB. serviceAccountB must have the Token Creator on serviceAccountC. Finally, C must have Token Creator on target_principal. If left unset, source_credential must have that role on target_principal.
lifetime (int) – Number of seconds the delegated credential should be valid for (upto 3600).
quota_project_id (Optional
str
) – The project ID used for quota and billing. This project may be different from the project used to create the credentials.
iam_endpoint_override (Optional
str
) – The full IAM endpoint override with the target_principal embedded. This is useful when supporting impersonation with regional endpoints.
subject (Optional
str
) – sub field of a JWT. This field should only be set if you wish to impersonate as a user. This feature is useful when using domain wide delegation.
When the token expires and is no longer valid. If this is None, the token is assumed to never expire.
Refreshes the access token.
request (google.auth.transport.Request) – The object used to make HTTP requests.
google.auth.exceptions.RefreshError – If the credentials could not be refreshed.
Signs the given message.
The signer used to sign bytes.
True if these credentials require scopes to obtain an access token.
The credential information JSON.
The credential information will be added to auth related error messages by client library.
Returns a copy of these credentials with a modified quota project.
quota_project_id (str) – The project to use for quota and billing purposes
A new credentials instance.
Create a copy of these credentials with the specified scopes.
scopes (Sequence
str
) – The list of scopes to attach to the current credentials.
NotImplementedError – If the credentials’ scopes can not be changed. This can be avoided by checking requires_scopes
before calling this method.
Apply the token to the authentication header.
Performs credential-specific before request logic.
Refreshes the credentials if necessary, then calls apply()
to apply the token to the authentication header.
request (google.auth.transport.Request) – The object used to make HTTP requests.
method (str) – The request’s HTTP method or the RPC method being invoked.
url (str) – The request’s URI or the RPC service’s URI.
headers (Mapping) – The request’s headers.
Checks if the credentials are expired.
Note that credentials can be invalid but not expired because Credentials with expiry
set to None is considered to never expire.
Deprecated since version v2.24.0: Prefer checking token_state
instead.
Checks if the credentials have the given scopes.
Project to use for quota and billing purposes.
See :obj:`TokenState
The universe domain value.
Checks the validity of the credentials.
This is True if the credentials have a token
and the token is not expired
.
Deprecated since version v2.24.0: Prefer checking token_state
instead.
Bases: google.auth.credentials.CredentialsWithQuotaProject
Open ID Connect ID Token-based service account credentials.
Returns a copy of these credentials with a modified quota project.
quota_project_id (str) – The project to use for quota and billing purposes
A new credentials instance.
Refreshes the access token.
request (google.auth.transport.Request) – The object used to make HTTP requests.
google.auth.exceptions.RefreshError – If the credentials could not be refreshed.
Apply the token to the authentication header.
Performs credential-specific before request logic.
Refreshes the credentials if necessary, then calls apply()
to apply the token to the authentication header.
request (google.auth.transport.Request) – The object used to make HTTP requests.
method (str) – The request’s HTTP method or the RPC method being invoked.
url (str) – The request’s URI or the RPC service’s URI.
headers (Mapping) – The request’s headers.
Checks if the credentials are expired.
Note that credentials can be invalid but not expired because Credentials with expiry
set to None is considered to never expire.
Deprecated since version v2.24.0: Prefer checking token_state
instead.
The credential information JSON.
The credential information will be added to auth related error messages by client library.
Project to use for quota and billing purposes.
See :obj:`TokenState
The universe domain value.
Checks the validity of the credentials.
This is True if the credentials have a token
and the token is not expired
.
Deprecated since version v2.24.0: Prefer checking token_state
instead.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4