GoogleCloudPlatform / gcs-oauth2-boto-plugin

Auth plugin allowing use the use of OAuth 2.0 credentials for Google Cloud Storage in the Boto library.
Apache License 2.0
21 stars 38 forks source link

fix python3 json lib error in oauth2_helper.py #41

Closed jmoiron closed 5 years ago

jmoiron commented 6 years ago

When using this as a python module from python3 and providing it with a key file, it reads with 'rb', which causes python3 json library to choke:

Traceback (most recent call last):
  File "./repl.py", line 23, in <module>
  elif token_cache_type == 'in_memory':
    connection = boto.connect_s3(provider='google', host='storage.googleapis.com')
  File "/usr/local/lib/python3.5/dist-packages/boto/__init__.py", line 141, in connect_s3
    return S3Connection(aws_access_key_id, aws_secret_access_key, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/boto/s3/connection.py", line 194, in __init__
    validate_certs=validate_certs, profile_name=profile_name)
  File "/usr/local/lib/python3.5/dist-packages/boto/connection.py", line 569, in __init__
    host, config, self.provider, self._required_auth_capability())
  File "/usr/local/lib/python3.5/dist-packages/boto/auth.py", line 1011, in get_auth_handler
    ready_handlers.append(handler(host, config, provider))
  File "/usr/local/lib/python3.5/dist-packages/gcs_oauth2_boto_plugin/oauth2_plugin.py", line 56, in __init__
    config, cred_type=oauth2_client.CredTypes.OAUTH2_SERVICE_ACCOUNT)
  File "/usr/local/lib/python3.5/dist-packages/gcs_oauth2_boto_plugin/oauth2_helper.py", line 85, in OAuth2ClientFromBotoConfig
    json_key_dict = json.loads(private_key)
  File "/usr/lib/python3.5/json/__init__.py", line 312, in loads
    s.__class__.__name__))
TypeError: the JSON object must be str, not 'bytes'

There are two potential fixes; read with 'r' instead of 'rb', or decode the bytes into a string for json. I chose the latter, in case the fallback file format requires the bytes, but I did not test with the fallback format.

Sample code to cause this from this gist:

import boto
import gcs_oauth2_boto_plugin

boto.config.add_section('Credentials')
boto.config.set('Credentials', 'gs_service_key_file', '/path/to/account/file.json')
boto.config.set('Credentials', 'gs_service_client_id', 'user@project.iam.gserviceaccount.com')
connection = boto.connect_s3(provider='google', host='storage.googleapis.com')
houglum commented 5 years ago

Terribly sorry to have let this sit for so long. Just saw that we have a few open PRs that nobody triaged :(

That aside, it looks like this issue was fixed in #44, so I'm going to mark this one as closed. Thanks for submitting a PR though!