【问题标题】:Google AppEngine Getting 403 forbidden trying to update cron.yamlGoogle AppEngine 获取 403 禁止尝试更新 cron.yaml
【发布时间】:2019-07-04 20:14:48
【问题描述】:

我正在使用 AppEngine 关注 docs on how to backup datastore。 我正在 GCE VM 上执行 gcloud app deploy cron.yaml 命令,该命令旨在更新 AppEngine 中的 cronjob。 GCE VM 和 AppEngine cron 在同一个项目中,我已通过默认服务帐户将 AppEngine 管理员授予 GCE VM。当我在本地机器上运行它时,它更新得很好。然而在 GCE 实例上,问题就出现了

这是文件

app.yaml

runtime: python27
api_version: 1
threadsafe: true
service: cloud-datastore-admin
libraries:
- name: webapp2
  version: "latest"
handlers:
- url: /cloud-datastore-export
  script: cloud_datastore_admin.app
  login: admin

cron.yaml

cron:
- description: "Daily Cloud Datastore Export"
  url: /cloud-datastore-export?namespace_id=&output_url_prefix=gs://<my-project-id>-bucket
  target: cloud-datastore-admin
  schedule: every 24 hours

cloud_datastore_export.yaml

import datetime
import httplib
import json
import logging
import webapp2
from google.appengine.api import app_identity
from google.appengine.api import urlfetch

class Export(webapp2.RequestHandler):
  def get(self):
    access_token, _ = app_identity.get_access_token(
        'https://www.googleapis.com/auth/datastore')
    app_id = app_identity.get_application_id()
    timestamp = datetime.datetime.now().strftime('%Y%m%d-%H%M%S')
    output_url_prefix = self.request.get('output_url_prefix')
    assert output_url_prefix and output_url_prefix.startswith('gs://')
    if '/' not in output_url_prefix[5:]:
      # Only a bucket name has been provided - no prefix or trailing slash
      output_url_prefix += '/' + timestamp
    else:
      output_url_prefix += timestamp
    entity_filter = {
        'kinds': self.request.get_all('kind'),
        'namespace_ids': self.request.get_all('namespace_id')
    }
    request = {
        'project_id': app_id,
        'output_url_prefix': output_url_prefix,
        'entity_filter': entity_filter
    }
    headers = {
        'Content-Type': 'application/json',
        'Authorization': 'Bearer ' + access_token
    }
    url = 'https://datastore.googleapis.com/v1/projects/%s:export' % app_id
    try:
      result = urlfetch.fetch(
          url=url,
          payload=json.dumps(request),
          method=urlfetch.POST,
          deadline=60,
          headers=headers)
      if result.status_code == httplib.OK:
        logging.info(result.content)
      elif result.status_code >= 500:
        logging.error(result.content)
      else:
        logging.warning(result.content)
      self.response.status_int = result.status_code
    except urlfetch.Error:
      logging.exception('Failed to initiate export.')
      self.response.status_int = httplib.INTERNAL_SERVER_ERROR
app = webapp2.WSGIApplication(
    [
        ('/cloud-datastore-export', Export),
    ], debug=True)

我遇到的错误是

Configurations to update:
descriptor:      [/usr/local/sbin/pluto/<my-project-id>/datastore/cron.yaml]
type:            [cron jobs]
target project:  [<my-project-id>]
Do you want to continue (Y/n)?  
Updating config [cron]...
failed.
ERROR: (gcloud.app.deploy) Server responded with code [403]:
  Forbidden Unexpected HTTP status 403.
  You do not have permission to modify this app (app_id=u'e~<my-project-id>').

我查看了与此相关的其他帖子,但它们似乎处理的是旧版本/应用引擎的部署

服务帐号!

【问题讨论】:

    标签: google-app-engine cron google-cloud-datastore google-compute-engine


    【解决方案1】:

    来自Deploying using IAM roles

    授予用户帐户部署到 App Engine 的能力:

    1. 单击添加成员将用户帐户添加到项目中,然后使用下拉菜单选择该帐户的所有角色:

      • 必需的角色以允许帐户部署到 App Engine:

        一个。设置以下角色之一:

        • 使用 App Engine > App Engine Deployer 角色允许帐户部署应用版本。
        • 要同时允许在应用中部署 dos.yamldispatch.yaml 文件,请使用 App Engine > App Engine Admin 角色 而是。

        用户帐户现在有足够的权限使用Admin API to deploy apps

        b.要允许使用 App Engine tooling 部署应用程序,您还必须为用户帐户授予 Storage > Storage Admin 角色 以便该工具有权上传到Cloud Storage

      • 可选。为用户帐户授予以下角色以授予上传其他配置文件的权限:

        • Cloud Scheduler > Cloud Scheduler Admin 角色:上传cron.yaml 文件的权限。

    可能感兴趣:

    【讨论】:

      【解决方案2】:

      经过一番修改后好的。我将项目编辑角色添加到链接到运行我的服务器的 GCE 实例的服务帐户中。我不完全知道这是否是使其发挥作用的最少特权的角色。

      【讨论】:

      • -1 建议添加项目编辑角色只是为了启用 AppEngine 的 cron 部署,这是对权限的大量过度提供。项目编辑器封装了 1000 多个单独的权限,因此从安全角度来看,没有人应该这样做。
      猜你喜欢
      • 2016-09-08
      • 1970-01-01
      • 2014-09-10
      • 2011-05-15
      • 2021-07-11
      • 1970-01-01
      • 1970-01-01
      • 2021-02-19
      • 2013-08-06
      相关资源
      最近更新 更多