您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

快速入门:使用 Azure CLI 在 Azure Key Vault 中设置和检索机密Quickstart: Set and retrieve a secret from Azure Key Vault using Azure CLI

在本快速入门中,你将使用 Azure CLI 在 Azure Key Vault 中创建一个密钥保管库。In this quickstart, you create a key vault in Azure Key Vault with Azure CLI. Azure Key Vault 是一项云服务,用作安全的机密存储。Azure Key Vault is a cloud service that works as a secure secrets store. 可以安全地存储密钥、密码、证书和其他机密。You can securely store keys, passwords, certificates, and other secrets. 有关 Key Vault 的详细信息,可以参阅概述For more information on Key Vault you may review the Overview. Azure CLI 用于通过命令或脚本创建和管理 Azure 资源。Azure CLI is used to create and manage Azure resources using commands or scripts. 完成该操作后,即可存储机密。Once that you have completed that, you will store a secret.

如果还没有 Azure 订阅,可以在开始前创建一个免费帐户If you don't have an Azure subscription, create a free account before you begin.

先决条件Prerequisites

  • Azure Cloud Shell 中使用 Bash 环境。Use the Bash environment in Azure Cloud Shell.

    在新窗口中启动 Cloud ShellLaunch Cloud Shell in a new window

  • 如果需要,请安装 Azure CLI 来运行 CLI 参考命令。If you prefer, install the Azure CLI to run CLI reference commands.

    • 如果使用的是本地安装,请使用 az login 命令登录到 Azure CLI。If you're using a local installation, sign in to the Azure CLI by using the az login command. 若要完成身份验证过程,请遵循终端中显示的步骤。To finish the authentication process, follow the steps displayed in your terminal. 有关其他登录选项,请参阅登录 Azure CLIFor additional sign-in options, see Sign in with the Azure CLI.

    • 出现提示时,请在首次使用时安装 Azure CLI 扩展。When you're prompted, install Azure CLI extensions on first use. 有关扩展详细信息,请参阅使用 Azure CLI 的扩展For more information about extensions, see Use extensions with the Azure CLI.

    • 运行 az version 以查找安装的版本和依赖库。Run az version to find the version and dependent libraries that are installed. 若要升级到最新版本,请运行 az upgradeTo upgrade to the latest version, run az upgrade.

  • 本快速入门需要 Azure CLI 版本 2.0.4 或更高版本。This quickstart requires version 2.0.4 or later of the Azure CLI. 如果使用 Azure Cloud Shell,则最新版本已安装。If using Azure Cloud Shell, the latest version is already installed.

创建资源组Create a resource group

资源组是在其中部署和管理 Azure 资源的逻辑容器。A resource group is a logical container into which Azure resources are deployed and managed. 使用 az group create 命令在 eastus 位置创建一个名为 myResourceGroup 的资源组 。Use the az group create command to create a resource group named myResourceGroup in the eastus location.

az group create --name "myResourceGroup" -l "EastUS"

创建密钥保管库Create a key vault

使用 Azure CLI az keyvault create 命令在上一步骤的资源组中创建 Key Vault。Use the Azure CLI az keyvault create command to create a Key Vault in the resource group from the previous step. 需要提供某些信息:You will need to provide some information:

  • Key Vault 名称:由 3 到 24 个字符构成的字符串,只能包含数字 (0-9)、字母(a-z、A-Z)和连字符 (-)Key vault name: A string of 3 to 24 characters that can contain only numbers (0-9), letters (a-z, A-Z), and hyphens (-)

    重要

    每个密钥保管库必须具有唯一的名称。Each key vault must have a unique name. 在以下示例中,将 替换为密钥保管库的名称。Replace with the name of your key vault in the following examples.

  • 资源组名称:myResourceGroup。Resource group name: myResourceGroup.

  • 位置:EastUS。The location: EastUS.

az keyvault create --name "<your-unique-keyvault-name>" --resource-group "myResourceGroup" --location "EastUS"

此命令的输出会显示新建的 Key Vault 的属性。The output of this command shows properties of the newly created key vault. 请记下下面列出的两个属性:Take note of the two properties listed below:

  • 保管库名称:为上面的 --name 参数提供的名称。Vault Name: The name you provided to the --name parameter above.
  • 保管库 URI:在本示例中为 https://<your-unique-keyvault-name>.vault.azure.net/。Vault URI: In the example, this is https://<your-unique-keyvault-name>.vault.azure.net/. 通过其 REST API 使用保管库的应用程序必须使用此 URI。Applications that use your vault through its REST API must use this URI.

目前,只有你的 Azure 帐户才有权对这个新保管库执行任何操作。At this point, your Azure account is the only one authorized to perform any operations on this new vault.

向 Key Vault 添加机密Add a secret to Key Vault

只需再执行几个步骤即可向保管库添加机密。To add a secret to the vault, you just need to take a couple of additional steps. 此密码可供应用程序使用。This password could be used by an application. 此密码将名为 ExamplePassword,将在其中存储的值为 hVFkk965BuUvThe password will be called ExamplePassword and will store the value of hVFkk965BuUv in it.

使用以下 Azure CLI az keyvault secret set 命令在密钥保管库中创建一个名为 ExamplePassword 的机密,该机密将存储值 hVFkk965BuUvUse the Azure CLI az keyvault secret set command below to create a secret in Key Vault called ExamplePassword that will store the value hVFkk965BuUv :

az keyvault secret set --vault-name "<your-unique-keyvault-name>" --name "ExamplePassword" --value "hVFkk965BuUv"

现在,可以通过使用密码的 URI,引用已添加到 Azure Key Vault 的此密码。You can now reference this password that you added to Azure Key Vault by using its URI. 使用“https://.vault.azure.net/secrets/ExamplePassword”来获取当前版本。Use 'https://.vault.azure.net/secrets/ExamplePassword' to get the current version.

若要查看机密中包含的纯文本形式的值,请执行以下命令:To view the value contained in the secret as plain text:

az keyvault secret show --name "ExamplePassword" --vault-name "<your-unique-keyvault-name>" --query "value"

现在,你已创建 Key Vault 并存储和检索了机密。Now, you have created a Key Vault, stored a secret, and retrieved it.

清理资源Clean up resources

本系列中的其他快速入门和教程是在本快速入门的基础上制作的。Other quickstarts and tutorials in this collection build upon this quickstart. 如果打算继续使用后续的快速入门和教程,则可能需要保留这些资源。If you plan to continue on to work with subsequent quickstarts and tutorials, you may wish to leave these resources in place.

如果不再需要资源组和所有相关的资源,可以使用 Azure CLI az group delete 命令将其删除:When no longer needed, you can use the Azure CLI az group delete command to remove the resource group and all related resources:

az group delete --name "myResourceGroup"

后续步骤Next steps

在本快速入门中,你创建了 Key Vault 并在其中存储了一个机密。In this quickstart you created a Key Vault and stored a secret in it. 若要详细了解 Key Vault 以及如何将其与应用程序集成,请继续阅读以下文章。To learn more about Key Vault and how to integrate it with your applications, continue on to the articles below.