Skip to content

Commit 8ffb4d3

Browse files
author
chenyumic
authored
fix: replace environment variable GCE_METADATA_ROOT with GCE_METADATA_HOST (#433)
…_HOST * keeps consistent naming across auth packages of all languages. The package will now check GCE_METADATA_HOST (the new name) first; if not present, it falls back to GCE_METADATA_ROOT (the old name), then the default value. closes [#339](#339).
1 parent 9d5a9a9 commit 8ffb4d3

File tree

3 files changed

+38
-5
lines changed

3 files changed

+38
-5
lines changed

google/auth/compute_engine/_metadata.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,16 @@
3232

3333
_LOGGER = logging.getLogger(__name__)
3434

35-
_METADATA_ROOT = "http://{}/computeMetadata/v1/".format(
36-
os.getenv(environment_vars.GCE_METADATA_ROOT, "metadata.google.internal")
37-
)
35+
# Environment variable GCE_METADATA_HOST is originally named
36+
# GCE_METADATA_ROOT. For compatiblity reasons, here it checks
37+
# the new variable first; if not set, the system falls back
38+
# to the old variable.
39+
_GCE_METADATA_HOST = os.getenv(environment_vars.GCE_METADATA_HOST, None)
40+
if not _GCE_METADATA_HOST:
41+
_GCE_METADATA_HOST = os.getenv(
42+
environment_vars.GCE_METADATA_ROOT, "metadata.google.internal"
43+
)
44+
_METADATA_ROOT = "http://{}/computeMetadata/v1/".format(_GCE_METADATA_HOST)
3845

3946
# This is used to ping the metadata server, it avoids the cost of a DNS
4047
# lookup.

google/auth/environment_vars.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,15 @@
4040

4141
# These two variables allow for customization of the addresses used when
4242
# contacting the GCE metadata service.
43+
GCE_METADATA_HOST = "GCE_METADATA_HOST"
4344
GCE_METADATA_ROOT = "GCE_METADATA_ROOT"
4445
"""Environment variable providing an alternate hostname or host:port to be
45-
used for GCE metadata requests."""
46+
used for GCE metadata requests.
47+
48+
This environment variable is originally named GCE_METADATA_ROOT. System will
49+
check the new variable first; should there be no value present,
50+
the system falls back to the old variable.
51+
"""
4652

4753
GCE_METADATA_IP = "GCE_METADATA_IP"
4854
"""Environment variable providing an alternate ip:port to be used for ip-only

tests/compute_engine/test__metadata.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,27 @@ def test_get_success_text():
155155
assert result == data
156156

157157

158-
def test_get_success_custom_root():
158+
def test_get_success_custom_root_new_variable():
159+
request = make_request("{}", headers={"content-type": "application/json"})
160+
161+
fake_root = "another.metadata.service"
162+
os.environ[environment_vars.GCE_METADATA_HOST] = fake_root
163+
reload_module(_metadata)
164+
165+
try:
166+
_metadata.get(request, PATH)
167+
finally:
168+
del os.environ[environment_vars.GCE_METADATA_HOST]
169+
reload_module(_metadata)
170+
171+
request.assert_called_once_with(
172+
method="GET",
173+
url="http://{}/computeMetadata/v1/{}".format(fake_root, PATH),
174+
headers=_metadata._METADATA_HEADERS,
175+
)
176+
177+
178+
def test_get_success_custom_root_old_variable():
159179
request = make_request("{}", headers={"content-type": "application/json"})
160180

161181
fake_root = "another.metadata.service"

0 commit comments

Comments
 (0)