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

Commit

Permalink
Look at the v1 endpoint for the GCE metadata server
Browse files Browse the repository at this point in the history
We are updating all uses of the legacy GCE metadata server endpoints
(0.1 and v1beta) to use the current endpoint.
  • Loading branch information
Galabar001 committed Feb 9, 2016
1 parent d783f1c commit c9809c8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
7 changes: 4 additions & 3 deletions oauth2client/contrib/gce.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
logger = logging.getLogger(__name__)

# URI Template for the endpoint that returns access_tokens.
META = ('http://metadata.google.internal/0.1/meta-data/service-accounts/'
'default/acquire{?scope}')
META = ('http://metadata.google.internal/computeMetadata/v1/instance/'
'service-accounts/default/acquire{?scope}')


class AppAssertionCredentials(AssertionCredentials):
Expand Down Expand Up @@ -84,7 +84,8 @@ def _refresh(self, http_request):
"""
query = '?scope=%s' % urllib.parse.quote(self.scope, '')
uri = META.replace('{?scope}', query)
response, content = http_request(uri)
response, content = http_request(
uri, headers={'Metadata-Flavor': 'Google'})
content = _from_bytes(content)
if response.status == 200:
try:
Expand Down
13 changes: 8 additions & 5 deletions tests/contrib/test_gce.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,13 @@ def _refresh_success_helper(self, bytes_response=False):
credentials.refresh(http)
self.assertEquals(access_token, credentials.access_token)

base_metadata_uri = ('http://metadata.google.internal/0.1/meta-data/'
'service-accounts/default/acquire')
base_metadata_uri = (
'http://metadata.google.internal/computeMetadata/v1/instance/'
'service-accounts/default/acquire')
escaped_scopes = urllib.parse.quote(' '.join(scopes), safe='')
request_uri = base_metadata_uri + '?scope=' + escaped_scopes
http.request.assert_called_once_with(request_uri)
http.request.assert_called_once_with(
request_uri, headers={'Metadata-Flavor': 'Google'})

def test_refresh_success(self):
self._refresh_success_helper(bytes_response=False)
Expand Down Expand Up @@ -158,8 +160,9 @@ def test_get_access_token(self):
self.assertEqual(None, token.expires_in)

http.request.assert_called_once_with(
'http://metadata.google.internal/0.1/meta-data/service-accounts/'
'default/acquire?scope=dummy_scope')
'http://metadata.google.internal/computeMetadata/v1/instance/'
'service-accounts/default/acquire?scope=dummy_scope',
headers={'Metadata-Flavor': 'Google'})

def test_save_to_well_known_file(self):
import os
Expand Down

0 comments on commit c9809c8

Please sign in to comment.