Skip to content

Commit 86f7bfa

Browse files
author
Arvind Krishnaa Jagannathan
committed
Update the cosmosdb auth document
1 parent 6df060c commit 86f7bfa

File tree

1 file changed

+64
-32
lines changed

1 file changed

+64
-32
lines changed

articles/search/search-howto-managed-identities-cosmos-db.md

Lines changed: 64 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
title: Set up an indexer connection to Azure Cosmos DB using a managed identity
33
titleSuffix: Azure AI Search
44
description: Learn how to set up an indexer connection to an Azure Cosmos DB account using a managed identity.
5-
author: gmndrg
6-
ms.author: gimondra
5+
author: arv100kri
6+
ms.author: arjagann
77

88
ms.service: azure-ai-search
99
ms.topic: how-to
10-
ms.date: 06/10/2024
10+
ms.date: 12/23/2024
1111
ms.custom:
1212
- subject-rbac-steps
1313
- ignite-2023
@@ -19,17 +19,27 @@ This article explains how to set up an indexer connection to an Azure Cosmos DB
1919

2020
You can use a system-assigned managed identity or a user-assigned managed identity. Managed identities are Microsoft Entra logins and require Azure role assignments to access data in Azure Cosmos DB.
2121

22-
## Prerequisites
22+
## Background
2323

24-
* [Create a managed identity](search-howto-managed-identities-data-sources.md) for your search service.
24+
Azure AI Search supports two mechanisms to connect using managed identity.
25+
26+
1. The _legacy_ aproach requires configuring the managed identity to have reader permissions on the management plane of the target Azure Cosmos DB account. Azure AI Search will then utilize that identity to fetch the account keys of Cosmos DB account in the background to access the data. This approach will not work if the Cosmos DB account has `"disableLocalAuth": true`. This approach is no longer recommended when connecting to Azure Cosmos DB accounts for NoSQL accounts.
27+
28+
1. The _recommended_ approach requires configuring the managed identity appropriatte roles on the management and data plane of the target Azure Cosmos DB account. Azure AI Search will then request an access token to access the data in the Cosmos DB account. This approach will work if the Cosmos DB account has `"disableLocalAuth": true`, and is therefore recommended as the more secure option when connecting to Azure Cosmos DB accounts for NoSQL accounts.
2529

26-
* Azure Cosmos DB for NoSQL. You can optionally [enforce role-based access as the only authentication method](/azure/cosmos-db/how-to-setup-rbac#disable-local-auth) for data connections by setting `disableLocalAuth` to `true` for your Cosmos DB account.
30+
The rest of this document will walk through the steps for the _recommended_ approach, with callouts as needed comparing it with the _legacy_ approach.
2731

28-
## Limitations
32+
### Limitations
2933

30-
Indexer support for Azure Cosmos DB for Gremlin and MongoDB Collections is currently in preview. At this time, a preview limitation requires Azure AI Search to connect using keys. You can still set up a managed identity and role assignment, but Azure AI Search will only use the role assignment to get keys for the connection. This limitation means that you can't configure a [role-based approach](/azure/cosmos-db/how-to-setup-rbac#disable-local-auth) if your indexers are connecting to Gremlin or MongoDB.
34+
Indexer support for Azure Cosmos DB for Gremlin and MongoDB Collections is currently in preview and only supports the _legacy_ approach. The _recommended_ approach only works with Azure Cosmos DB for NoSQL.
35+
36+
## Prerequisites
37+
38+
* [Create a managed identity](search-howto-managed-identities-data-sources.md) for your search service.
3139

32-
## Create a role assignment in Azure Cosmos DB
40+
* You can optionally [enforce role-based access as the only authentication method](/azure/cosmos-db/how-to-setup-rbac#disable-local-auth) for data connections by setting `disableLocalAuth` to `true` for your Azure Cosmos DB for NoSQL account.
41+
42+
## Step 1: Configure role assignments on Azure Cosmos DB control plane
3343

3444
### [**Azure portal**](#tab/portal)
3545

@@ -49,32 +59,36 @@ Indexer support for Azure Cosmos DB for Gremlin and MongoDB Collections is curre
4959

5060
1. Select the identity and save the role assignment.
5161

52-
For more information, see [Configure role-based access control with Microsoft Entra ID for your Azure Cosmos DB account](/azure/cosmos-db/how-to-setup-rbac).
62+
For more information, see [Use control plane role-based access control with Azure Cosmos DB for NoSQL](/azure/cosmos-db/nosql/security/how-to-grant-control-plane-role-based-access).
63+
64+
## Step 2: Configure role assignments on Azure Cosmos DB data plane
5365

54-
### [**PowerShell**](#tab/powershell)
66+
The managed identity needs to assigned a role to read from the Cosmos DB account's data plane. This step can only be performed via Azure CLI at the moment. The Object (principal) ID for the search service's system/user assigned identity can be found from the search service's "Identity" tab.
67+
68+
### [**Azure CLI**](#tab/azcli)
5569

5670
Set variables:
5771

58-
```azurepowershell
72+
```azurecli
5973
$cosmosdb_acc_name = <cosmos db account name>
6074
$resource_group = <resource group name>
6175
$subsciption = <subscription ID>
62-
$system_assigned_principal = <principal ID for the search service's system assigned identity>
76+
$system_assigned_principal = <Object (principal) ID for the search service's system/user assigned identity>
6377
$readOnlyRoleDefinitionId = "00000000-0000-0000-0000-00000000000"
6478
$scope=$(az cosmosdb show --name $cosmosdbname --resource-group $resourcegroup --query id --output tsv)
6579
```
6680

6781
Define a role assignment for the system-assigned identity:
6882

69-
```azurepowershell
83+
```azurecli
7084
az cosmosdb sql role assignment create --account-name $cosmosdbname --resource-group $resourcegroup --role-definition-id $readOnlyRoleDefinitionId --principal-id $sys_principal --scope $scope
7185
```
7286

73-
---
87+
For more information, see [Use data plane role-based access control with Azure Cosmos DB for NoSQL](/azure/cosmos-db/nosql/security/how-to-grant-data-plane-role-based-access)
7488

75-
## Specify a managed identity in a connection string
89+
## Step 3: Configure Azure AI Search data source with managed identity connection string
7690

77-
Once you have a role assignment, you can set up a connection to Azure Cosmos DB for NoSQL that operates under that role.
91+
Once you have configured **both** control plane and data plane role assignments on the Azure Cosmos DB for NoSQL account, you can set up a connection to it that operates under that role.
7892

7993
Indexers use a data source object for connections to an external data source. This section explains how to specify a system-assigned managed identity or a user-assigned managed identity on a data source connection string. You can find more [connection string examples](search-howto-managed-identities-data-sources.md#connection-string-examples) in the managed identity article.
8094

@@ -88,19 +102,22 @@ The [REST API](/rest/api/searchservice/data-sources/create), Azure portal, and t
88102
When you're connecting with a system-assigned managed identity, the only change to the data source definition is the format of the "credentials" property. Provide a database name and a ResourceId that has no account key or password. The ResourceId must include the subscription ID of Azure Cosmos DB, the resource group, and the Azure Cosmos DB account name.
89103

90104
* For SQL collections, the connection string doesn't require "ApiKind".
91-
* For SQL collections, add "IdentityAuthType=AccessToken" if role-based access is enforced as the only authentication method. It isn't applicable for MongoDB and Gremlin collections.
105+
* For SQL collections, add "IdentityAuthType=AccessToken" to go through the _recommended_ approach, that is more secure and will work even if the account is configured to enforce role-based access as the only authentication method (i.e., `"disableLocalAuth": true`)
106+
* When using the REST API or the SDK, if this property is not specified on the connection string, Azure AI Search will default to using the _legacy_ approach. Azure portal will append this property to the connection string as the default.
92107
* For MongoDB collections, add "ApiKind=MongoDb" to the connection string and use a preview REST API.
93108
* For Gremlin graphs, add "ApiKind=Gremlin" to the connection string and use a preview REST API.
94109

95-
Here's an example of how to create a data source to index data from a Cosmos DB account using the [Create Data Source](/rest/api/searchservice/data-sources/create) REST API and a managed identity connection string. The managed identity connection string format is the same for the REST API, .NET SDK, and the Azure portal.
110+
MongoDB and Gremlin do not yet support the _recommended_ approach.
111+
112+
Here's an example of how to create a data source to index data from a Cosmos DB account using the [Create Data Source](/rest/api/searchservice/data-sources/create) REST API and a managed identity connection string that exercises the _recommended_ approach.
96113

97114
```http
98115
POST https://[service name].search.windows.net/datasources?api-version=2024-07-01
99116
{
100117
"name": "my-cosmosdb-ds",
101118
"type": "cosmosdb",
102119
"credentials": {
103-
"connectionString": "ResourceId=/subscriptions/[subscription-id]/resourceGroups/[rg-name]/providers/Microsoft.DocumentDB/databaseAccounts/[cosmos-account-name];Database=[cosmos-database];ApiKind=SQL;IdentityAuthType=[AccessToken | AccountKey]"
120+
"connectionString": "ResourceId=/subscriptions/[subscription-id]/resourceGroups/[rg-name]/providers/Microsoft.DocumentDB/databaseAccounts/[cosmos-account-name];Database=[cosmos-database];ApiKind=SQL;IdentityAuthType=AccessToken"
104121
},
105122
"container": { "name": "[my-cosmos-collection]", "query": null },
106123
"dataChangeDetectionPolicy": null
@@ -109,20 +126,31 @@ POST https://[service name].search.windows.net/datasources?api-version=2024-07-0
109126
}
110127
```
111128

112-
### User-assigned managed identity
129+
For completeness, here's the same example presented that goes through the _legacy_ approach
113130

114-
When you're connecting with a user-assigned managed identity, there are two changes to the data source definition:
131+
```http
132+
POST https://[service name].search.windows.net/datasources?api-version=2024-07-01
133+
{
134+
"name": "my-cosmosdb-ds",
135+
"type": "cosmosdb",
136+
"credentials": {
137+
"connectionString": "ResourceId=/subscriptions/[subscription-id]/resourceGroups/[rg-name]/providers/Microsoft.DocumentDB/databaseAccounts/[cosmos-account-name];Database=[cosmos-database];ApiKind=SQL;IdentityAuthType=ApiKey"
138+
},
139+
"container": { "name": "[my-cosmos-collection]", "query": null },
140+
"dataChangeDetectionPolicy": null
141+
}
142+
```
115143

116-
* First, the format of the "credentials" property is the database name and a ResourceId that has no account key or password. The ResourceId must include the subscription ID of Azure Cosmos DB, the resource group, and the Azure Cosmos DB account name.
144+
>[!NOTE]
145+
> If the `IdentityAuthType` property is not specified at all, then Azure AI Search defaults to the _legacy_ approach to ensure backward compatibility.
117146
118-
* For SQL collections, the connection string doesn't require "ApiKind".
119-
* For SQL collections, add "IdentityAuthType=AccessToken" if role-based access is enforced as the only authentication method. It isn't applicable for MongoDB and Gremlin collections.
120-
* For MongoDB collections, add "ApiKind=MongoDb" to the connection string
121-
* For Gremlin graphs, add "ApiKind=Gremlin" to the connection string.
147+
### User-assigned managed identity
122148

123-
* Second, you add an "identity" property that contains the collection of user-assigned managed identities. Only one user-assigned managed identity should be provided when creating the data source. Set it to type "userAssignedIdentities".
149+
When you're connecting with a user-assigned managed identity, the connection string definition remains the same as before.
124150

125-
Here's an example of how to create an indexer data source object using the REST API.
151+
You will need to add an "identity" property to the data source definition, where you'll specify the specific identity (out of several that can be assigned to the search service), that'll be used to connect to the Azure Cosmos DB account.
152+
153+
Here's an example of how to configure the data source definition, for an Azure Cosmos DB for NOSQL account, using user-assigned identity via the _recommended_ approach.
126154

127155
```http
128156
POST https://[service name].search.windows.net/datasources?api-version=2024-07-01
@@ -131,7 +159,7 @@ POST https://[service name].search.windows.net/datasources?api-version=2024-07-0
131159
"name": "[my-cosmosdb-ds]",
132160
"type": "cosmosdb",
133161
"credentials": {
134-
"connectionString": "ResourceId=/subscriptions/[subscription-id]/resourceGroups/[rg-name]/providers/Microsoft.DocumentDB/databaseAccounts/[cosmos-account-name];Database=[cosmos-database];ApiKind=SQL;IdentityAuthType=[AccessToken | AccountKey]"
162+
"connectionString": "ResourceId=/subscriptions/[subscription-id]/resourceGroups/[rg-name]/providers/Microsoft.DocumentDB/databaseAccounts/[cosmos-account-name];Database=[cosmos-database];ApiKind=SQL;IdentityAuthType=AccessToken"
135163
},
136164
"container": {
137165
"name": "[my-cosmos-collection]", "query": null
@@ -144,13 +172,17 @@ POST https://[service name].search.windows.net/datasources?api-version=2024-07-0
144172
}
145173
```
146174

175+
## Step 4: Run the indexer and validate the outcome
176+
147177
Connection information and permissions on the remote service are validated at run time during indexer execution. If the indexer is successful, the connection syntax and role assignments are valid. For more information, see [Run or reset indexers, skills, or documents](search-howto-run-reset-indexers.md).
148178

149179
## Troubleshooting
150180

151-
For Azure Cosmos DB for NoSQL, check whether the account has its access restricted to select networks. You can rule out any firewall issues by trying the connection without restrictions in place.
181+
* For Azure Cosmos DB for NoSQL, check whether the account has its access restricted to select networks. You can rule out any firewall issues by trying the connection without restrictions in place. Refer to [Indexer access to content protected by Azure network security](search-indexer-securing-resources) for more information
182+
183+
* For Azure Cosmos DB for NoSQL, if the indexer fails due to authentication issues, ensure that the role assignments have been done **both** on the control plane as well as data plane of the Cosmos DB account.
152184

153-
For Gremlin or MongoDB, if you recently rotated your Azure Cosmos DB account keys, you need to wait up to 15 minutes for the managed identity connection string to work.
185+
* For Gremlin or MongoDB, if you recently rotated your Azure Cosmos DB account keys, you need to wait up to 15 minutes for the managed identity connection string to work.
154186

155187
## See also
156188

0 commit comments

Comments
 (0)