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
+21-15Lines changed: 21 additions & 15 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,5 @@
1
1
---
2
-
title: How to use a system-assigned managed identity to access Azure Cosmos DB data.
2
+
title: How to use a system-assigned managed identity to access Azure Cosmos DB data
3
3
description: Learn how to configure an Azure AD system-assigned managed identity to access keys from Azure Cosmos DB. msi, managed service identity, aad, azure active directory, identity
4
4
author: j-patrick
5
5
ms.service: cosmos-db
@@ -10,7 +10,7 @@ ms.reviewer: sngun
10
10
11
11
---
12
12
13
-
# How to use a system-assigned managed identity to access Azure Cosmos DB data.
13
+
# How to use a system-assigned managed identity to access Azure Cosmos DB data
14
14
15
15
In this article you will set up a **robust, key rotation agnostic,** solution to access Azure Cosmos DB keys by leveraging [managed identities](../active-directory/managed-identities-azure-resources/services-support-managed-identities.md). The example in this article uses an Azure Function. However, you can achieve this solution by using any service that supports managed identities.
16
16
@@ -25,16 +25,18 @@ In this step, you'll assign a system-assigned managed identity to your Azure Fun
25
25
1. In the [Azure portal](https://portal.azure.com/), open the **Azure Function** pane and navigate to your function app.
26
26
27
27
1. Open the **Platform features** > **Identity** tab:
1. On the **Identity** tab, turn **On** the **System Identity** status. Be sure to select **Save**, and confirm that you want to turn on the system identity. At the end the **System Identity** pane should look as follows:
## Grant the managed identity access to your Azure Cosmos DB account
34
36
35
37
In this step, you'll assign a role to the Azure Function's system-assigned managed identity. Azure Cosmos DB has multiple built-in roles that you can assign to the managed identity. For this solution, you will use the following two roles:
36
38
37
-
|**Built-in role**|**Description**|
39
+
|Built-in role |Description |
38
40
|---------|---------|
39
41
|[DocumentDB Account Contributor](../role-based-access-control/built-in-roles.md#documentdb-account-contributor)|Can manage Azure Cosmos DB accounts. Allows retrieval of read/write keys. |
40
42
|[Cosmos DB Account Reader](../role-based-access-control/built-in-roles.md#cosmos-db-account-reader-role)|Can read Azure Cosmos DB account data. Allows retrieval of read keys. |
@@ -47,33 +49,35 @@ In this step, you'll assign a role to the Azure Function's system-assigned manag
47
49
48
50
For your scenario, you will read the temperature, then write back that data to a container in Azure Cosmos DB. Because you have to write the data, you will use the **DocumentDB Account Contributor** role.
49
51
50
-
1. Sign in to the Azure portal and navigate to your Azure Cosmos account. Open the **Access Management (IAM) Pane**, and then the **Role Assignments** tab:
1. Sign in to the Azure portal and navigate to your Azure Cosmos DB account. Open the **Access Management (IAM) Pane**, and then the **Role Assignments** tab:
***Assign access to** - Under the Select **System-assigned managed identity** subsection, select **Function App**.
61
-
***Select** - The pane will be populated with all the function apps, in your subscription, that have a **Managed System Identity**. In our case I select the **SummaryService** function app:
***Assign access to** - Under the Select **System-assigned managed identity** subsection, select **Function App**.
64
+
***Select** - The pane will be populated with all the function apps, in your subscription, that have a **Managed System Identity**. In our case I select the **SummaryService** function app:
1. After the function app's identity is selected click **Save**.
66
69
67
70
## Programmatically access the Azure Cosmos DB keys from the Azure Function
68
71
69
72
Now we have a function app that has a system-assigned managed identity. That identity is given the **DocumentDB Account Contributor** role in the Azure Cosmos DB permissions. The following function app code will get the Azure Cosmos DB keys, create a CosmosClient object, get the temperature, then save this to Cosmos DB.
70
73
71
-
This sample uses the [List Keys API](https://docs.microsoft.com/rest/api/cosmos-db-resource-provider/DatabaseAccounts/ListKeys) to access your Azure Cosmos account keys.
74
+
This sample uses the [List Keys API](https://docs.microsoft.com/rest/api/cosmos-db-resource-provider/DatabaseAccounts/ListKeys) to access your Azure Cosmos DB account keys.
72
75
73
76
> [!IMPORTANT]
74
77
> If you want to [assign the **Cosmos DB Account Reader**](#grant-the-managed-identity-access-to-your-azure-cosmos-account) role, you will need to use the read only [List Keys api](https://docs.microsoft.com/rest/api/cosmos-db-resource-provider/DatabaseAccounts/ListReadOnlyKeys). This will only populate the read only keys.
75
78
76
79
The List Keys API returns the `DatabaseAccountListKeysResult` object. This type isn't defined in the C# libraries. The following code shows the implementation of this class:
80
+
77
81
```csharp
78
82
namespaceSummarizationService
79
83
{
@@ -88,6 +92,7 @@ namespace SummarizationService
88
92
```
89
93
90
94
The example also uses a simple document called "TemperatureRecord", which is defined as follows:
95
+
91
96
```csharp
92
97
usingSystem;
93
98
@@ -185,7 +190,8 @@ namespace Monitor
185
190
}
186
191
}
187
192
```
188
-
You are now ready to [deploy your Azure Function.](../azure-functions/functions-create-first-function-vs-code.md)
193
+
194
+
You are now ready to [deploy your Azure Function](../azure-functions/functions-create-first-function-vs-code.md).
0 commit comments