如何使用资源管理器模板创建 Azure 密钥保管库和保管库访问策略

Azure Key Vault 是一项云服务,它为密钥、密码和证书等机密提供了安全的存储。 本文介绍了部署 Azure 资源管理器模板(ARM 模板)以创建密钥保管库的过程。

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

先决条件

完成本文中的步骤:

  • 如果没有 Azure 订阅,请在开始前创建一个试用版订阅

创建 Key Vault 资源管理器模板

以下模板显示了创建密钥保管库的基本方式。 某些值在模板中指定。

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "keyVaultName": {
      "type": "string",
      "metadata": {
        "description": "Specifies the name of the key vault."
      }
    },
    "skuName": {
      "type": "string",
      "defaultValue": "Standard",
      "allowedValues": [
        "Standard",
        "Premium"
      ],
      "metadata": {
        "description": "Specifies whether the key vault is a standard vault or a premium vault."
      }
    }
   },
  "resources": [
    {
      "type": "Microsoft.KeyVault/vaults",
      "apiVersion": "2019-09-01",
      "name": "[parameters('keyVaultName')]",
      "location": "[resourceGroup().location]",
      "properties": {
        "enabledForDeployment": "false",
        "enabledForDiskEncryption": "false",
        "enabledForTemplateDeployment": "false",
        "tenantId": "[subscription().tenantId]",
        "accessPolicies": [],
        "sku": {
          "name": "[parameters('skuName')]",
          "family": "A"
        },
        "networkAcls": {
          "defaultAction": "Allow",
          "bypass": "AzureServices"
        }
      }
    }
  ]
}

有关 Key Vault 模板设置的详细信息,请参阅 Key Vault ARM 模板参考

重要

如果重新部署了模板,则将覆盖密钥保管库中的任何现有访问策略。 建议你使用现有的访问策略填充 accessPolicies 属性,以免失去对密钥保管库的访问权限。

将访问策略添加到 Key Vault 资源管理器模板

可以向现有密钥保管库部署访问策略,而无需重新部署整个密钥保管库模板。 以下模板显示了创建访问策略的基本方式:

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "keyVaultName": {
      "type": "string",
      "metadata": {
        "description": "Specifies the name of the key vault."
      }
    },
    "objectId": {
      "type": "string",
      "metadata": {
        "description": "Specifies the object ID of a user, service principal or security group in the Microsoft Entra ID tenant for the vault. The object ID must be unique for the list of access policies. Get it by using Get-AzADUser or Get-AzADServicePrincipal cmdlets."
      }
    },
    "keysPermissions": {
      "type": "array",
      "defaultValue": [
        "list"
      ],
      "metadata": {
        "description": "Specifies the permissions to keys in the vault. Valid values are: all, encrypt, decrypt, wrapKey, unwrapKey, sign, verify, get, list, create, update, import, delete, backup, restore, recover, and purge."
      }
    },
    "secretsPermissions": {
      "type": "array",
      "defaultValue": [
        "list"
      ],
      "metadata": {
        "description": "Specifies the permissions to secrets in the vault. Valid values are: all, get, list, set, delete, backup, restore, recover, and purge."
      }
    },
    "certificatePermissions": {
      "type": "array",
      "defaultValue": [
        "list"
      ],
      "metadata": {
        "description": "Specifies the permissions to certificates in the vault. Valid values are: all,  create, delete, update, deleteissuers, get, getissuers, import, list, listissuers, managecontacts, manageissuers,  recover, backup, restore, setissuers, and purge."
      }
    }
  },
  "resources": [
     {
      "type": "Microsoft.KeyVault/vaults/accessPolicies",
      "name": "[concat(parameters('keyVaultName'), '/add')]",
      "apiVersion": "2019-09-01",
      "properties": {
        "accessPolicies": [
          {
            "tenantId": "[subscription().tenantId]",
            "objectId": "[parameters('objectId')]",
            "permissions": {
              "keys": "[parameters('keysPermissions')]",
              "secrets": "[parameters('secretsPermissions')]",
              "certificates": "[parameters('certificatePermissions')]"
            }
          }
        ]
      }
    }
  ]
}

有关 Key Vault 模板设置的详细信息,请参阅 Key Vault ARM 模板参考

更多 Key Vault 资源管理器模板

还有其他资源管理器模板可用于 Key Vault 对象:

机密 证书
空值 空值

可在以下文章中找到更多 Key Vault 模板:Key Vault 资源管理器参考

部署模板

可以使用 Azure 门户来部署上述模板,方法是按下面所述使用“在编辑器中生成自己的模板”选项:从自定义模板部署资源

还可以将上述模板保存到文件,并使用以下命令:New-AzResourceGroupDeploymentaz deployment group create

New-AzResourceGroupDeployment -ResourceGroupName ExampleGroup -TemplateFile key-vault-template.json
az deployment group create --resource-group ExampleGroup --template-file key-vault-template.json

清理资源

如果打算继续使用后续的快速入门和教程,则可以将这些资源保留在原处。 当不再需要资源时,请删除资源组。 如果删除该组,则也会删除密钥保管库和相关资源。 若要使用 Azure CLI 或 Azure PowerShell 删除资源组,请完成以下步骤:

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

资源

后续步骤