简体   繁体   English

从云调度程序调用谷歌云 function 时获取权限被拒绝错误

[英]Getting permission denied error when calling Google cloud function from Cloud scheduler

I am trying to envoke Google cloud function which is Http triggered by cloud scheduler.我试图唤起谷歌云 function 这是由云调度程序触发的 Http。 But whenever I try to run cloud scheduler it always says Permsiion denies error但是每当我尝试运行云调度程序时,它总是说 Permsiion 否认错误

 httpRequest: {
  status: 403   
 }
 insertId: "14igacagbanzk3b"  
 jsonPayload: {
  @type: "type.googleapis.com/google.cloud.scheduler.logging.AttemptFinished"   
  jobName: "projects/***********/locations/europe-west1/jobs/twilio-cloud-scheduler"   
  status: "PERMISSION_DENIED"   
  targetType: "HTTP"   
  url: "https://europe-west1-********.cloudfunctions.net/function-2"   
 }
 logName: "projects/*******/logs/cloudscheduler.googleapis.com%2Fexecutions"  
 receiveTimestamp: "2020-09-20T15:11:13.240092790Z"  
 resource: {
  labels: {
   job_id: "***********"    
   location: "europe-west1"    
   project_id: "**********"    
  }
  type: "cloud_scheduler_job"   
 }
 severity: "ERROR"  
 timestamp: "2020-09-20T15:11:13.240092790Z"  
}

Solutions I tried -我尝试过的解决方案 -

  1. Tried putting Google cloud function in the same region as the App engine as suggested by some users.尝试按照一些用户的建议将谷歌云 function 与 App 引擎放在同一区域。
  2. Gave access to Google provided cloud schduler sa service- **** @gcp-sa-cloudscheduler.iamaccount.gserviceaccount.com owner role and Cloud Functions Admin role授予对 Google 提供的 cloud schduler sa服务的访问权限 - **** @gcp-sa-cloudscheduler.iamaccount.gserviceaccount.com所有者角色和 Cloud Functions 管理员角色
  3. My cloud function has ingress setting of Allow all traffic.我的云 function 具有允许所有流量的入口设置。

My cloud scheduler only works when I run below command我的云调度程序仅在我运行以下命令时才有效

gcloud functions add-iam-policy-binding cloud-function --member="allUsers" --role="roles/cloudfunctions.invoker" gcloud 函数 add-iam-policy-binding cloud-function --member="allUsers" --role="roles/cloudfunctions.invoker"

On Cloud Scheduler page, you have to add a service account to use to call the private Cloud Function.在 Cloud Scheduler 页面上,您必须添加一个服务帐户以用于调用私有 Cloud Function。 In the Cloud Scheduler set up, you have to在 Cloud Scheduler 设置中,您必须

  • Click on SHOW MORE on the bottom点击底部的SHOW MORE
  • Select Add OIDC token in the Auth Header sectionAuth Header部分选择Add OIDC token
  • Add a service account email in the service account email for the Scheduler在 Scheduler 的服务帐户电子邮件中添加服务帐户电子邮件
  • Fill in the Audience with the same base URL as the Cloud Functions (the URL provided when you deployed it)使用与 Cloud Functions 相同的基本 URL(部署时提供的 URL)填写 Audience

The service account email for the Scheduler must be granted with the role functions.Invoker Scheduler 的服务帐户电子邮件必须被授予角色 functions.Invoker

在此处输入图片说明

In my case the problem was related to restricted ingress setting for the cloud function.在我的情况下,问题与云功能的受限入口设置有关。 I set it to 'allow internal traffic only', but that allows only traffic from services using VPC, whereas Cloud Scheduler doesn't as per doc explanation:我将其设置为“仅允许内部流量”,但这仅允许来自使用 VPC 的服务的流量,而 Cloud Scheduler 不按照文档解释:

Internal-only HTTP functions can only be invoked by HTTP requests that are created within a VPC network, such as those from Kubernetes Engine, Compute Engine, or the App Engine Flexible Environment.仅限内部使用的 HTTP 函数只能由在 VPC 网络中创建的 HTTP 请求调用,例如来自 Kubernetes Engine、Compute Engine 或 App Engine 柔性环境的请求。 This means that events created by or routed through Pub/Sub, Eventarc, Cloud Scheduler, Cloud Tasks and Workflows cannot trigger these functions.这意味着由 Pub/Sub、Eventarc、Cloud Scheduler、Cloud Tasks 和 Workflows 创建或路由的事件无法触发这些功能。

So the proper way to do it is:所以正确的做法是:

  • set the ingress to 'all traffic'将入口设置为“所有流量”
  • remove the permission for allUsers with role Cloud Function Invoker删除角色为 Cloud Function Invoker 的 allUsers 的权限
  • add the permission for created service account with role Cloud Function Invoker为创建的具有角色 Cloud Function Invoker 的服务帐户添加权限
  • or just set that permission globally for the service account in IAM console(you could do that when creating service account as well)或者只是在 IAM 控制台中为服务帐户全局设置该权限(您也可以在创建服务帐户时这样做)

If you tried all of the above (which should be the first things to look at, such as Add OIDC token, giving your service account role Cloud Function Invoker etc.), please also check the following:如果您尝试了以上所有方法(这应该是首先要查看的内容,例如添加 OIDC 令牌,赋予您的服务帐户角色 Cloud Function Invoker 等),还请检查以下内容:

For me the only thing that fixed this, was adding the following google internal service account to IAM:对我来说,唯一解决此问题的是将以下 google 内部服务帐户添加到 IAM:
service-YOUR_PROJECT_NUMBER@gcp-sa-cloudscheduler.iam.gserviceaccount.com
And give this internal service account the following role:并赋予此内部服务帐户以下角色:
Cloud Scheduler Service Agent

See also:也可以看看:
https://cloud.google.com/scheduler/docs/http-target-auth https://cloud.google.com/scheduler/docs/http-target-auth

And especially for this case:特别是对于这种情况:
https://cloud.google.com/scheduler/docs/http-target-auth#addhttps://cloud.google.com/scheduler/docs/http-target-auth#add

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从谷歌云计算中提取 docker 的权限被拒绝 - Permission denied on docker pull from Google Cloud Compute 部署 Gen2 云时出现权限被拒绝错误 Function - Permission Denied Error while deploying Gen2 Cloud Function 从 Blazor WASM 调用 Google Cloud Function - Calling Google Cloud Function from Blazor WASM 从另一个云 Function 调用云 Function - Calling a Cloud Function from another Cloud Function 在 Google 云上部署 django 应用程序时出错“google.api_core.exceptions.PermissionDenied: 403 Permission denied on resource project” - Error in deploying django app on Google cloud "google.api_core.exceptions.PermissionDenied: 403 Permission denied on resource project " 获取公钥时出现 Google Cloud KMS 错误 - Google Cloud KMS error when getting public key Google Cloud Function Cors 仅在引发错误时出错 - Google Cloud Function Cors Error Only When Error is Thrown 使用谷歌云 SDK Windows 10 上的身份验证方法获取 Git 推送到谷歌云源存储库的权限被拒绝(公钥) - Get Permission denied (publickey) for Git push to Google Cloud Source Repository with Google Cloud SDK authentication method on Windows 10 从 Google Cloud Storage 加载 csv 文件时出现 BigQuery 错误 - BigQuery error when loading csv file from Google Cloud Storage 从云功能启动数据流管道时出错 - Error when launching dataflow pipeline from cloud function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM