Skip to content

[Firestore] Permission denials different when using GOOGLE_APPLICATION_CREDENTIALS versus setCredentials #2788

Closed

Description

@samtstern

I've got the following piece of code in the Firestore snippets:

  String returnInfoFromTransaction(long population) throws Exception {
    Map<String, Object> map = new HashMap<>();
    map.put("population", population);
    // Block until transaction is complete is using transaction.get()
    db.collection("cities").document("SF").set(map).get();
    // [START fs_return_info_transaction]
    final DocumentReference docRef = db.collection("cities").document("SF");
    ApiFuture<String> transaction =
        db.runTransaction(
            new Transaction.Function<String>() {
              @Override
              public String updateCallback(Transaction transaction) throws Exception {
                DocumentSnapshot snapshot = transaction.get(docRef).get();
                Long newPopulation = snapshot.getLong("population") + 1;
                // conditionally update based on current population
                if (newPopulation <= 1000000L) {
                  transaction.update(docRef, "population", newPopulation);
                  return "Population increased to " + newPopulation;
                } else {
                  throw new Exception("Sorry! Population is too big.");
                }
              }
            });
    // Print information retrieved from transaction
    System.out.println(transaction.get());
    // [END fs_return_info_transaction]
    return transaction.get();
  }

I can initialize Firestore in two ways:

Option 1: Environment var

$ export GOOGLE_APPLICATION_CREDENTIALS="/usr/local/google/home/samstern/service-accounts/firestore-snippets/keyfile.json"
    FirestoreOptions firestoreOptions = FirestoreOptions.getDefaultInstance().toBuilder()
        .setProjectId(projectId)
        .build();

Option 2: Explicit

    FirestoreOptions firestoreOptions = FirestoreOptions.getDefaultInstance().toBuilder()
        .setCredentials(GoogleCredentials.fromStream(new FileInputStream("/usr/local/google/home/samstern/service-accounts/firestore-snippets/keyfile.json")))
        .setProjectId(projectId)
        .build();

In both cases most of my API calls work (read, write, update, etc). However in the case of using the GOOGLE_APPLICATION_CREDENTIALS environment variable I get failures when running transactions and calling DocumentReference#getCollections():

java.util.concurrent.ExecutionException: com.google.api.gax.rpc.PermissionDeniedException: io.grpc.StatusRuntimeException: PERMISSION_DENIED: Missing or insufficient permissions.

	at com.google.common.util.concurrent.AbstractFuture.getDoneValue(AbstractFuture.java:500)
	at com.google.common.util.concurrent.AbstractFuture.get(AbstractFuture.java:479)
	at com.google.api.core.AbstractApiFuture.get(AbstractApiFuture.java:56)
	at com.example.firestore.snippets.ManageDataSnippetsIT.testSimpleTransaction(ManageDataSnippetsIT.java:150)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
	at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
	at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
	at org.junit.runners.Suite.runChild(Suite.java:128)
	at org.junit.runners.Suite.runChild(Suite.java:27)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
	at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
	at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
	at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: com.google.api.gax.rpc.PermissionDeniedException: io.grpc.StatusRuntimeException: PERMISSION_DENIED: Missing or insufficient permissions.
	at com.google.api.gax.rpc.ApiExceptionFactory.createException(ApiExceptionFactory.java:55)
	at com.google.api.gax.grpc.GrpcExceptionCallable$ExceptionTransformingFuture.setException(GrpcExceptionCallable.java:118)
	at com.google.api.gax.grpc.GrpcExceptionCallable$ExceptionTransformingFuture.onFailure(GrpcExceptionCallable.java:101)
	at com.google.api.core.ApiFutures$1.onFailure(ApiFutures.java:61)
	at com.google.common.util.concurrent.Futures$4.run(Futures.java:1123)
	at com.google.common.util.concurrent.MoreExecutors$DirectExecutor.execute(MoreExecutors.java:435)
	at com.google.common.util.concurrent.AbstractFuture.executeListener(AbstractFuture.java:900)
	at com.google.common.util.concurrent.AbstractFuture.complete(AbstractFuture.java:811)
	at com.google.common.util.concurrent.AbstractFuture.setException(AbstractFuture.java:675)
	at io.grpc.stub.ClientCalls$GrpcFuture.setException(ClientCalls.java:458)
	at io.grpc.stub.ClientCalls$UnaryStreamToFuture.onClose(ClientCalls.java:433)
	at io.grpc.ForwardingClientCallListener.onClose(ForwardingClientCallListener.java:41)
	at io.grpc.internal.CensusTracingModule$TracingClientInterceptor$1$1.onClose(CensusTracingModule.java:339)
	at io.grpc.internal.ClientCallImpl.closeObserver(ClientCallImpl.java:443)
	at io.grpc.internal.ClientCallImpl.access$300(ClientCallImpl.java:63)
	at io.grpc.internal.ClientCallImpl$ClientStreamListenerImpl.close(ClientCallImpl.java:525)
	at io.grpc.internal.ClientCallImpl$ClientStreamListenerImpl.access$600(ClientCallImpl.java:446)
	at io.grpc.internal.ClientCallImpl$ClientStreamListenerImpl$1StreamClosed.runInContext(ClientCallImpl.java:557)
	at io.grpc.internal.ContextRunnable.run(ContextRunnable.java:37)
	at io.grpc.internal.SerializingExecutor.run(SerializingExecutor.java:107)
	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
	at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180)
	at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:295)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
	at java.lang.Thread.run(Thread.java:745)
