@open-mercato/core 0.11.1-develop.8517.1.f52e23452d → 0.11.1-develop.8523.1.c6b0fd2058
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/modules/auth/lib/backendChrome.js +4 -1
- package/dist/modules/auth/lib/backendChrome.js.map +2 -2
- package/dist/modules/query_index/cli.js +7 -3
- package/dist/modules/query_index/cli.js.map +2 -2
- package/dist/modules/query_index/lib/batch.js +6 -1
- package/dist/modules/query_index/lib/batch.js.map +2 -2
- package/dist/modules/query_index/lib/indexer.js +14 -15
- package/dist/modules/query_index/lib/indexer.js.map +2 -2
- package/dist/modules/query_index/lib/reindexer.js +6 -3
- package/dist/modules/query_index/lib/reindexer.js.map +3 -3
- package/dist/modules/query_index/lib/search-tokens.js +10 -4
- package/dist/modules/query_index/lib/search-tokens.js.map +2 -2
- package/package.json +7 -7
- package/src/modules/auth/lib/backendChrome.tsx +7 -2
- package/src/modules/query_index/cli.ts +8 -3
- package/src/modules/query_index/lib/batch.ts +13 -0
- package/src/modules/query_index/lib/indexer.ts +13 -14
- package/src/modules/query_index/lib/reindexer.ts +5 -2
- package/src/modules/query_index/lib/search-tokens.ts +22 -3
|
@@ -23,6 +23,7 @@ import {
|
|
|
23
23
|
type AnyRow,
|
|
24
24
|
} from './lib/batch'
|
|
25
25
|
import { createSearchTokenGateQueryEngine } from './lib/search-entity-policy'
|
|
26
|
+
import { resolveCiphertextGuard } from './lib/search-tokens'
|
|
26
27
|
import { reindexEntity, isTenantlessReindexTarget, DEFAULT_REINDEX_PARTITIONS } from './lib/reindexer'
|
|
27
28
|
import type { ReindexJobResult } from './lib/reindexer'
|
|
28
29
|
|
|
@@ -216,15 +217,19 @@ async function rebuildEntityIndexes(options: RebuildExecutionOptions): Promise<R
|
|
|
216
217
|
doc: Record<string, unknown>,
|
|
217
218
|
scope: { organizationId: string | null; tenantId: string | null },
|
|
218
219
|
) => {
|
|
220
|
+
const tenantId = scope.tenantId ?? null
|
|
221
|
+
if (dekKeyCache.get(tenantId) === null) dekKeyCache.delete(tenantId)
|
|
219
222
|
return await decryptIndexDocForSearch(
|
|
220
223
|
targetEntity,
|
|
221
224
|
doc,
|
|
222
|
-
{ tenantId
|
|
225
|
+
{ tenantId, organizationId: scope.organizationId ?? null },
|
|
223
226
|
encryption,
|
|
224
227
|
dekKeyCache,
|
|
225
228
|
)
|
|
226
229
|
}
|
|
227
230
|
|
|
231
|
+
const ciphertextGuard = () => resolveCiphertextGuard(encryption)
|
|
232
|
+
|
|
228
233
|
const applyFilters = <QB extends { where: (...args: any[]) => QB }>(q: QB): QB => {
|
|
229
234
|
let chain = q
|
|
230
235
|
if (!global) {
|
|
@@ -249,7 +254,7 @@ async function rebuildEntityIndexes(options: RebuildExecutionOptions): Promise<R
|
|
|
249
254
|
.executeTakeFirst() as AnyRow | undefined
|
|
250
255
|
if (!row) return { processed: 0, matched: 0 }
|
|
251
256
|
const bar = createProgressBar(progressLabel ?? `Rebuilding ${entityType}`, 1)
|
|
252
|
-
const singleResult = await upsertIndexBatch(db, entityType, [row], { orgId: orgOverride, tenantId: tenantOverride }, { encryptDoc, decryptDoc, searchTokenGateQueryEngine })
|
|
257
|
+
const singleResult = await upsertIndexBatch(db, entityType, [row], { orgId: orgOverride, tenantId: tenantOverride }, { encryptDoc, decryptDoc, searchTokenGateQueryEngine, ciphertextGuard })
|
|
253
258
|
bar.update(singleResult.written)
|
|
254
259
|
bar.complete()
|
|
255
260
|
assertIndexBatchWritesLanded(entityType, singleResult)
|
|
@@ -291,7 +296,7 @@ async function rebuildEntityIndexes(options: RebuildExecutionOptions): Promise<R
|
|
|
291
296
|
const chunkResult = await upsertIndexBatch(db, entityType, chunk as AnyRow[], {
|
|
292
297
|
orgId: orgOverride,
|
|
293
298
|
tenantId: tenantOverride,
|
|
294
|
-
}, { encryptDoc, decryptDoc, searchTokenGateQueryEngine })
|
|
299
|
+
}, { encryptDoc, decryptDoc, searchTokenGateQueryEngine, ciphertextGuard })
|
|
295
300
|
mergeUpsertIndexBatchResults(writeTotals, chunkResult)
|
|
296
301
|
|
|
297
302
|
processed += chunkResult.written
|
|
@@ -105,6 +105,13 @@ export type IndexBatchOptions = {
|
|
|
105
105
|
) => Promise<Record<string, unknown> | null | undefined>
|
|
106
106
|
/** Query engine handed to the entity's `shouldIndex` gates before its search tokens are written. */
|
|
107
107
|
searchTokenGateQueryEngine?: QueryEngine
|
|
108
|
+
/**
|
|
109
|
+
* Whether the token writer must skip ciphertext-shaped fields, evaluated per row straight after
|
|
110
|
+
* that row's `decryptDoc`, so a Vault that recovers mid-batch cannot clear the guard for rows
|
|
111
|
+
* decrypted while it was down. Pass `() => resolveCiphertextGuard(encryption)` with the service
|
|
112
|
+
* `decryptDoc` uses. A row whose `decryptDoc` throws is always guarded.
|
|
113
|
+
*/
|
|
114
|
+
ciphertextGuard?: () => boolean
|
|
108
115
|
}
|
|
109
116
|
|
|
110
117
|
function normalizeId(value: unknown): string {
|
|
@@ -123,6 +130,7 @@ type IndexRowPayload = {
|
|
|
123
130
|
tenant_id: string | null
|
|
124
131
|
doc: Record<string, unknown>
|
|
125
132
|
tokenDoc: Record<string, unknown>
|
|
133
|
+
guardCiphertext?: boolean
|
|
126
134
|
index_version: number
|
|
127
135
|
}
|
|
128
136
|
|
|
@@ -296,6 +304,7 @@ export async function upsertIndexBatch(
|
|
|
296
304
|
const scopeTenant = built.tenantId
|
|
297
305
|
let doc = built.doc
|
|
298
306
|
let tokenDoc: Record<string, unknown> = doc
|
|
307
|
+
let guardCiphertext: boolean | undefined
|
|
299
308
|
if (typeof options.encryptDoc === 'function') {
|
|
300
309
|
try {
|
|
301
310
|
const encrypted = await options.encryptDoc(entityType, doc, {
|
|
@@ -336,6 +345,7 @@ export async function upsertIndexBatch(
|
|
|
336
345
|
// Only affects the search tokens built below; the indexed document itself is
|
|
337
346
|
// unharmed, so the row still counts as written.
|
|
338
347
|
logger.warn('Failed to decrypt index document for search tokens', { entityType, recordId })
|
|
348
|
+
guardCiphertext = true
|
|
339
349
|
await recordIndexerError(
|
|
340
350
|
{ db },
|
|
341
351
|
{
|
|
@@ -350,6 +360,7 @@ export async function upsertIndexBatch(
|
|
|
350
360
|
).catch(() => undefined)
|
|
351
361
|
}
|
|
352
362
|
}
|
|
363
|
+
guardCiphertext ??= options.ciphertextGuard?.()
|
|
353
364
|
basePayloads.push({
|
|
354
365
|
entity_type: entityType,
|
|
355
366
|
entity_id: recordId,
|
|
@@ -357,6 +368,7 @@ export async function upsertIndexBatch(
|
|
|
357
368
|
tenant_id: scopeTenant ?? null,
|
|
358
369
|
doc,
|
|
359
370
|
tokenDoc,
|
|
371
|
+
...(guardCiphertext === undefined ? {} : { guardCiphertext }),
|
|
360
372
|
index_version: 1,
|
|
361
373
|
})
|
|
362
374
|
if (debugEnabled) {
|
|
@@ -410,6 +422,7 @@ export async function upsertIndexBatch(
|
|
|
410
422
|
organizationId: payload.organization_id,
|
|
411
423
|
tenantId: payload.tenant_id,
|
|
412
424
|
doc: payload.tokenDoc,
|
|
425
|
+
...(payload.guardCiphertext === undefined ? {} : { guardCiphertext: payload.guardCiphertext }),
|
|
413
426
|
}))
|
|
414
427
|
|
|
415
428
|
const writeSearchTokens = async (payloads = tokenPayloads): Promise<void> => {
|
|
@@ -16,7 +16,7 @@ import { recordIndexerError } from '@open-mercato/shared/lib/indexers/error-log'
|
|
|
16
16
|
import { getTelemetryRuntime } from '@open-mercato/shared/lib/telemetry/runtime'
|
|
17
17
|
import { tryGetModules } from '@open-mercato/shared/lib/modules/registry'
|
|
18
18
|
import { resolveSearchConfig } from '@open-mercato/shared/lib/search/config'
|
|
19
|
-
import { replaceSearchTokensForRecord, deleteSearchTokensForRecord } from './search-tokens'
|
|
19
|
+
import { replaceSearchTokensForRecord, deleteSearchTokensForRecord, resolveCiphertextGuard } from './search-tokens'
|
|
20
20
|
import { attachAggregateSearchField, rebuildAggregateSearchField } from './document'
|
|
21
21
|
import { applyIndexDocEnrichers } from './doc-enrichers'
|
|
22
22
|
import {
|
|
@@ -454,22 +454,20 @@ export async function reindexSearchTokensForRecord(
|
|
|
454
454
|
}, { trx: args.trx })
|
|
455
455
|
return
|
|
456
456
|
}
|
|
457
|
-
const
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
)
|
|
467
|
-
})()
|
|
457
|
+
const encryption = resolveTenantEncryptionService(em as any)
|
|
458
|
+
const tokenDoc = args.searchTokenDoc ?? await decryptIndexDocForSearch(
|
|
459
|
+
args.entityType,
|
|
460
|
+
args.doc,
|
|
461
|
+
{ tenantId: args.tenantId ?? null, organizationId: args.organizationId ?? null },
|
|
462
|
+
encryption,
|
|
463
|
+
new Map<string | null, string | null>(),
|
|
464
|
+
)
|
|
465
|
+
const guardCiphertext = resolveCiphertextGuard(encryption)
|
|
468
466
|
const gates = resolveSearchTokenIndexGates(args.entityType)
|
|
469
467
|
if (gates.length) {
|
|
470
468
|
const allowed = await passesSearchTokenIndexGates(args.entityType, gates, {
|
|
471
469
|
recordId: String(args.recordId),
|
|
472
|
-
record:
|
|
470
|
+
record: tokenDoc,
|
|
473
471
|
tenantId: args.tenantId ?? null,
|
|
474
472
|
organizationId: args.organizationId ?? null,
|
|
475
473
|
queryEngine: createSearchTokenGateQueryEngine(em),
|
|
@@ -491,7 +489,7 @@ export async function reindexSearchTokensForRecord(
|
|
|
491
489
|
// rebuild and the token writer — that also makes the aggregate's blocklist filter and
|
|
492
490
|
// the per-field one provably read the same snapshot, as the batch path already does.
|
|
493
491
|
const searchConfig = resolveSearchConfig()
|
|
494
|
-
const searchDoc = rebuildAggregateSearchField(
|
|
492
|
+
const searchDoc = rebuildAggregateSearchField(tokenDoc, {
|
|
495
493
|
entityType: args.entityType,
|
|
496
494
|
config: searchConfig,
|
|
497
495
|
})
|
|
@@ -502,6 +500,7 @@ export async function reindexSearchTokensForRecord(
|
|
|
502
500
|
tenantId: args.tenantId ?? null,
|
|
503
501
|
doc: searchDoc,
|
|
504
502
|
config: searchConfig,
|
|
503
|
+
guardCiphertext,
|
|
505
504
|
}, { trx: args.trx })
|
|
506
505
|
}
|
|
507
506
|
|
|
@@ -21,7 +21,7 @@ import {
|
|
|
21
21
|
import { prepareJob, updateJobProgress, finalizeJob, type JobScope } from './jobs'
|
|
22
22
|
import { purgeOrphans, retireScopedCatalogueRows } from './stale'
|
|
23
23
|
import type { VectorIndexService } from '@open-mercato/search/vector'
|
|
24
|
-
import { isSearchDebugEnabled } from './search-tokens'
|
|
24
|
+
import { isSearchDebugEnabled, resolveCiphertextGuard } from './search-tokens'
|
|
25
25
|
import { isTenantGlobalEntityType } from './tenant-global'
|
|
26
26
|
import { createLogger } from '@open-mercato/shared/lib/logger'
|
|
27
27
|
import { getTelemetryRuntime } from '@open-mercato/shared/lib/telemetry/runtime'
|
|
@@ -653,10 +653,12 @@ export async function reindexEntity(
|
|
|
653
653
|
doc: Record<string, unknown>,
|
|
654
654
|
scope: { organizationId: string | null; tenantId: string | null },
|
|
655
655
|
) => {
|
|
656
|
+
const tenantId = scope.tenantId ?? null
|
|
657
|
+
if (dekKeyCache.get(tenantId) === null) dekKeyCache.delete(tenantId)
|
|
656
658
|
const result = await decryptIndexDocForSearch(
|
|
657
659
|
targetEntity,
|
|
658
660
|
doc,
|
|
659
|
-
{ tenantId
|
|
661
|
+
{ tenantId, organizationId: scope.organizationId ?? null },
|
|
660
662
|
encryption,
|
|
661
663
|
dekKeyCache,
|
|
662
664
|
)
|
|
@@ -671,6 +673,7 @@ export async function reindexEntity(
|
|
|
671
673
|
encryptDoc,
|
|
672
674
|
decryptDoc,
|
|
673
675
|
searchTokenGateQueryEngine,
|
|
676
|
+
ciphertextGuard: () => resolveCiphertextGuard(encryption),
|
|
674
677
|
})
|
|
675
678
|
mergeUpsertIndexBatchResults(writeTotals, batchResult)
|
|
676
679
|
|
|
@@ -102,6 +102,23 @@ function shouldGuardCiphertext(): boolean {
|
|
|
102
102
|
return resolveEncryptionMode(guardKmsService) !== 'active'
|
|
103
103
|
}
|
|
104
104
|
|
|
105
|
+
export type CiphertextGuardSource = { isEnabled?: () => boolean } | null | undefined
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* The guard decision for a document decrypted by `encryption`, read AFTER that decrypt.
|
|
109
|
+
*
|
|
110
|
+
* {@link shouldGuardCiphertext} asks a KMS that never fetches a key, so its Vault circuit breaker
|
|
111
|
+
* never trips: it reports `active` through a real outage while the decrypt that ran just before
|
|
112
|
+
* got no DEK and handed back ciphertext. `encryption.isEnabled()` is the toggle ANDed with the
|
|
113
|
+
* health of the KMS whose `getDek` just called Vault, and it is the same signal
|
|
114
|
+
* `decryptIndexDocForSearch` uses to skip decryption, so the decrypt and the guard cannot disagree.
|
|
115
|
+
* Callers without a service keep the process-level check.
|
|
116
|
+
*/
|
|
117
|
+
export function resolveCiphertextGuard(encryption: CiphertextGuardSource): boolean {
|
|
118
|
+
if (typeof encryption?.isEnabled !== 'function') return shouldGuardCiphertext()
|
|
119
|
+
return !encryption.isEnabled()
|
|
120
|
+
}
|
|
121
|
+
|
|
105
122
|
/**
|
|
106
123
|
* Fields whose value is an AES-GCM envelope rather than the text it is supposed to hold.
|
|
107
124
|
*
|
|
@@ -516,10 +533,12 @@ export async function replaceSearchTokensForBatch(
|
|
|
516
533
|
// here would still delete the tokens we are trying to protect. Skipping the record leaves every
|
|
517
534
|
// one of its tokens, encrypted-field and plaintext-field alike, exactly as it was. The state is
|
|
518
535
|
// transient by construction: `decrypt-database` followed by a reindex rebuilds all of it.
|
|
519
|
-
|
|
536
|
+
let processGuard: boolean | undefined
|
|
537
|
+
const guardFor = (payload: BuildTokenOptions): boolean =>
|
|
538
|
+
payload.guardCiphertext ?? (processGuard ??= shouldGuardCiphertext())
|
|
520
539
|
const preservedRecordIds = new Set<string>()
|
|
521
540
|
const payloads = indexablePayloads.filter((payload) => {
|
|
522
|
-
const ciphertextFields = ciphertextFieldsOf(payload.doc,
|
|
541
|
+
const ciphertextFields = ciphertextFieldsOf(payload.doc, guardFor(payload))
|
|
523
542
|
if (!ciphertextFields.size) return true
|
|
524
543
|
warnCiphertextSkipped(payload.entityType, payload.tenantId ?? null, ciphertextFields)
|
|
525
544
|
preservedRecordIds.add(String(payload.recordId))
|
|
@@ -527,7 +546,7 @@ export async function replaceSearchTokensForBatch(
|
|
|
527
546
|
})
|
|
528
547
|
if (!payloads.length) return
|
|
529
548
|
|
|
530
|
-
const rows = payloads.flatMap((payload) => buildSearchTokenRows({ ...payload, config, guardCiphertext }))
|
|
549
|
+
const rows = payloads.flatMap((payload) => buildSearchTokenRows({ ...payload, config, guardCiphertext: guardFor(payload) }))
|
|
531
550
|
if (!rows.length) {
|
|
532
551
|
const entityType = payloads[0]?.entityType
|
|
533
552
|
if (!entityType) return
|