Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Authentication error in tasks firebase emulator on enqueue #2754

Open
jootten opened this issue Nov 4, 2024 · 2 comments
Open

Authentication error in tasks firebase emulator on enqueue #2754

jootten opened this issue Nov 4, 2024 · 2 comments
Assignees

Comments

@jootten
Copy link

jootten commented Nov 4, 2024

Environment

  • Operating System version: Fedora Linux 40
  • Firebase SDK version: 6.0.1
  • Firebase Product: auth, tasks, emulator
  • Node.js version: 18
  • NPM version: 10.2.3
  • Firebase Tools: 13.23.1

Problem

Authentication fails in the firebase tasks emulator on

await queue.enqueue(...)  \\ full sample below

with the following error:
Error scheduling task FirebaseAppError: Credential implementation provided to initializeApp() via the "credential" property failed to fetch a valid Google OAuth2 access token with the following error: "Error fetching access token: Error while making request: getaddrinfo ENOTFOUND metadata.google.internal. Error code: ENOTFOUND".

We were able to reproduce the bug reported here firebase/firebase-tools#7821 with the latest SDK and firebase tools versions.

The error does not occur when application default credentials are available and is thus easily overlooked.

Steps to reproduce:

  1. Setup firebase project as in Cloud Tasks Emulator Error: Error fetching access token: invalid_grant (reauth related error (invalid_rapt)) firebase-tools#7821.
  2. Start the emulator with the code below.
npm run build && npx firebase emulators:start --project='demo-project' --debug
  1. Enqueue a task by executing the testOnRequest function (http://127.0.0.1:5001/demo-project/us-central1/testOnRequest).

emulator-debug.log

Relevant Code:

Reproducible example that is even more minimal than in the mentioned issue:

import { onRequest } from "firebase-functions/v2/https";
import { getFunctions } from "firebase-admin/functions";
import { onTaskDispatched } from "firebase-functions/v2/tasks";
import * as logger from "firebase-functions/logger";
import { initializeApp } from "firebase-admin/app";

// Initialize the Firebase app
initializeApp()

// The http function
export const testOnRequest = onRequest(async (request, response) => {
  const taskPayload = {
    foo: "bar",
  };

  const taskFunctionName = `testOnTaskDispatched`;
  const queue = getFunctions().taskQueue(taskFunctionName);

  try {
    await queue.enqueue(taskPayload);
    response.send("Success. Hello from HTTP onRequest!");
  } catch (error) {
    console.error("Error scheduling task", error);
    response.status(500).send("Error scheduling task");
    return;
  }
});

// The task function
export const testOnTaskDispatched = onTaskDispatched( (request) => {
  logger.info("Success. Hello logs from TASKS onTaskDispatched!", {
    foo: request.data,
  });
});
@exetico
Copy link

exetico commented Feb 5, 2025

I'm facing the same problem. Running locally with default credentials works just fine. But running in my testing env. for Jest like Github Workflows, things does not work, as it does expect a Google OAuth2 access token. But no default credentials are set in the test-env, ofc.

Credential implementation provided to initializeApp() via the "credential" property failed to fetch a valid Google OAuth2 access token with the following error: "Could not load the default credentials. Browse to https://cloud.google.com/docs/authentication/getting-started for more information.

There's now five votes to be found in the submission by @jootten.

I've created another MCVE with a Jest test, inspired by one of my codebases:
https://github.com/exetico/issues-2754

See tests here. And here's one workflow result with a Jest test report.

The solution works just fine locally, with default credentials set, but I expect this to work in a GitHub Workflow too, as I'm using a demo-tagget project (docs link).

@lahirumaramba & @blidd-google; Are you able to push this forward, by any chance?

@lahirumaramba lahirumaramba self-assigned this Feb 5, 2025
@exetico
Copy link

exetico commented Feb 10, 2025

Hi @lahirumaramba,

I noticed that you've self-assigned this issue — thank you for looking into it! I just wanted to check in and see if there's any update on the progress. Let me know if you need any additional information from my side.

Thanks in advance!

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

No branches or pull requests

5 participants