Skip to main content

Provides simplified credential fetching for Google Cloud Platform.

Project description

Simple GCP Auth

PyPI version License: MIT

A Python package that simplifies the Google Cloud Platform authentication process. It provides easy-to-use functions for fetching user credentials in various environments, abstracting away the complexities of Google's authentication flows.

Overview

Authenticating with Google Cloud can be challenging, especially when your application needs to run in different environments like a local development machine, a CI/CD pipeline, a Docker container, or a cloud-based notebook like Google Colab. simple-gcp-auth provides a set of straightforward helper functions to handle these scenarios gracefully.

Features

  • Interactive User Flow: Authenticate on a local machine through a web browser.
  • Credential Caching: Opt-in to securely cache refresh tokens in your system's native keychain to avoid repeated logins.
  • Manual Code Flow: Authenticate in headless environments (like remote servers or Google Colab) by copying a code from a URL.
  • Application Default Credentials (ADC): Seamlessly use credentials in standard GCP environments (e.g., GCE, GKE, Cloud Functions).
  • Service Account Impersonation:
    • Use existing ADC to impersonate a service account.
    • Use an interactive user flow to get credentials for impersonating a service account with Domain-Wide Delegation.
  • Standard Credentials Object: All functions return a standard google.oauth2.credentials.Credentials object, which is compatible with all official Google Cloud client libraries.

Prerequisites

Before using this package, ensure you have:

  1. A Google Cloud Platform project.
  2. The necessary IAM permissions for your user or service account to access the required resources.

Installation

Install the package from PyPI:

pip install simple-gcp-auth

Usage

Here are the different ways you can use simple-gcp-auth to get credentials.


1. For Local Development: from_interactive_user

Ideal for scripts and applications running on your local machine where a web browser is available.

from simple_gcp_auth import from_interactive_user
from google.cloud import storage

# This will open a browser window for you to log in.
credentials = from_interactive_user(
    scopes=['https://www.googleapis.com/auth/devstorage.read_only'],
    quota_project_id='your-gcp-project-id'
)

# Use the credentials with a GCP client
storage_client = storage.Client(credentials=credentials, project='your-gcp-project-id')
print("Listing buckets:")
for bucket in storage_client.list_buckets():
    print(bucket.name)

Caching Credentials

To avoid authenticating every time you run your script, you can enable credential caching. The refresh token will be stored securely in your system's keychain.

credentials = from_interactive_user(
    scopes=['https://www.googleapis.com/auth/devstorage.read_only'],
    quota_project_id='your-gcp-project-id',
    cache_credentials=True  # Enable caching
)

The next time you run this code, it will try to use the cached token instead of opening the browser.


2. For Headless Environments: from_manual_flow

Perfect for environments without a direct browser interface, such as remote SSH sessions, Docker containers, or Google Colab.

from simple_gcp_auth import from_manual_flow
from google.cloud import bigquery

# This will print a URL. Open it, authenticate, and paste the authorization code back.
credentials = from_manual_flow(
    SCOPES=['https://www.googleapis.com/auth/bigquery.readonly'],
    quota_project_id='your-gcp-project-id'
)

# Use the credentials with a GCP client
bq_client = bigquery.Client(credentials=credentials, project='your-gcp-project-id')
query = "SELECT corpus FROM `bigquery-public-data.samples.shakespeare` LIMIT 10"
for row in bq_client.query(query):
    print(row.corpus)

3. For GCP Environments: from_application_default_credentials

The standard way to authenticate when your code is running within GCP (e.g., Compute Engine, GKE, Cloud Run, App Engine). It automatically finds credentials from the environment.

from simple_gcp_auth import from_application_default_credentials
from google.cloud import pubsub_v1

# ADC will be found automatically from the environment.
credentials = from_application_default_credentials(
    scopes=['https://www.googleapis.com/auth/pubsub'],
    quota_project_id='your-gcp-project-id'
)

# Use the credentials with a GCP client
publisher = pubsub_v1.PublisherClient(credentials=credentials)
# ... use the client

4. For Service Account Impersonation (using ADC): from_adc_impersonated

Use this when your code, running with some base credentials (ADC), needs to assume the identity of another service account or user. The principal running the code must have the "Service Account Token Creator" role.

from simple_gcp_auth import from_adc_impersonated

target_sa = 'my-target-service-account@your-gcp-project-id.iam.gserviceaccount.com'

# Get credentials for the target service account
impersonated_credentials = from_adc_impersonated(
    username=target_sa,
    scopes=['https://www.googleapis.com/auth/cloud-platform']
)

# Now you can use impersonated_credentials to act as the target service account

5. For Domain-Wide Delegation (Interactive): from_interactive_user_delegated

A more advanced flow for when a user needs to interactively authenticate to then impersonate a service account or another user that has been granted domain-wide delegation.

Prerequisites:

  • The service account must be configured for Domain-Wide Delegation in the Google Workspace Admin console.
  • The authenticating user must have the "Service Account Token Creator" role on the service account.
from simple_gcp_auth import from_interactive_user_delegated

delegated_credentials = from_interactive_user_delegated(
    service_account_email='dwd-service-account@your-project.iam.gserviceaccount.com',
    subject_email='user-to-impersonate@your-domain.com',
    scopes=['https://www.googleapis.com/auth/admin.directory.user.readonly'],
    quota_project_id='your-gcp-project-id',
    cache_credentials=True  # Caching is also supported here
)

# Use these credentials to call APIs on behalf of the subject_email

Contributing

Contributions are welcome! Please feel free to submit a pull request or open an issue on the GitHub repository.

License

This project is licensed under the MIT License - see the LICENSE file for details.

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

simple_gcp_auth-0.2.0.tar.gz (10.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

simple_gcp_auth-0.2.0-py3-none-any.whl (8.9 kB view details)

Uploaded Python 3

File details

Details for the file simple_gcp_auth-0.2.0.tar.gz.

File metadata

  • Download URL: simple_gcp_auth-0.2.0.tar.gz
  • Upload date:
  • Size: 10.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for simple_gcp_auth-0.2.0.tar.gz
Algorithm Hash digest
SHA256 7ba6a9df46a5541843f9d4c2379262d15db08b7c6e8a1c8ece552303d9bef713
MD5 4269d48060721f2fd1ae220de145fe61
BLAKE2b-256 b65d2050804357ba0da965a4375317cc66b5cfe2213c58cbd8a276cbf4f0d5d3

See more details on using hashes here.

Provenance

The following attestation bundles were made for simple_gcp_auth-0.2.0.tar.gz:

Publisher: python-publish.yml on shuvalov/simple-gcp-auth

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file simple_gcp_auth-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for simple_gcp_auth-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 197d6ef4a1941b43f5b227328479c0a1d67b982954ee3973a45901a663fed48c
MD5 3553fdb9b90a6202ad3d4b783e93f9b8
BLAKE2b-256 28e75b8c955526ec382f11aa6130943e8b274c4b149cbc9b9b681278d11d05e0

See more details on using hashes here.

Provenance

The following attestation bundles were made for simple_gcp_auth-0.2.0-py3-none-any.whl:

Publisher: python-publish.yml on shuvalov/simple-gcp-auth

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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