Skip to content

Commit feec15f

Browse files
craigcitroJon Wayne Parrott
authored and
Jon Wayne Parrott
committed
Allow customizing the GCE metadata service address via an env var. (googleapis#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.
1 parent a3cf56b commit feec15f

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

oauth2client/client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@
108108
GCE_METADATA_TIMEOUT = 3
109109

110110
_SERVER_SOFTWARE = 'SERVER_SOFTWARE'
111-
_GCE_METADATA_URI = 'http://169.254.169.254'
111+
_GCE_METADATA_URI = 'http://' + os.getenv('GCE_METADATA_IP', '169.254.169.254')
112112
_METADATA_FLAVOR_HEADER = 'metadata-flavor' # lowercase header
113113
_DESIRED_METADATA_FLAVOR = 'Google'
114114
_GCE_HEADERS = {_METADATA_FLAVOR_HEADER: _DESIRED_METADATA_FLAVOR}

oauth2client/contrib/_metadata.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import datetime
2121
import json
22+
import os
2223

2324
from six.moves import http_client
2425
from six.moves.urllib import parse as urlparse
@@ -28,7 +29,8 @@
2829
from oauth2client import transport
2930

3031

31-
METADATA_ROOT = 'http://metadata.google.internal/computeMetadata/v1/'
32+
METADATA_ROOT = 'http://{}/computeMetadata/v1/'.format(
33+
os.getenv('GCE_METADATA_ROOT', 'metadata.google.internal'))
3234
METADATA_HEADERS = {'Metadata-Flavor': 'Google'}
3335

3436

tests/contrib/test_gce.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@
1616

1717
import datetime
1818
import json
19+
import os
1920
import unittest
2021

2122
import mock
2223
from six.moves import http_client
24+
from six.moves import reload_module
2325

2426
from oauth2client import client
2527
from oauth2client.contrib import _metadata
@@ -155,3 +157,19 @@ def test_save_to_well_known_file(self):
155157
client.save_to_well_known_file(credentials)
156158
finally:
157159
os.path.isdir = ORIGINAL_ISDIR
160+
161+
def test_custom_metadata_root_from_env(self):
162+
headers = {'content-type': 'application/json'}
163+
http = http_mock.HttpMock(headers=headers, data='{}')
164+
fake_metadata_root = 'another.metadata.service'
165+
os.environ['GCE_METADATA_ROOT'] = fake_metadata_root
166+
reload_module(_metadata)
167+
try:
168+
_metadata.get(http, '')
169+
finally:
170+
del os.environ['GCE_METADATA_ROOT']
171+
reload_module(_metadata)
172+
# Verify mock.
173+
self.assertEqual(http.requests, 1)
174+
expected_uri = 'http://{}/computeMetadata/v1/'.format(fake_metadata_root)
175+
self.assertEqual(http.uri, expected_uri)

0 commit comments

Comments
 (0)