amazon-web-services - 从 AWS ECS Fargate 访问 AWS RDS 的最佳方式

标签 amazon-web-services amazon-rds amazon-iam

我有运行 AWS ECS Fargate 的基于 Java 的容器。现在我们通过用户名密码组合访问 AWS RDS Postgres 数据库,但我想知道如何使这个过程更安全。
但我有一个问题: 如何避免在 .psql 脚本 ( CREATE USER username WITH PASSWORD 'xxx' ) 中存储用户密码?我认为更好的选择是完全避免使用密码,但我不知道该怎么做......
将密码加载到 Fargate 容器来自 AWS Secrets Manager - 这没问题。
请问您有什么建议吗?

编辑:我将在这里分享我的进展,以便任何人都可以重现
文档:https://docs.aws.amazon.com/.../.../.../UsingWithRDS.IAMDBAuth.html
- 第一阶段 -
目标 :IAM 身份验证的 PoC(使用 IAM 用户)

  • I have prepared my RDS instance to allow IAM Auth
  • 我已经创建了 IAM 用户 attached new IAM Policy to it 并创建了一个访问 key 。
  • {
        "Version": "2012-10-17",
        "Statement": [
            {
                "Sid": "VisualEditor0",
                "Effect": "Allow",
                "Action": "rds-db:*",
                "Resource": "arn:aws:rds-db:eu-central-1:666:dbuser:db-YFVS/iam_user"
            }
        ]
    }
    
    NOTE :在资源中使用的是resourceId,而不是arn! (这可以在 rds 实例的配置选项卡中找到)
  • I have created user in my database
  • CREATE USER iam_user; 
    GRANT rds_iam TO iam_user;
    
  • I tested the connection(导出访问 key 后)
  • # Generate token
    export RDSHOST="dev.aaaaa.eu-central-1.rds.amazonaws.com"
    export PGPASSWORD="$(aws rds generate-db-auth-token --hostname $RDSHOST --port 5432 --region eu-central-1 --username iam_user )"
    
    #Connect
    psql "host=$RDSHOST port=5432 dbname=mydb user=iam_user password=$PGPASSWORD"
    
    mydb->
    
    现在我想继续 Java 实现并进一步迁移到 Fargate
    - 第二阶段 -
    目标 :在 Python 应用程序中实现 IAM 身份验证
    我已经按照此处的教程进行操作,并且效果很好。我使用了我在上一步中创建的 IAM 用户的 IAM 凭证( key 、 secret )。另外,我已经从 ENV 而不是配置文件加载了凭据。
    https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/UsingWithRDS.IAMDBAuth.Connecting.Python.html
    import psycopg2
    import sys
    import boto3
    
    ENDPOINT="postgresmydb.123456789012.us-east-1.rds.amazonaws.com"
    PORT="5432"
    USR="jane_doe"
    REGION="us-east-1"
    DBNAME="mydb"
    
    session = boto3.Session(region_name='us-east-1',
                            aws_access_key_id=os.environ['AWS_ACCESS_KEY_ID'],
                            aws_secret_access_key=os.environ['AWS_SECRET_ACCESS_KEY'])
    
    token = client.generate_db_auth_token(DBHostname=ENDPOINT, Port=PORT, DBUsername=USR, Region=REGION)
    
    try:
        conn = psycopg2.connect(host=ENDPOINT, port=PORT, database=DBNAME, user=USR, password=token)
        cur = conn.cursor()
        cur.execute("""SELECT now()""")
        query_results = cur.fetchall()
        print(query_results)
    except Exception as e:
        print("Database connection failed due to {}".format(e))                
                    
    

    最佳答案

    根据评论。
    一种不使用密码访问 RDS PostgreSQL 的方法是使用 IAM Database Authentication :

    With this authentication method, you don't need to use a password when you connect to a DB instance. Instead, you use an authentication token.


    有一些limitations考虑,其中之一是:

    The maximum number of connections per second for your DB instance might be limited depending on its DB instance class and your workload.

    关于amazon-web-services - 从 AWS ECS Fargate 访问 AWS RDS 的最佳方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64167883/

    相关文章:

    amazon-web-services - AppSync - 使用数组和对象创建嵌套突变?

    mysql - Percona 工具包对比Openark 套件。架构变更

    postgresql - 当 RDS Postgres DB 被删除时,它是否清除连接的 Redis Elasticache?

    amazon-web-services - DynamoDBleadingKeys 限制的 IAM 策略 Cognito 变量

    amazon-web-services - 用于限制 EC2 实例读取访问的 AWS IAM 策略

    amazon-web-services - CloudFront Lambda@Edge 缓存行为未显示在 AWS 控制台中

    amazon-web-services - 在 AWS CloudFormation 脚本中自定义合作伙伴事件源名称

    python - 将图像上传到S3 python

    amazon-web-services - 为什么 CloudFormation 不创建我的数据库?

    aws-cloudformation - 为参数 'instanceProfileName' 提供的值无效