A tour of Always Encrypted for Azure Cosmos DB

Santosh

Always encrypted for Azure Cosmos DB provides encryption support on the client-side to protect sensitive data by encrypting data such as credit card numbers or national identification numbers, before it gets transferred over the wire to be stored in Azure Cosmos DB. Always encrypted allows clients to protect JSON property values which are deemed sensitive, by means of client encryption policies which can be configured on the containers just like any other policy such as indexing policy etc.

How to start using always encrypted

Let us try to run the sample code. Encrypting data using always encrypted requires an initial setup as always encrypted relies on Azure Key Vault (when the KeyStoreProvider selected is Azure Key Vault – I will get to that in a bit) to store wrapped data encryption keys which are encrypted using a customer-provided key encryption key created (and stored) in Azure Key Vault. In this case, Azure Key Vault provides all the required services to wrap (Encrypt) and unwrap (Decrypt) the data encryption key in use.

Step 1: Enable client application access

When you have an application that connects to your Azure Cosmos DB instance, you can create an identity for the app. This identity is known as a service principal. Access to resources is restricted by the roles assigned to the service principal, giving you control over which resources can be accessed and at which level.

Let us look at how to use the portal to create the service principal in the Azure portal. Let us focus on a single-tenant application where the application is intended to run within only one organization.

  1. Sign into your Azure Account through the Azure portal.
  2. Select Azure Active Directory/App registrations.
  3. Select New registration.
  4. Name the application. Select a supported account type, which determines who can use the application.

There you go, now you have your app registered.

Graphical user interface, text, application Description automatically generated

Step 2: Get tenant and app ID values for signing in

You would need these two IDs to create an instance of the ClientCertificateCredential class (which is a token credential that can provide an OAuth Token) with the details needed to authenticate against Azure Active Directory with a certificate, when using certificate-based authentication. This is how Azure Key Vault would allow the set of operations like wrapping and unwrapping of data encryption keys. To get those values, use the following steps:

  1. Select Azure Active Directory/App registrations in Azure AD, select your application.
  2. Copy the Directory (tenant) ID and store it in your application app settings.

Graphical user interface, text, application, email Description automatically generated

Graphical user interface, text, application, email Description automatically generated

Step 3: Authentication: Two options

Let’s look at couple of authentication methods available for service principals: password-based authentication (application secret) and certificate-based authentication. We recommend using a certificate so this is shown as part of the sample code, but you can also create an application secret to get a token via ClientCredential

Option 1: Upload a certificate.

You can use an existing certificate if you have one. Optionally, you can create a self-signed certificate for testing purposes only. To generate a certificate, you can use Azure Key Vault.

To generate the certificate using Azure Key Vault:
  1. On the Key Vault properties pages, select Certificates.
  2. Click on Generate/Import.
  3. On the Create a certificate screen choose the following values:
  • Method of Certificate Creation: Generate.
  • Certificate Name: ExampleCertificate.
  • Subject: CN=ExampleDomain
  • Leave the other values to their defaults.

Click Create. This may take some time to create and enable. Once that is done and you have received the message that the certificate has been successfully created, you may click on it on the list. You can then see some of the properties.

Graphical user interface, text, application, email Description automatically generated

Export/Download certificate from Azure Key Vault:

By clicking “Download in CER format” or “Download in PFX/PEM format” button, you can download the certificate. Once you have downloaded it, you need to install it under the current user on the client machine which will run always encrypted Azure Cosmos DB client. (The steps to create an Azure Key Vault are provided in the later section).

Graphical user interface, text Description automatically generated

To upload the certificate:
  1. Select Azure Active Directory/App registrations in Azure AD, select your application.
  2. Select Certificates & secrets.
  3. Select Upload certificate and select the certificate.

Graphical user interface, application Description automatically generated

Option 2: Create a new application secret.

If you choose not to use a certificate, you can create a new application secret.

  1. Select Azure Active Directory.
  2. From App registrations in Azure AD, select your application.
  3. Select Certificates & secrets.
  4. Select Client secrets -> New client secret.
  5. Provide a description of the secret, and a duration. When done, select Add.

After saving the client secret, the value of the client secret is displayed.

Graphical user interface, text, application Description automatically generated

Step 5: Create an Azure Key Vault using the Azure portal

Sign into the Azure portal at https://portal.azure.com.

Create a key vault:
  1. From the Azure portal menu, or from the Home page, select Create a resource.
  2. In the Search box, enter Key Vault.
  3. From the results list, choose Key Vault.
  4. On the Key Vault section, choose Create.
  5. On the Create key vault section provide the following information:
  6. Name: A unique name is required let us use example-vault.
  7. Subscription: Choose a subscription.
  8. Under Resource Group, choose Create new and enter a resource group name.
  9. In the Region pull-down menu, choose a region.
  10. Leave the other options to their defaults.
  11. After providing the information above, select Create.

Graphical user interface, application Description automatically generated

Step 6: Configure access policies on resources

Now that your client app is configured and you have your application ID, it is time to configure its access policy so you and your application can access the vault’s secrets (key encryption keys). The getlistsignverifywrapKey, and unwrapKey permissions are required for creating a new data encryption key which is used by Always encrypted Azure Cosmos DB to encrypt the data.

  1. In the Azure portal, navigate to your key vault and select Access policies.
  2. Select Add access policy, then select the key, secret, and certificate permissions you want to grant your application. Select the service principal you created previously.
  3. Select Add to add the access policy, then Save to commit your changes.

