CI (SIAX Cloud) / quality (push) Successful in 51s
Security Scan (SIAX Cloud) / security (push) Successful in 26s
CI (SIAX Cloud) / security (push) Successful in 28s
CI (SIAX Cloud) / integration (push) Successful in 1m1s
CI (SIAX Cloud) / integration (pull_request) Successful in 30s
CI (SIAX Cloud) / security (pull_request) Successful in 24s
CI (SIAX Cloud) / quality (pull_request) Successful in 48s
Security Scan (SIAX Cloud) / security (pull_request) Successful in 22s
Tenant isolation (security-critical): every app/api/0pt/** route reading
tenant_id from client-controlled query/body never cross-checked it against
the token middleware.ts already verified (x-0pt-verified-tenant). Adds a
shared assertTenantMatchesVerified guard and wires it into all 20+ affected
routes. Also closes two full-leak-by-omission bugs found in this pass
(documents/capture and lakehouse/mappings GETs returned every tenant's rows
when tenant_id was simply omitted) and two "no tenant check at all" reads
(rate-policies/[id], lakehouse/mappings/[id]) reachable by ID alone.
Duplicate-delivery idempotency (correctness-critical): POST /api/orders now
honors Idempotency-Key (sql/006, replay-or-409-on-conflict, Postgres +
in-memory); the Python orchestrator's POST /orders/{id}/run — the actual
delivery trigger — is now idempotent under an asyncio lock, closing a real
concurrent-request race, not just the sequential-retry case.
Engine-duplication resolution: adaptive_router.py's docstring wrongly
invited a bypass ("see b0ts for scraping") contradicting the product's own
compliance stance; corrected, and given real (additive, not escalating)
block-signal tracking so a rate-limited/blocked source is excluded from the
plan instead of hammered. Also fixes a previously-undiscovered production
bug: the Python engine's only call into the TS quality bridge
(X-Internal-Token) was unconditionally 401'd by middleware.ts, which never
checked that credential — the advisory quality bridge was unreachable in any
environment enforcing auth.
Restore drill: executed for real against throwaway Docker Postgres
containers (not simulated). Found and fixed two real blockers along the way:
sql/004 referenced opt_datasets, which no migration ever created (chain was
never actually runnable end to end); lib/db.ts hardcoded ssl:"require",
making any non-TLS Postgres (incl. compose.dev.yaml's own local-dev
container) unreachable. After both fixes: full migration chain applies
cleanly, pg_dump/pg_restore round-trip is byte-identical (row counts +
content checksums), and real application code (lib/orders-store.ts,
SourceRegistryService's Postgres repository) reads the restored data back
correctly, including cross-tenant isolation still holding.
82 new tests (55 TS + 27 Python), all passing; full existing suite (291 TS +
55 Python) still green; typecheck clean on every changed/added file.
Co-Authored-By: Claude Sonnet 5 <[email protected]>
184 lines
7.6 KiB
TypeScript
184 lines
7.6 KiB
TypeScript
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<string, DatasetIcebergMappingRecord> = 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<DatasetIcebergMapping> {
|
|
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<DatasetIcebergMapping> {
|
|
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<DatasetIcebergMapping | undefined> {
|
|
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<DatasetIcebergMapping[]> {
|
|
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,
|
|
};
|
|
}
|
|
}
|