firebase-tools icon indicating copy to clipboard operation
firebase-tools copied to clipboard

"Error: The incoming JSON object does not contain a client_email field" after upgrading to 7.0.2

Open serbanstef opened this issue 7 years ago • 61 comments

[REQUIRED] Environment info

firebase-tools: 7.0.2

Platform: Oracle Linux Server 7.6, Node 10.15

[REQUIRED] Test case

emulate https function with "firebase-admin": "^8.2.0", "firebase-functions": "^3.0.2"

[REQUIRED] Steps to reproduce

run any https function with firebase functions:shell

[REQUIRED] Expected behavior

no error (firebase-tools 7.0.0 throws no error)

[REQUIRED] Actual behavior

Sent request to function.

firebase > ⚠  Error: The incoming JSON object does not contain a client_email field
    at JWT.fromJSON (/riderequest/functions/node_modules/google-auth-library/build/src/auth/jwtclient.js:165:19)
    at GoogleAuth.fromJSON (/riderequest/functions/node_modules/google-auth-library/build/src/auth/googleauth.js:294:16)
    at GoogleAuth.getClient (/riderequest/functions/node_modules/google-auth-library/build/src/auth/googleauth.js:476:52)
    at GrpcClient._getCredentials (/riderequest/functions/node_modules/google-gax/build/src/grpc.js:107:40)
    at GrpcClient.createStub (/riderequest/functions/node_modules/google-gax/build/src/grpc.js:223:34)
    at new FirestoreClient (/riderequest/functions/node_modules/@google-cloud/firestore/build/src/v1/firestore_client.js:128:39)
    at ClientPool.Firestore._clientPool.pool_1.ClientPool [as clientFactory] (/riderequest/functions/node_modules/@google-cloud/firestore/build/src/index.js:315:26)
    at ClientPool.acquire (/riderequest/functions/node_modules/@google-cloud/firestore/build/src/pool.js:61:35)
    at ClientPool.run (/riderequest/functions/node_modules/@google-cloud/firestore/build/src/pool.js:114:29)
    at Firestore.request (/riderequest/functions/node_modules/@google-cloud/firestore/build/src/index.js:957:33)
⚠  Your function was killed because it raised an unhandled error.

serbanstef avatar Jun 28 '19 15:06 serbanstef

Relevant stackoverflow post: https://stackoverflow.com/questions/56789424/the-incoming-json-object-does-not-contain-a-client-email-field

noelmansour avatar Jun 28 '19 16:06 noelmansour

@noelmansour thanks so it seems like something between 7.0.1 and 7.0.2 caused this, I will look at the changes made.

samtstern avatar Jun 28 '19 21:06 samtstern

Looks like that error comes from this library: https://github.com/googleapis/google-auth-library-nodejs/issues/137

https://github.com/googleapis/google-auth-library-nodejs/blob/37bb8c7cd0a6501103274284d9cddd6816cc881e/src/auth/jwtclient.ts#L253

samtstern avatar Jun 28 '19 21:06 samtstern

Am also experiencing this issue since 7.0.2

mschristensen avatar Jun 30 '19 19:06 mschristensen

Just upgraded to 7.0.2, seeing this as well. Checked my account credentials and the client_email field is present.

jrthib avatar Jul 01 '19 02:07 jrthib

Could someone share the code of a function they're trying to run in the emulator that fails with this error? The simpler the better.

samtstern avatar Jul 01 '19 17:07 samtstern

@samtstern The following code fails for me:

import * as admin from 'firebase-admin';
admin.initializeApp();

import * as functions from 'firebase-functions';

export const testFunction = functions.https.onRequest(async (req, res) => {
  const request = await admin.firestore().doc('test/123').get();

  return request.ref.set({ test: 'random' })
    .then(() => res.send('ok!'))
    .catch((err) => res.status(500).send('error'));
});

It only works if I set a service account when calling admin.initializeApp():

// this works
admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
});

ceolinwill avatar Jul 01 '19 18:07 ceolinwill

@wceolin thanks for that! Can you run the following inside your functions directory?

$ npm list @google-cloud/firestore

This is my result (and everything works for me):

functions@ /tmp/tmp.ZkMEM0XGPF/functions
└─┬ [email protected]
  └── @google-cloud/[email protected]

samtstern avatar Jul 01 '19 18:07 samtstern

I got the same result: 🤔

Screen Shot 2019-07-01 at 15 44 35

ceolinwill avatar Jul 01 '19 18:07 ceolinwill

So I am walking my way up the error stack trace and in this function: https://github.com/googleapis/gax-nodejs/blob/e1be4ebcc2287c61d5f1884033449e3b4e143242/src/grpc.ts#L141

