首页 > 解决方案 > 如何使用 Google Cloud Build 和 Google Cloud KMS 从构建时解密的文件中读取变量

问题描述

我正在按照教程将加密密钥添加到我的 cloudbuild YAML 文件中。我试图了解我应该如何在我的 YAML 文件的后续步骤中“使用工作区目录中的解密 ... 文件”变量。

我解密密钥文件的 cloudbuild 步骤如下:

- name: gcr.io/cloud-builders/gcloud
  args: ['kms', 'decrypt', '--ciphertext-file=<encrypted_file>', '--plaintext-file=<decrypted_file>', '--location=<location>', '--keyring=<keyring>', '--key=<key>']

该教程不清楚如何执行此操作,我在 Internet 上找不到与此相关的任何内容。

非常感谢任何帮助。

谢谢。

标签: encryptiongoogle-cloud-buildgoogle-cloud-kms

解决方案


使用 加密内容时gcloud kms encrypt,可以将输出写入工作区中的文件,例如:

# replace with your values
gcloud kms encrypt \
  --location=global \
  --keyring=my-kr \
  --key=my-key \
  --plaintext-file=./data-to-encrypt \
  --ciphertext-file=./encrypted-data

./data-to-encrypt磁盘上包含明文机密的文件在哪里,并且是磁盘上应写入加密密文./encrypted-data的目标路径。

直接使用 API 时,交互如下所示:

plaintext -> kms(encrypt) -> ciphertext

但是,当使用 时gcloud,它看起来像这样:

plaintext-file -> gcloud(read) -> kms(encrypt) -> ciphertext -> gcloud(write)

当您调用 Cloud Build 时,它会有效地获取应用程序的 tarball,减去.gcloudignore. 这意味着./encrypted-data将在容器步骤内的文件系统上可用:

steps:
# decrypt the value in ./my-secret
- name: gcr.io/cloud-builders/gcloud
  args:
  - kms
  - decrypt
  - --location=global
  - --keyring=my-kr
  - --key=my-key
  - --ciphertext=file=./encrypted-data
  - --plaintext-file=./my-secret

- name: gcr.io/my-project/my-image
  steps: 
  - my-app start --secret=./my-secret

目前,在 Cloud Build 中,步骤之间共享数据的唯一方法是使用文件,但所有构建步骤都具有相同的共享文件系统。


推荐阅读