Skip to content

Commit 0d33097

Browse files
authored
Merge pull request #223604 from HeidiSteen/heidist-rbac
[azure search] more revisions to api key docs
2 parents 324bdb9 + 781812d commit 0d33097

File tree

1 file changed

+82
-36
lines changed

1 file changed

+82
-36
lines changed

articles/search/search-security-api-keys.md

Lines changed: 82 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ API keys are frequently used when making REST API calls to a search service. You
2020
> [!NOTE]
2121
> 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.
2222
23-
## What's an API key?
23+
## Types of API keys
2424

2525
There are two kinds of keys used for authenticating a request:
2626

@@ -29,58 +29,109 @@ There are two kinds of keys used for authenticating a request:
2929
| 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. |
3030
| 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. |
3131

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.
3333

3434
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).
3535

3636
## Use API keys on connections
3737

3838
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.
3939

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).
4141

42-
Best practices for using hard-coded in source files include:
42+
Best practices for using hard-coded keys in source files include:
4343

4444
+ During early development and proof-of-concept testing when security is looser, use sample or public data.
4545

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.
4747

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)
4959

5060
+ 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.
5161

5262
+ Query keys are also specified in an HTTP request header for search, suggestion, or lookup operation that use POST.
5363

5464
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]`
5565

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)
6167

6268
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.
6369

6470
---
6571

6672
> [!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
6889

6990
## Find existing keys
7091

7192
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/).
7293

73-
### [**Azure portal**](#tab/portal-find)
94+
### [**Portal**](#tab/portal-find)
7495

7596
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).
7697

7798
1. Under **Settings**, select **Keys** to view admin and query keys.
7899

79100
:::image type="content" source="media/search-manage/azure-search-view-keys.png" alt-text="Screenshot of a portal page showing API keys." border="true":::
80101

81-
### [**REST**](#tab/rest-find)
102+
### [**PowerShell**](#tab/azure-ps-find)
103+
104+
1. Install the Az.Search module:
105+
106+
```azurepowershell
107+
Install-Module Az.Search
108+
```
109+
110+
1. Return admin keys:
111+
112+
```azurepowershell
113+
Get-AzSearchAdminKeyPair -ResourceGroupName <resource-group-name> -ServiceName <search-service-name>
114+
```
115+
116+
1. Return query keys:
117+
118+
```azurepowershell
119+
Get-AzSearchQueryKey -ResourceGroupName <resource-group-name> -ServiceName <search-service-name>
120+
```
82121

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.
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.
84135

85136
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.
86137

@@ -96,7 +147,7 @@ Query keys are used for read-only access to documents within an index for operat
96147

97148
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.
98149

99-
### [**Azure portal**](#tab/portal-query)
150+
### [**Portal**](#tab/portal-query)
100151

101152
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).
102153

@@ -106,13 +157,23 @@ Restricting access and operations in client apps is essential to safeguarding th
106157

107158
:::image type="content" source="media/search-security-overview/create-query-key.png" alt-text="Screenshot of the query key management options." border="true":::
108159

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+
109164
### [**Azure CLI**](#tab/azure-cli-query)
110165

111166
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).
112167

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.
114173

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+
```
116177

117178
---
118179

@@ -122,7 +183,7 @@ A code example showing query key usage can be found in [DotNetHowTo](https://git
122183

123184
Two admin keys are created for each service so that you can rotate a primary key while using the secondary key for business continuity.
124185

125-
1. In the **Settings** > **Keys** page, copy the secondary key.
186+
1. Under **Settings**, select **Keys**, then copy the secondary key.
126187

127188
1. For all applications, update the API key settings to use the secondary key.
128189

@@ -136,22 +197,7 @@ You can still access the service through the portal or programmatically. Managem
136197

137198
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.
138199

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
155201

156202
Use role assignments to restrict access to API keys.
157203

@@ -163,7 +209,7 @@ Note that it's not possible to use [customer-managed key encryption](search-secu
163209

164210
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.
165211

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.
167213

168214
## See also
169215

0 commit comments

Comments
 (0)