@rehearsal-db/core 0.1.0-beta.13 → 0.1.0-beta.15

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.
Files changed (169) hide show
  1. package/CHANGELOG.md +93 -0
  2. package/README.md +4 -1
  3. package/dist/package.json +100 -0
  4. package/dist/scripts/install/create_root_config.d.mts +3 -0
  5. package/dist/scripts/install/create_root_config.mjs +13 -0
  6. package/dist/scripts/runtime/manage_database.d.mts +6 -0
  7. package/{scripts → dist/scripts}/runtime/manage_database.mjs +6 -8
  8. package/dist/src/application/session.d.mts +96 -0
  9. package/dist/src/application/session.mjs +380 -0
  10. package/dist/src/baseline/artifact.d.mts +119 -0
  11. package/dist/src/baseline/artifact.mjs +522 -0
  12. package/dist/src/baseline/builder.d.mts +19 -0
  13. package/dist/src/baseline/builder.mjs +142 -0
  14. package/dist/src/baseline/input_discovery.d.mts +13 -0
  15. package/dist/src/baseline/input_discovery.mjs +149 -0
  16. package/dist/src/baseline/policy_review.d.mts +56 -0
  17. package/dist/src/baseline/policy_review.mjs +73 -0
  18. package/dist/src/baseline/preparation.d.mts +63 -0
  19. package/dist/src/baseline/preparation.mjs +295 -0
  20. package/dist/src/baseline/privacy_engine.d.mts +162 -0
  21. package/dist/src/baseline/privacy_engine.mjs +1162 -0
  22. package/dist/src/baseline/privacy_structured_json.d.mts +66 -0
  23. package/dist/src/baseline/privacy_structured_json.mjs +464 -0
  24. package/dist/src/baseline/sanitization_policy.d.mts +69 -0
  25. package/dist/src/baseline/sanitization_policy.mjs +227 -0
  26. package/dist/src/baseline/schema_snapshot.d.mts +19 -0
  27. package/dist/src/baseline/schema_snapshot.mjs +79 -0
  28. package/dist/src/cli/arguments.d.mts +43 -0
  29. package/dist/src/cli/arguments.mjs +135 -0
  30. package/dist/src/cli/guided.d.mts +51 -0
  31. package/dist/src/cli/guided.mjs +713 -0
  32. package/dist/src/cli/init_command.d.mts +21 -0
  33. package/dist/src/cli/init_command.mjs +90 -0
  34. package/dist/src/cli/project_commands.d.mts +157 -0
  35. package/dist/src/cli/project_commands.mjs +183 -0
  36. package/dist/src/cli/rehearsal.d.mts +7 -0
  37. package/dist/src/cli/rehearsal.mjs +706 -0
  38. package/dist/src/cli/renderers.d.mts +299 -0
  39. package/dist/src/cli/renderers.mjs +554 -0
  40. package/dist/src/cli/runtime_commands.d.mts +265 -0
  41. package/dist/src/cli/runtime_commands.mjs +599 -0
  42. package/dist/src/cli/source_commands.d.mts +432 -0
  43. package/dist/src/cli/source_commands.mjs +299 -0
  44. package/dist/src/cli/terminal.d.mts +59 -0
  45. package/dist/src/cli/terminal.mjs +230 -0
  46. package/dist/src/identity/claim.d.mts +144 -0
  47. package/dist/src/identity/claim.mjs +947 -0
  48. package/dist/src/identity/provider_configuration.d.mts +37 -0
  49. package/dist/src/identity/provider_configuration.mjs +132 -0
  50. package/dist/src/identity/storage.d.mts +21 -0
  51. package/dist/src/identity/storage.mjs +122 -0
  52. package/dist/src/index.d.mts +4 -0
  53. package/dist/src/index.mjs +3 -0
  54. package/dist/src/project/configuration.d.mts +181 -0
  55. package/dist/src/project/configuration.mjs +958 -0
  56. package/dist/src/project/install_scaffold.d.mts +42 -0
  57. package/dist/src/project/install_scaffold.mjs +263 -0
  58. package/dist/src/project/setup.d.mts +87 -0
  59. package/dist/src/project/setup.mjs +414 -0
  60. package/dist/src/project/support_report.d.mts +88 -0
  61. package/dist/src/project/support_report.mjs +83 -0
  62. package/dist/src/runtime/cleanup.d.mts +85 -0
  63. package/dist/src/runtime/cleanup.mjs +311 -0
  64. package/dist/src/runtime/migration_history.d.mts +51 -0
  65. package/dist/src/runtime/migration_history.mjs +170 -0
  66. package/dist/src/runtime/plan.d.mts +134 -0
  67. package/dist/src/runtime/plan.mjs +636 -0
  68. package/dist/src/runtime/policy.d.mts +66 -0
  69. package/dist/src/runtime/policy.mjs +369 -0
  70. package/dist/src/runtime/restore.d.mts +22 -0
  71. package/dist/src/runtime/restore.mjs +276 -0
  72. package/dist/src/runtime/service_environment.d.mts +11 -0
  73. package/dist/src/runtime/service_environment.mjs +58 -0
  74. package/dist/src/runtime/topology.d.mts +51 -0
  75. package/dist/src/runtime/topology.mjs +128 -0
  76. package/dist/src/shared/diagnostics.d.mts +97 -0
  77. package/dist/src/shared/diagnostics.mjs +170 -0
  78. package/dist/src/shared/human_output.d.mts +2 -0
  79. package/{src → dist/src}/shared/human_output.mjs +1 -3
  80. package/dist/src/shared/node_runtime.d.mts +14 -0
  81. package/dist/src/shared/node_runtime.mjs +25 -0
  82. package/dist/src/shared/operation_guard.d.mts +22 -0
  83. package/dist/src/shared/operation_guard.mjs +150 -0
  84. package/dist/src/shared/process_environment.d.mts +15 -0
  85. package/dist/src/shared/process_environment.mjs +50 -0
  86. package/dist/src/source/access.d.mts +165 -0
  87. package/dist/src/source/access.mjs +602 -0
  88. package/dist/src/source/asset_transfer.d.mts +27 -0
  89. package/dist/src/source/asset_transfer.mjs +202 -0
  90. package/dist/src/source/baseline.d.mts +66 -0
  91. package/dist/src/source/baseline.mjs +372 -0
  92. package/dist/src/source/external_reader.d.mts +42 -0
  93. package/dist/src/source/external_reader.mjs +278 -0
  94. package/dist/src/source/postgresql_access.d.mts +86 -0
  95. package/dist/src/source/postgresql_access.mjs +454 -0
  96. package/dist/src/source/privacy_paths.d.mts +10 -0
  97. package/dist/src/source/privacy_paths.mjs +27 -0
  98. package/dist/src/targets/driver.d.mts +2 -0
  99. package/dist/src/targets/driver.mjs +13 -0
  100. package/dist/src/targets/postgresql.d.mts +5 -0
  101. package/dist/src/targets/postgresql.mjs +641 -0
  102. package/dist/src/targets/supabase.d.mts +8 -0
  103. package/dist/src/targets/supabase.mjs +659 -0
  104. package/dist/src/targets/supabase_environment.d.mts +49 -0
  105. package/dist/src/targets/supabase_environment.mjs +162 -0
  106. package/dist/src/targets/target.d.mts +33 -0
  107. package/dist/src/targets/target.mjs +39 -0
  108. package/docs/README.md +1 -0
  109. package/docs/architecture.md +17 -9
  110. package/docs/configuration.md +49 -8
  111. package/docs/getting-started.md +28 -0
  112. package/docs/index.md +1 -0
  113. package/docs/production-source.md +43 -7
  114. package/docs/releasing.md +5 -3
  115. package/docs/roadmap.md +3 -2
  116. package/docs/sanitization.md +415 -14
  117. package/docs/standalone-workflow.md +12 -6
  118. package/docs/troubleshooting.md +5 -1
  119. package/docs/tutorial.md +5 -0
  120. package/docs/typescript.md +87 -0
  121. package/package.json +35 -29
  122. package/scripts/install/create_root_config.mjs +0 -36
  123. package/src/README.md +0 -17
  124. package/src/application/session.mjs +0 -435
  125. package/src/baseline/artifact.mjs +0 -684
  126. package/src/baseline/builder.mjs +0 -164
  127. package/src/baseline/input_discovery.mjs +0 -155
  128. package/src/baseline/policy_review.mjs +0 -92
  129. package/src/baseline/preparation.mjs +0 -323
  130. package/src/baseline/privacy_engine.mjs +0 -952
  131. package/src/baseline/sanitization_policy.mjs +0 -299
  132. package/src/baseline/schema_snapshot.mjs +0 -113
  133. package/src/cli/arguments.mjs +0 -126
  134. package/src/cli/guided.mjs +0 -814
  135. package/src/cli/init_command.mjs +0 -94
  136. package/src/cli/rehearsal.mjs +0 -970
  137. package/src/cli/renderers.mjs +0 -626
  138. package/src/cli/runtime_commands.mjs +0 -707
  139. package/src/cli/source_commands.mjs +0 -347
  140. package/src/cli/terminal.mjs +0 -275
  141. package/src/identity/claim.mjs +0 -1163
  142. package/src/identity/provider_configuration.mjs +0 -177
  143. package/src/identity/storage.mjs +0 -165
  144. package/src/project/configuration.mjs +0 -1337
  145. package/src/project/install_scaffold.mjs +0 -291
  146. package/src/project/setup.mjs +0 -466
  147. package/src/project/support_report.mjs +0 -105
  148. package/src/runtime/cleanup.mjs +0 -381
  149. package/src/runtime/migration_history.mjs +0 -220
  150. package/src/runtime/plan.mjs +0 -722
  151. package/src/runtime/policy.mjs +0 -438
  152. package/src/runtime/restore.mjs +0 -337
  153. package/src/runtime/service_environment.mjs +0 -82
  154. package/src/runtime/topology.mjs +0 -177
  155. package/src/shared/diagnostics.mjs +0 -207
  156. package/src/shared/node_runtime.mjs +0 -31
  157. package/src/shared/operation_guard.mjs +0 -162
  158. package/src/shared/process_environment.mjs +0 -68
  159. package/src/source/access.mjs +0 -753
  160. package/src/source/asset_transfer.mjs +0 -207
  161. package/src/source/baseline.mjs +0 -482
  162. package/src/source/external_reader.mjs +0 -255
  163. package/src/source/postgresql_access.mjs +0 -581
  164. package/src/source/privacy_paths.mjs +0 -38
  165. package/src/targets/postgresql.mjs +0 -808
  166. package/src/targets/supabase.mjs +0 -840
  167. package/src/targets/supabase_environment.mjs +0 -229
  168. package/src/targets/target.mjs +0 -66
  169. /package/{src/project → types}/configuration.d.mts +0 -0
