Downscoping restricts the IAM permissions of a short-lived credential using a Credential Access Boundary. This is primarily used to implement the Principle of Least Privilege, especially when passing tokens to consumers.
Note: Currently, only Cloud Storage supports Credential Access Boundaries. Other Google Cloud services do not support this feature.
Workflow
- Token Broker: Defines
AvailabilityCondition and AccessBoundaryRule to create a CredentialAccessBoundary. It then generates downscoped credentials from a source credential. - Token Consumer: Receives the downscoped token and expiry, then uses them to initialize a client (e.g.,
google.cloud.storage.Client).
import google.auth
from google.auth import downscoped
from google.auth.transport import requests
# 1. Define the boundary
available_resource = '//storage.googleapis.com/projects/_/buckets/bucket-123'
available_permissions = ['inRole:roles/storage.objectViewer']
availability_expression = "resource.name.startsWith('projects/_/buckets/bucket-123/objects/customer-a')"
availability_condition = downscoped.AvailabilityCondition(availability_expression)
rule = downscoped.AccessBoundaryRule(
available_resource=available_resource,
available_permissions=available_permissions,
availability_condition=availability_condition
)
credential_access_boundary = downscoped.CredentialAccessBoundary(rules=[rule])
# 2. Create downscoped credentials from source
source_credentials, _ = google.auth.default()
downscoped_credentials = downscoped.Credentials(
source_credentials=source_credentials,
credential_access_boundary=credential_access_boundary
)
# 3. Refresh to get the token
downscoped_credentials.refresh(requests.Request())
# Values to pass to consumer
access_token = downscoped_credentials.token
expiry = downscoped_credentials.expiry