gpt4 book ai didi

python - 无法执行 Cloud Function 触发不允许未经身份验证的调用的 HTTP 触发 Cloud Function?

转载 作者:行者123 更新时间:2023-12-05 07:10:55 25 4
gpt4 key购买 nike

我有一种情况,我正在尝试创建两个 Cloud Functions,即 CF1 和 CF2,并且我有一个 Cloud Scheduler。这两个云功能都启用了经过身份验证的调用。我的流程是 Cloud Scheduler 将触发 CF1。 CF1 完成后,CF1 将触发 CF2 作为 http 调用。我提到了Cannot invoke Google Cloud Function from GCP Scheduler从 Cloud Scheduler 访问经过身份验证的 CF1 并能够访问 CF1。但是从 CF1 访问 CF2 时出现问题。 CF1 不会触发 CF2,也不会给出任何错误消息。从另一个经过身份验证的 Cloud Function 访问经过身份验证的 Cloud Function 时,我们是否需要遵循任何其他技术。

CF1代码:

import json
import logging
from requests_futures.sessions import FuturesSession


def main(request):
# To read parameter values from request (url arguments or Json body).
raw_request_data = request.data
string_request_data = raw_request_data.decode("utf-8")
request_json: dict = json.loads(string_request_data)

request_args = request.args

if request_json and 'cf2_endpoint' in request_json:
cf2_endpoint = request_json['cf2_endpoint']
elif request_args and 'cf2_endpoint' in request_args:
cf2_endpoint = request_args['cf2_endpoint']
else:
cf2_endpoint = 'Invalid endpoint for CF2'

logger = logging.getLogger('test')
try:
session = FuturesSession()
session.get("{}".format(cf2_endpoint))
logger.info("First cloud function executed successfully.")

except RuntimeError:
logger.error("Exception occurred {}".format(RuntimeError))

CF2代码:

import logging

def main(request):
logger = logging.getLogger('test')
logger.info("second cloud function executed successfully.")

当前输出日志:

First cloud function executed successfully.

预期输出日志:

First cloud function executed successfully.
second cloud function executed successfully.

注意:如果我对这两个云功能使用未经身份验证的访问,则相同的流程有效。

最佳答案

这里发生了两件事:

  1. 您没有完全正确地使用request-futures。由于请求是异步发出的,因此您需要在函数隐式返回之前阻塞结果,否则它可能会在您的 HTTP 请求完成之前返回(尽管在本例中可能是这样):
session = FuturesSession()
future = session.get("{}".format(cf2_endpoint))
resp = future.result() # Block on the request completing
  1. 您向第二个函数发出的请求实际上并不是经过身份验证的请求。来自 Cloud Functions 的出站请求默认情况下不经过身份验证。如果您查看上面的实际响应,您会看到:
>>> resp.status_code
403
>>> resp.content
b'\n<html><head>\n<meta http-equiv="content-type" content="text/html;charset=utf-8">\n<title>403 Forbidden</title>\n</head>\n<body text=#000000 bgcolor=#ffffff>\n<h1>Error: Forbidden</h1>\n<h2>Your client does not have permission to get URL <code>/function_two</code> from this server.</h2>\n<h2></h2>\n</body></html>\n'

您可以跳过很多环节来正确验证此请求,如文档中所述:https://cloud.google.com/functions/docs/securing/authenticating#function-to-function

但是,更好的替代方法是让您的第二个函数成为“后台”函数,并通过从第一个函数发布的 PubSub 消息调用它:

from google.cloud import pubsub

publisher = pubsub.PublisherClient()
topic_name = 'projects/{project_id}/topics/{topic}'.format(
project_id=<your project id>,
topic='MY_TOPIC_NAME', # Set this to something appropriate.
)

def function_one(request):
message = b'My first message!'
publisher.publish(topic_name, message)

def function_two(event, context):
message = event['data'].decode('utf-8')
print(message)

只要您的函数具有发布 PubSub 消息的权限,这就避免了对 HTTP 请求添加授权的需要,并且还确保了至少一次传递。

关于python - 无法执行 Cloud Function 触发不允许未经身份验证的调用的 HTTP 触发 Cloud Function?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61055972/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com