Skip to content
This repository was archived by the owner on Mar 6, 2026. It is now read-only.

Commit 6f9bfa2

Browse files
authored
Merge branch 'main' into gcp_detect_retries
2 parents 83bec07 + bfc07e1 commit 6f9bfa2

4 files changed

Lines changed: 33 additions & 6 deletions

File tree

google/auth/_default.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -330,31 +330,31 @@ def _get_explicit_environ_credentials(quota_project_id=None):
330330
from google.auth import _cloud_sdk
331331

332332
cloud_sdk_adc_path = _cloud_sdk.get_application_default_credentials_path()
333-
explicit_file = os.environ.get(environment_vars.CREDENTIALS)
333+
explicit_file = os.environ.get(environment_vars.CREDENTIALS, "")
334334

335335
_LOGGER.debug(
336-
"Checking %s for explicit credentials as part of auth process...", explicit_file
336+
"Checking '%s' for explicit credentials as part of auth process...",
337+
explicit_file,
337338
)
338339

339-
if explicit_file is not None and explicit_file == cloud_sdk_adc_path:
340+
if explicit_file != "" and explicit_file == cloud_sdk_adc_path:
340341
# Cloud sdk flow calls gcloud to fetch project id, so if the explicit
341342
# file path is cloud sdk credentials path, then we should fall back
342343
# to cloud sdk flow, otherwise project id cannot be obtained.
343344
_LOGGER.debug(
344-
"Explicit credentials path %s is the same as Cloud SDK credentials path, fall back to Cloud SDK credentials flow...",
345+
"Explicit credentials path '%s' is the same as Cloud SDK credentials path, fall back to Cloud SDK credentials flow...",
345346
explicit_file,
346347
)
347348
return _get_gcloud_sdk_credentials(quota_project_id=quota_project_id)
348349

349-
if explicit_file is not None:
350+
if explicit_file != "":
350351
with warnings.catch_warnings():
351352
warnings.simplefilter("ignore", DeprecationWarning)
352353
credentials, project_id = load_credentials_from_file(
353354
os.environ[environment_vars.CREDENTIALS],
354355
quota_project_id=quota_project_id,
355356
)
356357
credentials._cred_file_path = f"{explicit_file} file via the GOOGLE_APPLICATION_CREDENTIALS environment variable"
357-
358358
return credentials, project_id
359359

360360
else:

google/auth/compute_engine/_metadata.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,10 @@ def _get_metadata_ip_root(use_mtls: bool):
110110
except ValueError: # pragma: NO COVER
111111
_METADATA_DETECT_RETRIES = 3
112112

113+
# This is used to disable checking for the GCE metadata server and directly
114+
# assuming it's not available.
115+
_NO_GCE_CHECK = os.getenv(environment_vars.NO_GCE_CHECK) == "true"
116+
113117
# Detect GCE Residency
114118
_GOOGLE = "Google"
115119
_GCE_PRODUCT_NAME_FILE = "/sys/class/dmi/id/product_name"
@@ -125,6 +129,9 @@ def is_on_gce(request):
125129
Returns:
126130
bool: True if the code runs on Google Compute Engine, False otherwise.
127131
"""
132+
if _NO_GCE_CHECK:
133+
return False
134+
128135
if ping(request):
129136
return True
130137

google/auth/environment_vars.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,12 @@
7070
attempted on metadata lookup.
7171
"""
7272

73+
NO_GCE_CHECK = "NO_GCE_CHECK"
74+
"""Environment variable controlling whether to check if running on GCE or not.
75+
76+
The default value is false. Users have to explicitly set this value to true
77+
in order to disable the GCE check."""
78+
7379
GCE_METADATA_MTLS_MODE = "GCE_METADATA_MTLS_MODE"
7480
"""Environment variable controlling the mTLS behavior for GCE metadata requests.
7581

tests/compute_engine/test__metadata.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,20 @@ def test_is_on_gce_ping_success():
108108
assert _metadata.is_on_gce(request)
109109

110110

111+
def test_is_on_gce_no_gce_check():
112+
request = make_request("", headers=_metadata._METADATA_HEADERS)
113+
114+
os.environ[environment_vars.NO_GCE_CHECK] = "true"
115+
importlib.reload(_metadata)
116+
117+
try:
118+
assert not _metadata.is_on_gce(request)
119+
assert request.call_count == 0
120+
finally:
121+
del os.environ[environment_vars.NO_GCE_CHECK]
122+
importlib.reload(_metadata)
123+
124+
111125
@mock.patch("os.name", new="nt")
112126
def test_is_on_gce_windows_success():
113127
request = make_request("", headers={_metadata._METADATA_FLAVOR_HEADER: "meep"})

0 commit comments

Comments
 (0)