8000 Validating project ID values globallly in App class (#122) · AFFOA/firebase-admin-python@5f90e02 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5f90e02

Browse files
authored
Validating project ID values globallly in App class (firebase#122)
1 parent d2d0060 commit 5f90e02

File tree

4 files changed

+31
-11
lines changed

4 files changed

+31
-11
lines changed

firebase_admin/__init__.py

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -218,15 +218,38 @@ def __init__(self, name, credential, options):
218218
self._options = _AppOptions(options)
219219
self._lock = threading.RLock()
220220
self._services = {}
221-
pid = self._options.get('projectId')
221+
self._project_id = App._lookup_project_id(self._credential, self._options)
222+
223+
@classmethod
224+
def _lookup_project_id(cls, credential, options):
225+
"""Looks up the Firebase project ID associated with an App.
226+
227+
This method first inspects the app options for a ``projectId`` entry. Then it attempts to
228+
get the project ID from the credential used to initialize the app. If that also fails,
229+
attempts to look up the ``GCLOUD_PROJECT`` environment variable.
230+
231+
Args:
232+
credential: A Firebase credential instance.
233+
options: A Firebase AppOptions instance.
234+
235+
Returns:
236+
str: A project ID string or None.
237+
238+
Raises:
239+
ValueError: If a non-string project ID value is specified.
240+
"""
241+
pid = options.get('projectId')
222242
if not pid:
223243
try:
224-
pid = self._credential.project_id
244+
pid = credential.project_id
225245
except AttributeError:
226246
pass
227247
if not pid:
228248
pid = os.environ.get('GCLOUD_PROJECT')
229-
self._project_id = pid
249+
if pid is not None and not isinstance(pid, six.string_types):
250+
raise ValueError(
251+
'Invalid project ID: "{0}". project ID must be a string.'.format(pid))
252+
return pid
230253

231254
@property
232255
def name(self):

firebase_admin/firestore.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@
2828
raise ImportError('Failed to import the Cloud Firestore library for Python. Make sure '
2929
'to install the "google-cloud-firestore" module.')
3030

31-
import six
32-
3331
from firebase_admin import _utils
3432

3533

@@ -75,7 +73,4 @@ def from_app(cls, app):
7573
'Project ID is required to access Firestore. Either set the projectId option, '
7674
'or use service account credentials. Alternatively, set the GCLOUD_PROJECT '
7775
'environment variable.')
78-
elif not isinstance(project, six.string_types):
79-
raise ValueError(
80-
'Invalid project ID: "{0}". project ID must be a string.'.format(project))
8176
return _FirestoreClient(credentials, project)

firebase_admin/instance_id.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,6 @@ def __init__(self, app):
7979
'Project ID is required to access Instance ID service. Either set the projectId '
8080
'option, or use service account credentials. Alternatively, set the '
8181
'GCLOUD_PROJECT environment variable.')
82-
elif not isinstance(project_id, six.string_types):
83-
raise ValueError(
84-
'Invalid project ID: "{0}". project ID must be a string.'.format(project_id))
8582
self._project_id = project_id
8683
self._client = _http_client.JsonHttpClient(
8784
credential=app.credential.get_credential(), base_url=_IID_SERVICE_URL)

tests/test_app.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,11 @@ def test_no_project_id(self):
317317
if project_id:
318318
os.environ[GCLOUD_PROJECT] = project_id
319319

320+
def test_non_string_project_id(self):
321+
options = {'projectId': {'key': 'not a string'}}
322+
with pytest.raises(ValueError):
323+
firebase_admin.initialize_app(CREDENTIAL, options=options)
324+
320325
def test_app_get(self, init_app):
321326
a 3D1F ssert init_app is firebase_admin.get_app(init_app.name)
322327

0 commit comments

Comments
 (0)
0