Last active
March 25, 2025 04:17
Revisions
-
buzztaiki revised this gist
Mar 25, 2025 . No changes.There are no files selected for viewing
-
buzztaiki revised this gist
Mar 25, 2025 . No changes.There are no files selected for viewing
-
buzztaiki revised this gist
Mar 25, 2025 . No changes.There are no files selected for viewing
-
buzztaiki created this gist
Mar 25, 2025 .There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,49 @@ # Entra 認証を有効にした Azure MySQL に mysql provider で接続するときは `azure://` を使わなければいけない はまったのでメモ。 ## 現象 https://registry.terraform.io/providers/petoju/mysql で `azure://` のプレフィックスを使わず `authentication_plugin=true` の設定にして azure-cli から取得したアクセストークンで MySQl に接続すると timeout まで待った後に 以下のエラーが出る ``` │ Error: failed to connect to MySQL: could not create new connection: could not connect to server: this user requires mysql native password authentication ``` ## 結論 `azure://` を使う。 ## 原因 - terraform mysql provider は利用する認証プラグインを [`authentication_plugin`](https://registry.terraform.io/providers/petoju/mysql/latest/docs#authentication_plugin-1) で指定する。 - これは値として以下の二つを許容する - `native` - `cleartext` - これが設定されると、go mysql driver に渡す値が以下のように設定される https://github.com/petoju/terraform-provider-mysql/blob/946a912ae10ebf194599f8359970eb7272cd4d45/mysql/provider.go#L327-L328 ``` var allowClearTextPasswords = authPlugin == cleartextPasswords var allowNativePasswords = authPlugin == nativePasswords ``` - つまり、DSN に付与される値は以下になる - `cleartext`: `allowClearTextPasswords=true&allowNativePasswords=false` - `native`: `allowClearTextPasswords=false&allowNativePasswords=true` - Entra 認証が有効になった MySQL は、何故か認証レスポンスとして `mysql_clear_password` と `mysql_native_password` の両方を返すらしく、mysql driver の以下の個所でエラーになる https://github.com/go-sql-driver/mysql/blob/1fbafa8082dab81e2c2e8caeb55d569dfeafcf94/auth.go#L304-L311 ``` case "mysql_native_password": if !mc.cfg.AllowNativePasswords { return nil, ErrNativePassword } // https://dev.mysql.com/doc/internals/en/secure-password-authentication.html // Native password authentication only need and will need 20-byte challenge. authResp := scramblePassword(authData[:20], mc.cfg.Passwd) return authResp, nil ``` - `azure://` を指定した場合はこれに対策していて、`allowNativePasswords` を設定しないというような事をやっている https://github.com/petoju/terraform-provider-mysql/blob/946a912ae10ebf194599f8359970eb7272cd4d45/mysql/provider.go#L483-L485 ``` // Azure AD does not support native password authentication but go-sql-driver/mysql // has to be configured only with ?allowClearTextPasswords=true not with allowNativePasswords=false in this case allowClearTextPasswords = true ```