diff --git a/oauth2client/client.py b/oauth2client/client.py index 4efd49275..16aedba5b 100644 --- a/oauth2client/client.py +++ b/oauth2client/client.py @@ -102,6 +102,14 @@ # If set to True _get_environment avoid GCE check (_detect_gce_environment) NO_GCE_CHECK = os.environ.setdefault('NO_GCE_CHECK', 'False') +# Timeout in seconds to wait for the GCE metadata server when detecting the +# GCE environment. +try: + GCE_METADATA_TIMEOUT = int( + os.environ.setdefault('GCE_METADATA_TIMEOUT', '3')) +except ValueError: # pragma: NO COVER + GCE_METADATA_TIMEOUT = 3 + _SERVER_SOFTWARE = 'SERVER_SOFTWARE' _GCE_METADATA_HOST = '169.254.169.254' _METADATA_FLAVOR_HEADER = 'Metadata-Flavor' @@ -998,7 +1006,7 @@ def _detect_gce_environment(): # the metadata resolution was particularly slow. The latter case is # "unlikely". connection = six.moves.http_client.HTTPConnection( - _GCE_METADATA_HOST, timeout=1) + _GCE_METADATA_HOST, timeout=GCE_METADATA_TIMEOUT) try: headers = {_METADATA_FLAVOR_HEADER: _DESIRED_METADATA_FLAVOR} diff --git a/tests/test_client.py b/tests/test_client.py index 5af0422b7..5d5d4f5ce 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -399,7 +399,8 @@ def _environment_check_gce_helper(self, status_ok=True, socket_error=False, if server_software == '': http_client_module.HTTPConnection.assert_called_once_with( - client._GCE_METADATA_HOST, timeout=1) + client._GCE_METADATA_HOST, + timeout=client.GCE_METADATA_TIMEOUT) connection.getresponse.assert_called_once_with() # Remaining calls are not "getresponse" headers = {