You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: articles/cosmos-db/managed-identity-based-authentication.md
+17-19Lines changed: 17 additions & 19 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
---
2
-
title: Managed Identity based authentication with Azure Cosmos DB and Azure Active Directory
3
-
description: Learn how to configure an Azure AD identity for msi-based authentication to access keys from Azure Cosmos DB.
2
+
title: Managed Identity based authentication and authorization with Azure Cosmos DB and Azure Active Directory
3
+
description: Learn how to configure an Azure AD identity to access keys from Azure Cosmos DB.
4
4
author: justipat
5
5
ms.service: cosmos-db
6
6
ms.topic: conceptual
@@ -12,33 +12,31 @@ ms.reviewer: sngun
12
12
13
13
# Managed Identity based authentication and authorization with Azure Cosmos DB and Azure Active Directory
14
14
15
-
Managed Identity authentication enables your client application to be authenticated by using Azure Active Directory (Azure AD). You can perform Managed Identity based authentication / authorization on any service that [supports system assigned identities](/articles/active-directory/managed-identities-azure-resources/services-support-managed-identities.md). The end result will be a service that can read Azure Cosmos DB keys without having the keys directly in the service. The solution outlined in this article also creates a **robust key rotation agnostic** solution for cosmos key management.
15
+
In this article we'll setup a **robust, key rotation agnostic,** solution for Cosmos DB key management by leveraging [Managed Service Identities](/articles/active-directory/managed-identities-azure-resources/services-support-managed-identities.md). Though we're using an Azure Function for this example, this solution can be used with any service that [supports managed service identities](/articles/active-directory/managed-identities-azure-resources/services-support-managed-identities.md).
16
16
17
-
You will learn how to:
17
+
You'll learn how to:
18
18
19
19
* Assign a System Identity
20
20
* Grant the System Identity access to your Cosmos DB
21
21
* Write the code for robust Cosmos DB key management
22
22
23
-
In the solution below we will be building an Azure Function that wakes up every hour to read a set of sale receipts from Cosmos DB and create an hourly summary of sales, storing it back into Cosmos DB.
23
+
In the solution below, we'll be building an Azure Function that will handle summarizing the last hour of sales. The function will wake up every hour, and will read a set of sale receipts from Cosmos DB. Then the function will create an hourly summary of sales and store it back in the container. To simplify the scenario, cleanup of the already processed receipts will be handled by an already configured [TTL](./time-to-live.md) setting.
24
24
25
-
Though we are using Azure Functions below this will work with all Azure Services that support [system managed identity](/articles/active-directory/managed-identities-azure-resources/services-support-managed-identities.md)
25
+
## Assign a System Identity to an Azure Function
26
26
27
-
## Assign a System Identity to the your Azure Function
28
-
29
-
In this step, you will assign a managed system identity to your Azure Function.
27
+
In this step, you'll assign a managed system identity to your Azure Function.
30
28
31
29
1. Sign into the [Azure portal](https://portal.azure.com/)
32
30
33
31
1. Open the Azure Function pane, and for your function app select the **Identity tab**:
1. On the **Identity tab** switch **System Identity** to the "On" position. Be sure to click **Save**. You will be prompted to confirm you want to turn this on. Please accept. In the end the **System Identity** pane should look like this:
34
+
1. On the **Identity tab** switch **System Identity** to the "On" position. Be sure to click **Save**, and confirm you want to turn on System Identity. In the end the **System Identity** pane should look like this:
37
35

38
36
39
37
## Grant the System Identity Access to your Cosmos DB.
40
38
41
-
In this step, you will assign a role to the Azure Function's System Identity. Cosmos DB has multiple builtin roles you can assign the System Identity too. For this exercise we will just focus on two:
39
+
In this step, you'll assign a role to the Azure Function's System Identity. Cosmos DB has multiple built-in roles you can assign the System Identity too. For this exercise we'll just focus on two:
42
40
43
41
|**Built-in role**|**Description**|
44
42
|---------|---------|
@@ -49,11 +47,11 @@ In this step, you will assign a role to the Azure Function's System Identity. Co
49
47
> RBAC support in Azure Cosmos DB applies to control plane operations only. Data plane operations are secured using master keys or resource tokens. To learn more, see [Secure access to data in Azure Cosmos DB](secure-access-to-data.md)
50
48
51
49
> [!TIP]
52
-
> When assigning roles, only assign the needed access. So if your service only need to read, then only assign the Service Identity to **Cosmos DB Account Reader**. For more information on the importance of **least privilege access** please see [lower exposure of privileged accounts](/azure/security/fundamentals/identity-management-best-practices#lower-exposure-of-privileged-accounts).
50
+
> When assigning roles, only assign the needed access. So if your service only need to read, then only assign the Service Identity to **Cosmos DB Account Reader**. For more information about the importance of **least privilege access,** see [lower exposure of privileged accounts](/azure/security/fundamentals/identity-management-best-practices#lower-exposure-of-privileged-accounts).
53
51
54
-
For our scenario we need to be able to read the sale receipt documents summarize them and then write back that summary to Cosmos DB. We will use the **DocumentDB Account Contributor** role.
52
+
For our scenario, we'll read the sale receipt documents, summarize them, and then write back that summary to Cosmos DB. Since we need write access, we'll use the **DocumentDB Account Contributor** role.
55
53
56
-
1. Open your Cosmos DB in the portal, select the **Access Management (IAM) Pane**, and the the **Role Assignments** tab:
54
+
1. Open your Cosmos DB in the portal, select the **Access Management (IAM) Pane**, and then the **Role Assignments** tab:
1. Select the **+ Add** button, then **add role assignment**:
@@ -71,12 +69,12 @@ For our scenario we need to be able to read the sale receipt documents summarize
71
69
72
70
## Programmatically access the Cosmos DB keys from the Azure Function
73
71
74
-
Now we have a function app that has a system managed identity. That identity is given the **DocumentDB Account Contributor** role in the Cosmos DB permissions. The code below will run on the **Function App**and will get the keys and create a CosmosClient. Then perform the the summarization business logic.
72
+
Now we have a function app that has a system managed identity. That identity is given the **DocumentDB Account Contributor** role in the Cosmos DB permissions. The **Function App**code below will get the needed Cosmos DB Keys, create a CosmosClient, and run the summarization business logic.
75
73
76
-
The api we will be using to get the Cosmos DB Keys is the [List Keys API](https://docs.microsoft.com/en-us/rest/api/cosmos-db-resource-provider/DatabaseAccounts/ListKeys).
74
+
We'll be using to get the Cosmos DB Keys is the [List Keys API](https://docs.microsoft.com/en-us/rest/api/cosmos-db-resource-provider/DatabaseAccounts/ListKeys).
77
75
78
76
79
-
The api returns DatabaseAccountListKeysResult. This type is not defined in the C# libraries, but it is easy to implement. The follow is an implementation for this class. Add it to the solution:
77
+
The api returns DatabaseAccountListKeysResult. This type isn't defined in the C# libraries. The code below is an implementation for this class. Add it to the solution:
80
78
```csharp
81
79
namespaceSummarizationService
82
80
{
@@ -90,7 +88,7 @@ namespace SummarizationService
90
88
}
91
89
```
92
90
93
-
The library we will use to get our Service Managed token is [Microsoft.Azure.Services.AppAuthentication](https://www.nuget.org/packages/Microsoft.Azure.Services.AppAuthentication). You can find other ways to get the token and more information about the Microsoft.Azure.Service.AppAuthentication library by reading up on [Service To Service Authentication](https://docs.microsoft.com/en-us/azure/key-vault/service-to-service-authentication).
91
+
The library we'll use to get our Service Managed token is [Microsoft.Azure.Services.AppAuthentication](https://www.nuget.org/packages/Microsoft.Azure.Services.AppAuthentication). You can find other ways to get the token and more information about the Microsoft.Azure.Service.AppAuthentication library by reading up on [Service To Service Authentication](https://docs.microsoft.com/en-us/azure/key-vault/service-to-service-authentication).
You are now ready to [deploy your Azure Function.](https://docs.microsoft.com/en-us/azure/azure-functions/functions-create-first-function-vs-code?pivots=programming-language-csharp#publish-the-project-to-azure)
189
187
190
188
> [!IMPORTANT]
191
-
> If you want to [assign the **Cosmos DB Account Reader**](#Grant-the-System-Identity-Access-to-your-Cosmos-DB.), you will need to use the read only [List Keys api](https://docs.microsoft.com/en-us/rest/api/cosmos-db-resource-provider/DatabaseAccounts/ListReadOnlyKeys). This would only populate the read only keys on the DatabaseAccountListKeysResult class.
189
+
> If you want to [assign the **Cosmos DB Account Reader**](#Grant-the-System-Identity-Access-to-your-Cosmos-DB.) role, you will need to use the read only [List Keys api](https://docs.microsoft.com/en-us/rest/api/cosmos-db-resource-provider/DatabaseAccounts/ListReadOnlyKeys). This would only populate the read only keys on the DatabaseAccountListKeysResult class.
0 commit comments