Skip to content

Commit b93d73b

Browse files
committed
Adding demo for Always Encrypted with secure enclaves
Adding the demo for Always Encrypted with secure enclaves in SQL Server 2019.
1 parent ea644e0 commit b93d73b

File tree

123 files changed

+32186
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

123 files changed

+32186
-0
lines changed

samples/features/security/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
<!--laceholder for summary.-->
44

5+
[Always Encrypted with secure enclaves Demos](always-encrypted-with-secure-enclaves)
6+
57
[Azure Active Directory Authentication Demos](azure-active-directory-auth)
68

79
[Azure SQL Security Demo](https://github.com/Microsoft/azure-sql-security-sample)
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
# Always Encrypted with secure enclaves
2+
3+
This sample/demo showcases the benefits of [Always Encrypted with secure enclaves](https://aka.ms/AlwaysEncryptedwithSecureEnclaves).
4+
5+
## About this sample
6+
- **Applies to:** SQL Server 2019 CTP 2.1
7+
- **Programming Language:** .NET C#, T-SQL
8+
- **Authors:** Jakub Szymaszek [jaszymas-MSFT]
9+
10+
This project has adopted the [Microsoft Open Source Code of Conduct](http://microsoft.github.io/codeofconduct). For more information see the [Code of Conduct FAQ](http://microsoft.github.io/codeofconduct/faq.md) or contact [[email protected]](mailto:[email protected]) with any additional questions or comments.
11+
12+
## Contents
13+
1. [Prerequisites](#prerequisites)
14+
2. [Setup](#setup)
15+
3. [Demo Part 1 - Tour the Clinic database and the demo application](#demo-part-1---tour-the-clinic-database-and-the-demo-application)
16+
4. [Demo Part 2 - Encrypt columns in place](#demo-part-2---encrypt-columns-in-place)
17+
5. [Demo Part 3 - Run rich queries from SSMS](#Demo-Part-3---run-rich-queries-from-SSMS)
18+
6. [Demo Part 4 - Re-encrypt and decrypt columns in-place](#demo-part-4---re-encrypt-and-decrypt-columns-in-place)
19+
7. [Resetting your demo environment](#resetting-your-demo-environment)
20+
21+
22+
23+
## Prerequisites
24+
You need two machines (they can be virtual machines):
25+
- The SQL Server computer:
26+
+ Windows Server 2019 Datacenter or Windows 10 Enterprise version 1809
27+
+ SQL Server 2019 CTP 2.1 or later
28+
+ [SQL Server Management Studio 18.0 or later](https://msdn.microsoft.com/en-us/library/mt238290.aspx)
29+
+ Visual Studio 2015 (or newer)
30+
+ If this machine is a virtual machine, it must be a generation 2 VM.
31+
- The HGS computer to host Windows Host Guardian Service for enclave attestation:
32+
+ Windows Server 2019 Datacenter or Standard
33+
34+
## Setup
35+
1. Configure host attestation for the SQL Server computer, following Steps 1-2 in [Tutorial: Getting started with Always Encrypted with secure enclaves using SSMS](https://aka.ms/AlwaysEncryptedEnclavesTutorial).
36+
1. Enable Always Encrypted with secure enclaves in your SQL Server instances by following instructions in Step 2 in [Tutorial: Getting started with Always Encrypted with secure enclaves using SSMS](https://aka.ms/AlwaysEncryptedEnclavesTutorial).
37+
1. Set up the Clinic demo database.
38+
+ Clone/Download the repository.
39+
+ Open SSMS and connect to your SQL Server 2019 instance.
40+
+ In SSMS, right-click on **Databases** in Object Explorer and select **Import Data-tier Application...**.
41+
+ Locate the **Clinic** bacpac file the **/setup** folder.
42+
![Import Data-tier Application Wizard](img/import-bacpac.png)
43+
+ Complete the steps of the wizard to import the **Clinic** database.
44+
1. Set up the database connection string in the demo application.
45+
+ Start Visual Studio and open the **ContosoClinic** solution file- located in **/src**.
46+
+ Using Solution Explorer, locate and open the **web.config** file under the **ContosoClinic** project.
47+
+ Look for the line that looks like this:
48+
```csharp
49+
<connectionStrings>
50+
<add name="DefaultConnection" connectionString="Data Source=.;Initial Catalog=Clinic; Integrated Security=true; Column Encrypteiond Setting = Enabled; Enclave Attestation URL=http://YourHGSComputer/Attestation"
51+
</connectionStrings>
52+
```
53+
+ Make sure the value of the **Data Source** key word in the database connection string is correct (identifies your instance).
54+
+ Make sure the **Initial Catalog** value is set to **Clinic**
55+
+ Replace **YourHGSComputer** with a DNS name or an IP address of your HGS computer.
56+
+ Save the file.
57+
58+
1. Build the demo application in Visual Studio.
59+
+ Right click on your project in Solution Explorer and select **Properties**.
60+
+ Make sure the .NET Framework 4.7.2 or higher is configured as the target .NET Framework for your project (change it, if necessary).
61+
![Contoso Clinic Application Properties .Net Framework Version Setting](img/dot-net-framework.png)
62+
+ Build the solution.
63+
64+
## Demo Part 1 - tour the Clinic database and the demo application
65+
1. Connect to your database using SSMS without Always Encrypted enabled in the database connection.
66+
- Open SSMS.
67+
- In the **Connect to Server** dialog, enter your server name and authentication parameters. For more information on using SSMS to connect to a Database, [click here](https://azure.microsoft.com/en-us/documentation/articles/sql-database-connect-query-ssms/).
68+
- Click the **Options >>** button and select the **Always Encrypted** tab. Make sure **Enable Always Encrypted** is not selected.
69+
![SSMS](img/ssms-ae-disabled.png)
70+
- Click **Connect**.
71+
1. In SSMS, open and execute **tsql-scripts/ListAllPatients.sql**. The results of this query include sensitive information about patients: social security numbers, names, dates of birth, etc.
72+
1. In SSMS, configure an extended event session, you will use to monitor Transact-SQL queries the demo application sends to the database.
73+
- Open and execute **tsql-scripts/CreateXESession.sql**. This creates an extended event session,
74+
- In Object Explorer, locate the newly created **ClinicDemo** extended event session - under your server, go to **Management/Extended Events/Sessions**.
75+
- Righ-click the **ClinicDemo** session and select **Watch Live Data**. This will open the **ClinicDemo Live Data** window.
76+
![Contoso Clinic Application](img/xe-session-watch-live-data.png)
77+
1. Run the demo application
78+
+ In Visual Studio, start the app (**F5**). This will open the Contoso Clinic application in the default browser.
79+
+ Select the **Patients** tab in the application.
80+
+ Enter a part of a patient's name and the maximum patient age. You should see the patients that meet your search criteria.
81+
![Contoso Clinic Application](img/list-of-patients.png)
82+
1. In SSMS, inspect the queries the demo application sends to the database.
83+
+ Select the **ClinicDemo Live Data** window, which should contain a table with some events your demo application triggered.
84+
+ If the table does not contain the **statement** column, right click on the header of the table and select **Choose Columns...**. Move **statement** to **Selected columns** and click **OK**.
85+
![XEventSessionColumns](img/xevent-session-columns.png)
86+
+ Back in the **ClinicDemo Live Data** window, double click on the statement column in the last row of the table to see the last query the application sent to the database. Inspect the query statement. Note the **WHERE** clause of the query contains the **LIKE** predicate on some string columns and a comparison (**>=**) on the **BirthDate** column.
87+
![XEventSessionColumns](img/xevent-before-encryption.png)
88+
+ Click **OK** to close the statement window.
89+
90+
## Demo Part 2 - Encrypt columns in place
91+
1. Connect to your database using SSMS with Always Encrypted enabled in the database connection.
92+
- Open a new instance of SSMS. (Keep the instance from the previous part of the demo open.)
93+
- In the **Connect to Server** dialog, enter your server name and authentication parameters.
94+
- Click the **Options >>** button and select the **Always Encrypted** tab. Make sure **Enable Always Encrypted** is selected. Enter the URL of your enclave attestation service, you have configured when setting up your demo environment.
95+
![SSMSAEEnabled](img/ssms-ae-enabled.png)
96+
- Click **Connect**.
97+
1. Provision a column master key in SSMS.
98+
+ In Object Explorer, expand your database and navigate to **Security/ Always Encrypted Keys/Column Master Keys**.
99+
+ Right-click on the **Column Master Keys** folder and select **New Column Master Key…**.
100+
![NewCMK](img/new-cmk.png)
101+
+ Enter a column master key name: **CMK1**.
102+
+ Select **Windows Certificate Store - Current User**.
103+
+ Make sure **Allow enclave computations** is selected.
104+
![NewCMK](img/new-cmk-dialog.png)
105+
+ Click **Generate Certificate** to create a new certificate to be used as a column master key.
106+
+ Click **OK**.
107+
1. Provision a column encryption key.
108+
+ In Object Explorer, expand your database and navigate to **Security/ Always Encrypted Keys/Column Encryption Keys**.
109+
+ Right-click on the **Column Encryption Keys** folder and select **New Column Encryption Key…**.
110+
+ Enter a column encryption key name: **CEK1**.
111+
+ Select **CMK1** as the column master key to protect your new column encryption key.
112+
+ Click **OK**.
113+
1. Encrypt a few columns in-place using Transact-SQL.
114+
+ In SSMS, open and review **tsql-scripts/EncryptColumns.sql**. Note the **ALTER TABLE ALTER COLUMN** statements that encrypt three columns: **SSN**, **LastName**, and **BirthDate**. The statements also change the sort order of both string columns to **BIN2**, which is required to support rich queries on those columns.
115+
+ Execute the script.
116+
1. Check if the columns are encrypted.
117+
+ Switch to the other instance of SSMS (from the previous part of the demo) that uses a database connection with Always Encrypted disabled.
118+
+ Rerun the query from **tsql-scripts/ListAllPatients.sql**. Note that the data the **SSN**, **LastName**, **BirthDate** columns are now encrypted.
119+
![NewCMK](img/encrypted-results.png)
120+
1. Test the Contoso Clinic web application.
121+
+ In your web browser, refresh the **Patients** page in the Contoso Clinic application. Notice the application shows plaintext data. This is because Always Encrypted has been already configured in the database connection for the application in the **web.config** file.
122+
+ Enter a part of a patient's name and the maximum patient age. You should see the patients that meet your search criteria.
123+
+ In SSMS, click on the **ClinicDemo Live Data** windows/tab, capturing extending events.
124+
+ Double click on the statement column in the last row of the table to see the last query the application sent to the database. Note the query statement has not changed, but SQL Server now receives encrypted query parameters.
125+
![XEventSessionColumnsAfterEncryption](img/xevent-after-encryption.png)
126+
+ Click **OK** to close the statement window.
127+
128+
## Demo Part 3 - run rich queries from SSMS
129+
1. Enable Parameterization for Always Encrypted the SSMS instance that uses a database connection with Always Encrypted enabled.
130+
+ In the main menu, select **Tools** and then select **Options**
131+
+ In the left pane of the **Options** window, navigate to **Query Execution/SQL Server/Advanced**. Scroll down in the right pane and make sure **Enabled Parameterization for Always Encrypted** is enabled.
132+
![NewCMK](img/parameterization.png)
133+
+ Click **OK**.
134+
1. Open, review (change, if you want) and execute the query in **tsql-scripts/QueryColumns.sql**. The query should return the rows in the **Patients** table meeting the specified search criteria.
135+
136+
## Demo Part 4 - Re-encrypt and decrypt columns in-place
137+
1. Re-encrypt your columns to rotate/change the column encryption key.
138+
+ In the SSMS instance that uses a database connection with Always Encrypted enabled, use Object Explorer to expand your database and navigate to **Security/ Always Encrypted Keys/Column Encryption Keys**.
139+
+ Right-click on the **Column Encryption Keys** folder and select **New Column Encryption Key…**.
140+
+ Enter a column encryption key name: **CEK2**.
141+
+ Select **CMK1** as the column master key to protect your new column encryption key.
142+
+ Click **OK** to provision a new column encryption key.
143+
+ Open and review **tsql-scripts/RotateKeys.sql**. Note that the only difference between this script and **tsql-scripts/EncryptColumns.sql** is the column encryption key specified for the three columns (this script uses **CEK2**). When you execute the script, the columns are re-encrypted using the new column encryption key.
144+
+ Refresh the demo app in the browser. The app should continue to work.
145+
1. Decrypt your columns (convert them back to plaintext).
146+
+ Open, review and execute **tsql-scripts/DecryptColumns.sql**. This script decrypts the encrypted columns. It also re-sets the original collation of the columns.
147+
148+
## Resetting your demo environment
149+
1. Open and execute **tsql-scripts/DecryptColumns.sql**.
150+
1. Open and execute **tsql-scripts/DropKeys.sql**.
151+
1. Close both SSMS windows.
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading

0 commit comments

Comments
 (0)