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/search/search-security-trimming-for-azure-search.md
+37-36Lines changed: 37 additions & 36 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: Security filters for trimming results
2
+
title: Security filter pattern
3
3
titleSuffix: Azure AI Search
4
4
description: Learn how to implement security privileges at the document level for Azure AI Search search results, using security filters and user identities.
5
5
@@ -10,48 +10,50 @@ ms.service: cognitive-search
10
10
ms.custom:
11
11
- ignite-2023
12
12
ms.topic: how-to
13
-
ms.date: 01/10/2024
13
+
ms.date: 06/20/2024
14
14
---
15
15
16
16
# Security filters for trimming results in Azure AI Search
17
17
18
-
Azure AI Search doesn't provide document-level permissions and can't vary search results from within the same index by user permissions. As a workaround, you can create a filter that trims search results based on a string containing a group or user identity.
18
+
Azure AI Search doesn't provide native document-level permissions and can't vary search results from within the same index by user permissions. As a workaround, you can create a filter that trims search results based on a string containing a group or user identity.
19
19
20
-
This article describes a pattern for security filtering that includes following steps:
20
+
This article describes a pattern for security filtering having the following steps:
21
21
22
22
> [!div class="checklist"]
23
23
> * Assemble source documents with the required content
24
24
> * Create a field for the principal identifiers
25
25
> * Push the documents to the search index for indexing
26
26
> * Query the index with the `search.in` filter function
27
27
28
+
It concludes with links to demos and examples that provide hands-on learning. We recommend reviewing this article first to understand the pattern.
29
+
28
30
## About the security filter pattern
29
31
30
-
Although Azure AI Search doesn't integrate with security subsystems for access to content within an index, many customers who have document-level security requirements have found that filters can meet their needs.
32
+
Although Azure AI Search doesn't integrate with security subsystems for access to content within an index, many customers who have document-level security requirements find that filters can meet their needs.
31
33
32
-
In Azure AI Search, a security filter is a regular OData filter that includes or excludes a search result based on a matching value, except that in a security filter, the criteria is a string consisting of a security principal. There's no authentication or authorization through the security principal. The principal is just a string, used in a filter expression, to include or exclude a document from the search results.
34
+
In Azure AI Search, a security filter is a regular OData filter that includes or excludes a search result based on a string consisting of a security principal. There's no authentication or authorization through the security principal. The principal is just a string, used in a filter expression, to include or exclude a document from the search results.
33
35
34
36
There are several ways to achieve security filtering. One way is through a complicated disjunction of equality expressions: for example, `Id eq 'id1' or Id eq 'id2'`, and so forth. This approach is error-prone, difficult to maintain, and in cases where the list contains hundreds or thousands of values, slows down query response time by many seconds.
35
37
36
38
A better solution is using the `search.in` function for security filters, as described in this article. If you use `search.in(Id, 'id1, id2, ...')` instead of an equality expression, you can expect subsecond response times.
37
39
38
40
## Prerequisites
39
41
40
-
*The field containing group or user identitymust be a string with the filterable attribute. It should be a collection. It shouldn't allow nulls.
42
+
*A string field containing a group or user identity. It must have the filterable attribute. It should be a collection. It shouldn't allow nulls.
41
43
42
-
* Other fields in the same document should provide the content that's accessible to that group or user. In the following JSON documents, the "security_id" fields contain identities used in a security filter, and the name, salary, and marital status will be included if the identity of the caller matches the "security_id" of the document.
44
+
* Other fields in the same document should provide the content that's accessible to that group or user. In the following JSON documents, the "security_id" fields contain identities used in a security filter, and the name, salary, and marital status are included if the identity of the caller matches the "security_id" of the document.
43
45
44
46
```json
45
47
{
46
48
"Employee-1": {
47
-
"id": "100-1000-10-1-10000-1",
49
+
"employee_id": "100-1000-10-1-10000-1",
48
50
"name": "Abram",
49
51
"salary": 75000,
50
52
"married": true,
51
53
"security_id": "10011"
52
54
},
53
55
"Employee-2": {
54
-
"id": "200-2000-20-2-20000-2",
56
+
"employee_id": "200-2000-20-2-20000-2",
55
57
"name": "Adams",
56
58
"salary": 75000,
57
59
"married": true,
@@ -60,18 +62,19 @@ A better solution is using the `search.in` function for security filters, as des
60
62
}
61
63
```
62
64
63
-
>[!NOTE]
64
-
> The process of retrieving the principal identifiers and injecting those strings into source documents that can be indexed by Azure AI Search isn't covered in this article. Refer to the documentation of your identity service provider for help with obtaining identifiers.
65
-
66
65
## Create security field
67
66
68
-
In the search index, within the field collection, you need one field that contains the group or user identity, similar to the fictitious "security_id" field in the previous example.
67
+
In the search index, within the fields collection, you need one field that contains the group or user identity, similar to the fictitious "security_id" field in the previous example.
68
+
69
+
1. Add a security field as a `Collection(Edm.String)`. Make sure it has a `filterable` attribute set to `true`. In this example, the security field is `group_ids`. It's populated with the string `["group_id1, group_id2"]`.
70
+
71
+
1. Set the field's `retrievable` attribute to `false` so that it isn't returned as part of the search request.
69
72
70
-
1. Add a security field as a `Collection(Edm.String)`. Make sure it has a `filterable` attribute set to `true` so that search results are filtered based on the access the user has. For example, if you set the `group_ids` field to `["group_id1, group_id2"]` for the document with `file_name` "secured_file_b", only users that belong to group IDs "group_id1" or "group_id2" have read access to the file.
73
+
1. Indexes require a document key. The "file_id" field satisfies that requirement.
71
74
72
-
Set the field's `retrievable` attribute to `false` so that it isn't returned as part of the search request.
75
+
1. Indexes should also contain searchable and retrievable content. The "file_name" and "file_description" fields represent that in this example.
73
76
74
-
1. Indexes require a document key. The "file_id" field satisfies that requirement. Indexes should also contain searchable content. The "file_name" and "file_description" fields represent that in this example.
77
+
The following index schema satisfies the field requirements. Documents that you index on Azure AI Search should have values for all of these fields, including the "group_ids". For the document with `file_name` "secured_file_b", only users that belong to group IDs "group_id1" or "group_id2" have read access to the file.
75
78
76
79
```https
77
80
POST https://[search service].search.windows.net/indexes/securedfiles/docs/index?api-version=2023-11-01
@@ -87,23 +90,25 @@ In the search index, within the field collection, you need one field that contai
87
90
```
88
91
89
92
## Push data into your index using the REST API
90
-
91
-
Send an HTTP POST request to the docs collection of your index's URL endpoint (see [Documents - Index](/rest/api/searchservice/documents/)). The body of the HTTP request is a JSON rendering of the documents to be indexed:
92
93
93
-
```http
94
-
POST https://[search service].search.windows.net/indexes/securedfiles/docs/index?api-version=2023-11-01
95
-
```
94
+
Populate your search index with documents that provide values for each field in the fields collection, including values for the security field. Azure AI Search doesn't provide APIs or features for populating the security field specifically. However, several of the examples listed at the end of this article explain techniques for populating this field.
96
95
97
-
In the request body, specify the content of your documents:
96
+
In Azure AI Search, the approaches for loading data are:
98
97
99
-
```JSON
98
+
* A single push or pull (indexer) operation that imports documents populated with all fields
99
+
* Multiple push or pull operations. As long as secondary import operations target the right document identifier, you can load fields individually through multiple imports.
100
+
101
+
The following example shows a single HTTP POST request to the docs collection of your index's URL endpoint (see [Documents - Index](/rest/api/searchservice/documents/)). The body of the HTTP request is a JSON rendering of the documents to be indexed:
102
+
103
+
```http
104
+
POST https://[search service].search.windows.net/indexes/securedfiles/docs/index?api-version=2023-11-01
100
105
{
101
106
"value": [
102
107
{
103
108
"@search.action": "upload",
104
109
"file_id": "1",
105
110
"file_name": "secured_file_a",
106
-
"file_description": "File access is restricted to the Human Resources.",
111
+
"file_description": "File access is restricted to Human Resources.",
107
112
"group_ids": ["group_id1"]
108
113
},
109
114
{
@@ -147,17 +152,11 @@ For full details on searching documents using Azure AI Search, you can read [Sea
147
152
148
153
This sample shows how to set up query using a POST request.
149
154
150
-
Issue the HTTP POST request:
155
+
Issue the HTTP POST request, specifying the filter in the request body:
151
156
152
157
```http
153
-
POST https://[service name].search.windows.net/indexes/securedfiles/docs/search?api-version=2020-06-30
154
-
Content-Type: application/json
155
-
api-key: [admin or query key]
156
-
```
157
-
158
-
Specify the filter in the request body:
158
+
POST https://[service name].search.windows.net/indexes/securedfiles/docs/search?api-version=2023-11-01
@@ -186,7 +185,9 @@ You should get the documents back where `group_ids` contains either "group_id1"
186
185
187
186
This article describes a pattern for filtering results based on user identity and the `search.in()` function. You can use this function to pass in principal identifiers for the requesting user to match against principal identifiers associated with each target document. When a search request is handled, the `search.in` function filters out search results for which none of the user's principals have read access. The principal identifiers can represent things like security groups, roles, or even the user's own identity.
188
187
189
-
For an alternative pattern based on Microsoft Entra ID, or to revisit other security features, see the following links.
188
+
For more examples, demos, and videos:
190
189
191
-
*[Security filters for trimming results using Microsoft Entra ID identities](search-security-trimming-for-azure-search-with-aad.md)
192
-
*[Security in Azure AI Search](search-security-overview.md)
190
+
*[Configure document security for a chat app in Python](/azure/developer/python/get-started-app-chat-document-security-trim)
191
+
*[Set up a security filter using Microsoft Entra ID](search-security-trimming-for-azure-search-with-aad.md)
192
+
*[Set up optional sign in and document level access control (modifications to the AzureOpenAIDemo app)](https://github.com/Azure-Samples/azure-search-openai-demo/blob/main/docs/login_and_acl.md)
193
+
*[Video: Secure your Intelligent Applications with Microsoft Entra](https://build.microsoft.com/en-US/sessions/b5636ca7-64c2-493c-9b30-4a35852acfbe?source=/speakers/cc9b56a0-4af0-4b60-a2f3-8312c5b35ca2)
0 commit comments