PowerShell 和 Azure CLI:使用 Azure 密钥保管库 中客户管理的密钥实现透明数据加密

适用于:Azure Synapse Analytics 专用 SQL 池(以前称为 SQL DW)

本文将详细介绍如何在 Azure Synapse Analytics 上使用 Azure 密钥保管库 中的密钥进行透明数据加密(TDE)。 要了解更多关于 TDE 与 Azure 密钥保管库 集成(即自带密钥 (BYOK) 支持)的信息,请访问使用 Azure 密钥保管库 中由客户管理的密钥进行 TDE。

Note

本文介绍了独立的专用SQL池(前称SQL DW)。

  • 对于 Azure Synapse Analytics 专用的 SQL 池(前称 SQL DW),在服务器层设置 TDE 保护器。 所有与该服务器关联的加密数据库都会继承TDE保护器。
  • 在 Synapse 工作区中,使用在工作区级别配置的客户管理密钥对专用 SQL 池和无服务器 SQL 池中的数据进行加密。 有关 Synapse 工作区中专用 SQL 池的透明数据加密的详细信息,请参阅 Azure Synapse Analytics 加密。

PowerShell 的先决条件

  • 必须有一个 Azure 订阅,并且是该订阅的管理员。
  • 必须安装并运行 Azure PowerShell。
  • 创建用于 TDE 的 Azure 密钥保管库 和密钥。
  • 密钥必须包含用于 TDE 的以下特性:
    • 激活日期(如果已设置)必须是过去的日期和时间
    • 到期日期(若已设置)必须是将来的日期和时间
    • 密钥必须处于“已启用”状态
    • 能够执行“获取”、“包装密钥”和“解包密钥”操作

有关 Az PowerShell 模块安装说明,请参阅安装 Azure PowerShell。

有关 Azure 密钥保管库 的详细信息,请参阅 Azure 密钥保管库 中的 PowerShell 说明 以及如何 通过 PowerShell 使用 Azure 密钥保管库 软删除。

为服务器分配 Microsoft Entra 标识

如果您已有服务器,请按照以下步骤为服务器添加 Microsoft Entra 身份:

$server = Set-AzSqlServer -ResourceGroupName <SQLDatabaseResourceGroupName> -ServerName <LogicalServerName> -AssignIdentity

如果你正在创建服务器,请使用 -Identity cmdlet 和 标签在创建服务器时添加 Microsoft Entra 标识:

$server = New-AzSqlServer -ResourceGroupName <SQLDatabaseResourceGroupName> -Location <RegionName> `
    -ServerName <LogicalServerName> -ServerVersion "12.0" -SqlAdministratorCredentials <PSCredential> -AssignIdentity

向服务器授予 Azure 密钥保管库 权限

将 Key Vault 中的密钥用于 TDE 之前,请使用 Set-AzKeyVaultAccessPolicy cmdlet 向服务器授权 Key Vault 的访问权限。

Set-AzKeyVaultAccessPolicy -VaultName <KeyVaultName> `
    -ObjectId $server.Identity.PrincipalId -PermissionsToKeys get, wrapKey, unwrapKey

将 Azure 密钥保管库 密钥添加到服务器并设置 TDE 保护程序

Note

密钥库名称和密钥名称的总长度不得超过94字符。

Tip

将 Azure 密钥保管库 中有版本的密钥用于 TDE

设置 TDE 保护器时,通过特定的密钥版本引用 Azure 密钥保管库 密钥。

示例:

  • 包含特定版本的密钥标识符

    https://<key-vault-name>.vault.azure.cn/keys/<key-name>/<key-version>

# add the key from Azure Key Vault to the server
Add-AzSqlServerKeyVaultKey -ResourceGroupName <SQLDatabaseResourceGroupName> -ServerName <LogicalServerName> -KeyId <KeyVaultKeyId>

# set the key as the TDE protector for all resources under the server
Set-AzSqlServerTransparentDataEncryptionProtector -ResourceGroupName <SQLDatabaseResourceGroupName> -ServerName <LogicalServerName> `
   -Type AzureKeyVault -KeyId <KeyVaultKeyId>

# confirm the TDE protector was configured as intended
Get-AzSqlServerTransparentDataEncryptionProtector -ResourceGroupName <SQLDatabaseResourceGroupName> -ServerName <LogicalServerName>

启用 TDE

使用 Set-AzSqlDatabaseTransparentDataEncryption cmdlet 来启用 TDE。

Set-AzSqlDatabaseTransparentDataEncryption -ResourceGroupName <SQLDatabaseResourceGroupName> `
   -ServerName <LogicalServerName> -DatabaseName <DatabaseName> -State "Enabled"

现在,数据库或数据仓库已在 Azure 密钥保管库 中使用加密密钥启用 TDE。

检查加密状态和加密活动

使用 Get-AzSqlDatabaseTransparentDataEncryption 获取数据库或数据仓库的加密状态。

# get the encryption state of the database
Get-AzSqlDatabaseTransparentDataEncryption -ResourceGroupName <SQLDatabaseResourceGroupName> `
   -ServerName <LogicalServerName> -DatabaseName <DatabaseName> `

有用的 PowerShell 命令

  • 使用 Set-AzSqlDatabaseTransparentDataEncryption cmdlet 来禁用 TDE。

    Set-AzSqlDatabaseTransparentDataEncryption -ServerName <LogicalServerName> -ResourceGroupName <SQLDatabaseResourceGroupName> `
        -DatabaseName <DatabaseName> -State "Disabled"
    
  • 使用 Get-AzSqlServerKeyVaultKey cmdlet 返回添加到服务器的 Azure 密钥保管库 密钥列表。

    # KeyId is an optional parameter, to return a specific key version
    Get-AzSqlServerKeyVaultKey -ServerName <LogicalServerName> -ResourceGroupName <SQLDatabaseResourceGroupName>
    
  • 使用 Remove-AzSqlServerKeyVaultKey 从服务器中删除 Azure 密钥保管库 密钥。

    # the key set as the TDE Protector cannot be removed
    Remove-AzSqlServerKeyVaultKey -KeyId <KeyVaultKeyId> -ServerName <LogicalServerName> -ResourceGroupName <SQLDatabaseResourceGroupName>
    

Troubleshooting

  • 如果找不到密钥保管库,请确保你选择了正确的订阅。

    Get-AzSubscription -SubscriptionId <SubscriptionId>
    

  • 如果你无法将新密钥添加到服务器,或者无法将新密钥更新为TDE保护器,请检查以下条件:

    • 密钥不应有有效期。
    • 密钥必须支持“获取”、“包装密钥”和“解包密钥”操作。