The database usually holds the organization's most sensitive data, which makes it a high-value target. In RDS, security has several layers: where in the network the database sits, who can reach it, how data is protected at rest and in transit, how access is authenticated and how backups are stored. A failure in any one of them can expose everything.
For RDS security, the topics follow the path of someone trying to reach the data. First come network isolation and exposure (where the database sits and who can reach it), then encryption and authentication, next what each connection can do once inside and, finally, auditing, detection of anomalous access and backups. The idea is to shrink the attack surface and keep every access to the database controlled and traceable.
Private networking and exposure
The database should sit in private subnets, with no direct access from the internet. Publicly accessible databases are among the most serious findings in any review, because they are exposed to scanning and attacks all the time. Security groups should allow connections only from the workloads that actually need them, preferably by referencing the application's group instead of IP ranges.
Also, do not expose the database to make administration easier. When you need administrative access, use a controlled bastion host or temporary tunnels and keep the database port closed to the internet.
Reference the application's group, not IP ranges
A security group that opens the database port to a broad range lets more hosts reach the database than necessary. The tighter pattern is to have the inbound rule point to the application's security group as the source. Only members of that group can connect, and the rule stays correct even when the workloads' IPs change.
aws ec2 authorize-security-group-ingress \ --group-id sg-db \ --protocol tcp --port 5432 \ --source-group sg-appEncryption, authentication and logs
Enable encryption at rest, which protects the data and the snapshots, and require encrypted connections in transit. In most cases, encryption at rest has to be set when the database is created, so plan for it from the start. IAM authentication, in turn, uses temporary credentials and, in the scenarios where it applies, eliminates long-lived database passwords.
- Keep the database in a private subnet, with no public access
- Allow only the workloads that need to connect in the security group
- Enable encryption at rest and require TLS in transit
- Consider IAM authentication to have fewer fixed passwords
- Turn on database logs for auditing and investigation
Enable the logs the database engine offers, such as error and audit logs, and export them for analysis. They are what help you detect anomalous access and investigate incidents that involve data.
IAM authentication in practice
With IAM authentication enabled on the database, the application generates a temporary token and connects with it, without a fixed password. Permission to connect is granted in IAM and restricted to the corresponding database user. Control becomes centralized, and the application no longer carries a long-lived password.
{ "Effect": "Allow", "Action": "rds-db:connect", "Resource": "arn:aws:rds-db:REGION:ACCOUNT:dbuser:DB-ID/app_read"}Least privilege inside the database
The network and authentication control who reaches the database, but not what each user does once connected. Inside the database, apply least privilege by creating users per function, each with only the privileges its workload uses. An application that only reads and writes rows does not need privileges to alter the schema, create users or act as a superuser.
-- Application role limited to reading and writing dataCREATE ROLE app_rw LOGIN;GRANT CONNECT ON DATABASE shop TO app_rw;GRANT USAGE ON SCHEMA public TO app_rw;GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA public TO app_rw;-- No DDL, no role creation, no superuserAlso separate administrative roles from application roles. The schema migration credential, which needs DDL, should not be the same one the application uses at runtime. If someone compromises the production workload, they do not also gain the power to change the database structure.
Detecting anomalous access
Preventive controls reduce the chance of unauthorized access, but you still need to see what is happening. Export the engine logs to CloudWatch Logs and create alerts for signs of abuse, such as a sequence of authentication failures or connections from an unexpected source. GuardDuty has an RDS protection plan that watches for anomalous login attempts on managed databases, and it works well as an extra detection layer.
Also keep an eye on unusual queries, such as bulk reads of sensitive tables, which may indicate exfiltration. This kind of signal tends to go unnoticed when no audit log is exported and nobody is looking. So give detection in the database the same care you give the network and identity.
Backups and snapshots
Automated backups and snapshots are as much a part of security as of availability. Set an appropriate retention period, keep backups encrypted and protect them against deletion. Be careful with snapshot sharing: a snapshot shared broadly, or made public, hands over a complete copy of the database. And test restores from time to time, because until a backup has been restored, you are only assuming it works.
Practical checklist
- Place the database in a private subnet, with no public access
- Restrict connections in the security group to the workloads that need them
- Do not expose the database for direct administration
- Enable encryption at rest at creation time
- Require TLS for connections in transit
- Use the application's group as the source in the security group, not IP ranges
- Evaluate IAM authentication with temporary tokens
- Apply least privilege inside the database, with users per function
- Separate administrative credentials from application credentials
- Enable and export the database engine logs
- Alert on login failures and unexpected sources
- Set backup retention and keep backups encrypted
- Prevent snapshots from being shared broadly or publicly
- Test backup restores periodically
Best practices
- As a rule, isolate the database in a private network
- Give network access only to the workloads that need it
- Reference the application's group instead of IP ranges
- Plan encryption at rest from the moment you create the database
- Replace fixed passwords with IAM authentication when you can
- Apply least privilege inside the database, not just on the network
- Protect and test backups, not just configure them
- Audit database access with exported logs
Common mistakes
- Publicly accessible database
- Sensitive data is exposed to scans and attacks from the internet all the time.
- Security group that allows broad ranges
- More hosts than necessary can reach the database.
- Application connecting as superuser
- Whoever compromises the workload gains unlimited power over the schema and the data.
- Database without encryption at rest
- Data and snapshots are unprotected, and turning on encryption later takes work.
- Snapshot shared broadly or made public
- A complete copy of the database is within reach of people who should not have it.
- Backups never tested
- The restore fails right when the incident hits.
Authorship and review
Cesar Gargiulo
CEO and founder of GUARDIASEC. Technical and editorial lead for the content published in this library.
Published on . Found a technical error? Write to [email protected] and we will correct it and keep a record of the change.