-
Notifications
You must be signed in to change notification settings - Fork 349
feat: implement updated design for regional access boundary #1955
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
nbayati
wants to merge
8
commits into
googleapis:main
Choose a base branch
from
nbayati:rab-update-feb
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
4d5f3bf
feat: Bring TB up to date with design changes
nbayati b1cb7e1
Update RAB based on the new design
nbayati e482e53
fix minor issues
nbayati 21196fd
Remove manual override and reactie reftesh
nbayati f615aee
Fixing lint issues
nbayati e5388a7
Refactor unit tests
nbayati 2d69358
test: Correct regional access boundary lookup URL path.
nbayati 6458f5a
Fix minor issues
nbayati File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| """Shared constants.""" | ||
|
|
||
| _SERVICE_ACCOUNT_TRUST_BOUNDARY_LOOKUP_ENDPOINT = "https://iamcredentials.{universe_domain}/v1/projects/-/serviceAccounts/{service_account_email}/allowedLocations" | ||
| _WORKFORCE_POOL_TRUST_BOUNDARY_LOOKUP_ENDPOINT = "https://iamcredentials.{universe_domain}/v1/locations/global/workforcePools/{pool_id}/allowedLocations" | ||
| _WORKLOAD_IDENTITY_POOL_TRUST_BOUNDARY_LOOKUP_ENDPOINT = "https://iamcredentials.{universe_domain}/v1/projects/{project_number}/locations/global/workloadIdentityPools/{pool_id}/allowedLocations" | ||
| _SERVICE_ACCOUNT_REGIONAL_ACCESS_BOUNDARY_LOOKUP_ENDPOINT = "https://iamcredentials.googleapis.com/v1/projects/-/serviceAccounts/{service_account_email}/allowedLocations" | ||
| _WORKFORCE_POOL_REGIONAL_ACCESS_BOUNDARY_LOOKUP_ENDPOINT = "https://iamcredentials.googleapis.com/v1/locations/global/workforcePools/{pool_id}/allowedLocations" | ||
| _WORKLOAD_IDENTITY_POOL_REGIONAL_ACCESS_BOUNDARY_LOOKUP_ENDPOINT = "https://iamcredentials.googleapis.com/v1/projects/{project_number}/locations/global/workloadIdentityPools/{pool_id}/allowedLocations" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| """Utilities for Regional Access Boundary management.""" | ||
|
|
||
| import datetime | ||
| import threading | ||
|
|
||
| import logging | ||
|
|
||
| from google.auth import _helpers | ||
|
|
||
| _LOGGER = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| # The default lifetime for a cached Regional Access Boundary. | ||
| DEFAULT_REGIONAL_ACCESS_BOUNDARY_TTL = datetime.timedelta(hours=6) | ||
|
|
||
| # The initial cooldown period for a failed Regional Access Boundary lookup. | ||
| DEFAULT_REGIONAL_ACCESS_BOUNDARY_COOLDOWN = datetime.timedelta(minutes=15) | ||
|
|
||
| # The maximum cooldown period for a failed Regional Access Boundary lookup. | ||
| MAX_REGIONAL_ACCESS_BOUNDARY_COOLDOWN = datetime.timedelta(hours=6) | ||
|
|
||
|
|
||
| class _RegionalAccessBoundaryRefreshThread(threading.Thread): | ||
| """Thread for background refreshing of the Regional Access Boundary.""" | ||
|
|
||
| def __init__(self, credentials, request): | ||
| super(_RegionalAccessBoundaryRefreshThread, self).__init__() | ||
| self.daemon = True | ||
| self._credentials = credentials | ||
| self._request = request | ||
|
|
||
| def run(self): | ||
| """ | ||
| Performs the Regional Access Boundary lookup and updates the credential's state. | ||
|
|
||
| This method is run in a separate thread. It delegates the actual lookup | ||
| to the credentials object's `_lookup_regional_access_boundary` method. | ||
| Based on the lookup's outcome (success or complete failure after retries), | ||
| it updates the credential's cached Regional Access Boundary information, | ||
| its expiry, its cooldown expiry, and its exponential cooldown duration. | ||
| """ | ||
| regional_access_boundary_info = ( | ||
| self._credentials._lookup_regional_access_boundary(self._request) | ||
| ) | ||
|
|
||
| with self._credentials._stale_boundary_lock: # Acquire the lock | ||
| if regional_access_boundary_info: | ||
| # On success, update the boundary and its expiry, and clear any cooldown. | ||
| self._credentials._regional_access_boundary = ( | ||
| regional_access_boundary_info | ||
| ) | ||
| self._credentials._regional_access_boundary_expiry = ( | ||
| _helpers.utcnow() + DEFAULT_REGIONAL_ACCESS_BOUNDARY_TTL | ||
| ) | ||
| self._credentials._regional_access_boundary_cooldown_expiry = None | ||
| # Reset the cooldown duration on success. | ||
| self._credentials._current_rab_cooldown_duration = ( | ||
| DEFAULT_REGIONAL_ACCESS_BOUNDARY_COOLDOWN | ||
| ) | ||
| if _helpers.is_logging_enabled(_LOGGER): | ||
| _LOGGER.debug( | ||
| "Asynchronous Regional Access Boundary lookup successful." | ||
| ) | ||
| else: | ||
| # On complete failure, calculate the next exponential cooldown duration and set the cooldown expiry. | ||
| if _helpers.is_logging_enabled(_LOGGER): | ||
| _LOGGER.warning( | ||
| "Asynchronous Regional Access Boundary lookup failed. Entering cooldown." | ||
| ) | ||
| self._credentials._regional_access_boundary_cooldown_expiry = ( | ||
| _helpers.utcnow() + self._credentials._current_rab_cooldown_duration | ||
| ) | ||
| new_cooldown_duration = ( | ||
| self._credentials._current_rab_cooldown_duration * 2 | ||
| ) | ||
| self._credentials._current_rab_cooldown_duration = min( | ||
| new_cooldown_duration, MAX_REGIONAL_ACCESS_BOUNDARY_COOLDOWN | ||
| ) | ||
| # If the proactive refresh failed, clear any existing expired RAB data. | ||
| # This ensures we don't continue using stale data. | ||
| self._credentials._regional_access_boundary = None | ||
| self._credentials._regional_access_boundary_expiry = None | ||
|
|
||
|
|
||
| class _RegionalAccessBoundaryRefreshManager(object): | ||
| """Manages a thread for background refreshing of the Regional Access Boundary.""" | ||
|
|
||
| def __init__(self): | ||
| self._lock = threading.Lock() | ||
| self._worker = None | ||
|
|
||
| def start_refresh(self, credentials, request): | ||
| """ | ||
| Starts a background thread to refresh the Regional Access Boundary if one is not already running. | ||
|
|
||
| Args: | ||
| credentials (CredentialsWithRegionalAccessBoundary): The credentials | ||
| to refresh. | ||
| request (google.auth.transport.Request): The object used to make | ||
| HTTP requests. | ||
| """ | ||
| with self._lock: | ||
| if self._worker and self._worker.is_alive(): | ||
| # A refresh is already in progress. | ||
| return | ||
|
|
||
| self._worker = _RegionalAccessBoundaryRefreshThread(credentials, request) | ||
| self._worker.start() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.