Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions packages/google-auth/google/auth/_default.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@

from google.auth import environment_vars
from google.auth import exceptions
import google.auth.transport._http_client

if TYPE_CHECKING: # pragma: NO COVER
from google.auth.credentials import Credentials # noqa: F401
Expand Down Expand Up @@ -391,21 +390,22 @@ def _get_gae_credentials():
def _get_gce_credentials(request=None, quota_project_id=None):
"""Gets credentials and project ID from the GCE Metadata Service."""
# Ping requires a transport, but we want application default credentials
# to require no arguments. So, we'll use the _http_client transport which
# uses http.client. This is only acceptable because the metadata server
# doesn't do SSL and never requires proxies.
# to require no arguments.
# MDS connections use mTLS (#1856), which has a hard requirement for requests,
# so we cant use http.client

# While this library is normally bundled with compute_engine, there are
# some cases where it's not available, so we tolerate ImportError.
try:
from google.auth import compute_engine
from google.auth.compute_engine import _metadata
from google.auth.transport.requests import Request
except ImportError:
_LOGGER.warning("Import of Compute Engine auth library failed.")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The ImportError block now handles potential failures from importing google.auth.transport.requests.Request in addition to compute_engine and _metadata. If the underlying requests library is missing, the current warning message "Import of Compute Engine auth library failed." might be misleading. Consider making the warning message more general to accurately reflect that multiple authentication-related libraries could have failed to import, which would aid in debugging.

_LOGGER.warning("Failed to import necessary authentication libraries for GCE.")

return None, None

if request is None:
request = google.auth.transport._http_client.Request()
request = Request()

if _metadata.is_on_gce(request=request):
# Get the project ID.
Expand Down
Loading