From a2fcd59881c527d9741ad1544a300927c6f9b5c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dae=E2=9D=A4=EF=B8=8F?= <74119677+daeisbae@users.noreply.github.com> Date: Tue, 31 Dec 2024 22:44:03 -0800 Subject: [PATCH 1/2] Support SSL based DB connection (#59) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Dae❤️ <74119677+daeisbae@users.noreply.github.com> --- .env.example | 3 ++ src/db/config/config.js | 17 +++--- src/db/scripts/init-db.js | 109 ++++++++++++++++++++++---------------- src/db/utils/connector.ts | 15 ++++-- 4 files changed, 86 insertions(+), 58 deletions(-) diff --git a/.env.example b/.env.example index f54f91b..5418512 100644 --- a/.env.example +++ b/.env.example @@ -4,6 +4,9 @@ DB_PORT= DB_NAME= DB_USER= DB_PASSWORD= +# if you do not require any certificate, you can ignore this +# if you want to need ssl connection with the DB, place the certificate inside the certificates folder then give the filename of the certificate +DB_CERTIFICATE= # Github Token for increasing rate limit of reading the repository GITHUB_TOKEN= diff --git a/src/db/config/config.js b/src/db/config/config.js index 5e56239..6e29ff0 100644 --- a/src/db/config/config.js +++ b/src/db/config/config.js @@ -1,10 +1,11 @@ -import dotenv from 'dotenv'; -dotenv.config(); +import dotenv from 'dotenv' +dotenv.config() export const DBConfig = { - host: process.env.DB_HOST, - port: parseInt(process.env.DB_PORT), - database: process.env.DB_NAME, - user: process.env.DB_USER, - password: process.env.DB_PASSWORD -}; \ No newline at end of file + host: process.env.DB_HOST, + port: parseInt(process.env.DB_PORT), + database: process.env.DB_NAME, + user: process.env.DB_USER, + password: process.env.DB_PASSWORD, + certificate: process.env.DB_CERTIFICATE, +} diff --git a/src/db/scripts/init-db.js b/src/db/scripts/init-db.js index e8c8541..0143a11 100644 --- a/src/db/scripts/init-db.js +++ b/src/db/scripts/init-db.js @@ -1,51 +1,70 @@ -import { createRequire } from "module"; -const require = createRequire(import.meta.url); -const { Client } = require("pg"); -const { readFile } = require("fs/promises"); -import { fileURLToPath } from "url"; -import { dirname, join } from "path"; -import { DBConfig } from "../config/config.js"; +import { createRequire } from 'module' +const require = createRequire(import.meta.url) +const { Client } = require('pg') +const { readFile } = require('fs/promises') +import { fileURLToPath } from 'url' +import { dirname, join } from 'path' +import { DBConfig } from '../config/config.js' +import { promises as fs } from 'fs' -const __dirname = dirname(fileURLToPath(import.meta.url)); +const __dirname = dirname(fileURLToPath(import.meta.url)) async function initDatabase() { - console.log("Initializing database with config:", { - host: DBConfig.host, - port: DBConfig.port, - database: DBConfig.database, - user: DBConfig.user, - }); - - const pgClient = new Client({ - ...DBConfig, - }); - pgClient.connect(); - - await pgClient - .query(`CREATE DATABASE ${DBConfig.database};`) - .catch((error) => { - console.log("❌ Database is already created"); - }) - .then(() => { - console.log("✅ Database created successfully"); - }); - - const schemaPath = join(__dirname, "../migrations/create_tables.sql"); - const sql = await readFile(schemaPath, "utf8"); - - await pgClient.query(sql).catch((error) => { - console.error("❌ Schema loading failed:", error); - }); - console.log("✅ Schema loaded successfully"); - pgClient.end(); + console.log('Initializing database with config:', { + host: DBConfig.host, + port: DBConfig.port, + database: DBConfig.database, + user: DBConfig.user, + }) + + const { certificate, config } = DBConfig + + let pgClient = undefined + + if (!certificate) { + pgClient = new Client({ + ...config, + }) + } else { + pgClient = new Client({ + ...config, + ssl: { + rejectUnauthorized: false, + ca: fs + .readFile(process.cwd() + '/certificates/' + certificate) + .toString(), + sslmode: 'require', + }, + }) + } + + pgClient.connect() + + await pgClient + .query(`CREATE DATABASE ${DBConfig.database};`) + .catch((error) => { + console.log('❌ Database is already created') + }) + .then(() => { + console.log('✅ Database created successfully') + }) + + const schemaPath = join(__dirname, '../migrations/create_tables.sql') + const sql = await readFile(schemaPath, 'utf8') + + await pgClient.query(sql).catch((error) => { + console.error('❌ Schema loading failed:', error) + }) + console.log('✅ Schema loaded successfully') + pgClient.end() } initDatabase() - .then(() => { - console.log("✅ Database initialization complete"); - process.exit(0); - }) - .catch((error) => { - console.error("❌ Fatal error:", error); - process.exit(1); - }); + .then(() => { + console.log('✅ Database initialization complete') + process.exit(0) + }) + .catch((error) => { + console.error('❌ Fatal error:', error) + process.exit(1) + }) diff --git a/src/db/utils/connector.ts b/src/db/utils/connector.ts index f8519cd..a03c04a 100644 --- a/src/db/utils/connector.ts +++ b/src/db/utils/connector.ts @@ -1,6 +1,4 @@ import pg, { QueryResult } from 'pg' -// import { promises as fs } from 'fs' -import {readFileSync} from 'fs' import { DBConfig } from '@/db/config/config' import { promises as fs } from 'fs' @@ -20,14 +18,21 @@ class DBConnector { private constructor() { const { Pool } = pg this.conn = false - this.pool = new Pool({...DBConfig, ...{ + const { certificate, ...config } = DBConfig + if (!certificate) { + this.pool = new Pool(config) + return + } + this.pool = new Pool({ + ...config, ssl: { rejectUnauthorized: false, - ca: fs.readFile(process.cwd() + '/src/assets/ca-certificate.crt') + ca: fs + .readFile(process.cwd() + '/certificates/' + certificate) .toString(), sslmode: 'require', }, - }}) + }) } /** From 33cc3e0a739b4d71e2dc5181eb7cf5840a0bbd75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dae=E2=9D=A4=EF=B8=8F?= <74119677+daeisbae@users.noreply.github.com> Date: Tue, 31 Dec 2024 22:49:02 -0800 Subject: [PATCH 2/2] Include certificate folder (#59) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Dae❤️ <74119677+daeisbae@users.noreply.github.com> --- certificates/ADD_YOUR_CERTIFICATE_HERE | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 certificates/ADD_YOUR_CERTIFICATE_HERE diff --git a/certificates/ADD_YOUR_CERTIFICATE_HERE b/certificates/ADD_YOUR_CERTIFICATE_HERE new file mode 100644 index 0000000..e69de29