oidcToken을 사용하여 클라우드 작업에서 클라우드 함수를 호출하는 문제에 직면

Nov 16 2020

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

process.env.GOOGLE_APPLICATION_SERVICE_ACCOUNT_EMAIL에는 Cloud Functions Invoker 역할이 있고 Cloud Function에는 문서에 따라 Cloud Functions Invoker 역할 이있는 allAuthenticatedUsers 구성원이 있습니다.

하지만 여전히 Cloud Task에 의해 수신 된 401 resposnse가 표시되고 Cloud Function이 호출되지 않습니다 (아래 이미지 참조).

이것에 대한 의견, 여기에서 무슨 일이 일어나고 있는지

답변

1 vitooh Nov 17 2020 at 12:46

이것은 Firebase에서 함수를 만든 것과 관련이있는 것 같습니다 (URL에서 추측). Firebase 함수에는 'Cloud Functions Invoker'가 충분하지 않은 것 같습니다. Firebase의 HelloWorld 함수에서 유사한 동작을 복제했습니다. 오류는 differentnet (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!

나는 그것이 잘 될 것이라고 생각한다. 동일한 권한을 추가 할 수 있습니다. 작동하지 않으면 동일한 테스트를 시도하십시오.

참조 :

  • gcloud auth 명령어
  • Cloud IAM에서 커스텀 역할 만들기
  • gcloud 함수 호출