{Aurora} IAM データベース認証


PostgreSQL の場合、IAM ロール (rds_iam) がマスターユーザーに追加されると、
IAM 認証はパスワード認証よりも優先されるため、マスターユーザーは IAM ユーザーとしてログインする必要があります


IAMユーザ(testiamuser)がDBユーザ(testdbuser)でIAM認証されるように設定

前提:
MySQLクライアント → 8.0.26
PostgreSQLクライアント → 13.3

-- 1. クラスタの作成

aws rds create-db-cluster \
--db-cluster-identifier cluster01 \
--engine aurora-mysql \
--engine-version 5.7.mysql_aurora.2.10.0 \
--master-username root \
--master-user-password 'password' \
--enable-iam-database-authentication

aws rds create-db-instance \
--db-instance-identifier cluster01-instance01 \
--db-cluster-identifier cluster01 \
--db-instance-class db.t3.small \
--engine aurora-mysql \
--no-auto-minor-version-upgrade

-- 2. IAMユーザの作成
aws iam create-user --user-name testiamuser

aws iam create-access-key --user-name testiamuser


-- 3. IAMポリシー作成


aws rds describe-db-clusters --query "DBClusters[*].[DBClusterIdentifier,DbClusterResourceId]"

vim policy01.json


{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"rds-db:connect"
],
"Resource": [
"arn:aws:rds-db:ap-northeast-1:999999999999:dbuser:cluster-XXXXXXXXXXXXXXXXXXXXXXXXXX/testdbuser"
]
}
]
}

aws iam create-policy \
--policy-name policy01 \
--policy-document file://policy01.json

-- 4. ポリシーをユーザにアタッチ
aws iam attach-user-policy \
--policy-arn arn:aws:iam::999999999999:policy/policy01 \
--user-name testiamuser

-- 5. IAM 認証を使用したデータベースアカウントの作成
CREATE USER testdbuser IDENTIFIED WITH AWSAuthenticationPlugin AS 'RDS';

create database test;
create table test.tab1(col1 int);
insert into test.tab1 values(1);
select * from test.tab1;

grant select on test.* to 'testdbuser'@'%';
show grants for 'testdbuser'@'%';


-- 6. IAM 認証を使用した DB クラスターへの接続
※testiamuserで実行

curl https://truststore.pki.rds.amazonaws.com/global/global-bundle.pem -o global-bundle.pem

 

RDSHOST="cluster01.cluster-xxxxxxxxxxxx.ap-northeast-1.rds.amazonaws.com"
TOKEN="$(aws rds generate-db-auth-token --hostname $RDSHOST --port 3306 --region ap-northeast-1 --username testdbuser )"

mysql --host=$RDSHOST --port=3306 --ssl-ca=/home/ec2-user/global-bundle.pem --enable-cleartext-plugin --user=testdbuser --password=$TOKEN

 


-- 7. クリーンアップ

-- ユーザ一覧
aws iam list-users


-- ユーザ削除

aws iam detach-user-policy \
--user-name testiamuser \
--policy-arn arn:aws:iam::999999999999:policy/policy01

aws iam delete-access-key \
--user-name testiamuser \
--access-key-id XXXXXXXXXXXXXXXXXXXX

aws iam delete-user --user-name testiamuser


-- ポリシーの一覧
aws iam list-policies | grep policy01

-- ポリシーの削除
aws iam delete-policy \
--policy-arn arn:aws:iam::999999999999:policy/policy01


-- クラスタ削除
aws rds delete-db-instance \
--db-instance-identifier cluster01-instance01 \
--skip-final-snapshot

aws rds delete-db-cluster \
--db-cluster-identifier cluster01 \
--skip-final-snapshot

 

-- 1. クラスタの作成

aws rds create-db-cluster \
--db-cluster-identifier cluster02 \
--engine aurora-postgresql \
--engine-version 12.6 \
--master-username postgres \
--master-user-password 'password' \
--enable-iam-database-authentication

aws rds create-db-instance \
--db-instance-identifier cluster02-instance01 \
--db-cluster-identifier cluster02 \
--db-instance-class db.t3.medium \
--engine aurora-postgresql \
--no-auto-minor-version-upgrade

-- 2. IAMユーザの作成
aws iam create-user --user-name testiamuser

aws iam create-access-key --user-name testiamuser


-- 3. IAMポリシー作成


aws rds describe-db-clusters --query "DBClusters[*].[DBClusterIdentifier,DbClusterResourceId]"

vim policy01.json


{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"rds-db:connect"
],
"Resource": [
"arn:aws:rds-db:ap-northeast-1:999999999999:dbuser:cluster-XXXXXXXXXXXXXXXXXXXXXXXXXX/testdbuser"
]
}
]
}

aws iam create-policy \
--policy-name policy01 \
--policy-document file://policy01.json

-- 4. ポリシーをユーザにアタッチ
aws iam attach-user-policy \
--policy-arn arn:aws:iam::999999999999:policy/policy01 \
--user-name testiamuser


-- 5. IAM 認証を使用したデータベースアカウントの作成
CREATE USER testdbuser;
GRANT rds_iam TO testdbuser;

create database test;
\c test
create table tab1(col1 int);
insert into tab1 values(1);
select * from tab1;

revoke connect on database test from public;
grant connect on database test to testdbuser;
grant select on tab1 to testdbuser;

 

-- 6. IAM 認証を使用した DB クラスターへの接続
※testiamuserで実行

curl https://truststore.pki.rds.amazonaws.com/global/global-bundle.pem -o global-bundle.pem


RDSHOST="cluster02.cluster-xxxxxxxxxxxx.ap-northeast-1.rds.amazonaws.com"
TOKEN="$(aws rds generate-db-auth-token --hostname $RDSHOST --port 5432 --region ap-northeast-1 --username testdbuser )"

psql "host=$RDSHOST port=5432 sslmode=verify-full sslrootcert=/home/ec2-user/global-bundle.pem dbname=test user=testdbuser password=$TOKEN"


-- 7. クリーンアップ

-- ユーザ一覧
aws iam list-users


-- ユーザ削除

aws iam detach-user-policy \
--user-name testiamuser \
--policy-arn arn:aws:iam::999999999999:policy/policy01

aws iam delete-access-key \
--user-name testiamuser \
--access-key-id XXXXXXXXXXXXXXXXXXXX


aws iam delete-user --user-name testiamuser


-- ポリシーの一覧
aws iam list-policies | grep policy01

-- ポリシーの削除
aws iam delete-policy \
--policy-arn arn:aws:iam::999999999999:policy/policy01


-- クラスタ削除
aws rds delete-db-instance \
--db-instance-identifier cluster02-instance01 \
--skip-final-snapshot

aws rds delete-db-cluster \
--db-cluster-identifier cluster02 \
--skip-final-snapshot