无法使用App Service上的Azure MSI访问Key Vault

问题描述 投票:5回答:5

我已在App Service上启用托管服务身份。但是,我的WebJobs似乎无法访问密钥。

他们举报:

Tried the following 3 methods to get an access token, but none of them worked. Parameters: Connectionstring: [No connection string specified], Resource: https://vault.azure.net, Authority: . Exception Message: Tried to get token using Managed Service Identity. Unable to connect to the Managed Service Identity (MSI) endpoint. Please check that you are running on an Azure resource that has MSI setup. Parameters: Connectionstring: [No connection string specified], Resource: https://vault.azure.net, Authority: https://login.microsoftonline.com/common. Exception Message: Tried to get token using Active Directory Integrated Authentication. Access token could not be acquired. password_required_for_managed_user: Password is required for managed user Parameters: Connectionstring: [No connection string specified], Resource: https://vault.azure.net, Authority: . Exception Message: Tried to get token using Azure CLI. Access token could not be acquired. 'az' is not recognized as an internal or external command,

工藤未显示任何MSI_环境变量。

这应该如何工作?这是现有的应用程序服务计划。

azure azure-webjobs azure-web-app-service azure-keyvault azure-managed-identity
5个回答
3
投票

AppAuthentication库利用App Service中的内部端点来代表您的网站接收令牌。该端点是非静态的,因此被设置为环境变量。通过ARM为您的站点激活MSI后,将需要重新启动站点以在其中设置两个新的环境变量:

MSI_ENDPOINTMSI_SECRET

这些变量的存在对于MSI功能在运行时正常运行至关重要,因为AppAuthentication库使用它们来获取授权令牌。错误消息反映了这一点:

异常消息:尝试使用托管服务标识来获取令牌。无法连接到Managed Service Identity(MSI)端点。请检查您是否正在运行具有MSI安装程序的Azure资源。

如果缺少这些变量,则可能需要重新启动站点。

https://docs.microsoft.com/en-us/azure/app-service/app-service-managed-service-identity

如果设置了环境变量,但是您仍然看到相同的错误,则上面的文章中有一个代码示例,显示了如何手动将请求发送到该端点。

public static async Task<HttpResponseMessage> GetToken(string resource, string apiversion)  {
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Add("Secret", Environment.GetEnvironmentVariable("MSI_SECRET"));
return await client.GetAsync(String.Format("{0}/?resource={1}&api-version={2}", Environment.GetEnvironmentVariable("MSI_ENDPOINT"), resource, apiversion));

}

我会尝试一下,看看我能得到什么样的回应。


2
投票

尽管我已经设置了环境变量,但在尝试将MSI与Function应用程序一起使用时,我刚刚解决了此问题。我尝试重新启动多次,但未成功。我最终要做的是手动关闭该功能的MSI,然后重新启用它。这不是理想的方法,但确实有效。

希望有帮助!


1
投票

我发现,如果启用MSI,然后换出插槽,则功能会随着插槽更改而消失。您可以通过将其关闭然后再打开来重新启用它,但这将在AD中创建一个新的身份,并且需要您重置密钥库的权限才能使它起作用。


0
投票

就我而言,我忘记了在密钥库中为该应用程序添加访问策略


0
投票

对于那些人,就像我自己,想知道如何启用MSI

我的情况:我已经部署了App Service并运行了很长时间。另外,在Azure DevOps上,我已将管道配置为自动交换 部署槽(暂存/生产)。突然,在正常推送之后,由于上述问题,生产开始失败。

因此,为了启用MSI 再次(我不知道为什么必须重新启用它,但我相信这只是一种解决方法,而不是解决方案,因为它应该在第一个中仍然启用地方)

转到您的应用服务。然后在Settings-> Identity下。检查状态:就我而言,它是off:(

我在下面附加了一张图片,以使后续操作更容易。

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.