Azure SQL Database Setup, Authentication and User Permissions#
This document provides a comprehensive guide to setting up and managing Azure SQL Database with a focus on using Entra ID authentication. It covers the essential steps for creating an Azure SQL Database, configuring Azure SQL Server security controls such as enabling Entra ID-only authentication and restricting access to selected networks, mapping Entra ID users to specific Azure SQL DB roles like db_owner
, db_datawriter
, and db_datareader
, and verifying if corporate email users exist in the database while granting appropriate permissions to them.
If the Azure SQL Database is already set up, you can skip directly to Section 3 to manage user DB permissions.
1. Creating an Azure SQL Database#
To create an Azure SQL Database, follow the steps in Azure SQL DB Creation Guide.
During the creation of the Azure SQL DB, you can either create a new Azure SQL Server or select an existing one.
2. Configuring Azure SQL Server security controls#
Once the Azure SQL DB is created, the associated Azure SQL Server needs to be configured. Security controls must be applied as follows to ensure Microsoft security best practices:
a. Entra ID Only Authentication#
As per best security practices, it is recommended to use Entra ID Only Authentication for Azure SQL DB, rather than username/password-based authentication.
You can find detailed steps in the official documentation here.
Steps:
Go to your Azure SQL Server instance -> Settings -> Microsoft Entra ID.
Check the box that says “Support only Microsoft Entra authentication for this server.”
Hit Save
b. Selected Networks#
For enhanced security, it is recommended to restrict access to selected public IP addresses.
Steps:
Go to Azure SQL Server instance -> Security -> Networking.
Enable Selected Networks and create firewall rules with the public IP address of the user who will connect to the database.
Alternatively, you can use your organization’s VPN CIDR IP ranges so that users can connect through VPN and access the database seamlessly.
3. Azure SQL DB Roles and Entra ID Authentication#
In PyRIT, we currently support Entra ID Only Authentication for connecting to Azure SQL Database.
Even when users authenticate via Entra ID, they must be explicitly mapped to specific database roles to perform database operations.
Key Database Roles:#
db_owner: Provides full control over the Azure SQL Database. Users with this role can create, modify, and delete database objects, manage security, and grant/revoke permissions to other users.
db_datawriter: Allows users to insert, update, and delete data but does not permit modifying the database schema or managing users.
db_datareader: Grants read-only access to all tables and views in the database.
Mapping Entra ID Users to Database Roles#
To grant users access to the database, you must map them to the appropriate role. Developers/maintainers should be assigned the db_owner
role, while operators can be assigned the db_datawriter
role.
Example: Mapping Entra ID Users#
-- Create a database user for the Entra ID user
CREATE USER [user@domain.com] FROM EXTERNAL PROVIDER;
-- Map the user to the db_datawriter role
ALTER ROLE db_datawriter ADD MEMBER [user@domain.com];
user@domain.com
could be corporate email address, such as abc@microsoft.com
, which is linked to Entra ID.
4. Checking If a Corporate Email Address Exists#
To verify if a specific corporate email address (Entra ID user) exists in the database, you can run the following query from the Query Editor:
SELECT u.name AS UserName, u.type_desc AS UserType, r.name AS RoleName
FROM sys.database_principals AS u
LEFT JOIN sys.database_role_members AS rm ON rm.member_principal_id = u.principal_id
LEFT JOIN sys.database_principals AS r ON r.principal_id = rm.role_principal_id
WHERE u.type NOT IN('R', 'G') -- Exclude roles and groups
ORDER BY UserName, RoleName;
UserName: The user’s email address.
UserType: Type of user (e.g.,
EXTERNAL_USER
for Entra ID users).RoleName: The role assigned to the user in the database.
Example Output:#
UserName |
UserType |
RoleName |
---|---|---|
EXTERNAL_USER |
db_datawriter |
|
EXTERNAL_USER |
db_owner |
5. Granting Permissions to a New User#
Determine whether the user needs the
db_owner
ordb_datawriter
role.db_owner is recommended for developers and maintainers.
db_datawriter is recommended for operators interacting with the database.
Run the following commands from the query editor:
-- Create a database user for the Entra ID user
CREATE USER [user@domain.com] FROM EXTERNAL PROVIDER;
-- Map the user to the required role
ALTER ROLE db_datawriter ADD MEMBER [user@domain.com];
Verify the permissions by running the above Checking If a Corporate Email Address Exists query again in the Query Editor.
6. Configure Local Environment#
Connecting PyRIT to an Azure SQL Server database requires ODBC, PyODBC and Microsoft’s ODBC Driver for SQL Server to be installed in your local environment. Consult PyODBC’s documentation for detailed instruction on.
7. Testing#
After assigning the right permissions, the user can test executing the azure sql demo code by connecting through Azure.