|
| 1 | +# Example: Web (FastAPI) App setup |
| 2 | + |
| 3 | +This example shows how to create roles for a FastAPI web application with thoughtful permission boundaries intended for a production setup. |
| 4 | + |
| 5 | +## Prerequisites |
| 6 | + |
| 7 | +- Terraform installed on your local machine. |
| 8 | +- Access to a PostgreSQL instance where you can apply these configurations. |
| 9 | + |
| 10 | +## Usage |
| 11 | + |
| 12 | +1. Clone the repository and navigate to the `examples/web_app` directory. |
| 13 | +2. Review and update the `fixtures.tfvars` file with your specific configuration details. |
| 14 | +3. Run the following Terraform commands to apply the configuration: |
| 15 | + |
| 16 | +```bash |
| 17 | +terraform init |
| 18 | + |
| 19 | +# cat out the databases and roles |
| 20 | +cat fixtures.auto.vars |
| 21 | + |
| 22 | +terraform plan |
| 23 | +terraform apply |
| 24 | +``` |
| 25 | + |
| 26 | +## Roles and Permissions |
| 27 | + |
| 28 | +There are 3 roles in this example, |
| 29 | + |
| 30 | +- **fastapi_app_owner**: owner of the `web_app` database. |
| 31 | + |
| 32 | + - This DB role is only used and accessed in the CI/CD |
| 33 | + - This role applies migrations (makes changes to the DB structure) prior to the application booting up |
| 34 | + |
| 35 | +- **fastapi_app_writer**: writer role for the `web_app` database |
| 36 | + |
| 37 | + - This DB role is the primary role for the FastAPI application |
| 38 | + - This role has full Create, Read, Update, Delete abilities |
| 39 | + |
| 40 | +- **fastapi_app_reader**: reader role for the `web_app` database. |
| 41 | + - This DB role is a secondary role for the FastAPI application |
| 42 | + - This role can only Read from the DB. For example, we'd use this role for `GET` http endpoints. |
| 43 | + - For production setups, we'd have this role connect to a Postgres Read-Replica Server to minimize the amount of DB traffic on Postgres Write-DB instance. |
| 44 | + |
| 45 | +## NOTE about authentication on Mac with default settings |
| 46 | + |
| 47 | +Some default `pg_hba.conf` Postgres settings will allow you to log into the |
| 48 | +Postgres server without ensuring a valid password. We ran into this when |
| 49 | +we installed `postgresql@17` via [Homebrew](https://brew.sh/) |
| 50 | + |
| 51 | +Example `pg_hba.conf` settings that don't check for a valid password |
| 52 | + |
| 53 | +```bash |
| 54 | +$ PG_PASSWORD=doesNotMatter psql web_app -U fastapi_app_owner |
| 55 | +psql (17.4 (Homebrew), server 17.2 (Homebrew)) |
| 56 | +Type "help" for help. |
| 57 | +web_app=> |
| 58 | +``` |
| 59 | + |
| 60 | +```bash |
| 61 | +# TYPE DATABASE USER ADDRESS METHOD |
| 62 | + |
| 63 | +# "local" is for Unix domain socket connections only |
| 64 | +local all all trust |
| 65 | +# IPv4 local connections: |
| 66 | +host all all 127.0.0.1/32 trust |
| 67 | +# IPv6 local connections: |
| 68 | +host all all ::1/128 trust |
| 69 | +# Allow replication connections from localhost, by a user with the |
| 70 | +# replication privilege. |
| 71 | +local replication all trust |
| 72 | +host replication all 127.0.0.1/32 trust |
| 73 | +host replication all ::1/128 trust |
| 74 | +``` |
| 75 | + |
| 76 | +If you want Postgres to check the password values, swap in these `pg_hba.conf` changes, |
| 77 | + |
| 78 | +Example `pg_hba.conf` settings that require valid password values, |
| 79 | + |
| 80 | +```bash |
| 81 | +# Allow only "my_user" to connect without a password (local + IPv4 localhost) |
| 82 | +local all my_user trust |
| 83 | +host all my_user 127.0.0.1/32 trust |
| 84 | +# All other users must use a password (local + IPv4 localhost + IPv6 localhost + other hosts) |
| 85 | +local all all md5 |
| 86 | +host all all 127.0.0.1/32 md5 |
| 87 | +host all all ::1/128 md5 |
| 88 | +# Replication rules (if needed, otherwise you can remove them or secure similarly) |
| 89 | +local replication all md5 |
| 90 | +host replication all 127.0.0.1/32 md5 |
| 91 | +host replication all ::1/128 md5 |
| 92 | +``` |
| 93 | + |
| 94 | +```bash |
| 95 | +$ PG_PASSWORD=insecure-pass-for-demo-fastapi-app-owner psql web_app -U fastapi_app_owner |
| 96 | +psql (17.4 (Homebrew), server 17.2 (Homebrew)) |
| 97 | +Type "help" for help. |
| 98 | +web_app=> |
| 99 | +``` |
0 commit comments