AppEngine/Python&Development App上的Google KMS

2024-09-30 01:20:31 发布

您现在位置:Python中文网/ 问答频道 /正文

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

它看起来相当简单:

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

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

感觉就像我在开拓新的领域,我相信其他人肯定已经有了。在

有没有人在appenginestandard及其本地开发服务器上使用googlekms?在

编辑-用代码示例更新

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

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呼叫:

^{pr2}$

给出错误:

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


Tags: nameappapplicationgooglelocationidentityprojectscredentials
1条回答
网友
1楼 · 发布于 2024-09-30 01:20:31

如果您在GAE中使用cloudkms进行开发,那么没有本地的dev服务,您只能在收集到的时候与主生产服务通信。您可以使用您详细描述的库在本地开发,但仍然会影响生产。在

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

如果您使用GAE服务帐户,您也可以作为GAE服务帐户发出请求 gcloud iam service-accounts keysgcloud auth activate-service-account。在

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

相关问题 更多 >

    热门问题