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

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

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

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

部署到 Azure

先决条件

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

  • 如果没有 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": "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 并打开一个模板。 该模板将创建一个密钥保管库和一个机密。

    部署到 Azure

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

    • 订阅:选择 Azure 订阅。

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

    • 区域:选择一个位置。 例如,中国北部

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

    • 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 门户检查 Key Vault 和机密,或者使用以下 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 资源管理器,请继续阅读以下文章。