教程:使用 Linux VM 系统分配的托管标识访问 Azure Key VaultTutorial: Use a Linux VM system-assigned managed identity to access Azure Key Vault

Azure 资源的托管标识是 Azure Active Directory 的一项功能。Managed identities for Azure resources is a feature of Azure Active Directory. 支持 Azure 资源的托管标识的每个 Azure 服务都受其自己的时间线限制。Each of the Azure services that support managed identities for Azure resources are subject to their own timeline. 在开始之前,请务必查看资源的托管标识的可用性状态以及已知问题Make sure you review the availability status of managed identities for your resource and known issues before you begin.

本教程介绍如何使用 Linux 虚拟机 (VM) 的系统分配托管标识访问 Azure Key Vault。This tutorial shows you how to use a system-assigned managed identity for a Linux virtual machine (VM) to access Azure Key Vault. 作为引导,Key Vault 随后可让客户端应用程序使用机密访问未受 Azure Active Directory (AD) 保护的资源。Serving as a bootstrap, Key Vault makes it possible for your client application to then use the secret to access resources not secured by Azure Active Directory (AD). Azure 资源的托管标识由 Azure 自动管理,可用于向支持 Azure AD 身份验证的服务进行身份验证,这样就无需在代码中插入凭据了。Managed identities for Azure resources are automatically managed by Azure and enable you to authenticate to services that support Azure AD authentication, without needing to insert credentials into your code.

你将学习如何执行以下操作:You learn how to:

  • 授予 VM 对 Key Vault 中存储的密钥的访问权限Grant your VM access to a secret stored in a Key Vault
  • 使用 VM 标识获取访问令牌,并使用它来检索 Key Vault 中的密钥Get an access token using the VM's identity and use it to retrieve the secret from the Key Vault  

先决条件Prerequisites

授予 VM 对 Key Vault 中存储的密钥的访问权限Grant your VM access to a Secret stored in a Key Vault  

使用 Azure 资源的托管服务标识,代码可以获取访问令牌,对支持 Azure Active Directory 身份验证的资源进行身份验证。Using managed service identities for Azure resources your code can get access tokens to authenticate to resources that support Azure Active Directory authentication. 但是,并非所有 Azure 服务都支持 Azure AD 身份验证。 However, not all Azure services support Azure AD authentication. 若要将 Azure 资源的托管标识用于这些服务,请将服务凭据存储在 Azure Key Vault 中,然后使用 Azure 资源的托管标识访问 Key Vault 以检索凭据。 To use managed identities for Azure resources with those services, store the service credentials in Azure Key Vault, and use managed identities for Azure resources to access Key Vault to retrieve the credentials. 

首先,我们需要创建一个 Key Vault 并授予 VM 的系统分配托管标识对 Key Vault 的访问权限。First, we need to create a Key Vault and grant our VM’s system-assigned managed identity access to the Key Vault.   

  1. 在左侧导航栏的顶部,依次选择“创建资源” > “安全 + 标识” > “Key Vault” 。At the top of the left navigation bar, select Create a resource > Security + Identity > Key Vault.  

  2. 为新 Key Vault 提供一个名称 。Provide a Name for the new Key Vault. 

  3. 定位到之前创建的 VM 所在的订阅和资源组中的 Key Vault。Locate the Key Vault in the same subscription and resource group as the VM you created earlier. 

  4. 选择“访问策略” ,然后单击“添加新” 。Select Access policies and click Add new. 

  5. 在模板中的“配置”中,选择“密钥管理” 。In Configure from template, select Secret Management. 

  6. 选择“选择主体” ,并在搜索字段中输入之前创建的 VM 的名称。Choose Select Principal, and in the search field enter the name of the VM you created earlier.  选择结果列表中的 VM,并单击“选择” 。  Select the VM in the result list and click Select. 

  7. 单击“确定” 完成添加新的访问策略,然后单击“确定” 完成访问策略选择。Click OK to finishing adding the new access policy, and OK to finish access policy selection. 

  8. 单击“创建” 完成创建 Key Vault。Click Create to finish creating the Key Vault. 

    Alt 图像文本

