Skip to content

fix: Remove hardcoded credentials and implement environment variables

Summary

This MR addresses Critical Issue #1 from the comprehensive security audit by removing all hardcoded credentials and replacing them with environment variables.

Changes

New files

  • .env.example - Template for environment configuration
  • config/sink-postgres/init-template.sql - Template for sink database initialization
  • config/target-db/init-template.sql - Template for target database initialization

Modified files

  • docker-compose.yml - Added init-sql-generator service, all passwords now use env vars
  • config/grafana/provisioning/datasources/datasources.yml - Uses env vars for credentials
  • .gitignore - Added .env and generated files
  • instances.yml - Added documentation
  • README.md - Added environment configuration section

Security improvements

Before

POSTGRES_PASSWORD: postgres  # Hardcoded in git
GF_SECURITY_ADMIN_PASSWORD: demo  # Hardcoded in git

After

POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-changeme}  # From .env file
GF_SECURITY_ADMIN_PASSWORD: ${GF_SECURITY_ADMIN_PASSWORD:-changeme}  # From .env file

How it works

  1. init-sql-generator service runs first
  2. Processes template files with envsubst
  3. Generates init.sql files with actual credentials from environment
  4. Postgres containers use generated files
  5. All services reference environment variables consistently

Testing

# Validate configuration
docker-compose config --quiet
echo $?  # Should be 0

# Configuration renders correctly with defaults  
docker-compose config | grep -E "(POSTGRES_PASSWORD|GF_SECURITY)"

All services tested and verified:

  • Grafana: Accessible with monitor/changeme credentials
  • Prometheus: Collecting metrics successfully
  • pgwatch-postgres: Connecting and fetching measurements
  • Target and sink databases: Initialized with correct credentials

Migration guide

For users upgrading from versions with hardcoded credentials:

# 1. Create .env file
cp .env.example .env

# 2. Edit with secure passwords
nano .env

# 3. Restart services
docker-compose down -v
docker-compose up -d

See .env.example for complete environment variable documentation.

Related issues

Closes #1

Checklist

  • All hardcoded credentials removed
  • Environment variable system implemented
  • Template files created for SQL initialization
  • .env.example created with documentation
  • .gitignore updated to exclude .env
  • Configuration validated with docker-compose config
  • No credentials in git history (new files only)
  • SQL files follow style guide (lowercase keywords)
  • Follows core principles (minimal file creation)
  • Professional communication (no emojis)

Additional notes

This is the first fix from a comprehensive security audit that identified 25 issues across Critical, High, Medium, and Low severity levels. Additional MRs will follow for remaining issues.

Co-Authored-By: Claude noreply@anthropic.com

Edited by Nikolay Samokhvalov

Merge request reports

Loading