在 terraform 中解密 gcp 凭据?

问题描述

我有一个谷歌云/terraform 项目,其中我使用 terraform文件发送到谷歌云存储桶。我有一个服务帐户凭据的 json 文件,我已经使用云 kms 对其进行了加密,例如

gcloud kms encrypt \
    --key key \
    --keyring key-ring \
    --location location  \
    --plaintext-file file-with-data-to-encrypt \
    --ciphertext-file file-to-store-encrypted-data \
    | base64

但是我想在 terraform 项目中使用这些加密凭证,例如

provider "google" {

  credentials = file( "ENCRYPTED-CREDS")

  project = var.project
  region  = "europe-west2"
}

我的问题是 - 如何在 terraform 中解密这些凭据和/或使用加密的凭据?

解决方法

Terraform 包含一个 google_kms_secret 数据源,可在您的资源定义中将加密数据与 Cloud KMS 结合使用。

文档中有一个 example usage,您可以使用它来确定要遵循的步骤。

基本上,您需要创建一个 keyRing 和一个 criptoKey 资源。

使用 gcloud command you mention 加密 json 文件,最后在您的资源定义中引用引用加密的密文。

following section of the docs 也可以证明有用。

,

你的问题是死循环!

  • Terraform 需要凭据才能访问 GCP
  • 您使用 KMS 加密凭证
  • 您的加密凭据需要访问 GCP 才能解密
  • 您将加密凭证放入 Terraform
  • Terraform 需要凭据才能访问 GCP 并使用 KMS 解密密钥
  • ....

因此,您需要使用纯文本形式的凭证。通常,当您运行 Terraform 时,需要解密凭证。因此,凭据需要在 GCP 之外进行加密。

现在,我不知道您的运行时环境?是在 GCP 上吗?别处?以前我使用过 GitLab CI,我把这个秘密放在了一个 Gitlab 秘密变量中。