Access Azure Database for MySQL from Azure functions with SSL Certificate Verification

Image for post


However, I met an issue when I use python to implement this Azure function to connect to the Azure MySQL server. That is, the MySQL has enforced the SSL encryption, but the Azure Function side doesn’t provide a certificate. In our case, disabling the SSL enforcement is not an option because we don’t want to put customer’s data in a risky situation such as man-in-the-middle attacks. Therefore, I’ll need to solve this problem and finally, I did it. Here are the steps to reproduce the issue and how to solve it.

Step 1: Create Azure Database for MySQL

Image for post

To be able to test your Azure Functions on your local machine. Don’t forget to add your local IP address to the connection security settings. Specifically, Go to the Azure MySQL we just created. On the left navigation select Connection security in Settings section. Then on the right main view click + Add client IP button. This will automatically add your current IP address to whitelist. Then, click Save.

Image for post

Step 2: Create Azure Functions

Step 3: Install mysql-connector for Python

$ pip install mysql-connector

Step 4: Write Python code to connect to the MySQL server

Go to the Azure Database for MySQL that you just created. Note down the server name and login name.

Image for post

The code is simply connecting to the MySQL server and show all the databases.

import logging
import azure.functions as func
import mysql.connectordef main(req: func.HttpRequest) -> func.HttpResponse:
logging.info('Python HTTP trigger function processed a request.')
# Connect to MySQL
cnx = mysql.connector.connect(
user="ctao@mysql80-test-chris",
password='<your_password>',
host="mysql80-test-chris.mysql.database.azure.com",
port=3306
)
logging.info(cnx)
# Show databases
cursor = cnx.cursor()
cursor.execute("SHOW DATABASES")
result_list = cursor.fetchall()
# Build result response text
result_str_list = []
for row in result_list:
row_str = ', '.join([str(v) for v in row])
result_str_list.append(row_str)
result_str = '\n'.join(result_str_list)
return func.HttpResponse(
result_str,
status_code=200
)

Step 5: Run Azure Function (Reproducing the problem)

$ func start run
Image for post

Step 6: Add Certificate File and Correctly Use it in Python

crtpath = '../BaltimoreCyberTrustRoot.crt.pem'
crtpath = '/User/.../BaltimoreCyberTrustRoot.crt.pem'
# Connect to MySQL
cnx = mysql.connector.connect(
user="ctao@azure-mysql-test",
password='<your_password>',
host="azure-mysql-test.mysql.database.azure.com",
port=3306,
ssl_ca=crtpath
)
Image for post
import pathlibdef get_ssl_cert():
current_path = pathlib.Path(__file__).parent.parent
return str(current_path / 'BaltimoreCyberTrustRoot.crt.pem')
cnx = mysql.connector.connect(
user="ctao@mysql80-test-chris",
password='<your_password>',
host="mysql80-test-chris.mysql.database.azure.com",
port=3306,
ssl_ca=get_ssl_cert()
)

Let’s try it now!

Image for post

Comments