Skip to content

Commit ab34f5f

Browse files
committed
Refreshed the REST test roles section with ready-to-use steps
1 parent 3e10403 commit ab34f5f

File tree

3 files changed

+100
-8
lines changed

3 files changed

+100
-8
lines changed

articles/search/search-howto-aad.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ ms.date: 1/05/2022
1111
ms.custom: subject-rbac-steps
1212
---
1313

14-
# Authorize access to a search apps using Azure Active Directory
14+
# Authorize access to a search app using Azure Active Directory
1515

1616
> [!IMPORTANT]
1717
> Role-based access control for data plane operations, such as creating or querying an index, is currently in public preview and available under [supplemental terms of use](https://azure.microsoft.com/support/legal/preview-supplemental-terms/). This functionality is only available in public cloud regions and may impact the latency of your operations while the functionality is in preview. For more information on preview limitations, see [RBAC preview limitations](search-security-rbac.md#preview-limitations).

articles/search/search-manage.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ Several aspects of a search service are determined when the service is provision
6262
Service administration includes the following tasks:
6363

6464
* [Adjust capacity](search-capacity-planning.md) by adding or removing replicas and partitions
65-
* [Manage API keys](search-security-api-keys.md) used for admin and query operations
66-
* [Allow or deny access using Azure roles](search-security-rbac.md)
65+
* [Manage API keys](search-security-api-keys.md) used for content access
66+
* [Manage Azure roles](search-security-rbac.md) used for content and service access
6767
* [Configure IP firewall rules](service-configure-firewall.md) to restrict access by IP address
6868
* [Configure a private endpoint](service-create-private-endpoint.md) using Azure Private Link and a private virtual network
6969
* [Monitor service health and operations](monitor-azure-cognitive-search.md): storage, query volumes, and latency

articles/search/search-security-rbac.md

Lines changed: 97 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -216,19 +216,111 @@ When testing roles, remember that roles are cumulative and inherited roles that
216216

217217
### [**REST API**](#tab/test-rest)
218218

219-
+ Register your REST client with Azure Active Directory.
219+
This approach assumes Postman as the REST client and uses a Postman collection and variables to provided the bearer token. You'll need Azure CLI or another tool to create a security principal for the REST client.
220220

221-
+ Revise your code to use a [Search REST API](/rest/api/searchservice/) (any supported version) and set the **Authorization** header on requests, replacing the **api-key** header.
221+
1. Open a command shell for Azure CLI. If you don't have Azure CLI installed, you can open [Create a service principal](/cli/azure/create-an-azure-service-principal-azure-cli#1-create-a-service-principal), select **Try It**.
222222

223-
:::image type="content" source="media/search-security-rbac/rest-authorization-header.png" alt-text="Screenshot of an HTTP request with an Authorization header" border="true":::
223+
1. Sign in to your Azure subscription.
224+
225+
```azurecli
226+
az login
227+
```
228+
229+
1. First, get your subscription ID. In the console, enter the following command:
230+
231+
```azurecli
232+
az account show --query id -o tsv
233+
````
234+
235+
1. Create a resource group for your security principal, specifying a location and name. This example uses the West US region.
236+
237+
```azurecli
238+
az group create -l westus -n MyResourceGroup
239+
```
240+
241+
1. Create the service principal, replacing the placeholder values with valid values. You'll need a descriptive security principal name, subscription ID, resource group name. This example uses the "Search Service Contributor" (quote enclosed) role.
242+
243+
```azurecli
244+
az ad sp create-for-rbac --name mySecurityPrincipalName --role "Search Service Contributor" --scopes /subscriptions/mySubscriptionID/resourceGroups/myResourceGroupName
245+
```
246+
247+
A successful response includes "appId", "password", and "tenant". You'll use these values for the variables "clientId", "clientSecret", and "tenant".
248+
249+
1. Start a new Postman collection and edit its properties. In the Variables tab, create the following variables:
250+
251+
| Variable | Description |
252+
|----------|-------------|
253+
| clientId | Provide the previously generated "appID" that you created in Azure AD. |
254+
| clientSecret | Provide the "password" that was created for your client. |
255+
| tenantId | Provide the "tenant" that was returned in the previous step. |
256+
| subscriptionId | Provide the subscription ID for your subscription. |
257+
| resource | Enter `https://search.azure.com`. |
258+
| bearerToken | (leave blank; the token is generated programmatically) |
259+
260+
1. In the Authorization tab, select **Bearer Token** as the type.
261+
262+
1. In the **Token** field, specify the variable placeholder `{{bearerToken}}`.
263+
264+
1. In the Pre-request Script tab, paste in the following script:
265+
266+
```javascript
267+
pm.test("Check for collectionVariables", function () {
268+
let vars = ['clientId', 'clientSecret', 'tenantId', 'subscriptionId'];
269+
vars.forEach(function (item, index, array) {
270+
console.log(item, index);
271+
pm.expect(pm.collectionVariables.get(item), item + " variable not set").to.not.be.undefined;
272+
pm.expect(pm.collectionVariables.get(item), item + " variable not set").to.not.be.empty;
273+
});
274+
275+
if (!pm.collectionVariables.get("bearerToken") || Date.now() > new Date(pm.collectionVariables.get("bearerTokenExpiresOn") * 1000)) {
276+
pm.sendRequest({
277+
url: 'https://login.microsoftonline.com/' + pm.collectionVariables.get("tenantId") + '/oauth2/token',
278+
method: 'POST',
279+
header: 'Content-Type: application/x-www-form-urlencoded',
280+
body: {
281+
mode: 'urlencoded',
282+
urlencoded: [
283+
{ key: "grant_type", value: "client_credentials", disabled: false },
284+
{ key: "client_id", value: pm.collectionVariables.get("clientId"), disabled: false },
285+
{ key: "client_secret", value: pm.collectionVariables.get("clientSecret"), disabled: false },
286+
{ key: "resource", value: pm.collectionVariables.get("resource") || "https://search.azure.com", disabled: false }
287+
]
288+
}
289+
}, function (err, res) {
290+
if (err) {
291+
console.log(err);
292+
} else {
293+
let resJson = res.json();
294+
pm.collectionVariables.set("bearerTokenExpiresOn", resJson.expires_on);
295+
pm.collectionVariables.set("bearerToken", resJson.access_token);
296+
}
297+
});
298+
}
299+
});
300+
```
301+
302+
1. Save the collection.
303+
304+
1. Send a request that uses the variables you've specified. For the "Search Index Data Reader" role, you can query an index (remember to provide a valid search service name on the URI):
305+
306+
```http
307+
POST https://<service-name>.search.windows.net/indexes/hotels-quickstart/docs/search?api-version=2020-06-20
308+
{
309+
"queryType": "simple",
310+
"search": "motel",
311+
"filter": "",
312+
"select": "HotelName,Description,Category,Tags",
313+
"count": true
314+
}
315+
```
224316

225317
For more information on how to acquire a token for a specific environment, see [Microsoft identity platform authentication libraries](../active-directory/develop/reference-v2-libraries.md).
226318

227319
### [**.NET SDK**](#tab/test-csharp)
228320

229-
The Azure SDK for .NET supports an authorization header in the [NuGet Gallery | Azure.Search.Documents 11.4.0-beta.2](https://www.nuget.org/packages/Azure.Search.Documents/11.4.0-beta.2) package.
321+
See [Authorize access to a search app using Azure Active Directory](/search-howto-aad.md) for instructions that create an identity for your client app, assign a role, and call [DefaultAzureCredential()](/dotnet/api/azure.identity.defaultazurecredential).
230322

231-
Configuration is required to register an application with Azure Active Directory, and to obtain and pass authorization tokens:
323+
The Azure SDK for .NET supports an authorization header in the [NuGet Gallery | Azure.Search.Documents 11.4.0-beta.2](https://www.nuget.org/packages/Azure.Search.Documents/11.4.0-beta.2) package. Configuration is required to register an application with Azure Active Directory, and to obtain and pass authorization tokens:
232324

233325
+ When obtaining the OAuth token, the scope is "https://search.azure.com/.default". The SDK requires the audience to be "https://search.azure.com". The ".default" is an Azure AD convention.
234326

0 commit comments

Comments
 (0)