你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

快速入门:使用 ARM 模板从Azure 密钥保管库设置和检索机密

Azure 密钥保管库是一种云服务,它为机密(如密钥、密码、证书和其他机密)提供安全存储。 本快速入门重点介绍如何部署Azure 资源管理器模板(ARM 模板)来创建密钥保管库和机密。

Azure 资源管理器模板是一个 JavaScript 对象表示法(JSON)文件,用于定义项目的基础结构和配置。 模板使用声明性语法。 你可以在不编写用于创建部署的编程命令序列的情况下,描述预期部署。

如果你的环境满足先决条件,并且你熟悉使用 ARM 模板,请选择 部署到 Azure 按钮。 模板将在Azure门户中打开。

用于将 资源管理器 模板部署到 Azure 的按钮。

先决条件

若要完成本文,

  • 如果没有Azure订阅,请在开始前创建 free 帐户

查看模板

本快速入门中使用的模板来自 Azure 快速入门模板

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "metadata": {
    "_generator": {
      "name": "bicep",
      "version": "0.42.1.51946",
      "templateHash": "10998800669048245550"
    }
  },
  "parameters": {
    "keyVaultName": {
      "type": "string",
      "metadata": {
        "description": "Specifies the name of the key vault."
      }
    },
    "location": {
      "type": "string",
      "defaultValue": "[resourceGroup().location]",
      "metadata": {
        "description": "Specifies the Azure location where the key vault should be created."
      }
    },
    "enabledForDeployment": {
      "type": "bool",
      "defaultValue": false,
      "metadata": {
        "description": "Specifies whether Azure Virtual Machines are permitted to retrieve certificates stored as secrets from the key vault."
      }
    },
    "enabledForDiskEncryption": {
      "type": "bool",
      "defaultValue": false,
      "metadata": {
        "description": "Specifies whether Azure Disk Encryption is permitted to retrieve secrets from the vault and unwrap keys."
      }
    },
    "enabledForTemplateDeployment": {
      "type": "bool",
      "defaultValue": false,
      "metadata": {
        "description": "Specifies whether Azure Resource Manager is permitted to retrieve secrets from the key vault."
      }
    },
    "tenantId": {
      "type": "string",
      "defaultValue": "[subscription().tenantId]",
      "metadata": {
        "description": "Specifies the Azure Active Directory tenant ID that should be used for authenticating requests to the key vault. Get it by using Get-AzSubscription cmdlet."
      }
    },
    "skuName": {
      "type": "string",
      "defaultValue": "standard",
      "allowedValues": [
        "standard",
        "premium"
      ],
      "metadata": {
        "description": "Specifies whether the key vault is a standard vault or a premium vault."
      }
    },
    "secretsObject": {
      "type": "secureObject",
      "metadata": {
        "description": "Specifies all secrets {\"secretName\":\"\",\"secretValue\":\"\"} wrapped in a secure object."
      }
    }
  },
  "resources": [
    {
      "type": "Microsoft.KeyVault/vaults",
      "apiVersion": "2023-07-01",
      "name": "[parameters('keyVaultName')]",
      "location": "[parameters('location')]",
      "properties": {
        "enabledForDeployment": "[parameters('enabledForDeployment')]",
        "enabledForTemplateDeployment": "[parameters('enabledForTemplateDeployment')]",
        "enabledForDiskEncryption": "[parameters('enabledForDiskEncryption')]",
        "enableRbacAuthorization": true,
        "tenantId": "[parameters('tenantId')]",
        "enableSoftDelete": true,
        "softDeleteRetentionInDays": 90,
        "enablePurgeProtection": true,
        "sku": {
          "name": "[parameters('skuName')]",
          "family": "A"
        },
        "networkAcls": {
          "defaultAction": "Allow",
          "bypass": "AzureServices"
        }
      }
    },
    {
      "copy": {
        "name": "secrets",
        "count": "[length(parameters('secretsObject').secrets)]"
      },
      "type": "Microsoft.KeyVault/vaults/secrets",
      "apiVersion": "2023-07-01",
      "name": "[format('{0}/{1}', parameters('keyVaultName'), parameters('secretsObject').secrets[copyIndex()].secretName)]",
      "properties": {
        "value": "[parameters('secretsObject').secrets[copyIndex()].secretValue]"
      },
      "dependsOn": [
        "[resourceId('Microsoft.KeyVault/vaults', parameters('keyVaultName'))]"
      ]
    }
  ],
  "outputs": {
    "location": {
      "type": "string",
      "value": "[parameters('location')]"
    },
    "name": {
      "type": "string",
      "value": "[parameters('keyVaultName')]"
    },
    "resourceGroupName": {
      "type": "string",
      "value": "[resourceGroup().name]"
    },
    "resourceId": {
      "type": "string",
      "value": "[resourceId('Microsoft.KeyVault/vaults', parameters('keyVaultName'))]"
    }
  }
}

