Migrate a Python application to use passwordless connections with Azure SQL Database

Applies to: Azure SQL Database

Application requests to Azure SQL Database must be authenticated. Although there are multiple options for authenticating to Azure SQL Database, you should prioritize passwordless connections in your applications when possible. Traditional authentication methods that use passwords or secret keys create security risks and complications. Visit the passwordless connections for Azure services hub to learn more about the advantages of moving to passwordless connections. The following tutorial explains how to migrate an existing Python application to connect to Azure SQL Database to use passwordless connections instead of a username and password solution.

Configure the Azure SQL Database

Passwordless connections use Microsoft Entra authentication to connect to Azure services, including Azure SQL Database. Microsoft Entra authentication, you can manage identities in a central location to simplify permission management. Learn more about configuring Microsoft Entra authentication for your Azure SQL Database:

For this migration guide, ensure you have a Microsoft Entra admin assigned to your Azure SQL Database.

  1. Navigate to the Microsoft Entra page of your logical server.

  2. Select Set admin to open the Microsoft Entra ID flyout menu.

  3. In the Microsoft Entra ID flyout menu, search for the user you want to assign as admin.

  4. Select the user and choose Select.

    A screenshot showing how to enable Microsoft Entra admin.

Configure your local development environment

Passwordless connections can be configured to work for both local and Azure hosted environments. In this section, you apply configurations to allow individual users to authenticate to Azure SQL Database for local development.

Sign-in to Azure

For local development, make sure you're signed-in with the same Azure AD account you want to use to access Azure SQL Database. You can authenticate via popular development tools, such as the Azure CLI or Azure PowerShell. The development tools with which you can authenticate vary across languages.

Sign-in to Azure through the Azure CLI using the following command:

az login

Create a database user and assign roles

Create a user in Azure SQL Database. The user should correspond to the Azure account you used to sign-in locally in the Sign-in to Azure section.

  1. In the Azure portal, browse to your SQL database and select Query editor (preview).

  2. Select Continue as <your-username> on the right side of the screen to sign into the database using your account.

  3. On the query editor view, run the following T-SQL commands:

    CREATE USER [user@domain] FROM EXTERNAL PROVIDER;
    ALTER ROLE db_datareader ADD MEMBER [user@domain];
    ALTER ROLE db_datawriter ADD MEMBER [user@domain];
    ALTER ROLE db_ddladmin ADD MEMBER [user@domain];
    GO
    

    A screenshot showing how to use the Azure Query editor.

    Running these commands assigns the SQL DB Contributor role to the account specified. This role allows the identity to read, write, and modify the data and schema of your database. For more information about the roles assigned, see Fixed-database roles.

Update the local connection configuration

Existing application code that connects to Azure SQL Database using the Python SQL Driver - pyodbc continues to work with passwordless connections with minor changes. For example, the following code works with both SQL authentication and passwordless connections when running locally and when deployed to Azure App Service.

import os
import pyodbc, struct
from azure.identity import DefaultAzureCredential

connection_string = os.environ["AZURE_SQL_CONNECTIONSTRING"]

def get_all():
    with get_conn() as conn:
        cursor = conn.cursor()
        cursor.execute("SELECT * FROM Persons")
        # Do something with the data
    return

def get_conn():
    credential = DefaultAzureCredential(exclude_interactive_browser_credential=False)
    token_bytes = credential.get_token("https://database.windows.net/.default").token.encode("UTF-16-LE")
    token_struct = struct.pack(f'<I{len(token_bytes)}s', len(token_bytes), token_bytes)
    SQL_COPT_SS_ACCESS_TOKEN = 1256  # This connection option is defined by microsoft in msodbcsql.h
    conn = pyodbc.connect(connection_string, attrs_before={SQL_COPT_SS_ACCESS_TOKEN: token_struct})
    return conn

Tip

In this example code, the App Service environment variable WEBSITE_HOSTNAME is used to determine what environment the code is running in. For other deployment scenarios, you can use other environment variables to determine the environment.

To update the referenced connection string (AZURE_SQL_CONNECTIONSTRING) for local development, use the passwordless connection string format:

Driver={ODBC Driver 18 for SQL Server};Server=tcp:<database-server-name>.database.windows.net,1433;Database=<database-name>;Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30

Test the app

Run your app locally and verify that the connections to Azure SQL Database are working as expected. Keep in mind that it may take several minutes for changes to Azure users and roles to propagate through your Azure environment. Your application is now configured to run locally without developers having to manage secrets in the application itself.

Configure the Azure hosting environment

Once your app is configured to use passwordless connections locally, the same code can authenticate to Azure SQL Database after it's deployed to Azure. The sections that follow explain how to configure a deployed application to connect to Azure SQL Database using a managed identity. Managed identities provide an automatically managed identity in Microsoft Entra ID (formerly Azure Active Directory) for applications to use when connecting to resources that support Microsoft Entra authentication. Learn more about managed identities:

Create the managed identity

Create a user-assigned managed identity using the Azure portal or the Azure CLI. Your application uses the identity to authenticate to other services.

  1. At the top of the Azure portal, search for Managed identities. Select the Managed Identities result.
  2. Select + Create at the top of the Managed Identities overview page.
  3. On the Basics tab, enter the following values:
    • Subscription: Select your desired subscription.
    • Resource group: Select your desired resource group.
    • Region: Select a region near your location.
    • Name: Enter a recognizable name for your identity, such as MigrationIdentity.
  4. Select Review + create at the bottom of the page.
  5. When the validation checks finish, select Create. Azure creates a new user-assigned identity.

After the resource is created, select Go to resource to view the details of the managed identity.

A screenshot showing how to create a managed identity using the Azure portal.

Associate the managed identity with your web app

Configure your web app to use the user-assigned managed identity you created.

Complete the following steps in the Azure portal to associate the user-assigned managed identity with your app. These same steps apply to the following Azure services:

  • Azure Spring Apps
  • Azure Container Apps
  • Azure virtual machines
  • Azure Kubernetes Service
  • Navigate to the overview page of your web app.
  1. Select Identity from the left navigation.

  2. On the Identity page, switch to the User assigned tab.

  3. Select + Add to open the Add user assigned managed identity flyout.

  4. Select the subscription you used previously to create the identity.

  5. Search for the MigrationIdentity by name and select it from the search results.

  6. Select Add to associate the identity with your app.

    A screenshot showing how to assign a managed identity.

Create a database user for the identity and assign roles

Create a SQL database user that maps back to the user-assigned managed identity. Assign the necessary SQL roles to the user to allow your app to read, write, and modify the data and schema of your database.

  1. In the Azure portal, browse to your SQL database and select Query editor (preview).

  2. Select Continue as <username> on the right side of the screen to sign into the database using your account.

  3. On the query editor view, run the following T-SQL commands:

    CREATE USER [user-assigned-identity-name] FROM EXTERNAL PROVIDER;
    ALTER ROLE db_datareader ADD MEMBER [user-assigned-identity-name];
    ALTER ROLE db_datawriter ADD MEMBER [user-assigned-identity-name];
    ALTER ROLE db_ddladmin ADD MEMBER [user-assigned-identity-name];
    GO
    

    A screenshot showing how to use the Azure Query editor to create a SQL user for a managed identity.

    Running these commands assigns the SQL DB Contributor role to the user-assigned managed identity. This role allows the identity to read, write, and modify the data and schema of your database.


Important

Use caution when assigning database user roles in enterprise production environments. In those scenarios, the app shouldn't perform all operations using a single, elevated identity. Try to implement the principle of least privilege by configuring multiple identities with specific permissions for specific tasks.

You can read more about configuring database roles and security on the following resources:

Update the connection string

Update your Azure app configuration to use the passwordless connection string format. The format should be the same used in your local environment.

Connection strings can be stored as environment variables in your app hosting environment. The following instructions focus on App Service, but other Azure hosting services provide similar configurations.

Driver={ODBC Driver 18 for SQL Server};Server=tcp:<database-server-name>.database.windows.net,1433;Database=<database-name>;Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30

<database-server-name> is the name of your Azure SQL Database server and <database-name> is the name of your Azure SQL Database.

Create an app setting for the managed identity client ID

To use the user-assigned managed identity, create an AZURE_CLIENT_ID environment variable and set it equal to the client ID of the managed identity. You can set this variable in the Configuration section of your app in the Azure portal. You can find the client ID in the Overview section of the managed identity resource in the Azure portal.

Save your changes and restart the application if it doesn't do so automatically.

Note

The example connection code shown in this migration guide uses the DefaultAzureCredential class when deployed. Specifically, it uses the DefaultAzureCredential without passing the user-assigned managed identity client ID to the constructor. In this scenario, the fallback is to check for the AZURE_CLIENT_ID environment variable. If the AZURE_CLIENT_ID environment variable doesn't exist, a system-assigned managed identity will be used if configured.

If you pass the managed identity client ID in the DefaultAzureCredential constructor, the connection code can still be used locally and deployed because the authentication process falls back to interactive authentication in the local scenario. For more information, see the Azure Identity client library for Python.

Test the application

Test your app to make sure everything is still working. It may take a few minutes for all of the changes to propagate through your Azure environment.

Next steps

In this tutorial, you learned how to migrate an application to passwordless connections.

You can read the following resources to explore the concepts discussed in this article in more depth: