and*_*ddt 3 google-cloud-platform google-cloud-functions google-cloud-scheduler
我已经成功部署了一个云函数,它对一些数据进行了一些基本的预处理并将其上传到 gSheet。
现在,触发器 url 接受外部未经身份验证的调用,如果 url 最终落入坏人之手,则会导致产生过多账单的风险。
我想知道是否有任何方法可以限制对 IAM 中 Cloud Scheduler 的调用,从而完全阻止对服务的外部调用。
阅读似乎在请求中包含一些标头并在函数中检查它可能是强制执行真正基本身份验证的基本方法。
为了防止外部未经身份验证的调用,您可以将您的功能设置为私有。很容易做到,用--no-allow-unauthenticated参数部署它
gcloud functions deploy my-function --no-allow-unauthenticated --trigger... -- region... --runtime...
Run Code Online (Sandbox Code Playgroud)
但是现在,调度程序不能调用它。现在你必须做两件事
# Create the service account
gcloud iam service-accounts create your-service-account-name
# Grant the role for calling the function
gcloud functions add-iam-policy-binding \
--member=serviceAccount:your-service-account-name@YOUR_PROJECT_ID.iam.gserviceaccount.com \
--role=roles/cloudfunctions.invoker your-function-name
Run Code Online (Sandbox Code Playgroud)
使用 GUI,如果您cloudfunctions.invoker在项目级别授予角色,您的服务帐户将能够访问您项目中的所有功能。使用我的命令行,我只授予特定功能的角色。您可以通过控制台执行此操作,方法是转到功能列表,选择一个功能(复选框)并单击show info panel。在这里你有一个权限选项卡
gcloud scheduler jobs create http your-job name --schedule="0 0 * * *" \
--uri=your-function-URI \
--oidc-service-account-email=your-service-account-name@YOUR_PROJECT_ID.iam.gserviceaccount.com
Run Code Online (Sandbox Code Playgroud)
如果它不起作用,那是因为您的云调度程序服务代理无权使用服务帐户生成令牌。
gcloud projects add-iam-policy-binding YOUR_PROJECT_ID \
--member=serviceAccount:service-[project-number]@gcp-sa-cloudscheduler.iam.gserviceaccount.com \
--role roles/cloudscheduler.serviceAgent
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1726 次 |
| 最近记录: |