-
Notifications
You must be signed in to change notification settings - Fork 1.7k
fix(auth): handle PermissionError on workload certificates to avoid startup hang and crash #17568
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
4
commits into
googleapis:main
Choose a base branch
from
nbayati:fix/auth-mtls-permission-denied
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.
+118
−12
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
b007b71
fix(auth): handle PermissionError on workload certificates to avoid s…
nbayati 981a60a
fix(auth): restrict certificate readiness check to regular files
nbayati 5a7ce2c
test(auth): mock os.path.exists locally to avoid test runner side eff…
nbayati 022d2ae
fix(auth): fallback to unbound tokens on certificate permission or re…
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 |
|---|---|---|
|
|
@@ -19,6 +19,7 @@ | |
| import logging | ||
| import os | ||
| import re | ||
| import stat | ||
| import time | ||
| from urllib.parse import quote, urlparse | ||
|
|
||
|
|
@@ -58,8 +59,20 @@ | |
|
|
||
|
|
||
| def _is_certificate_file_ready(path): | ||
| """Checks if a file exists and is not empty.""" | ||
| return path and os.path.exists(path) and os.path.getsize(path) > 0 | ||
| """Checks if a file exists, is a regular file, and is not empty.""" | ||
| if not path: | ||
| return False | ||
| try: | ||
| # Check if the path points to a regular file and is not empty. | ||
| # stat.S_ISREG is used instead of os.path.isfile to avoid swallowing | ||
| # PermissionError exceptions, which the caller needs to propagate. | ||
| st = os.stat(path) | ||
| return stat.S_ISREG(st.st_mode) and st.st_size > 0 | ||
| except PermissionError: | ||
| # Propagate PermissionError to let caller handle it (fail-fast or fallback) | ||
| raise | ||
| except OSError: | ||
| return False | ||
|
|
||
|
|
||
| def get_agent_identity_certificate_path(): | ||
|
|
@@ -148,6 +161,13 @@ def get_agent_identity_certificate_path(): | |
| ) | ||
| has_logged_cert_warning = True | ||
|
|
||
| except PermissionError as e: | ||
| _LOGGER.warning( | ||
| "Permission denied when accessing certificate config or certificate file: %s. " | ||
| "Token binding protection cannot be enabled. Falling back to unbound tokens.", | ||
| e, | ||
| ) | ||
| return None | ||
|
lsirac marked this conversation as resolved.
|
||
| except (IOError, ValueError, KeyError) as e: | ||
| if cert_config_path and os.path.exists(cert_config_path): | ||
| # If the file exists but has invalid JSON or is unreadable, | ||
|
|
@@ -205,8 +225,17 @@ def get_and_parse_agent_identity_certificate(): | |
| if not cert_path: | ||
| return None | ||
|
|
||
| with open(cert_path, "rb") as cert_file: | ||
| cert_bytes = cert_file.read() | ||
| try: | ||
| with open(cert_path, "rb") as cert_file: | ||
| cert_bytes = cert_file.read() | ||
| except OSError as e: | ||
|
Contributor
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. Should we keep the fallback limited to PermissionError? |
||
| _LOGGER.warning( | ||
| "Failed to read agent identity certificate file at %s: %s. " | ||
| "Token binding protection cannot be enabled. Falling back to unbound tokens.", | ||
| cert_path, | ||
| e, | ||
| ) | ||
| return None | ||
|
|
||
| return parse_certificate(cert_bytes) | ||
|
|
||
|
|
||
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
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.