Skip to content

Commit

Permalink
Merge 4f63d5d into 8129b70
Browse files Browse the repository at this point in the history
  • Loading branch information
catleeball committed Jun 20, 2019
2 parents 8129b70 + 4f63d5d commit d02e9bd
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
13 changes: 11 additions & 2 deletions apitools/base/py/credentials_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,15 @@ def GetCredentials(package_name, scopes, client_id, client_secret, user_agent,
**kwds):
"""Attempt to get credentials, using an oauth dance as the last resort."""
scopes = util.NormalizeScopes(scopes)
if isinstance(scope_spec, six.string_types):
scope_spec = six.ensure_str(scope_spec)
return set(scope_spec.split(' '))
elif isinstance(scope_spec, collections.Iterable):
scope_spec = [six.ensure_str(x) for x in scope_spec]
return set(scope_spec)
raise exceptions.TypecheckError(
'NormalizeScopes expected string or iterable, found %s' % (
type(scope_spec),))
client_info = {
'client_id': client_id,
'client_secret': client_secret,
Expand Down Expand Up @@ -355,15 +364,15 @@ def _ScopesFromMetadataServer(self, scopes):
def GetServiceAccount(self, account):
relative_url = 'instance/service-accounts'
response = _GceMetadataRequest(relative_url)
response_lines = [six.ensure_text(line).rstrip(u'/\n\r')
response_lines = [six.ensure_str(line).rstrip(u'/\n\r')
for line in response.readlines()]
return account in response_lines

def GetInstanceScopes(self):
relative_url = 'instance/service-accounts/{0}/scopes'.format(
self.__service_account_name)
response = _GceMetadataRequest(relative_url)
return util.NormalizeScopes(scope.strip()
return util.NormalizeScopes(six.ensure_str(scope).strip()
for scope in response.readlines())

# pylint: disable=arguments-differ
Expand Down
2 changes: 2 additions & 0 deletions apitools/base/py/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,10 @@ def DetectGce():
def NormalizeScopes(scope_spec):
"""Normalize scope_spec to a set of strings."""
if isinstance(scope_spec, six.string_types):
scope_spec = six.ensure_str(scope_spec)
return set(scope_spec.split(' '))
elif isinstance(scope_spec, collections.Iterable):
scope_spec = [six.ensure_str(x) for x in scope_spec]
return set(scope_spec)
raise exceptions.TypecheckError(
'NormalizeScopes expected string or iterable, found %s' % (
Expand Down

0 comments on commit d02e9bd

Please sign in to comment.