Azure CLI 指令碼可讓您使用您自己的金鑰啟用透明資料加密

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

此 Azure CLI 指令碼範例使用 Azure Key Vault 的客戶自控金鑰,在 Azure SQL 受控執行個體中設定透明資料加密 (TDE)。 這通常稱為 TDE 的攜帶您自己的金鑰 (BYOK) 案例。 若要深入了解使用客戶自控金鑰的 TDE,請參閱 TDE 讓您在 Azure SQL 中攜帶您自己的金鑰

此範例需要現有的受控執行個體,請參閱使用 Azure CLI 建立 Azure SQL 受控執行個體

如果您沒有 Azure 訂閱,請在開始之前,先建立 Azure 免費帳戶

必要條件

範例指令碼

針對此指令碼,請在本機使用 Azure CLI,因為在 Cloud Shell 中執行將花費過長時間。

登入 Azure

使用下列指令碼,以使用特定訂閱進行登入。

subscription="<subscriptionId>" # add subscription here

az account set -s $subscription # ...or use 'az login'

如需詳細資訊,請參閱設定使用中訂閱以互動方式登入

執行指令碼

# Manage Transparent Data Encryption in a Managed Instance using your own key from Azure Key Vault

# Run this script after the script in https://docs.microsoft.com/en-us/azure/azure-sql/managed-instance/scripts/create-configure-managed-instance-cli creates a managed instance.
# You can use the same variables in both scripts/
# If running this script against a different existing instance, uncomment and add appropriate values to next 3 lines of code
# let "randomIdentifier=$RANDOM*$RANDOM"
# instance="<msdocs-azuresql-instance>" # add instance here
# resourceGroup="<msdocs-azuresql-rg>" # add resource here

# Variable block
location="East US"
vault="msdocssqlvault$randomIdentifier"
key="msdocs-azuresql-key-$randomIdentifier"

# echo assigning identity to service principal in the instance
az sql mi update --name $instance --resource-group $resourceGroup --assign-identity

echo "Creating $vault..."
az keyvault create --name $vault --resource-group $resourceGroup --location "$location"

echo "Getting service principal id and setting policy on $vault..."
instanceId=$(az sql mi show --name $instance --resource-group $resourceGroup --query identity.principalId --output tsv)

echo $instanceId
az keyvault set-policy --name $vault --object-id $instanceId --key-permissions get unwrapKey wrapKey

echo "Creating $key..."
az keyvault key create --name $key --vault-name $vault --size 2048 

# keyPath="C:\yourFolder\yourCert.pfx"
# keyPassword="yourPassword" 
# az keyvault certificate import --file $keyPath --name $key --vault-name $vault --password $keyPassword

echo "Setting security on $instance with $key..."
keyId=$(az keyvault key show --name $key --vault-name $vault -o json --query key.kid | tr -d '"')

az sql mi key create --kid $keyId --managed-instance $instance --resource-group $resourceGroup
az sql mi tde-key set --server-key-type AzureKeyVault --kid $keyId --managed-instance $instance --resource-group $resourceGroup

清除資源

您可以使用下列命令來移除資源群組及所有與其相關聯的資源,除非您仍持續需要這些資源,否則請使用 az group delete 命令。 某些資源可能需要一些時間才能建立或刪除。

az group delete --name $resourceGroup

範例參考

此指令碼會使用下列命令。 下表中的每個命令都會連結至命令特定的文件。

Command 描述
az sql db 資料庫命令。
az sql failover-group 容錯移轉群組命令。

後續步驟

如需 Azure CLI 的詳細資訊,請參閱 Azure CLI 文件

其他的 SQL Database CLI 指令碼範例可於 Azure SQL Database 文件中找到。