TDE with SQL Server AlwaysOn: Certificate Propagation to Availability Replicas

Comments 0

Share to social media

Executive Summary

Transparent Data Encryption (TDE) encrypts SQL Server database files at rest – including data files, log files, and backups – using a database encryption key (DEK) protected by a certificate stored in the master database. Adding TDE to a database that is part of an AlwaysOn Availability Group requires an additional step: the TDE certificate must be backed up and restored to every secondary replica’s master database before the secondary can decrypt and apply the encrypted log stream. This article walks through the complete process for SQL Server 2012: creating the master key, creating the TDE certificate, encrypting the database, backing up the certificate and private key, and restoring them to each secondary replica.

Previously, in my article SQL Server 2012 AlwaysOn, I discussed the components that make up SQL Server 2012’s AlwaysOn and how you can configure your SQL Server environment using AlwaysOn Availability Groups (AAG) to meet the ever-increasing need for ‘High Availability’ (HA) and ‘Disaster Recovery’ (DR). One of the benefits that I outlined was the ability to use the Enterprise Edition feature ‘Transparent Data Encryption’ (TDE) to secure your databases.

Transparent Data Encryption

TDE allows you to protect your databases by performing real-time I/O encryption utilising keys. This prevents anyone who does not have these keys from accessing the data. I am not going to dive into the details of TDE in this article; there are links in the References & Further Reading section later in the article for you to get a better understanding of TDE.

Configuring your AlwaysOn environment

If you have not yet set up and configured a SQL Server 2012 AlwaysOn environment, then please have a read of my article SQL Server AlwaysOn. In this article I shall assume that you already have an environment set up that is already using AlwaysOn Availability Groups (AAG). Normally, if you are going to create a new AAG or need to add a database into an existing AAG, you can simply use the wizards provided in SSMS. However, if you are going to add a database that has been configured to be encrypted using TDE then you will not be able to use these wizards.

Whether you are creating a new AAG, or you are needing to add a database that has been encrypted using TDE, there are steps that you need to complete on your SQL Server 2012 AlwaysOn Primary Replica. These are:

  • Create a Master Key on the Primary Replica
  • Backup the Master Key
  • Create a Certificate protected by the Master Key
  • Backup the Certificate
  • Create a Database Encryption Key
  • Enable a TDE on a Database

Here is some example code that can be used to perform these tasks:

We need to undertake some configuration steps on the Secondary Replicas in order to allow us to be able to replicate the Availability Databases. We need to:

  • Create a Master Key on the Secondary Replica
  • Backup the Master Key
  • Create Certificate from the Primary Replica

Here is some example code that can be used to perform these tasks:

The above will need to be undertaken on every Secondary Replica in the SQL Server 2012 AlwaysOn environment. After the Replicas are all configured with the Certificate, the encrypted database(s) can then be made available on all of the Replicas.

When we are using the ‘New Availability Group Wizard’, we have to select those database(s) which will participate in the AlwaysOn Availability Group. If one of the databases has been enabled for encryption utilising TDE, we will not be able to use the wizard to create the AlwaysOn Availability Group. To achieve this, we need to use T-SQL or PowerShell to create the Availability Group.

This T-SQL Code (to run in SQLCMD mode in SSMS) will manually create an Availability Group:

— The following code needs to be run using SQLCMD Mode

Alternatively if you prefer to use PowerShell here is some code to manually create an Availability Group:

If we already have an AAG, and we need to add a database that has been configured for encryption, then a slight modification to the T-SQL code above will allow us to achieve this. Swap Steps 1 & 2 in the T-SQL Code above for the code below:

We have now configured the environment to have an Encrypted database participate in a SQL Server 2012 AlwaysOn Availability Group.

References & Further Reading

FAQs: Encrypting Your SQL Server 2012 AlwaysOn Availability Databases

1. How do I add Transparent Data Encryption to a SQL Server AlwaysOn database?

On the primary replica: (1) Create a master key in the master database if one doesn’t exist. (2) Create a certificate protected by the master key. (3) Create a database encryption key (DEK) on the target database using the certificate. (4) Enable encryption: ALTER DATABASE [dbname] SET ENCRYPTION ON. Then propagate to secondary replicas: (5) Back up the certificate and private key from the primary with BACKUP CERTIFICATE. (6) On each secondary replica, restore the certificate from the backup files. Secondary replicas can then decrypt the encrypted log stream.

2. What is Transparent Data Encryption in SQL Server?

TDE encrypts the physical files of a SQL Server database at rest – the .mdf, .ndf, and .ldf files – so that someone with physical access to the files cannot read the data without the encryption keys. The encryption is transparent to the application: SQL Server decrypts data as it reads pages into the buffer pool, and the application connects normally. TDE protects against data exposure from stolen or improperly decommissioned hardware and storage, but does not protect against access by authenticated SQL Server users with appropriate permissions.

3. Do I need to do anything special for backups when using TDE?

Yes. TDE-encrypted database backups are also encrypted – they cannot be restored to a server that does not have the same TDE certificate. Before setting up TDE, back up the certificate and private key using BACKUP CERTIFICATE and store them securely offline. Without this backup, you cannot restore your database to another server instance. The private key backup requires a password for protection. Keep the certificate backup and password in a secure, separate location from the database backups themselves.

4. Has the TDE and AlwaysOn process changed in newer SQL Server versions?

Yes. In SQL Server 2014 and later, TDE certificates are propagated to secondary replicas more automatically, and the process of adding a TDE-encrypted database to an Availability Group has been simplified. Azure SQL Database and SQL Managed Instance handle encryption differently – encryption at rest is always on and managed by the service. For SQL Server 2016 and later, review the current Microsoft documentation for TDE + AlwaysOn configuration, as several steps described in this 2012-era article may have been superseded.

Article tags

About the author

Warwick Rudd

See Profile

Warwick Rudd is a Microsoft Certified Master – SQL 2008, MVP, MCT and the Principal Consultant at SQL Masters Consulting (http://www.sqlmastersconsulting.com.au). Warwick is a frequent speaker at local SQL Server User Groups and SQL Saturday events in Australia and New Zealand. You can find Warwick online with his blog (http://www.sqlmastersconsulting.com.au/blog) or on Twitter (@Warwick_Rudd). When he is not playing with the SQL Server Stack, he likes to get away to the cold and spend time snowboarding.

Warwick Rudd's contributions