-
Notifications
You must be signed in to change notification settings - Fork 611
Swarming: Patches Auth default credentials at startup #5245
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
IvanBM18
wants to merge
3
commits into
master
Choose a base branch
from
fix/swarming/patches_auth_at_startup
base: master
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 2 commits
Commits
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 |
|---|---|---|
|
|
@@ -44,6 +44,50 @@ def _patch_appengine_modules_for_bots(): | |
| pass | ||
|
|
||
|
|
||
| def _patch_google_auth_for_bots() -> None: | ||
| """Patch google.auth.default to use explicit credentials via GCE metadata. | ||
|
|
||
| This is required for Swarming bots to authenticate with GCP services | ||
| since they do not always have application default credentials configured | ||
| in their environment. | ||
| """ | ||
| if not os.getenv('SWARMING_BOT'): | ||
| # Only applicable when explicitly deployed as a Swarming task worker. | ||
| return | ||
|
|
||
| try: | ||
| import google.auth | ||
| from google.auth import compute_engine | ||
| import requests | ||
|
|
||
| def patched_default_credentials( | ||
| *args, **kwargs) -> tuple[compute_engine.Credentials, str] | None: | ||
| # pylint: disable=unused-argument | ||
| url = f"http://{os.environ['GCE_METADATA_HOST']}/computeMetadata/v1/" \ | ||
| "project/project-id" | ||
| project_id = os.environ.get('GOOGLE_CLOUD_PROJECT') or os.environ.get( | ||
| 'APPLICATION_ID') | ||
| if not project_id and os.environ.get('GCE_METADATA_HOST'): | ||
| try: | ||
| project_id = requests.get( | ||
| url, headers={ | ||
| "Metadata-Flavor": "Google" | ||
| }, timeout=2).text.strip() | ||
| except Exception: | ||
| pass | ||
|
|
||
| if not project_id: | ||
| print('''[Swarming] [Error] Failed to patch google.auth.default. | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will be logged into swarming only, not GCP, since when this executes we still haven't setup GCP logging |
||
| Reason: failed to get project ID''') | ||
| return None | ||
|
|
||
| return compute_engine.Credentials(), project_id | ||
|
|
||
| google.auth.default = patched_default_credentials | ||
| except ImportError as e: | ||
| print(f'[Swarming] [Error] Failed to patch google.auth.default. {e.msg}') | ||
|
|
||
|
|
||
| def fix_module_search_paths(): | ||
| """Add directories that we must be able to import from to path.""" | ||
| root_directory = os.environ['ROOT_DIR'] | ||
|
|
@@ -75,3 +119,4 @@ def fix_module_search_paths(): | |
|
|
||
| # TODO(ochang): Remove this once SDK is removed from images. | ||
| _patch_appengine_modules_for_bots() | ||
| _patch_google_auth_for_bots() | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note: We have the args and kwargs in the method signature because otherwise if we call the default() method with any args an exception would be thrown because the patch didn't matched the expected signature.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changing the function signature to this should fix it without having this pylint directive.