Google AppEngine/Python 上的 KMS 和开发 AppServer

Google KMS on AppEngine/Python & Development AppServer

the documentation 中不清楚如何在 Google App Engine Standard 上使用 Google 密钥管理系统 (KMS),尤其是在使用开发服务器进行本地开发时。

它看起来相当简单:

  1. 在 Python 虚拟环境中安装 google-api-python-client(并在 appengine_config.py 中添加带有 google.appengine.ext.vendor 的虚拟环境路径)
  2. 导入googleapiclient.discovery
  3. 使用 google.appengine.api.app_identity
  4. 获取应用程序标识
  5. 以预期/记录的方式使用 kms 客户端

... 然后按照文档中链接的教程进行操作。然而,到目前为止我的尝试还没有成功,文档似乎还需要一些步骤。

感觉我正在开辟新天地,我相信其他人一定已经有了。

是否有人记录了在 App Engine 标准版及其本地开发服务器上使用 Google KMS?

编辑 - 使用代码示例进行更新

这里有一些代码可以说明问题——问题似乎出在我的默认凭据设置上。

mykms.py

import googleapiclient.discovery
from google.appengine.api import app_identity

from oauth2client.client import GoogleCredentials
credentials = GoogleCredentials.get_application_default()

PROJECT = 'my-crypto-project'
IS_LOCAL = True
LOCATION = 'global'
TESTING_KR = 'testing-keyring'
KEY_RING = TESTING_KR if IS_LOCAL else app_identity.get_application_id()

kms = googleapiclient.discovery.build('cloudkms', 'v1', credentials=credentials)

def encrypt(plaintext, cryptokey, keyring=KEY_RING, location=LOCATION):
    name = 'projects/{}/locations/{}/keyRings/{}/cryptoKeys/{}'.format(
        PROJECT, location, keyring, cryptokey
    )
    cryptokeys = kms.projects().locations().keyRings().cryptoKeys()
    request = cryptokeys.encrypt(name=name, body={'plaintext': plaintext})
    return request.execute()


def decrypt(ciphertext, cryptokey, keyring=KEY_RING, location=LOCATION):
    name = 'projects/{}/locations/{}/keyRings/{}/cryptokey'.format(
        PROJECT, location, keyring
    )
    cryptokeys = kms.projects().locations().keyRings().cryptoKeys()
    request = cryptokeys.decrypt(name=name, body={'ciphertext': ciphertext})
    return request.execute()

正在呼叫,通过dev_appserver.py:

import mykms
mykms.encrypt("my text", cryptokey="my-key-ring")

给出错误:

HttpError: https://cloudkms.googleapis.com/v1/projects/np-crypto/locations/global/keyRings/localhost-testing/cryptoKeys/machine-identifiers:encrypt?alt=json returned "Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.">

这不是特别有用,主要是 Google 在网站上登录;但是,当我从命令行导入 mykms 时,出现错误:

The Application Default Credentials are not available. They are available if running in Google Compute Engine. Otherwise, the environment variable GOOGLE_APPLICATION_CREDENTIALS must be defined pointing to a file defining the credentials. See https://developers.google.com/accounts/docs/application-default-credentials for more information.

目前这似乎是正确的线索。会把它冲掉并报告回来。

编辑 #2

应用程序似乎现在连接到 KMS。我删除并重新登录 gcloud auth application-default login

但是,有一个奇怪的副作用 — 似乎有什么东西在扫描驱动器,并且像下面这样的数百条消息(似乎一条消息对应根目录中的每个可访问目录)使日志变得混乱:

INFO 30 Jun 2017 20:06:57 Sandbox prevented access to file "/Users"

INFO 30 Jun 2017 20:06:57 If it is a static file, check that application_readable: true is set in your app.yaml

如果您在 GAE 中使用 Cloud KMS 进行开发,则没有本地开发服务,您只能与收集到的主要生产服务通信。您可以使用您详细介绍的库在本地进行开发,但仍会影响生产。

请注意,您必须为 GAE 应用程序提供默认凭据以及使用范围,请参阅 https://cloud.google.com/kms/docs/accessing-the-api#google_app_engine

如果您使用 GAE 服务帐户,您也可以提出请求 gcloud iam service-accounts keysgcloud auth activate-service-account.

一般来说,对于开发环境,您可能希望将其从您的生产资源中分割为一个单独的 KeyRing(甚至一个单独的项目)。