Sub topic 7
Encrypting data at rest and in transit
This chapter assumes you can run the AWS CLI, know what a VPC and security group are, and have created an RDS instance as in the previous chapter. It covers the two places RDS data can be protected: on disk and on the network.
Two kinds of encryption
Encryption at rest protects the data stored on volumes, backups, snapshots, and read replicas. Encryption in transit protects data moving between your client and the database. They are configured separately, and enabling one does not enable the other.
| Aspect | At rest | In transit |
|---|---|---|
| What it protects | Storage, automated backups, snapshots, replicas | Client-to-database connections |
| Mechanism | AWS KMS key, AES-256 | TLS |
| Set by | `--storage-encrypted` at creation | Client connection settings |
| Can change later? | Not in place | Yes, per connection |
Encryption at rest
RDS encrypts storage with a key in AWS Key Management Service (KMS). If you pass --storage-encrypted and no key, RDS uses the AWS managed key aws/rds. To control key policy and rotation, pass a customer managed key with --kms-key-id.
The provisioning command from the earlier chapter already included the flag:
aws rds create-db-instance \
--db-instance-identifier demo-pg \
--engine postgres \
--db-instance-class db.t4g.micro \
--allocated-storage 20 \
--storage-type gp3 \
--master-username appadmin \
--manage-master-user-password \
--db-subnet-group-name demo-subnets \
--vpc-security-group-ids sg-0123456789abcdef0 \
--no-publicly-accessible \
--storage-encrypted \
--backup-retention-period 7Encryption is applied transparently. Your queries and tools see no difference, and the performance impact is small.
If you delete or disable the KMS key, or revoke RDS's access to it, the database becomes inaccessible. Treat key lifecycle as part of your database lifecycle.
Encryption in transit
RDS engines accept TLS connections. The server presents a certificate signed by an AWS certificate authority (CA). A client that only encrypts, without checking the certificate, is still open to impersonation. To prevent that, download the RDS CA bundle and verify the server against it.
curl -O https://truststore.pki.rds.amazonaws.com/global/global-bundle.pemThen connect with sslmode=verify-full, which both encrypts the session and checks that the certificate chain and hostname are valid:
psql "host=demo-pg.example.us-east-1.rds.amazonaws.com port=5432 dbname=postgres user=appadmin sslmode=verify-full sslrootcert=global-bundle.pem"The hostname above is a placeholder. Use the endpoint of your own instance.
Requiring TLS on the server
Clients choose whether to use TLS unless the server insists. For PostgreSQL, the rds.force_ssl parameter set to 1 rejects unencrypted connections. This is a parameter group setting, covered in a later chapter. MySQL and MariaDB have similar controls, such as require_secure_transport.
Common errors
SSL connection is required: the server enforces TLS and the client usedsslmode=disableor an old driver default. Setsslmodeexplicitly.certificate verify failed: the bundle file is missing, the path is wrong, or the bundle is outdated after a CA rotation. Download the current bundle.no pg_hba.conf entry ... no encryption: the server rejected an unencrypted attempt. Usesslmode=requireor stricter.- Unable to create an encrypted replica or restore: the KMS key policy does not allow the calling role to use the key.
Exercise
A team has a running, unencrypted RDS instance and must encrypt its data at rest. What are the steps, and why can't they just turn encryption on?
Show answer
Encryption at rest can only be set when an instance is created. The team takes a snapshot, copies it while specifying a KMS key so the copy is encrypted, and restores the encrypted copy as a new DB instance. They then point applications at the new endpoint and retire the old instance.
Exercise
Your application connects with sslmode=require. Why might a security reviewer still ask you to move to verify-full?
Show answer
require encrypts the connection but does not check the server certificate, so a man-in-the-middle could present its own certificate. verify-full validates the certificate chain against the RDS CA bundle and checks the hostname.