google.auth.downscoped module

Downscoping with Credential Access Boundaries

This module provides the ability to downscope credentials using Downscoping with Credential Access Boundaries. This is useful to restrict the Identity and Access Management (IAM) permissions that a short-lived credential can use.

To downscope permissions of a source credential, a Credential Access Boundary that specifies which resources the new credential can access, as well as an upper bound on the permissions that are available on each resource, has to be defined. A downscoped credential can then be instantiated using the source credential and the Credential Access Boundary.

The common pattern of usage is to have a token broker with elevated access generate these downscoped credentials from higher access source credentials and pass the downscoped short-lived access tokens to a token consumer via some secure authenticated channel for limited access to Google Cloud Storage resources.

For example, a token broker can be set up on a server in a private network. Various workloads (token consumers) in the same network will send authenticated requests to that broker for downscoped tokens to access or modify specific google cloud storage buckets.

The broker will instantiate downscoped credentials instances that can be used to generate short lived downscoped access tokens that can be passed to the token consumer. These downscoped access tokens can be injected by the consumer into google.oauth2.Credentials and used to initialize a storage client instance to access Google Cloud Storage resources with restricted access.

Note: Only Cloud Storage supports Credential Access Boundaries. Other Google Cloud services do not support this feature.

class CredentialAccessBoundary(rules=[])[source]

Bases: object

Defines a Credential Access Boundary which contains a list of access boundary rules. Each rule contains information on the resource that the rule applies to, the upper bound of the permissions that are available on that resource and an optional condition to further restrict permissions.

Instantiates a Credential Access Boundary. A Credential Access Boundary can contain up to 10 access boundary rules.

Parameters:

rules (Sequencegoogle.auth.downscoped.AccessBoundaryRule) – The list of access boundary rules limiting the access that a downscoped credential will have.

Raises:
  • InvalidType – If any of the rules are not a valid type.

  • InvalidValue – If the provided rules exceed the maximum allowed.

property rules

Returns the list of access boundary rules defined on the Credential Access Boundary.

Returns:

The list of access

boundary rules defined on the Credential Access Boundary. These are returned as an immutable tuple to prevent modification.

Return type:

Tuplegoogle.auth.downscoped.AccessBoundaryRule, …

add_rule(rule)[source]

Adds a single access boundary rule to the existing rules.

Parameters:

rule (google.auth.downscoped.AccessBoundaryRule) – The access boundary rule, limiting the access that a downscoped credential will have, to be added to the existing rules.

Raises:
  • InvalidType – If any of the rules are not a valid type.

  • InvalidValue – If the provided rules exceed the maximum allowed.

to_json()[source]

Generates the dictionary representation of the Credential Access Boundary. This uses the format expected by the Security Token Service API as documented in Defining a Credential Access Boundary.

Returns:

Credential Access Boundary Rule represented in a dictionary object.

Return type:

Mapping

class AccessBoundaryRule(available_resource, available_permissions, availability_condition=None)[source]

Bases: object

Defines an access boundary rule which contains information on the resource that the rule applies to, the upper bound of the permissions that are available on that resource and an optional condition to further restrict permissions.

Instantiates a single access boundary rule.

Parameters:
  • available_resource (str) – The full resource name of the Cloud Storage bucket that the rule applies to. Use the format “//storage.googleapis.com/projects/_/buckets/bucket-name”.

  • available_permissions (Sequencestr) – A list defining the upper bound that the downscoped token will have on the available permissions for the resource. Each value is the identifier for an IAM predefined role or custom role, with the prefix “inRole:”. For example: “inRole:roles/storage.objectViewer”. Only the permissions in these roles will be available.

  • availability_condition (Optionalgoogle.auth.downscoped.AvailabilityCondition) – Optional condition that restricts the availability of permissions to specific Cloud Storage objects.

Raises:
  • InvalidType – If any of the parameters are not of the expected types.

  • InvalidValue – If any of the parameters are not of the expected values.

property available_resource

Returns the current available resource.

Returns:

The current available resource.

Return type:

str

property available_permissions

Returns the current available permissions.

Returns:

The current available permissions. These are returned

as an immutable tuple to prevent modification.

Return type:

Tuplestr, …

property availability_condition

Returns the current availability condition.

Returns:

The current

availability condition.

Return type:

