|
| 1 | +# rds-iam-to-dsn |
| 2 | + |
| 3 | +A CLI that resolves a `postgres+rds-iam://...` URL into a usable tokenized PostgreSQL DSN and prints it to stdout. |
| 4 | + |
| 5 | +Use this when you want to script `psql`, `pg_dump`, or other Postgres tools without manual IAM token generation. |
| 6 | + |
| 7 | +## Installation |
| 8 | + |
| 9 | +```bash |
| 10 | +go install github.com/corbaltcode/go-libraries/cmd/rds-iam-to-dsn@latest |
| 11 | +``` |
| 12 | + |
| 13 | +Or build from source: |
| 14 | + |
| 15 | +```bash |
| 16 | +cd ./cmd/rds-iam-to-dsn |
| 17 | +go build |
| 18 | +``` |
| 19 | + |
| 20 | +## Prerequisites |
| 21 | + |
| 22 | +- **AWS credentials** configured (env vars, `~/.aws/credentials`, IAM role, etc.) |
| 23 | +- **AWS region** configured for SDK resolution (for example: `AWS_REGION`, shared config profile, or runtime role config) |
| 24 | +- **RDS IAM authentication enabled** on your database instance |
| 25 | +- A DB user configured for IAM auth (`CREATE USER myuser WITH LOGIN; GRANT rds_iam TO myuser;`) |
| 26 | + |
| 27 | +## Usage |
| 28 | + |
| 29 | +```bash |
| 30 | +rds-iam-to-dsn '<postgres+rds-iam-url>' |
| 31 | +``` |
| 32 | + |
| 33 | +- Database path is optional. If omitted, `pgutils` defaults DB name to the username. |
| 34 | +- The command prints the resolved DSN to **stdout**. |
| 35 | + |
| 36 | +## Examples |
| 37 | + |
| 38 | +Resolve DSN only: |
| 39 | + |
| 40 | +```bash |
| 41 | +rds-iam-to-dsn 'postgres+rds-iam://app_user@mydb.abc123.us-east-1.rds.amazonaws.com:5432/myapp' |
| 42 | +``` |
| 43 | + |
| 44 | +Use with `psql` in a script: |
| 45 | + |
| 46 | +```bash |
| 47 | +DSN="$(rds-iam-to-dsn 'postgres+rds-iam://app_user@mydb.abc123.us-east-1.rds.amazonaws.com:5432/myapp')" |
| 48 | +psql "$DSN" |
| 49 | +``` |
| 50 | + |
| 51 | +Or directly: |
| 52 | + |
| 53 | +```bash |
| 54 | +psql "$(rds-iam-to-dsn 'postgres+rds-iam://app_user@mydb.abc123.us-east-1.rds.amazonaws.com:5432/myapp')" |
| 55 | +``` |
| 56 | + |
| 57 | +Use with `pg_dump`: |
| 58 | + |
| 59 | +```bash |
| 60 | +DSN="$(rds-iam-to-dsn 'postgres+rds-iam://app_user@mydb.abc123.us-east-1.rds.amazonaws.com:5432/myapp')" |
| 61 | +pg_dump "$DSN" > myapp.sql |
| 62 | +``` |
| 63 | + |
| 64 | +Cross-account role assumption: |
| 65 | + |
| 66 | +```bash |
| 67 | +rds-iam-to-dsn 'postgres+rds-iam://app_user@mydb.abc123.us-east-1.rds.amazonaws.com:5432/myapp?assume_role_arn=arn:aws:iam::123456789012:role/db-connect&assume_role_session_name=foo' |
| 68 | +``` |
| 69 | + |
| 70 | +## Troubleshooting |
| 71 | + |
| 72 | +`PAM authentication failed for user "<user>"` |
| 73 | + |
| 74 | +- This indicates IAM database authentication failed, but the message itself is not specific. |
| 75 | +- Check RDS IAM auth error logs in CloudWatch: |
| 76 | + `/aws/rds/instance/<db-instance-identifier>/iam-db-auth-error` |
| 77 | + |
| 78 | +`pg_hba.conf rejects connection for host "...", user "...", database "...", no encryption` |
| 79 | + |
| 80 | +- This usually means the connection attempt was not encrypted. |
| 81 | +- In MAC-FC, RDS parameter groups should enforce SSL. If this appears, verify the endpoint, user, and DSN being used. |
| 82 | + |
| 83 | +## Notes |
| 84 | + |
| 85 | +- IAM auth tokens are short-lived (typically 15 minutes). Generate DSNs close to use time. |
| 86 | +- Treat emitted DSNs as secrets while valid. |
0 commit comments