通过


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

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

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

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

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

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

先决条件

若要完成本文,

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

  • 模板需要 Microsoft Entra 用户对象 ID 来配置权限。 以下过程获取对象 ID (GUID)。

    1. 通过选择Try it,运行以下 Azure PowerShell 或 Azure CLI 命令,然后将脚本粘贴到 shell 窗格中。 若要粘贴脚本,请右键单击 shell,然后选择“粘贴”。

      echo "Enter your email address that is used to sign in to Azure:" &&
      read upn &&
      az ad user show --id $upn --query "Id" &&
      echo "Press [ENTER] to continue ..."
      
    2. 记下对象 ID。 在本快速入门的下一部分中需要用到它。

查看模板

重要

本快速入门使用外部模板创建具有传统访问策略的保管库。 对于生产部署,请改用 Azure RBAC 授权。 请参阅《使用 ARM 模板创建密钥库》以获取使用该模板的示例,或参阅enableRbacAuthorization: true《保护您的 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.26.54.24096",
      "templateHash": "8629186205194254058"
    }
  },
  "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."
      }
    },
    "objectId": {
      "type": "string",
      "metadata": {
        "description": "Specifies the object ID of a user, service principal or security group in the Azure Active Directory 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."
      }
    },
    "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')]",
        "tenantId": "[parameters('tenantId')]",
        "enableSoftDelete": true,
        "softDeleteRetentionInDays": 90,
        "accessPolicies": [
          {
            "objectId": "[parameters('objectId')]",
            "tenantId": "[parameters('tenantId')]",
            "permissions": {
              "keys": "[parameters('keysPermissions')]",
              "secrets": "[parameters('secretsPermissions')]"
            }
          }
        ],
        "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资源:

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

部署模板

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

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

  2. 选择或输入以下值。

    ARM 模板、密钥保管库 集成、门户部署

    除非指定了密钥保管库,否则请使用默认值创建密钥保管库和机密。

    • Subscription:选择Azure订阅。
    • 资源组:选择“新建”,输入资源组的唯一名称,然后单击“确定”。
    • 位置:选择一个位置。 例如“美国中部”。
    • 密钥保管库 Name:输入key vault的名称,该名称在 .vault.azure.net 命名空间中必须全局唯一。 在下一部分验证部署时,需要该名称。
    • 租户 ID:模板函数会自动检索租户 ID。 不要更改默认值。
    • 广告用户 ID:输入在先决条件中获取的 Microsoft Entra 用户对象 ID。
    • 机密名称:输入密钥保管库中存储的机密的名称。 例如 ,adminpassword
    • 机密值:输入机密值。 如果存储密码,建议使用在先决条件中创建的生成的密码。
    • 我同意上述条款和条件:选中。
  3. 选择“购买”。 成功部署密钥保管库后,会收到通知:

    ARM 模板,密钥保管库集成,部署门户通知

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

查看已部署的资源

可以使用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 ..."

后续步骤

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