Optionalgoogle.auth.downscoped.AvailabilityCondition

to_json()[source]

Generates the dictionary representation of the access boundary rule. This uses the format expected by the Security Token Service API as documented in Defining a Credential Access Boundary.

Returns:

The access boundary rule represented in a dictionary object.

Return type:

Mapping

class AvailabilityCondition(expression, title=None, description=None)[source]

Bases: object

An optional condition that can be used as part of a Credential Access Boundary to further restrict permissions.

Instantiates an availability condition using the provided expression and optional title or description.

Parameters:
  • expression (str) – A condition expression that specifies the Cloud Storage objects where permissions are available. For example, this expression makes permissions available for objects whose name starts with “customer-a”: “resource.name.startsWith(‘projects/_/buckets/example-bucket/objects/customer-a’)”

  • title (Optionalstr) – An optional short string that identifies the purpose of the condition.

  • description (Optionalstr) – Optional details about the purpose of the condition.

Raises:
  • InvalidType – If any of the parameters are not of the expected types.

  • InvalidValue – If any of the parameters are not of the expected values.

property expression

Returns the current condition expression.

Returns:

The current conditon expression.

Return type:

str

property title

Returns the current title.

Returns:

The current title.

Return type:

Optionalstr

property description

Returns the current description.

Returns:

The current description.

Return type:

Optionalstr

to_json()[source]

Generates the dictionary representation of the availability condition. This uses the format expected by the Security Token Service API as documented in Defining a Credential Access Boundary.

Returns:

The availability condition represented in a dictionary

object.

Return type:

Mappingstr, str

class Credentials(source_credentials, credential_access_boundary, quota_project_id=None, universe_domain='googleapis.com')[source]

Bases: CredentialsWithQuotaProject

Defines a set of Google credentials that are downscoped from an existing set of Google OAuth2 credentials. This is useful to restrict the Identity and Access Management (IAM) permissions that a short-lived credential can use. The common pattern of usage is to have a token broker with elevated access generate these downscoped credentials from higher access source credentials and pass the downscoped short-lived access tokens to a token consumer via some secure authenticated channel for limited access to Google Cloud Storage resources.

Instantiates a downscoped credentials object using the provided source credentials and credential access boundary rules. To downscope permissions of a source credential, a Credential Access Boundary that specifies which resources the new credential can access, as well as an upper bound on the permissions that are available on each resource, has to be defined. A downscoped credential can then be instantiated using the source credential and the Credential Access Boundary.

Parameters:
  • source_credentials (google.auth.credentials.Credentials) – The source credentials to be downscoped based on the provided Credential Access Boundary rules.

  • credential_access_boundary (google.auth.downscoped.CredentialAccessBoundary) – The Credential Access Boundary which contains a list of access boundary rules. Each rule contains information on the resource that the rule applies to, the upper bound of the permissions that are available on that resource and an optional condition to further restrict permissions.

  • quota_project_id (Optionalstr) – The optional quota project ID.

  • universe_domain (Optionalstr) – The universe domain value, default is googleapis.com

Raises:
apply(headers, token=None)[source]

Apply the token to the authentication header.

Parameters:
  • headers (Mapping) – The HTTP request headers.

  • token (Optionalstr) – If specified, overrides the current access token.

before_request(request, method, url, headers)[source]

Performs credential-specific before request logic.

Refreshes the credentials if necessary, then calls apply() to apply the token to the authentication header.

Parameters:
  • 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.

property expired

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.

property quota_project_id

Project to use for quota and billing purposes.

property token_state

See :obj:`TokenState

property universe_domain

The universe domain value.

property valid

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.

token

The bearer token that can be used in HTTP headers to make authenticated requests.

Type:

str

expiry

When the token expires and is no longer valid. If this is None, the token is assumed to never expire.

Type:

Optionaldatetime

refresh(request)[source]

Refreshes the access token.

Parameters:

request (google.auth.transport.Request) – The object used to make HTTP requests.

Raises:

google.auth.exceptions.RefreshError – If the credentials could not be refreshed.

with_quota_project(quota_project_id)[source]

Returns a copy of these credentials with a modified quota project.

Parameters:

quota_project_id (str) – The project to use for quota and billing purposes

Returns:

A new credentials instance.

Return type:

google.auth.credentials.Credentials