使用不同服务帐号时,云函数无法调用另一个云函数

问题描述 投票:0回答:1

我有 2 个不同的云功能。当前设置如下:

  • 云调度器触发功能A
  • 函数 A 使用 pub/sub 在脚本末尾触发函数 B
  • 这两个云功能都是第二代并且需要身份验证
  • 这两个功能使用不同的服务帐户
  • 我设置了一个名为
    db-topic
  • 的发布/订阅主题

发布消息部分的功能A工作正常。但是当我检查函数 B 的日志时,出现以下错误:

The request was not authenticated. Either allow unauthenticated invocations or set the proper Authorization header

我创建了 2 个不同的云函数,如下所示: 部署http云函数A:

gcloud functions deploy db-func-pubsub-test \
--runtime=python310 \
--trigger-http \
--entry-point=start_script \
--region=europe-west3 \
--max-instances=1 \
--timeout=3500s \
--memory=1GiB \
--service-account=cloud-function-a@propane-nomad-396712.iam.gserviceaccount.com \
--ingress-settings=all \
--no-allow-unauthenticated \
--gen2 \
--source=./cloud_functions/code/6/publisher/

部署pubsub云函数B:

gcloud functions deploy func-pubsub-subscriber-test \
--runtime=python310 \
--trigger-topic=db-topic \
--entry-point=pubsub_handler \
--region=europe-west3 \
--max-instances=1 \
--timeout=60s \
--memory=256MiB \
--service-account=cloud-function-b@propane-nomad-396712.iam.gserviceaccount.com \
--ingress-settings=all \
--no-allow-unauthenticated \
--gen2 \
--source=./cloud_functions/code/6/subscriber/

并授予权限允许函数A调用函数B:

gcloud functions add-invoker-policy-binding func-pubsub-subscriber-test \
    --member="serviceAccount:[email protected]" \
    --region='europe-west3'

不知道为什么函数A没有权限调用函数B?我使用相同的方法授予云调度程序调用函数 a 的访问权限,效果很好。 此外,如果我对两个云功能使用相同的服务帐户,它也可以正常工作。

编辑: 该代码可以 100% 正常工作,因为如果我对两个云功能使用相同的服务帐户,整个设置就可以工作,但无论如何我都会提供我的代码,以防万一。

功能A代码:

# Publish a message to the Pub/Sub topic
    publisher = pubsub_v1.PublisherClient()
    topic_path = f'projects/{project_id}/topics/{topic_name}'
    data = {"project_id": project_id, "bucket_name": bucket_name, "prefix_path": prefix_path}
    data_str = json.dumps(data).encode('utf-8')  # Convert dictionary to JSON-encoded bytestring
    publisher.publish(topic_path, data=data_str)
    print(f"Printing json message being published: {data}")

功能B代码:

def pubsub_handler(event, context):
    # Retrieve the JSON payload from the Pub/Sub message
    pubsub_message = event['data']
    decoded_message = base64.b64decode(pubsub_message).decode('utf-8')  # Decode and convert to string
    print(f"Printing pubsub message received: {decoded_message}")
    data_dict = json.loads(decoded_message)

这里是授予功能A的服务帐户的所有角色。

编辑2: 所以我只是注意到功能A的服务帐户实际上没有发布消息的权限。这就是为什么我在函数 A 日志中遇到错误。我通过运行解决了这个问题:

gcloud projects add-iam-policy-binding propane-nomad-396712 --member=serviceAccount:[email protected] --role=roles/pubsub.publisher

然后我还注意到我可能没有授予功能 B 的服务帐户任何接收 pub/sub 消息的权限。所以我运行了以下命令:

gcloud projects add-iam-policy-binding propane-nomad-396712 --member=serviceAccount:[email protected] --role=roles/pubsub.subscriber

我现在可以使用函数A成功发布消息了。但我在函数 B 中仍然遇到与上面提到的相同的错误

google-cloud-platform google-cloud-functions publish-subscribe google-cloud-iam
1个回答
0
投票

好吧,我设法让它工作了。归根结底,需要做三件事才能使其发挥作用:

  1. 授予服务帐号A发布pub/sub消息的权限
gcloud projects add-iam-policy-binding propane-nomad-396712 \
  --member=serviceAccount:[email protected] \
  --role=roles/pubsub.publisher
  1. 授予服务帐号B接收pub/sub消息的权限
gcloud projects add-iam-policy-binding propane-nomad-396712 \
  --member=serviceAccount:[email protected] \
  --role=roles/pubsub.receiver
  1. 授予服务帐号B调用云功能B的权限
gcloud functions add-invoker-policy-binding func-pubsub-subscriber-test \
    --member="serviceAccount:[email protected]" \
    --region='europe-west3'
© www.soinside.com 2019 - 2024. All rights reserved.