Skip to content
This repository has been archived by the owner on Nov 5, 2019. It is now read-only.

Commit

Permalink
Allow customizing the GCE metadata service address via an env var.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
craigcitro committed Mar 24, 2017
1 parent c055e7f commit 44670b1
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
2 changes: 1 addition & 1 deletion oauth2client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@
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}
Expand Down
4 changes: 3 additions & 1 deletion oauth2client/contrib/_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import datetime
import json
import os

from six.moves import http_client
from six.moves.urllib import parse as urlparse
Expand All @@ -28,7 +29,8 @@
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'}


Expand Down
18 changes: 18 additions & 0 deletions tests/contrib/test_gce.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -155,3 +157,19 @@ def test_save_to_well_known_file(self):
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)

0 comments on commit 44670b1

Please sign in to comment.