T-210: M2.3s — Implement MarketDataStore.macroAsOf, crossSection, QuestDB fx('1m'), and copilot 1m routing
This commit is contained in:
parent
5e255d7e30
commit
373936c312
6 changed files with 168 additions and 129 deletions
|
|
@ -40,6 +40,7 @@ export {
|
|||
export {
|
||||
assertNoConflictingQuestDbSsl,
|
||||
hasConflictingQuestDbUrlSsl,
|
||||
hasQuestDbUrlTlsOptions,
|
||||
openQuestDbPgExecutor,
|
||||
type OpenQuestDbPgExecutorOptions,
|
||||
type QuestDbPgExecutorConfig,
|
||||
|
|
|
|||
|
|
@ -47,58 +47,34 @@ export interface OpenQuestDbPgExecutorOptions {
|
|||
|
||||
const SAFE_ERROR_CODE_RE = /^[A-Z0-9_]{1,32}$/;
|
||||
|
||||
function isQuestDbUrlTlsParamKey(rawKey: string): boolean {
|
||||
const key = rawKey.trim().toLowerCase();
|
||||
return key === 'uselibpqcompat' || key === 'ssl' || key.startsWith('ssl');
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns `true` when an explicit boolean `ssl` setting conflicts with SSL
|
||||
* query parameters (`sslmode`, `ssl`, `sslcert`, `sslkey`, `sslrootcert`)
|
||||
* embedded in `rawUrl`.
|
||||
* Returns `true` when `rawUrl` contains any TLS/SSL query parameter
|
||||
* (`ssl`, `sslmode`, `sslcert`, `sslkey`, `sslrootcert`, `uselibpqcompat`, or
|
||||
* any other `ssl*` parameter).
|
||||
*
|
||||
* TLS settings must have a single source of truth: either configure TLS in the
|
||||
* URL query string (omitting the explicit `ssl` flag) or pass the explicit
|
||||
* `ssl` flag with a URL that contains no TLS query parameters.
|
||||
*/
|
||||
export function hasConflictingQuestDbUrlSsl(rawUrl: string, explicitSsl: boolean): boolean {
|
||||
export function hasQuestDbUrlTlsOptions(rawUrl: string): boolean {
|
||||
let parsed: URL;
|
||||
try {
|
||||
parsed = new URL(rawUrl);
|
||||
} catch {
|
||||
return false;
|
||||
try {
|
||||
parsed = new URL(rawUrl, 'postgres://base');
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
for (const [rawKey, rawValue] of parsed.searchParams.entries()) {
|
||||
const key = rawKey.toLowerCase();
|
||||
const value = rawValue.trim().toLowerCase();
|
||||
|
||||
if (key === 'sslmode') {
|
||||
if (value === 'disable') {
|
||||
if (explicitSsl) {
|
||||
return true;
|
||||
}
|
||||
} else if (
|
||||
value === 'require' ||
|
||||
value === 'verify-ca' ||
|
||||
value === 'verify-full' ||
|
||||
value === 'no-verify'
|
||||
) {
|
||||
if (!explicitSsl) {
|
||||
return true;
|
||||
}
|
||||
} else {
|
||||
// `allow`, `prefer`, or any unrecognized sslmode conflicts with an explicit boolean override.
|
||||
return true;
|
||||
}
|
||||
} else if (key === 'ssl') {
|
||||
if (value === 'false' || value === '0') {
|
||||
if (explicitSsl) {
|
||||
return true;
|
||||
}
|
||||
} else if (value === 'true' || value === '1') {
|
||||
if (!explicitSsl) {
|
||||
return true;
|
||||
}
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
} else if (
|
||||
(key === 'sslcert' || key === 'sslkey' || key === 'sslrootcert') &&
|
||||
value.length > 0 &&
|
||||
!explicitSsl
|
||||
) {
|
||||
for (const [rawKey] of parsed.searchParams.entries()) {
|
||||
if (isQuestDbUrlTlsParamKey(rawKey)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
@ -107,13 +83,21 @@ export function hasConflictingQuestDbUrlSsl(rawUrl: string, explicitSsl: boolean
|
|||
}
|
||||
|
||||
/**
|
||||
* Throws a credential-free `RangeError` if `explicitSsl` conflicts with SSL
|
||||
* query parameters in `rawUrl`.
|
||||
* Returns `true` when an explicit boolean `ssl` setting is combined with any
|
||||
* TLS/SSL query parameter embedded in `rawUrl`.
|
||||
*/
|
||||
export function assertNoConflictingQuestDbSsl(rawUrl: string, explicitSsl: boolean): void {
|
||||
if (hasConflictingQuestDbUrlSsl(rawUrl, explicitSsl)) {
|
||||
export function hasConflictingQuestDbUrlSsl(rawUrl: string, _explicitSsl?: boolean): boolean {
|
||||
return hasQuestDbUrlTlsOptions(rawUrl);
|
||||
}
|
||||
|
||||
/**
|
||||
* Throws a credential-free `RangeError` if an explicit `ssl` option is combined
|
||||
* with any TLS/SSL query parameter in `rawUrl`.
|
||||
*/
|
||||
export function assertNoConflictingQuestDbSsl(rawUrl: string, _explicitSsl?: boolean): void {
|
||||
if (hasQuestDbUrlTlsOptions(rawUrl)) {
|
||||
throw new RangeError(
|
||||
'Conflicting QuestDB SSL configuration: explicit "ssl" option conflicts with SSL query parameters in the QuestDB URL.'
|
||||
'Conflicting QuestDB TLS/SSL configuration: cannot combine an explicit "ssl" option with TLS/SSL query parameters in the QuestDB URL. Configure TLS in either the URL or the explicit "ssl" option, not both.'
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
import { beforeEach, describe, expect, it, mock } from 'bun:test';
|
||||
import { EventEmitter } from 'node:events';
|
||||
import { Client, type PoolConfig } from 'pg';
|
||||
import { QuestDBHealthMonitor } from '../src/health';
|
||||
import { QuestDBInfluxWriter } from '../src/influx-writer';
|
||||
import {
|
||||
|
|
@ -650,25 +651,38 @@ describe('QuestDB', () => {
|
|||
expect(fakePool.ended).toBe(true);
|
||||
});
|
||||
|
||||
it('rejects conflicting URL SSL query parameters and explicit ssl before creating a pool', async () => {
|
||||
it('rejects any URL TLS/SSL query parameter when explicit ssl is set and verifies pinned pg effective TLS config without connecting', async () => {
|
||||
const secret = 'qdb_ssl_s3cr3t!';
|
||||
const base = `postgresql://admin:${encodeURIComponent(secret)}@10.0.0.51:8812/qdb`;
|
||||
|
||||
const conflictingCases: ReadonlyArray<readonly [string, boolean]> = [
|
||||
[`${base}?sslmode=disable`, true],
|
||||
[`${base}?SSLMODE=DISABLE`, true],
|
||||
[`${base}?sslmode=require`, false],
|
||||
[`${base}?sslmode=verify-ca`, false],
|
||||
[`${base}?sslmode=verify-full`, false],
|
||||
[`${base}?sslmode=no-verify`, true],
|
||||
[`${base}?sslmode=no-verify`, false],
|
||||
[`${base}?sslmode=disable`, true],
|
||||
[`${base}?sslmode=disable`, false],
|
||||
[`${base}?SSLMODE=DISABLE`, true],
|
||||
[`${base}?sslmode=require`, true],
|
||||
[`${base}?sslmode=require`, false],
|
||||
[`${base}?sslmode=verify-ca`, true],
|
||||
[`${base}?sslmode=verify-ca`, false],
|
||||
[`${base}?sslmode=verify-full`, true],
|
||||
[`${base}?sslmode=verify-full`, false],
|
||||
[`${base}?sslmode=prefer`, true],
|
||||
[`${base}?sslmode=prefer`, false],
|
||||
[`${base}?sslmode=allow`, true],
|
||||
[`${base}?ssl=false`, true],
|
||||
[`${base}?ssl=false`, false],
|
||||
[`${base}?ssl=0`, true],
|
||||
[`${base}?ssl=0`, false],
|
||||
[`${base}?ssl=true`, true],
|
||||
[`${base}?ssl=true`, false],
|
||||
[`${base}?ssl=1`, true],
|
||||
[`${base}?ssl=1`, false],
|
||||
[`${base}?ssl=`, true],
|
||||
[`${base}?sslrootcert=/etc/ssl/ca.pem`, true],
|
||||
[`${base}?sslrootcert=/etc/ssl/ca.pem`, false],
|
||||
[`${base}?uselibpqcompat=true`, true],
|
||||
[`${base}?uselibpqcompat=true`, false],
|
||||
];
|
||||
|
||||
let createPoolCalls = 0;
|
||||
|
|
@ -694,24 +708,57 @@ describe('QuestDB', () => {
|
|||
}
|
||||
expect(createPoolCalls).toBe(0);
|
||||
|
||||
// Compatible combinations proceed to pool creation.
|
||||
const compatibleCases: ReadonlyArray<readonly [string, boolean]> = [
|
||||
[base, true],
|
||||
[`${base}?sslmode=require`, true],
|
||||
[`${base}?sslmode=disable`, false],
|
||||
[`${base}?ssl=true`, true],
|
||||
[`${base}?ssl=false`, false],
|
||||
// Single-source-of-truth configs proceed to pool creation and produce the
|
||||
// expected effective TLS settings in the pinned `pg` driver without opening
|
||||
// a network connection.
|
||||
const effectiveCases: ReadonlyArray<{
|
||||
readonly config: Parameters<typeof openQuestDbPgExecutor>[0];
|
||||
readonly expectedSsl: unknown;
|
||||
}> = [
|
||||
{ config: { url: base, ssl: true }, expectedSsl: true },
|
||||
{
|
||||
config: { url: `${base}?application_name=copilot`, ssl: false },
|
||||
expectedSsl: false,
|
||||
},
|
||||
{ config: { url: `${base}?sslmode=require` }, expectedSsl: {} },
|
||||
{ config: { url: `${base}?sslmode=disable` }, expectedSsl: false },
|
||||
{
|
||||
config: { url: `${base}?sslmode=no-verify` },
|
||||
expectedSsl: { rejectUnauthorized: false },
|
||||
},
|
||||
{
|
||||
config: { host: '10.0.0.51', port: 8812, database: 'qdb', ssl: true },
|
||||
expectedSsl: true,
|
||||
},
|
||||
{
|
||||
config: { host: '10.0.0.51', port: 8812, database: 'qdb', ssl: false },
|
||||
expectedSsl: false,
|
||||
},
|
||||
];
|
||||
for (const [url, ssl] of compatibleCases) {
|
||||
|
||||
for (const { config, expectedSsl } of effectiveCases) {
|
||||
let capturedPoolConfig: PoolConfig | undefined;
|
||||
const pool = Object.assign(new EventEmitter(), {
|
||||
query: async () => ({ rows: [{ '?column?': 1 }] }),
|
||||
end: async () => {},
|
||||
}) as unknown as QuestDbPgPoolLike;
|
||||
const handle = await openQuestDbPgExecutor(
|
||||
{ url, ssl },
|
||||
{ createPool: () => pool }
|
||||
);
|
||||
const handle = await openQuestDbPgExecutor(config, {
|
||||
createPool: poolConfig => {
|
||||
capturedPoolConfig = poolConfig;
|
||||
return pool;
|
||||
},
|
||||
});
|
||||
await handle.close();
|
||||
|
||||
expect(capturedPoolConfig).toBeDefined();
|
||||
// Instantiate pg.Client without calling connect() to inspect the pinned
|
||||
// driver's effective ConnectionParameters.ssl resolution.
|
||||
const client = new Client(capturedPoolConfig);
|
||||
const effectiveSsl: unknown = (
|
||||
client as unknown as { connectionParameters: { ssl: unknown } }
|
||||
).connectionParameters.ssl;
|
||||
expect(effectiveSsl).toEqual(expectedSsl);
|
||||
expect(client.ssl as unknown).toEqual(expectedSsl);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue