Unable to connect to azure sql database via user managed identity withy python

Obaid Ur Rehman 86 Reputation points
2023-01-25T14:52:33.8433333+00:00

Hi,

I have an azure sql database in a one subscription. Iam using my AML compute for development (which is in other subscription). A user managed identity is attached to my compute.

User's image

A user name with exactly the same name as the identity is added to the SQL:image

In firewall setting, a rule is added with my computer public Ip Address:

ip

The user managed identity is in AAD:

MicrosoftTeams-image

NOTE: The azure SQL database and servers are in a VNET.

I am using the following code to access the database:

connstr = f"Driver={{ODBC Driver 18 for SQL Server}};Server={server};Database={database};Authentication=ActiveDirectoryMsi;UID=xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxx63;"

conn = pyodbc.connect(connstr)

But in odbc.logs I see following errors:

User's image

The error basically says that It fails to authenticate the user UID (which is basically the object ID of the managed identity) in Active Directory. Because couldn't get the access token.

Can someone please help?

Azure SQL Database
Microsoft Security | Microsoft Entra | Microsoft Entra ID
{count} votes

1 answer

Sort by: Most helpful
  1. Martin Cairney 2,266 Reputation points
    2023-01-26T06:17:01.2633333+00:00

    Hi @Obaid Ur Rehman

    I did a search on the error number returned in your code and it led to this GitHub bug report [https://github.com/Azure/azure-functions-python-worker/issues/664

    The discussion does not seem to indicate a complete resolution although there is a work around identified. I suggest you try the workaround and post back if that works for you.

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.