Azure Database for MySQL Flexible serverにてActive Directory認証を試す

はじめに

Azure Database for MySQL Flexible serverにてActive Directory認証がサポートされたので試してみます。なんとなくActive Directory認証については恐怖感があったのですが、実際にやってみると、一部を除きあっさりとできてしまったので実施した内容を記しておきます。

概念としては以下のものが公式ドキュメントとしてあります。

learn.microsoft.com

実際のやり方は以下のものが公式ドキュメントとして公開されています。

learn.microsoft.com

必要なもの

今回は、Azure ADグループとVirtual MachineのSystem Managed Identityによる認証を行います。

実際に設定する際には以下のものが必要でした。

  • Azure Database for MySQL Flexible server
  • マネージドID
  • Azure Active Directoryのグループ
  • Virtual Machine
  • Cloud Shell

具体的な設定

Active Directory管理者の設定と認証

具体的な設定については以下の公式ドキュメントに記載がありますので、それに従います。

learn.microsoft.com

Azure AD管理者の構成のところでマネージドIDに以下のアクセス許可が必要とあります。

  • User.Read.All
  • GroupMember.Read.All
  • Application.Read.ALL

このアクセス許可を付与する方法がわからなかったのですが、以下のブログにPowerShellを使ったやり方が掲載されていましたので、それをそのまま拝借します(というか、ほとんどこのブログに掲載されていることを真似れば以降の作業はうまくいきます)。

zenn.dev

PowerShellの環境をどうやって用意しようかなと思っていたのですが、Cloud Shellから設定できることを知り、そこから上記のやり方を実行することにしました。

learn.microsoft.com

うまくいくと、エンタープライズアプリケーションにてアクセス許可が設定できていることが確認できます。

あとは、Azure ADグループでログインすることを実行します。AzureにログインしているユーザーをAzure ADグループmysql-admin-hogehogeに所属しておき、以下のコマンドを実行します。

mysql -h "Azure Database for MySQL Flexible serverのホスト名" -u mysql-admin-hogehoge(Azure ADグループ) --enable-cleartext-plugin --password=$(az account get-access-token --resource-type oss-rdbms --output tsv --query accessToken)

成功すると、以下のように表示されます。

$ mysql -h "Azure Database for MySQL Flexible serverのホスト名" -u mysql-admin-hogehoge(Azure ADグループ) --enable-cleartext-plugin --password=$(az account get-access-token --resource-type oss-rdbms --output tsv --query accessToken)
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 22
Server version: 8.0.28 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> 

Virtual MachineのSystem Managed Identityによる認証

まずはVirtual MachineにてSystem Managed Identityを有効にします。やり方の公式ドキュメントは以下の通り。

learn.microsoft.com

これをAzure Database for MySQL Flexible serverにてユーザーとして設定する必要がありますが、公式ドキュメントには2023年1月22日時点で詳細な説明はなく、Azure Database for MySQL Single serverのドキュメントに記載がありました。

learn.microsoft.com

要はCREATE AADUSER '任意のユーザ名' IDENTIFIED BY 'CLIENT_ID';を実行すればよさそうです。システム要件に合わせて権限を付与してあげると良いでしょう。

dev.mysql.com

これでAzure Database for MySQL Flexible serverでユーザーとしての設定は完了です。

接続にはアクセストークンが必要となります。アクセストークンの取得にはAzure Instance Metadata Serviceから取得します。

learn.microsoft.com

以下のコマンドで取得することができました。

$ accessToken=$(curl -s 'http://169.254.169.254/metadata/identity/oauth2/token?api-version=2018-02-01&resource=https%3A%2F%2Fossrdbms-aad.database.windows.net' -H Metadata:true | jq -r .access_token)

このアクセストークンを使って以下のコマンドを実行すると、無事接続することができました。

$ mysql -h "Azure Database for MySQL Flexible serverのホスト名" -u 上記で設定した任意のユーザ名 --enable-cleartext-plugin --password=$accessToken
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 25
Server version: 8.0.28 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>