Skrypt programu PowerShell umożliwiający włączanie transparentnego szyfrowania danych przy użyciu własnego klucza

Dotyczy:Azure SQL Managed Instance

Ten przykładowy skrypt programu PowerShell umożliwia skonfigurowanie funkcji Transparent Data Encryption (TDE) w usłudze Azure SQL Managed Instance przy użyciu klucza zarządzanego przez klienta z usługi Azure Key Vault. Jest to często nazywane scenariuszem byOK (bring-your-own-key) dla funkcji TDE. Aby dowiedzieć się więcej, zobacz Usługa Azure SQL Transparent Data Encryption z kluczem zarządzanym przez klienta.

Wymagania wstępne

  • Istniejące wystąpienie zarządzane. Zobacz Tworzenie wystąpienia zarządzanego przy użyciu programu PowerShell.

Jeśli nie masz subskrypcji platformy Azure, przed rozpoczęciem utwórz bezpłatne konto platformy Azure.

Uwaga

W tym artykule użyto modułu Azure Az programu PowerShell, który jest zalecanym modułem programu PowerShell do interakcji z platformą Azure. Aby rozpocząć pracę z modułem Azure PowerShell, zobacz Instalowanie programu Azure PowerShell. Aby dowiedzieć się, jak przeprowadzić migrację do modułu Az PowerShell, zobacz Migracja programu Azure PowerShell z modułu AzureRM do modułu Az.

Używanie usługi Azure Cloud Shell

Na platforma Azure hostowane jest Azure Cloud Shell, interaktywne środowisko powłoki, z którego można korzystać w przeglądarce. Do pracy z usługami platformy Azure można używać programu Bash lub PowerShell w środowisku Cloud Shell. Aby uruchomić kod w tym artykule, możesz użyć wstępnie zainstalowanych poleceń usługi Cloud Shell bez konieczności instalowania niczego w środowisku lokalnym.

Aby uruchomić środowisko Azure Cloud Shell:

Opcja Przykład/link
Wybierz pozycję Wypróbuj w prawym górnym rogu bloku kodu. Wybranie pozycji Wypróbuj nie spowoduje automatycznego skopiowania kodu do środowiska Cloud Shell. Screenshot that shows an example of Try It for Azure Cloud Shell.
Przejdź do witryny https://shell.azure.com lub wybierz przycisk Uruchom Cloud Shell, aby otworzyć środowisko Cloud Shell w przeglądarce. Screenshot that shows how to launch Cloud Shell in a new window.
Wybierz przycisk Cloud Shell na pasku menu w prawym górnym rogu witryny Azure Portal. Screenshot that shows the Cloud Shell button in the Azure portal

Aby uruchomić kod z tego artykułu w środowisku Azure Cloud Shell:

  1. Uruchom usługę Cloud Shell.

  2. Wybierz przycisk Kopiuj w bloku kodu, aby skopiować kod.

  3. Wklej kod do sesji usługi Cloud Shell, wybierając klawisze Ctrl+Shift+V w systemach Windows i Linux lub wybierając pozycję Cmd+Shift+V w systemie macOS.

  4. Naciśnij klawisz Enter, aby uruchomić kod.

Korzystanie z programu PowerShell lokalnie lub przy użyciu usługi Azure Cloud Shell wymaga programu Azure PowerShell 2.3.2 lub nowszej wersji. Jeśli chcesz uaktualnić, zobacz Instalowanie modułu programu Azure PowerShell lub uruchom poniższy przykładowy skrypt, aby zainstalować moduł dla bieżącego użytkownika:

Install-Module -Name Az -AllowClobber -Scope CurrentUser

Jeśli używasz programu PowerShell lokalnie, musisz też uruchomić polecenie Connect-AzAccount, aby utworzyć połączenie z platformą Azure.

Przykładowe skrypty

# 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

Następne kroki

Aby uzyskać więcej informacji na temat programu Azure PowerShell, zobacz dokumentację programu Azure PowerShell.

Dodatkowe przykłady skryptów programu PowerShell dla usługi SQL Managed Instance można znaleźć w skryptach programu PowerShell usługi Azure SQL Managed Instance.