We have this branching:

 async _getCredentials(opts: ClientStubOptions) {
    if (opts.sslCreds) {
      return opts.sslCreds;
    }
    const grpc = this.grpc;
    const sslCreds = grpc.credentials.createSsl();
    const client = await this.auth.getClient();
    const credentials = grpc.credentials.combineChannelCredentials(
      sslCreds,
      grpc.credentials.createFromGoogleCredential(client)
    );
    return credentials;
  }

In my code opts.sslCreds is defined so I hit this branch:

    if (opts.sslCreds) {
      return opts.sslCreds;
    }

It's the other branch which tries to assemble credentials and causes problems. Now I need to figure out why my code goes one way and yours goes another way.

samtstern avatar Jul 01 '19 18:07 samtstern

@wceolin could you reach into your node_modules folder and edit this file functions/node_modules/google-gax/build/src/grpc.js to have a console.log() that prints the options object in the createStub function and let me know what you see?

Thank you for your help so far!

samtstern avatar Jul 01 '19 18:07 samtstern

@samtstern Not sure if this helps, but I did a bunch of tweaking with my package.json and node_modules over the weekend. I no longer have the issue on 7.0.2.

The npm list @google-cloud/firestore output matches yours.

I nuked my node_modules directory over the weekend so I wonder if that's related.

noelmansour avatar Jul 01 '19 18:07 noelmansour

@noelmansour thanks for that note, explains why I can't reproduce in a clean setup.

samtstern avatar Jul 01 '19 18:07 samtstern

@noelmansour I also tried deleting my node_modules, yarn.lock, removing dependencies but it didn't work for me. I'm gonna try creating a project from scratch to see what happens.

@noelmansour this is what I got from the options object:

{
  "clientConfig": {},
  "port":443,
  "servicePath":"firestore.googleapis.com",
  "credentials":{},
  "projectId":"my-test-project",
  "firebaseVersion":"8.2.0",
  "libName":"gccl",
  "libVersion":"2.2.3 fire/8.2.0",
  "scopes":[
    "https://www.googleapis.com/auth/cloud-platform", 
    "https://www.googleapis.com/auth/datastore"
  ]
}

ceolinwill avatar Jul 01 '19 19:07 ceolinwill

@wceolin thanks that's extremely helpful because this is what mine looks like (and what it should look like):

{
   "clientConfig":{},
   "port":8080,
   "servicePath":"localhost",
   "credentials":{},
   "projectId":"fir-dumpster",
   "firebaseVersion":"8.2.0",
   "libName":"gccl",
   "libVersion":"2.2.3 fire/8.2.0",
   "service":"firestore.googleapis.com",
   "sslCreds":{
      "callCredentials": {}
   },
   "customHeaders":{
      "Authorization":"Bearer owner"
   },
   "scopes":[
      "https://www.googleapis.com/auth/cloud-platform",
      "https://www.googleapis.com/auth/datastore"
   ]
}

As you can see your client is still trying to access production, and then it fails because the functions emulator is not authorized to do so.

Are you also running the firestore emulator? Is your goal to write to the firestore emulator or do you actually want to write to production firestore?

samtstern avatar Jul 01 '19 19:07 samtstern

OH I WAS FINALLY ABLE TO REPRODUCE THIS! If I run firebase emulators:start --only functions then I try to write to Firestore I get this error.

I see this log message:

⚠  The Cloud Firestore emulator is not running so database operations will fail with a 'default credentials' error.

Which is no longer exactly right (the error is about client_email) but the detection is still correct. Do others here see this warning as well?

samtstern avatar Jul 01 '19 19:07 samtstern

Here's some more info that may help. Over the weekend, I had also explicitly set the firestore emulator env variable in my .zshrc:

export FIRESTORE_EMULATOR_HOST="localhost:8080"

When I remove it, I get the error. And this is with the command firebase emulators:start

noelmansour avatar Jul 01 '19 19:07 noelmansour

@noelmansour can you run that command with --debug and attach the logs here as a txt file?

samtstern avatar Jul 01 '19 19:07 samtstern

@samtstern sometimes I was using the emulator to serve an HTTP function locally which sets some data in production (usually to do some data migration). I'm using:

firebase serve --only functions --project myProjectAlias

I'm not sure if it was supposed to work like that (allowing us to write data to production) but it used to work. 😅

ceolinwill avatar Jul 01 '19 19:07 ceolinwill

@wceolin it wasn't supposed to work like that with bare initializeApp() in versions after 6.8.0. I also have that use case but we want a find a way to make that opt-in so that the default is to protect production.

But I am glad to know where this error comes from now!

samtstern avatar Jul 01 '19 19:07 samtstern