Azure 虚拟机中托管的应用程序如何从 Azure 密钥保管库获取密钥和机密?

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

Azure 虚拟机(生产实例)中托管的应用程序如何从 Azure 密钥保管库获取密钥和机密?

我指的是这个链接。它说“为虚拟机创建系统分配的标识”

我的问题是,是否只是分配系统分配的托管身份将允许Azure VM中托管的应用程序(在Azure VM的IIS中)从密钥保管库获取凭据(密钥/秘密等..)要运行的应用程序?或者还需要进行任何额外的配置?如果有,那是什么?

请注意,Azure 虚拟机和密钥保管库位于同一 Azure 资源组中。

编辑:

我正在尝试使用以下步骤:

 Step1: From VM turn on the managed identity for that VM
 Step2: From KV in the access policy, added VM MI to access the KV 
        in the access policy.

 Step3: Now to test it, what is the exact PowerShell command I 
        should use from the VM?

 Step4: I think, once I use the PowerShell command, I can Invoke 
        the rest method and replace my KV URI and secret name.

 Step5: Then use the same REST API in my application.

我不太清楚如何准确地执行此操作(以上 3 个步骤)。请帮忙。谢谢

注意:从网络上,我看到人们使用以下命令。但不确定,那个 IP 地址是什么(里面有 HTTP - 我认为这不安全)?这是 MSFT 建议的 IP 地址和确切命令吗? 无法从 MSFT 教程中获取任何内容,是否有任何 MSFT 教程链接可以获取确切的详细信息?

$Response = Invoke-RestMethod -uri 'http://169.254.169.254/metadata/identity/oauth2/token?api-version=2018-02-01&resource=https%3F%2A%2Fvault.azure.net' -Method GET -Headers @{Metadata="true"}

$KeyVaultToken = $Response.access_token
azure azure-keyvault
1个回答
0
投票

Azure 虚拟机中托管的应用程序如何从 Azure 密钥保管库获取密钥和机密?

创建 Key Vault 时,请确保选择资源访问配置。

注意: 无需生成令牌即可从 VM 内访问 Key Vault;仅当从 VM 外部访问 Key Vault 时才需要令牌。

enter image description here

确保将虚拟机的托管身份添加到具有所需权限的 Key Vault 访问策略。

enter image description here

分配角色后,请确保使用以下命令以虚拟机的托管身份登录

Add-AzAccount -identity

虚拟机中的身份登录

enter image description here

以下是使用虚拟机的托管身份和默认凭据从 Key Vault 获取 keys

secrets
的代码。 托管身份将自动处理登录。

using System;
using Azure.Core;
using Azure.Identity;
using Azure.Security.KeyVault.Secrets;
using Azure.Security.KeyVault.Keys;

class Program
{
    static void Main(string[] args)
    {
        string keyVaultName = "VM-Keyvault-Demo";
        var kvUri = "https://VM-Keyvault-Demo.vault.azure.net";

        
        SecretClientOptions secretOptions = new SecretClientOptions()
        {
            Retry =
            {
                Delay= TimeSpan.FromSeconds(2),
                MaxDelay = TimeSpan.FromSeconds(16),
                MaxRetries = 5,
                Mode = RetryMode.Exponential
            }
        };

        var secretClient = new SecretClient(new Uri(kvUri), new DefaultAzureCredential(), secretOptions);

        
        KeyClientOptions keyOptions = new KeyClientOptions()
        {
            Retry =
            {
                Delay = TimeSpan.FromSeconds(2),
                MaxDelay = TimeSpan.FromSeconds(16),
                MaxRetries = 5,
                Mode = RetryMode.Exponential
            }
        };

        var keyClient = new KeyClient(new Uri(kvUri), new DefaultAzureCredential(), keyOptions);

        
        Console.WriteLine("Listing all secrets in " + keyVaultName + ":");
        foreach (var secret in secretClient.GetPropertiesOfSecrets())
        {
            Console.WriteLine($"- Secret Name: {secret.Name}");
        }

        
        Console.WriteLine("\nListing all keys in " + keyVaultName + ":");
        foreach (var key in keyClient.GetPropertiesOfKeys())
        {
            Console.WriteLine($"- Key Name: {key.Name}");
        }
    }
}

输出

enter image description here

参考: 将 Azure Key Vault 与 .NET 中的虚拟机结合使用

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