您无法隐藏您的 Cloud Functions 端点。在任何配置中,它都将是公开可见的。
但是,您可以限制谁有权访问。在您的情况下,以安全模式部署云功能(设置参数 --no-allow-unauthenticated 或从权限部分删除 allUsers)
然后,部署您的具有自定义(后端)服务帐户的 API 网关 https://cloud.google.com/api-gateway/docs/creating-api-config#creating-an-api-config。授予此服务帐户调用 Cloud Functions 的权限(角色:cloudfunctions.invoker)。
实现此目标后,仅允许 API 网关身份访问您的 Cloud Functions。用户将能够查看并请求 Cloud Functions URL,但他们将收到 403 或 401 错误。
EDIT 1
经过测试,对于 Cloud Functions(我在 Cloud Run 中没有遇到过这种情况),Cloud Functions 生成的目标受众是错误的,您在后端使用了附加路径。这是我的conf
/function:
get:
summary: Greet a user
operationId: function
x-google-backend:
address: https://us-central1-gdglyon-cloudrun.cloudfunctions.net/gdg-go
responses:
'200':
description: A successful response
schema:
type: string
/function-path:
get:
summary: Greet a user
operationId: function-path
x-google-backend:
address: https://us-central1-gdglyon-cloudrun.cloudfunctions.net/gdg-go/path
jwt_audience: https://us-central1-gdglyon-cloudrun.cloudfunctions.net/gdg-go
responses:
'200':
description: A successful response
schema:
type: string
The /function
使用云函数的根路径,直接调用没有问题。
The /function-path
add /path
到 Cloud Functions 的根路径。我猜 API Gateway 使用了相同的完整 URL(带有/path
在最后)这是该功能的错误受众。
您可以使用以下命令覆盖它jwt_audience
范围。