Skip to Content
ContributeBackendEnvironment Configuration

Environment Configuration

Overview

Configuration is environment variables (see 12-factor app ); different files and hosts supply values per environment.

Environment Variables Reference

For a complete list of all environment variables, their requirements, and descriptions, see the Environment Variables.

Environment Files

The application supports multiple environment files:

  • .env: Default environment file for local development
  • .env.docker: Environment configuration for Docker deployment
  • .env.test: Environment configuration for testing (not committed to version control)

Loading Environment Variables

Environment variables are loaded using the python-dotenv library:

python
from dotenv import load_dotenv

load_dotenv()  # Loads variables from .env file

Environment-Specific Configuration

The backend uses BACKEND_ENV as the canonical runtime environment label in Python settings and logging. Valid values are production, development, staging, local, and test.

settings.py
from rhesis.backend.app.config.settings import get_application_settings

settings = get_application_settings()

if settings.backend_env == "production":
    enable_local_debug = False
elif settings.backend_env == "test":
    enable_local_debug = True
else:
    enable_local_debug = settings.is_development

Shell entrypoints may still read ENVIRONMENT for deployment-script behavior, but application code should use BACKEND_ENV.

Database credentials

Database URLs are built from component environment variables. Runtime application sessions use APP_DB_USER / APP_DB_PASS; migrations and administrative jobs use ADMIN_DB_USER / ADMIN_DB_PASS when present and fall back to the app credentials for single-role local setups.

VariableRequiredUsed for
DB_DRIVERNo, defaults to postgresqlSQLAlchemy driver
DB_HOSTYesDatabase host or Unix socket path
DB_PORTNo, defaults to 5432TCP port when DB_HOST is not a socket
DB_NAMEYesDatabase name
APP_DB_USERYes for runtime backend and workersLeast-privilege application database role
APP_DB_PASSYes when APP_DB_USER is setApplication role password
ADMIN_DB_USEROptionalMigration or admin database role
ADMIN_DB_PASSRequired when ADMIN_DB_USER is setAdmin role password

Managed Postgres deployments should use a separate migration role through ADMIN_DB_* and a least-privilege runtime role through APP_DB_*. RLS-aware migrations do not require a PostgreSQL superuser.

Configuration Validation

The application validates critical configuration at startup:

python
def validate_config():
    """Validate that all required configuration is present."""
    required_vars = [
        "DB_HOST",
        "DB_NAME",
        "APP_DB_USER",
        "APP_DB_PASS",
        "JWT_SECRET_KEY",
    ]

    missing = [var for var in required_vars if not os.getenv(var)]

    if missing:
        raise ValueError(f"Missing required environment variables: {', '.join(missing)}")

Docker Environment

When running in Docker, environment variables can be passed in several ways:

  1. Through the environment section in docker-compose.yml
  2. Using the --env-file flag with docker run
  3. Setting individual variables with -e flags

Example Docker Compose configuration:

docker-compose.yml
services:
backend:
    build: ./apps/backend
    env_file:
      - ./apps/backend/.env.docker
    environment:
      - DB_HOST=postgres
      - LOG_LEVEL=INFO

Cloud Deployment

For cloud deployments, environment variables should be set using the cloud provider’s secrets or environment configuration:

  • Google Cloud: Secret Manager and environment variables in Cloud Run
  • AWS: Parameter Store/Secrets Manager and environment variables in ECS/Lambda
  • Azure: Key Vault and App Configuration

Security Best Practices

Never commit secrets. Use distinct keys per environment, store them in a secret manager, rotate periodically, and keep production config separate from dev.

Sensitive Information

Sensitive information such as API keys and passwords should never be committed to version control. Instead:

  1. Use placeholder values in .env.example
  2. Document the required variables in the Environment Setup Guide
  3. Use secrets management in production environments