-
Notifications
You must be signed in to change notification settings - Fork 0
/
docker-postgres-vault-example.sh
executable file
·56 lines (42 loc) · 1.42 KB
/
docker-postgres-vault-example.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/env bash
# https://learn.hashicorp.com/tutorials/vault/database-secrets
docker run \
--detach \
--name learn-postgres \
-e POSTGRES_USER=root \
-e POSTGRES_PASSWORD=rootpassword \
-p 5434:5432 \
--rm \
postgres
docker exec -i \
learn-postgres \
psql -U root -c "CREATE ROLE \"ro\" NOINHERIT;"
docker exec -i \
learn-postgres \
psql -U root -c "GRANT SELECT ON ALL TABLES IN SCHEMA public TO \"ro\";"
export VAULT_ADDR='http://127.0.0.1:8201'
export VAULT_TOKEN=root
vault server -dev -dev-root-token-id root -dev-listen-address=127.0.0.1:8201
vault secrets enable database
vault write database/config/postgresql \
plugin_name=postgresql-database-plugin \
connection_url="postgresql://{{username}}:{{password}}@localhost:5434/postgres?sslmode=disable" \
allowed_roles=readonly \
username="root" \
password="rootpassword"
tee readonly.sql <<EOF
CREATE ROLE "{{name}}" WITH LOGIN PASSWORD '{{password}}' VALID UNTIL '{{expiration}}' INHERIT;
GRANT ro TO "{{name}}";
EOF
vault write database/roles/readonly \
db_name=postgresql \
default_ttl=1h \
max_ttl=24h
vault read database/creds/readonly
# Connection details:
# Postgres address: localhost
# Postgres port: 5434
# Vault address: http://127.0.0.1:8201
# Vault secret: database/creds/readonly
# Vault token: <empty or /home/froque/.vault-token>