Connecting to Azure SQL

2022-05-08

Expected to be trivial experience of connecting to Azure SQL in Python and a lesser extent Java using SPN took more effort than anticipated.

Recent version of MS SQL ODBC driver(Python needs for connectivity) and JDBC driver(Java) support two ways of doing it:

  1. Using access token that you need to acquire separately
  2. Providing SPN credentials to the driver and letting it worry about the token(ActiveDirectoryServicePrincipal). This feature has been supported since ODBC driver 17.7 and JDBC 9.2

Using Java was by far the simpler of the two. All you need is a Microsoft JDBC driver and msal library dependencies. Have a look here.

Python presented more of challenge. You need unixODBC, MS SQL ODBC drivers as well as python packages specified in requirements.txt in my examples. Working samples are here for python 3 and here for python 2. I only implemented ActiveDirectoryServicePrincipal approach in Python 2, but if you are really stuck with this long EOLed version, consider going through this

Few things that took some time