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: Correctly catch DefaultCredentialsError when looking up project_id #720

Merged
merged 3 commits into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
3 changes: 2 additions & 1 deletion firebase_admin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import os
import threading

from google.auth.exceptions import DefaultCredentialsError
from firebase_admin import credentials
from firebase_admin.__about__ import __version__

Expand Down Expand Up @@ -257,7 +258,7 @@ def _lookup_project_id(self):
if not project_id:
try:
project_id = self._credential.project_id
except AttributeError:
except (AttributeError, DefaultCredentialsError):
pass
Copy link
Member

Choose a reason for hiding this comment

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

Do we want to throw here or pass?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We want to pass and look for other ways of getting a project_id.

if not project_id:
project_id = os.environ.get('GOOGLE_CLOUD_PROJECT',
Expand Down
14 changes: 14 additions & 0 deletions tests/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,20 @@ def evaluate():
assert app.project_id is None
testutils.run_without_project_id(evaluate)

def test_no_project_id_from_environment(self, app_credential):
env_var_name = 'GOOGLE_APPLICATION_CREDENTIALS'
def evaluate():
app = firebase_admin.initialize_app(app_credential, name='myApp')
app._credential._g_credential = None
old_env_var = os.environ.get(env_var_name)
if old_env_var:
del os.environ[env_var_name]
project_id = app.project_id
if old_env_var:
os.environ[env_var_name] = old_env_var
assert project_id is None
testutils.run_without_project_id(evaluate)

def test_non_string_project_id(self):
options = {'projectId': {'key': 'not a string'}}
with pytest.raises(ValueError):
Expand Down
Loading