PowerShell-script om transparante gegevensversleuteling in te schakelen met uw eigen sleutel

Van toepassing op: Azure SQL Managed Instance

In dit PowerShell-voorbeeldscript wordt TDE (Transparent Data Encryption) in Azure SQL Managed Instance geconfigureerd met behulp van een door de klant beheerde sleutel uit Azure Key Vault. Dit wordt vaak een BYOK-scenario (Bring Your Own Key) genoemd voor TDE. Zie Azure SQL Transparent Data Encryption met door de klant beheerde sleutel voor meer informatie.

Vereisten

Als u geen Azure-abonnement hebt, kunt u een gratis Azure-account maken voordat u begint.

Notitie

In dit artikel wordt gebruikgemaakt van de Azure Az PowerShell-module. Dit is de aanbevolen PowerShell-module voor interactie met Azure. Raadpleeg Azure PowerShell installeren om aan de slag te gaan met de Az PowerShell-module. Raadpleeg Azure PowerShell migreren van AzureRM naar Az om te leren hoe u naar de Azure PowerShell-module migreert.

Azure Cloud Shell gebruiken

Azure host Azure Cloud Shell, een interactieve shell-omgeving die u via uw browser kunt gebruiken. U kunt Bash of PowerShell gebruiken met Cloud Shell om met Azure-services te werken. U kunt de vooraf geïnstalleerde Cloud Shell-opdrachten gebruiken om de code in dit artikel uit te voeren zonder dat u iets hoeft te installeren in uw lokale omgeving.

Om Azure Cloud Shell op te starten:

Optie Voorbeeld/koppeling
Selecteer Nu proberen in de rechterbovenhoek van een codeblok. Als u Uitproberen selecteert, wordt de code niet automatisch gekopieerd naar Cloud Shell. Screenshot that shows an example of Try It for Azure Cloud Shell.
Ga naar https://shell.azure.com, of selecteer de knop Cloud Shell starten om Cloud Shell in uw browser te openen. Screenshot that shows how to launch Cloud Shell in a new window.
Klik op de knop Cloud Shell in het menu in de balk rechtsboven in de Azure-portal. Screenshot that shows the Cloud Shell button in the Azure portal

Om de code in dit artikel in Azure Cloud Shell uit te voeren:

  1. Start Cloud Shell.

  2. Selecteer de knop Kopiëren op een codeblok om de code te kopiëren.

  3. Plak de code in de Cloud Shell-sessie door Ctrl+Shift+V te selecteren in Windows en Linux of door Cmd+Shift+V in macOS te selecteren.

  4. Selecteer Invoeren om de code uit te voeren.

Voor het lokaal gebruiken van PowerShell of het gebruik van Azure Cloud Shell is Azure PowerShell 2.3.2 of een latere versie vereist. Als u een upgrade wilt uitvoeren, raadpleegt u De Azure PowerShell-module installeren of voert u het onderstaande voorbeeldscript uit om de module voor de huidige gebruiker te installeren:

Install-Module -Name Az -AllowClobber -Scope CurrentUser

Als u PowerShell lokaal uitvoert, moet u ook Connect-AzAccount uitvoeren om verbinding te kunnen maken met Azure.

Voorbeeldscripts

# You will need an existing Managed Instance as a prerequisite for completing this script.
# See https://docs.microsoft.com/en-us/azure/sql-database/scripts/sql-database-create-configure-managed-instance-powershell

# Log in to your Azure account:
Connect-AzAccount

# If there are multiple subscriptions, choose the one where AKV is created: 
Set-AzContext -SubscriptionId "subscription ID"

# Install the Az.Sql PowerShell package if you are running this PowerShell locally (uncomment below):
# Install-Module -Name Az.Sql

# 1. Create Resource and setup Azure Key Vault (skip if already done)

# Create Resource group (name the resource and specify the location)
$location = "westus2" # specify the location
$resourcegroup = "MyRG" # specify a new RG name
New-AzResourceGroup -Name $resourcegroup -Location $location

# Create new Azure Key Vault with a globally unique VaultName and soft-delete option turned on:
$vaultname = "MyKeyVault" # specify a globally unique VaultName
New-AzKeyVault -VaultName $vaultname -ResourceGroupName $resourcegroup -Location $location

# Authorize Managed Instance to use the AKV (wrap/unwrap key and get public part of key, if public part exists): 
$objectid = (Set-AzSqlInstance -ResourceGroupName $resourcegroup -Name "MyManagedInstance" -AssignIdentity).Identity.PrincipalId
Set-AzKeyVaultAccessPolicy -BypassObjectIdValidation -VaultName $vaultname -ObjectId $objectid -PermissionsToKeys get,wrapKey,unwrapKey

# Allow access from trusted Azure services: 
Update-AzKeyVaultNetworkRuleSet -VaultName $vaultname -Bypass AzureServices

# Allow access from your client IP address(es) to be able to complete remaining steps: 
Update-AzKeyVaultNetworkRuleSet -VaultName $vaultname -IpAddressRange "xxx.xxx.xxx.xxx/xx"

# Turn the network rules ON by setting the default action to Deny: 
Update-AzKeyVaultNetworkRuleSet -VaultName $vaultname -DefaultAction Deny


# 2. Provide TDE Protector key (skip if already done)

# First, give yourself necessary permissions on the AKV, (specify your account instead of contoso.com):
Set-AzKeyVaultAccessPolicy -VaultName $vaultname -UserPrincipalName "myaccount@contoso.com" -PermissionsToKeys create,import,get,list

# The recommended way is to import an existing key from a .pfx file. Replace "<PFX private key password>" with the actual password below:
$keypath = "c:\some_path\mytdekey.pfx" # Supply your .pfx path and name
$securepfxpwd = ConvertTo-SecureString -String "<PFX private key password>" -AsPlainText -Force 
$key = Add-AzKeyVaultKey -VaultName $vaultname -Name "MyTDEKey" -KeyFilePath $keypath -KeyFilePassword $securepfxpwd

# ...or get an existing key from the vault:
# $key = Get-AzKeyVaultKey -VaultName $vaultname -Name "MyTDEKey"

# Alternatively, generate a new key directly in Azure Key Vault (recommended for test purposes only - uncomment below):
# $key = Add-AzureKeyVaultKey -VaultName $vaultname -Name MyTDEKey -Destination Software -Size 2048

# 3. Set up BYOK TDE on Managed Instance:

# Assign the key to the Managed Instance:
# $key = 'https://contoso.vault.azure.net/keys/contosokey/01234567890123456789012345678901'
Add-AzSqlInstanceKeyVaultKey -KeyId $key.id -InstanceName "MyManagedInstance" -ResourceGroupName $resourcegroup

# Set TDE operation mode to BYOK: 
Set-AzSqlInstanceTransparentDataEncryptionProtector -Type AzureKeyVault -InstanceName "MyManagedInstance" -ResourceGroup $resourcegroup -KeyId $key.id

Volgende stappen

Zie Documentatie over Azure PowerShell voor meer informatie over Azure PowerShell.

Extra PowerShell-voorbeeldscripts voor SQL Managed Instance vindt u in PowerShell-scripts voor Azure SQL Managed Instance.