aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCraig Citro <craigcitro@gmail.com>2017-03-27 09:47:44 -0700
committerJon Wayne Parrott <jonwayne@google.com>2017-03-27 09:47:44 -0700
commitfeec15f070903069347b9386a24fb73148f97411 (patch)
treef90c1a0a8aeb25024c247763bd55969a45c3560d
parenta3cf56b659e067dea8ead933f81e4a6f42d30ed8 (diff)
downloadoauth2client-feec15f070903069347b9386a24fb73148f97411.tar.gz
Allow customizing the GCE metadata service address via an env var. (#704)
The goal here is to make it possible for a user of a binary that depends on this library (eg the google cloud SDK) to be able to customize where it looks for the GCE metadata service. (An adventurous user can already customize the GCE metadata service location via the existing global vars in this library.) The only bit of awkwardness here is really the test: since this is a top-level statement, reloading is the only way to ensure it works.
-rw-r--r--oauth2client/client.py2
-rw-r--r--oauth2client/contrib/_metadata.py4
-rw-r--r--tests/contrib/test_gce.py18
3 files changed, 22 insertions, 2 deletions
diff --git a/oauth2client/client.py b/oauth2client/client.py
index de2a314..27d62eb 100644
--- a/oauth2client/client.py
+++ b/oauth2client/client.py
@@ -108,7 +108,7 @@ except ValueError: # pragma: NO COVER
GCE_METADATA_TIMEOUT = 3
_SERVER_SOFTWARE = 'SERVER_SOFTWARE'
-_GCE_METADATA_URI = 'http://169.254.169.254'
+_GCE_METADATA_URI = 'http://' + os.getenv('GCE_METADATA_IP', '169.254.169.254')
_METADATA_FLAVOR_HEADER = 'metadata-flavor' # lowercase header
_DESIRED_METADATA_FLAVOR = 'Google'
_GCE_HEADERS = {_METADATA_FLAVOR_HEADER: _DESIRED_METADATA_FLAVOR}
diff --git a/oauth2client/contrib/_metadata.py b/oauth2client/contrib/_metadata.py
index 1dd3542..564cd39 100644
--- a/oauth2client/contrib/_metadata.py
+++ b/oauth2client/contrib/_metadata.py
@@ -19,6 +19,7 @@ See https://cloud.google.com/compute/docs/metadata
import datetime
import json
+import os
from six.moves import http_client
from six.moves.urllib import parse as urlparse
@@ -28,7 +29,8 @@ from oauth2client import client
from oauth2client import transport
-METADATA_ROOT = 'http://metadata.google.internal/computeMetadata/v1/'
+METADATA_ROOT = 'http://{}/computeMetadata/v1/'.format(
+ os.getenv('GCE_METADATA_ROOT', 'metadata.google.internal'))
METADATA_HEADERS = {'Metadata-Flavor': 'Google'}
diff --git a/tests/contrib/test_gce.py b/tests/contrib/test_gce.py
index a3a1e51..5f34995 100644
--- a/tests/contrib/test_gce.py
+++ b/tests/contrib/test_gce.py
@@ -16,10 +16,12 @@
import datetime
import json
+import os
import unittest
import mock
from six.moves import http_client
+from six.moves import reload_module
from oauth2client import client
from oauth2client.contrib import _metadata
@@ -155,3 +157,19 @@ class AppAssertionCredentialsTests(unittest.TestCase):
client.save_to_well_known_file(credentials)
finally:
os.path.isdir = ORIGINAL_ISDIR
+
+ def test_custom_metadata_root_from_env(self):
+ headers = {'content-type': 'application/json'}
+ http = http_mock.HttpMock(headers=headers, data='{}')
+ fake_metadata_root = 'another.metadata.service'
+ os.environ['GCE_METADATA_ROOT'] = fake_metadata_root
+ reload_module(_metadata)
+ try:
+ _metadata.get(http, '')
+ finally:
+ del os.environ['GCE_METADATA_ROOT']
+ reload_module(_metadata)
+ # Verify mock.
+ self.assertEqual(http.requests, 1)
+ expected_uri = 'http://{}/computeMetadata/v1/'.format(fake_metadata_root)
+ self.assertEqual(http.uri, expected_uri)