前回の記事の続きでもあります。
Azure PaaS の KeyVault を使おうとすると AuthenticationFaildException エラーになるので、調べたところ、az コマンドでのログインが出来ていなかったのが前回の結論でした。
az cli でログインすれば、ちゃんと動作しました。
ところが、Azure.Security.KeyVault.Secrets パッケージを 4.1.0 から 4.4.0 にアップデートしたところ、再び AuthenticationFaildException エラーが発生しました。
コードはこんな感じです。
static string GetHimichuString()
{
string keyVaultUrl = "https://himichumoji.vault.azure.net/"; //コンテナのURL
try
{
var client = new SecretClient(new Uri(keyVaultUrl), new DefaultAzureCredential());
KeyVaultSecret secret = client.GetSecret("himichu"); //<--ここでエラー
return secret.Value;
}
catch(Exception ex){
}
}
エラー内容としては、次の通りでした。
Azure.Identity.AuthenticationFailedException: 'Azure CLI authentication failed
due to an unknown error. See the troubleshooting guide for more information.
https://aka.ms/azsdk/net/identity/azclicredential/troubleshoot
ERROR: az: error: unrecognized arguments: --tenant
最終行の「unrecognized arguments: --tenant」が妙に気になるところですが、ひとまず、パッケージのアップデートで発生していますので、どのバージョンから発生したかを調べます。
バージョン | 成否 |
---|---|
4.1.0 | 成功 |
4.2.0 | 成功 |
4.3.0 | 失敗 |
4.4.0 | 失敗 |
4.3.0 から失敗するようです。Changelog を見てみます。
- 4.3.0 (2022-03-24)
- Changes from both the last release and the last beta include:Features Added
Added KeyVaultSecretIdentifier.TryCreate to parse secret URIs without throwing an exception when invalid. (#23146)
Support multi-tenant authentication against Key Vault and Managed HSM when using Azure.Identity 1.5.0 or newer. (#18359)
Bugs Fixed
Added secret version to distributed tracing. (#12907)
Other Changes
The default service version is now "7.3".
- Changes from both the last release and the last beta include:Features Added
- 4.3.0-beta.4 (2022-01-12)
- Other Changes
Package metadata fixed
- Other Changes
- 4.3.0-beta.3 (2022-01-11)
- Other Changes
Bug fixes
- Other Changes
- 4.3.0-beta.2 (2021-10-14)
- Features Added
Added KeyVaultSecretIdentifier.TryCreate to parse secret URIs without throwing an exception when invalid. (#23146)
Support multi-tenant authentication against Key Vault and Managed HSM when using Azure.Identity 1.5.0 or newer. (#18359)
Bugs Fixed
Added secret version to distributed tracing. (#12907)
- Features Added
- 4.3.0-beta.1 (2021-08-10)
- Fixed
The default service version is now "7.3-preview".
- Fixed
どうやら、4.3.0-beta.2 (2021-10-14) で multi-tenant での認証に対応したようで、これがキーになりそうです。
Azure.Security.KeyVault.Secrets は、バックグラウンドで python + azcli を起動して認証情報の処理を行っているようです。
ということは azcli も multi-tenant に対応したバージョンが必要ということになります。
次のページにある通り、az コマンドで multi-tenant に対応しているのは、2.38.0 以降のようです。
ということで、自環境の az コマンドバージョンを見てみると。。。
c:\> az --version
azure-cli (2.0.52)
acr (2.1.10)
:
に、2.0.52 ?!古すぎる!
az upgrade でのバージョンアップにも対応していない古いバージョンでした。
ということで、最新版にアップグレードします。
PS> winget install -e --id Microsoft.AzureCLI
次に、テナントが意識されるようになりましたので、環境変数として AZURE_TENANT_ID をセットします。
詳細はこちらが詳しいです。
https://azuresdkdocs.blob.core.windows.net/$web/dotnet/Azure.Security.KeyVault.Secrets/4.4.0/api/index.html
複数のテナントがある場合は、プログラム内で SecretClient を使う前に KeyVaul AZURE_TENANT_ID をセットします。
static string GetHimichuString()
{
string keyVaultUrl = "https://himichumoji.vault.azure.net/"; //コンテナのURL
try
{
Environment.SetEnvironmentVariable("AZURE_TENANT_ID", "aaaaaaaa-6800-9801-8801-bbbbbbbbbbbb");
var client = new SecretClient(new Uri(keyVaultUrl), new DefaultAzureCredential());
KeyVaultSecret secret = client.GetSecret("himichu");
return secret.Value;
}
catch(Exception ex){
}
}
プログラムを次項したところ、はい、うまく動きました。
学びのポイント
ツール類のバージョンアップはときどきちゃんとしておくべし。