|
| 1 | +--- |
| 2 | +title: Quickstart keyless connection |
| 3 | +titleSuffix: Azure AI Search |
| 4 | +description: In this quickstart, learn how to switch from API keys to Microsoft Entra identities and role-based access control (RBAC). |
| 5 | +author: HeidiSteen |
| 6 | +ms.author: heidist |
| 7 | +ms.service: azure-ai-search |
| 8 | + |
| 9 | +ms.topic: quickstart |
| 10 | +ms.date: 11/26/2024 |
| 11 | +--- |
| 12 | + |
| 13 | +# Quickstart: Connect without keys |
| 14 | + |
| 15 | +Configure Azure AI Search to use Microsoft Entra ID authentication and roles. Connect from your local system, running Jupyter notebooks, or using a REST client. |
| 16 | + |
| 17 | +If you stepped through other quickstarts that connect using API keys, this quickstart shows you how to switch to identity-based authentication so that you can avoid hard-coded API keys in your example code. |
| 18 | + |
| 19 | +## Prerequisites |
| 20 | + |
| 21 | +- An Azure subscription. [Create one for free](https://azure.microsoft.com/free/). |
| 22 | + |
| 23 | +- [Azure AI Search](search-create-service-portal.md), any region or tier, but you need Basic or higher to configure a system-assigned managed identity for Azure AI Search. |
| 24 | + |
| 25 | +- A command line tool, such as the [Azure CLI](/cli/azure/install-azure-cli). |
| 26 | + |
| 27 | +## Step 1: Set up your Azure subscription and tenant |
| 28 | + |
| 29 | +This step is necessary if you have more than one subscription or tenant. |
| 30 | + |
| 31 | +1. Get the Azure subscription and tenant for your search service: |
| 32 | + |
| 33 | + 1. Sign into the Azure portal and navigate to your search service. |
| 34 | + |
| 35 | + 1. Notice the subscription name and ID in **Overview** > **Essentials**. |
| 36 | + |
| 37 | + 1. Select the subscription name to view the parent management group (tenant ID). |
| 38 | + |
| 39 | + :::image type="content" source="media/search-get-started-rbac/select-subscription-name.png" lightbox="media/search-get-started-rbac/select-subscription-name.png" alt-text="Screenshot of the portal page providing the subscription name"::: |
| 40 | + |
| 41 | +1. Identify the active Azure subscription and tenant on your local device: |
| 42 | + |
| 43 | + `az account show` |
| 44 | + |
| 45 | +1. Set your Azure subscription to the subscription and tenant: |
| 46 | + |
| 47 | + `az account set --subscription <your-subscription-id>` |
| 48 | + |
| 49 | + `az login --tenant <your-tenant-id>` |
| 50 | + |
| 51 | +1. Check your tenant ID: |
| 52 | + |
| 53 | + `az account show --query tenantId --output tsv` |
| 54 | + |
| 55 | +## Step 2: Configure Azure AI Search for Microsoft Entra ID authentication |
| 56 | + |
| 57 | +1. Sign in to the Azure portal and navigate to your Azure AI Search service. |
| 58 | + |
| 59 | +1. Enable role-based access control (RBAC): |
| 60 | + |
| 61 | + 1. Go to **Settings** > **Keys**. |
| 62 | + |
| 63 | + 1. Choose **Role-based control** or **Both** if you need time to transition clients to role-based access control1. |
| 64 | + |
| 65 | +1. Assign roles in the Azure portal: |
| 66 | + |
| 67 | + 1. Navigate to your search service. |
| 68 | + |
| 69 | + 1. Select **Access Control (IAM)** in the left navigation pane. |
| 70 | + |
| 71 | + 1. Select **+ Add** > **Add role assignment**. |
| 72 | + |
| 73 | + 1. Choose a role (Search Service Contributor, Search Index Data Contributor, Search Index Data Reader) and assign it to your Microsoft Entra user or group identity. These three roles provide the full set of permissions for creating, loading, and querying objects on Azure AI Search. For more information, see [Connect using roles](search-security-rbac.md). |
| 74 | + |
| 75 | +## Step 3: Connect from your local system |
| 76 | + |
| 77 | +### Using Python and Jupyter notebooks |
| 78 | + |
| 79 | +1. Install the Azure Identity and Azure Search libraries: |
| 80 | + |
| 81 | + ```python |
| 82 | + pip install azure-identity azure-search-documents |
| 83 | + ``` |
| 84 | + |
| 85 | +1. Authenticate and connect to Azure AI Search: |
| 86 | + |
| 87 | + ```python |
| 88 | + from azure.identity import DefaultAzureCredential |
| 89 | + from azure.search.documents import SearchClient |
| 90 | + |
| 91 | + service_endpoint = "https://<your-search-service-name>.search.windows.net" |
| 92 | + index_name = "<your-index-name>" |
| 93 | + |
| 94 | + credential = DefaultAzureCredential() |
| 95 | + client = SearchClient(endpoint=service_endpoint, index_name=index_name, credential=credential) |
| 96 | + |
| 97 | + results = client.search("search text") |
| 98 | + for result in results: |
| 99 | + print(result) |
| 100 | + ``` |
| 101 | + |
| 102 | +### Using a REST client |
| 103 | + |
| 104 | +Several quickstarts and tutorials use a REST client, such as Visual Studio Code with the REST extension. Here's how you connect to Azure AI Search from Visual Studio Code. |
| 105 | + |
| 106 | +1. Get a personal identity token: |
| 107 | + |
| 108 | + `az account get-access-token --resource https://<your-search-service-name>.search.windows.net` |
| 109 | + |
| 110 | +1. Extract the token from the output: |
| 111 | + |
| 112 | + `TOKEN=$(az account get-access-token --resource https://<your-search-service-name>.search.windows.net --query accessToken --output tsv)` |
| 113 | + |
| 114 | +1. Provide the token in a request header: |
| 115 | + |
| 116 | + `az rest --method get --url "https://<your-search-service-name>.search.windows.net/indexes/<your-index-name>/docs?api-version=2021-04-30-Preview&search=*" --headers "Authorization=Bearer $TOKEN"` |
| 117 | + |
| 118 | +1. Specify the authorization bearer token in a REST call: |
| 119 | + |
| 120 | + ```REST |
| 121 | + POST https://{{baseUrl}}/indexes/{{index-name}}/docs/search?api-version=2024-07-01 HTTP/1.1 |
| 122 | + Content-type: application/json |
| 123 | + Authorization: Bearer {{token}} |
| 124 | + |
| 125 | + { |
| 126 | + "queryType": "simple", |
| 127 | + "search": "motel", |
| 128 | + "filter": "", |
| 129 | + "select": "HotelName,Description,Category,Tags", |
| 130 | + "count": true |
| 131 | + } |
| 132 | + ``` |
| 133 | + |
| 134 | +## Additional configuration |
| 135 | + |
| 136 | +Configure a managed identity for outbound connections: |
| 137 | + |
| 138 | +- [Configure a system-assigned or user-assigned managed identity](search-howto-managed-identities-data-sources.md) for your search service. |
| 139 | +- [Use role assignments](keyless-connections.md) to authorize access to other Azure resources. |
| 140 | + |
| 141 | +Network access configuration: |
| 142 | + |
| 143 | +- [Set inbound rules](service-configure-firewall.md) to accept or reject requests to Azure AI Search based on IP address. |
0 commit comments