Security Lab: Attacking the Controls
TLS-only pg_hba, a local-only superuser, least-privilege roles, SCRAM, and encrypted backups, each checked by trying to break it
A security setting you haven't tried to get past is a guess.
labs/security.py makes 17 attempts that should fail, or checks a
property that should hold, and prints what actually happened.
The controls
And roles, created by config/post_bootstrap.sh when the cluster is
born:
app_ownerowns theappschema and runs migrations (DDL).appis what the application logs in as:SELECT,INSERT,UPDATE,DELETEon the tablesapp_ownercreates (throughALTER DEFAULT PRIVILEGES), and nothing else.app_rois read only, for reporting.exporteris a member ofpg_monitor, for metrics.postgres, the superuser, can only log in over the local socket: an operator needsdocker exec(in production, SSH to the node), not just a password.
The run
docker compose -f compose_prod.yaml run --rm labs python security.py
1. Connections
[ok] plaintext connection (sslmode=disable) is refused
-> FATAL: pg_hba.conf rejects connection for host ...
[ok] superuser login over the network is refused
-> FATAL: pg_hba.conf rejects connection for host ...
[ok] wrong password is refused
-> FATAL: SASL authentication failed
[ok] the session is encrypted (pg_stat_ssl)
-> ('TLSv1.3', 'TLS_AES_256_GCM_SHA384', 256)
[ok] server certificate is verified against the lab CA (verify-full)
2. Least privilege
[ok] app cannot create tables -> permission denied for schema app
[ok] app cannot drop the owner's table -> must be owner of table secrets_test
[ok] app cannot create objects in schema public -> permission denied for schema public
[ok] app can write data -> inserted
[ok] app_ro cannot write -> permission denied for table secrets_test
[ok] app cannot read password hashes (pg_authid)
[ok] app cannot call PgBouncer's auth lookup -> permission denied for schema pgbouncer
[ok] the auth lookup never returns superusers -> 0 rows for 'postgres'
[ok] passwords are stored as SCRAM-SHA-256 -> SCRAM-SHA-256$
3. Backups
[ok] archived WAL in the bucket is encrypted
-> ...zst: starts with b'Salted__', 8.000 bits/byte
[ok] the pgbackrest key cannot read other buckets -> AccessDenied
[ok] the backup repository is reached over TLS only (plain HTTP to the gateway fails)
17 of 17 checks passedA few results deserve a closer look:
- The application can't drop its own tables. If an attacker gets SQL
injection in the app, they get
app's rights: they can read and change data, which is bad enough, but notDROP TABLE, not create functions, not touch other schemas. Migrations run asapp_owner, from the deploy pipeline, not from the running app. publicis closed. Since Postgres 15 ordinary users can't create objects in thepublicschema by default. On a database upgraded from 14 or earlier, that permission is still there:REVOKE CREATE ON SCHEMA public FROM PUBLIC.- The backup is ciphertext. The lab downloaded an archived WAL file
from SeaweedFS with pgBackRest's own S3 key. It begins with
Salted__(OpenSSL's encrypted format) instead of zstd's magic bytes, and its bytes are as random as random gets: 8.000 bits per byte. Whoever gets the bucket gets nothing without the passphrase. - The backup key is scoped. pgBackRest's S3 identity can read and
write
pg-backupsand nothing else (AccessDeniedonapp-data), so a compromised database node can't read application files, and a leaked application key can't read backups.
Two honest gaps. SeaweedFS's own S3 port (8333) still speaks plain HTTP on the host; only pgBackRest's path goes through the TLS gateway (and the data is encrypted before it leaves the node anyway). And every password, the backup passphrase and the certificate keys are committed to git so the lab starts as-is. In production they come from a secret manager, and rotating them is a runbook, not an edit.
Run this kind of check in CI against a fresh cluster. Configuration
drifts: someone adds a host all all 0.0.0.0/0 trust line "temporarily",
or grants app ownership to fix a migration. A failing check catches
that the same day.