Skip to content
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

fix: support enforcing SSL connection #354

Merged
merged 1 commit into from
Jul 31, 2023
Merged
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
3 changes: 3 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import dotenv from 'dotenv'

type StorageBackendType = 'file' | 's3'

type StorageConfigType = {
keepAliveTimeout: number
headersTimeout: number
Expand All @@ -20,6 +21,7 @@ type StorageConfigType = {
jwtAlgorithm: string
multitenantDatabaseUrl?: string
databaseURL: string
databaseSSLRootCert?: string
databasePoolURL?: string
databaseMaxConnections: number
databaseFreePoolAfterInactivity: number
Expand Down Expand Up @@ -108,6 +110,7 @@ export function getConfig(): StorageConfigType {
jwtSecret: getOptionalIfMultitenantConfigFromEnv('PGRST_JWT_SECRET') || '',
jwtAlgorithm: getOptionalConfigFromEnv('PGRST_JWT_ALGORITHM') || 'HS256',
multitenantDatabaseUrl: getOptionalConfigFromEnv('MULTITENANT_DATABASE_URL'),
databaseSSLRootCert: getOptionalConfigFromEnv('DATABASE_SSL_ROOT_CERT'),
databaseURL: getOptionalIfMultitenantConfigFromEnv('DATABASE_URL') || '',
databasePoolURL: getOptionalConfigFromEnv('DATABASE_POOL_URL') || '',
databaseMaxConnections: parseInt(
Expand Down
24 changes: 19 additions & 5 deletions src/database/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@ import { StorageBackendError } from '../storage'
// https://github.com/knex/knex/issues/387#issuecomment-51554522
pg.types.setTypeParser(20, 'text', parseInt)

const { databaseMaxConnections, databaseFreePoolAfterInactivity, databaseConnectionTimeout } =
getConfig()
const {
databaseSSLRootCert,
databaseMaxConnections,
databaseFreePoolAfterInactivity,
databaseConnectionTimeout,
} = getConfig()

interface TenantConnectionOptions {
user: User
Expand Down Expand Up @@ -73,7 +77,10 @@ export class TenantConnection {
idleTimeoutMillis: isExternalPool ? 100 : databaseFreePoolAfterInactivity,
reapIntervalMillis: isExternalPool ? 110 : undefined,
},
connection: connectionString,
connection: {
connectionString: connectionString,
...this.sslSettings(),
},
acquireConnectionTimeout: databaseConnectionTimeout,
})

Expand Down Expand Up @@ -110,6 +117,13 @@ export class TenantConnection {
return new this(knexPool, options)
}

protected static sslSettings() {
if (databaseSSLRootCert) {
return { ssl: { ca: databaseSSLRootCert } }
}
return {}
}

async dispose() {
if (this.options.isExternalPool) {
return this.pool.destroy()
Expand Down Expand Up @@ -137,8 +151,8 @@ export class TenantConnection {
},
{
minTimeout: 50,
maxTimeout: 500,
maxRetryTime: 2000,
maxTimeout: 200,
maxRetryTime: 3000,
retries: 10,
}
)
Expand Down
28 changes: 22 additions & 6 deletions src/database/migrate.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
import { Client } from 'pg'
import { Client, ClientConfig } from 'pg'
import { migrate } from 'postgres-migrations'
import { getConfig } from '../config'

const { multitenantDatabaseUrl } = getConfig()
const { multitenantDatabaseUrl, databaseSSLRootCert } = getConfig()

/**
* Runs tenant migrations
*/
export async function runMigrations(): Promise<void> {
console.log('running migrations')
await connectAndMigrate(process.env.DATABASE_URL, './migrations/tenant')
let ssl: ClientConfig['ssl'] | undefined = undefined

if (databaseSSLRootCert) {
ssl = { ca: databaseSSLRootCert }
}
await connectAndMigrate(process.env.DATABASE_URL, './migrations/tenant', ssl)
console.log('finished migrations')
}

Expand All @@ -27,15 +32,26 @@ export async function runMultitenantMigrations(): Promise<void> {
* @param databaseUrl
*/
export async function runMigrationsOnTenant(databaseUrl: string): Promise<void> {
await connectAndMigrate(databaseUrl, './migrations/tenant')
let ssl: ClientConfig['ssl'] | undefined = undefined

if (databaseSSLRootCert) {
ssl = { ca: databaseSSLRootCert }
}
await connectAndMigrate(databaseUrl, './migrations/tenant', ssl)
}

async function connectAndMigrate(databaseUrl: string | undefined, migrationsDirectory: string) {
const dbConfig = {
async function connectAndMigrate(
databaseUrl: string | undefined,
migrationsDirectory: string,
ssl?: ClientConfig['ssl']
) {
const dbConfig: ClientConfig = {
connectionString: databaseUrl,
connectionTimeoutMillis: 10_000,
options: '-c search_path=storage,public',
ssl,
}

const client = new Client(dbConfig)
try {
await client.connect()
Expand Down
1 change: 1 addition & 0 deletions src/queue/queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export abstract class Queue {
if (isMultitenant) {
url = pgQueueConnectionURL ?? multitenantDatabaseUrl
}

Queue.pgBoss = new PgBoss({
connectionString: url,
max: 4,
Expand Down
Loading