接下来,将密钥添加到 Key Vault,以便稍后可以使用在 VM 中运行的代码检索此密钥:Next, add a secret to the Key Vault, so that later you can retrieve the secret using code running in your VM: 

  1. 选择“所有资源” ,找到并选择已创建的 Key Vault。Select All Resources, and find and select the Key Vault you created. 
  2. 选择“密钥” ,然后单击“添加” 。Select Secrets, and click Add. 
  3. 从“上传选项” 中选择“手动” 。Select Manual, from Upload options. 
  4. 输入密钥的名称和值。Enter a name and value for the secret.  该值可以是任何需要的内容。  The value can be anything you want. 
  5. 明确指定激活日期和到期日期,并将“已启用” 设置为“是” 。Leave the activation date and expiration date clear, and leave Enabled as Yes. 
  6. 单击“创建” 以创建密钥。Click Create to create the secret.   

使用 VM 标识获取访问令牌,并使用它来检索 Key Vault 中的密钥Get an access token using the VM's identity and use it to retrieve the secret from the Key Vault  

若要完成这些步骤,需要使用 SSH 客户端。To complete these steps, you need an SSH client.  如果使用的是 Windows,可以在适用于 Linux 的 Windows 子系统中使用 SSH 客户端。  If you are using Windows, you can use the SSH client in the Windows Subsystem for Linux. 如果需要有关配置 SSH 客户端密钥的帮助,请参阅如何在 Azure 上将 SSH 密钥与 Windows 配合使用如何创建和使用适用于 Azure 中 Linux VM 的 SSH 公钥和私钥对If you need assistance configuring your SSH client's keys, see How to Use SSH keys with Windows on Azure, or How to create and use an SSH public and private key pair for Linux VMs in Azure.  

  1. 在门户中,转到 Linux VM,并单击“概述” 中的“连接” 。In the portal, navigate to your Linux VM and in the Overview, click Connect. 

  2. 使用所选的 SSH 客户端连接 到 VM。Connect to the VM with the SSH client of your choice. 

  3. 在终端窗口中,使用 CURL 向 Azure 资源终结点的本地托管标识发出请求,以获取 Azure Key Vault 的访问令牌。In the terminal window, using CURL, make a request to the local managed identities for Azure resources endpoint to get an access token for Azure Key Vault.     下面是用于获取访问令牌的 CURL 请求。The CURL request for the access token is below.  

    curl 'http://169.254.169.254/metadata/identity/oauth2/token?api-version=2018-02-01&resource=https%3A%2F%2Fvault.azure.cn' -H Metadata:true  
    

    响应包括访问资源管理器所需的访问令牌。The response includes the access token you need to access Resource Manager. 

    响应:Response:  

    {"access_token":"eyJ0eXAi...",
    "refresh_token":"",
    "expires_in":"3599",
    "expires_on":"1504130527",
    "not_before":"1504126627",
    "resource":"https://vault.azure.cn",
    "token_type":"Bearer"} 
    

    可以使用此访问令牌对 Azure Key Vault 进行身份验证。You can use this access token to authenticate to Azure Key Vault.  下一个 CURL 请求显示如何使用 CURL 和 Key Vault REST API 从 Key Vault 读取密钥。  The next CURL request shows how to read a secret from Key Vault using CURL and the Key Vault REST API.  将需要 Key Vault 的 URL,该 URL 位于 Key Vault 的“概述” 页的“软件包” 部分。  You’ll need the URL of your Key Vault, which is in the Essentials section of the Overview page of the Key Vault.  另外,还需要在前面的调用中获取的访问令牌。  You will also need the access token you obtained on the previous call. 

    curl https://<YOUR-KEY-VAULT-URL>/secrets/<secret-name>?api-version=2016-10-01 -H "Authorization: Bearer <ACCESS TOKEN>" 
    

    响应将如下所示:The response will look like this: 

    {"value":"p@ssw0rd!","id":"https://mytestkeyvault.vault.azure.cn/secrets/MyTestSecret/7c2204c6093c4d859bc5b9eff8f29050","attributes":{"enabled":true,"created":1505088747,"updated":1505088747,"recoveryLevel":"Purgeable"}} 
    

在检索 Key Vault 中的密钥后,可以使用该密钥对需要名称和密码的服务进行身份验证。Once you’ve retrieved the secret from the Key Vault, you can use it to authenticate to a service that requires a name and password.

后续步骤Next steps

在本教程中,你已学习了如何使用 Linux VM 系统分配的托管标识来访问 Azure Key Vault。In this tutorial, you learned how to use a Linux VM system-assigned managed identity to access Azure Key Vault. 若要详细了解 Azure Key Vault,请参阅:To learn more about Azure Key Vault see: