存储 Azure Vault 客户端 ID 和客户端密码

Storing Azure Vault Client ID and Client Secret

我正在使用 .NET Core 2.0 和 ASP.NET Core 2.0 进行应用程序开发。 "test" 应用程序是一个 .NET Core 控制台应用程序。我正在编写的核心代码是一个 class 库。一旦适当的测试。我选择这样做是因为我暂时不会使用它(它正在替换旧的 ASPNET 代码)。

无论如何,由于我必须为各种服务使用大量 API 密钥,所以我决定使用 Microsoft Azure Key Vault 来存储密钥。我已经完成了所有设置并了解其工作原理。测试应用程序使用测试 Azure 帐户,因此它并不重要。由于这是在替换遗留代码并且处于起步阶段,因此我是唯一的开发人员。

基本上,我 运行 关注这个问题。据我所知,关于 Azure Key Vault 的信息不多。许多示例将 Client ID 和 Secret 存储在 纯文本 json 文件中(例如:https://www.humankode.com/asp-net-core/how-to-store-secrets-in-azure-key-vault-using-net-core ).我真的不明白这怎么能安全。如果有人获得这些密钥,他们就可以轻松访问存储在 Azure 中的信息,对吧?

Microsoft MSDN 有一个授予访问权限的 powershell 命令(我丢失了原来的 link,这是我能找到的最接近的:https://www.red-gate.com/simple-talk/cloud/platform-as-a-service/setting-up-and-configuring-an-azure-key-vault/)我的开发操作系统是 Windows 10我的主要服务器操作系统是 Debian。

我该如何处理?

是的,你是对的,纯文本配置文件只能在开发期间使用,不能用于生产目的。通常,可用选项取决于您托管应用程序的位置和方式。

如果您有 Azure Web 应用程序,则至少有下一个内置选项 (from the documentation):

  • add the ClientId and ClientSecret values for the AppSettings in the Azure portal. By doing this, the actual values will not be in the web.config but protected via the Portal where you have separate access control capabilities. These values will be substituted for the values that you entered in your web.config. Make sure that the names are the same.

  • authenticate an Azure AD application is by using a Client ID and a Certificate instead of a Client ID and Client Secret. Following are the steps to use a Certificate in an Azure Web App:

    • Get or Create a Certificate
    • Associate the Certificate with an Azure AD application
    • Add code to your Web App to use the Certificate
    • Add a Certificate to your Web App

您可能还会发现一种使用环境变量存储凭据的方法。仅当您可以保证不可能在生产机器上对 env 变量进行快照时,这才可以。查看 Environment Variables Considered Harmful for Your Secrets 了解更多详情。


最后一件事:还有一个基于这个想法的技术,你只需要 store/pass 一个 ClientSecret 值,而 ClientId 应该基于 machine/container 详细信息构建应用已托管(例如 docker 容器 ID)。我找到了一个 Hashicorp Vault 的例子和一个在 AWS 上托管的应用程序,但总体思路是一样的:Secret management with Vault

除了第一个答案,在 Azure VM 上使用 运行 应用程序的上下文,而不是使用 client_secret 进行身份验证,您可以使用此 [=18] 中解释的客户端证书身份验证=].

上图中:

  • 应用程序通过证明它具有证书的私钥(如果您使用 Windows,它基本上存储在 CNG 中)来向 AAD 进行身份验证。
  • 应用程序取回 access_token,然后使用它访问 Key Vault。

开发者不需要知道证书的私钥值就可以成功验证他们的应用程序。相反,他们只需要知道证书存储中导入的 pfx(私钥及其证书的容器)的位置。

至少在Windows,你作为秘密管理员可以将私钥和证书转换成pfx格式,密码保护,然后将其部署到 Windows 证书存储区。这样一来,除非知道 pfx 文件的 密码 ,否则没人能知道私钥。


Azure Compute 的另一种具体方法是使用 Azure Managed Service Identity。使用 Azure MSI,Azure 将自动分配您的资源,例如具有身份/服务主体的 VM,您可以在特定端点发出请求,该端点只能由您的资源访问以获取 access_token。但请注意,Azure MSI 仍在 public 预览版中,因此请在使用前查看已知问题。

上图解释了 Azure 资源管理器如何为您的 VM 分配服务主体身份。

  • 当您在 VM 中启用 MSI 时,Azure 将在您的 AAD 中创建一个服务主体。
  • Azure 随后会将新的 MSI VM 扩展部署到您的 VM。这在 http://localhost:50432/oauth2/token 处提供了一个端点,用于获取服务主体的 access_token
  • 然后您可以使用 access_token 访问授权服务主体访问的资源,例如 Key Vault。