|
| 1 | +--- |
| 2 | +title: Azure Active Directory server principals |
| 3 | +description: Using Azure Active Directory server principals (logins) in Azure SQL |
| 4 | +ms.service: sql-db-mi |
| 5 | +ms.subservice: security |
| 6 | +ms.topic: conceptual |
| 7 | +author: GithubMirek |
| 8 | +ms.author: mireks |
| 9 | +ms.reviewer: vanto |
| 10 | +ms.date: 03/11/2022 |
| 11 | +--- |
| 12 | + |
| 13 | +# Azure Active Directory server principals |
| 14 | + |
| 15 | +[!INCLUDE[appliesto-sqldb-sqlmi-asa-dedicated-only](../includes/appliesto-sqldb-sqlmi-asa-dedicated-only.md)] |
| 16 | + |
| 17 | +> [!NOTE] |
| 18 | +> Azure Active Directory (Azure AD) server principals (logins) are currently in public preview for Azure SQL Database. Azure SQL Managed Instance can already utilize Azure AD logins. |
| 19 | +
|
| 20 | +You can now create and utilize Azure AD server principals, which are logins in the master database of a SQL Database. There are several benefits of using Azure AD server principals for SQL Database: |
| 21 | + |
| 22 | +- Support multiple Azure AD login accounts with high privileged server roles for SQL Database, such as the `loginmanager` and `dbmanager` roles. |
| 23 | +- Increase functional improvement support, such as utilizing [Azure AD-only authentication](authentication-azure-ad-only-authentication.md). Azure AD-only authentication allows SQL authentication to be disabled, which includes the SQL server admin, SQL logins, and users. |
| 24 | +- Allows Azure AD principals to support geo-replicas. Azure AD principals will be able to connect to the geo-replica of a user database, with a *read-only* permission and *deny* permission to the primary server. |
| 25 | +- Ability to use Azure AD service principal logins with high privilege server roles to execute a full automation of user and database creation, as well as maintenance provided by Azure AD applications. |
| 26 | +- Closer functionality between Managed Instance and SQL Database, as Managed Instance already supports Azure AD logins in the master database. |
| 27 | + |
| 28 | +For more information on Azure AD authentication in Azure SQL, see [Use Azure Active Directory authentication](authentication-aad-overview.md) |
| 29 | + |
| 30 | +## Permissions |
| 31 | + |
| 32 | +The following permissions are required to utilize or create Azure AD logins in the master database. |
| 33 | + |
| 34 | +- Azure AD admin permission or membership in the `loginmanager` server role. |
| 35 | +- Must be a member of Azure AD within the same directory used for Azure SQL Database |
| 36 | + |
| 37 | +By default, the standard permission granted to newly created Azure AD login in the `master` database is **VIEW ANY DATABASE**. |
| 38 | + |
| 39 | +## Azure AD logins syntax |
| 40 | + |
| 41 | +New syntax for Azure SQL Database to use Azure AD server principals has been introduced with this feature release. |
| 42 | + |
| 43 | +### Create login syntax |
| 44 | + |
| 45 | +```syntaxsql |
| 46 | +CREATE LOGIN login_name { FROM EXTERNAL PROVIDER [WITH OBJECT_ID = 'objectid'] | WITH <option_list> [,..] } |
| 47 | +
|
| 48 | +<option_list> ::= |
| 49 | + PASSWORD = {'password'} |
| 50 | + | , SID = sid, ] |
| 51 | +``` |
| 52 | + |
| 53 | +For more information, see [CREATE LOGIN (Transact-SQL)](/sql/t-sql/statements/create-login-transact-sql?view=azuresqldb-current&preserve-view=true). |
| 54 | + |
| 55 | +### Create user syntax |
| 56 | + |
| 57 | +The below T-SQL syntax is already available in SQL Database, and can be used for creating database-level Azure AD principals mapped to Azure AD logins in the master database. |
| 58 | + |
| 59 | +To create an Azure AD user from an Azure AD login, use the following syntax: |
| 60 | + |
| 61 | +```syntaxsql |
| 62 | +CREATE USER user_name FROM LOGIN login_name |
| 63 | +``` |
| 64 | + |
| 65 | +For more information, see [CREATE USER (Transact-SQL)](/sql/t-sql/statements/create-user-transact-sql). |
| 66 | + |
| 67 | +### Disable or enable a login using ALTER LOGIN syntax |
| 68 | + |
| 69 | +The [ALTER LOGIN (Transact-SQL)](/sql/t-sql/statements/alter-login-transact-sql?view=azuresqldb-current&preserve-view=true) DDL syntax can be used to enable or disable an Azure AD login in Azure SQL Database. |
| 70 | + |
| 71 | +```syntaxsql |
| 72 | +ALTER LOGIN login_name DISABLE |
| 73 | +``` |
| 74 | + |
| 75 | +The Azure AD principal `login_name` won't be able to log into any user database in the SQL Database server where an Azure AD user principal, `user_name` mapped to login `login_name` was created. |
| 76 | + |
| 77 | +> [!NOTE] |
| 78 | +> - `ALTER LOGIN login_name DISABLE` is not supported for contained users. |
| 79 | +> - `ALTER LOGIN login_name DISABLE` is not supported for Azure AD groups. |
| 80 | +> - An individual disabled login cannot belong to a user who is part of a login group created in the master database (for example, an Azure AD admin group). |
| 81 | +> - For the `DISABLE` or `ENABLE` changes to take immediate effect, the authentication cache and the **TokenAndPermUserStore** cache must be cleared using the T-SQL commands. |
| 82 | +> |
| 83 | +> ```sql |
| 84 | +> DBCC FLUSHAUTHCACHE |
| 85 | +> DBCC FREESYSTEMCACHE('TokenAndPermUserStore') WITH NO_INFOMSGS |
| 86 | +> ``` |
| 87 | +
|
| 88 | +## Azure AD logins and users with non-unique display names |
| 89 | +
|
| 90 | +It's possible to create Azure AD resources with the same display names. For example, creating an [Azure AD application (service principal)](authentication-aad-service-principal.md) with the same name. In this release, we're also introducing the ability to create logins and users using the **Object ID**. |
| 91 | +
|
| 92 | +```sql |
| 93 | +CREATE LOGIN login_name FROM EXTERNAL PROVIDER WITH OBJECT_ID = 'objectid' |
| 94 | +``` |
| 95 | +
|
| 96 | +- To execute the above query, the specified Object ID must exist in Azure AD where the Azure SQL resides. |
| 97 | +- Most non-unique display names in Azure AD are related to service principals. Group names can also be non-unique as well. All Azure AD user display names are unique. |
| 98 | + |
| 99 | +Using the display name of a service principal that isn't unique in Azure AD could lead to errors when creating the login or user in Azure SQL. For example, if `myapp` isn't unique, you may run into the following error when executing the following query: |
| 100 | + |
| 101 | +```sql |
| 102 | +CREATE USER [myapp] FROM EXTERNAL PROVIDER |
| 103 | +``` |
| 104 | + |
| 105 | +```output |
| 106 | +Msg 33131, Level 16, State 1, Line 4 |
| 107 | +Principal 'myapp' has a duplicate display name. Make the display name unique in Azure Active Directory and execute this statement again. |
| 108 | +``` |
| 109 | + |
| 110 | +With the T-SQL DDL extension to create logins or users with the Object ID, you can avoid this error and also specify an alias for the login or user created with the Object ID. For example, the following will create a user `myapp4466e` using the application Object ID `4466e2f8-0fea-4c61-a470-xxxxxxxxxxxx`. |
| 111 | + |
| 112 | +```sql |
| 113 | +CREATE USER [myapp4466e] FROM EXTERNAL PROVIDER |
| 114 | + WITH OBJECT_ID='4466e2f8-0fea-4c61-a470-xxxxxxxxxxxx' |
| 115 | +``` |
| 116 | + |
| 117 | +For more information on obtaining the Object ID of a service principal, see [Service principal object](/azure/active-directory/develop/app-objects-and-service-principals#service-principal-object.) |
| 118 | + |
| 119 | +To get the Object ID of the application, you can execute the following query: |
| 120 | + |
| 121 | +```sql |
| 122 | +SELECT CAST(sid as uniqueidentifier) ApplicationID from sys.database_principals WHERE NAME = 'myapp4466e' |
| 123 | +``` |
| 124 | + |
| 125 | +## Limitations and remarks |
| 126 | + |
| 127 | +- The SQL server admin can’t create Azure AD logins in the master database |
| 128 | +- Changing a database ownership to an Azure AD group as database owner isn't supported. |
| 129 | + - `ALTER AUTHORIZATION ON database::<mydb> TO [my_aad_group]` fails with an error message: |
| 130 | + ```output |
| 131 | + Msg 33181, Level 16, State 1, Line 4 |
| 132 | + The new owner cannot be Azure Active Directory group. |
| 133 | + ``` |
| 134 | + - Changing a database ownership to an individual user is supported. |
| 135 | +- A SQL admin or SQL user can’t execute the following Azure AD operations: |
| 136 | + - `CREATE LOGIN [[email protected]] FROM EXTERNAL PROVIDER` |
| 137 | + - `CREATE USER [[email protected]] FROM EXTERNAL PROVIDER` |
| 138 | + - `EXECUTE AS USER [[email protected]]` |
| 139 | + - `ALTER AUTHORIZATION ON securable::name TO [[email protected]]` |
| 140 | +- Impersonation of Azure AD server-level principals (logins) isn't supported: |
| 141 | + - [EXECUTE AS Clause (Transact-SQL)](/sql/t-sql/statements/execute-as-clause-transact-sql) |
| 142 | + - [EXECUTE AS (Transact-SQL)](/sql/t-sql/statements/execute-as-transact-sql) |
| 143 | + - Impersonation of Azure AD database-level principals (users) at a user database-level is still supported. |
| 144 | +- Azure AD logins overlapping with Azure AD administrator aren't supported. Azure AD admin takes precedence over any login. If an Azure AD account already has access to the server as an Azure AD admin, either directly or as a member of the admin group, the login created for this user won't have any effect. The login creation isn't blocked through T-SQL. After the account authenticates to the server, the login will have the effective permissions of an Azure AD admin, and not of a newly created login. |
| 145 | +- Changing permissions on specific Azure AD login object isn't supported: |
| 146 | + - `GRANT <PERMISSION> ON LOGIN :: <Azure AD account> TO <Any other login> ` |
| 147 | +- When permissions are altered for an Azure AD login with existing open connections to an Azure SQL Database, permissions aren't effective until the user reconnects. This applies to server role membership change using the [ALTER SERVER ROLE](/sql/t-sql/statements/alter-server-role-transact-sql) statement. |
| 148 | +- [SQL Server Management Studio (SSMS)](/sql/ssms/download-sql-server-management-studio-ssms) doesn't display the login names in **Object Explorer**. |
| 149 | +
|
| 150 | +## Next steps |
| 151 | +
|
| 152 | +> [!div class="nextstepaction"] |
| 153 | +> [Tutorial: Create and utilize Azure Active Directory server logins](authentication-azure-ad-logins-tutorial.md) |
0 commit comments