@nodii/dsar 0.8.3 → 0.9.0
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.
|
@@ -1,6 +1,64 @@
|
|
|
1
1
|
type SqlClient = {
|
|
2
2
|
unsafe: (sql: string, args?: unknown[]) => Promise<any>;
|
|
3
3
|
};
|
|
4
|
+
/**
|
|
5
|
+
* Emit the canonical 5-policy A1 RLS DDL for a tenant-scoped `tenant_id UUID`
|
|
6
|
+
* table per 08-rls-doctrine v7 § 1.1 + D412 (locked 2026-06-23):
|
|
7
|
+
*
|
|
8
|
+
* ENABLE ROW LEVEL SECURITY
|
|
9
|
+
* + nodii_owner FOR ALL bypass policy (BYPASSRLS role)
|
|
10
|
+
* + authenticated per-verb policies gated on
|
|
11
|
+
* `auth.user_tenant_ids(auth.get_user_id())`
|
|
12
|
+
* + nodii_services per-verb policies gated on the `app.tenant_id` GUC
|
|
13
|
+
* (`auth.get_tenant_id()`)
|
|
14
|
+
*
|
|
15
|
+
* This is the BYTE-EQUAL output of `@nodii/db-rls`'s `withRls(table, { allow:
|
|
16
|
+
* [SELECT,INSERT,UPDATE,DELETE] })` — NOT a hand-rolled predicate. It is
|
|
17
|
+
* re-derived here (rather than imported) only so the migration stays a static
|
|
18
|
+
* string constant with no `@nodii/db-rls` build-time dependency; the
|
|
19
|
+
* `withRls-parity.test.ts` suite asserts this function is line-for-line
|
|
20
|
+
* equivalent to the live `@nodii/db-rls` generator so it can never drift.
|
|
21
|
+
*
|
|
22
|
+
* Idempotent: each `CREATE POLICY` is preceded by `DROP POLICY IF EXISTS`
|
|
23
|
+
* (PG <18 has no `CREATE POLICY IF NOT EXISTS`) and `ENABLE ROW LEVEL
|
|
24
|
+
* SECURITY` is a natural no-op on re-run — so `applyDsarMigrations` stays
|
|
25
|
+
* boot-safe.
|
|
26
|
+
*
|
|
27
|
+
* The `auth.*` helpers are owned by `@nodii/db-rls`'s auth schema, which the
|
|
28
|
+
* adopting service installs + binds (08-rls § 7). Postgres resolves them
|
|
29
|
+
* lazily (at query time, not CREATE POLICY time), and the BYPASSRLS owner pool
|
|
30
|
+
* dsar uses for its own writes/reaper is unaffected.
|
|
31
|
+
*/
|
|
32
|
+
export declare function a1RlsDDL(table: string): string;
|
|
33
|
+
/**
|
|
34
|
+
* Emit `a1RlsDDL(table)` as the migration ships it. The raw `a1RlsDDL` is the
|
|
35
|
+
* canonical DDL (and the parity-tested surface); this splits it into a
|
|
36
|
+
* fail-CLOSED unconditional half + a uuid-guarded half.
|
|
37
|
+
*
|
|
38
|
+
* **Unconditional (always emitted — works on a TEXT or a UUID `tenant_id`):**
|
|
39
|
+
* `ENABLE ROW LEVEL SECURITY` + the non-owner `GRANT`s + the `nodii_owner`
|
|
40
|
+
* FOR-ALL bypass policy. None of these reference `tenant_id`'s type. The effect
|
|
41
|
+
* on a still-TEXT table is **fail-CLOSED**: RLS is ON and the only policy is the
|
|
42
|
+
* owner bypass, so `nodii_services` / `authenticated` (which now hold the table
|
|
43
|
+
* GRANT) match NO policy → deny-all. Tenant data is never exposed.
|
|
44
|
+
*
|
|
45
|
+
* **Guarded on `tenant_id` being `uuid`:** the per-verb `authenticated` +
|
|
46
|
+
* `nodii_services` policies, whose predicates compare `tenant_id` to a `uuid`
|
|
47
|
+
* (`auth.get_tenant_id()` / `auth.user_tenant_ids(...)`). `lifecycle_requests` +
|
|
48
|
+
* `data_residency_holds` are converted to `tenant_id UUID` earlier in this
|
|
49
|
+
* migration; `platform_lifecycle_requests` may still be mid-rewrite TEXT on a
|
|
50
|
+
* deployment whose orchestrator (tenant-service) has not yet run its b1461a50
|
|
51
|
+
* TEXT->UUID conversion. A bare `CREATE POLICY ... tenant_id = auth.get_tenant_id()`
|
|
52
|
+
* against a TEXT column raises `operator does not exist: text = uuid` at
|
|
53
|
+
* policy-create time, so these per-verb policies are deferred until the column
|
|
54
|
+
* is actually `uuid` — but the table is already RLS-locked-down (fail-closed)
|
|
55
|
+
* meanwhile, NOT fail-open. Each guarded statement runs via `EXECUTE` so plpgsql
|
|
56
|
+
* defers the `text = uuid` parse to runtime.
|
|
57
|
+
*
|
|
58
|
+
* Both artifacts (this runtime block + the copy-verbatim `001-lifecycle-requests.sql`)
|
|
59
|
+
* use this exact shape; a drift-guard test asserts they stay identical.
|
|
60
|
+
*/
|
|
61
|
+
export declare function a1RlsMigrationBlock(table: string): string;
|
|
4
62
|
export declare function getDsarMigrationSQL(): string;
|
|
5
63
|
/**
|
|
6
64
|
* Apply the DSAR migrations idempotently. Safe to call at every service boot.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/migrations/index.ts"],"names":[],"mappings":"AAQA,KAAK,SAAS,GAAG;IAEf,MAAM,EAAE,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,OAAO,EAAE,KAAK,OAAO,CAAC,GAAG,CAAC,CAAC;CACzD,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/migrations/index.ts"],"names":[],"mappings":"AAQA,KAAK,SAAS,GAAG;IAEf,MAAM,EAAE,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,OAAO,EAAE,KAAK,OAAO,CAAC,GAAG,CAAC,CAAC;CACzD,CAAC;AAwMF;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,wBAAgB,QAAQ,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAuC9C;AAaD;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,wBAAgB,mBAAmB,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAoCzD;AAmDD,wBAAgB,mBAAmB,IAAI,MAAM,CAE5C;AAED;;;;;GAKG;AACH,wBAAsB,mBAAmB,CAAC,GAAG,EAAE,SAAS,GAAG,OAAO,CAAC,IAAI,CAAC,CAMvE;AAED;;;;;;;;GAQG;AACH,wBAAgB,eAAe,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,EAAE,CA4BrD"}
|
package/dist/migrations/index.js
CHANGED
|
@@ -5,12 +5,19 @@
|
|
|
5
5
|
// `applySagaMigrations` in shape. The SQL source-of-truth lives at
|
|
6
6
|
// `src/migrations/001-lifecycle-requests.sql` for downstream services that
|
|
7
7
|
// prefer a different migration runner (Flyway, golang-migrate, alembic).
|
|
8
|
+
/**
|
|
9
|
+
* The canonical CRUD verb set the A1 RLS policies cover (D412 § 2 — "SELECT /
|
|
10
|
+
* INSERT / UPDATE / DELETE scoped by the `app.tenant_id` GUC + the nodii_owner
|
|
11
|
+
* BYPASSRLS policy"). Declared above `DSAR_MIGRATION_SQL` so `a1RlsDDL` (called
|
|
12
|
+
* during the template eval) can read it without hitting the const TDZ.
|
|
13
|
+
*/
|
|
14
|
+
const A1_RLS_VERBS = ["SELECT", "INSERT", "UPDATE", "DELETE"];
|
|
8
15
|
const DSAR_MIGRATION_SQL = `
|
|
9
16
|
CREATE TABLE IF NOT EXISTS lifecycle_requests (
|
|
10
17
|
id UUID PRIMARY KEY,
|
|
11
18
|
request_id TEXT NOT NULL,
|
|
12
19
|
rpc_method TEXT NOT NULL,
|
|
13
|
-
tenant_id
|
|
20
|
+
tenant_id UUID NOT NULL,
|
|
14
21
|
subject_kind TEXT NOT NULL,
|
|
15
22
|
subject_id TEXT NOT NULL,
|
|
16
23
|
status TEXT NOT NULL,
|
|
@@ -30,7 +37,7 @@ CREATE TABLE IF NOT EXISTS lifecycle_requests (
|
|
|
30
37
|
-- rows. (id is the PK column — never ADD COLUMN it.)
|
|
31
38
|
ALTER TABLE lifecycle_requests ADD COLUMN IF NOT EXISTS request_id TEXT NOT NULL DEFAULT '';
|
|
32
39
|
ALTER TABLE lifecycle_requests ADD COLUMN IF NOT EXISTS rpc_method TEXT NOT NULL DEFAULT '';
|
|
33
|
-
ALTER TABLE lifecycle_requests ADD COLUMN IF NOT EXISTS tenant_id
|
|
40
|
+
ALTER TABLE lifecycle_requests ADD COLUMN IF NOT EXISTS tenant_id UUID;
|
|
34
41
|
ALTER TABLE lifecycle_requests ADD COLUMN IF NOT EXISTS subject_kind TEXT NOT NULL DEFAULT '';
|
|
35
42
|
ALTER TABLE lifecycle_requests ADD COLUMN IF NOT EXISTS subject_id TEXT NOT NULL DEFAULT '';
|
|
36
43
|
ALTER TABLE lifecycle_requests ADD COLUMN IF NOT EXISTS status TEXT NOT NULL DEFAULT '';
|
|
@@ -40,6 +47,25 @@ ALTER TABLE lifecycle_requests ADD COLUMN IF NOT EXISTS error_detail TEXT;
|
|
|
40
47
|
ALTER TABLE lifecycle_requests ADD COLUMN IF NOT EXISTS created_at TIMESTAMPTZ NOT NULL DEFAULT now();
|
|
41
48
|
ALTER TABLE lifecycle_requests ADD COLUMN IF NOT EXISTS completed_at TIMESTAMPTZ;
|
|
42
49
|
ALTER TABLE lifecycle_requests ADD COLUMN IF NOT EXISTS cleanup_after TIMESTAMPTZ NOT NULL DEFAULT (now() + interval '7 days');
|
|
50
|
+
-- D412 (08-rls v7 A1): tenant_id TEXT -> UUID, idempotent + guarded. A table
|
|
51
|
+
-- created by a pre-D412 (<=0.8.x) migration has tenant_id TEXT; convert it
|
|
52
|
+
-- in-place via USING tenant_id::uuid (succeeds on valid UUID data, fails loudly
|
|
53
|
+
-- otherwise). Guarded by the data_type check so a fresh/already-UUID table is a
|
|
54
|
+
-- no-op. The lib + platform always stored UUID tenants in the TEXT column.
|
|
55
|
+
DO $rls_uuid$
|
|
56
|
+
BEGIN
|
|
57
|
+
IF EXISTS (
|
|
58
|
+
SELECT 1 FROM information_schema.columns
|
|
59
|
+
WHERE table_schema = current_schema()
|
|
60
|
+
AND table_name = 'lifecycle_requests'
|
|
61
|
+
AND column_name = 'tenant_id'
|
|
62
|
+
AND data_type <> 'uuid'
|
|
63
|
+
) THEN
|
|
64
|
+
EXECUTE 'ALTER TABLE lifecycle_requests
|
|
65
|
+
ALTER COLUMN tenant_id TYPE uuid USING tenant_id::uuid';
|
|
66
|
+
END IF;
|
|
67
|
+
END
|
|
68
|
+
$rls_uuid$;
|
|
43
69
|
CREATE INDEX IF NOT EXISTS idx_lifecycle_requests_tenant_at
|
|
44
70
|
ON lifecycle_requests (tenant_id, created_at);
|
|
45
71
|
CREATE INDEX IF NOT EXISTS idx_lifecycle_requests_cleanup
|
|
@@ -127,7 +153,7 @@ CREATE INDEX IF NOT EXISTS idx_retention_policies_active
|
|
|
127
153
|
WHERE soft_deleted_at IS NULL;
|
|
128
154
|
CREATE TABLE IF NOT EXISTS data_residency_holds (
|
|
129
155
|
id UUID PRIMARY KEY,
|
|
130
|
-
tenant_id
|
|
156
|
+
tenant_id UUID NOT NULL,
|
|
131
157
|
subject_id TEXT,
|
|
132
158
|
hold_kind TEXT NOT NULL,
|
|
133
159
|
data_class TEXT,
|
|
@@ -141,6 +167,24 @@ CREATE TABLE IF NOT EXISTS data_residency_holds (
|
|
|
141
167
|
);
|
|
142
168
|
ALTER TABLE data_residency_holds ADD COLUMN IF NOT EXISTS data_class TEXT;
|
|
143
169
|
ALTER TABLE data_residency_holds ADD COLUMN IF NOT EXISTS hold_request_id TEXT;
|
|
170
|
+
-- D412 (08-rls v7 A1): tenant_id TEXT -> UUID, idempotent + guarded (mirrors
|
|
171
|
+
-- lifecycle_requests above). A pre-D412 table has tenant_id TEXT; convert in
|
|
172
|
+
-- place. Guarded by the data_type check so a fresh/already-UUID table is a
|
|
173
|
+
-- no-op. Must run BEFORE the (tenant_id, ...) indexes below.
|
|
174
|
+
DO $rls_uuid$
|
|
175
|
+
BEGIN
|
|
176
|
+
IF EXISTS (
|
|
177
|
+
SELECT 1 FROM information_schema.columns
|
|
178
|
+
WHERE table_schema = current_schema()
|
|
179
|
+
AND table_name = 'data_residency_holds'
|
|
180
|
+
AND column_name = 'tenant_id'
|
|
181
|
+
AND data_type <> 'uuid'
|
|
182
|
+
) THEN
|
|
183
|
+
EXECUTE 'ALTER TABLE data_residency_holds
|
|
184
|
+
ALTER COLUMN tenant_id TYPE uuid USING tenant_id::uuid';
|
|
185
|
+
END IF;
|
|
186
|
+
END
|
|
187
|
+
$rls_uuid$;
|
|
144
188
|
CREATE INDEX IF NOT EXISTS idx_data_residency_holds_lookup
|
|
145
189
|
ON data_residency_holds (tenant_id, subject_id, hold_kind)
|
|
146
190
|
WHERE lifted_at IS NULL;
|
|
@@ -152,7 +196,146 @@ CREATE UNIQUE INDEX IF NOT EXISTS uq_data_residency_holds_request
|
|
|
152
196
|
WHERE hold_request_id IS NOT NULL;
|
|
153
197
|
${holdKindCheckDDL("data_residency_holds")}
|
|
154
198
|
${citationCheckDDL("data_residency_holds")}
|
|
199
|
+
${a1RlsMigrationBlock("lifecycle_requests")}
|
|
200
|
+
${a1RlsMigrationBlock("platform_lifecycle_requests")}
|
|
201
|
+
${a1RlsMigrationBlock("data_residency_holds")}
|
|
155
202
|
`.trim();
|
|
203
|
+
/**
|
|
204
|
+
* Emit the canonical 5-policy A1 RLS DDL for a tenant-scoped `tenant_id UUID`
|
|
205
|
+
* table per 08-rls-doctrine v7 § 1.1 + D412 (locked 2026-06-23):
|
|
206
|
+
*
|
|
207
|
+
* ENABLE ROW LEVEL SECURITY
|
|
208
|
+
* + nodii_owner FOR ALL bypass policy (BYPASSRLS role)
|
|
209
|
+
* + authenticated per-verb policies gated on
|
|
210
|
+
* `auth.user_tenant_ids(auth.get_user_id())`
|
|
211
|
+
* + nodii_services per-verb policies gated on the `app.tenant_id` GUC
|
|
212
|
+
* (`auth.get_tenant_id()`)
|
|
213
|
+
*
|
|
214
|
+
* This is the BYTE-EQUAL output of `@nodii/db-rls`'s `withRls(table, { allow:
|
|
215
|
+
* [SELECT,INSERT,UPDATE,DELETE] })` — NOT a hand-rolled predicate. It is
|
|
216
|
+
* re-derived here (rather than imported) only so the migration stays a static
|
|
217
|
+
* string constant with no `@nodii/db-rls` build-time dependency; the
|
|
218
|
+
* `withRls-parity.test.ts` suite asserts this function is line-for-line
|
|
219
|
+
* equivalent to the live `@nodii/db-rls` generator so it can never drift.
|
|
220
|
+
*
|
|
221
|
+
* Idempotent: each `CREATE POLICY` is preceded by `DROP POLICY IF EXISTS`
|
|
222
|
+
* (PG <18 has no `CREATE POLICY IF NOT EXISTS`) and `ENABLE ROW LEVEL
|
|
223
|
+
* SECURITY` is a natural no-op on re-run — so `applyDsarMigrations` stays
|
|
224
|
+
* boot-safe.
|
|
225
|
+
*
|
|
226
|
+
* The `auth.*` helpers are owned by `@nodii/db-rls`'s auth schema, which the
|
|
227
|
+
* adopting service installs + binds (08-rls § 7). Postgres resolves them
|
|
228
|
+
* lazily (at query time, not CREATE POLICY time), and the BYPASSRLS owner pool
|
|
229
|
+
* dsar uses for its own writes/reaper is unaffected.
|
|
230
|
+
*/
|
|
231
|
+
export function a1RlsDDL(table) {
|
|
232
|
+
const lines = [
|
|
233
|
+
`-- D412 / 08-rls v7 A1 — canonical 5-policy RLS for ${table} (tenant_id UUID).`,
|
|
234
|
+
`ALTER TABLE ${table} ENABLE ROW LEVEL SECURITY;`,
|
|
235
|
+
// Non-owner GRANTs — the table-level privilege the per-verb policies layer
|
|
236
|
+
// on top of (PG RLS never grants access; a missing GRANT = "permission
|
|
237
|
+
// denied for table" before RLS even evaluates). Byte-equal to db-rls's
|
|
238
|
+
// generateTableGrants(table); emitted right after ENABLE (the canonical
|
|
239
|
+
// shape, mirroring saga).
|
|
240
|
+
`GRANT SELECT, INSERT, UPDATE, DELETE ON ${table} TO nodii_services;`,
|
|
241
|
+
`GRANT SELECT, INSERT, UPDATE, DELETE ON ${table} TO authenticated;`,
|
|
242
|
+
];
|
|
243
|
+
const emit = (name, body) => {
|
|
244
|
+
lines.push(`DROP POLICY IF EXISTS ${name} ON ${table};`);
|
|
245
|
+
lines.push(`CREATE POLICY ${name} ON ${table} ${body};`);
|
|
246
|
+
};
|
|
247
|
+
// 1. Owner: FOR ALL bypass.
|
|
248
|
+
emit(`${table}_owner_policy`, "FOR ALL TO nodii_owner USING (true) WITH CHECK (true)");
|
|
249
|
+
// 2. authenticated — gated on auth.user_tenant_ids(auth.get_user_id()).
|
|
250
|
+
const authCond = "tenant_id IN (SELECT auth.user_tenant_ids(auth.get_user_id()))";
|
|
251
|
+
for (const verb of A1_RLS_VERBS) {
|
|
252
|
+
emit(`${table}_auth_${verb.toLowerCase()}_policy`, verbClause(verb, "authenticated", authCond));
|
|
253
|
+
}
|
|
254
|
+
// 3. nodii_services — gated on the app.tenant_id GUC (auth.get_tenant_id()).
|
|
255
|
+
const svcCond = "tenant_id = auth.get_tenant_id()";
|
|
256
|
+
for (const verb of A1_RLS_VERBS) {
|
|
257
|
+
emit(`${table}_svc_${verb.toLowerCase()}_policy`, verbClause(verb, "nodii_services", svcCond));
|
|
258
|
+
}
|
|
259
|
+
return lines.join("\n");
|
|
260
|
+
}
|
|
261
|
+
/** Per-verb policy body — mirrors db-rls's `policyLine` USING/WITH CHECK shape. */
|
|
262
|
+
function verbClause(verb, role, cond) {
|
|
263
|
+
if (verb === "INSERT") {
|
|
264
|
+
return `FOR INSERT TO ${role} WITH CHECK (${cond})`;
|
|
265
|
+
}
|
|
266
|
+
if (verb === "UPDATE") {
|
|
267
|
+
return `FOR UPDATE TO ${role} USING (${cond}) WITH CHECK (${cond})`;
|
|
268
|
+
}
|
|
269
|
+
return `FOR ${verb} TO ${role} USING (${cond})`;
|
|
270
|
+
}
|
|
271
|
+
/**
|
|
272
|
+
* Emit `a1RlsDDL(table)` as the migration ships it. The raw `a1RlsDDL` is the
|
|
273
|
+
* canonical DDL (and the parity-tested surface); this splits it into a
|
|
274
|
+
* fail-CLOSED unconditional half + a uuid-guarded half.
|
|
275
|
+
*
|
|
276
|
+
* **Unconditional (always emitted — works on a TEXT or a UUID `tenant_id`):**
|
|
277
|
+
* `ENABLE ROW LEVEL SECURITY` + the non-owner `GRANT`s + the `nodii_owner`
|
|
278
|
+
* FOR-ALL bypass policy. None of these reference `tenant_id`'s type. The effect
|
|
279
|
+
* on a still-TEXT table is **fail-CLOSED**: RLS is ON and the only policy is the
|
|
280
|
+
* owner bypass, so `nodii_services` / `authenticated` (which now hold the table
|
|
281
|
+
* GRANT) match NO policy → deny-all. Tenant data is never exposed.
|
|
282
|
+
*
|
|
283
|
+
* **Guarded on `tenant_id` being `uuid`:** the per-verb `authenticated` +
|
|
284
|
+
* `nodii_services` policies, whose predicates compare `tenant_id` to a `uuid`
|
|
285
|
+
* (`auth.get_tenant_id()` / `auth.user_tenant_ids(...)`). `lifecycle_requests` +
|
|
286
|
+
* `data_residency_holds` are converted to `tenant_id UUID` earlier in this
|
|
287
|
+
* migration; `platform_lifecycle_requests` may still be mid-rewrite TEXT on a
|
|
288
|
+
* deployment whose orchestrator (tenant-service) has not yet run its b1461a50
|
|
289
|
+
* TEXT->UUID conversion. A bare `CREATE POLICY ... tenant_id = auth.get_tenant_id()`
|
|
290
|
+
* against a TEXT column raises `operator does not exist: text = uuid` at
|
|
291
|
+
* policy-create time, so these per-verb policies are deferred until the column
|
|
292
|
+
* is actually `uuid` — but the table is already RLS-locked-down (fail-closed)
|
|
293
|
+
* meanwhile, NOT fail-open. Each guarded statement runs via `EXECUTE` so plpgsql
|
|
294
|
+
* defers the `text = uuid` parse to runtime.
|
|
295
|
+
*
|
|
296
|
+
* Both artifacts (this runtime block + the copy-verbatim `001-lifecycle-requests.sql`)
|
|
297
|
+
* use this exact shape; a drift-guard test asserts they stay identical.
|
|
298
|
+
*/
|
|
299
|
+
export function a1RlsMigrationBlock(table) {
|
|
300
|
+
const tag = `$rls_${table}$`;
|
|
301
|
+
const stmts = a1RlsDDL(table)
|
|
302
|
+
.split("\n")
|
|
303
|
+
.map((l) => l.trim())
|
|
304
|
+
.filter((l) => l.length > 0 && !l.startsWith("--"));
|
|
305
|
+
// The per-verb authenticated/services policies compare tenant_id to a uuid →
|
|
306
|
+
// guarded. ENABLE + GRANTs + the owner bypass policy carry no tenant_id
|
|
307
|
+
// predicate → unconditional (and make a still-TEXT table fail-closed).
|
|
308
|
+
const isGuarded = (s) => /_(auth|svc)_(select|insert|update|delete)_policy\b/.test(s);
|
|
309
|
+
const unconditional = stmts.filter((s) => !isGuarded(s));
|
|
310
|
+
const guarded = stmts.filter(isGuarded);
|
|
311
|
+
const execLines = guarded
|
|
312
|
+
.map((s) => ` EXECUTE ${sqlStringLiteral(s.replace(/;$/, ""))};`)
|
|
313
|
+
.join("\n");
|
|
314
|
+
return `-- D412 / 08-rls v7 A1 — ${table}: ENABLE + GRANTs + owner-bypass are
|
|
315
|
+
-- UNCONDITIONAL (a still-TEXT tenant_id is fail-CLOSED — RLS on, non-owner
|
|
316
|
+
-- deny-all until the per-verb policies land). The per-verb authenticated +
|
|
317
|
+
-- nodii_services policies compare tenant_id to a uuid, so they are guarded on
|
|
318
|
+
-- the column actually being uuid (deferred while a sibling table is mid
|
|
319
|
+
-- TEXT->UUID rewrite; they land on the next run once it is uuid).
|
|
320
|
+
${unconditional.join("\n")}
|
|
321
|
+
DO ${tag}
|
|
322
|
+
BEGIN
|
|
323
|
+
IF EXISTS (
|
|
324
|
+
SELECT 1 FROM information_schema.columns
|
|
325
|
+
WHERE table_schema = current_schema()
|
|
326
|
+
AND table_name = '${table}'
|
|
327
|
+
AND column_name = 'tenant_id'
|
|
328
|
+
AND data_type = 'uuid'
|
|
329
|
+
) THEN
|
|
330
|
+
${execLines}
|
|
331
|
+
END IF;
|
|
332
|
+
END
|
|
333
|
+
${tag};`;
|
|
334
|
+
}
|
|
335
|
+
/** Single-quote a string for embedding as a SQL string literal (doubles `'`). */
|
|
336
|
+
function sqlStringLiteral(s) {
|
|
337
|
+
return `'${s.replace(/'/g, "''")}'`;
|
|
338
|
+
}
|
|
156
339
|
/**
|
|
157
340
|
* Emit an idempotent `ADD CONSTRAINT` for the billing-spec `hold_kind_valid`
|
|
158
341
|
* CHECK. Postgres has no `ADD CONSTRAINT IF NOT EXISTS`, so we guard via a
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/migrations/index.ts"],"names":[],"mappings":"AAAA,kFA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K,MAAM,CAAC,IAAI,OAAO,EAAE,CAAC;YACxB,IAAI,SAAS,KAAK,IAAI,EAAE,CAAC;gBACvB,SAAS,GAAG,CAAC,CAAC;YAChB,CAAC;iBAAM,IAAI,SAAS,KAAK,CAAC,EAAE,CAAC;gBAC3B,SAAS,GAAG,IAAI,CAAC;YACnB,CAAC;QACH,CAAC;QAED,IAAI,SAAS,KAAK,IAAI,IAAI,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,EAAE,CAAC;YAC7C,MAAM,OAAO,GAAG,GAAG,CAAC,IAAI,EAAE,CAAC;YAC3B,IAAI,OAAO;gBAAE,GAAG,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;YAC/B,GAAG,GAAG,EAAE,CAAC;QACX,CAAC;IACH,CAAC;IACD,IAAI,GAAG,CAAC,IAAI,EAAE;QAAE,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,IAAI,EAAE,CAAC,CAAC;IACrC,OAAO,GAAG,CAAC;AACb,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nodii/dsar",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.9.0",
|
|
4
4
|
"description": "GDPR Data Subject Access Request (DSAR) library for the Nodii microservice stack — 9-RPC DataLifecycleService surface with typed handler registry + DsarStore + retention policy helpers. Polyglot ship: TS + Python + Go in parity. Spec: planning hub docKey=dsar.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -31,8 +31,8 @@
|
|
|
31
31
|
},
|
|
32
32
|
"devDependencies": {
|
|
33
33
|
"@nodii/telemetry": "0.9.0",
|
|
34
|
-
"@nodii/pii": "0.
|
|
35
|
-
"@nodii/audit-chain": "0.
|
|
34
|
+
"@nodii/pii": "0.9.0",
|
|
35
|
+
"@nodii/audit-chain": "0.9.0",
|
|
36
36
|
"@types/bun": "^1.3.13",
|
|
37
37
|
"typescript": "^5.9.3"
|
|
38
38
|
},
|