Skrip PowerShell untuk mengaktifkan enkripsi data transparan menggunakan kunci Anda sendiri

Berlaku untuk:Azure SQL Managed Instance

Contoh skrip PowerShell ini mengonfigurasi enkripsi data transparan (TDE) di Azure SQL Managed Instance, menggunakan kunci yang dikelola pelanggan dari Azure Key Vault. Hal ini sering disebut sebagai skenario bring-your-own-key (BYOK) untuk TDE. Untuk mempelajari selengkapnya, lihat Enkripsi Data Transparan Azure SQL dengan kunci yang dikelola pelanggan.

Prasyarat

Jika Anda tidak memiliki langganan Azure, buat akun Azure gratis sebelum memulai.

Catatan

Artikel ini menggunakan modul Azure Az PowerShell, yang merupakan modul PowerShell yang direkomendasikan untuk berinteraksi dengan Azure. Untuk mulai menggunakan modul Az PowerShell, lihat Menginstal Azure PowerShell. Untuk mempelajari cara bermigrasi ke modul Az PowerShell, lihat Memigrasikan Azure PowerShell dari AzureRM ke Az.

Menggunakan Azure Cloud Shell

Azure meng-hosting Azure Cloud Shell, lingkungan shell interaktif yang dapat Anda gunakan melalui browser. Anda dapat menggunakan Bash atau PowerShell dengan Cloud Shell untuk bekerja dengan layanan Azure. Anda dapat menggunakan perintah Cloud Shell yang telah diinstal sebelumnya untuk menjalankan kode dalam artikel ini tanpa harus menginstal apa-apa di lingkungan lokal Anda.

Untuk memulai Azure Cloud Shell:

Opsi Contoh/Tautan
Pilih Coba di sudut kanan atas blok kode. Memilih Coba tidak secara otomatis menyalin kode ke Cloud Shell. Screenshot that shows an example of Try It for Azure Cloud Shell.
Buka https://shell.azure.com, atau pilih tombol Luncurkan Cloud Shell untuk membuka Cloud Shell di browser Anda. Screenshot that shows how to launch Cloud Shell in a new window.
Pilih tombol Cloud Shell pada bilah menu di kanan atas di portal Microsoft Azure. Screenshot that shows the Cloud Shell button in the Azure portal

Untuk menjalankan kode dalam artikel ini di Azure Cloud Shell:

  1. Mulai Cloud Shell.

  2. Pilih tombol Salin pada blok kode untuk menyalin kode.

  3. Tempelkan kode tersebut ke sesi Cloud Shell dengan memilih Ctrl+Shift+V untuk Windows dan Linux, atau dengan memilih Cmd+Shift+V untuk macOS.

  4. Pilih Enter untuk menjalankan kode.

Menggunakan PowerShell secara lokal atau menggunakan Azure Cloud Shell memerlukan Azure PowerShell 2.3.2 atau versi yang lebih baru. Jika Anda perlu memutakhirkan, lihat Menginstal modul Azure PowerShell, atau menjalankan contoh skrip di bawah ini untuk menginstal modul untuk pengguna saat ini:

Install-Module -Name Az -AllowClobber -Scope CurrentUser

Jika Anda menjalankan PowerShell secara lokal, Anda juga perlu menjalankan Connect-AzAccount untuk membuat koneksi dengan Azure.

Skrip sampel

# 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

Langkah berikutnya

Untuk informasi selengkapnya tentang Azure PowerShell, lihat Dokumentasi Microsoft Azure PowerShell.

Sampel skrip PowerShell tambahan untuk Azure SQL Managed Instance dapat ditemukan di skrip PowerShell Azure SQL Managed Instance.