|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +import { describe, it, expect, vi } from 'vitest'; |
| 4 | +import { DatasourceSchema } from '@objectstack/spec/data'; |
| 5 | +import { MetadataProtectionFields } from '@objectstack/spec/kernel'; |
| 6 | +import { HonoHttpServer } from '@objectstack/plugin-hono-server'; |
| 7 | +import { registerDatasourceAdminRoutes } from '../admin-routes.js'; |
| 8 | +import { |
| 9 | + DatasourceAdminService, |
| 10 | + type DatasourceAdminServiceConfig, |
| 11 | + type ProbeInput, |
| 12 | + type StoredDatasource, |
| 13 | +} from '../datasource-admin-service.js'; |
| 14 | +import { DatasourceAdminServicePlugin } from '../datasource-admin-plugin.js'; |
| 15 | +import type { |
| 16 | + DatasourceConnectionSpec, |
| 17 | + IDatasourceAdminService, |
| 18 | + IDatasourceDriverFactory, |
| 19 | +} from '../contracts/index.js'; |
| 20 | +import { |
| 21 | + ENTITLED_CREDENTIAL, |
| 22 | + createSessionAuthService, |
| 23 | + createGrantsEngine, |
| 24 | +} from './entitled-caller.fixture.js'; |
| 25 | + |
| 26 | +/** |
| 27 | + * #28633 — the datasource admin doors keep every top-level key |
| 28 | + * `DatasourceSchema` declares, `ssl`, `autoConnect` and `description` among |
| 29 | + * them. |
| 30 | + * |
| 31 | + * `POST /api/v1/datasources` and `PATCH /api/v1/datasources/:name` built the |
| 32 | + * stored record from a hand list of eight keys, so these three passed the |
| 33 | + * undeclared-key refusal (the schema declares them) and were then dropped: a |
| 34 | + * datasource saved with a TLS block was stored, and connected, without one. |
| 35 | + * `POST /api/v1/datasources/test` probed the same short projection. |
| 36 | + * |
| 37 | + * Driven through the REAL routes over the REAL service on in-memory stores, |
| 38 | + * reading the stored record, the record the pool seam is handed, the probe |
| 39 | + * input and the read answer. The plugin's own probe is read through a fake |
| 40 | + * driver factory, for the hand-over to the driver. |
| 41 | + * |
| 42 | + * Every certificate and key below is an obvious placeholder. |
| 43 | + */ |
| 44 | + |
| 45 | +const authService = createSessionAuthService(); |
| 46 | +const grantsEngine = createGrantsEngine(); |
| 47 | + |
| 48 | +const PLACEHOLDER_CA = 'PLACEHOLDER_CA_CERTIFICATE'; |
| 49 | +const PLACEHOLDER_KEY = 'PLACEHOLDER_NOT_A_PRIVATE_KEY'; |
| 50 | +const SSL = { enabled: true, rejectUnauthorized: true, ca: PLACEHOLDER_CA }; |
| 51 | +const BASE = { driver: 'postgres', config: { host: 'db.example.invalid', database: 'analytics' } }; |
| 52 | + |
| 53 | +/** In-memory stores behind a real {@link DatasourceAdminService}, recording every seam call. */ |
| 54 | +function makeDoor(seed: StoredDatasource[] = []) { |
| 55 | + const records = new Map<string, StoredDatasource>(seed.map((r) => [r.name, structuredClone(r)])); |
| 56 | + const ops: string[] = []; |
| 57 | + const pooled: StoredDatasource[] = []; |
| 58 | + const probes: ProbeInput[] = []; |
| 59 | + |
| 60 | + const config: DatasourceAdminServiceConfig = { |
| 61 | + probe: async (input) => { |
| 62 | + ops.push('probe'); |
| 63 | + probes.push(structuredClone(input)); |
| 64 | + return { ok: true, latencyMs: 1 }; |
| 65 | + }, |
| 66 | + listDatasourceRecords: async () => [...records.values()].map((r) => structuredClone(r)), |
| 67 | + getDatasourceRecord: async (name) => { |
| 68 | + const r = records.get(name); |
| 69 | + return r ? structuredClone(r) : undefined; |
| 70 | + }, |
| 71 | + putDatasourceRecord: async (record) => { |
| 72 | + ops.push(`put:${record.name}`); |
| 73 | + records.set(record.name, structuredClone(record)); |
| 74 | + }, |
| 75 | + deleteDatasourceRecord: async (name) => { |
| 76 | + records.delete(name); |
| 77 | + }, |
| 78 | + writeSecret: async (_input, hint) => { |
| 79 | + ops.push('writeSecret'); |
| 80 | + return `sys_secret:sec_${hint.name}`; |
| 81 | + }, |
| 82 | + countBoundObjects: async () => 0, |
| 83 | + listObjectsBoundTo: async () => [], |
| 84 | + registerPool: (record) => { |
| 85 | + ops.push('registerPool'); |
| 86 | + pooled.push(structuredClone(record)); |
| 87 | + }, |
| 88 | + unregisterPool: () => { |
| 89 | + ops.push('unregisterPool'); |
| 90 | + }, |
| 91 | + reregisterPool: (_previous, next) => { |
| 92 | + ops.push('reregisterPool'); |
| 93 | + pooled.push(structuredClone(next)); |
| 94 | + }, |
| 95 | + }; |
| 96 | + |
| 97 | + const service = new DatasourceAdminService(config); |
| 98 | + const server = new HonoHttpServer(0); |
| 99 | + const ctx = { |
| 100 | + getService: vi.fn((name: string) => |
| 101 | + name === 'auth' |
| 102 | + ? authService |
| 103 | + : name === 'objectql' || name === 'data' |
| 104 | + ? grantsEngine |
| 105 | + : name === 'datasource-admin' |
| 106 | + ? service |
| 107 | + : undefined, |
| 108 | + ), |
| 109 | + } as any; |
| 110 | + registerDatasourceAdminRoutes(server, ctx, '/api/v1'); |
| 111 | + const app = server.getRawApp(); |
| 112 | + |
| 113 | + const send = (method: string, path: string, body?: unknown) => |
| 114 | + app.fetch( |
| 115 | + new Request(`http://local/api/v1${path}`, { |
| 116 | + method, |
| 117 | + headers: { 'content-type': 'application/json', authorization: ENTITLED_CREDENTIAL }, |
| 118 | + ...(body === undefined ? {} : { body: JSON.stringify(body) }), |
| 119 | + }), |
| 120 | + ); |
| 121 | + |
| 122 | + return { send, records, ops, pooled, probes }; |
| 123 | +} |
| 124 | + |
| 125 | +const STORED: StoredDatasource = { name: 'wh', ...BASE, origin: 'runtime' }; |
| 126 | + |
| 127 | +async function expectRefusal(res: Response): Promise<string> { |
| 128 | + const raw = await res.text(); |
| 129 | + const body = JSON.parse(raw) as { success: boolean; error?: { code: string; message: string } }; |
| 130 | + expect({ status: res.status, success: body.success, code: body.error?.code }).toEqual({ |
| 131 | + status: 400, |
| 132 | + success: false, |
| 133 | + code: 'DATASOURCE_ADMIN_ERROR', |
| 134 | + }); |
| 135 | + // Named, never echoed. |
| 136 | + expect(raw).not.toContain(PLACEHOLDER_KEY); |
| 137 | + return body.error!.message; |
| 138 | +} |
| 139 | + |
| 140 | +describe('#28633 premise — the keys this card is about are declared by DatasourceSchema', () => { |
| 141 | + it('declares ssl, autoConnect and description (so the undeclared-key refusal lets them through)', () => { |
| 142 | + const shape = Object.keys((DatasourceSchema as unknown as { shape: object }).shape); |
| 143 | + expect(shape).toEqual(expect.arrayContaining(['ssl', 'autoConnect', 'description'])); |
| 144 | + }); |
| 145 | +}); |
| 146 | + |
| 147 | +describe('POST /api/v1/datasources — create keeps every declared key (#28633)', () => { |
| 148 | + it('stores ssl, autoConnect and description, and the pool is opened from a record carrying ssl', async () => { |
| 149 | + const { send, records, pooled } = makeDoor(); |
| 150 | + const res = await send('POST', '/datasources', { |
| 151 | + name: 'wh', |
| 152 | + ...BASE, |
| 153 | + ssl: SSL, |
| 154 | + autoConnect: true, |
| 155 | + description: 'Analytics warehouse', |
| 156 | + }); |
| 157 | + expect(res.status).toBe(201); |
| 158 | + expect(records.get('wh')).toMatchObject({ ssl: SSL, autoConnect: true, description: 'Analytics warehouse' }); |
| 159 | + expect(pooled).toHaveLength(1); |
| 160 | + expect(pooled[0].ssl).toEqual(SSL); |
| 161 | + }); |
| 162 | + |
| 163 | + it('a read returns them', async () => { |
| 164 | + const { send } = makeDoor(); |
| 165 | + await send('POST', '/datasources', { name: 'wh', ...BASE, ssl: SSL, autoConnect: true, description: 'd' }); |
| 166 | + const res = await send('GET', '/datasources/wh'); |
| 167 | + expect(res.status).toBe(200); |
| 168 | + const body = (await res.json()) as { data: { datasource: Record<string, unknown> } }; |
| 169 | + expect(body.data.datasource).toMatchObject({ ssl: SSL, autoConnect: true, description: 'd' }); |
| 170 | + }); |
| 171 | + |
| 172 | + it('a stored TLS block is still judged by the schema: an unknown ssl key is refused, nothing stored', async () => { |
| 173 | + const { send, records, ops } = makeDoor(); |
| 174 | + const message = await expectRefusal( |
| 175 | + await send('POST', '/datasources', { name: 'wh', ...BASE, ssl: { enabled: true, insecure: true } }), |
| 176 | + ); |
| 177 | + expect(message).toContain('ssl'); |
| 178 | + expect(records.has('wh')).toBe(false); |
| 179 | + expect(ops).toEqual([]); |
| 180 | + }); |
| 181 | + |
| 182 | + it('control: a draft without these keys stores none of them, as before', async () => { |
| 183 | + const { send, records } = makeDoor(); |
| 184 | + expect((await send('POST', '/datasources', { name: 'wh', ...BASE })).status).toBe(201); |
| 185 | + const stored = records.get('wh')!; |
| 186 | + expect(Object.keys(stored).sort()).toEqual(['config', 'driver', 'name', 'origin']); |
| 187 | + }); |
| 188 | + |
| 189 | + it('server-stamped keys are not taken from the body: origin is stamped runtime, the protection envelope is not stored', async () => { |
| 190 | + const { send, records } = makeDoor(); |
| 191 | + const res = await send('POST', '/datasources', { |
| 192 | + name: 'wh', |
| 193 | + ...BASE, |
| 194 | + origin: 'code', |
| 195 | + _lock: 'full', |
| 196 | + _lockReason: 'locked by the author', |
| 197 | + _provenance: 'package', |
| 198 | + _packageId: 'com.example.pkg', |
| 199 | + }); |
| 200 | + expect(res.status).toBe(201); |
| 201 | + const stored = records.get('wh')!; |
| 202 | + expect(stored.origin).toBe('runtime'); |
| 203 | + for (const key of Object.keys(MetadataProtectionFields)) expect(stored).not.toHaveProperty(key); |
| 204 | + }); |
| 205 | +}); |
| 206 | + |
| 207 | +describe('PATCH /api/v1/datasources/:name — edit keeps every declared key (#28633)', () => { |
| 208 | + it('stores a new ssl block and rebuilds the pool from a record carrying it', async () => { |
| 209 | + const { send, records, ops, pooled } = makeDoor([STORED]); |
| 210 | + const res = await send('PATCH', '/datasources/wh', { ssl: SSL }); |
| 211 | + expect(res.status).toBe(200); |
| 212 | + expect(records.get('wh')?.ssl).toEqual(SSL); |
| 213 | + expect(ops).toContain('reregisterPool'); |
| 214 | + expect(pooled[pooled.length - 1]?.ssl).toEqual(SSL); |
| 215 | + }); |
| 216 | + |
| 217 | + it('stores autoConnect and description', async () => { |
| 218 | + const { send, records } = makeDoor([STORED]); |
| 219 | + expect((await send('PATCH', '/datasources/wh', { autoConnect: true, description: 'Moved' })).status).toBe(200); |
| 220 | + expect(records.get('wh')).toMatchObject({ autoConnect: true, description: 'Moved' }); |
| 221 | + }); |
| 222 | + |
| 223 | + it('control: a description-only edit does not churn the live connection', async () => { |
| 224 | + const { send, ops } = makeDoor([STORED]); |
| 225 | + expect((await send('PATCH', '/datasources/wh', { description: 'Moved' })).status).toBe(200); |
| 226 | + expect(ops).toContain('registerPool'); |
| 227 | + expect(ops).not.toContain('reregisterPool'); |
| 228 | + }); |
| 229 | + |
| 230 | + it('control: a label-only edit leaves a stored ssl block as it was', async () => { |
| 231 | + const { send, records } = makeDoor([{ ...STORED, ssl: SSL }]); |
| 232 | + expect((await send('PATCH', '/datasources/wh', { label: 'Warehouse' })).status).toBe(200); |
| 233 | + expect(records.get('wh')?.ssl).toEqual(SSL); |
| 234 | + }); |
| 235 | +}); |
| 236 | + |
| 237 | +describe('POST /api/v1/datasources/test — the probe reads the record a Save stores (#28633)', () => { |
| 238 | + it('hands the draft ssl block to the probe', async () => { |
| 239 | + const { send, probes } = makeDoor(); |
| 240 | + const res = await send('POST', '/datasources/test', { ...BASE, ssl: SSL }); |
| 241 | + expect(res.status).toBe(200); |
| 242 | + expect(probes).toHaveLength(1); |
| 243 | + expect(probes[0].ssl).toEqual(SSL); |
| 244 | + }); |
| 245 | + |
| 246 | + it('control: a draft without ssl probes without it, as before', async () => { |
| 247 | + const { send, probes } = makeDoor(); |
| 248 | + expect((await send('POST', '/datasources/test', BASE)).status).toBe(200); |
| 249 | + expect(probes[0]).not.toHaveProperty('ssl'); |
| 250 | + }); |
| 251 | + |
| 252 | + it('the plugin probe hands ssl to the driver factory', async () => { |
| 253 | + const created: DatasourceConnectionSpec[] = []; |
| 254 | + const factory: IDatasourceDriverFactory = { |
| 255 | + supports: () => true, |
| 256 | + create: async (spec) => { |
| 257 | + created.push(structuredClone(spec)); |
| 258 | + return { connect: async () => {}, ping: async () => {}, disconnect: async () => {} } as any; |
| 259 | + }, |
| 260 | + } as IDatasourceDriverFactory; |
| 261 | + let admin: IDatasourceAdminService | undefined; |
| 262 | + const ctx: any = { |
| 263 | + getService: (name: string) => { |
| 264 | + if (name === 'metadata') return { get: async () => undefined, list: async () => [] }; |
| 265 | + throw new Error(`no service ${name}`); |
| 266 | + }, |
| 267 | + registerService: (name: string, svc: unknown) => { |
| 268 | + if (name === 'datasource-admin') admin = svc as IDatasourceAdminService; |
| 269 | + }, |
| 270 | + trigger: async () => {}, |
| 271 | + logger: { warn() {}, info() {} }, |
| 272 | + }; |
| 273 | + await new DatasourceAdminServicePlugin({ driverFactory: factory }).init(ctx); |
| 274 | + const result = await admin!.testConnection({ name: 'x', ...BASE, ssl: SSL }); |
| 275 | + expect(result.ok).toBe(true); |
| 276 | + expect(created).toHaveLength(1); |
| 277 | + expect(created[0].ssl).toEqual(SSL); |
| 278 | + }); |
| 279 | +}); |
| 280 | + |
| 281 | +describe('ssl.key — a TLS client private key is never stored in cleartext or served (#28633)', () => { |
| 282 | + it('create refuses an inline ssl.key by name: nothing stored, minted or opened', async () => { |
| 283 | + const { send, records, ops } = makeDoor(); |
| 284 | + const message = await expectRefusal( |
| 285 | + await send('POST', '/datasources', { name: 'wh', ...BASE, ssl: { ...SSL, key: PLACEHOLDER_KEY } }), |
| 286 | + ); |
| 287 | + expect(message.split('\n')[0]).toBe("Invalid datasource 'wh'."); |
| 288 | + expect(message).toContain('`ssl.key`'); |
| 289 | + expect(records.has('wh')).toBe(false); |
| 290 | + expect(ops).toEqual([]); |
| 291 | + }); |
| 292 | + |
| 293 | + it('edit refuses an inline ssl.key by name: the stored record is unchanged', async () => { |
| 294 | + const { send, records, ops } = makeDoor([STORED]); |
| 295 | + const message = await expectRefusal( |
| 296 | + await send('PATCH', '/datasources/wh', { ssl: { ...SSL, key: PLACEHOLDER_KEY } }), |
| 297 | + ); |
| 298 | + expect(message).toContain('`ssl.key`'); |
| 299 | + expect(records.get('wh')).toEqual(STORED); |
| 300 | + expect(ops).toEqual([]); |
| 301 | + }); |
| 302 | + |
| 303 | + it('the test door refuses it the same way, as a failed probe that never ran', async () => { |
| 304 | + const { send, probes } = makeDoor(); |
| 305 | + const res = await send('POST', '/datasources/test', { ...BASE, ssl: { ...SSL, key: PLACEHOLDER_KEY } }); |
| 306 | + const raw = await res.text(); |
| 307 | + expect(raw).not.toContain(PLACEHOLDER_KEY); |
| 308 | + const body = JSON.parse(raw) as { data: { result: { ok: boolean; error: string } } }; |
| 309 | + expect(res.status).toBe(200); |
| 310 | + expect(body.data.result.ok).toBe(false); |
| 311 | + expect(body.data.result.error).toContain('`ssl.key`'); |
| 312 | + expect(probes).toEqual([]); |
| 313 | + }); |
| 314 | + |
| 315 | + it('a read withholds a private key a stored row already holds', async () => { |
| 316 | + const { send } = makeDoor([{ ...STORED, ssl: { ...SSL, key: PLACEHOLDER_KEY } }]); |
| 317 | + const res = await send('GET', '/datasources/wh'); |
| 318 | + const raw = await res.text(); |
| 319 | + expect(res.status).toBe(200); |
| 320 | + expect(raw).not.toContain(PLACEHOLDER_KEY); |
| 321 | + const body = JSON.parse(raw) as { data: { datasource: { ssl: Record<string, unknown> } } }; |
| 322 | + expect(body.data.datasource.ssl).toEqual(SSL); |
| 323 | + }); |
| 324 | + |
| 325 | + it('an edit sending back the served ssl block keeps the stored key', async () => { |
| 326 | + const { send, records } = makeDoor([{ ...STORED, ssl: { ...SSL, key: PLACEHOLDER_KEY } }]); |
| 327 | + expect((await send('PATCH', '/datasources/wh', { ssl: { ...SSL, rejectUnauthorized: false } })).status).toBe(200); |
| 328 | + expect(records.get('wh')?.ssl).toEqual({ ...SSL, rejectUnauthorized: false, key: PLACEHOLDER_KEY }); |
| 329 | + }); |
| 330 | +}); |
0 commit comments