Home
%3CLINGO-SUB%20id%3D%22lingo-sub-1029357%22%20slang%3D%22en-US%22%3E'Password%20required'%20when%20trying%20to%20add%20a%20database%20having%20a%20'master%20key'%20to%20AG%20group%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1029357%22%20slang%3D%22en-US%22%3E%3CP%3EWhen%20you%20are%20trying%20add%20a%20database%20having%20a%20%E2%80%98master%20key%E2%80%99%20to%20a%20AG%20group%2C%20you%20will%20see%20the%20message%20in%20SSMS.%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F158686iDC899DC00F470081%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22clipboard_image_4.png%22%20title%3D%22clipboard_image_4.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EYou%20can%E2%80%99t%20move%20to%20next%20step%20until%20you%20put%20the%20correct%20password%20of%20the%20%E2%80%98master%20key%E2%80%99.%3C%2FP%3E%0A%3CP%3EThis%20is%20an%20requirement%20of%20SSMS%20to%20help%20you%20manage%20SQL%20Server%20database%20master%20key%20password%20in%20secondary%20replicas.%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3EWhy%20we%20have%20this%20requirement%3F%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3EWhen%20a%20master%20key%20is%20created%2C%20it%E2%80%99s%20encrypted%20by%20both%20password%20and%20service%20master%20key.%3C%2FP%3E%0A%3CP%3Ecreate%20MASTER%20KEY%20ENCRYPTION%20BY%20PASSWORD%20%3D%20'Password1'%3B%3C%2FP%3E%0A%3CP%3EThis%20master%20key%20will%20be%20open%20automatically%20when%20it%E2%80%99s%20need%20for%20decryption%20or%20encryption.%20In%20this%20case%2C%20it%20is%20not%20necessary%20to%20use%20the%20%E2%80%98OPEN%20Master%20Key%E2%80%99%20T-SQL%20statement.%3C%2FP%3E%0A%3CP%3EHowever%2C%20when%20a%20database%20is%20first%20attached%20or%20restored%20to%20a%20%3CSTRONG%3Enew%3C%2FSTRONG%3E%20instance%20of%20SQL%20Server%2C%20a%20copy%20of%20the%20database%20master%20key%20(encrypted%20by%20the%20service%20master%20key)%20is%20not%20yet%20stored%20in%20the%20server.%20You%20must%20use%20the%20%3CSTRONG%3EOPEN%20MASTER%20KEY%3C%2FSTRONG%3E%20statement%20to%20decrypt%20the%20database%20master%20key%20(DMK).%20Once%20the%20DMK%20has%20been%20decrypted%2C%20you%20have%20the%20option%20of%20enabling%20automatic%20decryption%20in%20the%20future%20by%20using%20the%20%3CSTRONG%3EALTER%20MASTER%20KEY%20REGENERATE%3C%2FSTRONG%3E%20statement%20to%20provision%20the%20server%20with%20a%20copy%20of%20the%20DMK%2C%20encrypted%20with%20the%20service%20master%20key.%3C%2FP%3E%0A%3CP%3ELet%E2%80%99s%20go%20back%20to%20SQL%20Server%20Alwayson%20Availability%20group%20scenario.%20If%20the%20password%20is%20not%20provided%2C%20the%20database%20is%20able%20to%20restored%20in%20secondary%20replicas.%20However%2C%20the%20database%20master%20key%20can%E2%80%99t%20be%20opened%20automatically%20because%20it%E2%80%99s%20not%20managed%20by%20the%20Service%20Master%20key%20in%20the%20secondary%20replica.%20It%20may%20cause%20application%20fail.%3C%2FP%3E%0A%3CP%3ETo%20avoid%20this%20issue%2C%20SQL%20Server%20(since%202016)%20has%20this%20enhancement%20to%20open%20the%20database%20master%20key%20automatically%20in%20secondary%20replicas.%3C%2FP%3E%0A%3CP%3EThe%20wizard%20will%20call%20stored%20procedure%20sp_control_dbmasterkey_password%20in%20all%20replicas%20to%20create%20an%20credential%20containing%20the%20password%20of%20master%20key.%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F158685iD358529116519344%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22clipboard_image_5.png%22%20title%3D%22clipboard_image_5.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EWhen%20SQL%20Server%20needs%20a%20database%20master%20key%20to%20decrypt%20or%20encrypt%20a%20key%2C%20SQL%20Server%20tries%20to%20decrypt%20the%20database%20master%20key%20with%20the%20service%20master%20key%20of%20the%20instance.%20If%20the%20decryption%20fails%2C%20SQL%20Server%20searches%20the%20credential%20store%20for%20master%20key%20credentials%20that%20have%20the%20same%20family%20GUID%20as%20the%20database%20for%20which%20it%20needs%20the%20master%20key.%20SQL%20Server%20then%20tries%20to%20decrypt%20the%20database%20master%20key%20with%20each%20matching%20credential%20until%20the%20decryption%20succeeds%20or%20there%20are%20no%20more%20credentials.%3C%2FP%3E%0A%3CP%3EThis%20is%20transparent%20to%20application.%20The%20application%20is%20able%20to%20use%20encrypt%2Fdecrypt%20in%20secondary%20replicas%20as%20it%20does%20in%20primary%20replica%20without%20modifying%20any%20code.%3C%2FP%3E%0A%3CP%3EHere%20is%20an%20example%3A%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%221%22%20color%3D%22%230000ff%22%3Ecreate%20database%20dbtest%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%221%22%20color%3D%22%230000ff%22%3Ego%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%221%22%20color%3D%22%230000ff%22%3Euse%20dbtest%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%221%22%20color%3D%22%230000ff%22%3Ego%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%221%22%20color%3D%22%230000ff%22%3ECREATE%20MASTER%20KEY%20ENCRYPTION%20BY%20PASSWORD%20%3D%20'Password1'%3B%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%221%22%20color%3D%22%230000ff%22%3E---This%20database%20master%20key%20will%20be%20encrypted%20by%20service%20master%20key%20automatically.%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%221%22%20color%3D%22%230000ff%22%3Ego%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%221%22%20color%3D%22%230000ff%22%3ECREATE%20CERTIFICATE%20%5BcertTest%5D%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%221%22%20color%3D%22%230000ff%22%3EWITH%20SUBJECT%20%3D%20'certTest2019'%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%221%22%20color%3D%22%230000ff%22%3E---This%20certificate%20will%20be%20encrypted%20by%20database%20master%20key%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%221%22%20color%3D%22%230000ff%22%3Ego%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%221%22%20color%3D%22%230000ff%22%3ECREATE%20SYMMETRIC%20KEY%20%5Bsymkey_Test%5D%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%221%22%20color%3D%22%230000ff%22%3EWITH%20ALGORITHM%20%3D%20AES_192%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%221%22%20color%3D%22%230000ff%22%3EENCRYPTION%20BY%20CERTIFICATE%20%5BcertTest%5D%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%221%22%20color%3D%22%230000ff%22%3E---this%20symmetric%20key%20will%20be%20encrypted%20by%20the%20certificate%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%221%22%20color%3D%22%230000ff%22%3Ego%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%221%22%20color%3D%22%230000ff%22%3E---This%20is%20a%20stored%20procedure%20to%20encrypt%2Fdecrypt.%20Assume%20application%20will%20call%20the%20stored%20procedure%20to%20do%20transactions%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%221%22%20color%3D%22%230000ff%22%3Ecreate%20proc%20procTest%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%221%22%20color%3D%22%230000ff%22%3Eas%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%221%22%20color%3D%22%230000ff%22%3EOPEN%20SYMMETRIC%20KEY%20%5Bsymkey_Test%5D%20DECRYPTION%20BY%20CERTIFICATE%20%5BcertTest%5D%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%221%22%20color%3D%22%230000ff%22%3Edeclare%20%40blob%20varbinary(1000)%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%221%22%20color%3D%22%230000ff%22%3Edeclare%20%40pt%20varchar(1000)%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%221%22%20color%3D%22%230000ff%22%3ESET%20%40blob%20%3D%20encryptbykey(%20key_guid(%20'symkey_Test')%2C%20'data'%20)%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%221%22%20color%3D%22%230000ff%22%3ESET%20%40pt%20%3D%20convert(%20varchar(1000)%2C%20decryptbykey(%20%40blob%20))%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%221%22%20color%3D%22%230000ff%22%3ESELECT%20%40pt%2C%20%40blob%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%221%22%20color%3D%22%230000ff%22%3Eclose%20SYMMETRIC%20KEY%20%5Bsymkey_Test%5D%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%221%22%20color%3D%22%230000ff%22%3Ego%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3EOnce%20the%20database%20is%20stored%20to%20a%20different%20SQL%20Server%20instance.%20Running%20the%20%E2%80%98procTest%E2%80%99%20will%20fails%20because%20the%20database%20master%20key%20can%E2%80%99t%20be%20opened%20by%20the%20SQL%20Server%20instance.%3C%2FP%3E%0A%3CP%3EIn%20your%20case%2C%20if%20master%20key%20is%20not%20used(encryption%2Fdescription%2Fservice%20broker%2C%20etc%20)%2C%20cx%20can%20remove%20it.%3C%2FP%3E%0A%3CP%3EAn%20known%20issue%3A%20%E2%80%98sp_control_dbmasterkey_password%E2%80%99%20is%20only%20executed%20if%20%E2%80%98full%20database%20and%20log%20backup%E2%80%99%20option%20is%20checked%20in%20%E2%80%98Select%20Initial%20Data%20Synchronization%E2%80%99%20section%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F158687iF261C36A715F6D03%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22clipboard_image_6.png%22%20title%3D%22clipboard_image_6.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%E2%80%98Checking%20password%20of%20the%20database%20master%20key%E2%80%99%20is%20skipped%20if%20you%20use%20other%20options.%20And%20%E2%80%98sp_control_dbmasterkey_password%E2%80%99%20is%20not%20called.%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F158689i1B808E14989636BA%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22clipboard_image_7.png%22%20title%3D%22clipboard_image_7.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1029357%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EHigh%20Availability%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EStorage%20Engine%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Microsoft

