如何将 Azure 密钥保管库中存储的机密值直接连接到 Azure 应用服务环境变量

问题描述 投票:0回答:1

我有一个密钥保管库和一个托管在 Azure 上的应用服务。我在该密钥保管库中存储了一个秘密,我想直接在应用程序服务中使用它的值。我的意思是直接我能够覆盖环境。直接使用秘密值的变量,无需任何代码干预,如屏幕截图所示。 enter image description here

我为应用程序服务创建了一个托管身份,并从密钥保管库授予其访问角色,它们完美连接。 我尝试使用此模式 @Microsoft.KeyVault(SecretUri=https://key-vault.vault.azure.net/secrets/encryption-key) 来尝试获取该值但没有成功。 因此,我需要一个解决方案,而不需要使用电源自动化来自动化流程。我需要一个 Azure 上的解决方案,使我能够读取并获取密钥保管库中的值,并将其放入应用程序服务的 env.variables 中。

azure azure-keyvault azure-appservice
1个回答
0
投票
  • 首先创建您的机密并提供 Key Vault 访问所需的权限。

enter image description here

我的

Secrets

enter image description here

  • 在 Key Vault 中,通过选择主体作为部署的应用程序名称,使用
    Key & Secret Management
    创建新的访问策略。

enter image description here

将其放入应用服务的env.variables中

  • 您可以在环境变量中手动添加带有密钥保管库引用的键值。

  • 您只需引用秘密名称(不包含标识符id)即可获取最新值。

@Microsoft.KeyVault(SecretUri=https://HarshuKVJuly.vault.azure.net/secrets/SampleSecret)

enter image description here

  • 请参阅此MSDoc,它解释了相同的内容。

  • Secret 可能有多个版本。

enter image description here

  • 如果您在 Identifier 中使用 Secret ID,则可以使用
    Pull reference values
    获取最新值。

enter image description here

  • 您可以在KUDU中查看应用程序设置环境变量。

enter image description here

  • 现在您可以直接在
    .cshtml
    页面中使用环境变量检索KeyVault的值。
@page
 
@model IndexModel
@{
    ViewData["Title"] = "Home page";
    var SampleSecret = Environment.GetEnvironmentVariable("SampleSecret");
}

<div class="text-center">
    <h1 class="display-4">Welcome</h1>
<p>@SampleSecret</p></div>

enter image description here

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