【问题标题】:Get secrets for GCP deployments from KMS从 KMS 获取 GCP 部署的机密
【发布时间】:2018-07-09 13:19:47
【问题描述】:

我想使用 Deployment Manager 在 GCP 中部署 Cloud VPN 隧道

为此,我使用 Python 设置了一个部署脚本,并且我不希望在我的配置中以纯文本形式提供 VPN 隧道的共享密钥。

所以我尝试包含通过KMS 加密的秘密,然后在 python 脚本中调用 KMS 以获取纯文本秘密。

解密秘密的python代码如下所示:

import base64
import googleapiclient.discovery 

def decryptSecret(enc_secret,context):
    """ decrypts the given Secret via KMS"""

    # KMS Configuration
    KEY_RING     = <Key Ring>
    KEY_ID       = <Key>
    KEY_LOCATION = REGION
    KEY_PROJECT  = context.env['project'],

    # Creates an API client for the KMS API.
    kms_client = googleapiclient.discovery.build('cloudkms', 'v1')

    key_name = 'projects/{}/locations/{}/keyRings/{}/cryptoKeys/{}'.format(
       KEY_PROJECT, KEY_LOCATION, KEY_RING, KEY_ID)

    crypto_keys =  kms_client.projects().locations().keyRings().cryptoKeys()

    request = crypto_keys.decrypt(
            name=key_name,
            body={'ciphertext': enc_secret})
    response = request.execute()
    plaintext = base64.b64decode(response['plaintext'].encode('ascii'))

    return plaintext

但如果我部署此代码,我只会从部署管理器收到以下错误消息:

Waiting for update [operation-<...>]...failed.
ERROR: (gcloud.deployment-manager.deployments.update) Error in Operation [operation-1517326129267-5640004f18139-450d8883-8d57c3ff]: errors:
- code: MANIFEST_EXPANSION_USER_ERROR
  message: |
    Manifest expansion encountered the following errors: Error compiling Python code: No module named googleapiclient.discovery Resource: cloudvpn-testenv.py Resource: config

我还尝试在我的配置 yaml 中包含完整的 google-api-python-client 库,但我仍然收到此错误。

有人知道吗?

【问题讨论】:

  • 在文档中看到这一点:“不要在模板中使用系统调用。如果模板包含进行系统或网络调用的任何代码,则会被拒绝。这包括使用执行系统调用的任何方法上面列出的支持的库。” - cloud.google.com/deployment-manager/docs/configuration/…
  • 那么...没有办法从部署管理器获取 KMS 机密吗?
  • 就我的研究而言:不,我还没有找到任何解决方案。

标签: google-cloud-platform google-deployment-manager google-cloud-kms google-secret-manager


【解决方案1】:

直接回答您的问题:

# requirements.txt

google-api-python-client
# main.py

import base64
import os
import googleapiclient.discovery

crypto_key_id = os.environ['KMS_CRYPTO_KEY_ID']

def decrypt(client, s):
    response = client \
        .projects() \
        .locations() \
        .keyRings() \
        .cryptoKeys() \
        .decrypt(name=crypto_key_id, body={"ciphertext":s}) \
        .execute()

    return base64.b64decode(response['plaintext']).decode('utf-8').strip()


kms_client = googleapiclient.discovery.build('cloudkms', 'v1')

auth = decrypt(kms_client, '...ciphertext...'])

您可以找到更多示例和示例on GitHub

为了间接回答您的问题,您可能对Secret Manager 感兴趣。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-07-15
    • 2019-09-21
    • 2021-11-16
    • 1970-01-01
    • 2019-05-30
    • 1970-01-01
    • 2021-10-25
    • 2020-04-22
    相关资源
    最近更新 更多