PowerShell 和 Azure CLI:使用 Azure Key Vault 中的客戶自控金鑰來啟用透明資料加密

適用於:Azure SQL DatabaseAzure SQL 受控執行個體

本文說明如何使用 Azure Key Vault 的金鑰在 Azure SQL Database 上進行透明資料加密(TDE)。 若要深入了解 Azure Key Vault 整合的 TDE—使用自帶金鑰 (BYOK) 的支援,請參閱 Azure Key Vault 中客戶管理金鑰的 TDE。 如果你想要 Azure 入口網站關於如何從 Azure Key Vault 啟用客戶管理金鑰的 TDE 說明,請參考「建立已設定使用者指派受控識別與客戶管理 TDE 的伺服器」。

注意

對於 Azure SQL Database,請使用儲存在 Azure Key Vault Premium 或 Azure Key Vault Managed HSM 中的 AES-256 對稱金鑰,作為新的 TDE 保護裝置。 Azure SQL 受控執行個體 需要非對稱的 RSA 金鑰。 有關支援的金鑰類型與遷移指引,請參閱「使用客戶管理金鑰進行 透明資料加密」。

PowerShell 的必要條件

  • 您必須具有 Azure 訂用帳戶,並且是該訂用帳戶的系統管理員。
  • [建議執行的選擇性作業] 備妥硬體安全性模組 (HSM) 或本機金鑰存放區,用來建立 TDE 保護裝置金鑰內容的本機複本。
  • 您必須安裝並執行 Azure PowerShell。
  • 建立要用於 TDE 的 Azure Key Vault 和金鑰。
  • 金鑰必須具有下列屬性才能用於 TDE:
    • 啟用日期 (若已設定) 必須是過去的日期和時間
    • 期間屆滿日 (若已設定) 必須是未來的日期和時間
    • 金鑰必須處於「已啟用」狀態
    • 能夠執行取得、封裝金鑰、解封裝金鑰作業
  • 要使用 Azure Key Vault 受管 HSM 金鑰,請依照指示使用 Azure CLI 建立並啟用受管型 HSM

如需 Az PowerShell 模組安裝指示,請參閱安裝 Azure PowerShell。

如需 Azure Key Vault 的詳細資訊,請參閱 Azure Key Vault 的 PowerShell 指示 和 如何使用 PowerShell 操作 Azure Key Vault 的軟刪除。

將 Microsoft Entra 身分識別指派給您的伺服器

如果你已有伺服器,請依照以下步驟為你的伺服器新增 Microsoft Entra 身份:

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

如果你正在建立伺服器,請使用 New-AzSqlServer Cmdlet 搭配 -Identity 標記,在建立伺服器時新增 Microsoft Entra 身分識別:

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

將 Azure Key Vault 許可權授與伺服器

請先使用 Set-AzKeyVaultAccessPolicy cmdlet 將存取權授予您的伺服器,以便在 TDE 中使用金鑰保存庫中的金鑰。

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

若要在受控 HSM 上為您的伺服器新增權限,請將 Managed HSM Crypto Service Encryption User 本機 RBAC 角色指派給伺服器。 此角色使伺服器能對受管理 HSM 中的金鑰執行 get、 wrap key及 unwrap key 操作。 欲了解更多資訊,請參閱 受管理型 HSM 角色管理。

將 Azure Key Vault 金鑰新增至伺服器並設定 TDE 保護裝置

注意

針對受控 HSM 金鑰,請使用 Az.Sql 2.11.1 版的 PowerShell 或更高版本。

注意

金鑰庫名稱與金鑰名稱的總長度不得超過 94 個字元。

提示

在 TDE 中使用含版本和無版本的 Azure Key Vault 金鑰

設定 TDE 保護器時,您可以使用特定金鑰版本或無版本金鑰識別碼來參考 Azure Key Vault 金鑰。

在這兩種情況下,Azure SQL Database 都會解析並使用最新啟用的 Azure Key Vault 或 Azure Key Vault Managed HSM 中的金鑰版本。 使用無版本金鑰識別碼以避免在 TDE 保護器配置中嵌入特定金鑰版本。

目前僅支援 Azure SQL 資料庫的無版本金鑰識別碼。

範例:

  • 包含特定版本的金鑰識別碼

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

  • 無版本金鑰識別碼

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

# 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"

現在資料庫啟用了 TDE,並使用 Azure Key Vault 中的加密金鑰。

檢查加密狀態和加密活動

使用 Get-AzSqlDatabaseTransparentDataEncryption 來取得資料庫的加密狀態。

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

有用的 PowerShell Cmdlet

  • 使用 Set-AzSqlDatabaseTransparentDataEncryption Cmdlet 以關閉 TDE。

    Set-AzSqlDatabaseTransparentDataEncryption -ServerName <LogicalServerName> -ResourceGroupName <SQLDatabaseResourceGroupName> `
        -DatabaseName <DatabaseName> -State "Disabled"
    
  • 使用 Get-AzSqlServerKeyVaultKey Cmdlet 傳回新增至伺服器的 Azure Key Vault 密鑰清單。

    # KeyId is an optional parameter, to return a specific key version
    Get-AzSqlServerKeyVaultKey -ServerName <LogicalServerName> -ResourceGroupName <SQLDatabaseResourceGroupName>
    
  • 使用 Remove-AzSqlServerKeyVaultKey 從伺服器移除 Azure Key Vault 密鑰。

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

疑難排解

  • 如果找不到金鑰庫,請確保你使用的是正確的訂閱。

    Get-AzSubscription -SubscriptionId <SubscriptionId>
    

  • 如果你無法將新金鑰加入伺服器,或無法將新金鑰更新為 TDE 保護器,請檢查以下條件:

    • 金鑰不應該有有效期限。
    • 金鑰必須已啟用取得、封裝金鑰和解封金鑰作業。

TDE BYOK Azure Synapse Analytics

關於 Azure Synapse Analytics 的 TDE BYOK 文件,請參閱 Azure Synapse Analytics SQL 文件。 如需 Synapse 工作區中專用 SQL 集區的透明資料加密文件,請參閱 Azure Synapse Analytics 加密。