Skip to content

Commit 4524cb3

Browse files
committed
Updated RBAC steps for testing roles using REST API
1 parent a8fe989 commit 4524cb3

File tree

1 file changed

+25
-77
lines changed

1 file changed

+25
-77
lines changed

articles/search/search-security-rbac.md

Lines changed: 25 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -201,103 +201,51 @@ Make sure that you [register your client application with Microsoft Entra ID](se
201201

202202
### [**REST API**](#tab/test-rest)
203203

204-
This approach assumes Postman as the REST client and uses a Postman collection and variables to provide the bearer token. Use Azure CLI or another tool to create a security principal for the REST client.
204+
This approach assumes Visual Studio Code with a REST client extension.
205205

206206
1. Open a command shell for Azure CLI and sign in to your Azure subscription.
207207

208208
```azurecli
209209
az login
210210
```
211211

212-
1. Get your subscription ID. The ID is used as a variable in a future step.
212+
1. Get your tenant ID and subscription ID. The ID is used as a variable in a future step.
213213

214214
```azurecli
215-
az account show --query id -o tsv
216-
````
215+
az account show
216+
```
217217

218-
1. Create a resource group for your security principal. This example uses the West US region. You provide this value as a variable in a future step. The role that you create is scoped to the resource group.
218+
1. Get an access token.
219219

220220
```azurecli
221-
az group create -l westus -n MyResourceGroup
221+
az account get-access-token --query accessToken --output tsv
222222
```
223223

224-
1. Create the service principal, replacing the placeholder values with valid values for a security principal name, subscription ID, and resource group name. This example uses the "Search Index Data Reader" (quote enclosed) role.
225-
226-
```azurecli
227-
az ad sp create-for-rbac --name mySecurityPrincipalName --role "Search Index Data Reader" --scopes /subscriptions/mySubscriptionID/resourceGroups/myResourceGroupName
228-
```
229-
230-
A successful response includes "appId", "password", and "tenant". You use these values for the variables "clientId", "clientSecret", and "tenant".
231-
232-
1. Start a new Postman collection and edit its properties. In the Variables tab, create the following variables:
233-
234-
| Variable | Description |
235-
|----------|-------------|
236-
| clientId | Provide the previously generated "appID" that you created in Microsoft Entra ID. |
237-
| clientSecret | Provide the "password" that was created for your client. |
238-
| tenantId | Provide the "tenant" that was returned in the previous step. |
239-
| subscriptionId | Provide the subscription ID for your subscription. |
240-
| resource | Enter `https://search.azure.com`. |
241-
| bearerToken | (leave blank; the token is generated programmatically) |
242-
243-
1. In the Authorization tab, select **Bearer Token** as the type.
244-
245-
1. In the **Token** field, specify the variable placeholder `{{bearerToken}}`.
246-
247-
1. In the Pre-request Script tab, paste in the following script:
248-
249-
```javascript
250-
pm.test("Check for collectionVariables", function () {
251-
let vars = ['clientId', 'clientSecret', 'tenantId', 'subscriptionId'];
252-
vars.forEach(function (item, index, array) {
253-
console.log(item, index);
254-
pm.expect(pm.collectionVariables.get(item), item + " variable not set").to.not.be.undefined;
255-
pm.expect(pm.collectionVariables.get(item), item + " variable not set").to.not.be.empty;
256-
});
257-
258-
if (!pm.collectionVariables.get("bearerToken") || Date.now() > new Date(pm.collectionVariables.get("bearerTokenExpiresOn") * 1000)) {
259-
pm.sendRequest({
260-
url: 'https://login.microsoftonline.com/' + pm.collectionVariables.get("tenantId") + '/oauth2/token',
261-
method: 'POST',
262-
header: 'Content-Type: application/x-www-form-urlencoded',
263-
body: {
264-
mode: 'urlencoded',
265-
urlencoded: [
266-
{ key: "grant_type", value: "client_credentials", disabled: false },
267-
{ key: "client_id", value: pm.collectionVariables.get("clientId"), disabled: false },
268-
{ key: "client_secret", value: pm.collectionVariables.get("clientSecret"), disabled: false },
269-
{ key: "resource", value: pm.collectionVariables.get("resource") || "https://search.azure.com", disabled: false }
270-
]
271-
}
272-
}, function (err, res) {
273-
if (err) {
274-
console.log(err);
275-
} else {
276-
let resJson = res.json();
277-
pm.collectionVariables.set("bearerTokenExpiresOn", resJson.expires_on);
278-
pm.collectionVariables.set("bearerToken", resJson.access_token);
279-
}
280-
});
281-
}
282-
});
283-
```
224+
1. In a new text file in Visual Studio Code, paste in these variables:
284225

285-
1. Save the collection.
226+
```http
227+
@baseUrl = PASTE-YOUR-SEARCH-SERVICE-URL-HERE
228+
@index-name = PASTE-YOUR-INDEX-NAME-HERE
229+
@token = PASTE-YOUR-TOKEN-HERE
230+
```
286231

287-
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). You can use any [supported API version](/rest/api/searchservice/search-service-api-versions).
232+
1. Paste in and then send a request that uses the variables you've specified. For the "Search Index Data Reader" role, you can send a query. You can use any [supported API version](/rest/api/searchservice/search-service-api-versions).
288233

289234
```http
290-
POST https://<service-name>.search.windows.net/indexes/hotels-quickstart/docs/search?api-version=2020-06-30
291-
{
292-
"queryType": "simple",
293-
"search": "motel",
294-
"filter": "",
295-
"select": "HotelName,Description,Category,Tags",
296-
"count": true
297-
}
235+
POST https://{{baseUrl}}/indexes/{{indexName}}/docs/search?api-version=2023-11-01 HTTP/1.1
236+
Content-type: application/json
237+
Authorization: Bearer {{token}}
238+
239+
{
240+
"queryType": "simple",
241+
"search": "motel",
242+
"filter": "",
243+
"select": "HotelName,Description,Category,Tags",
244+
"count": true
245+
}
298246
```
299247

300-
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).
248+
For more information on how to acquire a token for a specific environment, see [Manage a Azure AI Search service with REST APIs](search-manage-rest.md) and [Microsoft identity platform authentication libraries](../active-directory/develop/reference-v2-libraries.md).
301249

302250
### [**.NET**](#tab/test-csharp)
303251

0 commit comments

Comments
 (0)