Caused by: io.grpc.StatusRuntimeException: PERMISSION_DENIED: Missing or insufficient permissions.
	at io.grpc.Status.asRuntimeException(Status.java:526)
	... 17 more

Activity

  1. pongad commented on Jan 18, 2018

    @pongad
    Contributor

    This sounds like a Firestore-specific problem. Sebastian, can you take a look?

  2. samtstern commented on Jan 18, 2018

    @samtstern
    Author

    @pongad I kind of suspect it's related to application default credentials in general. Sebastian has not been able to reproduce on his machine.

  3. schmidt-sebastian commented on Jan 18, 2018

    @schmidt-sebastian
    Contributor

    I suspect that this might be a threading issue in GRPC itself, since it's actually the DocumentSnapshot snapshot = transaction.get(docRef).get() call that is failing. When run inside a transaction, this call is (by default) running on the GRPC executor thread and somehow not picking up the right credentials. Even with Sam's exact setup, I am not able to reproduce this.

    Can you route this over to someone who is responsible for the GRPC stack?

  4. pongad commented on Jan 24, 2018

    @pongad
    Contributor

    It's possible that this is a problem in codegen or gax. I'll try to track this down.

  5. added
    type: bug
    on Jan 30, 2018
  6. added
    type: question
    and removed
    type: bug
    on Jan 30, 2018
  7. pongad commented on Feb 5, 2018

    @pongad
    Contributor

    I looked through codegen and gax layer. Almost everything is sequential, so I don't think it can cause this kind of working-sometimes problem. We haven't ruled out the possibility that this is caused by the auth lib itself though.

    @ejona, do you have a sense of how we could approach this problem? I believe @samtstern was running on gRPC 1.7.

  8. pongad commented on Feb 22, 2018

    @pongad
    Contributor

    Ping @ejona. Since this is on Firestore, I'll assign to Sebastian to track.

  9. salamanders commented on Oct 12, 2018

    @salamanders

    I'm having a similar issue. I https://stackoverflow.com/questions/52772199/credentials-failed-to-obtain-metadata-error-when-trying-to-auth-java-client-to Now that I switched from FirestoreOptions.getDefaultInstance() to FirestoreOptions.newBuilder() I'm one step closer, and am getting the same io.grpc.StatusRuntimeException: PERMISSION_DENIED: Missing or insufficient permissions. error when trying a simple write.

    Erk. So when I used a service account through the Google Cloud console (because I thought I had to) it gave this issue. But when I regenerated a new service account json through the firebase instructions, it started working!

  10. ajaaym commented on Jan 11, 2019

    @ajaaym
    Contributor

    Closing this, since seems like regenerating new json works. Please feel free to reopen if need further help.

  11. vermashivam commented on Jul 11, 2020

    @vermashivam

    I am getting similar error, and also have tried using new json file, but still the error persists.

    com.google.api.gax.rpc.PermissionDenied Exception: io.grpc.StatusRuntimeException: PERMISSION_DENIED: Missing or insufficient permissions.

  12. valtoni commented on Mar 14, 2021

    @valtoni

    I'm getting similar error too:

    1. If I use GoogleCredentials.getApplicationDefault() and setting GOOGLE_APPLICATION_CREDENTIALS variable to json service file, everything is going fine.
    2. If I use FirestoreOptions and give the following line pointing to the same file, it's fails.
      The line:
    FirestoreOptions.getDefaultInstance().toBuilder()
                        .setProjectId(PROJECTID)
                        .setCredentials(GoogleCredentials.fromStream(new FileInputStream("/Users/valtoni/access-86bc4a199533.json")))
                        .build();
    
    

    Stacktrace:

    Exception in thread "main" java.lang.RuntimeException: Problem listing store
    	at com.example.firebase.FirebaseBeanDataSourceTest.listTestBeans01(FirebaseBeanDataSourceTest.java:72)
    	at com.example.firebase.FirebaseBeanDataSourceTest.main(FirebaseBeanDataSourceTest.java:78)
    Caused by: java.util.concurrent.ExecutionException: com.google.api.gax.rpc.PermissionDeniedException: io.grpc.StatusRuntimeException: PERMISSION_DENIED: Missing or insufficient permissions.
    	at com.google.common.util.concurrent.AbstractFuture.getDoneValue(AbstractFuture.java:566)
    	at com.google.common.util.concurrent.AbstractFuture.get(AbstractFuture.java:547)
    	at com.google.api.core.AbstractApiFuture.get(AbstractApiFuture.java:56)
    	at com.example.firebase.FirebaseBeanDataSourceTest.listTestBeans01(FirebaseBeanDataSourceTest.java:61)
    	... 1 more
    Caused by: com.google.api.gax.rpc.PermissionDeniedException: io.grpc.StatusRuntimeException: PERMISSION_DENIED: Missing or insufficient permissions.
    	at com.google.api.gax.rpc.ApiExceptionFactory.createException(ApiExceptionFactory.java:55)
    	at com.google.api.gax.grpc.GrpcApiExceptionFactory.create(GrpcApiExceptionFactory.java:72)
    	at com.google.api.gax.grpc.GrpcApiExceptionFactory.create(GrpcApiExceptionFactory.java:60)
    	at com.google.api.gax.grpc.ExceptionResponseObserver.onErrorImpl(ExceptionResponseObserver.java:82)
    	at com.google.api.gax.rpc.StateCheckingResponseObserver.onError(StateCheckingResponseObserver.java:86)
    	at com.google.api.gax.grpc.GrpcDirectStreamController$ResponseObserverAdapter.onClose(GrpcDirectStreamController.java:149)
    	at io.grpc.internal.DelayedClientCall$DelayedListener$3.run(DelayedClientCall.java:464)
    	at io.grpc.internal.DelayedClientCall$DelayedListener.delayOrExecute(DelayedClientCall.java:428)
    	at io.grpc.internal.DelayedClientCall$DelayedListener.onClose(DelayedClientCall.java:461)
    	at io.grpc.internal.ClientCallImpl.closeObserver(ClientCallImpl.java:553)
    	at io.grpc.internal.ClientCallImpl.access$300(ClientCallImpl.java:68)
    	at io.grpc.internal.ClientCallImpl$ClientStreamListenerImpl$1StreamClosed.runInternal(ClientCallImpl.java:739)
    	at io.grpc.internal.ClientCallImpl$ClientStreamListenerImpl$1StreamClosed.runInContext(ClientCallImpl.java:718)
    	at io.grpc.internal.ContextRunnable.run(ContextRunnable.java:37)
    	at io.grpc.internal.SerializingExecutor.run(SerializingExecutor.java:123)
    	at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
    	at java.base/java.util.concurrent.FutureTask.run$$$capture(FutureTask.java:264)
    	at java.base/java.util.concurrent.FutureTask.run(FutureTask.java)
    	at java.base/java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:304)
    	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    	at java.base/java.lang.Thread.run(Thread.java:834)
    Caused by: io.grpc.StatusRuntimeException: PERMISSION_DENIED: Missing or insufficient permissions.
    	at io.grpc.Status.asRuntimeException(Status.java:535)
    	... 17 more
    
  13. schmidt-sebastian commented on Mar 16, 2021

    @schmidt-sebastian
    Contributor
  14. guptapavitra commented on Jun 3, 2021

    @guptapavitra

    .setCredentialsProvider(FixedCredentialsProvider.create(credentials)) instead of .setCredentials(credentials) worked for me. Thank you! However, I would like to understand the difference between the two.

  15. schmidt-sebastian commented on Jun 4, 2021

    @schmidt-sebastian
    Contributor

    setCredentialsProvider has a higher "priority" and since we have to use it ourselves, you also have to use this method to apply your settings. Unfortunately, I cannot explain why we have two methods to begin with.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

authtype: question

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions