Facing challenge to invoke cloud Function from cloud task using oidcToken
Asked Answered
C

1

1

I am facing challenge to invoke cloud Function from cloud task using oidcToken.

Here are details of my IAM & Code:

const { CloudTasksClient } = require('@google-cloud/tasks');
const client = new CloudTasksClient();

//See https://cloud.google.com/tasks/docs/tutorial-gcf
module.exports = async (payload, scheduleTimeInSec) => {
  const project = process.env.GOOGLE_APPLICATION_PROJECTID;
  const queue = process.env.QUEUE_NAME;
  const location = process.env.QUEUE_LOCATION;
  const callBackUrl = https://asia-south2-trial-288318.cloudfunctions.net/cloud-function-node-expres/;

  // Construct the fully qualified queue name.
  const parent = client.queuePath(project, location, queue);

  const body = Buffer.from(JSON.stringify(payload)).toString('base64');

  const task = {
    httpRequest: {
      httpMethod: 'POST',
      url: callBackUrl,
      headers: { 'Content-Type': 'application/json' },
      body
    },
    scheduleTime: {
      seconds: scheduleTimeInSec,
    }
  };

  if (process.env.GOOGLE_APPLICATION_SERVICE_ACCOUNT_EMAIL) {
    task.httpRequest.oidcToken = {
      serviceAccountEmail: process.env.GOOGLE_APPLICATION_SERVICE_ACCOUNT_EMAIL
    }
  }

  const request = {
    parent: parent,
    task: task,
  };

  // Send create task request.
  try {
    let [responses] = await client.createTask(request);

    return ({ sts: true, taskName: responses.name, msg: "Email Schedule Task Created" })
  }
  catch (e) {
    return ({ sts: true, err: true, errInfo: e, msg: "Unable to Schedule Task. Internal Error." })
  }
}

The process.env.GOOGLE_APPLICATION_SERVICE_ACCOUNT_EMAIL has Cloud Functions Invoker role and the Cloud Function has allAuthenticatedUsers member with role Cloud Functions Invoker as per the doc.

But still I am seeing the 401 resposnse recevied by Cloud Task and Cloud Function is not getting called(See below image):

enter image description here

Any comment on this, whats going wrong here

Channing answered 17/11, 2020 at 3:44 Comment(0)
S
2

This seems to be related that you have created the function in Firebase (guessing from the url). Seems the "Cloud Functions Invoker" is not enough for Firebase functions. I have replicated similar behavior on HelloWorld function from Firebase. The error is differnet (403) but I hope it will help you to troubleshoot the same way.

After creation helloWorld in Firebase I tested it with glcoud command in following steps:

  1. Create service acount with role "Cloud Functions Invoker" or use exiting one
  2. Download key for the account in JSON.
  3. Change gcloud to act as service account:
gcloud auth activate-service-account <service-account@email> --key-file=<key-form-step-2.json>
  1. gcloud functions call helloWorld

As the result of last action I got this error:

ERROR: (gcloud.functions.call) ResponseError: status=[403], code=[Forbidden], message=[Permission 'cloudfunctions.functions.call' denied on resource 'projects/functions-asia-test-vitooh/locations/us-central1/functions/helloWorld' (or reso
urce may not exist).]

So I created custom role in IAM: Cloud Functions Invoker + Firebase adding permission from the error massage cloudfunctions.functions.call.

The function started to work with the same gcloud functions call:

executionId: 3fgndpolu981
result: Hello from Firebase!

I think it will work as well. You can try add the same permission. If it wont work, try the same testing.

References:

Snout answered 17/11, 2020 at 12:46 Comment(2)
@vitooth, Thank you so much for taking your valuable time and creating examples, I will try and let you know. Sorry for the late response, I was busy with some other work.Channing
I have the same problem but still not working, any workaround with this?Hope

© 2022 - 2025 — McMap. All rights reserved.