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

Commit

Permalink
Merge pull request #387 from Galabar001/master
Browse files Browse the repository at this point in the history
Look at the v1 endpoint for the GCE metadata server.
  • Loading branch information
nathanielmanistaatgoogle committed Feb 10, 2016
2 parents 6d46c46 + c9809c8 commit b13ab40
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 @@ -33,8 +33,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 @@ -85,7 +85,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 == http_client.OK:
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 @@ -67,11 +67,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 @@ -159,8 +161,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 b13ab40

Please sign in to comment.