Hush Line Security Spotlight: Securing Database Connections with SSL

Using Let’s Encrypt to secure your database connection

Science & Design
2 min readFeb 25, 2024

Ensuring the confidentiality and integrity of data is paramount for Hush Line, our lightweight, secure, and anonymous tip line-as-a-service. One critical aspect of maintaining this security is fortifying the connection between our web service and its underlying database. Utilizing SSL (Secure Sockets Layer) is a key strategy for keeping data transmissions over the network encrypted and safe from potential interception. This guide offers a straightforward approach to implementing SSL encryption for MariaDB database connections within a Flask application, using our recent Hush Line upgrades as a working example.

The Importance of SSL

SSL encryption helps protect data in transit between your application and database from eavesdropping, tampering, and message forgery. For applications dealing with sensitive information, such as personal data, financial transactions, or login credentials, SSL isn’t just an option; it’s a necessity.

Step-by-Step SSL Setup for MariaDB and Flask

Here’s how you can secure your Flask app’s MariaDB database connections with SSL:

1. Prepare SSL Certificates

First, obtain an SSL certificate and private key for your server. This can be done through a Certificate Authority (CA) like Let’s Encrypt. Once you have your certificates, copy them and update their permissions so MariaDB may access them:

# Create a directory for MariaDB SSL and copy certificates
sudo mkdir -p /etc/mariadb/ssl
sudo cp /etc/letsencrypt/live/$DOMAIN/fullchain.pem /etc/mariadb/ssl/
sudo cp /etc/letsencrypt/live/$DOMAIN/privkey.pem /etc/mariadb/ssl/

# Set ownership and permissions
sudo chown mysql:mysql /etc/mariadb/ssl/fullchain.pem /etc/mariadb/ssl/privkey.pem
sudo chmod 400 /etc/mariadb/ssl/fullchain.pem /etc/mariadb/ssl/privkey.pem

2. Configure MariaDB for SSL

Next, update the MariaDB configuration to use the SSL certificates:

# MariaDB configuration file path
MY_CNF="/etc/mysql/my.cnf"

# Append SSL configuration to the MariaDB configuration file
echo "ssl_cert=/etc/mariadb/ssl/fullchain.pem" | sudo tee -a $MY_CNF > /dev/null
echo "ssl_key=/etc/mariadb/ssl/privkey.pem" | sudo tee -a $MY_CNF > /dev/null

# Restart MariaDB to apply the new configuration
sudo systemctl restart mariadb

3. Update Your Flask Application

Finally, ensure your Flask application is configured to use SSL for database connections:

ssl_cert = "/etc/mariadb/ssl/fullchain.pem"
ssl_key = "/etc/mariadb/ssl/privkey.pem"

# Ensure SSL files exist
if not all(os.path.exists(path) for path in [ssl_cert, ssl_key]):
raise FileNotFoundError("SSL certificate or key file is missing.")

# SQLAlchemy database URI with SSL configuration
app.config["SQLALCHEMY_DATABASE_URI"] = (
f"mysql+pymysql://{db_user}:{db_pass}@localhost/{db_name}"
"?ssl_cert={ssl_cert}&ssl_key={ssl_key}".format(ssl_cert=ssl_cert, ssl_key=ssl_key)
)

This configuration ensures that your Flask app’s connection to its MariaDB database is encrypted using SSL, protecting it from potential eavesdropping and data tampering.

Conclusion

Implementing SSL encryption for your database connections is a crucial step in securing your application’s data. While it might seem daunting initially, the process is straightforward with the proper guidance. By following the steps outlined above, you can significantly enhance the security of your data transmissions, ensuring that your application’s data remains confidential and integral.

--

--

Science & Design
Science & Design

Written by Science & Design

👋 We’re a non-profit design and software development organization. Let’s make something great together! https://scidsg.org

No responses yet