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-api-keys.md
+82-36Lines changed: 82 additions & 36 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -20,7 +20,7 @@ API keys are frequently used when making REST API calls to a search service. You
20
20
> [!NOTE]
21
21
> A quick note about "key" terminology in Cognitive Search. An "API key", which is described in this article, refers to a GUID used for authenticating a request. A "document key" refers to a unique string in your indexed content that's used to uniquely identify documents in a search index. API keys and document keys are unrelated.
22
22
23
-
## What's an API key?
23
+
## Types of API keys
24
24
25
25
There are two kinds of keys used for authenticating a request:
26
26
@@ -29,58 +29,109 @@ There are two kinds of keys used for authenticating a request:
29
29
| Admin | Full access (read-write) for all content operations | 2 <sup>1</sup>| Two admin keys, referred to as *primary* and *secondary* keys in the portal, are generated when the service is created and can be individually regenerated on demand. |
30
30
| Query | Read-only access, scoped to the documents collection of a search index | 50 | One query key is generated with the service. More can be created on demand by a search service administrator. |
31
31
32
-
<sup>1</sup> Having two allows you to roll over one key while using the second key for continued access to the service.
32
+
<sup>1</sup> Having two allows you to roll over one key while using the second key for continued access to the service.
33
33
34
34
Visually, there's no distinction between an admin key or query key. Both keys are strings composed of 52 randomly generated alpha-numeric characters. If you lose track of what type of key is specified in your application, you can [check the key values in the portal](#find-existing-keys).
35
35
36
36
## Use API keys on connections
37
37
38
38
API keys are specified on client requests to a search service. Passing a valid API key on the request is considered proof that the request is from an authorized client. If you're creating, modifying, or deleting objects, you'll need an admin API key. Otherwise, query keys are typically distributed to client applications that issue queries.
39
39
40
-
You can specify API keys in a request header for REST API calls, or in code that calls the azure.search.documents client libraries in the Azure SDKs. If you're using the Azure portal to perform tasks, your role assignment determines the level of access.
40
+
You can specify API keys in a request header for REST API calls, or in code that calls the azure.search.documents client libraries in the Azure SDKs. If you're using the Azure portal to perform tasks, your role assignment determines the [level of access](#permissions-to-view-or-manage-api-keys).
41
41
42
-
Best practices for using hard-coded in source files include:
42
+
Best practices for using hard-coded keys in source files include:
43
43
44
44
+ During early development and proof-of-concept testing when security is looser, use sample or public data.
45
45
46
-
+After advancing into deeper development or production scenarios, switch to [Azure Active Directory and role-based access](search-security-rbac.md) to eliminate the need for having hard-coded keys. Or, if you want to continue using API keys, be sure to always monitor who has access to your API keys and regenerate API keys on a regular cadence.
46
+
+For mature solutions or production scenarios, switch to [Azure Active Directory and role-based access](search-security-rbac.md) to eliminate the need for having hard-coded keys. Or, if you want to continue using API keys, be sure to always monitor [who has access to your API keys](#secure-api-keys) and [regenerate API keys](#regenerate-admin-keys) on a regular cadence.
47
47
48
-
### [**REST**](#tab/rest-use)
48
+
### [**Portal**](#tab/portal-use)
49
+
50
+
In Cognitive Search, most tasks can be performed in Azure portal, including object creation, indexing through the Import data wizard, and queries through Search explorer.
51
+
52
+
Authentication is built in so no action is required. By default, the portal uses API keys to authenticate the request automatically. However, if you [disable API keys](search-security-rbac.md#disable-api-key-authentication) and set up role assignments, the portal uses role assignments instead.
53
+
54
+
### [**PowerShell**](#tab/azure-ps-use)
55
+
56
+
A script example showing API key usage for various operations can be found at [Quickstart: Create an Azure Cognitive Search index in PowerShell using REST APIs](search-get-started-powershell.md).
57
+
58
+
### [**REST API**](#tab/rest-use)
49
59
50
60
+ Admin keys are only specified in HTTP request headers. You can't place an admin API key in a URL. See [Connect to Azure Cognitive Search using REST APIs](search-get-started-rest.md#connect-to-azure-cognitive-search) for an example that specifies an admin API key on a REST call.
51
61
52
62
+ Query keys are also specified in an HTTP request header for search, suggestion, or lookup operation that use POST.
53
63
54
64
Alternatively, you can pass a query key as a parameter on a URL if you're using GET: `GET /indexes/hotels/docs?search=*&$orderby=lastRenovationDate desc&api-version=2020-06-30&api-key=[query key]`
55
65
56
-
### [**Azure PowerShell**](#tab/azure-ps-use)
57
-
58
-
A script example showing API key usage can be found at [Quickstart: Create an Azure Cognitive Search index in PowerShell using REST APIs](search-get-started-powershell.md).
59
-
60
-
### [**.NET**](#tab/dotnet-use)
66
+
### [**C#**](#tab/dotnet-use)
61
67
62
68
In search solutions, a key is often specified as a configuration setting and then passed as an [AzureKeyCredential](/dotnet/api/azure.azurekeycredential). See [How to use Azure.Search.Documents in a C# .NET Application](search-howto-dotnet-sdk.md) for an example.
63
69
64
70
---
65
71
66
72
> [!NOTE]
67
-
> It's considered a poor security practice to pass sensitive data such as an `api-key` in the request URI. For this reason, Azure Cognitive Search only accepts a query key as an `api-key` in the query string. As a general rule, we recommend passing your `api-key` as a request header.
73
+
> It's considered a poor security practice to pass sensitive data such as an `api-key` in the request URI. For this reason, Azure Cognitive Search only accepts a query key as an `api-key` in the query string. As a general rule, we recommend passing your `api-key` as a request header.
74
+
75
+
## Permissions to view or manage API keys
76
+
77
+
Permissions for viewing and managing API keys are conveyed through [role assignments](search-security-rbac.md). Members of the following roles can view and regenerate keys:
78
+
79
+
+ Owner
80
+
+ Contributor
81
+
+[Search Service Contributor](../role-based-access-control/built-in-roles.md#search-service-contributor)
82
+
+ Administrator and co-administrator (classic)
83
+
84
+
The following roles don't have access to API keys:
85
+
86
+
+ Reader
87
+
+ Search Index Data Contributor
88
+
+ Search Index Data Reader
68
89
69
90
## Find existing keys
70
91
71
92
You can view and manage API keys in the [Azure portal](https://portal.azure.com), or through [PowerShell](/powershell/module/az.search), [Azure CLI](/cli/azure/search), or [REST API](/rest/api/searchmanagement/).
72
93
73
-
### [**Azure portal**](#tab/portal-find)
94
+
### [**Portal**](#tab/portal-find)
74
95
75
96
1. Sign in to the [Azure portal](https://portal.azure.com) and [find your search service](https://portal.azure.com/#blade/HubsExtension/BrowseResourceBlade/resourceType/Microsoft.Search%2FsearchServices).
76
97
77
98
1. Under **Settings**, select **Keys** to view admin and query keys.
78
99
79
100
:::image type="content" source="media/search-manage/azure-search-view-keys.png" alt-text="Screenshot of a portal page showing API keys." border="true":::
Use [ListAdminKeys](/rest/api/searchmanagement/2020-08-01/admin-keys) or [ListQueryKeys](/rest/api/searchmanagement/2020-08-01/query-keys/list-by-search-service) in the Management REST API to return API keys.
122
+
### [**Azure CLI**](#tab/azure-cli-find)
123
+
124
+
Use the following commands to return admin and query API keys, respectively:
125
+
126
+
```azurecli
127
+
az search admin-key show --resource-group <myresourcegroup> --service-name <myservice>
128
+
129
+
az search query-key list --resource-group <myresourcegroup> --service-name <myservice>
130
+
```
131
+
132
+
### [**REST API**](#tab/rest-find)
133
+
134
+
Use [List Admin Keys](/rest/api/searchmanagement/2020-08-01/admin-keys) or [List Query Keys](/rest/api/searchmanagement/2020-08-01/query-keys/list-by-search-service) in the Management REST API to return API keys.
84
135
85
136
You must have a [valid role assignment](#permissions-to-view-or-manage-api-keys) to return or update API keys. See [Manage your Azure Cognitive Search service with REST APIs](search-manage-rest.md) for guidance on meeting role requirements using the REST APIs.
86
137
@@ -96,7 +147,7 @@ Query keys are used for read-only access to documents within an index for operat
96
147
97
148
Restricting access and operations in client apps is essential to safeguarding the search assets on your service. Always use a query key rather than an admin key for any query originating from a client app.
98
149
99
-
### [**Azure portal**](#tab/portal-query)
150
+
### [**Portal**](#tab/portal-query)
100
151
101
152
1. Sign in to the [Azure portal](https://portal.azure.com) and [find your search service](https://portal.azure.com/#blade/HubsExtension/BrowseResourceBlade/resourceType/Microsoft.Search%2FsearchServices).
102
153
@@ -106,13 +157,23 @@ Restricting access and operations in client apps is essential to safeguarding th
106
157
107
158
:::image type="content" source="media/search-security-overview/create-query-key.png" alt-text="Screenshot of the query key management options." border="true":::
108
159
160
+
### [**PowerShell**](#tab/azure-ps-query)
161
+
162
+
A script example showing API key usage can be found at [Create or delete query keys](search-manage-powershell.md#create-or-delete-query-keys).
163
+
109
164
### [**Azure CLI**](#tab/azure-cli-query)
110
165
111
166
A script example showing query key usage can be found at [Create or delete query keys](search-manage-azure-cli.md#create-or-delete-query-keys).
112
167
113
-
### [**.NET**](#tab/dotnet-query)
168
+
### [**REST API**](#tab/rest-query)
169
+
170
+
Use [Create Query Keys](/rest/api/searchmanagement/2020-08-01/query-keys/create) in the Management REST API.
171
+
172
+
You must have a [valid role assignment](#permissions-to-view-or-manage-api-keys) to create or manage API keys. See [Manage your Azure Cognitive Search service with REST APIs](search-manage-rest.md) for guidance on meeting role requirements using the REST APIs.
114
173
115
-
A code example showing query key usage can be found in [DotNetHowTo](https://github.com/Azure-Samples/search-dotnet-getting-started/tree/master/DotNetHowTo).
174
+
```rest
175
+
POST https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Search/searchServices/{searchServiceName}/createQueryKey/{name}?api-version=2020-08-01
176
+
```
116
177
117
178
---
118
179
@@ -122,7 +183,7 @@ A code example showing query key usage can be found in [DotNetHowTo](https://git
122
183
123
184
Two admin keys are created for each service so that you can rotate a primary key while using the secondary key for business continuity.
124
185
125
-
1.In the **Settings** >**Keys** page, copy the secondary key.
186
+
1.Under **Settings**, select**Keys**, then copy the secondary key.
126
187
127
188
1. For all applications, update the API key settings to use the secondary key.
128
189
@@ -136,22 +197,7 @@ You can still access the service through the portal or programmatically. Managem
136
197
137
198
After you create new keys via portal or management layer, access is restored to your content (indexes, indexers, data sources, synonym maps) once you provide those keys on requests.
138
199
139
-
## Permissions to view or manage API keys
140
-
141
-
Permissions for viewing and managing API keys is conveyed through [role assignments](search-security-rbac.md). Members of the following roles can view and regenerate keys:
142
-
143
-
+ Administrator and co-administrator (classic)
144
-
+ Owner
145
-
+ Contributor
146
-
+[Search Service Contributors](../role-based-access-control/built-in-roles.md#search-service-contributor)
147
-
148
-
The following roles don't have access to API keys:
149
-
150
-
+ Reader
151
-
+ Search Index Data Contributor
152
-
+ Search Index Data Reader
153
-
154
-
## Secure API key access
200
+
## Secure API keys
155
201
156
202
Use role assignments to restrict access to API keys.
157
203
@@ -163,7 +209,7 @@ Note that it's not possible to use [customer-managed key encryption](search-secu
163
209
164
210
1. In the **Role** filter, select the roles that have permission to view or manage keys (Owner, Contributor, Search Service Contributor). The resulting security principals assigned to those roles have key permissions on your search service.
165
211
166
-
1. As a precaution, also check the **Classic administrators** tab for administrators and co-administrators.
212
+
1. As a precaution, also check the **Classic administrators** tab to determine whether administrators and co-administrators have access.
0 commit comments