Skip to content
This repository has been archived by the owner on Jan 18, 2025. It is now read-only.

Use the vendor BIOS file to determine if we are on GCE. #539

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions oauth2client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@
NO_GCE_CHECK = os.environ.setdefault('NO_GCE_CHECK', 'False')

_SERVER_SOFTWARE = 'SERVER_SOFTWARE'
_BIOS_VENDOR_FILE = '/sys/class/dmi/id/bios_vendor'
_GCE_METADATA_HOST = '169.254.169.254'
_METADATA_FLAVOR_HEADER = 'Metadata-Flavor'
_DESIRED_METADATA_FLAVOR = 'Google'
Expand Down Expand Up @@ -1107,6 +1108,16 @@ def _detect_gce_environment():
Boolean indicating whether or not the current environment is Google
Compute Engine.
"""
# We first check the BIOS vendor file, possibly short-circuiting the more
# expensive (and flaky) metadata server connection test.
if os.path.isfile(_BIOS_VENDOR_FILE):
try:
with open(_BIOS_VENDOR_FILE, 'r') as content:
if content.read().rstrip() == 'Google':
return True
except IOError:
logger.info('Could not read BIOS vendor file found.')

# NOTE: The explicit ``timeout`` is a workaround. The underlying
# issue is that resolving an unknown host on some networks will take
# 20-30 seconds; making this timeout short fixes the issue, but
Expand Down