Google AppEngine获取403禁止尝试更新cron.yaml

时间:2019-02-11 10:45:59

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

我正在使用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>').

我检查了与此相关的其他帖子,但是它们似乎处理的是旧版本/ appengine的部署

服务帐户!

Service Account Permissions

2 个答案:

答案 0 :(得分:2)

好了,经过一些修补。我将项目编辑者角色添加到了与运行服务器的GCE实例链接的服务帐户中。我不完全知道这是否是拥有最小权限的角色,才能使它正常工作。

enter image description here

答案 1 :(得分:1)

来自Deploying using IAM roles

  

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

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

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

           

      a。设置以下角色之一:

           
          
      • 使用 App Engine> App Engine Deployer 角色,以允许该帐户部署应用程序的版本。
      •   
      • 要同时允许将dos.yamldispatch.yaml文件与应用程序一起部署,请使用 App Engine> App Engine管理员角色   代替。
      •   
           

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

           

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

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

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

可能感兴趣的地方: