-
Notifications
You must be signed in to change notification settings - Fork 1
infra(cpb): add database, env vars, and setup scripts for Connecting People Bot #120
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e35a0d4
infra(cpb): add database, user, and env vars for Connecting People Bot
SashkoMarchuk 8c673c3
fix(cpb): harden cpb-setup-db.sh against SQL injection and silent fai…
SashkoMarchuk e74a8b7
fix(cpb): address CodeRabbit review findings for PR #120
SashkoMarchuk 4c3dded
fix(cpb): address remaining nitpick findings and SonarCloud gate
SashkoMarchuk 47d4221
fix(cpb): add identifier validation to init-db.sh and fix production …
SashkoMarchuk d2ed4f8
fix(cpb): use safe defaults for CPB env vars in production compose
SashkoMarchuk 36051a7
Merge branch 'main' into feature/cpb-infrastructure
SashkoMarchuk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| #!/bin/bash | ||
| set -eo pipefail | ||
|
|
||
| # ============================================================================= | ||
| # CPB Production Database Setup | ||
| # ============================================================================= | ||
| # Creates the cpb_bot database and cpb_app user on an external PostgreSQL server. | ||
| # Idempotent — safe to re-run. Does not modify existing data. | ||
| # | ||
| # Usage: | ||
| # POSTGRES_PASSWORD_CPB="<password>" CPB_POSTGRES_HOST="<host>" ./scripts/cpb-setup-db.sh | ||
| # | ||
| # Required env vars: | ||
| # CPB_POSTGRES_HOST — PostgreSQL server hostname or IP | ||
| # POSTGRES_PASSWORD_CPB — Password for the cpb_app user | ||
| # | ||
| # Optional env vars: | ||
| # CPB_POSTGRES_PORT — PostgreSQL port (default: 5432) | ||
| # CPB_POSTGRES_ADMIN_USER — Admin user for psql connection (default: postgres) | ||
| # POSTGRES_DB_CPB — Database name (default: cpb_bot) | ||
| # POSTGRES_USER_CPB — Username (default: cpb_app) | ||
| # ============================================================================= | ||
|
|
||
| PGHOST="${CPB_POSTGRES_HOST:?CPB_POSTGRES_HOST is required}" | ||
| PGPORT="${CPB_POSTGRES_PORT:-5432}" | ||
| PGADMIN_USER="${CPB_POSTGRES_ADMIN_USER:-postgres}" | ||
| CPB_DB="${POSTGRES_DB_CPB:-cpb_bot}" | ||
| CPB_USER="${POSTGRES_USER_CPB:-cpb_app}" | ||
| CPB_PASS="${POSTGRES_PASSWORD_CPB:?POSTGRES_PASSWORD_CPB is required}" | ||
|
|
||
| # Validate PostgreSQL identifiers (prevent SQL injection via crafted names) | ||
| validate_pg_identifier() { | ||
| local value="$1" name="$2" | ||
| if [[ -z "$value" ]]; then | ||
| echo "ERROR: ${name} cannot be empty" >&2; exit 1 | ||
| fi | ||
| if [[ ${#value} -gt 63 ]]; then | ||
| echo "ERROR: ${name} exceeds PostgreSQL's 63-char identifier limit" >&2; exit 1 | ||
| fi | ||
| if [[ ! "$value" =~ ^[a-zA-Z_][a-zA-Z0-9_]*$ ]]; then | ||
| echo "ERROR: ${name} contains invalid characters (must match ^[a-zA-Z_][a-zA-Z0-9_]*$)" >&2; exit 1 | ||
| fi | ||
| return 0 | ||
| } | ||
| validate_pg_identifier "$CPB_USER" "POSTGRES_USER_CPB" | ||
| validate_pg_identifier "$CPB_DB" "POSTGRES_DB_CPB" | ||
|
|
||
| # Escape single quotes in password for SQL string literal safety | ||
| CPB_PASS_SQL="${CPB_PASS//\'/''}" | ||
|
|
||
| echo "Setting up CPB database on ${PGHOST}:${PGPORT}..." | ||
| echo " Database: ${CPB_DB}" | ||
| echo " User: ${CPB_USER}" | ||
|
|
||
| psql -v ON_ERROR_STOP=1 -h "$PGHOST" -p "$PGPORT" -U "$PGADMIN_USER" <<-EOSQL | ||
| -- Create role if it doesn't exist | ||
| DO \$\$ | ||
| BEGIN | ||
| IF NOT EXISTS (SELECT FROM pg_catalog.pg_roles WHERE rolname = '${CPB_USER}') THEN | ||
| CREATE ROLE "${CPB_USER}" WITH LOGIN ENCRYPTED PASSWORD '${CPB_PASS_SQL}'; | ||
| RAISE NOTICE 'Created role: ${CPB_USER}'; | ||
| ELSE | ||
| RAISE NOTICE 'Role already exists: ${CPB_USER}'; | ||
| END IF; | ||
| END | ||
| \$\$; | ||
|
|
||
| -- Create database if it doesn't exist | ||
| SELECT 'CREATE DATABASE "${CPB_DB}" OWNER "${CPB_USER}"' | ||
| WHERE NOT EXISTS (SELECT FROM pg_database WHERE datname = '${CPB_DB}')\gexec | ||
|
|
||
| -- Grant privileges (idempotent) | ||
| GRANT ALL PRIVILEGES ON DATABASE "${CPB_DB}" TO "${CPB_USER}"; | ||
| EOSQL | ||
|
|
||
| echo "" | ||
| echo "CPB database setup complete." | ||
| echo " Database: ${CPB_DB}" | ||
| echo " User: ${CPB_USER}" | ||
| echo " Host: ${PGHOST}:${PGPORT}" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,48 @@ | ||
| #!/bin/bash | ||
| set -e | ||
|
|
||
| # Validate PostgreSQL identifiers (prevent SQL injection via crafted names) | ||
| validate_pg_identifier() { | ||
| local value="$1" name="$2" | ||
| if [[ -z "$value" ]]; then | ||
| echo "ERROR: ${name} cannot be empty" >&2; exit 1 | ||
| fi | ||
| if [[ ${#value} -gt 63 ]]; then | ||
| echo "ERROR: ${name} exceeds PostgreSQL's 63-char identifier limit" >&2; exit 1 | ||
| fi | ||
| if [[ ! "$value" =~ ^[a-zA-Z_][a-zA-Z0-9_]*$ ]]; then | ||
| echo "ERROR: ${name} contains invalid characters (must match ^[a-zA-Z_][a-zA-Z0-9_]*$)" >&2; exit 1 | ||
| fi | ||
| return 0 | ||
| } | ||
|
|
||
| validate_pg_identifier "$POSTGRES_USER_N8N" "POSTGRES_USER_N8N" | ||
| validate_pg_identifier "$POSTGRES_DB_N8N" "POSTGRES_DB_N8N" | ||
| validate_pg_identifier "$POSTGRES_USER_TEMPORAL" "POSTGRES_USER_TEMPORAL" | ||
| validate_pg_identifier "$POSTGRES_DB_TEMPORAL" "POSTGRES_DB_TEMPORAL" | ||
| validate_pg_identifier "$POSTGRES_DB_TEMPORAL_VISIBILITY" "POSTGRES_DB_TEMPORAL_VISIBILITY" | ||
| validate_pg_identifier "$POSTGRES_USER_CPB" "POSTGRES_USER_CPB" | ||
| validate_pg_identifier "$POSTGRES_DB_CPB" "POSTGRES_DB_CPB" | ||
|
|
||
| # Escape single quotes in passwords for SQL safety | ||
| ESCAPED_POSTGRES_PASSWORD_N8N="${POSTGRES_PASSWORD_N8N//\'/''}" | ||
| ESCAPED_POSTGRES_PASSWORD_TEMPORAL="${POSTGRES_PASSWORD_TEMPORAL//\'/''}" | ||
| ESCAPED_POSTGRES_PASSWORD_CPB="${POSTGRES_PASSWORD_CPB//\'/''}" | ||
|
|
||
| psql -v ON_ERROR_STOP=1 --username "postgres" <<-EOSQL | ||
| CREATE USER "$POSTGRES_USER_N8N" WITH ENCRYPTED PASSWORD '$POSTGRES_PASSWORD_N8N'; | ||
| CREATE USER "$POSTGRES_USER_N8N" WITH ENCRYPTED PASSWORD '$ESCAPED_POSTGRES_PASSWORD_N8N'; | ||
| CREATE DATABASE "$POSTGRES_DB_N8N" OWNER "$POSTGRES_USER_N8N"; | ||
| GRANT ALL PRIVILEGES ON DATABASE "$POSTGRES_DB_N8N" TO "$POSTGRES_USER_N8N"; | ||
|
|
||
| CREATE USER "$POSTGRES_USER_TEMPORAL" WITH ENCRYPTED PASSWORD '$POSTGRES_PASSWORD_TEMPORAL'; | ||
| CREATE USER "$POSTGRES_USER_TEMPORAL" WITH ENCRYPTED PASSWORD '$ESCAPED_POSTGRES_PASSWORD_TEMPORAL'; | ||
| CREATE DATABASE "$POSTGRES_DB_TEMPORAL" OWNER "$POSTGRES_USER_TEMPORAL"; | ||
| GRANT ALL PRIVILEGES ON DATABASE "$POSTGRES_DB_TEMPORAL" TO "$POSTGRES_USER_TEMPORAL"; | ||
|
|
||
| ALTER USER "$POSTGRES_USER_TEMPORAL" CREATEDB; | ||
| CREATE DATABASE "$POSTGRES_DB_TEMPORAL_VISIBILITY" OWNER "$POSTGRES_USER_TEMPORAL"; | ||
| GRANT ALL PRIVILEGES ON DATABASE "$POSTGRES_DB_TEMPORAL_VISIBILITY" TO "$POSTGRES_USER_TEMPORAL"; | ||
|
|
||
| CREATE USER "$POSTGRES_USER_CPB" WITH ENCRYPTED PASSWORD '$ESCAPED_POSTGRES_PASSWORD_CPB'; | ||
| CREATE DATABASE "$POSTGRES_DB_CPB" OWNER "$POSTGRES_USER_CPB"; | ||
| GRANT ALL PRIVILEGES ON DATABASE "$POSTGRES_DB_CPB" TO "$POSTGRES_USER_CPB"; | ||
| EOSQL | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.