GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — feat: honor `NO_GCE_CHECK` environment variable (#1610) · googleapis/google-auth-library-python@383c982

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

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • google/auth
    • compute_engine
      • _metadata.py
    • environment_vars.py
  • tests/compute_engine
    • test__metadata.py
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎google/auth/compute_engine/_metadata.py‎

Copy file name to clipboardExpand all lines: google/auth/compute_engine/_metadata.py
+7Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ def _get_metadata_ip_root(use_mtls: bool):
101101
except ValueError: # pragma: NO COVER
102102
_METADATA_DEFAULT_TIMEOUT = 3
103103

104+
# This is used to disable checking for the GCE metadata server and directly
105+
# assuming it's not available.
106+
_NO_GCE_CHECK = os.getenv(environment_vars.NO_GCE_CHECK) == "true"
107+
104108
# Detect GCE Residency
105109
_GOOGLE = "Google"
106110
_GCE_PRODUCT_NAME_FILE = "/sys/class/dmi/id/product_name"
@@ -116,6 +120,9 @@ def is_on_gce(request):
116120
Returns:
117121
bool: True if the code runs on Google Compute Engine, False otherwise.
118122
"""
123+
if _NO_GCE_CHECK:
124+
return False
125+
119126
if ping(request):
120127
return True
121128

Collapse file

‎google/auth/environment_vars.py‎

Copy file name to clipboardExpand all lines: google/auth/environment_vars.py
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@
6060
"""Environment variable providing an alternate ip:port to be used for ip-only
6161
GCE metadata requests."""
6262

63+
NO_GCE_CHECK = "NO_GCE_CHECK"
64+
"""Environment variable controlling whether to check if running on GCE or not.
65+
66+
The default value is false. Users have to explicitly set this value to true
67+
in order to disable the GCE check."""
68+
6369
GCE_METADATA_MTLS_MODE = "GCE_METADATA_MTLS_MODE"
6470
"""Environment variable controlling the mTLS behavior for GCE metadata requests.
6571
Collapse file

‎tests/compute_engine/test__metadata.py‎

Copy file name to clipboardExpand all lines: tests/compute_engine/test__metadata.py
+14Lines 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 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.