Tutorial: Using automation to set up the Azure Active Directory admin for SQL Server

Applies to: SQL Server 2022 (16.x)

In this article, we'll go over how to set up the Azure Active Directory (Azure AD) admin to allow Azure AD authentication for SQL Server using the Azure portal, and APIs such as:

  • PowerShell
  • The Azure CLI
  • ARM Template

We'll also go over the updated functionality to set up an Azure AD admin for SQL Server in the Azure portal that would allow for automated certificate creation and application registration. Previously, setting up Azure AD authentication for SQL server required manual setup of Azure AD admin with an Azure certificate and application registration.

Note

This feature is available in SQL Server 2022 (16.x) or later versions, and is only supported for SQL Server on-premises, for Windows and Linux hosts and SQL Server 2022 on Windows Azure VMs.

Prerequisites

Preparation before setting the Azure AD admin

The following permissions are necessary to set up Azure AD admin in the SQL Server – Azure Arc and Key vault resources.

Configure permissions for Azure Arc

Follow the guide to make sure your SQL Server is connected to Azure Arc. The user setting up Azure AD admin for the SQL Server – Azure Arc resource should have the Contributor role for the server.

  1. Go to the Azure portal
  2. Select SQL Server – Azure Arc, and select the instance for your SQL Server host.
  3. Select Access control (IAM).
  4. Select Add > Add role assignment to add the Contributor role to the user setting up the Azure AD admin.

Configure permissions for Azure Key Vault

Create an Azure Key Vault if you don't already have one. The user setting up Azure AD admin should have the Contributor role for your Azure Key Vault. To add a role to a user in Azure Key Vault:

  1. Go to the Azure portal
  2. Go to your Key vault resource.
  3. Select the Access control (IAM).
  4. Select Add > Add role assignment to add the Contributor role to the user setting up the Azure AD admin.

Set access policies for the SQL Server host

  1. In the Azure portal, navigate to your Azure Key Vault instance, and select Access policies.

  2. Select Add Access Policy.

  3. For Key permissions, use 0 selected.

  4. For Secret permissions, select Get and List.

  5. For Certificate permissions, select Get and List.

  6. For Select principal, use the account for your Azure Arc instance, which is the hostname of the SQL Server host.

  7. Select Add and then select Save.

    You must Save to ensure the permissions are applied. They aren't applied after selecting Add. To ensure permissions have been stored, refresh the browser window, and check the row for your Azure Arc instance is still present.

Set access policies for Azure AD users

  1. In the Azure portal, navigate to your Azure Key Vault instance, and select Access policies.
  2. Select Add Access Policy.
  3. For Key permissions, select Get, List, and Create.
  4. For Secret permissions, select Get, List, and Set.
  5. For Certificate permissions, select Get, List, and Create.
  6. For Select principal, add the Azure AD user you want to use to connect to SQL Server.
  7. Select Add and then select Save.

Setting up the Azure AD admin for the SQL Server

New APIs and portal functionality allows users to set up an Azure AD admin for SQL Server without having to separately create an Azure certificate and Azure AD application. Select a tab to learn how to set up an Azure AD admin for your SQL Server connected to Azure Arc with automatic certificate and application creation.

Note

The ARM template still requires the creation of an Azure Key Vault certificate and Azure AD application before setting up an Azure AD admin. For more information on this process, see Tutorial: Set up Azure Active Directory authentication for SQL Server.

A new Azure portal functionality is enabled to set up an Azure AD admin, and creates an Azure Key Vault certificate and Azure AD application in the same process. This is necessary to use Azure AD authentication with SQL Server.

Note

Previously, before setting up an Azure AD admin, an Azure Key Vault certificate and Azure AD application registration was needed. This is no longer the case with this new functionality. Users can still choose to provide their own certificate and application to set up the Azure AD admin.

Setting up Azure AD admin using the Azure portal

  1. Go to the Azure portal, and select SQL Server – Azure Arc. Select the instance for your SQL Server host.

  2. Check the status of your SQL Server - Azure Arc resource and see if it's connected by going to the Properties menu. For more information, see Validate your Arc-enabled SQL Server resources.

  3. Select Azure Active Directory on the left-hand column.

  4. Select Set Admin, and choose an account that will be added as an admin login to SQL Server.

  5. Select Service-managed cert.

  6. Select Change key vault and select your existing Azure Key vault resource.

  7. Select Service-managed app registration.

  8. Select Save. This will send a request to the Arc server agent, which will configure Azure AD authentication for that SQL Server instance. After saving, the operation can take several minutes to complete. Wait until the save process is confirmed with Saved successfully, before attempting an Azure AD login.

    The following actions are applied after you save:

    • Create a certificate in your key vault with a name like <hostname>-<instanceName><uniqueNumber>.
    • Create an Azure AD application with a name like <hostname>-<instanceName><uniqueNumber>, and assign the necessary permissions for that application. For more information, see Grant application permissions
    • Assign the certificate from the Azure Key Vault to the application.
    • Saves these settings to Azure Arc.

    Screenshot of setting Azure Active Directory authentication with automatic certificate and application generation in the Azure portal.

Note

The certificates created for the Azure AD setup are not rotated automatically. Customers can choose to provide their own certificate and application for the Azure AD admin setup. For more information, see Tutorial: Set up Azure Active Directory authentication for SQL Server.

Once the Azure AD admin has been set up, using the Azure AD admin credentials allows you to connect to SQL Server. However, any further database activities involving creating new Azure AD logins and users will fail until admin consent is granted to the Azure AD application.

Note

To grant Admin consent for the application, the account granting consent requires a role of Azure AD Global Administrator or Privileged Role Administrator. These roles are necessary to grant admin consent for the application, but is not necessary to set up Azure AD admin.

  1. In the Azure portal, select Azure Active Directory > App registrations, select the newly created application. The application should have a name like <hostname>-<instanceName><uniqueNumber>.

  2. Select the API permissions menu.

  3. Select Grant admin consent.

    Screenshot of application permissions in the Azure portal.

Without granting admin consent to the application, creating an Azure AD login or user in SQL Server will result in the following error:

Msg 37455, Level 16, State 1, Line 2
Server identity does not have permissions to access MS Graph.

Using Azure AD authentication to connect to SQL Server

Azure AD authentication is now set up for your SQL Server that is connected to Azure Arc. Follow the sections after setting up Azure AD admin in the article, Tutorial: Set up Azure Active Directory authentication for SQL Server to connect to SQL Server using Azure AD authentication.

See also