模板中定义了两个Azure资源:

由于保管库使用 Azure RBAC 进行数据平面授权,因此可以通过分配Azure角色(而不是配置访问策略)来授予对机密的访问权限。

可以在 Azure 快速入门模板中找到更多Azure 密钥保管库模板示例。

部署模板

  1. 选择下图以登录到Azure并打开模板。 该模板将创建 Key Vault 和机密。

    用于将 资源管理器 模板部署到 Azure 的按钮。

  2. 选择或输入以下值。 除非指定了默认值,否则请使用默认值。

    • Subscription:选择Azure订阅。

    • 资源组:选择“新建”,为资源组输入一个独一无二的名称,然后选择“确定”。

    • 区域:选择一个位置。 例如“美国中部”。

    • 密钥保管库 Name:输入key vault的名称,该名称在 vault.azure.net 命名空间中必须全局唯一。 在下一部分验证部署时,您需要用到此名称。

    • Sku 名称:选择 标准高级。 默认值为 标准

    • Secrets 对象:以包含 secrets 数组的 JSON 对象形式提供要创建的一个或多个密钥。 例如:

      {
        "secrets": [
          {
            "secretName": "adminpassword",
            "secretValue": "<your-secret-value>"
          }
        ]
      }
      

      由于 Secrets 对象 是参数 secureObject ,因此部署后不会记录或回显其值。

  3. 选择查看 + 创建,然后选择创建。 成功部署密钥保管库和机密后,会收到通知。

还可以使用Azure PowerShell、Azure CLI或 REST API 部署模板。 若要了解其他部署方法,请参阅部署模板

分配 密钥保管库 RBAC 角色

此模板创建的密钥保管库使用 Azure RBAC 进行授权。 若要通过数据平面访问机密(例如,使用Azure CLI或Azure PowerShell),需要为自己分配适当的角色。

  1. 获取 Microsoft Entra 用户对象 ID:

    az ad signed-in-user show --query id -o tsv
    
  2. 在密钥保管库上为自己分配 密钥保管库 机密负责人 角色:

    echo "Enter your key vault name:" &&
    read keyVaultName &&
    az role assignment create --role "Key Vault Secrets Officer" \
        --assignee-object-id $(az ad signed-in-user show --query id -o tsv) \
        --scope $(az keyvault show --name $keyVaultName --query id -o tsv)
    

    注释

    角色分配可能需要一两分钟才能生效。

查看已部署的资源

可以使用Azure门户检查密钥保管库和机密,也可以使用以下Azure CLI或Azure PowerShell脚本列出创建的机密。

echo "Enter your key vault name:" &&
read keyVaultName &&
az keyvault secret list --vault-name $keyVaultName &&
echo "Press [ENTER] to continue ..."

清理资源

其他 密钥保管库 快速入门和教程都是基于本快速入门。 如果打算继续使用后续的快速入门和教程,则可能需要保留这些资源。 不再需要时,请删除资源组,该资源组会删除密钥保管库和相关资源。 若要使用Azure CLI或Azure PowerShell删除资源组,请执行以下操作:

echo "Enter the Resource Group name:" &&
read resourceGroupName &&
az group delete --name $resourceGroupName &&
echo "Press [ENTER] to continue ..."

注释

删除资源组也会删除密钥保管库,但保管库随后进入软删除状态,并在保留期(默认情况下为 90 天)保持可恢复状态。 保管库名称在该时间段内保持全局保留状态,并且由于启用了清除保护,因此无法提前清除保管库。 对于标准密钥保管库,已软删除的保管库不会产生费用。 有关详细信息,请参阅 密钥保管库 软删除概述

后续步骤

在本快速入门中,你使用 ARM 模板创建了密钥保管库和机密,并验证了部署。 若要详细了解密钥保管库和Azure 资源管理器,请继续阅读以下文章。