Chapter 3: Rules and hazards¶
3.1 Non-negotiable rules¶
| ID | Rule |
|---|---|
| R1 | The dev compose file never runs on a server. Each server selects its own file with COMPOSE_FILE=docker-compose.<env>.yml in its .env. |
| R2 | Only nginx faces the internet (80/443). App ports are published on 127.0.0.1 only ("127.0.0.1:8000:8000") or not at all. |
| R3 | Databases and caches publish no ports. They live on internal: true Docker networks (or in private subnets for RDS/ElastiCache) and require a password. |
| R4 | RDS: Public access = No, in private subnets, behind a dedicated security group whose only inbound rule is 5432 from the app's security group. |
| R5 | The application never connects as a superuser (self-hosted Postgres) or as the RDS master user. It gets its own role that owns only its database. |
| R6 | Every secret is long and random (≥ 32 chars, generated), unique per environment, never in git, never in an image, chmod 600 on disk. |
| R7 | Production settings: DEBUG=False, explicit ALLOWED_HOSTS, gunicorn (not runserver), HTTPS only, secure cookies. |
| R8 | Every AWS resource has a meaningful name and tags. Security groups: <project>-<env>-<role>. No launch-wizard-*, nothing in default. |
| R9 | Environments share nothing: separate instances, keys, passwords, security groups, databases. Never build production from a staging image. |
| R10 | SSH only from known IPs (or no SSH at all: Session Manager). Key-only, no passwords, no shared keys between environments. |
| R11 | The VPC default security group has no rules and nothing important is attached to it. |
| R12 | You will know within minutes if something is wrong: uptime check, CPU alarm, GuardDuty findings to email, logs kept ≥ 30 days. |
| R13 | Backups exist, live off the server, and a restore has been tested. |
| R14 | Every change is verified from outside (a network that isn't the office) before you call it done. |
3.2 Hazard catalogue¶
Each hazard: what it is → why it bites → how to detect → how to prevent.
Warning
H1: Docker-published ports bypass the host firewall.
Docker inserts NAT rules that divert traffic before it reaches the INPUT chain that ufw/firewalld use. ufw deny 5432 does nothing for ports: "5432:5432".
Detect: sudo ss -tlnp shows 0.0.0.0:<port> owned by docker-proxy. Prevent: R2/R3. Publish on 127.0.0.1 or not at all. If you must filter, use the DOCKER-USER chain with -m conntrack --ctorigdstport (the iptables backend; the newer nftables backend has no DOCKER-USER chain).
H2: The EC2 launch wizard creates a security group that allows SSH from anywhere.
In the "Network settings → Firewall (security groups)" section, the default "Create security group" path offers Allow SSH traffic from: Anywhere 0.0.0.0/0 and names the group launch-wizard-N.
Prevent: create named security groups before launching; in the wizard choose Select existing security group.
H3: Security group names can't be changed. Name and description are fixed after creation. A bad name stays forever. Fix: create a new group, move resources, delete the old one.
H4: The VPC default security group allows all traffic from itself. Its default inbound rule allows everything from any resource in the same group. Anything dropped into "default" can reach everything else in "default" on every port. It can't be deleted. Prevent: R11. Remove all its inbound/outbound rules (AWS Security Hub recommends exactly this) and never attach resources to it.
H5: 0.0.0.0/0 means the whole internet. Acceptable only for 80/443 on the web tier. Anywhere else it's an incident waiting to happen.
H6: A Postgres superuser = a shell on the database host.
COPY … TO/FROM PROGRAM runs OS commands as the postgres user. Superusers (or members of pg_execute_server_program) can do it.
Prevent: R5. Test: as the app role, copy (select 1) to program 'id' must fail with permission denied.
H7: pg_hba.conf inside the data directory is writable by the database.
An attacker with superuser can rewrite access rules.
Prevent (self-hosted): mount pg_hba.conf read-only from the repo and point hba_file at it.
H8: ALTER SYSTEM changes survive in postgresql.auto.conf, some only apply on restart.
Settings like archive_mode need a restart, so a planted archive_command is dormant until someone restarts the DB. pg_settings even shows archive_command as "(disabled)" while archive_mode=off.
Detect: select * from pg_file_settings (shows settings pending a restart) and cat $PGDATA/postgresql.auto.conf. Rule: never restart a database you suspect is compromised before checking (Chapter 14).
H9: RDS "Public access" plus a permissive security group = database on the internet.
Both must be wrong for exposure; in the Chapter 1 incident, both were.
Detect: from your laptop, dig +short <endpoint> (a public IP means public access) and nc -zv -w5 <endpoint> 5432 (must time out).
Warning
H10: "Internet access gateway: Disabled" does NOT mean "not public".
In the current RDS/Aurora console, Network access type is either VPC or Internet access gateway (a newer network type that only allows IAM authentication). "Internet access gateway: Disabled" just means the instance uses normal VPC networking. Public accessibility is a separate setting. Always confirm with dig/nc (H9).
H11: Auto-generated names hide what things are.
launch-wizard-5, default, ec2-rds-1, rds-ec2-1. Nobody knows what they protect or whether they're still needed. Prevent: R8.
H12: Firewall rules must match the path the traffic actually takes.
A publicly accessible RDS endpoint may resolve to a public IP, yet traffic from an EC2 instance in the same VPC can still arrive from the instance's private IP. A rule for the instance's public IP then doesn't match; a security-group-reference rule does.
Detect what the DB actually sees: select inet_client_addr(); from the app, or client_addr in pg_stat_activity. Rule: add the new rule, verify, then remove the old one.
H13: Cloning carries everything.
An AMI/snapshot of a server copies its data volumes, SSH authorized_keys, .env, Docker volumes, and any attacker leftovers.
Prevent: R9. Build each environment from scratch (or from a clean, purpose-built image with no data and no keys).
H14: Stop/start changes the public IP. Without an Elastic IP, an EC2 stop/start (not an OS reboot) assigns a new public IPv4. DNS records and any IP-based rules break. Prevent: allocate an Elastic IP for web servers, and use security-group references rather than IPs between AWS resources.
H15: Logs disappear quickly by default.
RDS keeps its log files 3 days by default (rds.log_retention_period = 4320 min, max 7 days). CloudTrail Event history shows only 90 days of management events. log_connections is off by default.
Prevent: export RDS logs to CloudWatch (with a retention period), create a CloudTrail trail to S3, turn on log_connections/log_disconnections.
H16: Restarting a container wipes its docker logs history.
Recreating a container starts a fresh log. Evidence is lost. Rule: in an incident, docker logs <c> > file before any restart.
H17: A plain docker compose restart keeps the old environment.
env_file is read when the container is created. After changing .env, use docker compose up -d --force-recreate <services>.
H18: Secrets-Manager-managed RDS passwords vs a static .env.
If the master password is "Managed in AWS Secrets Manager" and automatic rotation is enabled, a password copied into .env breaks at the next rotation. Rotate via Secrets Manager (Rotate secret immediately), not via RDS Modify → Self managed.
H19: Being in the docker group = root. Anyone in it can mount / into a container. Keep membership minimal.
H20: .env.example defaults leak into production. Weak example values (DB_PASSWORD=root, DEBUG=TRUE, ALLOWED_HOSTS=*) get copied as-is. Keep examples empty, or obviously invalid, and let the stack refuse to start without real values (${VAR:?message} in compose).
H21: The Django dev server and DEBUG=True on the internet. runserver isn't built for hostile traffic. Debug pages leak settings, paths and environment details.
H22: Container-level "internal" networks block egress, and so they block payload downloads too.
internal: true networks have no route out. Use them for DB/cache. The app containers need a second, normal network for outbound calls.