Compartilhar via

将Azure Key Vault移动到另一个订阅

注意事项

建议使用 Azure Az PowerShell 模块与Azure交互。 若要开始,请参阅“Install Azure PowerShell”。 若要了解如何迁移到 Az PowerShell 模块,请参阅 Migrate Azure PowerShell从 AzureRM 迁移到 Az

概述

重要

将密钥保管库移动到另一个订阅将会对环境造成重大变更。 在决定将key vault移动到新订阅之前,请确保了解此更改的影响,并仔细遵循本文中的指南。 如果使用托管服务标识(MSI),请阅读文档末尾的移动后说明。

Azure Key Vault 会自动关联到其创建时所属订阅的默认 Microsoft Entra ID 租户 ID。 您可以按照此指南查找与您的订阅关联的租户 ID。 所有访问策略条目和角色分配也都绑定到此租户 ID。 如果将Azure订阅从租户 A 移到租户 B,则租户 B 中的服务主体(用户和应用程序)无法访问现有密钥保管库。若要解决此问题,需要:

注意事项

如果Key Vault是通过 Azure Lighthouse 创建的,则会将其绑定到管理租户 ID。 Azure Lighthouse 仅支持保管库访问策略权限模型。 有关 Azure Lighthouse 租户的详细信息,请参阅Azure Lighthouse 中的租户、用户和角色

  • 将与订阅中所有现有密钥保管库关联的租户 ID 更改到租户 B。
  • 删除所有现有的访问策略条目。
  • 关联租户 B 的新访问策略条目。

有关Azure Key Vault和Microsoft Entra ID的详细信息,请参阅:

限制

重要

无法移动用于磁盘加密的 Key Vaults 如果您使用 Key Vault 对虚拟机(VM)进行磁盘加密,那么在启用磁盘加密时,Key Vault 不能移动到其他资源组或订阅。 在将key vault移动到新的资源组或订阅之前,必须禁用磁盘加密。

某些服务主体(用户和应用程序)绑定到特定的租户。 如果将密钥保管库移到另一个租户中的订阅,则可能无法恢复对特定服务主体的访问权限。 检查并确保所有必要的服务主体均已存在于您要迁移密钥保管库到的租户中。

先决条件

  • Contributor 级别或更高级别的访问权限,在密钥保管库所在的当前订阅中。 可以使用 Azure portalAzure CLIPowerShell2 分配角色。
  • Contributor级别或更高的访问权限,适用于您希望迁移密钥保管库的订阅。 可以使用 Azure portalAzure CLIPowerShell2 分配角色。
  • 新订阅中的一个资源组。 可以使用 Azure portalPowerShellAzure CLI 创建一个。

可以使用 Azure portalPowerShellAzure CLIREST API 来检查现有角色。

将密钥保管库移动到新订阅

  1. 登录到 Azure portal
  2. 导航到 密钥保管库
  3. 选择“概述”选项卡
  4. 选择“移动”按钮
  5. 从下拉选项中选择“移动到另一个订阅”
  6. 选择您要将密钥保管库移动至的资源组。
  7. 注意关于移动资源的警告
  8. 选择“确定”

订阅在新租户中时的其他步骤

如果将包含密钥保管库的订阅移动到新租户,则需要手动更新租户 ID,并删除旧的访问策略和角色分配。 下面是有关 PowerShell 和 Azure CLI 这些步骤的教程。 如果使用的是 PowerShell,可能需要运行 Clear-AzContext 命令,这样就能查看当前所选范围外的资源。

更新密钥保管库中的租户 ID

Select-AzSubscription -SubscriptionId <your-subscriptionId>                # Select your Azure Subscription
$vaultResourceId = (Get-AzKeyVault -VaultName myvault).ResourceId          # Get your key vault's Resource ID 
$vault = Get-AzResource -ResourceId $vaultResourceId -ExpandProperties     # Get the properties for your key vault
$vault.Properties.TenantId = (Get-AzContext).Tenant.TenantId               # Change the Tenant that your key vault resides in
$vault.Properties.AccessPolicies = @()                                     # Access policies can be updated with real
                                                                           # applications/users/rights so that it does not need to be                             # done after this whole activity. Here we are not setting 
                                                                           # any access policies. 
Set-AzResource -ResourceId $vaultResourceId -Properties $vault.Properties  # Modifies the key vault's properties.

Clear-AzContext                                                            #Clear the context from PowerShell
Connect-AzAccount -Environment AzureChinaCloud                                                          #Log in again to confirm you have the correct tenant id
az account set -s <your-subscriptionId>                                    # Select your Azure Subscription
tenantId=$(az account show --query tenantId)                               # Get your tenantId
az keyvault update -n myvault --remove Properties.accessPolicies           # Remove the access policies
az keyvault update -n myvault --set Properties.tenantId=$tenantId          # Update the key vault tenantId

更新访问策略和角色分配

注意事项

如果Key Vault使用的是 Azure RBAC 权限模型,则还需要删除key vault角色分配。 可以使用 Azure portalAzure CLIPowerShell 删除角色分配。

将保管库和正确的租户 ID 及旧的访问策略项目或角色分配相关联后,请设置新的访问策略项目或角色分配。

有关分配策略,请参阅:

有关添加角色分配,请参阅:

更新托管标识

如果您正在传输整个订阅并使用用于 Azure 资源的托管身份,则还需要将其更新至新的 Microsoft Entra 租户。 要了解有关托管标识的详细信息,请参阅托管标识概述

如果使用的是托管标识,则还必须更新标识,因为旧标识将不再位于相应的 Microsoft Entra 租户中。 参阅下述有助于解决此问题的文档。

后续步骤