使用 CloudFormation 读取机密

Ale*_*tos 5 aws-cloudformation aws-secrets-manager

我正在尝试在 CloudFormation 中创建一个 AWS 堆栈,该堆栈在 JSON 中有一个秘密。

我不希望参数中显示机密的值,也不希望我的实例(fargate 或 ec2)访问机密管理器。我希望 CloudFormation 从秘密管理器中检索值并在运行时将其注入模板。

这就是我所做的:

  1. 创建一个秘密

  2. 使用 Designer 创建模板

  3. 阅读秘密并创建资源。在这种情况下,我正在创建一个将秘密标记为标签的存储桶。我知道这不安全,但存储桶仅用作概念证明。

  4. 验证存储桶是否包含带有秘密的标签

这是我的模板:

{
    "AWSTemplateFormatVersion": "2010-09-09",
    "Description": "create a single S3 bucket",
    "Resources": {
        "SampleBucket": {
            "Type": "AWS::S3::Bucket",
            "Properties": {
                "BucketName": "asantostestbucket",
                "Tags" : [
                    {
                        "Key" : "keyname",
                        "Value" : "{{resolve:secretsmanager:dev/learning:SecretString:hello}}"
                    }
            ]
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这给了我错误One or more tags are not valid

我如何向 CloudFormation 表明我希望它读取机密,而不是尝试将标签作为文本读取?换句话说,将“{{resolve:secretsmanager:dev/learning:SecretString:hello}}”替换为值,而不是将其作为文本阅读。

Joh*_*ein 5

为了重现这种情况,我执行了以下操作:

  • Secrets Manager 中,创建了一个新的 secret
    • “其他类型的秘密”
    • 钥匙: hello
    • 价值: surprise
    • 秘密名称: dev/learning
  • 使用 AWS CLI 测试了密钥

这是输出:

aws secretsmanager get-secret-value --secret-id dev/learning
{
    "ARN": "arn:aws:secretsmanager:ap-southeast-2:123456789012:secret:dev/learning-kCxSK3",
    "Name": "dev/learning",
    "VersionId": "...",
    "SecretString": "{\"hello\":\"surprise\"}",
    "VersionStages": [
        "AWSCURRENT"
    ],
    "CreatedDate": 1560925072.106
}
Run Code Online (Sandbox Code Playgroud)
  • 启动您在上面提供的 CloudFormation 模板(但使用不同的存储桶名称)

结果:我收到消息One or more tags are not valid

所以,我得到了和你一样的结果。

然后我尝试使用秘密创建不同类型的资源:

{
    "AWSTemplateFormatVersion": "2010-09-09",
    "Resources": {
        "SecurityGroup": {
            "Type": "AWS::EC2::SecurityGroup",
            "Properties": {
                "GroupDescription": "{{resolve:secretsmanager:dev/learning:SecretString:hello}}"
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这工作成功:

aws ec2 describe-security-groups --group-id sg-03cfd71f4539a4b7e
{
    "SecurityGroups": [
        {
            "Description": "surprise",
            ...
Run Code Online (Sandbox Code Playgroud)

所以,看起来它的{{resolve}}行为是正确的,但出于某种原因,S3 标签不喜欢它。

底线:这是可能的,但不可取。

  • 我相信 Cloud Formation 团队不允许将动态引用扩展到公开可见的地方。这包括 EC2 元数据(在控制台中作为 base64 编码数据可见)和标签之类的内容。 (2认同)