Enabling Transparent data encryption using Customer-managed key on Azure SQL Managed Instances that are part of failover group

Environment

-> I have two Azure SQL Managed Instances that are part of a failover group. Currently these two instances have Transparent data encryption (TDE) enabled using service-managed key.

-> Please note that the SQL Managed Instances as part of failover group as depicted in above diagram are not in correct paired regions. I have created this for testing only. SQL managed instances that will be deployed in a failover group should use paired regions for performance reasons. Managed instances residing in geo-paired regions have much better performance compared to unpaired regions. For example Managed instances deployed in East US and West US will be a good fit for a filover group as they are in paired regions. For more details on paired regions, refer this article.

-> My requirement is to configure Transparent data encryption (TDE) using Customer-Managed key as I have a requirement to backup the database in one Managed Instance that is part of a failover group and restore it to a different Managed Instance that is part of a different failover group. The restores are basically required to configure Transactional replication between Managed Instances using “Initialize from LSN”.

-> I am using this article as a reference to configure Transparent data encryption (TDE) using Customer-Managed key on SQL managed Instances.

-> Below are my Managed Instances configured,

-> Creating a key vault in East US to be used for SQL Managed Instance jbmi-eastus-db-primary using below steps,

-> If Managed Instance uses Azure AD, then below prerequisites should be followed.

-> In my case I dont have an Azure AD, so skipping above prerequisites.

-> Wait for the key vault in East US to get created.

-> Create another Key Vault using above procedure. But make sure you create the key vault in the region same as Managed Instance jbmi-eastus2-db-secondary. In my case it is East US2.

-> Below are the key vaults created in my case,

-> Check failover group portal and verify which instance is Primary and secondary. In my case jbmi-eastus-db-primary is primary and jbmi-eastus2-db-secondary is secondary.

-> Create a key in Key vault that is in East US which will be utilized by primary managed instance jbmi-eastus-db-primary using below procedure,

-> Now click on the key created. In my case it is MI-Key1. Click “Download Backup” and download the key backup.

-> Key backup file looks as below,

-> Now open key vault in East US2 that is associated with Managed instance jbmi-eastus2-db-secondary. Click on “Restore Backup” and navigate to the key backup file that was performed in earlier step.

-> Now its time to enable TDE using Customer-Managed key in Azure SQL Managed Instance.

-> Open Secondary managed instance jbmi-eastus2-db-secondary from azure portal and navigate to “Transparent data encryption” and follow below procedure,

-> Click on “Change Key”,

-> Click on “save”.

-> Getting below error,

Failed to save Transparent Data Encryption settings
Failed to save Transparent Data Encryption settings for SQL resource: jbmi-eastus2-db-secondary. Error message: The encryption protectors for all servers linked by GeoDR must be in the same region as their respective servers.Please upload key ‘https://eastus2-mi-keyvault.vault.azure.net/keys/MI-Key1/b1e1ceea175d43deb8ee2c29d039e0cd’ to a Key Vault in the region ‘East US’ as server ‘jbmi-eastus-db-primary’

-> The error states that the key is missing in East US key vault that is utilized by Primary Managed Instance. But we know that the key is there already. As it was first created there and then it was ported to Key vault in East US2.

-> Lets follow the below procedure now. Open Primary managed instance jbmi-eastus-db-Primary from azure portal and navigate to “Transparent data encryption” and follow below procedure,

-> IMPORTANT : In above screenshot, remember to uncheck “Make the selected key the default TDE protector.

-> Click “Save”,

-> Now open Secondary managed instance jbmi-eastus2-db-Secondary from azure portal and navigate to “Transparent data encryption” and follow below procedure,

-> IMPORTANT : In above screenshot, remember to check “Make the selected key the default TDE protector.

-> Now open Primary managed instance jbmi-eastus-db-Primary from azure portal and navigate to “Transparent data encryption” and follow below procedure,

-> IMPORTANT : In above screenshot, this time remember to check “Make the selected key the default TDE protector.

-> It is complete now. We have configured Transparent data encryption (TDE) using Customer-Managed key on Azure SQL Managed Instances that are part of a failover group.

-> Lets validate the setting once and try a failover and failback to check everything works fine.

-> First lets check if we are able to query the database JBDB on primary Managed Instance,

-> Next on Secondary Managed Instance. We get below error for the insert statement because secondary instance does not allow writes.

Msg 3906, Level 16, State 2, Line 5
Failed to update database “JBDB” because the database is read-only.

-> Lets perform a failover now,

-> Lets perform a failback now,

