简体   繁体   English

使用MSI部署容器实例和部署到其的容器无法读取密钥库机密

[英]Deploying Container Instance with MSI and containers deployed to it cannot read keyvault secrets

I followed this guide from Microsoft: https://docs.microsoft.com/en-us/azure/container-instances/container-instances-managed-identity 我遵循了Microsoft的本指南: https : //docs.microsoft.com/en-us/azure/container-instances/container-instances-managed-identity

All that works fine, but when I want to deploy with a ASP.NET Core 2.1 inside a container, where in my code I try to read the KeyVault secrets it does not work. 一切正常,但是当我想在容器中使用ASP.NET Core 2.1进行部署时,在我的代码中尝试读取KeyVault机密的地方不起作用。 It will work without deploying it inside a Docker container though. 它可以工作,但无需将其部署在Docker容器中。

But the goal is to deploy the project into a docker container, register it into Azure Container Registry and then create a Container Instance with containers that can read keyvault secrets. 但目标是将项目部署到docker容器中,将其注册到Azure容器注册表中,然后使用可读取密钥库机密的容器创建容器实例。

The error I get is typically what I would get when trying to read secrets from Keyvaults inside a running .NET core Docker container: 我收到的错误通常是尝试从运行的.NET核心Docker容器中的Keyvaults读取机密时遇到的错误:

AzureServiceTokenProviderException: Parameters: Connectionstring: [No connection string specified], Resource: https://vault.azure.net , Authority: https://login.windows.net/ . AzureServiceTokenProviderException:参数:Connectionstring:[未指定连接字符串],资源: https ://vault.azure.net,权限: https ://login.windows.net/。 Exception Message: 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: https://login.windows.net/ . 参数:Connectionstring:[未指定连接字符串],Resource: https : //vault.azure.net,Authorityhttps : //login.windows.net/ Exception Message: Tried to get token using Managed Service Identity. 异常消息:尝试使用托管服务身份获取令牌。 Unable to connect to the Managed Service Identity (MSI) endpoint. 无法连接到托管服务标识(MSI)端点。 Please check that you are running on an Azure resource that has MSI setup. 请检查您是否正在运行具有MSI安装程序的Azure资源。 Parameters: Connectionstring: [No connection string specified], Resource: https://vault.azure.net , Authority: https://login.windows.net/ . 参数:Connectionstring:[未指定连接字符串],Resource: https : //vault.azure.net,Authorityhttps : //login.windows.net/ Exception Message: Tried to get token using Visual Studio. 异常消息:尝试使用Visual Studio获取令牌。 Access token could not be acquired. 无法获取访问令牌。 Environment variable LOCALAPPDATA not set. 未设置环境变量LOCALAPPDATA。 Parameters: Connectionstring: [No connection string specified], Resource: https://vault.azure.net , Authority: https://login.windows.net/ . 参数:Connectionstring:[未指定连接字符串],Resource: https : //vault.azure.net,Authorityhttps : //login.windows.net/ Exception Message: Tried to get token using Azure CLI. 异常消息:尝试使用Azure CLI获取令牌。 Access token could not be acquired. 无法获取访问令牌。 /bin/bash: az: No such file or directory. / bin / bash:az:没有这样的文件或目录。

Any guide to get Docker containers read from Azure Keyvault secrets? 是否有从Azure Keyvault机密中读取Docker容器的指南?

The source code for reading a keyvault secrets: 读取密钥库机密的源代码:

  public static async Task<string> GetSecret(string baseUrl, string keyName)
    {
        AzureServiceTokenProvider azureServiceTokenProvider = new AzureServiceTokenProvider();

        using (var keyVaultClient = new KeyVaultClient(new KeyVaultClient.AuthenticationCallback(azureServiceTokenProvider.KeyVaultTokenCallback)))
        {
            var secret = await keyVaultClient.GetSecretAsync(baseUrl, keyName).ConfigureAwait(false);
            return secret
        }
    }

First of all, you should know managed identities is just the feather of Azure Services. 首先,您应该知道托管身份只是Azure服务的羽毛。 Also, even if for Azure Services, not all the service are supported for the feather. 此外,即使对于Azure Services,也不支持羽毛所有的服务。 You can know which Azure service support the managed identities here . 您可以在此处了解哪种Azure服务支持托管身份。

And you can take a look that how does the MSI work for Azure Service here . 您可以在此处查看 MSI如何用于Azure Service。 So it seems that you cannot use the MSI for the Docker container which does not belong to Azure inside the Azure Container Instance. 因此,似乎无法将MSI用于不属于Azure容器实例内部Azure的Docker容器。 The error also shows that: 该错误还显示:

Exception Message: Tried to get token using Managed Service Identity. 异常消息:尝试使用托管服务身份获取令牌。 Unable to connect to the Managed Service Identity (MSI) endpoint. 无法连接到托管服务标识(MSI)端点。 Please check that you are running on an Azure resource that has MSI setup. 请检查您是否正在运行具有MSI安装程序的Azure资源。

But I suggest you can try to use the Service Principal to read the key stored in KeyVault or access other Azure Service. 但是我建议您可以尝试使用服务主体来读取存储在KeyVault中的密钥或访问其他Azure服务。

Seems that I was not aware using AppAuthentication NuGet 1.1.0-preview produced the error. 似乎我不知道使用AppAuthentication NuGet 1.1.0-preview产生了错误。 Use 1.0.3 and everything works fine inside azure container instance with a container image running :) 使用1.0.3,在运行容器图像的azure容器实例中,一切正常:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM