快速入门:使用 ARM 模板创建Azure密钥保管库

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

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

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

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

Prerequisites

若要完成本文,需要做好以下准备:

  • 如果没有 Azure 订阅,请在开始之前先创建一个免费帐户

查看模板

本快速入门中使用的模板来自 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": "5424899472990749957"
    }
  },
  "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."
      }
    },
    "secretName": {
      "type": "string",
      "metadata": {
        "description": "Specifies the name of the secret that you want to create."
      }
    },
    "secretValue": {
      "type": "securestring",
      "metadata": {
        "description": "Specifies the value of the secret that you want to create."
      }
    }
  },
  "resources": [
    {
      "type": "Microsoft.KeyVault/vaults",
      "apiVersion": "2023-07-01",
      "name": "[parameters('keyVaultName')]",
      "location": "[parameters('location')]",
      "properties": {
        "enabledForDeployment": "[parameters('enabledForDeployment')]",
        "enabledForDiskEncryption": "[parameters('enabledForDiskEncryption')]",
        "enabledForTemplateDeployment": "[parameters('enabledForTemplateDeployment')]",
        "enableRbacAuthorization": true,
        "tenantId": "[parameters('tenantId')]",
        "enableSoftDelete": true,
        "softDeleteRetentionInDays": 90,
        "enablePurgeProtection": true,
        "sku": {
          "name": "[parameters('skuName')]",
          "family": "A"
        },
        "networkAcls": {
          "defaultAction": "Allow",
          "bypass": "AzureServices"
        }
      }
    },
    {
      "type": "Microsoft.KeyVault/vaults/secrets",
      "apiVersion": "2023-07-01",
      "name": "[format('{0}/{1}', parameters('keyVaultName'), parameters('secretName'))]",
      "properties": {
        "value": "[parameters('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 资源:

  • Microsoft.KeyVault/vaults:创建 Azure 密钥保管库。 该模板启用 Azure RBAC 授权(enableRbacAuthorization: true)、软删除和清除保护。

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

部署模板

  1. 选择下图以登录到Azure并打开模板。 该模板创建密钥保管库。

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

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

    • Subscription:选择Azure订阅。
    • 资源组:选择“新建”,为资源组输入一个独一无二的名称,然后选择“确定”。
    • 区域:选择一个位置。 例如,中国北部
    • 保管库名称:输入密钥保管库的名称,该名称在命名空间中 vault.azure.cn 必须全局唯一。
    • Sku 名称:选择 标准高级。 默认值为 标准
  3. 选择查看 + 创建,然后选择创建。 成功部署密钥保管库后,会收到通知。

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

分配 密钥保管库 RBAC 角色

此模板创建的密钥保管库使用 Azure RBAC 进行授权。 若要通过数据平面创建或读取密钥、机密或证书,需要为自己分配适当的角色。 例如,若要管理机密,请自行分配 密钥保管库 Secrets Officer 角色:

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门户检查密钥保管库,也可以使用以下Azure CLI或Azure PowerShell脚本:

echo "Enter your key vault name:" &&
read keyVaultName &&
az keyvault show --name $keyVaultName

清理资源

其他 密钥保管库 快速入门与教程,都是以此快速入门为基础开发的。 如果打算继续使用后续的快速入门和教程,则可能需要保留这些资源。 如果不再需要资源组,则删除用于删除密钥保管库和相关资源的资源组。

echo "Enter the Resource Group name:" &&
read resourceGroupName &&
az group delete --name $resourceGroupName

注释

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

更多 密钥保管库 资源管理器模板

其他快速入门将逐步讲解如何向密钥保管库中添加机密、密钥或证书:

Secrets Keys 证书

可在此处找到更多 密钥保管库 模板:密钥保管库 资源管理器 参考文档

后续步骤