When you are trying add a database having a ‘master key’ to a AG group, you will see the message in SSMS.

clipboard_image_4.png

 

You can’t move to next step until you put the correct password of the ‘master key’.

This is an requirement of SSMS to help you manage SQL Server database master key password in secondary replicas.

Why we have this requirement?

When a master key is created, it’s encrypted by both password and service master key.

create MASTER KEY ENCRYPTION BY PASSWORD = 'Password1';

This master key will be open automatically when it’s need for decryption or encryption. In this case, it is not necessary to use the ‘OPEN Master Key’ T-SQL statement.

However, when a database is first attached or restored to a new instance of SQL Server, a copy of the database master key (encrypted by the service master key) is not yet stored in the server. You must use the OPEN MASTER KEY statement to decrypt the database master key (DMK). Once the DMK has been decrypted, you have the option of enabling automatic decryption in the future by using the ALTER MASTER KEY REGENERATE statement to provision the server with a copy of the DMK, encrypted with the service master key.

Let’s go back to SQL Server Alwayson Availability group scenario. If the password is not provided, the database is able to restored in secondary replicas. However, the database master key can’t be opened automatically because it’s not managed by the Service Master key in the secondary replica. It may cause application fail.

To avoid this issue, SQL Server (since 2016) has this enhancement to open the database master key automatically in secondary replicas.

The wizard will call stored procedure sp_control_dbmasterkey_password in all replicas to create an credential containing the password of master key.

clipboard_image_5.png

 

When SQL Server needs a database master key to decrypt or encrypt a key, SQL Server tries to decrypt the database master key with the service master key of the instance. If the decryption fails, SQL Server searches the credential store for master key credentials that have the same family GUID as the database for which it needs the master key. SQL Server then tries to decrypt the database master key with each matching credential until the decryption succeeds or there are no more credentials.

This is transparent to application. The application is able to use encrypt/decrypt in secondary replicas as it does in primary replica without modifying any code.

Here is an example:

create database dbtest

go

use dbtest

go

CREATE MASTER KEY ENCRYPTION BY PASSWORD = 'Password1';

---This database master key will be encrypted by service master key automatically.

go

CREATE CERTIFICATE [certTest]

WITH SUBJECT = 'certTest2019'

---This certificate will be encrypted by database master key

go

CREATE SYMMETRIC KEY [symkey_Test]

WITH ALGORITHM = AES_192

ENCRYPTION BY CERTIFICATE [certTest]

---this symmetric key will be encrypted by the certificate

go

---This is a stored procedure to encrypt/decrypt. Assume application will call the stored procedure to do transactions

create proc procTest

as

OPEN SYMMETRIC KEY [symkey_Test] DECRYPTION BY CERTIFICATE [certTest]

declare @blob varbinary(1000)

declare @pt varchar(1000)

SET @blob = encryptbykey( key_guid( 'symkey_Test'), 'data' )

SET @pt = convert( varchar(1000), decryptbykey( @blob ))

SELECT @pt, @blob

close SYMMETRIC KEY [symkey_Test]

go

Once the database is stored to a different SQL Server instance. Running the ‘procTest’ will fails because the database master key can’t be opened by the SQL Server instance.

In your case, if master key is not used(encryption/description/service broker, etc ), cx can remove it.

An known issue: ‘sp_control_dbmasterkey_password’ is only executed if ‘full database and log backup’ option is checked in ‘Select Initial Data Synchronization’ section

                          

clipboard_image_6.png

 

‘Checking password of the database master key’ is skipped if you use other options. And ‘sp_control_dbmasterkey_password’ is not called.

clipboard_image_7.png