question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

FirestoreOptions not loading projectid from credentials correctly

See original GitHub issue

I recently migrated from an older version (0.45) where I configured firestore using the FirebaseOptions, e.g.

GoogleCredentials credentials = GoogleCredentials.fromStream(new FileInputStream(_firebaseCredentialsPath));
FirebaseOptions options = new FirebaseOptions.Builder()
            .setCredentials(credentials)
            .build();
 FirebaseApp.initializeApp(options);
 _database = FirestoreClient.getFirestore();

This works. I migrated to the new way of doing things with FirestoreOptions (0.56.0). I got warnings to change the configuration. So the resulting code is now:

GoogleCredentials credentials = GoogleCredentials.fromStream(new FileInputStream(_firebaseCredentialsPath));
FirestoreOptions fireStoreOptions =
            FirestoreOptions.newBuilder().setTimestampsInSnapshotsEnabled(true)
                    .setCredentials(credentials)
                    .build();
_database = fireStoreOptions.getService();

However, this code fails. It requires that the projectId field be set using .setProjectId(“…”). In my case it defaulted to another project in cloud that did not have firestore enabled. Perhaps the default project in cloud? It’s unclear. This seems like a bug. The project id is not available from the credentials API, even though it is there. Why is the projectid being ignored / not set from credentials correctly?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
marshallpiercecommented, Aug 7, 2018

I can confirm this, and that https://github.com/GoogleCloudPlatform/google-cloud-java/issues/3458 still is a problem. I’m using 0.56.0-beta.

For context: val credentials = ServiceAccountCredentials.fromStream(stream) loads credentials from a service account json file that is an owner in a project set aside for running tests.

  • setCredentialsProvider(FixedCredentialsProvider.create(credentials)), no setProjectId:
com.google.api.gax.rpc.FailedPreconditionException: io.grpc.StatusRuntimeException: FAILED_PRECONDITION: The Cloud Firestore API is not enabled for the project <the default project for my local gcloud config, which is not the project id in the credentials>

In other words, it’s looking in the wrong place.

  • setCredentialsProvider with a FixedCredentialsProvider.create(credentials), with setProjectId: everything works.

  • setCredentials(credentials), no setProjectId:

com.google.api.gax.rpc.FailedPreconditionException: io.grpc.StatusRuntimeException: FAILED_PRECONDITION: The Cloud Firestore API is not enabled for the project <the default project for my local gcloud config, which is not the project id in the credentials>
  • setCredentials(credentials), with setProjectId:
com.google.api.gax.rpc.PermissionDeniedException: io.grpc.StatusRuntimeException: PERMISSION_DENIED: Missing or insufficient permissions.

Which, I’m guessing, is it trying to use the default credentials on the test project’s Firestore, which of course won’t work. In other words, https://github.com/GoogleCloudPlatform/google-cloud-java/issues/3458. (Have I mentioned how much I dislike the concept of silent default credentials?)

For comparison, with 0.51.0-beta, setCredentials(credentials), with setProjectId works fine.

0reactions
sduskiscommented, Feb 21, 2019

If I read this issue correctly, this seems like “working as intended”. The user / service account used by the application needs to have access to the project resources being accessed. In this case, it sounds like the permissions of the user being used didn’t align properly with the code.

There is an effort to move away from the implicit permissions, but that’s going to take some time. I don’t see anything actionable on this issue, at this point, so I’m closing it. Please feel free to open a different issue to address documentation related issues around credentials.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Firestore Server Authentication: Overlapping Documentation
Initialize on your own server To use the Firebase Admin SDK on your own server, use a service account. Go to IAM &...
Read more >
The application default credentials are not available. They are ...
I have a project where i am calling Google Cloud Storage API from Firebase Cloud FireStore API. This is my Firebase code before...
Read more >
FirestoreOptions (Google Cloud 0.98.0-alpha API)
Returns whether a service requires a project ID. This method may be overridden in service-specific Options objects. Overrides: projectIdRequired in class ...
Read more >
firebase-admin.app package
Credential, Interface that provides Google OAuth2 access tokens used to authenticate with Firebase services.In most cases, you will not need to implement ...
Read more >
Spring Cloud GCP
The project ID specified in the JSON credentials file pointed by the ... instead, let the Spring Cloud GCP Starter get the correct...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found