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
+57-31Lines changed: 57 additions & 31 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -39,48 +39,79 @@ API keys are specified on client requests to a search service. Passing a valid A
39
39
40
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.
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-key-access) 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
+
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 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 is 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":::
80
101
81
-
### [**REST**](#tab/rest-find)
102
+
### [**Azure CLI**](#tab/azure-cli-find)
103
+
104
+
Use the following commands to return admin and query API keys, respectively:
82
105
83
-
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.
106
+
```azurecli
107
+
az search admin-key show --resource-group <myresourcegroup> --service-name <myservice>
108
+
109
+
az search query-key list --resource-group <myresourcegroup> --service-name <myservice>
110
+
```
111
+
112
+
### [**REST API**](#tab/rest-find)
113
+
114
+
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
115
85
116
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
117
@@ -96,7 +127,7 @@ Query keys are used for read-only access to documents within an index for operat
96
127
97
128
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
129
99
-
### [**Azure portal**](#tab/portal-query)
130
+
### [**Portal**](#tab/portal-query)
100
131
101
132
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
133
@@ -106,13 +137,23 @@ Restricting access and operations in client apps is essential to safeguarding th
106
137
107
138
:::image type="content" source="media/search-security-overview/create-query-key.png" alt-text="Screenshot of the query key management options." border="true":::
108
139
140
+
### [**PowerShell**](#tab/azure-ps-query)
141
+
142
+
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).
143
+
109
144
### [**Azure CLI**](#tab/azure-cli-query)
110
145
111
146
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
147
113
-
### [**.NET**](#tab/dotnet-query)
148
+
### [**REST API**](#tab/rest-query)
114
149
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).
150
+
Use [Create Query Keys](/rest/api/searchmanagement/2020-08-01/query-keys/create) in the Management REST API.
151
+
152
+
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.
153
+
154
+
```rest
155
+
POST https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Search/searchServices/{searchServiceName}/createQueryKey/{name}?api-version=2020-08-01
156
+
```
116
157
117
158
---
118
159
@@ -122,7 +163,7 @@ A code example showing query key usage can be found in [DotNetHowTo](https://git
122
163
123
164
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
165
125
-
1.In the **Settings** >**Keys** page, copy the secondary key.
166
+
1.Under **Settings**, select**Keys**, then copy the secondary key.
126
167
127
168
1. For all applications, update the API key settings to use the secondary key.
128
169
@@ -136,21 +177,6 @@ You can still access the service through the portal or programmatically. Managem
136
177
137
178
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
179
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
180
## Secure API key access
155
181
156
182
Use role assignments to restrict access to API keys.
0 commit comments