我正在尝试通过Java中的托管服务身份(MSI)读取Azure Key Vault中的秘密。我希望令牌通过MSI访问密钥库。
有可供.net使用的参考资料
但没有在Java中找到任何东西。我不想通过客户端ID /秘密密钥或证书来执行此操作。
我想要Java中接近以下.net代码的内容
using Microsoft.Azure.KeyVault;
using Microsoft.Azure.Services.AppAuthentication;
AzureServiceTokenProvider azureServiceTokenProvider = new AzureServiceTokenProvider();
try
{
var keyVaultClient = new KeyVaultClient(new KeyVaultClient.AuthenticationCallback(azureServiceTokenProvider.KeyVaultTokenCallback));
var secret = await keyVaultClient.GetSecretAsync("https://abcded.vault.azure.net/secrets/secretname/").ConfigureAwait(false);
ViewBag.Secret = $"Secret: {secret.Value}";
}
catch (Exception exp)
{
ViewBag.Error = $"Something went wrong: {exp.Message}";
}
最佳答案
我们可以在Java中使用AppServiceMSICredentials。请尝试使用以下代码。
import com.microsoft.azure.AzureEnvironment;
import com.microsoft.azure.credentials.AppServiceMSICredentials;
import com.microsoft.azure.keyvault.KeyVaultClient;
import com.microsoft.azure.keyvault.models.KeyBundle;
AppServiceMSICredentials credentials = new AppServiceMSICredentials(AzureEnvironment.AZURE);
KeyVaultClient keyVaultClient = new KeyVaultClient(credentials);
keyVaultClient.getSecret("https://xxxx.vault.azure.net","secretName");