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 (
Sequence
[google.auth.downscoped.AccessBoundaryRule
]) – The list of access boundary rules limiting the access that a downscoped credential will have.- Raises:
TypeError – If any of the rules are not a valid type.
ValueError – 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:
- 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:
TypeError – If any of the rules are not a valid type.
ValueError – 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 (
Sequence
[str
]) – 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 (
Optional
[google.auth.downscoped.AvailabilityCondition
]) – Optional condition that restricts the availability of permissions to specific Cloud Storage objects.
- Raises:
TypeError – If any of the parameters are not of the expected types.
ValueError – 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:
- property available_permissions¶
Returns the current available permissions.
- property availability_condition¶
Returns the current availability condition.
- Returns:
- The current
availability condition.
- Return type:
- 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 (
Optional
[str
]) – An optional short string that identifies the purpose of the condition.description (
Optional
[str
]) – Optional details about the purpose of the condition.
- Raises:
TypeError – If any of the parameters are not of the expected types.
ValueError – 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:
- property title¶
Returns the current title.
- property description¶
Returns the current description.
- 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.
- class Credentials(source_credentials, credential_access_boundary, quota_project_id=None)[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 (
Optional
[str
]) – The optional quota project ID.
- Raises:
google.auth.exceptions.RefreshError – If the source credentials return an error on token refresh.
google.auth.exceptions.OAuthError – If the STS token exchange endpoint returned an error during downscoped token generation.
- 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.
- property quota_project_id¶
Project to use for quota and billing purposes.
- property valid¶
Checks the validity of the credentials.
This is True if the credentials have a
token
and the token is notexpired
.
- expiry¶
When the token expires and is no longer valid. If this is None, the token is assumed to never expire.
- 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.