简体   繁体   English

如何在 AWS CDK 中复制和修改密钥

[英]How to copy and modify a secret in AWS CDK

I have a CDK app in python that creates an Postgres RDS database.我在 python 中有一个 CDK 应用程序,它创建了一个 Postgres RDS 数据库。 I let the DatabaseCluster generate a secret for the database admin user.我让 DatabaseCluster 为数据库管理员用户生成一个秘密。 I would like to take that secret and create 2 other secrets with everything the same except the endpoint (one for RDS proxy and one to use the read replica).我想获取该秘密并创建 2 个其他秘密,除了端点之外的所有内容都相同(一个用于 RDS 代理,一个用于使用只读副本)。 I'm not sure how I would do that, and do it securely without exposing the original password in Cloudformation.我不确定我将如何做到这一点,并且在不暴露 Cloudformation 中的原始密码的情况下安全地做到这一点。

from aws_cdk import core as cdk
from aws_cdk.aws_ec2 import InstanceType, IVpc, Peer, Port, SecurityGroup, SubnetSelection, SubnetType
from aws_cdk.aws_rds import AuroraPostgresEngineVersion, Credentials, DatabaseCluster, DatabaseClusterEngine, InstanceProps

class AuroraPostgresRdsModule(cdk.Construct):
    def __init__(self, scope: cdk.Construct, construct_id: str, vpc: IVpc, **kwargs) -> None:
        super().__init__(scope, construct_id, **kwargs)
        self.rds = DatabaseCluster(
            self,
            "rds",
            engine=DatabaseClusterEngine.aurora_postgres(version=AuroraPostgresEngineVersion.VER_11_9),
            instance_props=InstanceProps(vpc=vpc),
            instances=2,
            default_database_name="test"
        )

        self.rds_proxy = self.rds.add_proxy(
            "rds-proxy",
            secrets=[self.rds.secret],
            vpc=vpc
        )
        
        # How to do this???
        # self.proxy_secret = self.rds.secret.copy(updates={"host": self.rds_proxy.endpoint})
        # self.replica_secret = self.rds.secret.copy(updates={"host": self.rds.cluster_read_endpoint})

One way is to define the secret on its own first and then pass it to your other resources.一种方法是先自行定义秘密,然后将其传递给您的其他资源。 You could also specify credentials for other resources based your secret's values, eg secret_value_from_json("username").to_string() and secret_value_from_json("password") .您还可以根据您的机密值指定其他资源的凭据,例如secret_value_from_json("username").to_string()secret_value_from_json("password")

See: https://docs.aws.amazon.com/cdk/api/latest/python/aws_cdk.aws_secretsmanager/README.html请参阅: https://docs.aws.amazon.com/cdk/api/latest/python/aws_cdk.aws_secretsmanager/README.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM