Skip to main content

No project description provided

Project description

GCP Impersonation wrapper

The gcp-impersonation-wrapper is a small util used to impersonate service accounts using service account. We pass the main credentials, list service accounts we can impersonate and then actually impersonate like so:

#!/usr/bin/env python3
from src.service_account import ServiceAccount


if __name__ == "__main__":
    # Insert credentials here
    credentials = None

    service_account = ServiceAccount(
        project_id="your-project-id",
        impersonator_credentials=credentials
    )

    # Can be used in a with statement
    with service_account.impersonate(
        service_account_email="your-service-account-email",
        target_scopes=["https://www.googleapis.com/auth/cloud-platform"],
        service="iam",
        version="v1"
    ) as impersonated_service_account:
        # do something with the impersonated service account
        service_accounts_the_impersonated_sacc_can_see = impersonated_service_account.service.projects(
        ).serviceAccounts().list().execute()
        print(service_accounts_the_impersonated_sacc_can_see)

    # Or as a variable
    impersonated_service_account = service_account.impersonate(
        service_account_email="your-service-account-email",
        target_scopes=["https://www.googleapis.com/auth/cloud-platform"],
        service="iam",
        version="v1"
    )
    service_accounts_the_impersonated_sacc_can_see = impersonated_service_account.service.projects(
    ).serviceAccounts().list().execute()

To list the impersonatable service accounts we do:

#!/usr/bin/env python3
from src.service_account import ServiceAccount


if __name__ == "__main__":
    # Insert credentials here
    credentials = None

    service_account = ServiceAccount(
        project_id="your-project-id",
        impersonator_credentials=credentials
    )

    # This returns a list of email like ["john@doe.com", "jane@doe.com", ....]
    service_account_emails_we_can_impersonate = service_account.list_all_impersonatable_service_account_emails() 

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

gcp-impersonation-wrapper-0.0.6.tar.gz (2.8 kB view hashes)

Uploaded Source

Built Distribution

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page