Encrypted Drupal Database Connections with Amazon RDS
Malicious users can intercept or monitor plaintext data transmitting across unencrypted networks, jeopardising the confidentiality of sensitive data in Drupal applications. This tutorial will show you how to mitigate this type of attack by encrypting your database queries in transit.
by
Nick Santamaria
/ 8 August 2018
With attackers and data breaches becoming more sophisticated every day, it is imperative that we take as many steps as practical to protect sensitive data in our Drupal apps. PreviousNext use Amazon RDS for our MariaDB and MySQL database instances. RDS supports SSL encryption for data in transit, and it is extremely simple to configure your Drupal app to connect in this manner.
1. RDS PEM Bundle
The first step is ensuring your Drupal application has access to the RDS public certificate chain to initiate the handshake. How you achieve this will depend on your particular deployment methodology - we have opted to bake these certificates into our standard container images. Below are the lines we've added to our PHP Dockerfile
.
# Add Amazon RDS TLS public certificate.ADD https://s3.amazonaws.com/rds-downloads/rds-combined-ca-bundle.pem /etc/ssl/certs/rds-combined-ca-bundle.pemRUN chmod 755 /etc/ssl/certs/rds-combined-ca-bundle.pem
If you use a configuration management tool like ansible or puppet, the same principal applies - download that .pem
file to a known location on the app server.
If you have limited control of your hosting environment, you can also commit this file to your codebase and have it deployed alongside your application.
2. Drupal Database Configuration
Next you need to configure Drupal to use this certificate chain if it is available. The PDO extension makes light work of this. This snippet is compatible with Drupal 7 and 8.
$rds_cert_path = "/etc/ssl/certs/rds-combined-ca-bundle.pem";if (is_readable($rds_cert_path)) { $databases['default']['default']['pdo'][PDO::MYSQL_ATTR_SSL_CA] = $rds_cert_path;}
3. Confirmation
The hard work is done, you'll now want to confirm that the connections are actually encrypted.
Use drush to smoke check the PDO options are being picked up correctly. Running drush sql-connect
should give you a new flag: --ssl-ca
.
$ drush sql-connectmysql ... --ssl-ca=/etc/ssl/certs/rds-combined-ca-bundle.pem
If that looks OK, you can take it a step further and sniff the TCP connection between Drupal and the RDS server.
This requires root access to your server, and the tcpflow
package installed - this tool will stream the data being transmitted over port 3306. You are wanting to see illegible garbled data - definitely not content that looks like a SQL queries or responses!
Run this command, and click around your site while logged in (to ensure minimal cache hits).
$ tc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