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

Infer project from credentials if not passed explicitly #27

Closed
tseaver opened this issue Sep 16, 2020 · 1 comment · Fixed by #51
Closed

Infer project from credentials if not passed explicitly #27

tseaver opened this issue Sep 16, 2020 · 1 comment · Fixed by #51
Assignees
Labels
type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design.

Comments

@tseaver
Copy link
Contributor

tseaver commented Sep 16, 2020

Split off from #17

See also: googleapis/python-datastore#74
See also: googleapis/python-storage#177

In a "blank slate" design, I would argue that the credentials' project should supersede the one present in the environment. However, for backward compatibility, we need to use the credentials' project only if the environment variable is not set. So the order should follow the pattern from google.auth.default when inferring project ID:

  • Explicit project ID passed to client constructor
  • Project ID set in GOOGLE_CLOUD_PROJECT envvar
  • Project ID from credentials, if present
  • Project ID from SDK
  • Project ID from GAE
  • Project ID from GCE
@yoshi-automation yoshi-automation added the triage me I really want to be triaged. label Sep 17, 2020
@busunkim96 busunkim96 added type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design. and removed triage me I really want to be triaged. labels Sep 18, 2020
@tseaver
Copy link
Contributor Author

tseaver commented Dec 8, 2020

In the case where credentials are discovered for us, google.auth.default() already falls back to the project in the credentials. E.g.:

$ unset GOOGLE_CLOUD_PROJECT GCLOUD_PROJECT
$ export GOOGLE_APPLICATION_CREDENTIALS=/path/to/service_account.json
$ grep project_id /path/to/service_account.json
    "project_id": "some-project-123"
$ python
>>> from google.auth import default
>>> credentials, project = default()
>>> project
'some-project-123'
>>> from google.cloud.client import ClientWithProject
>>> client = ClientWithProject()
>>> client.project
'some-project-123'

So, the case we need to cover for this feature is when the user has passed in explicit service account credentials, but not an explicit project ID, e.g. (this won't currently work):

$ unset GOOGLE_CLOUD_PROJECT GCLOUD_PROJECT GOOGLE_APPLICATION_CREDENTIALS
$ python
>>> from google.oauth2.service_account import Credentials
>>> credentials = Credentials.from_service_account_file("/path/to/service_account.json")
>>> from google.cloud.client import ClientWithProject
>>> client = ClientWithProject(credentials=credentials)  # currently raises

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants