mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-20 11:06:06 +00:00
25f5e34558
A port for the engine TREK runs on, without changing what it runs on. src/db/engine/ names the engine (SqliteEngine, PostgresEngine) and builds the MikroORM config from it; src/mikro-orm.config.ts exports that config for the CLI and the Nest container alike. Without TREK_DB_URL the config is the one it was: the shared SQLite driver on TREK_DB_FILE or data/travel.db. - TREK_DB_URL takes a postgres:// or postgresql:// URL and is masked in the boot report; TREK_DB_POOL_MAX sizes the pool (1 to 100, default 10). TREK_DB_URL beside TREK_DB_FILE, TREK_DB_JOURNAL_MODE or TREK_DB_SYNCHRONOUS refuses the boot as an ambiguous configuration. - MikroORM builds the pg pool from the URL's host, port, user, password and database and drops the rest of the query, so the engine reads sslmode and sslrootcert itself. require, verify-ca and verify-full all verify the server's certificate, as the pg driver reads them; a mode that would not verify, or a misspelt one, refuses. - Postgres cannot run TREK yet, so a boot with TREK_DB_URL is refused with that reason, in the boot check and again where the config is built, before anything connects. - @mikro-orm/postgresql moves to dependencies; pg is plain JavaScript. - lint:engine-dispatch (CI) holds the engine checks outside src/db/dialect/ and src/db/engine/ (isSqlite(, isPostgres(, an instanceof on a platform, a comparison on the engine's name) to a baseline that starts and stays empty.
111 lines
5.2 KiB
TypeScript
111 lines
5.2 KiB
TypeScript
import { deriveDb } from '../../../../src/app-config/derive';
|
|
import { POSTGRES_NOT_READY } from '../../../../src/app-config/env';
|
|
import { buildOrmConfig, resolveEngine } from '../../../../src/db/engine';
|
|
import { sslOptionFor } from '../../../../src/db/engine/postgres/postgres-engine';
|
|
import { redactUrl } from '../../../../src/db/engine/redact-url';
|
|
import { SharedSqliteDriver } from '../../../../src/db/orm-driver';
|
|
import { PostgreSqlDriver } from '@mikro-orm/postgresql';
|
|
|
|
import { mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
|
|
import { tmpdir } from 'node:os';
|
|
import path from 'node:path';
|
|
import { describe, expect, it } from 'vitest';
|
|
|
|
const PG_URL = 'postgresql://trek:s3cret@db.example:5432/trek';
|
|
|
|
describe('resolveEngine', () => {
|
|
it('ENGINE-001: SQLite when no TREK_DB_URL is set, which is the test environment', () => {
|
|
const engine = resolveEngine();
|
|
expect(engine.id).toBe('sqlite');
|
|
expect(engine.ready).toBe(true);
|
|
expect(engine.ormOptions()).toEqual({ driver: SharedSqliteDriver, dbName: ':memory:' });
|
|
expect(engine.describe()).toBe('SQLite file :memory:');
|
|
});
|
|
|
|
it('ENGINE-002: Postgres when TREK_DB_URL names a database, with the pool size and no SSL by default', () => {
|
|
const engine = resolveEngine(deriveDb({ TREK_DB_URL: PG_URL, TREK_DB_POOL_MAX: '7' }));
|
|
expect(engine.id).toBe('postgres');
|
|
expect(engine.ready).toBe(false);
|
|
expect(engine.ormOptions()).toEqual({
|
|
driver: PostgreSqlDriver,
|
|
clientUrl: PG_URL,
|
|
pool: { min: 0, max: 7 },
|
|
driverOptions: {},
|
|
});
|
|
});
|
|
|
|
it('ENGINE-003: describes a Postgres database without its password', () => {
|
|
const engine = resolveEngine(deriveDb({ TREK_DB_URL: `${PG_URL}?sslmode=require&password=other` }));
|
|
expect(engine.describe()).toBe('Postgres postgresql://trek:***@db.example:5432/trek');
|
|
expect(engine.describe()).not.toContain('s3cret');
|
|
expect(engine.describe()).not.toContain('other');
|
|
});
|
|
});
|
|
|
|
describe('sslOptionFor', () => {
|
|
it('ENGINE-010: maps sslmode onto the pg pool option (MikroORM drops the URL query), always verifying the certificate', () => {
|
|
// As the pg driver itself reads them: require and verify-ca are verify-full.
|
|
expect(sslOptionFor(PG_URL)).toBeUndefined();
|
|
expect(sslOptionFor(`${PG_URL}?sslmode=disable`)).toBe(false);
|
|
for (const mode of ['require', 'verify-ca', 'verify-full']) {
|
|
expect(sslOptionFor(`${PG_URL}?sslmode=${mode}`), mode).toEqual({ rejectUnauthorized: true });
|
|
}
|
|
});
|
|
|
|
it('ENGINE-011: refuses an sslmode it does not know or that would skip verification', () => {
|
|
expect(() => sslOptionFor(`${PG_URL}?sslmode=requrie`)).toThrow(/sslmode "requrie"/);
|
|
expect(() => sslOptionFor(`${PG_URL}?sslmode=no-verify`)).toThrow(/sslmode "no-verify"/);
|
|
expect(() => sslOptionFor(`${PG_URL}?sslmode=prefer`)).toThrow(/sslmode "prefer"/);
|
|
});
|
|
|
|
it('ENGINE-012: passes the SSL option to the pool when the URL asks for one', () => {
|
|
const engine = resolveEngine(deriveDb({ TREK_DB_URL: `${PG_URL}?sslmode=require` }));
|
|
expect(engine.ormOptions().driverOptions).toEqual({ ssl: { rejectUnauthorized: true } });
|
|
});
|
|
|
|
it('ENGINE-013: trusts the CA in sslrootcert (a managed host that signs its own certificates), and refuses a missing file', () => {
|
|
const dir = mkdtempSync(path.join(tmpdir(), 'trek-ca-'));
|
|
try {
|
|
const ca = path.join(dir, 'root.crt');
|
|
writeFileSync(ca, '-----BEGIN CERTIFICATE-----\nprobe\n-----END CERTIFICATE-----\n');
|
|
expect(sslOptionFor(`${PG_URL}?sslmode=verify-full&sslrootcert=${encodeURIComponent(ca)}`)).toEqual({
|
|
rejectUnauthorized: true,
|
|
ca: readFileSync(ca, 'utf8'),
|
|
});
|
|
expect(() =>
|
|
sslOptionFor(`${PG_URL}?sslmode=require&sslrootcert=${encodeURIComponent(path.join(dir, 'no.crt'))}`),
|
|
).toThrow(/sslrootcert/);
|
|
expect(() => sslOptionFor(`${PG_URL}?sslrootcert=${encodeURIComponent(ca)}`)).toThrow(
|
|
/sslrootcert without sslmode/,
|
|
);
|
|
} finally {
|
|
rmSync(dir, { recursive: true, force: true });
|
|
}
|
|
});
|
|
});
|
|
|
|
describe('redactUrl', () => {
|
|
it('ENGINE-020: hides the password and drops the query, which can carry one too', () => {
|
|
expect(redactUrl('postgres://u:p@h/d?password=x&sslmode=require')).toBe('postgres://u:***@h/d');
|
|
expect(redactUrl('postgres://u@h:5433/d')).toBe('postgres://u@h:5433/d');
|
|
expect(redactUrl('not a url')).toBe('<unparseable URL>');
|
|
});
|
|
});
|
|
|
|
describe('buildOrmConfig', () => {
|
|
it('ENGINE-030: the SQLite config carries the entities, migrations and seeders with the engine options', () => {
|
|
const config = buildOrmConfig(resolveEngine());
|
|
expect(config.driver).toBe(SharedSqliteDriver);
|
|
expect(config.dbName).toBe(':memory:');
|
|
expect(config.migrations).toMatchObject({ pathTs: 'src/db/migrations', snapshot: false });
|
|
expect(config.seeder).toMatchObject({ defaultSeeder: 'DatabaseSeeder' });
|
|
expect(Array.isArray(config.entities) && config.entities.length).toBeGreaterThan(100);
|
|
});
|
|
|
|
it('ENGINE-031: refuses an engine this release cannot run, before anything connects, without the password', () => {
|
|
const engine = resolveEngine(deriveDb({ TREK_DB_URL: PG_URL }));
|
|
expect(() => buildOrmConfig(engine)).toThrow(POSTGRES_NOT_READY);
|
|
expect(() => buildOrmConfig(engine)).not.toThrow(/s3cret/);
|
|
});
|
|
});
|