简体   繁体   English

AppEngine / Python和开发AppServer上的Google KMS

[英]Google KMS on AppEngine/Python & Development AppServer

It's not clear from the documentation how one might wield Google Key Management System (KMS) on Google App Engine Standard, particularly when developing locally using the development server. 文档中尚不清楚如何在Google App Engine Standard上使用Google密钥管理系统(KMS),尤其是在使用开发服务器进行本地开发时。

It would appear as reasonably straightforward as: 它看起来像这样简单合理:

  1. Installing google-api-python-client in a Python virtual env (and adding the virtualenv path with google.appengine.ext.vendor in appengine_config.py ) 在Python虚拟环境中安装google-api-python-client (并在appengine_config.py使用google.appengine.ext.vendor添加virtualenv路径)
  2. importing googleapiclient.discovery 导入googleapiclient.discovery
  3. getting the application identity with google.appengine.api.app_identity 使用google.appengine.api.app_identity获取应用程序身份
  4. Using the kms client in the anticipated / documented way 以预期/记录的方式使用kms客户端

... then following the tutorial linked in the Documentation. ...然后按照文档中链接的教程进行操作。 However my attempts so far have not resulted in success, and it appears the documentation is wanting for a few steps. 但是,到目前为止,我的尝试并没有取得成功,并且看来该文档需要一些步骤。

It feels like I'm breaking new ground that I'm sure others must have already. 感觉我在确定别人必须已经拥有的新突破。

Has anyone documented using Google KMS on App Engine Standard & its local development server? 是否有人在App Engine Standard及其本地开发服务器上使用Google KMS进行了文档记录?

EDIT - Update with Code Example 编辑-使用代码示例更新

Here's some code that illuminates -- the problem would appear to be with my setup of default credentials. 这是一些说明性的代码-问题似乎出在我的默认凭据设置上。

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()

Now calling, via dev_appserver.py : 现在通过dev_appserver.py调用:

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

gives an error of: 给出以下错误:

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 ."> HttpError:https://cloudkms.googleapis.com/v1/projects/np-crypto/locations/global/keyRings/localhost-testing/cryptoKeys/machine-identifiers:encrypt?alt=json返回“请求具有无效的身份验证凭据。应为OAuth 2访问令牌,登录Cookie或其他有效的身份验证凭据。请参阅https://developers.google.com/identity/sign-in/web/devconsole-project 。“>

That's not especially helpful, being mostly concerned with Google Sign-In on the website; 这并不是特别有用,因为它主要与网站上的Google登录有关; however, when I import the mykms from the command line, I get the error: 但是,当我从命令行导入mykms时,出现错误:

The Application Default Credentials are not available. 应用程序默认凭据不可用。 They are available if running in Google Compute Engine. 如果它们在Google Compute Engine中运行,则可用。 Otherwise, the environment variable GOOGLE_APPLICATION_CREDENTIALS must be defined pointing to a file defining the credentials. 否则,必须定义环境变量GOOGLE_APPLICATION_CREDENTIALS,指向指向定义凭据的文件。 See https://developers.google.com/accounts/docs/application-default-credentials for more information. 有关更多信息,请参见https://developers.google.com/accounts/docs/application-default-credentials

This seems like the correct lead for now. 目前看来,这是正确的线索。 Will flush it out and report back. 将其冲洗掉并报告。

EDIT #2 编辑#2

The application seems to now connect to KMS. 该应用程序现在似乎已连接到KMS。 I deleted and re-logged into gcloud auth application-default login . 我删除并重新登录到gcloud auth application-default login

However, there's a weird side effect — something seems to be scanning the drive, and hundreds of messages (seemingly one for every accessible directory from root) like the following clutter the log: 但是,这有一个怪异的副作用-似乎正在扫描驱动器,并且有数百条消息(似乎来自root用户的每个可访问目录都包含一条消息),如下所示使日志混乱:

INFO 30 Jun 2017 20:06:57 Sandbox prevented access to file "/Users" INFO 30 Jun 2017 20:06:57沙箱阻止访问文件“ /用户”

INFO 30 Jun 2017 20:06:57 If it is a static file, check that application_readable: true is set in your app.yaml INFO 30 Jun 2017 20:06:57如果它是静态文件,请检查app.yaml中是否设置了application_readable: true

If you're developing using Cloud KMS in GAE, there isn't a local dev service, you can only talk to the main production service as you've gathered. 如果您在GAE中使用Cloud KMS进行开发,则没有本地开发服务,您只能在聚集时与主要生产服务进行交谈。 You could use the libraries as you've detailed to develop locally, but would still be hitting production. 您可以使用已详细介绍的库在本地进行开发,但仍会影响生产。

Note that you'll have to give GAE application default credentials with a scope for use, see https://cloud.google.com/kms/docs/accessing-the-api#google_app_engine 请注意,您必须为GAE应用程序提供默认凭据以及使用范围,请参阅https://cloud.google.com/kms/docs/accessing-the-api#google_app_engine

You can also make requests as the GAE service account if you use gcloud iam service-accounts keys and gcloud auth activate-service-account . 如果您使用gcloud iam service-accounts keysgcloud auth activate-service-account也可以将请求作为GAE服务帐户gcloud iam service-accounts keys

In general, for a dev environment, you might want to segment this as a separate KeyRing (or even a separate project) from your production resources. 通常,对于开发环境,您可能希望将其与生产资源分开作为单独的KeyRing(甚至是单独的项目)。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM