タダです.
RDS の IAM 認証をする時に言われてみれば試したことなかったことを経験したので備忘録でまとめます.
sadayoshi-tada.hatenablog.com
sadayoshi-tada.hatenablog.com
概要
Aurora Serverless に接続するために IAM 認証を使うことにしたのですが,接続時に下記のように aws rds generate-db-auth-token
でトークンを取得する処理があります.その際に --hostname
で指定するのがこれまでは Aurora Serverless で生成されるエンドポイントを指定していました.ここに指定するのを Route53 に登録している CNAME レコードを指定するとどうなるのかを経験したのでまとめます.
mysql -h [Aurora Serveless V2 エンドポイント] -u [DB ユーザー名] -p`aws rds generate-db-auth-token --hostname [Aurora Serveless V2 エンドポイント] --port 3306 --username [DB ユーザ名] --region ap-northeast-1` --enable-cleartext-plugin
事前準備
予め Aurora Serverless に加え接続用 EC2 と Route53 のレコードを用意して CNAME で名前解決できるようになった状態にしました.
$dig write-db.hoge.com
; <<>> DiG 9.10.6 <<>> write-db.hoge.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 54943
;; flags: qr rd ra; QUERY: 1, ANSWER: 3, AUTHORITY: 0, ADDITIONAL: 1
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;write-db.hogecom. IN A
;; ANSWER SECTION:
write-db.hoge.com. 377 IN CNAME test.cluster-rtyuikfghj.ap-northeast-1.rds.amazonaws.com.
test.cluster-rtyuikfghj.ap-northeast-1.rds.amazonaws.com. 15 IN CNAME test-instance-1.rtyuikfghj.ap-northeast-1.rds.amazonaws.com.
test-instance-1.rtyuikfghj.ap-northeast-1.rds.amazonaws.com. 15 IN A 10.123.456.789
$dig read-db.hoge.com ~
; <<>> DiG 9.10.6 <<>> read-db.hoge.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 38417
;; flags: qr rd ra; QUERY: 1, ANSWER: 3, AUTHORITY: 0, ADDITIONAL: 1
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;read-db.hoge.com. IN A
;; ANSWER SECTION:
read-db.hoge.com. 377 IN CNAME test.cluster-ro-rtyuikfghj.ap-northeast-1.rds.amazonaws.com.
test.cluster-ro-rtyuikfghj.ap-northeast-1.rds.amazonaws.com. 15 IN CNAME test-instance-1.rtyuikfghj.ap-northeast-1.rds.amazonaws.com.
test-instance-1.rtyuikfghj.ap-northeast-1.rds.amazonaws.com. 15 IN A 10.123.456.789
試した接続方法
次の3つの方法を試しました.
- トークン取得ではエンドポイント名を指定し,MySQL 接続時のホスト名は CNAME レコード
- トークン取得も MySQL 接続時のホスト名も CNAME レコード
- トークン取得時は CNAME レコードにし,MySQL 接続時のホスト名はエンドポイント名
なお,過去の記事ではトークン取得も MySQL 接続時のホスト名もエンドポイント名を指定しており,下記のように接続成功しています.
$ mysql -h test.cluster-rtyuikfghj.ap-northeast-1.rds.amazonaws.com -u iam_test -p`aws rds generate-db-auth-token --hostname test.cluster-rtyuikfghj.ap-northeast-1.rds.amazonaws.com --port 3306 --username iam_test --region ap-northeast-1` --enable-cleartext-plugin
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 35
Server version: 8.0.23 Source distribution
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
トークン取得ではエンドポイント名を指定し,MySQL 接続時のホスト名は CNAME レコード
トークン取得ではエンドポイント名を指定し,MySQL 接続時のホスト名は CNAME レコードの場合ですが,これは成功します.
$ mysql -h write-db.hoge.com -u iam_test -p`aws rds generate-db-auth-token --hostname test.cluster-rtyuikfghj.ap-northeast-1.rds.amazonaws.com --port 3306 --username iam_test --region ap-northeast-1` --enable-cleartext-plugin
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 37
Server version: 8.0.23 Source distribution
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
トークン取得も MySQL 接続時のホスト名も CNAME レコード
次にトークン取得も MySQL 接続時のホスト名も CNAME レコードを指定した場合,こちらは失敗します.
$ mysql -h write-db.hoge.com -u iam_test -p`aws rds generate-db-auth-token --hostname write-db.hoge.com --port 3306 --username iam_test --region ap-northeast-1` --enable-cleartext-plugin
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user 'iam_test'@'10.123.456.789' (using password: YES)
トークン取得時は CNAME レコードにし,MySQL 接続時のホスト名はエンドポイント名
最後にトークン取得時は CNAME レコードにし,MySQL 接続時のホスト名はエンドポイント名を指定した場合も失敗します.
$ mysql -h test.cluster-rtyuikfghj.ap-northeast-1.rds.amazonaws.com -u iam_test -p`aws rds generate-db-auth-token --hostname write-db.hoge.com --port 3306 --username iam_test --region ap-northeast-1` --enable-cleartext-plugin
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user 'iam_test'@'10.123.456.789' (using password: YES)
まとめ
以上のようにドメインを接続時に指定する場合は,トークン取得ではエンドポイント名を指定し,MySQL 接続時のホスト名は CNAME レコードというパターンが成功しています.ドキュメントを見ると The hostname of the database to connect to
と指定が --hostname
の箇所があったので勘違いしてたのですが,調べてみないとわからかったことなので備忘録として残しておきます.
awscli.amazonaws.com