@@ -1,753 +0,0 @@
1
- /**
2
- * Validate and digest the separately authorized source-access boundary.
3
- * Plans contain no credentials or raw owner identifiers.
4
- */
5
-
6
- import { createHash } from "node:crypto";
7
-
8
- const IDENTIFIER = /^[a-z][a-z0-9_]{0,62}$/u;
9
- const ENVIRONMENT_KEY = /^[A-Z][A-Z0-9_]{1,127}$/u;
10
- const SHA256 = /^[a-f0-9]{64}$/u;
11
-
12
- const isObject = (value) =>
13
- value !== null &&
14
- typeof value === "object" &&
15
- !Array.isArray(value) &&
16
- Object.getPrototypeOf(value) === Object.prototype;
17
-
18
- const knownKeys = (value, allowed, label) => {
19
- for (const key of Object.keys(value)) {
20
- if (!allowed.includes(key)) throw new Error(`${label}.${key} is unknown.`);
21
- }
22
- };
23
-
24
- const identifier = (value, label) => {
25
- if (!IDENTIFIER.test(value ?? "")) {
26
- throw new Error(`${label} must be a lowercase PostgreSQL identifier.`);
27
- }
28
- return value;
29
- };
30
-
31
- const envKey = (value, label) => {
32
- if (!ENVIRONMENT_KEY.test(value ?? "")) {
33
- throw new Error(`${label} must name an uppercase environment variable.`);
34
- }
35
- return value;
36
- };
37
-
38
- const fingerprint = (value, label) => {
39
- if (!SHA256.test(value ?? "")) throw new Error(`${label} must be a SHA-256.`);
40
- return value;
41
- };
42
-
43
- const positiveInteger = (value, label) => {
44
- if (!Number.isSafeInteger(value) || value < 1) {
45
- throw new Error(`${label} must be a positive integer.`);
46
- }
47
- return value;
48
- };
49
-
50
- const canonicalJson = (value) => `${JSON.stringify(value, null, "\t")}\n`;
51
- const sha256 = (value) => createHash("sha256").update(value).digest("hex");
52
-
53
- export const externalViewDefinitionFingerprint = (definition) => {
54
- if (typeof definition !== "string" || !definition.trim()) {
55
- throw new Error("External view definition must be a non-empty string.");
56
- }
57
- return sha256(definition.trim());
58
- };
59
-
60
- export const sourceTargetFingerprint = (connectionString) => {
61
- let target;
62
- try {
63
- const parsed = new URL(connectionString);
64
- if (!["postgres:", "postgresql:"].includes(parsed.protocol))
65
- throw new Error();
66
- target = {
67
- protocol: "postgresql",
68
- hostname: parsed.hostname.toLowerCase(),
69
- port: parsed.port || "5432",
70
- database: decodeURIComponent(parsed.pathname.replace(/^\//u, "")),
71
- };
72
- } catch {
73
- throw new Error(
74
- "Source credential is not a valid PostgreSQL connection URL.",
75
- );
76
- }
77
- if (!target.hostname || !target.database) {
78
- throw new Error("Source credential must identify a host and database.");
79
- }
80
- return sha256(canonicalJson(target));
81
- };
82
-
83
- export const validateSourceAccessPolicy = (policy) => {
84
- if (!isObject(policy))
85
- throw new Error("Source access policy must be an object.");
86
- knownKeys(
87
- policy,
88
- [
89
- "accessVersion",
90
- "targetFingerprint",
91
- "administratorEnvironmentVariable",
92
- "reader",
93
- "exportSchema",
94
- "migrationLedger",
95
- "relations",
96
- "assetReader",
97
- "assets",
98
- ],
99
- "sourcePolicy",
100
- );
101
- if (policy.accessVersion !== 1) {
102
- throw new Error("Source access policy must use accessVersion 1.");
103
- }
104
- fingerprint(policy.targetFingerprint, "sourcePolicy.targetFingerprint");
105
- if (!isObject(policy.reader))
106
- throw new Error("sourcePolicy.reader must be an object.");
107
- const readerMode = policy.reader.mode ?? "managed";
108
- if (!["managed", "external"].includes(readerMode)) {
109
- throw new Error("sourcePolicy.reader.mode must be managed or external.");
110
- }
111
- knownKeys(
112
- policy.reader,
113
- readerMode === "managed"
114
- ? ["mode", "role", "ownerRole", "credentialFile", "validForMinutes"]
115
- : [
116
- "mode",
117
- "role",
118
- "connectionEnvironmentVariable",
119
- "credentialFile",
120
- "maximumValidForMinutes",
121
- ],
122
- "sourcePolicy.reader",
123
- );
124
- let administratorEnvironmentVariable;
125
- if (readerMode === "managed") {
126
- administratorEnvironmentVariable = envKey(
127
- policy.administratorEnvironmentVariable,
128
- "sourcePolicy.administratorEnvironmentVariable",
129
- );
130
- } else if (policy.administratorEnvironmentVariable !== undefined) {
131
- throw new Error(
132
- "sourcePolicy.administratorEnvironmentVariable is not allowed for an external reader.",
133
- );
134
- }
135
- const role = identifier(policy.reader.role, "sourcePolicy.reader.role");
136
- let ownerRole;
137
- let validForMinutes;
138
- let connectionEnvironmentVariable;
139
- let maximumValidForMinutes;
140
- if (readerMode === "managed") {
141
- ownerRole = identifier(
142
- policy.reader.ownerRole,
143
- "sourcePolicy.reader.ownerRole",
144
- );
145
- if (role === ownerRole)
146
- throw new Error("Source reader and owner roles must differ.");
147
- if (
148
- !Number.isSafeInteger(policy.reader.validForMinutes) ||
149
- policy.reader.validForMinutes < 5 ||
150
- policy.reader.validForMinutes > 240
151
- ) {
152
- throw new Error(
153
- "sourcePolicy.reader.validForMinutes must be from 5 through 240.",
154
- );
155
- }
156
- validForMinutes = policy.reader.validForMinutes;
157
- } else {
158
- connectionEnvironmentVariable = envKey(
159
- policy.reader.connectionEnvironmentVariable,
160
- "sourcePolicy.reader.connectionEnvironmentVariable",
161
- );
162
- if (
163
- !Number.isSafeInteger(policy.reader.maximumValidForMinutes) ||
164
- policy.reader.maximumValidForMinutes < 5 ||
165
- policy.reader.maximumValidForMinutes > 240
166
- ) {
167
- throw new Error(
168
- "sourcePolicy.reader.maximumValidForMinutes must be from 5 through 240.",
169
- );
170
- }
171
- maximumValidForMinutes = policy.reader.maximumValidForMinutes;
172
- }
173
- if (
174
- typeof policy.reader.credentialFile !== "string" ||
175
- !policy.reader.credentialFile.startsWith(".rehearsal/") ||
176
- policy.reader.credentialFile.split(/[\\/]/u).includes("..")
177
- ) {
178
- throw new Error(
179
- "sourcePolicy.reader.credentialFile must stay under .rehearsal/.",
180
- );
181
- }
182
- const exportSchema = identifier(
183
- policy.exportSchema,
184
- "sourcePolicy.exportSchema",
185
- );
186
- if (!isObject(policy.migrationLedger)) {
187
- throw new Error("sourcePolicy.migrationLedger must be an object.");
188
- }
189
- knownKeys(
190
- policy.migrationLedger,
191
- ["schema", "table", "versionColumn", "nameColumn", "statementsColumn"],
192
- "sourcePolicy.migrationLedger",
193
- );
194
- const migrationLedger = Object.freeze({
195
- schema: identifier(
196
- policy.migrationLedger.schema,
197
- "sourcePolicy.migrationLedger.schema",
198
- ),
199
- table: identifier(
200
- policy.migrationLedger.table,
201
- "sourcePolicy.migrationLedger.table",
202
- ),
203
- versionColumn: identifier(
204
- policy.migrationLedger.versionColumn,
205
- "sourcePolicy.migrationLedger.versionColumn",
206
- ),
207
- nameColumn: identifier(
208
- policy.migrationLedger.nameColumn,
209
- "sourcePolicy.migrationLedger.nameColumn",
210
- ),
211
- statementsColumn: identifier(
212
- policy.migrationLedger.statementsColumn,
213
- "sourcePolicy.migrationLedger.statementsColumn",
214
- ),
215
- });
216
- if (!Array.isArray(policy.relations) || policy.relations.length === 0) {
217
- throw new Error(
218
- "sourcePolicy.relations must declare at least one relation.",
219
- );
220
- }
221
- const viewNames = new Set();
222
- const targetTables = new Set();
223
- const relations = policy.relations.map((relation, index) => {
224
- const label = `sourcePolicy.relations[${index}]`;
225
- if (!isObject(relation)) throw new Error(`${label} must be an object.`);
226
- knownKeys(
227
- relation,
228
- [
229
- "source",
230
- "view",
231
- "targetSchema",
232
- "targetTable",
233
- "columns",
234
- "orderBy",
235
- "rowScope",
236
- "viewDefinitionSha256",
237
- ],
238
- label,
239
- );
240
- if (!isObject(relation.source))
241
- throw new Error(`${label}.source must be an object.`);
242
- knownKeys(relation.source, ["schema", "table"], `${label}.source`);
243
- const source = Object.freeze({
244
- schema: identifier(relation.source.schema, `${label}.source.schema`),
245
- table: identifier(relation.source.table, `${label}.source.table`),
246
- });
247
- const view = identifier(relation.view, `${label}.view`);
248
- const targetSchema = identifier(
249
- relation.targetSchema ?? "public",
250
- `${label}.targetSchema`,
251
- );
252
- const targetTable = identifier(
253
- relation.targetTable,
254
- `${label}.targetTable`,
255
- );
256
- const targetKey = `${targetSchema}.${targetTable}`;
257
- if (targetTables.has(targetKey)) {
258
- throw new Error(
259
- `Source access policy duplicates target table ${targetKey}.`,
260
- );
261
- }
262
- targetTables.add(targetKey);
263
- if (viewNames.has(view))
264
- throw new Error(`Source access policy duplicates view ${view}.`);
265
- viewNames.add(view);
266
- if (
267
- !Array.isArray(relation.columns) ||
268
- relation.columns.length === 0 ||
269
- new Set(relation.columns).size !== relation.columns.length
270
- ) {
271
- throw new Error(`${label}.columns must be a non-empty unique array.`);
272
- }
273
- const columns = Object.freeze(
274
- relation.columns.map((column, columnIndex) =>
275
- identifier(column, `${label}.columns[${columnIndex}]`),
276
- ),
277
- );
278
- if (
279
- !Array.isArray(relation.orderBy) ||
280
- relation.orderBy.length === 0 ||
281
- new Set(relation.orderBy).size !== relation.orderBy.length ||
282
- relation.orderBy.some((column) => !columns.includes(column))
283
- ) {
284
- throw new Error(`${label}.orderBy must name unique exported columns.`);
285
- }
286
- const orderBy = Object.freeze([...relation.orderBy]);
287
- let viewDefinitionSha256;
288
- if (readerMode === "external") {
289
- viewDefinitionSha256 = fingerprint(
290
- relation.viewDefinitionSha256,
291
- `${label}.viewDefinitionSha256`,
292
- );
293
- } else if (relation.viewDefinitionSha256 !== undefined) {
294
- throw new Error(
295
- `${label}.viewDefinitionSha256 is supported only for an external reader.`,
296
- );
297
- }
298
- if (!isObject(relation.rowScope))
299
- throw new Error(`${label}.rowScope must be an object.`);
300
- knownKeys(
301
- relation.rowScope,
302
- ["kind", "column", "valueEnvironmentVariable"],
303
- `${label}.rowScope`,
304
- );
305
- let rowScope;
306
- if (relation.rowScope.kind === "approved-public") {
307
- if (
308
- relation.rowScope.column !== undefined ||
309
- relation.rowScope.valueEnvironmentVariable !== undefined
310
- ) {
311
- throw new Error(
312
- `${label}.rowScope approved-public accepts no owner fields.`,
313
- );
314
- }
315
- rowScope = Object.freeze({ kind: "approved-public" });
316
- } else if (relation.rowScope.kind === "approved-owner") {
317
- const column = identifier(
318
- relation.rowScope.column,
319
- `${label}.rowScope.column`,
320
- );
321
- if (!columns.includes(column)) {
322
- throw new Error(
323
- `${label}.rowScope.column must be included in exported columns.`,
324
- );
325
- }
326
- rowScope = Object.freeze({
327
- kind: "approved-owner",
328
- column,
329
- valueEnvironmentVariable: envKey(
330
- relation.rowScope.valueEnvironmentVariable,
331
- `${label}.rowScope.valueEnvironmentVariable`,
332
- ),
333
- });
334
- } else {
335
- throw new Error(`${label}.rowScope.kind is unsupported.`);
336
- }
337
- return Object.freeze({
338
- source,
339
- view,
340
- targetSchema,
341
- targetTable,
342
- columns,
343
- orderBy,
344
- rowScope,
345
- ...(viewDefinitionSha256 ? { viewDefinitionSha256 } : {}),
346
- });
347
- });
348
- const assets = policy.assets ?? [];
349
- if (!Array.isArray(assets))
350
- throw new Error("sourcePolicy.assets must be an array.");
351
- const normalizedAssets = assets.map((asset, index) => {
352
- const label = `sourcePolicy.assets[${index}]`;
353
- if (!isObject(asset)) throw new Error(`${label} must be an object.`);
354
- knownKeys(
355
- asset,
356
- [
357
- "bucket",
358
- "prefix",
359
- "prefixEnvironmentVariable",
360
- "rights",
361
- "pathMapping",
362
- ],
363
- label,
364
- );
365
- if (!/^[a-z0-9][a-z0-9.-]{0,99}$/u.test(asset.bucket ?? "")) {
366
- throw new Error(`${label}.bucket is invalid.`);
367
- }
368
- const hasFixedPrefix = asset.prefix !== undefined;
369
- const hasPrivatePrefix = asset.prefixEnvironmentVariable !== undefined;
370
- if (hasFixedPrefix === hasPrivatePrefix) {
371
- throw new Error(
372
- `${label} must declare exactly one of prefix or prefixEnvironmentVariable.`,
373
- );
374
- }
375
- if (hasFixedPrefix) {
376
- const prefixParts =
377
- typeof asset.prefix === "string"
378
- ? asset.prefix.replace(/\/$/u, "").split("/")
379
- : [];
380
- if (
381
- typeof asset.prefix !== "string" ||
382
- !asset.prefix ||
383
- asset.prefix.startsWith("/") ||
384
- prefixParts.some((part) => !part || part === "." || part === "..")
385
- ) {
386
- throw new Error(`${label}.prefix is unsafe.`);
387
- }
388
- } else {
389
- envKey(
390
- asset.prefixEnvironmentVariable,
391
- `${label}.prefixEnvironmentVariable`,
392
- );
393
- }
394
- if (!["approved-public", "approved-owner"].includes(asset.rights)) {
395
- throw new Error(`${label}.rights must be explicitly approved.`);
396
- }
397
- if (
398
- asset.pathMapping !== undefined &&
399
- !/^[a-z][a-z0-9-]{0,62}$/u.test(asset.pathMapping)
400
- ) {
401
- throw new Error(`${label}.pathMapping is invalid.`);
402
- }
403
- if (hasPrivatePrefix && asset.rights !== "approved-owner") {
404
- throw new Error(
405
- `${label}.prefixEnvironmentVariable requires approved-owner rights.`,
406
- );
407
- }
408
- if (hasPrivatePrefix && asset.pathMapping === undefined) {
409
- throw new Error(
410
- `${label}.prefixEnvironmentVariable requires pathMapping.`,
411
- );
412
- }
413
- if (hasFixedPrefix && asset.pathMapping !== undefined) {
414
- throw new Error(
415
- `${label}.pathMapping requires prefixEnvironmentVariable so the raw identity stays out of tracked policy.`,
416
- );
417
- }
418
- return Object.freeze({
419
- bucket: asset.bucket,
420
- ...(hasFixedPrefix
421
- ? { prefix: asset.prefix }
422
- : { prefixEnvironmentVariable: asset.prefixEnvironmentVariable }),
423
- rights: asset.rights,
424
- ...(asset.pathMapping === undefined
425
- ? {}
426
- : { pathMapping: asset.pathMapping }),
427
- });
428
- });
429
- let assetReader = null;
430
- if (normalizedAssets.length > 0) {
431
- if (!isObject(policy.assetReader)) {
432
- throw new Error(
433
- "sourcePolicy.assetReader is required when assets are declared.",
434
- );
435
- }
436
- knownKeys(
437
- policy.assetReader,
438
- [
439
- "endpointFingerprint",
440
- "baseUrlEnvironmentVariable",
441
- "tokenEnvironmentVariable",
442
- "maximumObjects",
443
- "maximumObjectBytes",
444
- "maximumTotalBytes",
445
- ],
446
- "sourcePolicy.assetReader",
447
- );
448
- assetReader = Object.freeze({
449
- endpointFingerprint: fingerprint(
450
- policy.assetReader.endpointFingerprint,
451
- "sourcePolicy.assetReader.endpointFingerprint",
452
- ),
453
- baseUrlEnvironmentVariable: envKey(
454
- policy.assetReader.baseUrlEnvironmentVariable,
455
- "sourcePolicy.assetReader.baseUrlEnvironmentVariable",
456
- ),
457
- tokenEnvironmentVariable: envKey(
458
- policy.assetReader.tokenEnvironmentVariable,
459
- "sourcePolicy.assetReader.tokenEnvironmentVariable",
460
- ),
461
- maximumObjects: positiveInteger(
462
- policy.assetReader.maximumObjects,
463
- "sourcePolicy.assetReader.maximumObjects",
464
- ),
465
- maximumObjectBytes: positiveInteger(
466
- policy.assetReader.maximumObjectBytes,
467
- "sourcePolicy.assetReader.maximumObjectBytes",
468
- ),
469
- maximumTotalBytes: positiveInteger(
470
- policy.assetReader.maximumTotalBytes,
471
- "sourcePolicy.assetReader.maximumTotalBytes",
472
- ),
473
- });
474
- } else if (policy.assetReader !== undefined && policy.assetReader !== null) {
475
- throw new Error(
476
- "sourcePolicy.assetReader is not allowed without declared assets.",
477
- );
478
- }
479
- return Object.freeze({
480
- accessVersion: 1,
481
- targetFingerprint: policy.targetFingerprint,
482
- ...(administratorEnvironmentVariable
483
- ? { administratorEnvironmentVariable }
484
- : {}),
485
- reader: Object.freeze({
486
- mode: readerMode,
487
- role,
488
- credentialFile: policy.reader.credentialFile,
489
- ...(readerMode === "managed"
490
- ? { ownerRole, validForMinutes }
491
- : { connectionEnvironmentVariable, maximumValidForMinutes }),
492
- }),
493
- exportSchema,
494
- migrationLedger,
495
- assetReader,
496
- relations: Object.freeze(relations),
497
- assets: Object.freeze(normalizedAssets),
498
- });
499
- };
500
-
501
- export const createSourceAccessPlan = ({
502
- policy,
503
- environment = process.env,
504
- }) => {
505
- const normalized = validateSourceAccessPolicy(policy);
506
- const sourceCredential =
507
- environment[
508
- normalized.reader.mode === "managed"
509
- ? normalized.administratorEnvironmentVariable
510
- : normalized.reader.connectionEnvironmentVariable
511
- ];
512
- if (!sourceCredential) {
513
- throw new Error(
514
- `Source preparation requires ${
515
- normalized.reader.mode === "managed"
516
- ? normalized.administratorEnvironmentVariable
517
- : normalized.reader.connectionEnvironmentVariable
518
- }; the value is read from the environment and never printed.`,
519
- );
520
- }
521
- const actualTarget = sourceTargetFingerprint(sourceCredential);
522
- if (actualTarget !== normalized.targetFingerprint) {
523
- throw new Error(
524
- "Source credential does not match the reviewed target fingerprint.",
525
- );
526
- }
527
- const ownerScopes = Object.fromEntries(
528
- normalized.relations
529
- .filter((relation) => relation.rowScope.kind === "approved-owner")
530
- .map((relation) => {
531
- const key = relation.rowScope.valueEnvironmentVariable;
532
- const value = environment[key];
533
- if (!value) throw new Error(`Source preparation requires ${key}.`);
534
- return [relation.view, sha256(value)];
535
- }),
536
- );
537
- const assetPrefixReceipts = Object.fromEntries(
538
- normalized.assets
539
- .filter((asset) => asset.prefixEnvironmentVariable)
540
- .map((asset) => {
541
- const key = asset.prefixEnvironmentVariable;
542
- const value = environment[key];
543
- if (!value) throw new Error(`Source preparation requires ${key}.`);
544
- return [`${asset.bucket}:${asset.pathMapping}`, sha256(value)];
545
- }),
546
- );
547
- if (normalized.assetReader) {
548
- const baseUrl =
549
- environment[normalized.assetReader.baseUrlEnvironmentVariable];
550
- const token = environment[normalized.assetReader.tokenEnvironmentVariable];
551
- if (
552
- !baseUrl ||
553
- assetEndpointFingerprint(baseUrl) !==
554
- normalized.assetReader.endpointFingerprint
555
- ) {
556
- throw new Error(
557
- "Asset reader URL does not match the reviewed endpoint fingerprint.",
558
- );
559
- }
560
- if (!token)
561
- throw new Error(
562
- `Source preparation requires ${normalized.assetReader.tokenEnvironmentVariable}.`,
563
- );
564
- }
565
- const review = {
566
- planVersion: 1,
567
- operation: "prepare-source-access",
568
- targetFingerprint: normalized.targetFingerprint,
569
- exportSchema: normalized.exportSchema,
570
- reader: {
571
- mode: normalized.reader.mode,
572
- role: normalized.reader.role,
573
- credentialFile: normalized.reader.credentialFile,
574
- ...(normalized.reader.mode === "managed"
575
- ? {
576
- ownerRole: normalized.reader.ownerRole,
577
- validForMinutes: normalized.reader.validForMinutes,
578
- sourceChanges: true,
579
- }
580
- : {
581
- connectionEnvironmentVariable:
582
- normalized.reader.connectionEnvironmentVariable,
583
- maximumValidForMinutes: normalized.reader.maximumValidForMinutes,
584
- sourceChanges: false,
585
- }),
586
- },
587
- relations: normalized.relations.map((relation) => ({
588
- source: relation.source,
589
- view: relation.view,
590
- targetTable: relation.targetTable,
591
- columns: relation.columns,
592
- orderBy: relation.orderBy,
593
- ...(relation.viewDefinitionSha256
594
- ? { viewDefinitionSha256: relation.viewDefinitionSha256 }
595
- : {}),
596
- rowScope: {
597
- kind: relation.rowScope.kind,
598
- ...(relation.rowScope.kind === "approved-owner"
599
- ? {
600
- column: relation.rowScope.column,
601
- valueSha256: ownerScopes[relation.view],
602
- }
603
- : {}),
604
- },
605
- })),
606
- assets: normalized.assets.map((asset) =>
607
- asset.prefixEnvironmentVariable
608
- ? {
609
- ...asset,
610
- prefixValueSha256:
611
- assetPrefixReceipts[`${asset.bucket}:${asset.pathMapping}`],
612
- }
613
- : asset,
614
- ),
615
- assetReader: normalized.assetReader
616
- ? {
617
- endpointFingerprint: normalized.assetReader.endpointFingerprint,
618
- maximumObjects: normalized.assetReader.maximumObjects,
619
- maximumObjectBytes: normalized.assetReader.maximumObjectBytes,
620
- maximumTotalBytes: normalized.assetReader.maximumTotalBytes,
621
- }
622
- : null,
623
- denyChecks:
624
- normalized.reader.mode === "managed"
625
- ? [
626
- "raw relation reads",
627
- "writes",
628
- "role creation and escalation",
629
- "network functions",
630
- "unrelated private assets",
631
- ]
632
- : [
633
- "all non-system readable columns match the approved views and migration ledger",
634
- "no relation, column, schema, or database writes",
635
- "no inherited roles or privileged role attributes",
636
- "no executable non-system security-definer functions",
637
- "database-enforced credential expiration",
638
- "unrelated private assets",
639
- ],
640
- };
641
- return Object.freeze({
642
- policy: normalized,
643
- review: Object.freeze(review),
644
- digest: sha256(canonicalJson(review)),
645
- });
646
- };
647
-
648
- export const sourceAccessPolicyFingerprint = (policy) =>
649
- sha256(canonicalJson(validateSourceAccessPolicy(policy)));
650
-
651
- export const assetEndpointFingerprint = (value) => {
652
- let endpoint;
653
- try {
654
- const parsed = new URL(value);
655
- if (
656
- parsed.protocol !== "https:" &&
657
- !(
658
- parsed.protocol === "http:" &&
659
- ["127.0.0.1", "::1", "localhost"].includes(parsed.hostname)
660
- )
661
- ) {
662
- throw new Error();
663
- }
664
- endpoint = `${parsed.protocol}//${parsed.host}${parsed.pathname.replace(/\/$/u, "")}`;
665
- } catch {
666
- throw new Error("Asset reader endpoint is invalid or insecure.");
667
- }
668
- return sha256(endpoint);
669
- };
670
-
671
- export const assertSourceAccessConfirmation = (plan, confirmation) => {
672
- if (confirmation !== plan.digest) {
673
- throw new Error(
674
- `Source access confirmation does not match this exact plan. Expected ${plan.digest}.`,
675
- );
676
- }
677
- return plan;
678
- };
679
-
680
- export const createSourceRetirementPlan = ({
681
- receipt,
682
- targetFingerprint: target,
683
- }) => {
684
- if (!isObject(receipt) || receipt.receiptVersion !== 1) {
685
- throw new Error("Source access receipt is invalid.");
686
- }
687
- fingerprint(target, "Source retirement target fingerprint");
688
- if (receipt.targetFingerprint !== target) {
689
- throw new Error(
690
- "Source access receipt belongs to a different source target.",
691
- );
692
- }
693
- if (!Array.isArray(receipt.views) || !Array.isArray(receipt.relations)) {
694
- throw new Error("Source access receipt object inventory is invalid.");
695
- }
696
- const accessMode = receipt.accessMode ?? "managed";
697
- if (!["managed", "external"].includes(accessMode)) {
698
- throw new Error("Source access receipt mode is invalid.");
699
- }
700
- const relations = receipt.relations.map((relation, index) => {
701
- if (!isObject(relation))
702
- throw new Error(`receipt.relations[${index}] is invalid.`);
703
- knownKeys(
704
- relation,
705
- ["schema", "table", "columns"],
706
- `receipt.relations[${index}]`,
707
- );
708
- if (!Array.isArray(relation.columns) || relation.columns.length === 0) {
709
- throw new Error(`receipt.relations[${index}].columns is invalid.`);
710
- }
711
- return {
712
- schema: identifier(relation.schema, `receipt.relations[${index}].schema`),
713
- table: identifier(relation.table, `receipt.relations[${index}].table`),
714
- columns: relation.columns.map((column) =>
715
- identifier(column, `receipt.relations[${index}].columns[]`),
716
- ),
717
- };
718
- });
719
- if (!isObject(receipt.migrationLedger)) {
720
- throw new Error("Source access receipt migration ledger is invalid.");
721
- }
722
- const ledger = {
723
- schema: identifier(
724
- receipt.migrationLedger.schema,
725
- "receipt.migrationLedger.schema",
726
- ),
727
- table: identifier(
728
- receipt.migrationLedger.table,
729
- "receipt.migrationLedger.table",
730
- ),
731
- };
732
- const review = {
733
- planVersion: 1,
734
- operation: "retire-source-access",
735
- accessMode,
736
- targetFingerprint: target,
737
- preparationDigest: fingerprint(receipt.planDigest, "receipt.planDigest"),
738
- exportSchema: identifier(receipt.exportSchema, "receipt.exportSchema"),
739
- views: [...receipt.views]
740
- .map((view) => identifier(view, "receipt.views[]"))
741
- .sort(),
742
- relations,
743
- migrationLedger: ledger,
744
- readerRole: identifier(receipt.readerRole, "receipt.readerRole"),
745
- ...(accessMode === "managed"
746
- ? { ownerRole: identifier(receipt.ownerRole, "receipt.ownerRole") }
747
- : {}),
748
- };
749
- return Object.freeze({
750
- review: Object.freeze(review),
751
- digest: sha256(canonicalJson(review)),
752
- });
753
- };