Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issues with GceAssertionCredentials in Python 3 #254

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
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')
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like #271 fixes this Py3 incompatibility, but not the one in _do_refresh_request below.

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
10 changes: 5 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')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like this might be at fault for the Python 3.5 regression mentioned in this comment.

Maybe something like this would help remedy the regression:

if six.PY2:
    creds.GetServiceAccount(b'default')
else:
    creds.GetServiceAccount('default')

Copy link
Contributor

@catleeball catleeball Feb 20, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Marking this above comment "request changes", sorry I'm still a little new to the github workflow. 🙂

self.assertEqual(1, build_opener.call_count)
self.assertEqual(1, opener.open.call_count)
req = opener.open.call_args[0][0]
Expand Down