【问题标题】:How to extract sensitive output variables in terraform?如何在 terraform 中提取敏感的输出变量?
【发布时间】:2020-04-15 19:50:27
【问题描述】:

我有一个 terraform 配置,它使用访问密钥创建一个 AWS IAM 用户,并将 id 和 secret 分配给输出变量:

...

resource "aws_iam_access_key" "brand_new_user" {
  user = aws_iam_user.brand_new_user.name
}

output "brand_new_user_id" {
  value = aws_iam_access_key.brand_new_user.id
}

output "brand_new_user_secret" {
  value     = aws_iam_access_key.brand_new_user.encrypted_secret
  sensitive = true
}

这里brand_new_user_secret 被声明为敏感,所以terraform output 显然不打印它。

有没有办法在不解析整个状态文件的情况下获得它的输出值? 尝试直接访问它 (terraform output brand_new_user_secret) 不起作用(导致错误“在状态文件中找不到请求的输出变量...”)。

Terraform 版本:0.12.18

【问题讨论】:

    标签: terraform secret-key


    【解决方案1】:

    我没有尝试过,但文档似乎建议,如果您想输出 encrypted_secret,您必须向 aws_iam_access_key 资源提供 pgp_key

    • pgp_key -(可选)base-64 编码的 PGP 公钥或 keybase:some_person_that_exists 形式的密钥库用户名,用于 encrypted_secret 输出属性。

    • encrypted_secret - 如果指定了 pgp_key,则加密的秘密,base64 编码。 ~> 注意:加密的秘密可以使用命令行解密,例如:terraform output encrypted_secret | base64 --解码 | keybase pgp 解密。

    https://www.terraform.io/docs/providers/aws/r/iam_access_key.html

    【讨论】:

      【解决方案2】:

      我有一些希望避免它,但到目前为止我没有找到比解析 terraform state 更好的方法:

      terraform state pull | jq '.resources[] | select(.type == "aws_iam_access_key") | .instances[0].attributes'
      

      这将导致类似于以下的结构:

      {
        "encrypted_secret": null,
        "id": "....",
        "key_fingerprint": null,
        "pgp_key": null,
        "secret": "....",
        "ses_smtp_password": "....",
        "ses_smtp_password_v4": null,
        "status": "Active",
        "user": "...."
      }
      

      【讨论】:

        【解决方案3】:

        我在这里使用了一个像这样的 hacky 解决方法...

        resource "aws_iam_access_key" "brand_new_user" {
          user = aws_iam_user.brand_new_user.name
        }
        
        output "brand_new_user_id" {
          value = aws_iam_access_key.brand_new_user.id
        }
        
        data "template_file" "secret" {
          template = aws_iam_access_key.brand_new_user.encrypted_secret
        }
        
        output "brand_new_user_secret" {
          value     = data.template_file.secret.rendered
        }
        
        

        【讨论】:

        • 谢谢你,@mark!我考虑过,但我不想走这条路,因为它不安全:普通输出通常保存并存储在非安全位置 - 例如在系统不同模块使用的配置文件夹中(例如,如果您不想在代码中硬编码实例 ID)。相反,该状态应始终被视为敏感状态。
        【解决方案4】:

        要以交互方式查看敏感值,即出于分析/调试状态的目的,您可以使用 Terraform 的 console commandnonsensitive() 函数:

        $ terraform console
        
        > nonsensitive(aws_iam_access_key.brand_new_user.encrypted_secret)
        

        在打印之前,您可能需要使用其他函数来解码/操作该值。

        【讨论】:

          猜你喜欢
          • 2022-07-08
          • 2021-02-26
          • 1970-01-01
          • 1970-01-01
          • 2020-07-10
          • 1970-01-01
          • 1970-01-01
          • 2018-09-24
          • 2020-06-02
          相关资源
          最近更新 更多