import type { DatasetIcebergMapping, DatasetIcebergMappingStatus, CreateDatasetIcebergMappingInput, } from "../types/dataset-iceberg-mapping"; import { createAud0Client, type Aud0Client } from "../clients/aud0-client"; import { DatasetService } from "./dataset-service"; interface DatasetIcebergMappingRecord { dataset_iceberg_mapping_id: string; tenant_id: string; project_id: string; dataset_id: string; iceberg_table_id: string; namespace_id: string | null; namespace: string | null; table_name: string | null; mapping_status: string; mapped_at: string; mapped_by: string | null; correlation_id: string | null; created_at: string; updated_at: string | null; } /** * Owns 0pt.dataset_iceberg_mapping.schema.v1 -- the pointer from a 0pt * dataset to the Iceberg table it writes to. `iceberg_table_id` is always a * REFERENCE into ST0RE's table registry (never a copy of ST0RE's table row); * this service holds no table/catalog/snapshot state itself. See * docs/ICEBERG_WRITE_BOUNDARY.md. * * This service does not call ST0RE directly -- it records a mapping to an * `iceberg_table_id` that already exists in ST0RE's registry. * `IcebergWriteService.mapDatasetToIcebergTable` (src/services/ * iceberg-write-service.ts) is the higher-level flow that registers a NEW * table in ST0RE first and then calls this service to record the mapping -- * kept as two composable steps so a mapping can also be created against an * already-registered table (e.g. one N0D or another 0pt job already set up). */ export class DatasetIcebergMappingService { private mappings: Map = new Map(); private aud0: Aud0Client; private datasetService: DatasetService; constructor(opts?: { aud0BaseUrl?: string; datasetService?: DatasetService }) { this.aud0 = createAud0Client(opts?.aud0BaseUrl); this.datasetService = opts?.datasetService ?? new DatasetService(opts?.aud0BaseUrl); } async createMapping(input: CreateDatasetIcebergMappingInput): Promise { if (this.mappings.has(input.dataset_iceberg_mapping_id)) { throw new Error(`Dataset Iceberg mapping ${input.dataset_iceberg_mapping_id} already exists`); } if (!input.iceberg_table_id) { throw new Error("iceberg_table_id is required (reference into ST0RE's table registry)"); } // Verify the dataset exists -- a mapping with no dataset is meaningless. try { await this.datasetService.getDataset(input.dataset_id); } catch { throw new Error(`Dataset ${input.dataset_id} not found`); } const now = new Date().toISOString(); const record: DatasetIcebergMappingRecord = { dataset_iceberg_mapping_id: input.dataset_iceberg_mapping_id, tenant_id: input.tenant_id, project_id: input.project_id, dataset_id: input.dataset_id, iceberg_table_id: input.iceberg_table_id, namespace_id: input.namespace_id ?? null, namespace: input.namespace ?? null, table_name: input.table_name ?? null, mapping_status: input.mapping_status ?? "active", mapped_at: now, mapped_by: input.mapped_by ?? null, correlation_id: input.correlation_id ?? null, created_at: now, updated_at: null, }; this.mappings.set(input.dataset_iceberg_mapping_id, record); // 0pt.dataset_iceberg_table_mapped -- docs/11-lakehouse-write-plan.md event list, // docs/ICEBERG_WRITE_BOUNDARY.md. Fired when the mapping is created/updated. await this.aud0.ingestEvent({ event_id: `evt_0pt_dataset_iceberg_table_mapped_${input.dataset_iceberg_mapping_id}`, event_type: "0pt.dataset_iceberg_table_mapped", tenant_id: input.tenant_id, project_id: input.project_id, actor_id: input.mapped_by ?? "system", app_id: "0pt", capability_id: "0pt.map_dataset_iceberg_table", resource_type: "dataset_iceberg_mapping", resource_id: input.dataset_iceberg_mapping_id, risk_level: "L2", payload: { dataset_id: input.dataset_id, iceberg_table_id: input.iceberg_table_id, namespace_id: input.namespace_id, namespace: input.namespace, table_name: input.table_name, }, created_at: now, correlation_id: input.correlation_id ?? "", }); // Lineage: dataset -> iceberg table. await this.aud0.ingestLineageEvent({ lineage_event_id: `lineage_0pt_dataset_iceberg_${input.dataset_iceberg_mapping_id}`, tenant_id: input.tenant_id, project_id: input.project_id, app_id: "0pt", capability_id: "0pt.map_dataset_iceberg_table", source_type: "dataset", source_id: input.dataset_id, target_type: "iceberg_table", target_id: input.iceberg_table_id, lineage_type: "stored_as", metadata: { dataset_iceberg_mapping_id: input.dataset_iceberg_mapping_id }, created_at: now, correlation_id: input.correlation_id ?? "", }); return this.toMapping(record); } // P014: `tenantId` is optional so trusted in-process callers (existing // unit tests, and internal callers that already resolved the tenant some // other way) are unaffected -- but both HTTP routes that reach this method // now always pass it, closing a real gap: `getMapping` used to be // reachable by mapping ID alone with NO tenant check anywhere, so any // authenticated caller could read -- or, via requestIcebergWrite below, // WRITE a new snapshot onto -- another tenant's dataset<->Iceberg-table // mapping just by guessing/enumerating dataset_iceberg_mapping_id. A // mismatched tenantId reports identically to "not found" (no existence // oracle), matching SourceRegistryService.assertSourceTenant's convention. async getMapping(mappingId: string, tenantId?: string): Promise { const record = this.mappings.get(mappingId); if (!record || (tenantId && record.tenant_id !== tenantId)) { throw new Error(`Dataset Iceberg mapping ${mappingId} not found`); } return this.toMapping(record); } async getMappingForDataset(datasetId: string): Promise { const record = Array.from(this.mappings.values()).find((r) => r.dataset_id === datasetId && r.mapping_status === "active"); return record ? this.toMapping(record) : undefined; } async listMappings(filter?: { tenant_id?: string; project_id?: string; dataset_id?: string; mapping_status?: DatasetIcebergMappingStatus; }): Promise { let results = Array.from(this.mappings.values()); if (filter?.tenant_id) results = results.filter((r) => r.tenant_id === filter.tenant_id); if (filter?.project_id) results = results.filter((r) => r.project_id === filter.project_id); if (filter?.dataset_id) results = results.filter((r) => r.dataset_id === filter.dataset_id); if (filter?.mapping_status) results = results.filter((r) => r.mapping_status === filter.mapping_status); return results.map((r) => this.toMapping(r)); } private toMapping(r: DatasetIcebergMappingRecord): DatasetIcebergMapping { return { dataset_iceberg_mapping_id: r.dataset_iceberg_mapping_id, tenant_id: r.tenant_id, project_id: r.project_id, dataset_id: r.dataset_id, iceberg_table_id: r.iceberg_table_id, namespace_id: r.namespace_id ?? undefined, namespace: r.namespace ?? undefined, table_name: r.table_name ?? undefined, mapping_status: r.mapping_status as DatasetIcebergMappingStatus, mapped_at: r.mapped_at, mapped_by: r.mapped_by ?? undefined, correlation_id: r.correlation_id ?? undefined, created_at: r.created_at, updated_at: r.updated_at ?? undefined, }; } }