Skip to content

Commit

Permalink
Fix issues with GceAssertionCredentials in Python 3
Browse files Browse the repository at this point in the history
  • Loading branch information
charlesccychen committed Feb 2, 2019
1 parent 5cc8d57 commit 2917f22
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
4 changes: 2 additions & 2 deletions apitools/base/py/credentials_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ def _ScopesFromMetadataServer(self, scopes):
def GetServiceAccount(self, account):
relative_url = 'instance/service-accounts'
response = _GceMetadataRequest(relative_url)
response_lines = [line.rstrip('/\n\r')
response_lines = [line.rstrip(b'/\n\r').decode('utf-8')
for line in response.readlines()]
return account in response_lines

Expand Down Expand Up @@ -395,7 +395,7 @@ def _do_refresh_request(self, unused_http_request):
raise
content = response.read()
try:
credential_info = json.loads(content)
credential_info = json.loads(content.decode('utf-8'))
except ValueError:
raise exceptions.CredentialsError(
'Could not parse response as JSON: %s' % content)
Expand Down
11 changes: 6 additions & 5 deletions apitools/base/py/credentials_lib_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ def __init__(self, scopes=None, service_account_name=None):

def __call__(self, request_url):
if request_url.endswith('scopes'):
return six.StringIO(''.join(self._scopes))
return six.BytesIO(''.join(self._scopes).encode('utf-8'))
elif request_url.endswith('service-accounts'):
return six.StringIO(self._sa)
return six.BytesIO(self._sa.encode('utf-8'))
elif request_url.endswith(
'/service-accounts/%s/token' % self._sa):
return six.StringIO('{"access_token": "token"}')
return six.BytesIO('{"access_token": "token"}'.encode('utf-8'))
self.fail('Unexpected HTTP request to %s' % request_url)


Expand Down Expand Up @@ -132,11 +132,11 @@ def testGetServiceAccount(self):
creds = self._GetServiceCreds()
opener = mock.MagicMock()
opener.open = mock.MagicMock()
opener.open.return_value = six.StringIO('default/\nanother')
opener.open.return_value = six.BytesIO(b'default/\nanother')
with mock.patch.object(six.moves.urllib.request, 'build_opener',
return_value=opener,
autospec=True) as build_opener:
creds.GetServiceAccount('default')
creds.GetServiceAccount(b'default')
self.assertEqual(1, build_opener.call_count)
self.assertEqual(1, opener.open.call_count)
req = opener.open.call_args[0][0]
Expand Down Expand Up @@ -188,3 +188,4 @@ def test_without_gflags(self):
self.assertEqual(flags.auth_host_port, [8080, 8090])
self.assertEqual(flags.logging_level, 'ERROR')
self.assertEqual(flags.noauth_local_webserver, False)
unittest2.main()

0 comments on commit 2917f22

Please sign in to comment.