Skip to content
This repository was archived by the owner on Mar 31, 2026. It is now read-only.
This repository was archived by the owner on Mar 31, 2026. It is now read-only.

bad client behavior when loading a service account key or token #177

Description

@rvandegrift

The storage client doesn't behave correctly when a service account key or token is supplied via the credentials parameter, but the project_id is not provided. In this case, it falls backs to searching for credentials.

There's two issues with the implementation:

  1. The project_id docstring says it will be inferred from the environment if omitted. If credentials contains a service account key, that search should include the data in the key (it contains the correct project id).

  2. If other credentials are found, the client silently loads them. This is very surprising, and could result in security issues when multiple credentials are available. (I discovered this when the client autoloaded my SDK credentials despite being provided a service account key.)

Ideally, if credentials is not None, the client should disable all searches and just use the provided info. If those creds fail, subsequent requests should fail.

If this is impossible due to being an API incompatibility, then a warning should be issued to tell the user that the provided credentials were ignored.

Environment details

  • OS type and version: Debian buster
  • Python version: Python 3.7.7
  • pip version: pip 20.1.1 from /usr/local/lib/python3.7/site-packages/pip (python 3.7)
  • google-cloud-storage version: 1.29.0

Steps to reproduce

  1. Create a service account and a key or token.
  2. Instantiate a client using that credential without specifying project_id.

Code example

Example showing that it falls back to default search even when a key is provided:

>>>fromgoogle.cloudimportstorage>>>fromgoogle.oauth2importservice_account>>>credentials=service_account.Credentials.from_service_account_file('gcs-test.json')
>>>storage_client=storage.Client(credentials=credentials)
Traceback (mostrecentcalllast):
File"<stdin>", line1, in<module>File"/usr/local/lib/python3.7/site-packages/google/cloud/storage/client.py", line111, in__init__project=project, credentials=credentials, _http=_httpFile"/usr/local/lib/python3.7/site-packages/google/cloud/client.py", line226, in__init___ClientProjectMixin.__init__(self, project=project)
File"/usr/local/lib/python3.7/site-packages/google/cloud/client.py", line178, in__init__project=self._determine_default(project)
File"/usr/local/lib/python3.7/site-packages/google/cloud/client.py", line193, in_determine_defaultreturn_determine_default_project(project)
File"/usr/local/lib/python3.7/site-packages/google/cloud/_helpers.py", line186, in_determine_default_project_, project=google.auth.default()
File"/usr/local/lib/python3.7/site-packages/google/auth/_default.py", line321, indefaultraiseexceptions.DefaultCredentialsError(_HELP_MESSAGE)
google.auth.exceptions.DefaultCredentialsError: Couldnotautomaticallydeterminecredentials. PleasesetGOOGLE_APPLICATION_CREDENTIALSorexplicitlycreatecredentialsandre-runtheapplication. Formoreinformation, pleaseseehttps://cloud.google.com/docs/authentication/getting-started

To see the silent fallback, run this in an environment where you've done gcloud auth application-default.

Metadata

Metadata

Labels

api: storageIssues related to the googleapis/python-storage API.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions