postgresql Amazon RDS IAM PAM身份验证失败

w8rqjzmb  于 2023-02-04  发布在  PostgreSQL
关注(0)|答案(5)|浏览(153)

我在Postgresql上启用了IAM身份验证,我的用户myAWSusername具有RDSFullAccess

export RDSHOST="MYRDSHOSTNAME.us-east-2.rds.amazonaws.com"
export PGPASSWORD="$(aws rds generate-db-auth-token --hostname $RDSHOST --port 5432 --region us-east-2 --username myAWSusername(not db_userx) )"
psql "host=$RDSHOST port=5432 sslmode=verify-full sslrootcert=./rds-combined-ca-bundle.pem dbname=busscanner user=db_userx"

我得到:

psql: FATAL:  PAM authentication failed for user "db_userx"

这就是创建db_userx的方法

CREATE USER db_userx WITH LOGIN; 
GRANT rds_iam TO db_userx;

\du的输出

Role name     |                         Attributes                         |                   Member of                    
-------------------+------------------------------------------------------------+------------------------------------------------
 db_userx          |                                                            | {rds_iam}
 postgres_ro       |                                                            | {postgres_ro_group}
 postgres_ro_group | Cannot login                                               | {}
 rds_iam           | Cannot login                                               | {}
 rds_replication   | Cannot login                                               | {}
 rds_superuser     | Cannot login                                               | {pg_monitor,pg_signal_backend,rds_replication}
 rdsadmin          | Superuser, Create role, Create DB, Replication, Bypass RLS+| {}
                   | Password valid until infinity                              | 
 rdsrepladmin      | No inheritance, Cannot login, Replication                  | {}
 read_only_user    | Password valid until infinity                              | {}

rds_iam无法登录是否正确?
这是我附加到我的用户的策略:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "rds-db:connect"
            ],
            "Resource": [
                "arn:aws:rds-db:us-east-2:MYAWSROOTACCOUNTID:dbuser:*/db_userx"
            ]
        }
    ]
}
nszi6y05

nszi6y051#

必须使用IAM策略中的db_userx生成generate-db-auth-token
db-auth-token将是您的PGPASSWORD

export RDSHOST="MYRDSHOSTNAME.us-east-2.rds.amazonaws.com"
export PG_USER="db_userx"
export PGPASSWORD="$(aws rds generate-db-auth-token --hostname $RDSHOST --port 5432 --region us-west-2 --username $PG_USER )"

然后:

psql "host=$RDSHOST port=5432 sslmode=verify-full sslrootcert=./rds-combined-ca-bundle.pem dbname=db_roles_test user=$PG_USER"

这对于db_userx是正确的

CREATE USER db_userx WITH LOGIN; 
GRANT rds_iam TO db_userx;

\du的输出

List of roles
      Role name       |                   Attributes                   |                          Member of
----------------------+------------------------------------------------+--------------------------------------------------------------
 db_userx             |                                                | {rds_iam}
 pg_monitor           | Cannot login                                   | {pg_read_all_settings,pg_read_all_stats,pg_stat_scan_tables}
 pg_read_all_settings | Cannot login                                   | {}
 pg_read_all_stats    | Cannot login                                   | {}
 pg_signal_backend    | Cannot login                                   | {}
 pg_stat_scan_tables  | Cannot login                                   | {}
 rds_iam              | Cannot login                                   | {}
 rds_password         | Cannot login                                   | {}
 rds_replication      | Cannot login                                   | {}
 rds_superuser        | Cannot login                                   | {pg_monitor,pg_signal_backend,rds_replication,rds_password}
 rdsadmin             | Superuser, Create role, Create DB, Replication+| {}
                      | Password valid until infinity                  |
 rdsrepladmin         | No inheritance, Cannot login, Replication      | {}
 root                 | Create role, Create DB                        +| {rds_superuser}

以便您可以通过创建所需数量的用户

CREATE USER <you_user_name> WITH LOGIN;

小心Authentication tokens have a lifespan of 15 minutes
因此,在完成所有这些操作后,具有您的策略的任何AWS Resource都将能够访问RDS Db。

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "rds-db:connect"
            ],
            "Resource": [
                "arn:aws:rds-db:us-east-2:MYAWSROOTACCOUNTID:dbuser:*/db_userx"
            ]
        }
    ]
}
xdyibdwo

xdyibdwo2#

对于那些仍在纠结于“用户'xxxx'的PAM身份验证失败”的人,请检查您的AWS帐户是否是AWS组织的一部分。
如果账户是组织的一部分,则将rds-db:*添加到账户所属组织单元的服务控制策略中。
此外,请检查是否存在没有
rds-db
权限的IAM用户或角色的层次结构。
有关详细信息,请查看这些高级支持AWS文档:https://aws.amazon.com/premiumsupport/knowledge-center/rds-postgresql-connect-using-iam/#:~:text=如果您仍然收到该帐户所属的帐户的信息,则该帐户属于该帐户。

gudnpqoy

gudnpqoy3#

出现“PAM authentication failed”错误的一种方法是尝试从EC2连接,但没有将允许“rds-db:connect”的策略附加到EC2 IAM角色。注意,您可以在没有附加角色的情况下从aws rds generate-db-auth-token生成令牌;只有当试图向DB认证时才需要该角色。
此外,aws rds generate-db-auth-token--username参数需要DB用户(db_userx),而不是IAM用户(myAWSusername)。

cpjpxq1n

cpjpxq1n4#

您需要将rds-db:connect附加到IAM角色。然后将该角色附加到EC2示例Lambda。

vyu0f0g1

vyu0f0g15#

如果你仍然在挣扎,以上的解决方案都不适合你,那么:-如果你已经创建了一个用户与登录密码,并授予该用户'rds_iam'角色知情/不知情的ie

create user test_user with login password 'pass2122';
grant rds_iam to test_user;

那么这是错误的,因为rds_iam角色需要被赋予那些将通过IAM认证而不是密码认证登录的用户。所以,撤销rds_iam,您将能够登录。

Revoke rds_iam from test_user;

相关问题