Graphical user interface, text, application, email Description automatically generated

Step 7: Add a key to the Key Vault

To add a key to the vault, you just need to take a couple of additional steps. In this case, we add a key that will be used by Always Encrypted for Azure Cosmos DB. The key is called ExampleKey.

  1. On the Key Vault properties pages, select Keys.
  2. Click on Generate/Import.
  3. On the Create a key screen choose the following values:
    • Options: Generate.
    • Name: ExampleKey.
    • Leave the other values to their defaults. Click Create.

Once that you have received the message that the key has been successfully created, you may click on it on the list.

Graphical user interface, text, application Description automatically generated

Make sure you note the Key Identifier which will be later used in the sample code, where we would use it as part of EncryptionWrapMetaData while creating a new data encryption key.

There you go, you have registered an application and provided the desired permission for the application in the Key Vault.

How to build applications using Always Encrypted for Azure Cosmos DB

With Always Encrypted, the Azure Cosmos DB SDK has now exposed a bunch of new APIs that can help configure few things like the container-level encryption policies and corresponding data encryption keys. This is required before you start using your regular Azure Cosmos DB SDK API like CreateItemAsync(), ReadItemAsync() etc. which have remained unchanged with respect to function signature.

To begin with, you must initialize your KeyStoreProvider which has been abstracted out so that you can implement your own KeyStoreProvider if necessary. However, we recommend using the AzureKeyVaultKeyStoreProvider which comes implemented out of the box and relies on Azure Key Vault as a key store to store your key encryption key (which is used to encrypt your data encryption keys) and provide services like wrapping and unwrapping.

TokenCredential to access Azure Key Vault services

The TokenCredential class is the representation of a credential which can provide OAuth token used to prove an identity between Azure Key Vault and a registered application via Azure Active Directory.

So, every call we make to Azure Key Vault to wrap or unwrap a data encryption key is through a token obtained by authenticating with Azure AD (Active Directory). This token is in turn used by Azure Key Vault to validate the application identified with Azure AD.

So, the setup with respect to registering your application with Azure AD and Azure KeyVault were carried out to enable this support and thus obtain the TokenCredential to use Key Vault services.

Enable Container with Encryption Capabilities

The WithEncryption() extension on CosmosClient enables encryption on containers created using this client.

AzureKeyVaultKeyStoreProvider, which is passed as a parameter, internally provides all the functionality around managing securely your data encryption keys.

DataEncryptionKeyCacheTimeToLive property allows a user to set the TimeToLive value that determines how long the unwrapped data encryption keys are cached. After expiry, we refresh the cache by making a call to Azure Key Vault to unwrap the wrapped data encryption keys that are stored in Azure Cosmos DB. By default, DataEncryptionKeyCacheTimeToLive is set to 2 hours.

To configure a ClientEncryptionPolicy, you need to first create a data encryption key which you will use to encrypt your document properties.

The EncryptionKeyWrapMetadata requires the key encryption key URL, which was earlier noted during the setup. This key is used to encrypt/wrap the data encryption key created via this call and stored securely in Azure Cosmos DB.

A client Encryption Policy is configured by providing a ClientEncryptionIncludedPath, which specifies:

  • the property you want to encrypt.
  • the data encryption key you want the policy to use for this property.
  • the encryption type which is either Deterministic or Randomized.
  • the encryption algorithm to use.

If you are looking to run queries on encrypted properties, then Deterministic encryption type must be selected. The keys must be created first, before you create a ClientEncryptionPolicy which uses the keys.

Currently, only top-level paths are supported and if the top-level path is an Object or say an Array type, then all the corresponding nested/child properties are encrypted with the same Client Encryption Policy. You now have a container configured with an encryption policy and all documents created inside this container will have the corresponding properties specified in the policy, encrypted.

Key Encryption Key (Master Key) Rotation via RewrapClientEncryptionKeyAync

The RewrapClientEncryptionKeyAync(…) can be used to rewrap an existing data encryption key, which is useful when you want to rotate the Key Encryption Key. If you want to revoke the existing Key Encryption Key, this must be done after the rewrap operation has completed. For example, one way to do it would be to use the QueryIterator available over ClientEncryptionKey and run a query to fetch all the keys with their Azure Key Vault key encryption key ”value” field. Or if you have configured a unique name, you can use the “name” field to fetch all the keys which were encrypted using this key encryption key and rewrap them with a new key.

Running queries on Encrypted Documents

Running queries on encrypted documents requires the use of parameterized query using an AddParameterAsync(…) extension provided with QueryDefinition(…).

Since the containers configured with an encryption Policy have properties that are encrypted, you cannot run queries with a WHERE clause without the use of AddParameterAsync(…) extension since that would result in sending plaintext data and failing, since the property might be encrypted.

The third argument passed above in AddParameterAsync(…) should be the property path that was used in the client encryption policy to encrypt the property.

Are there any known limitations?

  • Client encryption policies are immutable, once a container is configured with a client encryption policy, we do not allow changing the client encryption policy.

Next steps

Try client-side encryption for Azure Cosmos DB by following this sample.

Use Azure Cosmos DB emulator version 2.11.13.0 or higher to use the preview of Always Encrypted, or request the preview to be enabled on your account here.

 

0 comments

Leave a comment