-> Failback completed fine. Lets query the database once and confirm,

-> Failover and failback completed fine. We are able to query the database without any issues.

Thank You,
Vivek Janakiraman

Disclaimer:
The views expressed on this blog are mine alone and do not reflect the views of my company or anyone else. All postings on this blog are provided “AS IS” with no warranties, and confers no rights.

The subscription does not exist

-> I was trying to create a subscription using below query on an existing transactional replication publication,

Use [JBDB]
GO
EXEC sp_addsubscription
@publication = N'JBDBREPL',
@subscriber = N'JBResearch\IN2019_1',
@destination_db = N'JBDBSub',
@sync_type= N'initialize from lsn',
@backupdevicetype='DISK',
@backupdevicename=N'c:\temp\jbdb.bak',
@subscription_type = N'push',
@subscriptionlsn =0x00000027000002500001,
@update_mode = N'read only';

-> We received below error when above query was executed,

Msg 14055, Level 11, State 1, Procedure sys.sp_MSrepl_dropsubscription, Line 178 [Batch Start Line 130]
The subscription does not exist.

-> I tried creating subscription from GUI and got below message,

You must select a Publisher and publication to continue the wizard.

-> Then I realized that the publication was created using TSQL and had no articles added,

-> I then selected required articles on the Publication and tried creating the subscription using below query,

Use [JBDB]
GO
EXEC sp_addsubscription
@publication = N'JBDBREPL',
@subscriber = N'JBResearch\IN2019_1',
@destination_db = N'JBDBSub',
@sync_type= N'initialize from lsn',
@backupdevicetype='DISK',
@backupdevicename=N'c:\temp\jbdb.bak',
@subscription_type = N'push',
@subscriptionlsn =0x00000027000002500001,
@update_mode = N'read only';

-> I got below message this time,

Msg 21399, Level 16, State 1, Procedure sys.sp_MSaddautonosyncsubscription, Line 274 [Batch Start Line 130]
The transactions required for synchronizing the subscription with the specified log sequence number (LSN) are unavailable at the Distributor. Specify a higher LSN.

-> I had to take a fresh publisher database backup and then restore it on the subscriber and had to use proper LSN to correct this. Please use this article to get more details on configuring transactional replication using Initialize from LSN.

-> Above steps will not be required if snapshot is used. As soon as article are added to the publication, you will be able to create a subscription that uses snapshot agent without any issues.

Thank You,
Vivek Janakiraman

Disclaimer:
The views expressed on this blog are mine alone and do not reflect the views of my company or anyone else. All postings on this blog are provided “AS IS” with no warranties, and confers no rights.

Cannot execute as the database principal because the principal “dbo” does not exist, this type of principal cannot be impersonated, or you do not have permission

-> I tried dropping a publication in a transactional replication setup and received below error,

TITLE: Microsoft SQL Server Management Studio

Could not delete publication ‘JBDBREP’.

For help, click: http://go.microsoft.com/fwlink?ProdName=Microsoft%20SQL%20Server&ProdVer=15.0.18338.0&EvtSrc=Microsoft.SqlServer.Management.UI.VSIntegration.ObjectExplorer.Replication.ReplicationMenuItem&EvtID=CantDeletePublication&LinkId=20476

ADDITIONAL INFORMATION:

An exception occurred while executing a Transact-SQL statement or batch. (Microsoft.SqlServer.ConnectionInfo)

Cannot execute as the database principal because the principal “dbo” does not exist, this type of principal cannot be impersonated, or you do not have permission.
Changed database context to ‘JBDBREPL’. (Microsoft SQL Server, Error: 15517)

For help, click: http://go.microsoft.com/fwlink?ProdName=Microsoft%20SQL%20Server&ProdVer=15.00.2070&EvtSrc=MSSQLServer&EvtID=15517&LinkId=20476

BUTTONS:
OK

-> The error message points to some issue related to owner of the replicated database.

-> Right click the publisher database and click on properties. Click on “Files” tab and check for Owner.

-> In my case I see it blank and this seems to be the problem.

-> I executed below query to change the owner to SA. Instead of SA an appropriate login will also help.

USE [JBDBREPL]
GO
ALTER AUTHORIZATION ON DATABASE::[JBDBREPL] TO [sa]
GO

-> I tried removing the publication and it worked fine this time.

Thank You,
Vivek Janakiraman

Disclaimer:
The views expressed on this blog are mine alone and do not reflect the views of my company or anyone else. All postings on this blog are provided “AS IS” with no warranties, and confers no rights.