LAB 02 Key Vault
LAB 02 Key Vault
Always Encrypted)
Scenario
This module includes the following tasks:
• Azure confidential computing
• Azure Azure Key Vault
Note: You do not need to wait for the SQL Management Studio to install before
continuing.
2. Use the following command to authenticate to Azure using the account for your Azure
subscription.
Login-AzAccount
4. Create a key vault in the resource group. The VaultName must be unique therefore
change to something unique.
6. Click on the Key Vault name to examine what you have created.
Note: For all future instructions replace KeyVaultPS with the name of your Key Vault.
8. Select Key, Secret and Certificate Management from Configure from template
(optional)
9. Click Select Principal and search for and then click on your account, then click on
Select
2. Add a software-protected key to the Key Vault using this command. Be sure to change
the placeholder text to your vault name.
3. Move back to KeyVaultPS in the Azure portal. Click Keys under Settings in the left
navigation pane.
4. Click MyLabKey
Note: You can always reference this key by using its URI. To get the most current
version, just reference https://keyvaultps.vault.azure.net/keys/MyLabKey/
or if need be the exact version:
https://keyvaultps.vault.azure.net/keys/MyLabKey/da1a3a1efa5dxxxxxx
xxxxxxxd53c5959e
7. Move back to the PowerShell window. To display the current version of the key, enter
the following command.
$Key.key.kid
8. To view the Key you just created you can use the Get-AzureKeyVaultKey cmdlet. Be
sure to change the placeholder text to your vault name.
Get-AZKeyVaultKey -VaultName '<YourVaultName>'
2. Next add the secret to the Vault with this command. Be sure to change the placeholder
text to your vault name.
Note: You can always reference this key by using its URI. To get the most current
version just reference
https://keyvaultps.vault.azure.net/secrets/SQLPassword or if need be the
exact version:
https://keyvaultps.vault.azure.net/secrets/SQLPassword/c5aada85d3ac
xxxxxxxxxxe8701efafcf3
7. Click the Show secret value button – notice that the password appears.
8. To view the Secret, use the Get-AzureKeyVaultSecret cmdlet. Be sure to change the
placeholder text to your vault name.
5. Click Register.
6. Once the App Registration is complete click on sqlApp if it does not automatically
appear.
10. In the Description section, enter Key1 for the description. Select 1 year from the
Expires list, then click Add
11. Copy the Key1 value as you will need it later. If you close and reopen the blade, the
value will show as hidden.
Task 6: Add a Key Vault Policy allowing the application access to the Key Vault.
1. In the Azure portal open your Resource Group created at the beginning of the lab
5. In the Key Permissions drop down select Select All to highlight all permissions
6. Select Save
Important! You must click save otherwise the permissions will not be committed
7. Run the following Powershell in the Powershell ISE to set the sqlApp key permissions
replacing the placeholder text with your account details
$subscriptionName = '[Azure_Subscription_Name]'
$applicationId = '[Azure_AD_Application_ID]'
$resourceGroupName = '[Resource_Group_with_KeyVault]'
$location = '[Azure_Region_of_KeyVault]'
$vaultName = '[KeyVault_Name]'
Login-AzAccount
2. Provide the following details on the SQL Database blade and click Create.
• Password: Pa55w.rd1234
• Then click OK
4. Once the SQL Database is deployed, open it in the Azure Portal to locate and then copy
the ADO.NET Connection String.
Note: When you save the connection string for future use, be sure to replace
{your_username} with demouser and {your_password} with Pa55w.rd1234.
4. Open SQL Server Management Studio. Connect to the Server using these properties for
the Connect to Server dialog.
– Server Type: Database Engine
– Login: demouser
– Password: Pa55w.rd1234
2. Paste the following code into the query window and click Execute
3. After the table is created successfully, expand medical > tables > right-click
dbo.Patients and select Encrypt Columns.
4. Click Next.
5. On the Column Selection Screen check SSN and Birthdate. Then set the Encryption
Type for SSN to Deterministic and for Birthdate Randomized. Click Next.
6. On the Master Key Configuration page on the Select the Key store provider, click Azure
Key Vault. Click Sign in and authenticate. Select your Azure Key Vault. Click Next.
7. On the Run Settings screen click Next and then Finish to Proceed with the encrypting.
8. When the encryption process is complete, click Close and expand medical > security
> Always Encrypted Keys and note that now there are keys found.
3. Next select C# > Console App (.NET Framework) and provide the
name OpsEncrypt in the location C:\ and then click Create.
5. Change the Target Framework to .NET Framework 4.7.2. Click Yes when prompted
to change the Target Framework.
6. Install the following NuGet packages by going to Tools > NuGet Package Manager >
Package Manager Console.
Install-Package
Microsoft.SqlServer.Management.AlwaysEncrypted.AzureKeyVaultProvide
r
Install-Package Microsoft.IdentityModel.Clients.ActiveDirectory
7. Open the program.cs file in notepad from Allfiles\Labs\Mod1_Lab02 and copy the
code.
8. Replace the code in Program.cs in Visual Studio with the code you just copied.
9. Locate the Connection string, clientId, and clientSecret settings in the Main method
and replace them with the values that you copied from the previous steps.
11. The Console Application will Build and then start. First it will ask for your password,
then the app will add data to the database.
13. Run the following query to see the data that was loaded into the database is encrypted.
14. Now, move back to the console application where you will be asked to Enter a Valid
SSN. This will query the encrypted column for the data. Notice that with the key called
from the Key Vault, now the data is unencrypted and shown to the console window.
999-99-0003
WARNING: Prior to continuing you should remove all resources used for this lab. To do
this in the Azure Portal click Resource groups. Select any resources groups you have
created. On the resource group blade click Delete Resource group, enter the Resource
Group Name and click Delete. Repeat the process for any additional Resource Groups
you may have created. Failure to do this may cause issues with other labs.