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

Commit 754a401

Browse files
Merge branch 'main' into add_cryptography_dependency
2 parents 3898071 + 35670fc commit 754a401

4 files changed

Lines changed: 42 additions & 4 deletions

File tree

google/auth/compute_engine/_metadata.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,19 @@ def _get_metadata_ip_root(use_mtls: bool):
9797
# Timeout in seconds to wait for the GCE metadata server when detecting the
9898
# GCE environment.
9999
try:
100-
_METADATA_DEFAULT_TIMEOUT = int(os.getenv("GCE_METADATA_TIMEOUT", 3))
100+
_METADATA_DEFAULT_TIMEOUT = int(os.getenv(environment_vars.GCE_METADATA_TIMEOUT, 3))
101101
except ValueError: # pragma: NO COVER
102102
_METADATA_DEFAULT_TIMEOUT = 3
103103

104+
# The number of tries to perform when waiting for the GCE metadata server
105+
# when detecting the GCE environment.
106+
try:
107+
_METADATA_DETECT_RETRIES = int(
108+
os.getenv(environment_vars.GCE_METADATA_DETECT_RETRIES, 3)
109+
)
110+
except ValueError: # pragma: NO COVER
111+
_METADATA_DETECT_RETRIES = 3
112+
104113
# This is used to disable checking for the GCE metadata server and directly
105114
# assuming it's not available.
106115
_NO_GCE_CHECK = os.getenv(environment_vars.NO_GCE_CHECK) == "true"
@@ -177,7 +186,9 @@ def _prepare_request_for_mds(request, use_mtls=False) -> None:
177186
request.session.mount(f"https://{host}/", adapter)
178187

179188

180-
def ping(request, timeout=_METADATA_DEFAULT_TIMEOUT, retry_count=3):
189+
def ping(
190+
request, timeout=_METADATA_DEFAULT_TIMEOUT, retry_count=_METADATA_DETECT_RETRIES
191+
):
181192
"""Checks to see if the metadata server is available.
182193
183194
Args:

google/auth/environment_vars.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,16 @@
6060
"""Environment variable providing an alternate ip:port to be used for ip-only
6161
GCE metadata requests."""
6262

63+
GCE_METADATA_TIMEOUT = "GCE_METADATA_TIMEOUT"
64+
"""Environment variable defining the timeout in seconds to wait for the
65+
GCE metadata server when detecting the GCE environment.
66+
"""
67+
68+
GCE_METADATA_DETECT_RETRIES = "GCE_METADATA_DETECT_RETRIES"
69+
"""Environment variable representing the number of retries that should be
70+
attempted on metadata lookup.
71+
"""
72+
6373
NO_GCE_CHECK = "NO_GCE_CHECK"
6474
"""Environment variable controlling whether to check if running on GCE or not.
6575

samples/cloud-client/snippets/requirements.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
google-cloud-compute==1.40.0
1+
google-cloud-compute==1.41.0
22
google-cloud-storage==3.7.0
3-
google-auth==2.45.0
3+
google-auth==2.47.0
44
pytest===8.4.2; python_version == '3.9'
55
pytest==9.0.2; python_version > '3.9'
66
boto3>=1.26.0

tests/compute_engine/test__metadata.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,23 @@ def test_ping_success_custom_root(mock_metrics_header_value):
201201
)
202202

203203

204+
@mock.patch("google.auth.metrics.mds_ping", return_value=MDS_PING_METRICS_HEADER_VALUE)
205+
def test_ping_failure_custom_retry(mock_metrics_header_value):
206+
request = make_request("")
207+
request.side_effect = exceptions.TransportError()
208+
209+
os.environ[environment_vars.GCE_METADATA_DETECT_RETRIES] = "10"
210+
importlib.reload(_metadata)
211+
212+
try:
213+
_metadata.ping(request)
214+
finally:
215+
del os.environ[environment_vars.GCE_METADATA_DETECT_RETRIES]
216+
importlib.reload(_metadata)
217+
218+
assert request.call_count == 10
219+
220+
204221
def test_get_success_json():
205222
key, value = "foo", "bar"
206223

0 commit comments

Comments
 (0)