From 47e3a7e65ae741889d82cacf721e8e698e8b155f Mon Sep 17 00:00:00 2001 From: Veniamin Stepanov Date: Mon, 14 Aug 2023 23:36:32 +0300 Subject: [PATCH] fix(emulator): add anonymous credentials for emulator based on env --- .github/workflows/ci.yml | 1 + CONTRIBUTING.md | 1 + firebase_admin/storage.py | 9 +++++++-- 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 6612efe55..64ff6ab3b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -30,6 +30,7 @@ jobs: run: | npm install -g firebase-tools firebase emulators:exec --only database --project fake-project-id 'pytest integration/test_db.py' + firebase emulators:exec --only storage --project fake-project-id 'pytest integration/test_storage.py --cert tests/data/service_account.json' lint: runs-on: ubuntu-latest diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 1d500cba8..69dc98ea6 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -218,6 +218,7 @@ First, install the Firebase CLI, then run: ``` firebase emulators:exec --only database --project fake-project-id 'pytest integration/test_db.py' +firebase emulators:exec --only storage --project fake-project-id 'pytest integration/test_storage.py --cert tests/data/service_account.json' ``` ### Test Coverage diff --git a/firebase_admin/storage.py b/firebase_admin/storage.py index f3948371c..843717bc0 100644 --- a/firebase_admin/storage.py +++ b/firebase_admin/storage.py @@ -21,12 +21,14 @@ # pylint: disable=import-error,no-name-in-module try: from google.cloud import storage + from google.cloud.storage._helpers import STORAGE_EMULATOR_ENV_VAR except ImportError: raise ImportError('Failed to import the Cloud Storage library for Python. Make sure ' 'to install the "google-cloud-storage" module.') -from firebase_admin import _utils +import os +from firebase_admin import _utils _STORAGE_ATTRIBUTE = '_storage' @@ -61,7 +63,10 @@ def __init__(self, credentials, project, default_bucket): @classmethod def from_app(cls, app): - credentials = app.credential.get_credential() + if os.getenv(STORAGE_EMULATOR_ENV_VAR) is not None: + credentials = _utils.EmulatorAdminCredentials() + else: + credentials = app.credential.get_credential() default_bucket = app.options.get('storageBucket') # Specifying project ID is not required, but providing it when available # significantly speeds up the initialization of the storage client.