Using Firestore emulator from Python (hopefully without secret keys)

1,563 views
Skip to first unread message

Alan deLespinasse

unread,
Apr 6, 2020, 7:32:17 PM4/6/20
to Firebase Google Group
I'm sure this has been asked before, but the only place I can find is this Stack Overflow question, and I'm pretty sure that is out of date. I know I can get it to work much more simply by just setting FIRESTORE_EMULATOR_HOST=localhost:8080 and then initializing with a credentials file, like:

import firebase_admin
from firebase_admin import auth
from firebase_admin import credentials
from firebase_admin import firestore

cred = credentials.Certificate('firebase-adminsdk.json')
firebase_admin.initialize_app(cred)
db = firestore.client()


(I'm happy to add a new answer there when I understand the issue better).

What I don't like about the above is that I need an admin credentials file to make it work. I wouldn't want to check such a thing into GitHub, for example, so anyone who checks out my code and wants to run tests needs to get the credentials file some other way, and I have to set up my CI environment to have the credentials somehow. These aren't unsolvable problems, but I don't think it should be necessary, because I've used the Firestore emulator from Node.js before, and it didn't need secret credentials. A user can just check out the code, install the dependencies, and run the tests.

I think what I should be able to do is just set FIRESTORE_EMULATOR_HOST=localhost:8080 and then initialize Firebase in a fairly normal way, i.e. something like

import firebase_admin
from firebase_admin import auth
from firebase_admin import credentials
from firebase_admin import firestore

# Use the application default credentials
cred = credentials.ApplicationDefault()
firebase_admin.initialize_app(
    cred,
    {
      'projectId': 'my_project_id',
    },
)
db = firestore.client()


Or maybe even leave out the initialize_app call if I don't need any Firebase functionality other than Firestore, i.e.,

from firebase_admin import firestore

db = firestore.client() # Or should it be firestore.Client() ?


When I try any of the above, though, I get errors saying it can't determine the default credentials (in the first case), or there is no default Firebase instance (in the second).

Is this posslble? It seems like it should be.

Hiranya Jayathilaka

unread,
Apr 6, 2020, 8:11:15 PM4/6/20
to fireba...@googlegroups.com
initialize_app() is required across all our SDKs, so you cannot skip it. As for getting over the credentials requirement you can try one of the following options:

1. Use a fake service account json file (like this one), and set the GOOGLE_APPLICATION_CREDENTIALS environment variable. Then you can use application default credentials as usual for your tests.
2. Implement the credentials.Base interface and provide your own fake credential type. SDK uses this approach when running against the RTDB emulator, but no such thing exists currently for Firestore. But it's pretty straightforward to handle that in your test code.



--
You received this message because you are subscribed to the Google Groups "Firebase Google Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to firebase-tal...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/firebase-talk/78c2ca65-a91a-43d6-90a4-e658674c5018%40googlegroups.com.


--

Hiranya Jayathilaka | Software Engineer | h...@google.com | 650-203-0128

Sam Stern

unread,
Apr 8, 2020, 11:36:36 AM4/8/20
to Firebase Google Group
Hi Alan,

I think it should also work with zero config if you export two environment variables:
export GCLOUD_PROJECT="your-project-id"
export FIRESTORE_EMULATOR_HOST="localhost:8080"

Then you should be able to do:
firebase_admin.initialize_app()
db = firestore.client()

I am basing this on my experience with other Admin SDKs (I don't write much Python) but if that doesn't work we should be able to fix it.

- Sam




Reply all
Reply to author
Forward
0 new messages