Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ DB_POOL_MAX=20
DB_CONNECTION_TIMEOUT=5000
DB_IDLE_TIMEOUT=30000

# Database SSL Configuration (Required in production)
# Path to CA certificate file for TLS verification
# In production, this must be set to enable secure certificate validation
# Example: /etc/ssl/certs/ca-bundle.crt or /path/to/ca.pem
DB_SSL_CA=

# SMS Integration (#448)
# Provider selection: twilio | sns | vonage (default: twilio)
SMS_PROVIDER=twilio
Expand Down
28 changes: 27 additions & 1 deletion src/lib/db/pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,38 @@ import { retryWithBackoff } from '@/utils/errorUtils';
* - Query queueing during reconnect windows
*/

/**
* Validate DB_SSL_CA is provided in production
*/
function validateSSLConfig(): void {
if (process.env.NODE_ENV === 'production' && !process.env.DB_SSL_CA) {
throw new Error(
'DB_SSL_CA environment variable is required in production. ' +
'This should contain the path to your CA certificate file.',
);
}
}

// Validate on module load
validateSSLConfig();

const getSSLConfig = () => {
if (process.env.NODE_ENV === 'production') {
return {
rejectUnauthorized: true,
ca: process.env.DB_SSL_CA,
};
}
// Allow unverified certificates in development
return false;
};

const DB_CONFIG: PoolConfig = {
connectionString: process.env.DATABASE_URL,
max: parseInt(process.env.DB_POOL_MAX || '20', 10),
connectionTimeoutMillis: parseInt(process.env.DB_CONNECTION_TIMEOUT || '5000', 10),
idleTimeoutMillis: parseInt(process.env.DB_IDLE_TIMEOUT || '30000', 10),
ssl: process.env.NODE_ENV === 'production' ? { rejectUnauthorized: false } : false,
ssl: getSSLConfig(),
};

type CircuitState = 'CLOSED' | 'OPEN';
Expand Down
Loading