How do you enable and verify TLS for database connections?
IntermediateConceptDatabase connections in apps
Answer
Short answer
Configure the client to require TLS and, ideally, verify the server certificate against the CA (for example the AWS RDS or Azure CA bundle). Then confirm on the server side that sessions are actually encrypted.
Client settings
| Stack | Setting |
|---|---|
| PostgreSQL URL / psql | sslmode=require (encrypt) or sslmode=verify-full&sslrootcert=/certs/rds-ca.pem (encrypt + verify) |
| MySQL JDBC | useSSL=true&requireSSL=true&verifyServerCertificate=true (or sslMode=VERIFY_IDENTITY) |
| .NET Npgsql | SSL Mode=VerifyFull;Root Certificate=/certs/ca.pem |
| MongoDB | tls=true&tlsCAFile=/certs/ca.pem |
Verify it's encrypted
-- PostgreSQL
SELECT ssl, version, cipher FROM pg_stat_ssl WHERE pid = pg_backend_pid();
-- MySQL
SHOW STATUS LIKE 'Ssl_cipher';
Enforce on the server
- RDS PostgreSQL: parameter
rds.force_ssl = 1; MySQL:require_secure_transport = ON. - Azure Database: "Enforce SSL connection" is on by default.
Why verify-full matters
require encrypts but would accept an impostor's certificate; verify-full also checks the certificate is signed by the trusted CA and matches the hostname.
What to say in the interview
"Clients use sslmode verify-full or the equivalent with the provider's CA bundle mounted into the pod; the server enforces TLS with force_ssl or require_secure_transport; and I verify with pg_stat_ssl or Ssl_cipher."
More on Database connections in apps
How does an application connect to a database, and how should connection details be passed?BeginnerAfter deployment, the app can't connect to the database. How do you troubleshoot it?IntermediateWhat is a connection string? Explain its parts.BeginnerWhat are the default ports for common databases?BeginnerWhy should database credentials never be stored in code or Git?BeginnerHow do Kubernetes Secrets work, and are they secure?Intermediate