تشفير البيانات الشفاف في مثيل SQL المُدار باستخدام مفتاحك الخاص من مخزن المفاتيح Azure

ينطبق على: مثيل Azure SQL المُدار

يقوم مثال البرنامج النصي PowerShell هذا بتكوين تشفير البيانات الشفاف (TDE) باستخدام مفتاح مُدار بواسطة العميل لمثيل Azure SQL المُدار، باستخدام مفتاح من مخزن المفاتيح Azure. غالباً ما يشار إلى هذا على أنه سيناريو إنشاء المفتاح الخاص بك (BYOK) لتشفير البيانات الشفاف (TED). لمعرفة المزيد، يمكنك الاطلاع على تشفير البيانات الشفافة في Azure SQL باستخدام مفتاح العميل المُدار.

المتطلبات الأساسية

إذا لم يكن لديك اشتراك في Azure، فأنشئ حساب Azure مجاني قبل أن تبدأ.

ملاحظة

تستخدم هذه المقالة الوحدة النمطية Azure Az PowerShell، وهي الوحدة النمطية PowerShell الموصى بها للتفاعل مع Azure. لبدء استخدام الوحدة النمطية Az PowerShell، راجع تثبيت Azure PowerShell. لمعرفة كيفية الترحيل إلى الوحدة النمطية Az PowerShell، راجع ترحيل Azure PowerShell من AzureRM إلى Az.

استخدام Azure Cloud Shell

Azure يستضيف Azure Cloud Shell، بيئة تفاعلية يمكن استخدامها من خلال المستعرض. يمكنك استخدام Bash أو PowerShell مع Cloud Shell للعمل مع خدمات Azure. يمكنك استخدام أوامر Cloud Shell المثبتة مسبقًا لتشغيل التعليمات البرمجية في هذه المقالة دون الحاجة إلى تثبيت أي شيء على البيئة المحلية.

لبدء Azure Cloud Shell:

خيار مثال/ رابط
انقر فوق ⁧⁩جربه⁧⁩ في الزاوية العلوية اليسرى من كتلة التعليمات البرمجية. تحديد ⁧⁩جربه⁧⁩ لا يقوم بنسخ التعليمات البرمجية تلقائيًا إلى Cloud Shell. Screenshot that shows an example of Try It for Azure Cloud Shell.
انتقل إلى ⁧⁩⁧ https://shell.azure.com⁩⁧⁩، أو حدد زر ⁩تشغيل Cloud Shell لفتح Cloud Shell في المتصفح لديك. Screenshot that shows how to launch Cloud Shell in a new window.
حدد زر ⁧⁩Cloud Shell⁧⁩ في شريط القوائم في أعلى اليمين في ⁧⁩مدخل Azure⁧⁩. Screenshot that shows the Cloud Shell button in the Azure portal

لتشغيل التعليمة البرمجية في هذا المقال في Azure Cloud Shell:

  1. ابدأ تشغيل Cloud Shell.

  2. حدد الزر ⁧⁩نسخ⁧⁩ على كتلة التعليمات البرمجية لنسخ التعليمات البرمجية.

  3. ألصق تعليمة البرمجية في جلسة Cloud Shell بتحديد Ctrl+Shift+Vعلى Windows وLunix، أو بتحديد Cmd+Shift+Vعلى macOS.

  4. اكتب ⁧⁩"Enter"⁧⁩ لتشغيل الأمر.

يتطلب استخدام PowerShell المحلي أو استخدام Azure Cloud Shell وجود Azure PowerShell 2.3.2 أو إصداراً أحدث. إذا كنت بحاجة إلى الترقية، يمكنك الاطلاع على تثبيت وحدة Azure PowerShell، أو قم بتشغيل نموذج البرنامج النصي أدناه لتثبيت الوحدة النمطية للمستخدم الحالي:

Install-Module -Name Az -AllowClobber -Scope CurrentUser

إذا كنت تقوم بتشغيل PowerShell محليّاً، فإنك تحتاج أيضاً إلى تشغيل Connect-AzAccount لإنشاء اتصال مع Azure.

نماذج البرامج النصية

# 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

الخطوات التالية

لمزيد من المعلومات حول Azure PowerShell، راجع مستندات Azure PowerShell.

يمكن العثور على نماذج نصية إضافية لبرنامج PowerShell لـ SQL Managed Instance في البرامج النصية Azure SQL Managed Instance PowerShell scripts.