Skip to content

Commit

Permalink
fix: replace environment variable GCE_METADATA_ROOT with GCE_METADATA…
Browse files Browse the repository at this point in the history
…_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).
  • Loading branch information
chenyumic authored Jun 4, 2020
1 parent 9d5a9a9 commit 8ffb4d3
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 5 deletions.
13 changes: 10 additions & 3 deletions google/auth/compute_engine/_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,16 @@

_LOGGER = logging.getLogger(__name__)

_METADATA_ROOT = "http://{}/computeMetadata/v1/".format(
os.getenv(environment_vars.GCE_METADATA_ROOT, "metadata.google.internal")
)
# Environment variable GCE_METADATA_HOST is originally named
# GCE_METADATA_ROOT. For compatiblity reasons, here it checks
# the new variable first; if not set, the system falls back
# to the old variable.
_GCE_METADATA_HOST = os.getenv(environment_vars.GCE_METADATA_HOST, None)
if not _GCE_METADATA_HOST:
_GCE_METADATA_HOST = os.getenv(
environment_vars.GCE_METADATA_ROOT, "metadata.google.internal"
)
_METADATA_ROOT = "http://{}/computeMetadata/v1/".format(_GCE_METADATA_HOST)

# This is used to ping the metadata server, it avoids the cost of a DNS
# lookup.
Expand Down
8 changes: 7 additions & 1 deletion google/auth/environment_vars.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,15 @@

# These two variables allow for customization of the addresses used when
# contacting the GCE metadata service.
GCE_METADATA_HOST = "GCE_METADATA_HOST"
GCE_METADATA_ROOT = "GCE_METADATA_ROOT"
"""Environment variable providing an alternate hostname or host:port to be
used for GCE metadata requests."""
used for GCE metadata requests.
This environment variable is originally named GCE_METADATA_ROOT. System will
check the new variable first; should there be no value present,
the system falls back to the old variable.
"""

GCE_METADATA_IP = "GCE_METADATA_IP"
"""Environment variable providing an alternate ip:port to be used for ip-only
Expand Down
22 changes: 21 additions & 1 deletion tests/compute_engine/test__metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,27 @@ def test_get_success_text():
assert result == data


def test_get_success_custom_root():
def test_get_success_custom_root_new_variable():
request = make_request("{}", headers={"content-type": "application/json"})

fake_root = "another.metadata.service"
os.environ[environment_vars.GCE_METADATA_HOST] = fake_root
reload_module(_metadata)

try:
_metadata.get(request, PATH)
finally:
del os.environ[environment_vars.GCE_METADATA_HOST]
reload_module(_metadata)

request.assert_called_once_with(
method="GET",
url="http://{}/computeMetadata/v1/{}".format(fake_root, PATH),
headers=_metadata._METADATA_HEADERS,
)


def test_get_success_custom_root_old_variable():
request = make_request("{}", headers={"content-type": "application/json"})

fake_root = "another.metadata.service"
Expand Down

0 comments on commit 8ffb4d3

Please # to comment.