Nov 032015

Azure SQL Database is a Platform as a Service (PaaS) that provides a relational database for use over the internet. That sounds super cool and easy to use. But wait, there’s one word I’d like to highlight in that first sentence: “internet”. Anyone with an internet connection could access your database. Now that’s no cool. So how does Microsoft keep your database safe? The answer is a multipronged approach of using encryption, authentication, authorization, and firewalls.

All connections to Azure SQL Database use SSL/TLS to protect your data while “in transit”, and you can use Transparent Data Encryption (TDE) to protect your data “at rest”. Authentication and authorization are no different from the on premise version of SQL Server. Authentication just means you must have a valid login to SQL Server, and authorization means you must have permissions on an object; for example, SELECT permission on a TABLE.

The other way Azure protects your data is by use of a firewall. It works like any other firewall; it blocks unauthorized traffic from passing through. By default, Azure blocks ALL traffic to your database. This may sound a bit crazy but it’s no different from the way the Windows firewall works; you must allow access through a port. In Azure SQL Database, it always listens on port 1433, so Azure uses the client’s IP address to authorize access. For example, if your workstation IP address is, then you would need to explicitly allow access to your database from that IP.

Azure SQL Database has several ways to configure the firewall. Access can be granted at the server-level or at the database-level. Each level can be managed through the Azure Portal, TSQL, PowerShell, or Rest API. Let’s take a closer look at the Azure Portal and TSQL options.

Configuring the firewall through the Azure Portal offers you two options; one will allow access from any Azure service, and the other will define server-level entries. The first is the “Allow access to Azure services” button. When this option is turned on, it allows any traffic from services within your Azure subscription to pass through. This is usually on by default when your SQL Server is first created.


The second way to manage the firewall through the Azure Portal is by defining server-level entries. On the Firewall settings page, you just need to give the setting a name, and then list the starting and ending range for the IP address. If it’s just a single IP address, then you would use the same value for both. The picture below shows a single entry and a range of IP addresses that are allowed to connect.


When defining the rules through the Azure Portal, you are just creating server-level rules under the hood. If we run a TSQL query against sys.firewall_rules on our SQL Server, we should see 3 entries.


There are the two entries we created: IP Range and Single IP. But there is also a third. That’s the entry for having “Allow access to azure services” set to ON. If we turn that option off and then rerun the TSQL query, we can see that entry for is removed.


If we wanted to create a server-level firewall from TSQL, then we would use the system stored procedure sp_set_firewall_rule.

EXEC sp_set_firewall_rule
     @name = N'TSQL Server Level'
    ,@start_ip_address = ''
    ,@end_ip_address = '';


To delete a server-level firewall rule, you can use the Azure Portal or the system stored procedure sp_delete_firewall_rule.

EXEC sp_delete_firewall_rule
     @name = N'TSQL Server Level';

To create the rule, it’s as easy as running a query using the system stored procedure, sp_set_database_firewall_rule, in the context of the user database.

EXEC sp_set_database_firewall_rule
     @name = N'TSQL Database Level'
    ,@start_ip_address = ''
    ,@end_ip_address = ''

We can query the sys.database_firewall_rules DMV to verify we have successfully created the rule.


Notice, I have added this rule to the DEMO database, which will give the IP address access to only that database. Now let’s try to connect to the database.

What’s this error? My IP address does not have access to the server? But I just added it as a database-level rule.


This error is because I’m attempting to connect to the default database, master, instead of the database, Demo, that I have explicit access. If I change my connection properties, then I will be allowed to connect to the Demo database.


After connecting, the first thing you will notice is there is only one database listed. Because I only have explicit firewall access to the DEMO database, I can’t even see DEMO2 on the list of databases.


To delete a database-level firewall rule, you can use the system stored procedure sp_delete_database_firewall_rule.

EXEC sp_delete_database_firewall_rule
     @name = N'TSQL Database Level';

In the event you lose all access to your database, either by entering the wrong IP address or removing all of them, there is an easy way to get it back through the Azure Portal. On the Firewall settings page, click the “Add client ip” button and click Save. This will add the IP address of your current client to the server-level rules list. Once you have server-level access, you can then connect to the server and correct the database-level rules.



The one thing to remember from all of the firewall settings, is these settings only open ports. There is no setting to deny an IP address.

As you can see, Azure provides several options for configuring the firewall settings for a SQL Database. I also think this is great example of how the Azure Portal can be viewed as nothing more than a different version of Management Studio. It’s just a graphical interface for managing SQL Server databases.

Additional resources:

 Leave a Reply



