【问题标题】:Facing challenge to invoke cloud Function from cloud task using oidcToken面临使用 oidcToken 从云任务调用云函数的挑战
【发布时间】:2020-11-17 03:44:00
【问题描述】:

我面临使用 oidcToken 从云任务调用云函数的挑战。

以下是我的 IAM 和代码的详细信息:

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." })
  }
}

根据doc.

但我仍然看到 Cloud Task 收到 401 响应,并且 Cloud Function 没有被调用(见下图):

对此有任何评论,这里出了什么问题

【问题讨论】:

    标签: google-cloud-functions google-iam google-cloud-iam google-cloud-tasks


    【解决方案1】:

    这似乎与您在 Firebase 中创建了该函数有关(从 url 猜测)。似乎“Cloud Functions Invoker”对于 Firebase 功能来说是不够的。我在 Firebase 的 HelloWorld 函数上复制了类似的行为。错误是不同的(403),但我希望它可以帮助您以相同的方式进行故障排除。

    在 Firebase 中创建 helloWorld 后,我使用glcoud 命令按以下步骤对其进行了测试:

    1. 使用角色“Cloud Functions Invoker”创建服务帐户或使用现有服务帐户
    2. 以 JSON 格式下载帐户的密钥。
    3. gcloud 更改为服务帐号:
    gcloud auth activate-service-account <service-account@email> --key-file=<key-form-step-2.json>
    
    1. gcloud functions call helloWorld

    作为最后一个操作的结果,我收到了这个错误:

    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).]
    

    所以我在 IAM 中创建了自定义角色:Cloud Functions Invoker + Firebase 从错误消息cloudfunctions.functions.call 添加权限。

    函数开始使用相同的gcloud functions call

    executionId: 3fgndpolu981
    result: Hello from Firebase!
    

    我认为它也会起作用。您可以尝试添加相同的权限。如果它不起作用,请尝试相同的测试。

    参考资料:

    【讨论】:

    • @vitooth,非常感谢您抽出宝贵的时间并创建示例,我会尽力让您知道。抱歉回复晚了,我正忙于其他工作。
    猜你喜欢
    • 1970-01-01
    • 2019-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-04
    • 2014-08-25
    • 1970-01-01
    相关资源
    最近更新 更多