Skip to content

Commit

Permalink
update db init script to set proper permissions for RO user
Browse files Browse the repository at this point in the history
  • Loading branch information
rplevka committed Nov 15, 2024
1 parent b2e4acd commit 2995639
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
18 changes: 14 additions & 4 deletions scripts/db_init/init-user-db.sh
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
#!/bin/bash
set -e

# Create the telemetry database and the telemetry role
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL
CREATE DATABASE telemetry;
CREATE ROLE telemetry WITH LOGIN PASSWORD 'changeme';
GRANT CONNECT ON DATABASE telemetry TO telemetry;
GRANT USAGE ON SCHEMA public TO telemetry;
GRANT SELECT ON ALL TABLES IN SCHEMA public TO telemetry;
CREATE ROLE ${POSTGRES_RO_USER:-telemetry} WITH LOGIN PASSWORD '${POSTGRES_RO_PASSWORD:-changeme}';
GRANT CONNECT ON DATABASE telemetry TO ${POSTGRES_RO_USER:-telemetry};
EOSQL

# Connect to the telemetry database to set up permissions and triggers
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "telemetry" <<-EOSQL
GRANT USAGE ON SCHEMA public TO ${POSTGRES_RO_USER:-telemetry};
-- Grant SELECT privileges on existing tables
GRANT SELECT ON ALL TABLES IN SCHEMA public TO ${POSTGRES_RO_USER:-telemetry};
-- Set default privileges for future tables created by the postgres user
ALTER DEFAULT PRIVILEGES FOR USER ${POSTGRES_USER} IN SCHEMA public GRANT SELECT ON TABLES TO ${POSTGRES_RO_USER:-telemetry};
EOSQL
12 changes: 11 additions & 1 deletion scripts/db_init/setup_db.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,12 @@
podman network create rekuper
podman run --name rekuper_db --network rekuper --rm -e POSTGRES_PASSWORD=changeme -v ./init-user-db.sh:/docker-entrypoint-initdb.d/init-user-db.sh:z -p 25432:5432 postgres:17
podman run \
--rm \
--name rekuper_db \
--network rekuper \
-e POSTGRES_USER=postgres \
-e POSTGRES_PASSWORD=changeme \
-e POSTGRES_RO_USER=telemetry \
-e POSTGRES_RO_PASSWORD=fero \
-v ./init-user-db.sh:/docker-entrypoint-initdb.d/init-user-db.sh:z \
-p 25432:5432 \
postgres:17 postgres -c log_statement=all

0 comments on commit 2995639

Please sign in to comment.