【问题标题】:How can I hide AWS credentials from external program?如何对外部程序隐藏 AWS 凭证?
【发布时间】:2020-02-15 21:09:28
【问题描述】:

就我而言,我试图隐藏通过输出打印的 aws 访问密钥和秘密访问密钥。

我尝试实施一个解决方案,但不幸的是它在计划中打印了凭据。因此,每当我将代码/提交推送到 GITHUB 时,我们都会在 Jenkins 中运行 terraform,它会在 GITHUB 中吐出计划,从而暴露 terraform 计划中的凭据。

虽然我已经隐藏在输出中,但现在我正在计划中打印它并在 GitHub 中公开。我还尝试在输出中使用sensitive:true,这很容易解决这个问题。但是我的团队想要实施这个解决方案:(

resource "aws_iam_access_key" "key" {
  user = "${aws_iam_user.user.name}"
}

resource "null_resource" "access_key_shell" {
  triggers = {
    aws_user = "${aws_iam_user.user.name}" // triggering an alert on the user, since if we pass aws_iam_access_key, access key is visible in plan.
  }
}

data "external" "stdout" {
  depends_on = ["null_resource.access_key_shell"]
  program    = ["sh", "${path.module}/read.sh"]

  query {
    access_id = "${aws_iam_access_key.key.id}"
    secret_id = "${aws_iam_access_key.key.secret}"
  }
}

resource "null_resource" "contents_access" {
  triggers = {
    stdout = "${lookup(data.external.logstash_stdout.result, "access_key")}"
    value  = "${aws_iam_access_key.key.id}"
  }
}

output "aws_iam_podcast_logstash_access_key" {
  value = "${chomp(null_resource.contents_access.triggers["stdout"])}"
}

read.sh

#!/bin/bash
set -eux
echo {\"access_key\":\"$(aws kms encrypt --key-id alias/amp_key --plaintext ${access_id}  --output text --query CiphertextBlob)\", > sample.json && echo \"secret_key\": \"$(aws kms encrypt --key-id alias/amp_key --plaintext ${secret_id} --output text --query CiphertextBlob)\"} >> sample.json
cat sample.json | jq -r '.access_key'
cat sample.json | jq -r '.secret_key'

我的地形计划:

<= data.external.stdout
      id:                <computed>
      program.#:         "2"
      program.0:         "sh"
      program.1:         "/Users/xxxx/projects/tf_iam_stage/read.sh"
      query.%:           "2"
      query.access_id:   "xxxxxxxx"  ----> I want to hide these values from the plan
      query.secret_id:   "xxxxxxxxxxxxxxxxxxxxxx/x" ----> I want to hide these values from the plan
      result.%:          <computed>

任何帮助! 提前致谢!

【问题讨论】:

  • 您需要实现一些直接输入provider 的凭据参数,例如Vault。在这种情况下,凭据将没有机会显示。
  • @MattSchuchard,这是一个非常好的解决方案!但是我们目前不想只为这个用例设置Vault,有没有其他办法?
  • 您可以编写自己的提供程序以在本地读取凭据。

标签: terraform amazon-iam terraform-provider-aws


【解决方案1】:

这里发生了几件事。

首先,您正在泄露您的凭据,因为您将 .tfstate 存储在 GitHub 中。这个有一个简单的解决方案。首先,将*.tfstate 添加到您的.gitignore,然后设置远程后端,如果您使用 S3,则检查策略和 ACL 以防止公共访问。

其次,您的另一个问题是您在运行时获取凭据,并且在运行时 Terraform 会显示所有内容,除非您添加敏感标志。所以,基本上如果你想遵循这种方法,无论你的团队说什么,你都不得不使用sensitive: true。但是,为什么要以这种方式获取凭据?为什么不使用这些凭据添加一个新的提供者,为这个提供者设置一个别名,然后将它用于你这些密钥所在的资源?

【讨论】:

    【解决方案2】:

    在您的场景中,如果您采用:Remote State 方法,您会很棒。

    Remote State 允许 Terraform 将状态存储在远程存储中。 Terraform 支持在 Terraform Enterprise、Consul、S3 等位置存储状态。

    设置是在 AWS S3 上创建一个存储桶,它不应该被任何人读取或写入,除了将用于 Terraform 的用户。

    我添加的代码是;

     terraform {
        backend "s3" {
          bucket = "my-new-bucket"
          key = "state/key"
          region = "eu-west-1"
        }
      }
    

    这只是告诉 Terraform 使用 S3 作为后端提供程序来执行诸如存储 tfstate 文件之类的操作。

    不要忘记运行terraform init,因为这是必需的,Terraform 会注意到您已从本地存储更改为在 S3 中存储。

    完成后,您可以安全地删除本地 tfstate 文件,因为您的详细信息已安全地存储在 S3 上。

    这里有一些有用的文档:Click docs

    第二种方法是在此处使用 Terraform plugin 更多信息:Terraform plugin

    祝你好运!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-09-22
      • 2016-07-13
      • 1970-01-01
      • 2011-12-05
      • 2013-03-07
      • 1970-01-01
      • 1970-01-01
      • 2013-09-12
      相关资源
      最近更新 更多