Loading...

How the Search Service Authenticates to Access an Azure SQL Database

How the Search Service Authenticates to Access an Azure SQL Database

When using Azure Cognitive Search to index data from an Azure SQL database, it's important to understand the different authentication methods that can be used to connect to the database. In this blog post, we'll explain the different authentication methods that the Search service can use to access an Azure SQL database.

 

Authentication Methods in SQL DB

There are several authentication methods that can be used to connect to an Azure SQL database, including:

 

SQL Authentication

SQL authentication is a method of authentication that uses a username and password to connect to an Azure SQL database. This method is commonly used when connected to a database from an application or service that does not support integrated Windows authentication.

 

Azure Active Directory Authentication:

This method of authentication uses Azure Active Directory to authenticate users and services to an Azure SQL database. It is commonly used when connecting to a database from a cloud-based application or service.

You can find it in Azure portal of your SQL database Connection String.

Scarlett_liu_0-1679919423174.png

 

Managed Identity

Managed identity is a feature of Azure Active Directory that allows you to authenticate to other Azure services without needing to store credentials in your code or configuration files. When you create a managed identity, Azure creates an identity for your service that can be used to authenticate to other Azure services within the same tenant and subscription.

 

How the Search Service Authenticates to Access an Azure SQL Database

When you create a data source to index data from an Azure SQL database, you can choose to use SQL authentication with your username and password.

 

Azure Active Directory Authentication

When we attempted to use Active Directory Authentication for users with the role of connecting to Azure Search service, an error occurred. The error message displayed was as follows:

"error": {

    "code": "",

    "message": "Failed to authenticate the {user email} in Active Directory (Authentication=ActiveDirectoryPassword).

Error code 0xparsing_wstrust_response_failed\r\nThere was an error parsing WS-Trust response from the endpoint.

Error Message: Federated service at {endpoint} returned an error: ID3242: The security token could not be authenticated or authorized. "

}

If you test in portal, it will show “Login failed for user {user email}”

Scarlett_liu_1-1679919423182.png

 

What’s the reason? As when Search service connects with Azure SQL DB as a data source, it directly connects with the SQL tables. However, there isn't an AAD user account on the SQL table. Other applications, such as Azure Data Studio, check SQL AAD users and then connect with tables. So, this error indicates that the AAD user account assigned in Azure SQL server account cannot be used to connect from the Azure Search service.

Any workarounds? Using a managed identity!

 

Set up an indexer connection to Azure SQL using a managed identity!

The diagram below illustrates how the Search Service connects to an SQL server through Managed Identity.

Scarlett_liu_2-1679919423184.png

To connect to an SQL database using a managed identity, you need to create a user with the name of the Search service or user-managed identity in the SQL database. Then, use the resource ID of the SQL database as the connection string. Here is the example to set up an indexer connection to an Azure SQL database using a User Assigned Managed Identity, follow these steps:

  • First, please assign a reader role in your managed identity to select data from SQL DB.

Scarlett_liu_3-1679919423194.png

 

  • And then you need to log in using the AAD admin and create the user in the database to create a user in an SQL database. You can use this SQL query below:

 

 

//Create the user use user-assigned managed identity name CREATE USER [user-assigned managed identity name] FROM EXTERNAL PROVIDER; EXEC sp_addrolemember 'db_datareader', [user-assigned managed identity name];

 

 

 

Scarlett_liu_4-1679919423209.png

 

  • After that please use the resource id of SQL server as the Connection String.

 

 

Database=[SQL database name];ResourceId=/subscriptions/[subscription ID]/resourceGroups/[resource group name]/providers/Microsoft.Sql/servers/[SQL Server name];Connection Timeout=30

 

 

 

Scarlett_liu_5-1679919423215.png

 

 

Attention:

Make sure to check whether the managed identity has been added to the Search service. When adding this data source from Rest API Create or Update Data Source (2021-04-30-Preview) - Azure Cognitive Search | Microsoft Learn, also verify whether it has been added to the identity in Search Service.

Scarlett_liu_6-1679919423224.png

Another thing to keep in mind is to ensure that you add your Search service IP to the firewall list of the Azure SQL service. Otherwise, you will receive the following error:

Scarlett_liu_7-1679919423231.png

Change the SQL server to “Selected networks” and add the firewall rules with your Search Service IP or using a shared private link instead.

For the details, please check the reference documents:

Connect through firewalls - Azure Cognitive Search | Microsoft Learn

Connect through a private endpoint - Azure Cognitive Search | Microsoft Learn

Scarlett_liu_0-1679920332343.png

 

Conclusion

In this blog post, we've explained the different authentication methods that can be used to connect to an Azure SQL database, and how the Search service can use managed identity to authenticate to the database. Using managed identity to authenticate to an Azure SQL database is a secure and scalable way to connect to the database from Azure Cognitive Search. By following the steps outlined in this blog post, you can set up an indexer connection to an Azure SQL database using a managed identity and start indexing your data in no time.

 

Reference: Connect to Azure SQL - Azure Cognitive Search | Microsoft Learn

 

Published on:

Learn more
Azure PaaS Blog articles
Azure PaaS Blog articles

Azure PaaS Blog articles

Share post:

Related posts

Azure Storage - TLS 1.0 and 1.1 retirement

Overview TLS 1.0 and 1.1 retirement on Azure Storage was previously announced for Nov 1st, 2024, and it was postponed recently to 1 year later...

1 year ago

Efficient Management of Append and Page Blobs Using Azure Storage Actions

  Overview In Azure Storage, Blob Lifecycle Management (BLM) allows you to automate the management of your data based on rules defined by...

1 year ago

[Azure AI Search] Internal Server Error when creating CMK encrypted objects

Scenario Customers follow the Microsoft doc to create CMK encrypted objects (data source, index etc.), but get the 500 Internal Serv...

1 year ago

Optimizing Azure Table Storage: Automated Data Cleanup using a PowerShell script with Azure Automate

Scenario This blog’s aim is to manage Table Storage data efficiently. Imagine you have a large Azure Table Storage that accumulates logs from ...

1 year ago

Optimizing Azure Table Storage: Automated Data Clean-up using a PowerShell script with Azure Automat

Scenario This blog’s aim is to manage Table Storage data efficiently. Imagine you have a large Azure Table Storage that accumulates logs from ...

1 year ago

Restoring Soft-Deleted Blobs with multithreading in Azure Storage Using C#

Blob soft delete is an essential feature that safeguards your data against accidental deletions or overwrites. By retaining deleted data for a...

1 year ago

Performing simple Azure Table Storage REST API operations using curl command.

The blog provides guidance to perform simple Table Storage REST API operations such as Create table, Delete Table, Insert entity, Delete entit...

1 year ago

Bulk delete all the old jobs from the batch account

Deleting a Job also deletes all Tasks that are part of that Job, and all Job statistics. This also overrides the retention period for Task dat...

1 year ago

Utilizing Azure Storage and Runbooks for scheduled automated backups of Azure SQL Databases

In this article, we are going to provide detailed steps to create a scheduled Azure SQL Database backup to storage account using automation. T...

2 years ago

[Azure Service Bus] JMS messages getting dead-lettered

The article discusses a problem where numerous messages end up in the dead letter queue (DLQ) when the JMS service bus consumer connects to th...

2 years ago

Newsletter

Get the latest Dynamics 365 and Power Platform content in your inbox

A curated digest of community blogs, product news, videos, and podcasts — delivered without the noise.

Weekly updates Unsubscribe anytime Fresh community picks
We use your email only for the newsletter and you can unsubscribe at any time.
By subscribing, you agree to the privacy policy.