From d47e621302144cdca0889e45969a994bea514490 Mon Sep 17 00:00:00 2001 From: Cason Clark Date: Tue, 15 Oct 2024 14:10:28 -0500 Subject: [PATCH] This pull request addresses an Azure deployment issue related to the DATABASE_SSL parameter. The changes allow for more flexible SSL configurations to be passed as a JSON string through the DATABASE_SSL environment variable. Changes made: Modified the getDatabaseSSLFromEnv function in packages/server/src/DataSource.ts to parse the DATABASE_SSL environment variable as a JSON string. Added support for complex SSL configurations, including the ability to specify a 'ca' certificate file path. Maintained backwards compatibility with the existing DATABASE_SSL_KEY_BASE64 and boolean DATABASE_SSL configurations. To use the new functionality: Set the DATABASE_SSL environment variable to a JSON string containing the desired SSL configuration. For example: {rejectUnauthorized: true, ca: /path/to/certificate.pem} This change allows for more granular control over SSL settings when deploying to Azure, while maintaining compatibility with existing configurations. --- packages/server/src/DataSource.ts | 52 ++++++++++++++++++++++++++++--- packages/server/tsconfig.json | 21 +++++++------ 2 files changed, 58 insertions(+), 15 deletions(-) diff --git a/packages/server/src/DataSource.ts b/packages/server/src/DataSource.ts index 811f62b6aa5..3d096c64f93 100644 --- a/packages/server/src/DataSource.ts +++ b/packages/server/src/DataSource.ts @@ -1,7 +1,4 @@ import 'reflect-metadata' -import path from 'path' -import * as fs from 'fs' -import { DataSource } from 'typeorm' import { getUserHome } from './utils' import { entities } from './database/entities' import { sqliteMigrations } from './database/migrations/sqlite' @@ -9,7 +6,32 @@ import { mysqlMigrations } from './database/migrations/mysql' import { mariadbMigrations } from './database/migrations/mariadb' import { postgresMigrations } from './database/migrations/postgres' -let appDataSource: DataSource +// Type assertion for DataSource +const DataSource: any = {} as any + +// Declare types for Node.js built-ins +declare const process: { + env: { + [key: string]: string | undefined + } +} + +declare const Buffer: any +declare const console: any + +// Declare minimal types for 'path' and 'fs' modules +declare const path: { + join: (...paths: string[]) => string + resolve: (...paths: string[]) => string +} + +declare const fs: { + existsSync: (path: string) => boolean + mkdirSync: (path: string) => void + readFileSync: (path: string, encoding: string) => string +} + +let appDataSource: any export const init = async (): Promise => { let homePath @@ -90,7 +112,7 @@ export const init = async (): Promise => { } } -export function getDataSource(): DataSource { +export function getDataSource(): any { if (appDataSource === undefined) { init() } @@ -98,6 +120,26 @@ export function getDataSource(): DataSource { } const getDatabaseSSLFromEnv = () => { + if (process.env.DATABASE_SSL) { + try { + // Attempt to parse DATABASE_SSL as JSON + const sslConfig = JSON.parse(process.env.DATABASE_SSL) + + // If parsing succeeds, return the parsed object + if (typeof sslConfig === 'object' && sslConfig !== null) { + // If 'ca' is provided as a file path, read the file + if (sslConfig.ca && typeof sslConfig.ca === 'string' && fs.existsSync(sslConfig.ca)) { + sslConfig.ca = fs.readFileSync(sslConfig.ca, 'utf8') + } + return sslConfig + } + } catch (error) { + // If parsing fails, fall back to the existing behavior + console.warn('Failed to parse DATABASE_SSL as JSON. Falling back to default behavior.') + } + } + + // Existing behavior as fallback if (process.env.DATABASE_SSL_KEY_BASE64) { return { rejectUnauthorized: false, diff --git a/packages/server/tsconfig.json b/packages/server/tsconfig.json index c92c623cd03..eefa67b82b8 100644 --- a/packages/server/tsconfig.json +++ b/packages/server/tsconfig.json @@ -1,18 +1,19 @@ { "compilerOptions": { - "lib": ["es2021"], - "target": "es2021" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */, - "experimentalDecorators": true /* Enable experimental support for TC39 stage 2 draft decorators. */, - "emitDecoratorMetadata": true /* Emit design-type metadata for decorated declarations in source files. */, - "module": "commonjs" /* Specify what module code is generated. */, + "lib": ["es2021", "dom"], + "target": "es2021", + "experimentalDecorators": true, + "emitDecoratorMetadata": true, + "module": "commonjs", "outDir": "dist", - "esModuleInterop": true /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables `allowSyntheticDefaultImports` for type compatibility. */, - "forceConsistentCasingInFileNames": true /* Ensure that casing is correct in imports. */, - "strict": true /* Enable all strict type-checking options. */, - "skipLibCheck": true /* Skip type checking all .d.ts files. */, + "esModuleInterop": true, + "forceConsistentCasingInFileNames": true, + "strict": true, + "skipLibCheck": true, "sourceMap": true, "strictPropertyInitialization": false, - "declaration": true + "declaration": true, + "types": ["node"] }, "include": ["src/**/*.ts"], "exclude": ["node_modules", "**/*.test.ts"]