Sub topic 10
Encrypting Database Connections with TLS
This chapter assumes you can edit service configuration files with sudo, understand private and public IP addresses, and know basic SQL authentication. You should also understand that a certificate authority (CA) signs certificates so clients can verify a server's identity.
What TLS protects
Transport Layer Security (TLS) encrypts data while it travels between a database client and server. It protects passwords, SQL statements, and query results from network observers. TLS does not decide whether a database user has permission to read a table. Authentication and authorization still apply.
A secure connection needs three things:
- The server has a private key and a certificate.
- The client trusts the CA that signed the certificate.
- The client verifies that the certificate name matches the server name it connects to.
The certificate should contain the database DNS name in its Subject Alternative Name field. Connecting to an IP address requires that IP address to appear there instead.
Configure PostgreSQL
Create or obtain a certificate and key from your organization's CA. For a temporary lab, a self-signed certificate can demonstrate encryption, but clients must explicitly trust it and it does not prove the server's identity.
PostgreSQL commonly reads these files from its data directory. The exact path differs by distribution.
# postgresql.conf
ssl = on
ssl_cert_file = 'server.crt'
ssl_key_file = 'server.key'
# pg_hba.conf
hostssl appdb appuser 10.0.0.0/24 scram-sha-256The hostssl rule permits only TLS connections for the matching database, user, and network. Place it before broader host rules. Set the private key owner and permissions so only the database service account can read it.
Reload or restart the service according to the setting change, then connect with certificate verification:
psql "host=db.example.internal dbname=appdb user=appuser sslmode=verify-full sslrootcert=/etc/ssl/certs/company-ca.crt"verify-full checks both the CA signature and the hostname. Avoid sslmode=require when identity verification matters because it encrypts the session without requiring a matching certificate name.
Configure MySQL
In the [mysqld] section, specify the server certificate, private key, and trusted CA:
[mysqld]
require_secure_transport = ON
ssl-ca = /etc/mysql/tls/ca.crt
ssl-cert = /etc/mysql/tls/server.crt
ssl-key = /etc/mysql/tls/server.keyAfter restarting MySQL, require TLS for an account as an additional safeguard:
ALTER USER 'appuser'@'10.0.0.%' REQUIRE SSL;A client must provide the CA and verify the server identity. Exact client options vary by connector, so configure the equivalent of CA verification and hostname verification in the application driver.
TLS adds CPU work and certificate-management tasks. In practice, use TLS for remote connections, especially across untrusted networks. Keep firewall rules and least-privilege database accounts as separate controls.
Exercise
A PostgreSQL client reports that the certificate is valid but the hostname does not match. What should you check?
Show answer
Check that the client connects using a DNS name listed in the certificate's Subject Alternative Name field, or issue a certificate containing the name the client actually uses.