自动允许未经身份验证的用户云运行实例

时间:2020-07-08 07:55:21

标签: google-cloud-run

您正在开发一种解决方案,该解决方案使用其REST API和OAuth作为为此目的而创建的服务帐户,来创建服务并将其部署到Google云运行。

我一直坚持将创建的服务公开提供。

我找不到与 gcloud 一样的相应--allow-unauthenticated参数,无法从API中使用。

我发现的唯一方法是,在我希望公开访问的每个服务上,将allUsers手动添加为 Cloud Run Invoker 。但是,我希望该 service-account 中的所有服务都可以自动公开获得。

我想知道是否有更好(自动)的方法来实现这一目标。

谢谢。

1 个答案:

答案 0 :(得分:3)

首先,您不能仅使用一个命令来执行此操作。您必须部署服务,然后授予服务上的allUsers。 CLI为您方便地完成了这两个步骤。

无论如何,当您陷入这样的困境时,有一个有用的技巧:在您的gcloud命令中添加--log-http。这样,您将看到由CLI执行的所有HTTP API调用。

如果在部署新的Cloud Run服务时执行此操作,则将获得大量支持,并且此刻,您将拥有

==== request start ====
uri: https://run.googleapis.com/v1/projects/gbl-imt-homerider-basguillaueb/locations/us-central1/services/predict2:setIamPolicy?alt=json
method: POST
== headers start ==
b'Authorization': --- Token Redacted ---
b'X-Goog-User-Project': b'gbl-imt-homerider-basguillaueb'
b'accept': b'application/json'
b'accept-encoding': b'gzip, deflate'
b'content-length': b'98'
b'content-type': b'application/json'
b'user-agent': b'google-cloud-sdk gcloud/299.0.0 command/gcloud.run.deploy invocation-id/61070d063a604fdda8e87ad63777e3ae environment/devshell environment-version/None interactive/True from-script/False python/3.7.3 term/screen (Linux 4.19.112+
)'
== headers end ==
⠹ Deploying new service...
{"policy": {"bindings": [{"members": ["allUsers"], "role": "roles/run.invoker"}], "etag": "ACAB"}}
== body end ==
  ⠹ Setting IAM Policy...
---- response start ----
status: 200
-- headers start --
-content-encoding: gzip
cache-control: private
content-length: 159
content-type: application/json; charset=UTF-8
date: Wed, 08 Jul 2020 11:37:11 GMT
server: ESF
transfer-encoding: chunked
vary: Origin, X-Origin, Referer
x-content-type-options: nosniff
x-frame-options: SAMEORIGIN
x-xss-protection: 0
-- headers end --
-- body start --
{
  "version": 1,
  "etag": "BwWp7IdZGHs=",
  "bindings": [
    {
      "role": "roles/run.invoker",
      "members": [
        "allUsers"
      ]
    }
  ]
}

因此,这是一个额外的API调用,可以为您执行CLI。您可以找到here the API definition

如果您想手动执行呼叫,则可以发出这样的请求

curl -H "Authorization: Bearer $(gcloud auth print-access-token)" \
     -H "content-type: application/json" -X POST \
     -d '{"policy": {"bindings": [{"members": ["allUsers"], "role": "roles/run.invoker"}]}}' \     
     "https://run.googleapis.com/v1/projects/<PROJECT_ID>/locations/<REGION>/services/<SERVICE_NAME>:setIamPolicy"