Skip to content

Commit 16000aa

Browse files
Merge pull request #223833 from HeidiSteen/heidist-rbac
[azure search] Refreshed the REST "test roles" section with repeatable steps
2 parents a034ea4 + f5bb457 commit 16000aa

File tree

3 files changed

+98
-8
lines changed

3 files changed

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

225315
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).
226316

227317
### [**.NET SDK**](#tab/test-csharp)
228318

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.
319+
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).
230320

231-
Configuration is required to register an application with Azure Active Directory, and to obtain and pass authorization tokens:
321+
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:
232322

233323
+ 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.
234324

0 commit comments

Comments
 (0)