|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +import { describe, it, expect, vi } from 'vitest'; |
| 4 | +import { HonoHttpServer } from '@objectstack/plugin-hono-server'; |
| 5 | +import { registerDatasourceAdminRoutes } from '../admin-routes.js'; |
| 6 | +import { |
| 7 | + DatasourceAdminService, |
| 8 | + type DatasourceAdminServiceConfig, |
| 9 | + type ProbeInput, |
| 10 | + type StoredDatasource, |
| 11 | +} from '../datasource-admin-service.js'; |
| 12 | +import { |
| 13 | + ENTITLED_CREDENTIAL, |
| 14 | + createSessionAuthService, |
| 15 | + createGrantsEngine, |
| 16 | +} from './entitled-caller.fixture.js'; |
| 17 | + |
| 18 | +/** |
| 19 | + * #28928 — an empty `secret` is refused at the datasource admin doors, never |
| 20 | + * sealed as the credential. |
| 21 | + * |
| 22 | + * `POST /api/v1/datasources` and `PATCH /api/v1/datasources/:name` take a |
| 23 | + * door-only `secret` and seal it into the secret store. `"secret": ""` was |
| 24 | + * read as a value: the create sealed an empty credential, and an update |
| 25 | + * minted a new `sys_secret` holding an empty value, repointed |
| 26 | + * `external.credentialsRef` at it and removed the working one. The pool in |
| 27 | + * service kept running, so the datasource broke only at the next connect. |
| 28 | + * |
| 29 | + * Now both doors refuse it `400` naming `secret`, before any write: no secret |
| 30 | + * is written, the stored record and its `credentialsRef` are unchanged, and no |
| 31 | + * secret is removed. `null`, an absent `secret` and a real one are unchanged. |
| 32 | + * |
| 33 | + * Driven through the REAL routes over the REAL service on in-memory stores |
| 34 | + * (the harness of `datasource-admin-declared-keys-kept.test.ts`). Every |
| 35 | + * secret below is an obvious placeholder. |
| 36 | + */ |
| 37 | + |
| 38 | +const authService = createSessionAuthService(); |
| 39 | +const grantsEngine = createGrantsEngine(); |
| 40 | + |
| 41 | +const BASE = { driver: 'postgres', config: { host: 'db.example.invalid', database: 'analytics', username: 'qa' } }; |
| 42 | +const WORKING_REF = 'sys_secret:sec_working'; |
| 43 | +const PLACEHOLDER_SECRET = 'dummy-rotated-password'; |
| 44 | + |
| 45 | +/** In-memory stores behind a real {@link DatasourceAdminService}, recording every seam call. */ |
| 46 | +function makeDoor(seed: StoredDatasource[] = []) { |
| 47 | + const records = new Map<string, StoredDatasource>(seed.map((r) => [r.name, structuredClone(r)])); |
| 48 | + const ops: string[] = []; |
| 49 | + const pooled: StoredDatasource[] = []; |
| 50 | + const probes: ProbeInput[] = []; |
| 51 | + const written: string[] = []; |
| 52 | + |
| 53 | + const config: DatasourceAdminServiceConfig = { |
| 54 | + probe: async (input) => { |
| 55 | + ops.push('probe'); |
| 56 | + probes.push(structuredClone(input)); |
| 57 | + return { ok: true, latencyMs: 1 }; |
| 58 | + }, |
| 59 | + listDatasourceRecords: async () => [...records.values()].map((r) => structuredClone(r)), |
| 60 | + getDatasourceRecord: async (name) => { |
| 61 | + const r = records.get(name); |
| 62 | + return r ? structuredClone(r) : undefined; |
| 63 | + }, |
| 64 | + putDatasourceRecord: async (record) => { |
| 65 | + ops.push(`put:${record.name}`); |
| 66 | + records.set(record.name, structuredClone(record)); |
| 67 | + }, |
| 68 | + deleteDatasourceRecord: async (name) => { |
| 69 | + records.delete(name); |
| 70 | + }, |
| 71 | + writeSecret: async (input, hint) => { |
| 72 | + ops.push('writeSecret'); |
| 73 | + written.push(input.value); |
| 74 | + return `sys_secret:sec_${hint.name}_${written.length}`; |
| 75 | + }, |
| 76 | + removeSecret: async (ref) => { |
| 77 | + ops.push(`removeSecret:${ref}`); |
| 78 | + }, |
| 79 | + countBoundObjects: async () => 0, |
| 80 | + listObjectsBoundTo: async () => [], |
| 81 | + registerPool: (record) => { |
| 82 | + ops.push('registerPool'); |
| 83 | + pooled.push(structuredClone(record)); |
| 84 | + }, |
| 85 | + unregisterPool: () => { |
| 86 | + ops.push('unregisterPool'); |
| 87 | + }, |
| 88 | + reregisterPool: (_previous, next) => { |
| 89 | + ops.push('reregisterPool'); |
| 90 | + pooled.push(structuredClone(next)); |
| 91 | + }, |
| 92 | + }; |
| 93 | + |
| 94 | + const service = new DatasourceAdminService(config); |
| 95 | + const server = new HonoHttpServer(0); |
| 96 | + const ctx = { |
| 97 | + getService: vi.fn((name: string) => |
| 98 | + name === 'auth' |
| 99 | + ? authService |
| 100 | + : name === 'objectql' || name === 'data' |
| 101 | + ? grantsEngine |
| 102 | + : name === 'datasource-admin' |
| 103 | + ? service |
| 104 | + : undefined, |
| 105 | + ), |
| 106 | + } as any; |
| 107 | + registerDatasourceAdminRoutes(server, ctx, '/api/v1'); |
| 108 | + const app = server.getRawApp(); |
| 109 | + |
| 110 | + const send = (method: string, path: string, body?: unknown) => |
| 111 | + app.fetch( |
| 112 | + new Request(`http://local/api/v1${path}`, { |
| 113 | + method, |
| 114 | + headers: { 'content-type': 'application/json', authorization: ENTITLED_CREDENTIAL }, |
| 115 | + ...(body === undefined ? {} : { body: JSON.stringify(body) }), |
| 116 | + }), |
| 117 | + ); |
| 118 | + |
| 119 | + return { send, records, ops, pooled, probes, written }; |
| 120 | +} |
| 121 | + |
| 122 | +/** A runtime datasource bound to a working credential. */ |
| 123 | +const STORED: StoredDatasource = { |
| 124 | + name: 'wh', |
| 125 | + ...BASE, |
| 126 | + origin: 'runtime', |
| 127 | + external: { credentialsRef: WORKING_REF }, |
| 128 | +} as StoredDatasource; |
| 129 | + |
| 130 | +async function expectSecretRefusal(res: Response): Promise<string> { |
| 131 | + const body = (await res.json()) as { success: boolean; error?: { code: string; message: string } }; |
| 132 | + expect({ status: res.status, success: body.success, code: body.error?.code }).toEqual({ |
| 133 | + status: 400, |
| 134 | + success: false, |
| 135 | + code: 'DATASOURCE_ADMIN_ERROR', |
| 136 | + }); |
| 137 | + expect(body.error!.message).toContain('`secret`'); |
| 138 | + return body.error!.message; |
| 139 | +} |
| 140 | + |
| 141 | +describe('PATCH /api/v1/datasources/:name — an empty secret (#28928)', () => { |
| 142 | + for (const [label, secret] of [['an empty string', ''], ['an empty `value` object', { value: '' }]] as const) { |
| 143 | + it(`${label} is refused 400 naming \`secret\`; the credential, its ref and the record are unchanged`, async () => { |
| 144 | + const { send, records, ops, written } = makeDoor([STORED]); |
| 145 | + await expectSecretRefusal(await send('PATCH', '/datasources/wh', { secret })); |
| 146 | + expect(records.get('wh')).toEqual(STORED); |
| 147 | + expect(written).toEqual([]); |
| 148 | + expect(ops).toEqual([]); |
| 149 | + }); |
| 150 | + } |
| 151 | + |
| 152 | + it('control: a real secret still rotates (a new ref, the old one removed)', async () => { |
| 153 | + const { send, records, ops, written } = makeDoor([STORED]); |
| 154 | + expect((await send('PATCH', '/datasources/wh', { secret: PLACEHOLDER_SECRET })).status).toBe(200); |
| 155 | + expect(written).toEqual([PLACEHOLDER_SECRET]); |
| 156 | + expect(records.get('wh')?.external?.credentialsRef).toBe('sys_secret:sec_wh_1'); |
| 157 | + expect(ops).toContain(`removeSecret:${WORKING_REF}`); |
| 158 | + }); |
| 159 | + |
| 160 | + for (const [label, body] of [['`secret: null`', { secret: null, label: 'Warehouse' }], ['no `secret`', { label: 'Warehouse' }]] as const) { |
| 161 | + it(`control: ${label} changes no credential`, async () => { |
| 162 | + const { send, records, ops, written } = makeDoor([STORED]); |
| 163 | + expect((await send('PATCH', '/datasources/wh', body)).status).toBe(200); |
| 164 | + expect(written).toEqual([]); |
| 165 | + expect(records.get('wh')?.external?.credentialsRef).toBe(WORKING_REF); |
| 166 | + expect(ops.some((op) => op.startsWith('removeSecret'))).toBe(false); |
| 167 | + }); |
| 168 | + } |
| 169 | +}); |
| 170 | + |
| 171 | +describe('POST /api/v1/datasources — an empty secret (#28928)', () => { |
| 172 | + it('is refused 400 naming `secret`; nothing is stored and no secret is written', async () => { |
| 173 | + const { send, records, ops, written } = makeDoor(); |
| 174 | + await expectSecretRefusal(await send('POST', '/datasources', { name: 'wh', ...BASE, secret: '' })); |
| 175 | + expect(records.has('wh')).toBe(false); |
| 176 | + expect(written).toEqual([]); |
| 177 | + expect(ops).toEqual([]); |
| 178 | + }); |
| 179 | + |
| 180 | + it('control: a real secret is sealed and bound', async () => { |
| 181 | + const { send, records, written } = makeDoor(); |
| 182 | + expect((await send('POST', '/datasources', { name: 'wh', ...BASE, secret: PLACEHOLDER_SECRET })).status).toBe(201); |
| 183 | + expect(written).toEqual([PLACEHOLDER_SECRET]); |
| 184 | + expect(records.get('wh')?.external?.credentialsRef).toBe('sys_secret:sec_wh_1'); |
| 185 | + }); |
| 186 | + |
| 187 | + it('control: `secret: null` creates the datasource with no credential', async () => { |
| 188 | + const { send, records, written } = makeDoor(); |
| 189 | + expect((await send('POST', '/datasources', { name: 'wh', ...BASE, secret: null })).status).toBe(201); |
| 190 | + expect(written).toEqual([]); |
| 191 | + expect(records.get('wh')?.external?.credentialsRef).toBeUndefined(); |
| 192 | + }); |
| 193 | +}); |
0 commit comments