mirror of
https://github.com/Microsoft/sql-server-samples.git
synced 2025-12-08 14:58:54 +00:00
22 KiB
22 KiB
In [15]:
USE master;
GO
sp_configure 'show advanced options', 1;
GO
RECONFIGURE WITH OVERRIDE;
GO
-- Enable EKM provider
sp_configure 'EKM provider enabled', 1;
GO
RECONFIGURE WITH OVERRIDE;
GOCommands completed successfully.
Configuration option 'show advanced options' changed from 1 to 1. Run the RECONFIGURE statement to install.
Commands completed successfully.
Configuration option 'EKM provider enabled' changed from 1 to 1. Run the RECONFIGURE statement to install.
Commands completed successfully.
Total execution time: 00:00:00.059
In [16]:
-- Create a cryptographic provider, using the SQL Server Connector
-- which is an EKM provider for the Azure Key Vault. This example uses
-- the name AzureKeyVault_EKM_Prov.
CREATE CRYPTOGRAPHIC PROVIDER AzureKeyVault_EKM_Prov
FROM FILE = 'C:\Program Files\SQL Server Connector for Microsoft Azure Key Vault\Microsoft.AzureKeyVaultService.EKM.dll';
GO
Commands completed successfully.
Total execution time: 00:00:00.022
In [25]:
CREATE CREDENTIAL Azure_EKM_TDE_cred
WITH IDENTITY = 'SQLStandardKeyVault', -- for global Azure
-- WITH IDENTITY = 'ContosoDevKeyVault.vault.usgovcloudapi.net', -- for Azure Government
-- WITH IDENTITY = 'ContosoDevKeyVault.vault.azure.cn', -- for Azure China 21Vianet
-- WITH IDENTITY = 'ContosoDevKeyVault.vault.microsoftazure.de', -- for Azure Germany
SECRET = '<combination of AAD Client ID without hyphens and AAD Client Secret>'
FOR CRYPTOGRAPHIC PROVIDER AzureKeyVault_EKM_Prov
Commands completed successfully.
Total execution time: 00:00:00.012
In [27]:
CREATE ASYMMETRIC KEY dbAKV_Key
FROM PROVIDER [AzureKeyVault_EKM_Prov]
WITH PROVIDER_KEY_NAME = 'SQLTDEKey', -- This is the KEY that was created in the Azure Key Vault
CREATION_DISPOSITION = OPEN_EXISTING;Commands completed successfully.
Total execution time: 00:00:00.145
In [30]:
USE master;
-- Create a SQL Server login associated with the asymmetric key
-- for the Database engine to use when it loads a database
-- encrypted by TDE.
CREATE LOGIN TDE_Login
FROM ASYMMETRIC KEY dbAKV_Key;
GO
-- Alter the TDE Login to add the credential for use by the
-- Database Engine to access the key vault
ALTER LOGIN TDE_Login
ADD CREDENTIAL Azure_EKM_TDE_cred ;
GO
CREATE DATABASE dbAKVTDE;
GO
USE dbAKVTDE;
GO
CREATE DATABASE ENCRYPTION KEY
WITH ALGORITHM = AES_256
ENCRYPTION BY SERVER ASYMMETRIC KEY dbAKV_Key; -- Use the key created above
GO
-- Alter the database to enable transparent data encryption.
ALTER DATABASE dbAKVTDE
SET ENCRYPTION ON;
GO In [36]:
USE MASTER
SELECT name,provider_type, algorithm_desc FROM sys.asymmetric_keys
-- Check which databases are encrypted using TDE
SELECT SERVERPROPERTY('Edition') as [Edition],d.name, dek.encryption_scan_state_desc, dek.encryptor_type
FROM sys.dm_database_encryption_keys AS dek
JOIN sys.databases AS d
ON dek.database_id = d.database_id;Out [36]:
(1 row affected)
(3 rows affected)
Total execution time: 00:00:00.048
| name | provider_type | algorithm_desc |
|---|---|---|
| dbAKV_Key | CRYPTOGRAPHIC PROVIDER | RSA_2048 |
| Edition | name | encryption_scan_state_desc | encryptor_type |
|---|---|---|---|
| Standard Edition (64-bit) | tempdb | COMPLETE | ASYMMETRIC KEY |
| Standard Edition (64-bit) | dbTDE | COMPLETE | CERTIFICATE |
| Standard Edition (64-bit) | dbAKVTDE | COMPLETE | ASYMMETRIC KEY |