【问题标题】:Google Cloud Tasks trigger Cloud Function with INTERNAL only ingressGoogle Cloud Tasks 触发 Cloud Functions with INTERNAL only ingress
【发布时间】:2020-11-10 13:52:42
【问题描述】:

当 Cloud Tasks 的入口设置设置为“仅允许内部”时,如何从 Cloud Tasks 触发 Cloud Function?

使用该设置,该函数会拒绝来自 Cloud Tasks 的传入 HTTP 流量。 即使 Cloud Tasks 与函数位于同一项目中,也会发生此行为。

【问题讨论】:

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


    【解决方案1】:

    这很有趣,因为我在本周二向 Google PM 询问了这个问题! 因为今天你不能!它在 PM 的雷达中,没有时间表,但有可能,一天。

    我今天的解决方案。

    如果我在内部和外部使用仅限内部模式的云功能(或由不符合 VPC 连接器的 Google 无服务器产品,如 Cloud Task、Cloud Scheduler、PubSub 和 Workflows),我创建一个“代理功能”

    • 此代理功能以ingress=all模式部署,并带有no-allow-unauthenticated参数
    • 我只授予外部产品的服务帐户作为代理功能上的 cloudfunctions.invoker 以确保只有此服务帐户才能调用代理功能
    • 我创建了一个serverless VPC connector 并将其添加到代理函数中
    • 代理函数只调用内部函数。

    【讨论】:

      【解决方案2】:

      你好@guillaume 和@Thomas, 我面临从云任务调用云功能的挑战。 以下是我的 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 没有被调用(见下图):

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

      【讨论】:

      • 请将此作为单独的问题提出
      • 您好@thomas-ruble & guillaume,对于给您带来的不便,我们深表歉意。你能帮我解决我问过的类似问题吗:stackoverflow.com/q/64869054/3076704
      猜你喜欢
      • 2021-02-04
      • 2018-03-27
      • 2020-05-11
      • 1970-01-01
      • 2019-04-12
      • 2020-01-26
      • 1970-01-01
      • 1970-01-01
      • 2021-07-15
      相关资源
      最近更新 更多