@runeditrun/rer 0.2.80 → 0.2.82
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/README.md +12 -0
- package/cli/deploy-needs.mjs +4 -2
- package/cli/export.mjs +1 -1
- package/cli/ledger-refusal.mjs +18 -0
- package/cli/portability-migrations.mjs +30 -11
- package/cli/portability-worker.mjs +3 -1
- package/cli/portability.mjs +2 -0
- package/cli/release.mjs +4 -4
- package/cli/site-hosting.mjs +90 -0
- package/cli/site-render.mjs +3 -2
- package/cli/site.mjs +5 -2
- package/edition-authoring/base-copies.mjs +12 -1
- package/edition-authoring/copy-checkout.mjs +152 -1
- package/edition-authoring/files.mjs +1 -1
- package/edition-authoring/runtime-dependencies.mjs +108 -0
- package/global/CHANGELOG.md +25 -0
- package/global/README.md +12 -0
- package/modules/demo/README.md +8 -0
- package/modules/demo/navigation.mjs +131 -15
- package/modules/dependencies/auth.mjs +2 -2
- package/modules/deploy-kit/owner.mjs +8 -1
- package/modules/import-kit/src/d1/apply-effects.ts +33 -0
- package/modules/import-kit/src/d1/apply.ts +38 -10
- package/modules/import-kit/src/door/ADOPTING.md +452 -0
- package/modules/import-kit/src/door/README.md +553 -0
- package/modules/import-kit/src/door/admission.ts +45 -0
- package/modules/import-kit/src/door/archive.ts +129 -0
- package/modules/import-kit/src/door/authority.ts +107 -0
- package/modules/import-kit/src/door/failures.ts +70 -0
- package/modules/import-kit/src/door/grants.ts +162 -0
- package/modules/import-kit/src/door/handler.ts +90 -0
- package/modules/import-kit/src/door/index.ts +20 -0
- package/modules/import-kit/src/door/intake/csv.ts +74 -0
- package/modules/import-kit/src/door/intake/json.ts +182 -0
- package/modules/import-kit/src/door/intake/projection.ts +61 -0
- package/modules/import-kit/src/door/intake/types.ts +28 -0
- package/modules/import-kit/src/door/intake-policy.ts +38 -0
- package/modules/import-kit/src/door/intake-upload.ts +91 -0
- package/modules/import-kit/src/door/inventory.ts +68 -0
- package/modules/import-kit/src/door/jobs.ts +134 -0
- package/modules/import-kit/src/door/limits.ts +16 -0
- package/modules/import-kit/src/door/operator.ts +41 -0
- package/modules/import-kit/src/door/planning.ts +227 -0
- package/modules/import-kit/src/door/protocol.ts +38 -0
- package/modules/import-kit/src/door/refusals.ts +51 -0
- package/modules/import-kit/src/door/schema.ts +101 -0
- package/modules/import-kit/src/door/store.ts +152 -0
- package/modules/import-kit/src/door/types.ts +100 -0
- package/modules/import-kit/src/door/uploads.ts +98 -0
- package/modules/import-kit/src/provenance/numeric-credentials.ts +40 -0
- package/modules/import-kit/src/readers/README.md +139 -0
- package/modules/import-kit/src/readers/api-policy.ts +67 -0
- package/modules/import-kit/src/readers/api.ts +86 -0
- package/modules/import-kit/src/readers/bytes.ts +73 -0
- package/modules/import-kit/src/readers/errors.ts +33 -0
- package/modules/import-kit/src/readers/file.ts +74 -0
- package/modules/import-kit/src/readers/index.ts +3 -0
- package/modules/import-kit/src/readers/json.ts +29 -0
- package/modules/import-kit/src/readers/numeric-lexemes.ts +104 -0
- package/modules/import-kit/src/readers/original.ts +31 -0
- package/modules/import-kit/src/readers/records.ts +105 -0
- package/modules/import-kit/src/types.ts +3 -1
- package/modules/operator-auth/README.md +92 -3
- package/modules/operator-auth/accounts/admin.mjs +52 -20
- package/modules/operator-auth/accounts/audit.mjs +24 -0
- package/modules/operator-auth/accounts/business-team.mjs +91 -0
- package/modules/operator-auth/accounts/domain-audit.mjs +24 -0
- package/modules/operator-auth/accounts/native.mjs +3 -1
- package/modules/operator-auth/accounts/settings-page.mjs +15 -2
- package/modules/operator-auth/accounts/status.mjs +7 -2
- package/modules/operator-auth/accounts/store.mjs +32 -6
- package/modules/operator-auth/business-settings.mjs +40 -0
- package/modules/operator-auth/identity/email-link/index.mjs +1 -1
- package/modules/operator-auth/identity/oidc/admin-audit.mjs +31 -0
- package/modules/operator-auth/identity/oidc/index.mjs +12 -5
- package/modules/operator-auth/identity/oidc/store.mjs +2 -0
- package/modules/operator-auth/index.d.mts +40 -1
- package/modules/operator-auth/routes.mjs +11 -6
- package/modules/operator-auth/service.mjs +8 -3
- package/modules/operator-auth/signin/index.mjs +1 -1
- package/modules/portability/README.md +82 -1
- package/modules/portability/export.mjs +6 -4
- package/modules/portability/format.mjs +5 -0
- package/modules/portability/index.d.mts +10 -2
- package/modules/portability/index.mjs +3 -2
- package/modules/portability/ledgers.mjs +27 -4
- package/modules/release/README.md +74 -2
- package/modules/release/sign.mjs +4 -2
- package/modules/release/signature.mjs +14 -7
- package/modules/release/signing-cli.mjs +2 -1
- package/modules/release/statement.mjs +2 -2
- package/modules/release/vectors/generate.mjs +1 -0
- package/modules/release/vectors/publisher-projects.mjs +133 -0
- package/modules/release/vectors/release-signature-vectors.json +5195 -0
- package/modules/release/verify.mjs +5 -1
- package/onboarding/schema/SETUP.schema.json +43 -1
- package/onboarding/schema/business-field-inputs.mjs +0 -1
- package/onboarding/schema/setup-validation.mjs +3 -0
- package/package.json +3 -2
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import { createD1ImportJob, readD1ImportJob } from '../d1/jobs.ts';
|
|
2
|
+
import { applyD1ImportBatches } from '../d1/apply.ts';
|
|
3
|
+
import { rollbackD1Import, undoD1Import } from '../d1/reverse.ts';
|
|
4
|
+
import { loadPlans } from '../d1/plans.ts';
|
|
5
|
+
import type { DoorPlanRow, DoorRequest, DoorSnapshot } from './types.ts';
|
|
6
|
+
import { kitContext, reserveApply } from './authority.ts';
|
|
7
|
+
import { inspectRecords } from './inventory.ts';
|
|
8
|
+
import { DoorError } from './protocol.ts';
|
|
9
|
+
import { assertion, authorized, fenced, lineage, loadSnapshot, planRow, snapshotHeld, sql, transact } from './store.ts';
|
|
10
|
+
import { text } from './uploads.ts';
|
|
11
|
+
import { approvalCaption, failedPlan, recordApplyFailure, requireNewPlan } from './failures.ts';
|
|
12
|
+
import { sizeLimitError } from './limits.ts';
|
|
13
|
+
import { D1RequestLimitError } from '../d1/budget.ts';
|
|
14
|
+
|
|
15
|
+
/** Whether this kit job holds the plan the owner reviewed. A person's erasure
|
|
16
|
+
* re-fingerprints the job's plan and keeps the reviewed fingerprint inside it. */
|
|
17
|
+
async function reviewedJob(request: DoorRequest, row: DoorPlanRow, job: Record<string,any>): Promise<boolean> {
|
|
18
|
+
if (job.fingerprint === row.fingerprint) return true;
|
|
19
|
+
const plan = (await loadPlans([job],request.options.database,request.options.targets.planCodec)).get(job.id);
|
|
20
|
+
return plan?.erasure?.reviewedFingerprint === row.fingerprint;
|
|
21
|
+
}
|
|
22
|
+
/** The kit context for a plan's own job. It needs no snapshot, so it still
|
|
23
|
+
* works after an erasure withdrew one; the kit job binds the same plan. An
|
|
24
|
+
* erasure re-fingerprints the job, so reviewedJob, not the kit, binds it to
|
|
25
|
+
* the reviewed fingerprint. */
|
|
26
|
+
async function jobContext(request: DoorRequest, row: DoorPlanRow & {job_id: string}) {
|
|
27
|
+
const job = await request.options.database.prepare('SELECT id,namespace,mapping_version,destination,principal_id,operation_key,fingerprint,plan_json FROM ik_jobs WHERE id=?').bind(row.job_id).first();
|
|
28
|
+
if (!job || job.destination !== request.options.destination || job.principal_id !== request.options.ownerPrincipalId
|
|
29
|
+
|| job.operation_key !== 'door:' + row.id || !await reviewedJob(request,row,job)) throw new DoorError('plan_custody_mismatch', 409);
|
|
30
|
+
return {...kitContext(request, {id: row.id, kitNamespace: job.namespace, plan: {mappingVersion: job.mapping_version} as DoorSnapshot['plan']}), reviewedFingerprint: undefined};
|
|
31
|
+
}
|
|
32
|
+
/** Creation and indexing may be separate commits; the immutable operation key
|
|
33
|
+
* makes a queued job recoverable even if its response or index update was lost. */
|
|
34
|
+
async function associated(request: DoorRequest, row: DoorPlanRow): Promise<DoorPlanRow> {
|
|
35
|
+
if (row.job_id) return row;
|
|
36
|
+
const job = await request.options.database.prepare('SELECT id,fingerprint,plan_json FROM ik_jobs WHERE destination=? AND principal_id=? AND operation_key=?')
|
|
37
|
+
.bind(request.options.destination,request.options.ownerPrincipalId,'door:'+row.id).first();
|
|
38
|
+
if (!job) return row;
|
|
39
|
+
if (!await reviewedJob(request,row,job)) throw new DoorError('job_binding_mismatch',409);
|
|
40
|
+
await transact(request.options.database,request.authority,[sql('UPDATE ik_door_plans SET job_id=? WHERE id=? AND job_id IS NULL',job.id,row.id)]);
|
|
41
|
+
return {...row,job_id:job.id as string};
|
|
42
|
+
}
|
|
43
|
+
export async function apply(request: DoorRequest, input: Record<string,unknown>) {
|
|
44
|
+
if (Object.keys(input).some(k=>!['planId','reviewedFingerprint'].includes(k))) throw new DoorError('invalid_apply_input',400);
|
|
45
|
+
const row = await associated(request,await planRow(request,text(input.planId,'plan_id')));
|
|
46
|
+
if (input.reviewedFingerprint !== row.fingerprint || request.access.fingerprint !== row.fingerprint) throw new DoorError('review_fingerprint_mismatch');
|
|
47
|
+
const failure = await failedPlan(request,row.id);
|
|
48
|
+
if (failure) requireNewPlan(row.id,failure);
|
|
49
|
+
const current = await lineage(request);
|
|
50
|
+
if (current.head_id === row.id && row.job_id) {
|
|
51
|
+
const approval = await request.options.database.prepare('SELECT approval_id,approved_at FROM ik_door_approvals WHERE plan_id=?').bind(row.id).first();
|
|
52
|
+
if (approval?.approval_id !== request.access.approvalRecordId || approval?.approved_at !== request.access.approvedAt) throw new DoorError('approval_mismatch');
|
|
53
|
+
return {planId:row.id,job:await readD1ImportJob(row.job_id,await jobContext(request,{...row,job_id:row.job_id}))};
|
|
54
|
+
}
|
|
55
|
+
const snapshot = await loadSnapshot(request,row), context = kitContext(request,snapshot);
|
|
56
|
+
// The kit asserts the snapshot is still held at both ends of every batch.
|
|
57
|
+
const result = await fenced(request,row.id,snapshotHeld(row.id),async()=>{
|
|
58
|
+
await reserveApply(request,snapshot);
|
|
59
|
+
// Only creation selects the policy; a resume, even after a remount with
|
|
60
|
+
// another policy, continues with the job's durable one.
|
|
61
|
+
const policy = row.job_id ? undefined : request.options.reversalPolicy
|
|
62
|
+
?? (typeof request.options.targets.preparePreservedUndo === 'function' ? 'preserve-conflicts' : 'strict');
|
|
63
|
+
const job = await createD1ImportJob(snapshot.plan,policy ? {...context,reversalPolicy:policy} : context);
|
|
64
|
+
await transact(request.options.database,request.authority,[
|
|
65
|
+
sql('UPDATE ik_door_plans SET job_id=? WHERE id=? AND (job_id IS NULL OR job_id=?)',job.id,row.id,job.id),
|
|
66
|
+
assertion(sql('EXISTS(SELECT 1 FROM ik_door_plans WHERE id=? AND job_id=?)',row.id,job.id)),
|
|
67
|
+
]);
|
|
68
|
+
// A failure is recorded only while no erasure overtook the request and the
|
|
69
|
+
// snapshot is held; otherwise the record batch is refused and the erasure's
|
|
70
|
+
// 410 (snapshot_withdrawn or erased_during_request) answers instead.
|
|
71
|
+
try { return await applyD1ImportBatches(job.id,context); }
|
|
72
|
+
catch (error) {
|
|
73
|
+
// A large job created in this request can leave too little of its budget
|
|
74
|
+
// for a first batch: that batch refused before writing, so the queued job
|
|
75
|
+
// answers and the next apply continues it.
|
|
76
|
+
if (!row.job_id && error instanceof D1RequestLimitError) return job;
|
|
77
|
+
// A kit size or request limit refuses before its batch writes anything: it
|
|
78
|
+
// answers the named 413 and is not an apply failure.
|
|
79
|
+
if (sizeLimitError(error)) throw error;
|
|
80
|
+
return recordApplyFailure(request,snapshot,job,error);
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
await lineage(request);
|
|
84
|
+
return {planId:row.id,job:result};
|
|
85
|
+
}
|
|
86
|
+
export async function status(request: DoorRequest, input: Record<string,unknown>) {
|
|
87
|
+
const row = await associated(request,await planRow(request,text(input.planId,'plan_id')));
|
|
88
|
+
if (!row.job_id) await loadSnapshot(request,row);
|
|
89
|
+
const job = row.job_id ? await readD1ImportJob(row.job_id,await jobContext(request,{...row,job_id:row.job_id})) : null;
|
|
90
|
+
await lineage(request);
|
|
91
|
+
const approval = await request.options.database.prepare('SELECT owner,approved_at,approval_id,reviewer FROM ik_door_approvals WHERE plan_id=?').bind(row.id).first<{owner:string;approved_at:string;approval_id:string;reviewer:string}>();
|
|
92
|
+
const failureCode = await failedPlan(request,row.id);
|
|
93
|
+
return {planId:row.id,fingerprint:row.fingerprint,job,approval,failureCode,
|
|
94
|
+
caption:approval?approvalCaption(approval,row.fingerprint,failureCode):null};
|
|
95
|
+
}
|
|
96
|
+
export async function reconcile(request: DoorRequest, input: Record<string,unknown>) {
|
|
97
|
+
const row = await associated(request,await planRow(request,text(input.planId,'plan_id'))), snapshot = await loadSnapshot(request,row);
|
|
98
|
+
if (!row.job_id || (await readD1ImportJob(row.job_id,kitContext(request,snapshot))).state!=='completed') throw new DoorError('import_not_completed',409);
|
|
99
|
+
const current = await lineage(request);
|
|
100
|
+
if (current.head_id!==row.id) throw new DoorError('import_superseded',409);
|
|
101
|
+
const report = await inspectRecords(request,snapshot.records);
|
|
102
|
+
await authorized(request);
|
|
103
|
+
return {planId:row.id,fingerprint:row.fingerprint,bundleSha256:snapshot.bundleSha256,...report};
|
|
104
|
+
}
|
|
105
|
+
export async function undo(request: DoorRequest, input: Record<string,unknown>) {
|
|
106
|
+
const id = text(input.planId,'plan_id');
|
|
107
|
+
const candidate = await request.options.database.prepare('SELECT * FROM ik_door_plans WHERE id=? AND installation_id=? AND namespace=?').bind(id,request.access.installationId,request.access.namespace).first<DoorPlanRow>();
|
|
108
|
+
if (!candidate) throw new DoorError('plan_not_found',404);
|
|
109
|
+
const row = await associated(request,candidate);
|
|
110
|
+
if (!row.job_id) {
|
|
111
|
+
// Cancel an interrupted private preparation, which has no kit effects.
|
|
112
|
+
await transact(request.options.database,request.authority,[
|
|
113
|
+
assertion(sql('NOT EXISTS(SELECT 1 FROM ik_jobs WHERE destination=? AND principal_id=? AND operation_key=?)',request.options.destination,request.options.ownerPrincipalId,'door:'+id)),
|
|
114
|
+
sql('UPDATE ik_door_namespaces SET pending_id=NULL WHERE installation_id=? AND namespace=? AND pending_id=?',request.access.installationId,request.access.namespace,id),
|
|
115
|
+
// A large job's creation staged in part is private custody, never a job: it goes too.
|
|
116
|
+
sql('DELETE FROM ik_d1_staging WHERE destination=? AND principal_id=? AND operation_key=?',request.options.destination,request.options.ownerPrincipalId,'door:'+id),
|
|
117
|
+
sql('DELETE FROM ik_door_plan_chunks WHERE plan_id=?',id),sql('DELETE FROM ik_door_approvals WHERE plan_id=?',id),sql('DELETE FROM ik_door_plans WHERE id=? AND job_id IS NULL',id),
|
|
118
|
+
]);
|
|
119
|
+
return {planId:id,state:'cancelled'};
|
|
120
|
+
}
|
|
121
|
+
const context = await jobContext(request,{...row,job_id:row.job_id}), job = await readD1ImportJob(row.job_id,context);
|
|
122
|
+
const result = await (['completed','undoing','undone'].includes(job.state)?undoD1Import(job.id,context):rollbackD1Import(job.id,context)).catch(error=>{throw undoRefusal(error) ?? error;});
|
|
123
|
+
await lineage(request);
|
|
124
|
+
return {planId:id,job:result};
|
|
125
|
+
}
|
|
126
|
+
/** The kit's undo refusals an owner can act on, by name, never a bare conflict. */
|
|
127
|
+
const undoRefusals: Record<string,[string,string]> = {
|
|
128
|
+
IMPORT_SUPERSEDED: ['import_superseded','A newer import of this source still holds records this import wrote. Undo the newer import first.'],
|
|
129
|
+
NATIVE_EDIT_CONFLICT: ['native_edit_conflict','Records this import wrote were changed in the seed since, and its undo is strict, so nothing more was undone.'],
|
|
130
|
+
};
|
|
131
|
+
function undoRefusal(error: unknown): DoorError | undefined {
|
|
132
|
+
const known = error instanceof Error && !(error instanceof DoorError) && Object.hasOwn(undoRefusals,error.message) ? undoRefusals[error.message] : undefined;
|
|
133
|
+
return known && new DoorError(known[0],409,{reason:known[1]});
|
|
134
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { ImportSizeLimitError } from '../d1/native-set.ts';
|
|
2
|
+
import { DoorError } from './protocol.ts';
|
|
3
|
+
|
|
4
|
+
const codes = new Set(['IMPORT_D1_TRANSPORT_LIMIT', 'IMPORT_D1_VALUE_LIMIT', 'IMPORT_D1_STATEMENT_LIMIT', 'IMPORT_D1_REQUEST_LIMIT']);
|
|
5
|
+
/** A kit size limit is a named 413, never a lineage conflict. Only the adapter's
|
|
6
|
+
* target kinds are reflected, never record content. */
|
|
7
|
+
export function sizeLimitError(error: unknown): DoorError | undefined {
|
|
8
|
+
if (!(error instanceof Error) || !codes.has(error.message)) return undefined;
|
|
9
|
+
const targetKinds = error instanceof ImportSizeLimitError ? error.targetKinds : [];
|
|
10
|
+
const reason = targetKinds.length
|
|
11
|
+
? `The exact native snapshot of target kind ${targetKinds.map(kind => JSON.stringify(kind)).join(', ')} exceeds one 2 MiB D1 read; the seed's target adapter needs a compact matchesMany.`
|
|
12
|
+
: error instanceof ImportSizeLimitError ? 'The import custody fence exceeds one 2 MiB D1 read.'
|
|
13
|
+
: error.message === 'IMPORT_D1_REQUEST_LIMIT' ? 'This request needs more than its 1,000 D1 statements; the seed\'s target adapter must read its native set in a fixed number of queries.'
|
|
14
|
+
: 'A D1 statement of this import exceeds its size limit.';
|
|
15
|
+
return new DoorError('import_door_too_large', 413, {targetKinds, reason});
|
|
16
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { DoorRequest, ImportOperatorOptions } from './types.ts';
|
|
2
|
+
import { DoorError } from './protocol.ts';
|
|
3
|
+
import { createDoorHandler, runtimeOptions } from './handler.ts';
|
|
4
|
+
import { authorized, doorFence, erasures, schemaCurrent, sql } from './store.ts';
|
|
5
|
+
export { importDoorSchema, importDoorApplyFailureMigration, importDoorScopesMigration, importDoorArchiveMigration, importDoorIntakeMigration } from './schema.ts';
|
|
6
|
+
export { writeImportDoorSettings, readImportDoorHistory } from './authority.ts';
|
|
7
|
+
export type { ImportOperatorOptions } from './types.ts';
|
|
8
|
+
|
|
9
|
+
/** Owner-session entry point. No owner-claim module or grant key ring required. */
|
|
10
|
+
export function createImportOperatorHandler(options: ImportOperatorOptions) {
|
|
11
|
+
if (!options.installationId?.trim() || !options.ownerEmail?.trim() || !options.namespace?.trim()
|
|
12
|
+
|| new URL(options.appOrigin).origin !== options.appOrigin || typeof options.authorize !== 'function') throw new DoorError('invalid_configuration',503);
|
|
13
|
+
runtimeOptions(options);
|
|
14
|
+
return createDoorHandler('/api/operator/import/', options.database, async (request,action,database) => {
|
|
15
|
+
if (new URL(request.url).origin !== options.appOrigin || request.headers.get('Origin') !== options.appOrigin) throw new DoorError('origin_required');
|
|
16
|
+
const owner = await options.authorize(request,action);
|
|
17
|
+
if (!owner) throw new DoorError('owner_required',401);
|
|
18
|
+
// The owner's session, under the same erasure and destination fence as a grant.
|
|
19
|
+
const epoch = await erasures(database, options.destination), fence = doorFence(options.installationId, options.namespace, options.destination, epoch);
|
|
20
|
+
const authority = sql(`(${owner.sql}) AND (${fence.sql})`, ...owner.params ?? [], ...fence.params ?? []);
|
|
21
|
+
const admitted: DoorRequest = {options:{...options,database},authority,via:'operator',epoch,access:{action,installationId:options.installationId,
|
|
22
|
+
email:options.ownerEmail,namespace:options.namespace,
|
|
23
|
+
maxBytes:Math.min(options.limits?.maxBytes ?? 67_108_864,67_108_864),
|
|
24
|
+
maxChunks:1024,maxRecords:Math.min(options.limits?.maxRecords ?? 1_000_000,1_000_000)}};
|
|
25
|
+
try { await authorized(admitted); }
|
|
26
|
+
catch (error) { if (await erasures(database, options.destination) !== epoch) throw new DoorError('erased_during_request', 410); throw error; }
|
|
27
|
+
return admitted;
|
|
28
|
+
}, async (request,input) => {
|
|
29
|
+
if (request.access.action !== 'apply') return request;
|
|
30
|
+
if (typeof input.planId !== 'string' || typeof input.reviewedFingerprint !== 'string'
|
|
31
|
+
|| !/^[a-f0-9]{64}$/.test(input.reviewedFingerprint)) throw new DoorError('review_fingerprint_required',400);
|
|
32
|
+
// The browser submits the fingerprint the owner reviewed. Keep one approval
|
|
33
|
+
// for all bounded apply calls, including a resume of a Business-approved job.
|
|
34
|
+
const approval = await request.options.database.prepare(`SELECT a.approval_id,a.approved_at FROM ik_door_approvals a
|
|
35
|
+
JOIN ik_door_plans p ON p.id=a.plan_id WHERE p.id=? AND p.installation_id=? AND p.namespace=? AND a.owner=?`)
|
|
36
|
+
.bind(input.planId,options.installationId,options.namespace,options.ownerEmail).first<{approval_id:string;approved_at:string}>();
|
|
37
|
+
return {...request,access:{...request.access,fingerprint:input.reviewedFingerprint,
|
|
38
|
+
approvalRecordId:approval?.approval_id ?? 'operator:'+input.planId,
|
|
39
|
+
approvedAt:approval?.approved_at ?? new Date().toISOString()}};
|
|
40
|
+
}, schemaCurrent);
|
|
41
|
+
}
|
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
import { randomUUID } from 'node:crypto';
|
|
2
|
+
import { canonicalJson, sha256 } from '../provenance/identity.ts';
|
|
3
|
+
import { readFileSource } from '../readers/file.ts';
|
|
4
|
+
import { planD1Import } from '../d1/jobs.ts';
|
|
5
|
+
import { erasedSourcePredicates } from '../d1/erasure-claims.ts';
|
|
6
|
+
import type { ImportPlan, MappingResult, SourceBundle } from '../types.ts';
|
|
7
|
+
import type { DoorPlanRow, DoorRecord, DoorRequest, DoorSnapshot } from './types.ts';
|
|
8
|
+
import { kitContext } from './authority.ts';
|
|
9
|
+
import { importedTargets, recordKey } from './inventory.ts';
|
|
10
|
+
import { DoorError } from './protocol.ts';
|
|
11
|
+
import { assertion, lineage, loadSnapshot, planRow, saveSnapshot, sql, statement, transact } from './store.ts';
|
|
12
|
+
import { bundleBytes, text, uploadRow, type UploadRow } from './uploads.ts';
|
|
13
|
+
import { sourceRefusal } from './refusals.ts';
|
|
14
|
+
import { intakeState } from './intake-policy.ts';
|
|
15
|
+
|
|
16
|
+
export function preview(snapshot: DoorSnapshot) {
|
|
17
|
+
const {plan,differences} = snapshot;
|
|
18
|
+
return {planId:snapshot.id,fingerprint:plan.fingerprint,bundleSha256:snapshot.bundleSha256,
|
|
19
|
+
summary:{sourceRecords:snapshot.sourceCount,created:differences.additions.length,updated:differences.replacements.length,unchanged:differences.unchanged.length,gone:differences.gone.length},
|
|
20
|
+
counts:plan.counts,rejected:plan.entries.filter(e=>e.rejection).map(e=>({kind:e.source.kind,sourceId:e.source.sourceId,reason:e.rejection})),
|
|
21
|
+
diagnostics:plan.diagnostics,differences};
|
|
22
|
+
}
|
|
23
|
+
export async function plan(request: DoorRequest, input: Record<string, unknown>) {
|
|
24
|
+
if (Object.keys(input).some(k=>!['uploadId','catchup'].includes(k)) || (input.catchup !== undefined && typeof input.catchup !== 'boolean')) throw new DoorError('invalid_plan_input',400);
|
|
25
|
+
const upload = await uploadRow(request,text(input.uploadId,'upload_id'));
|
|
26
|
+
try { return await planUpload(request,input,upload); }
|
|
27
|
+
catch (error) {
|
|
28
|
+
// A reader or bundle-check refusal answers a named 4xx, never a bare conflict.
|
|
29
|
+
const refused = error instanceof DoorError ? error : sourceRefusal(error);
|
|
30
|
+
// An upload holding a credential the reader cannot remove, or a value it
|
|
31
|
+
// cannot inspect for one, never plans, so it is not kept.
|
|
32
|
+
const unreadable = refused?.code === 'source_credential' || refused?.code === 'source_invalid_or_credential_source_data';
|
|
33
|
+
if (unreadable) await forget(request,upload);
|
|
34
|
+
if (unreadable || refused?.code === 'source_erased') throw refused;
|
|
35
|
+
// Whatever refuses this plan first (a pending import, interrupted staging, a
|
|
36
|
+
// withdrawn snapshot, the catch-up shape, a bound or the source itself), an
|
|
37
|
+
// upload holding an erased record is refused for that instead, and goes. A
|
|
38
|
+
// failing screen leaves the original refusal standing. No refused plan keeps
|
|
39
|
+
// credential values the reader removes: their uploaded bytes go.
|
|
40
|
+
const withheld = await screen(request,upload).catch(screened => { if (refused && screened instanceof DoorError && screened.code === 'source_erased') throw screened; return false; });
|
|
41
|
+
if (withheld) await dropChunks(request,upload);
|
|
42
|
+
throw refused ?? error;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
async function planUpload(request: DoorRequest, input: Record<string, unknown>, upload: UploadRow) {
|
|
46
|
+
const current = await lineage(request);
|
|
47
|
+
// A plan whose apply failed, or whose import was undone or rolled back, has
|
|
48
|
+
// left the lineage: the same bundle plans afresh instead of reusing it. A
|
|
49
|
+
// catch-up compares the bundle with the applied head, so it never reuses an
|
|
50
|
+
// applied plan (the head or one before it): an unchanged source plans a
|
|
51
|
+
// zero-change catch-up against the head, while an unapplied catch-up plan of
|
|
52
|
+
// the same bundle is still reused.
|
|
53
|
+
const catchup = input.catchup === true;
|
|
54
|
+
const existing = await request.options.database.prepare(`WITH RECURSIVE applied(id) AS (
|
|
55
|
+
SELECT head_id FROM ik_door_namespaces WHERE ? AND installation_id=? AND namespace=? AND head_id IS NOT NULL
|
|
56
|
+
UNION SELECT p.previous_id FROM ik_door_plans p JOIN applied a ON p.id=a.id WHERE p.previous_id IS NOT NULL)
|
|
57
|
+
SELECT p.* FROM ik_door_plans p WHERE installation_id=? AND namespace=? AND bundle_sha256=?
|
|
58
|
+
AND NOT EXISTS(SELECT 1 FROM ik_door_apply_failures f WHERE f.plan_id=p.id)
|
|
59
|
+
AND NOT EXISTS(SELECT 1 FROM ik_jobs j WHERE j.operation_key='door:'||p.id AND j.state IN ('undone','rolled_back'))
|
|
60
|
+
AND p.id NOT IN (SELECT id FROM applied)`)
|
|
61
|
+
.bind(catchup?1:0,request.access.installationId,request.access.namespace,request.access.installationId,request.access.namespace,upload.bundle_sha256).first<DoorPlanRow>();
|
|
62
|
+
if (existing) {
|
|
63
|
+
if (!existing.complete) throw new DoorError('plan_staging_interrupted_cancel_and_replan',409,{planId:existing.id});
|
|
64
|
+
const snapshot = await loadSnapshot(request,existing);
|
|
65
|
+
if (canonicalJson(snapshot.plan.source.intake ?? null) !== canonicalJson(intakeState(upload.intake_json)?.custody ?? null)) throw new DoorError('plan_intake_changed_use_new_namespace',409);
|
|
66
|
+
if (snapshot.sourceCount > request.access.maxRecords) throw new DoorError('plan_exceeds_grant_quota');
|
|
67
|
+
if (snapshot.plan.mappingVersion !== request.options.mapping.version) throw new DoorError('snapshot_mapping_changed_use_new_namespace',409);
|
|
68
|
+
if (request.access.catchup === 'bounded' && (!snapshot.standingApproval || snapshot.standingApproval.id !== request.access.approvalRecordId || snapshot.standingApproval.at !== request.access.approvedAt)) throw new DoorError('standing_approval_mismatch');
|
|
69
|
+
await dropCredentialBytes(request,snapshot,upload);
|
|
70
|
+
return preview(snapshot);
|
|
71
|
+
}
|
|
72
|
+
if (current.pending_id) throw new DoorError('namespace_has_pending_import',409);
|
|
73
|
+
// An erasure withdrew the live import's snapshot, so nothing can be compared
|
|
74
|
+
// with it. An owner-session plan retires it as the baseline and plans a new
|
|
75
|
+
// one, once no earlier import of this source still holds a record the new one
|
|
76
|
+
// writes. A Business catch-up still answers the snapshot's 410.
|
|
77
|
+
let head = current.head_id ? await planRow(request,current.head_id) : null, retired: DoorPlanRow | null = null;
|
|
78
|
+
if (head && request.via === 'operator' && (await request.options.database.prepare('SELECT 1 FROM ik_door_plan_chunks WHERE plan_id=?').bind(head.id).first()) === null) {
|
|
79
|
+
retired = head;
|
|
80
|
+
head = null;
|
|
81
|
+
}
|
|
82
|
+
const previous = head ? await loadSnapshot(request,head) : null;
|
|
83
|
+
if (previous && input.catchup !== true) throw new DoorError('catchup_required',409);
|
|
84
|
+
if (!previous && (input.catchup === true || request.access.catchup === 'bounded')) throw new DoorError('catchup_requires_baseline',409);
|
|
85
|
+
// A catch-up reads the destination's whole native set, which a large destination
|
|
86
|
+
// refuses (413); an upload holding an erased record is refused and goes first.
|
|
87
|
+
if (previous) await screen(request,upload);
|
|
88
|
+
const planId = randomUUID(), options = request.options, {kitNamespace,source,retainedFrom} = await readUpload(request,upload,planId);
|
|
89
|
+
const prior = new Map((previous?.records ?? []).map(r=>[recordKey(r),r]));
|
|
90
|
+
const revisions = new Map<string,{revision:string;matches:boolean}>();
|
|
91
|
+
const priorTargets = [...prior.values()].flatMap(record=>record.targets), found = await importedTargets(request,priorTargets);
|
|
92
|
+
priorTargets.forEach((target,i)=>revisions.set(canonicalJson([target.kind,target.id]),{revision:found[i].provenance.appliedRevision,matches:found[i].matches}));
|
|
93
|
+
const snapshot: DoorSnapshot = {id:planId,namespace:request.access.namespace,bundleSha256:upload.bundle_sha256,kitNamespace,
|
|
94
|
+
previous:previous?.id ?? null,records:[],sourceCount:0,plan:{} as ImportPlan,
|
|
95
|
+
differences:{additions:[],replacements:[],unchanged:[],gone:[],conflicts:[]},
|
|
96
|
+
standingApproval:request.access.catchup==='bounded'?{id:request.access.approvalRecordId!,at:request.access.approvedAt!}:null};
|
|
97
|
+
const mapping = {version:options.mapping.version,map(record:typeof source.records[number]):MappingResult {
|
|
98
|
+
const key = recordKey(record), old = prior.get(key);
|
|
99
|
+
let result: MappingResult;
|
|
100
|
+
if (old && (old.targets.length || old.explanation) && old.sourceHash === record.payloadHash && previous?.plan.mappingVersion === options.mapping.version) {
|
|
101
|
+
result = {effects:[],unmapped:[{reason:'Unchanged source; previous imported targets retained.'}]};
|
|
102
|
+
snapshot.differences.unchanged.push(key);
|
|
103
|
+
} else {
|
|
104
|
+
result = options.mapping.map(record);
|
|
105
|
+
if (old?.targets.length && !result.rejection) {
|
|
106
|
+
const keys = result.effects.map(e=>canonicalJson([e.targetKind,e.targetId])).sort();
|
|
107
|
+
const oldKeys = old.targets.map(t=>canonicalJson([t.kind,t.id])).sort();
|
|
108
|
+
if (canonicalJson(keys)!==canonicalJson(oldKeys)) result = {effects:[],rejection:'Changed target set requires a separate owner proposal; previous targets retained.'};
|
|
109
|
+
else if (keys.some(k=>!revisions.get(k)?.matches)) result = {effects:[],rejection:'Native edit conflict; previous imported targets preserved.'};
|
|
110
|
+
else result = {...result,effects:result.effects.map(e=>({...e,expectedRevision:revisions.get(canonicalJson([e.targetKind,e.targetId]))!.revision}))};
|
|
111
|
+
} else if (previous && result.effects.some(e=>e.expectedRevision!==undefined)) {
|
|
112
|
+
result = {effects:[],rejection:'Catch-up additions cannot replace records outside this import.'};
|
|
113
|
+
}
|
|
114
|
+
if (result.rejection) snapshot.differences.conflicts.push(key);
|
|
115
|
+
else if (result.effects.length) (old?.targets.length?snapshot.differences.replacements:snapshot.differences.additions).push(key);
|
|
116
|
+
}
|
|
117
|
+
return result;
|
|
118
|
+
}};
|
|
119
|
+
snapshot.plan = await planD1Import(source,mapping,kitContext(request,{...snapshot,plan:{mappingVersion:options.mapping.version} as ImportPlan}));
|
|
120
|
+
const seen = new Set<string>();
|
|
121
|
+
for (const entry of snapshot.plan.entries) {
|
|
122
|
+
const key = recordKey(entry.source), old = prior.get(key); seen.add(key);
|
|
123
|
+
const unchanged = snapshot.differences.unchanged.includes(key);
|
|
124
|
+
const record: DoorRecord = {kind:entry.source.kind,sourceId:entry.source.sourceId,
|
|
125
|
+
sourceHash:entry.rejection && old ? old.sourceHash : entry.source.payloadHash,
|
|
126
|
+
targets:entry.effects.length?entry.effects.map(e=>({kind:e.targetKind,id:e.targetId,ownerPlanId:snapshot.id,sourceIdentity:entry.identity})):(old?.targets ?? [])};
|
|
127
|
+
const reason = unchanged ? (old?.targets.length ? undefined : old?.explanation) : entry.rejection ?? (entry.unmapped.length ? entry.unmapped.map(u=>u.reason).join('; ') : undefined);
|
|
128
|
+
if (reason) record.explanation = reason;
|
|
129
|
+
snapshot.records.push(record);
|
|
130
|
+
}
|
|
131
|
+
snapshot.sourceCount = seen.size;
|
|
132
|
+
for (const [key,old] of prior) if (!seen.has(key)) {
|
|
133
|
+
snapshot.differences.gone.push(key);
|
|
134
|
+
snapshot.records.push({...old,explanation:'Absent from source snapshot; retained in the seed. No deletion proposed.'});
|
|
135
|
+
}
|
|
136
|
+
if (request.access.catchup === 'bounded') assertBounded(snapshot,previous!);
|
|
137
|
+
await refuseErased(request,upload,snapshot.plan);
|
|
138
|
+
if (retired) await retire(request,retired,snapshot.plan);
|
|
139
|
+
await saveSnapshot(request,snapshot,upload,retainedFrom);
|
|
140
|
+
await dropCredentialBytes(request,snapshot,upload);
|
|
141
|
+
return preview(snapshot);
|
|
142
|
+
}
|
|
143
|
+
/** Retire an erasure-withdrawn live import as the namespace's baseline. The kit
|
|
144
|
+
* never lets a new import write a record another live import holds, so while an
|
|
145
|
+
* earlier import of this source still holds one the new plan writes, the plan is
|
|
146
|
+
* refused now, by name, rather than its apply failing later; nothing is retired. */
|
|
147
|
+
async function retire(request: DoorRequest, head: DoorPlanRow, plan: ImportPlan): Promise<void> {
|
|
148
|
+
const db = request.options.database, writes = new Set(plan.entries.flatMap(entry=>entry.effects.map(e=>canonicalJson([e.targetKind,e.targetId]))));
|
|
149
|
+
const {results} = await db.prepare(`SELECT d.id AS plan_id,p.target_kind,p.target_id FROM ik_provenance p JOIN ik_door_plans d ON d.job_id=p.job_id
|
|
150
|
+
WHERE p.reversed=0 AND d.installation_id=? AND d.namespace=?`).bind(request.access.installationId,request.access.namespace).all<{plan_id:string;target_kind:string;target_id:string}>();
|
|
151
|
+
const holders = [...new Set(results.filter(row=>writes.has(canonicalJson([row.target_kind,row.target_id]))).map(row=>row.plan_id))];
|
|
152
|
+
if (holders.length) throw new DoorError('earlier_import_holds_records',409,{planIds:holders,
|
|
153
|
+
reason:'An earlier import of this source, whose snapshot an erasure withdrew, still holds records this plan would write. Undo the earlier import first, then plan again.'});
|
|
154
|
+
await transact(db,request.authority,[
|
|
155
|
+
assertion(sql('NOT EXISTS(SELECT 1 FROM ik_door_plan_chunks WHERE plan_id=?)',head.id)),
|
|
156
|
+
sql('UPDATE ik_door_namespaces SET head_id=NULL WHERE installation_id=? AND namespace=? AND head_id=? AND pending_id IS NULL',request.access.installationId,request.access.namespace,head.id),
|
|
157
|
+
]);
|
|
158
|
+
}
|
|
159
|
+
/** The reader removed credential values: the saved plan holds none and withholds
|
|
160
|
+
* the original, so the uploaded bytes that still hold them go once it is saved.
|
|
161
|
+
* The upload's header stays, so repeating this plan finds the saved one. */
|
|
162
|
+
async function dropCredentialBytes(request: DoorRequest, snapshot: DoorSnapshot, upload: UploadRow): Promise<void> {
|
|
163
|
+
if (withheldCredentials(snapshot.plan.source)) await dropChunks(request,upload);
|
|
164
|
+
}
|
|
165
|
+
const withheldCredentials = (source: SourceBundle) => source.artifacts.some(a=>a.original?.status==='unavailable' && a.original.reason==='credentials-removed');
|
|
166
|
+
const dropChunks = (request: DoorRequest, upload: UploadRow) => transact(request.options.database,request.authority,[sql('DELETE FROM ik_door_chunks WHERE upload_id=?',upload.id)]);
|
|
167
|
+
/** Each plan has its own kit namespace, so a replan after a failed apply never
|
|
168
|
+
* meets the failed job's kit namespace. */
|
|
169
|
+
async function readUpload(request: DoorRequest, upload: UploadRow, planId: string) {
|
|
170
|
+
const options = request.options;
|
|
171
|
+
const intake = intakeState(upload.intake_json)?.custody;
|
|
172
|
+
const kitNamespace = 'door:' + sha256(canonicalJson([request.access.namespace,upload.bundle_sha256,planId]));
|
|
173
|
+
const retained = await retainedSource(request,upload);
|
|
174
|
+
if (retained) return {kitNamespace,retainedFrom:retained.planId,source:{...retained.source,records:retained.source.records.map(record=>({...record,namespace:kitNamespace}))}};
|
|
175
|
+
// Credential values are removed per record, never imported, and never refuse a record.
|
|
176
|
+
const source = await readFileSource({namespace:kitNamespace,name:upload.name,bytes:await bundleBytes(request,upload),format:intake ? 'ndjson' : options.format,parse:options.parse,credentials:'remove'},
|
|
177
|
+
{...options.limits,maxBytes:Math.min(options.limits?.maxBytes ?? request.access.maxBytes,request.access.maxBytes),maxRecords:Math.min(options.limits?.maxRecords ?? request.access.maxRecords,request.access.maxRecords)});
|
|
178
|
+
if (intake) source.intake = intake;
|
|
179
|
+
return {kitNamespace,source,retainedFrom:undefined};
|
|
180
|
+
}
|
|
181
|
+
/** An upload whose bytes went because the reader removed credential values
|
|
182
|
+
* from them: chunks arrive with their upload header, so a header holding none
|
|
183
|
+
* lost them. Its records come from a plan of the same bytes the namespace still
|
|
184
|
+
* holds under the current mapping version (one whose apply failed, or that was
|
|
185
|
+
* undone), which read them without those values. With none left, Business
|
|
186
|
+
* uploads the bundle again. */
|
|
187
|
+
async function retainedSource(request: DoorRequest, upload: UploadRow): Promise<{planId: string; source: SourceBundle} | null> {
|
|
188
|
+
const db = request.options.database;
|
|
189
|
+
if (upload.total_bytes === 0 && intakeState(upload.intake_json)) return null;
|
|
190
|
+
if ((await db.prepare('SELECT 1 FROM ik_door_chunks WHERE upload_id=? LIMIT 1').bind(upload.id).first()) !== null) return null;
|
|
191
|
+
const {results} = await db.prepare('SELECT * FROM ik_door_plans WHERE installation_id=? AND namespace=? AND bundle_sha256=? AND complete=1 ORDER BY rowid DESC')
|
|
192
|
+
.bind(request.access.installationId,request.access.namespace,upload.bundle_sha256).all<DoorPlanRow>();
|
|
193
|
+
for (const row of results) {
|
|
194
|
+
const snapshot = await loadSnapshot(request,row).catch(error => { if (error instanceof DoorError && error.code === 'snapshot_withdrawn') return null; throw error; });
|
|
195
|
+
// Records parsed under another mapping version are not reused.
|
|
196
|
+
if (!snapshot || !withheldCredentials(snapshot.plan.source) || snapshot.plan.mappingVersion !== request.options.mapping.version
|
|
197
|
+
|| canonicalJson(snapshot.plan.source.intake ?? null) !== canonicalJson(intakeState(upload.intake_json)?.custody ?? null)) continue;
|
|
198
|
+
if (snapshot.plan.source.records.length > request.access.maxRecords) throw new DoorError('plan_exceeds_grant_quota');
|
|
199
|
+
return {planId:row.id,source:snapshot.plan.source};
|
|
200
|
+
}
|
|
201
|
+
throw new DoorError('upload_credentials_withheld',410,{reason:'The upload held credential values, so its bytes were not kept after its plan was refused or withdrawn. Upload the bundle again.'});
|
|
202
|
+
}
|
|
203
|
+
/** The kit never stages an erased record, and neither does the door: an upload
|
|
204
|
+
* holding one can never be planned, so it goes too. The epoch fences a later erasure. */
|
|
205
|
+
export async function refuseErased(request: DoorRequest, upload: UploadRow, plan: ImportPlan): Promise<void> {
|
|
206
|
+
const db = request.options.database;
|
|
207
|
+
for (const clear of erasedSourcePredicates(plan)) if ((await statement(db, sql(`SELECT ${clear.sql} AS clear`, ...clear.params ?? [])).first())?.clear !== 1) {
|
|
208
|
+
await forget(request, upload);
|
|
209
|
+
throw new DoorError('source_erased', 409);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
const forget = (request: DoorRequest, upload: UploadRow) => transact(request.options.database, request.authority,
|
|
213
|
+
[sql('DELETE FROM ik_door_chunks WHERE upload_id=?', upload.id), sql('DELETE FROM ik_door_uploads WHERE id=?', upload.id)]);
|
|
214
|
+
/** The upload planned on its own with the plain mapping: one holding an erased
|
|
215
|
+
* record is refused and goes. Answers whether the reader removed credential values. */
|
|
216
|
+
async function screen(request: DoorRequest, upload: UploadRow): Promise<boolean> {
|
|
217
|
+
const id = randomUUID(), {kitNamespace,source} = await readUpload(request,upload,id), mapping = request.options.mapping;
|
|
218
|
+
await refuseErased(request,upload,await planD1Import(source,mapping,kitContext(request,{id,kitNamespace,plan:{mappingVersion:mapping.version} as ImportPlan})));
|
|
219
|
+
return withheldCredentials(source);
|
|
220
|
+
}
|
|
221
|
+
export function assertBounded(snapshot: DoorSnapshot, previous: DoorSnapshot): void {
|
|
222
|
+
const d = snapshot.differences, changed = d.additions.length + d.replacements.length;
|
|
223
|
+
const refused = !previous.sourceCount ? 'empty_baseline' : d.gone.length ? 'source_records_gone' : d.conflicts.length ? 'native_or_mapping_conflict'
|
|
224
|
+
: snapshot.plan.counts.rejected || snapshot.records.some(r=>!r.targets.length && !d.unchanged.includes(recordKey(r))) ? 'rejected_or_unmapped_records'
|
|
225
|
+
: changed * 100 >= previous.sourceCount * 5 ? 'change_must_be_under_five_percent' : null;
|
|
226
|
+
if (refused) throw new DoorError('catchup_requires_proposal:' + refused,409);
|
|
227
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
export type DoorAction = 'uploads' | 'plan' | 'apply' | 'status' | 'reconcile' | 'undo' | 'archive' | 'restore-check';
|
|
2
|
+
/** Grant-only actions: the owner-session mount answers them 404. */
|
|
3
|
+
export const GRANT_ONLY_ACTIONS: readonly DoorAction[] = ['archive', 'restore-check'];
|
|
4
|
+
|
|
5
|
+
export interface DoorGrant {
|
|
6
|
+
v: 1;
|
|
7
|
+
iss: string;
|
|
8
|
+
aud: string;
|
|
9
|
+
installationId: string;
|
|
10
|
+
email: string;
|
|
11
|
+
jti: string;
|
|
12
|
+
iat: number;
|
|
13
|
+
exp: number;
|
|
14
|
+
action: DoorAction;
|
|
15
|
+
namespace: string;
|
|
16
|
+
maxBytes: number;
|
|
17
|
+
maxChunks: number;
|
|
18
|
+
maxRecords: number;
|
|
19
|
+
fingerprint?: string;
|
|
20
|
+
approvalRecordId?: string;
|
|
21
|
+
approvedAt?: string;
|
|
22
|
+
catchup?: 'bounded';
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export class DoorError extends Error {
|
|
26
|
+
readonly code: string;
|
|
27
|
+
readonly status: number;
|
|
28
|
+
readonly details?: Record<string, unknown>;
|
|
29
|
+
|
|
30
|
+
constructor(code: string, status = 403, details?: Record<string, unknown>) {
|
|
31
|
+
super(code);
|
|
32
|
+
this.name = 'DoorError';
|
|
33
|
+
this.code = code;
|
|
34
|
+
this.status = status;
|
|
35
|
+
this.details = details;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { ReaderError } from '../readers/errors.ts';
|
|
2
|
+
import { CredentialError, credentialFieldText } from '../provenance/credentials.ts';
|
|
3
|
+
import { PlanTooLargeError } from '../plan/size.ts';
|
|
4
|
+
import { DoorError } from './protocol.ts';
|
|
5
|
+
|
|
6
|
+
/** Why the reader or a bundle check refused an upload, by kit code. */
|
|
7
|
+
const reasons: Record<string, string> = {
|
|
8
|
+
MALFORMED_JSON: 'The upload is not valid JSON in the declared format.',
|
|
9
|
+
MALFORMED_UTF8: 'The upload is not valid UTF-8.',
|
|
10
|
+
INVALID_OR_CREDENTIAL_SOURCE_DATA: 'A source value is not plain JSON data, holds a URL that cannot be parsed, or is encoded too deeply to inspect for credentials.',
|
|
11
|
+
SOURCE_PARSER_FAILED: "The seed's parser refused a source value.",
|
|
12
|
+
SOURCE_OBJECT_DROPPED: "The seed's parser returned no record for a source value.",
|
|
13
|
+
INVALID_PARSED_RECORDS: "The seed's parser returned an invalid record list.",
|
|
14
|
+
INVALID_PARSED_RECORD: "The seed's parser returned an invalid record.",
|
|
15
|
+
INVALID_PARSED_RECORD_ID: "The seed's parser returned a record without a valid kind or source id.",
|
|
16
|
+
INVALID_SOURCE_POINTER: "The seed's parser returned an invalid JSON pointer.",
|
|
17
|
+
INVALID_SOURCE_ID: 'The upload name is not a valid source name.',
|
|
18
|
+
SOURCE_BYTE_LIMIT: 'The upload exceeds the byte limit.',
|
|
19
|
+
SOURCE_RECORD_LIMIT: 'The upload holds more records than the limit.',
|
|
20
|
+
SOURCE_RECORD_BYTE_LIMIT: 'A source record exceeds the per-record byte limit.',
|
|
21
|
+
DUPLICATE_SOURCE_CONFLICT: 'Two source records share a kind and source id but differ.',
|
|
22
|
+
DUPLICATE_TARGET: 'Two source records map to the same native target.',
|
|
23
|
+
SOURCE_EFFECT_LIMIT: 'One source record maps to more than 1,000 native effects.',
|
|
24
|
+
ACTIVE_EFFECT_FORBIDDEN: 'The mapping proposed an active native effect (a grant, job, session or enabled account).',
|
|
25
|
+
INVALID_MAPPING: "The seed's mapping returned an invalid result.",
|
|
26
|
+
REJECTED_RECORD_HAS_EFFECTS: "The seed's mapping rejected a record and also proposed effects for it.",
|
|
27
|
+
INVALID_JSON: 'A mapped value is not finite JSON.',
|
|
28
|
+
INVALID_ID: 'A mapped native target has no valid kind or id.',
|
|
29
|
+
IMPORT_PLAN_TOO_LARGE: 'The import plan exceeds its size limit.',
|
|
30
|
+
};
|
|
31
|
+
const tooLarge = new Set(['SOURCE_BYTE_LIMIT', 'SOURCE_RECORD_LIMIT', 'SOURCE_RECORD_BYTE_LIMIT', 'SOURCE_EFFECT_LIMIT', 'IMPORT_PLAN_TOO_LARGE']);
|
|
32
|
+
const credentialCodes = new Set(['CREDENTIAL_IN_SOURCE_DATA', 'CREDENTIAL_IN_DURABLE_DATA', 'CREDENTIAL_IN_DURABLE_URL']);
|
|
33
|
+
|
|
34
|
+
/** A reader or bundle-check refusal as a named 4xx: `source_credential` (422)
|
|
35
|
+
* naming the field kind, `source_<code>` (422), or 413 for a size bound. Details
|
|
36
|
+
* carry a fixed reason and the row/pointer, never a source value. Anything else
|
|
37
|
+
* (an adapter, guard or storage failure) is not a source refusal. */
|
|
38
|
+
export function sourceRefusal(error: unknown): DoorError | undefined {
|
|
39
|
+
if (!(error instanceof Error)) return undefined;
|
|
40
|
+
const code = error instanceof ReaderError ? error.code : error instanceof PlanTooLargeError ? error.code : error.message.split(':')[0];
|
|
41
|
+
const field = error instanceof ReaderError ? error.field : error instanceof CredentialError ? credentialFieldText(error.field) : undefined;
|
|
42
|
+
const location = error instanceof ReaderError ? error.diagnostics[0]?.location : undefined;
|
|
43
|
+
const at = {...(location?.row !== undefined ? {row: location.row} : {}), ...(location?.pointer !== undefined ? {pointer: location.pointer} : {})};
|
|
44
|
+
if (field !== undefined && (credentialCodes.has(code) || code === 'INVALID_OR_CREDENTIAL_SOURCE_DATA')) {
|
|
45
|
+
return new DoorError('source_credential', 422, {field,
|
|
46
|
+
reason: `The upload holds a credential the door cannot remove: a ${field}. Remove it and upload again.`, ...at});
|
|
47
|
+
}
|
|
48
|
+
if (!(error instanceof ReaderError) && !Object.hasOwn(reasons, code)) return undefined;
|
|
49
|
+
const reason = reasons[code] ?? `The reader refused the upload (${code}).`;
|
|
50
|
+
return new DoorError('source_' + code.toLowerCase().replace(/^source_/, ''), tooLarge.has(code) ? 413 : 422, {reason, ...at});
|
|
51
|
+
}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
function planTables(suffix = ''): string[] {
|
|
2
|
+
return [
|
|
3
|
+
`CREATE TABLE IF NOT EXISTS ik_door_plans${suffix} (
|
|
4
|
+
id TEXT PRIMARY KEY, installation_id TEXT NOT NULL, namespace TEXT NOT NULL,
|
|
5
|
+
bundle_sha256 TEXT NOT NULL, fingerprint TEXT NOT NULL, previous_id TEXT,
|
|
6
|
+
job_id TEXT UNIQUE, snapshot_hash TEXT NOT NULL,
|
|
7
|
+
complete INTEGER NOT NULL DEFAULT 0 CHECK(complete IN (0,1)))`,
|
|
8
|
+
`CREATE TABLE IF NOT EXISTS ik_door_plan_chunks${suffix} (
|
|
9
|
+
plan_id TEXT NOT NULL REFERENCES ik_door_plans${suffix}(id), ordinal INTEGER NOT NULL,
|
|
10
|
+
body TEXT NOT NULL, PRIMARY KEY(plan_id,ordinal))`,
|
|
11
|
+
`CREATE TABLE IF NOT EXISTS ik_door_approvals${suffix} (
|
|
12
|
+
plan_id TEXT PRIMARY KEY REFERENCES ik_door_plans${suffix}(id), approval_id TEXT NOT NULL,
|
|
13
|
+
owner TEXT NOT NULL, approved_at TEXT NOT NULL, fingerprint TEXT NOT NULL,
|
|
14
|
+
reviewer TEXT NOT NULL)`,
|
|
15
|
+
];
|
|
16
|
+
}
|
|
17
|
+
/** True while ik_door_plans keeps the pre-apply-failure rule of one plan per
|
|
18
|
+
* uploaded bundle, which blocks the documented replan after a failed apply. */
|
|
19
|
+
const outdatedPlans = `EXISTS(SELECT 1 FROM pragma_index_list('ik_door_plans') l WHERE l."unique"=1 AND (SELECT group_concat(name) FROM pragma_index_info(l.name))='installation_id,namespace,bundle_sha256')`;
|
|
20
|
+
/** For doors installed before erasure scoping: run in a new numbered migration.
|
|
21
|
+
* Additive; one logical namespace belongs to one product destination, even
|
|
22
|
+
* before an upload has a plan or a kit job. */
|
|
23
|
+
export const importDoorScopesMigration = [
|
|
24
|
+
`CREATE TABLE IF NOT EXISTS ik_door_scopes (
|
|
25
|
+
installation_id TEXT NOT NULL, namespace TEXT NOT NULL, destination TEXT NOT NULL,
|
|
26
|
+
PRIMARY KEY(installation_id,namespace))`,
|
|
27
|
+
];
|
|
28
|
+
/** One row, `current` 1 only when the failure and scope tables exist and the old rule is gone. */
|
|
29
|
+
export const importDoorSchemaCheck = `SELECT EXISTS(SELECT 1 FROM sqlite_master WHERE type='table' AND name='ik_door_apply_failures')
|
|
30
|
+
AND EXISTS(SELECT 1 FROM sqlite_master WHERE type='table' AND name='ik_door_scopes') AND NOT ${outdatedPlans} AS current`;
|
|
31
|
+
const failureSchema = [
|
|
32
|
+
`CREATE INDEX IF NOT EXISTS ik_door_plan_bundle ON ik_door_plans(installation_id,namespace,bundle_sha256)`,
|
|
33
|
+
`CREATE TABLE IF NOT EXISTS ik_door_apply_failures (
|
|
34
|
+
plan_id TEXT PRIMARY KEY REFERENCES ik_door_plans(id), reason_code TEXT NOT NULL)`,
|
|
35
|
+
];
|
|
36
|
+
/** For doors installed before apply-failure support: run ONCE in a new numbered
|
|
37
|
+
* migration, in one database.batch(). Copy children before dropping parents so
|
|
38
|
+
* foreign keys stay enabled and every retained approval/snapshot survives. */
|
|
39
|
+
export const importDoorApplyFailureMigration = [
|
|
40
|
+
...planTables('_apply_failure'),
|
|
41
|
+
'INSERT INTO ik_door_plans_apply_failure SELECT * FROM ik_door_plans',
|
|
42
|
+
'INSERT INTO ik_door_plan_chunks_apply_failure SELECT * FROM ik_door_plan_chunks',
|
|
43
|
+
'INSERT INTO ik_door_approvals_apply_failure SELECT * FROM ik_door_approvals',
|
|
44
|
+
'DROP TABLE ik_door_approvals',
|
|
45
|
+
'DROP TABLE ik_door_plan_chunks',
|
|
46
|
+
'DROP TABLE ik_door_plans',
|
|
47
|
+
'ALTER TABLE ik_door_plans_apply_failure RENAME TO ik_door_plans',
|
|
48
|
+
'ALTER TABLE ik_door_plan_chunks_apply_failure RENAME TO ik_door_plan_chunks',
|
|
49
|
+
'ALTER TABLE ik_door_approvals_apply_failure RENAME TO ik_door_approvals',
|
|
50
|
+
...failureSchema,
|
|
51
|
+
];
|
|
52
|
+
/** Archives of a tool's final export (the `archive` grant action), for doors
|
|
53
|
+
* installed before archives: run once in a new numbered migration. Additive.
|
|
54
|
+
* The header is custody bookkeeping and stays; an erasure withdraws the bytes. */
|
|
55
|
+
export const importDoorArchiveMigration = [
|
|
56
|
+
`CREATE TABLE IF NOT EXISTS ik_door_archives (
|
|
57
|
+
id TEXT PRIMARY KEY, installation_id TEXT NOT NULL, namespace TEXT NOT NULL,
|
|
58
|
+
upload_id TEXT NOT NULL, tool TEXT NOT NULL, taken_at TEXT NOT NULL, name TEXT NOT NULL,
|
|
59
|
+
bundle_sha256 TEXT NOT NULL, total_bytes INTEGER NOT NULL, total_chunks INTEGER NOT NULL,
|
|
60
|
+
counts_json TEXT NOT NULL, UNIQUE(installation_id,namespace,upload_id))`,
|
|
61
|
+
`CREATE TABLE IF NOT EXISTS ik_door_archive_chunks (
|
|
62
|
+
archive_id TEXT NOT NULL REFERENCES ik_door_archives(id), ordinal INTEGER NOT NULL,
|
|
63
|
+
sha256 TEXT NOT NULL, size INTEGER NOT NULL, base64 TEXT NOT NULL,
|
|
64
|
+
PRIMARY KEY(archive_id,ordinal))`,
|
|
65
|
+
];
|
|
66
|
+
/** Optional for existing doors: required only before declaring an intake list.
|
|
67
|
+
* Upload headers already participate in the door's erasure/retirement inventory. */
|
|
68
|
+
export const importDoorIntakeMigration = [
|
|
69
|
+
'ALTER TABLE ik_door_uploads ADD COLUMN intake_json TEXT',
|
|
70
|
+
];
|
|
71
|
+
/** Add these statements as a new seed migration, after the D1 import-kit schema.
|
|
72
|
+
* Installing the schema never enables the door. No seed is bound automatically.
|
|
73
|
+
* The first statement refuses the migration on a door installed before
|
|
74
|
+
* apply-failure support, whose CREATE TABLE IF NOT EXISTS would otherwise keep
|
|
75
|
+
* the old plan table: such seeds apply importDoorApplyFailureMigration instead. */
|
|
76
|
+
export const importDoorSchema = [
|
|
77
|
+
`INSERT INTO ik_d1_assertions(ok) SELECT 0 WHERE ${outdatedPlans}`,
|
|
78
|
+
`CREATE TABLE IF NOT EXISTS ik_door_settings (
|
|
79
|
+
installation_id TEXT PRIMARY KEY, enabled INTEGER NOT NULL DEFAULT 0 CHECK(enabled IN (0,1)))`,
|
|
80
|
+
`CREATE TABLE IF NOT EXISTS ik_door_revoked_keys (
|
|
81
|
+
installation_id TEXT NOT NULL, kid TEXT NOT NULL, PRIMARY KEY(installation_id,kid))`,
|
|
82
|
+
`CREATE TABLE IF NOT EXISTS ik_door_grants (
|
|
83
|
+
installation_id TEXT NOT NULL, jti TEXT NOT NULL, action TEXT NOT NULL,
|
|
84
|
+
namespace TEXT NOT NULL, kid TEXT NOT NULL, expires INTEGER NOT NULL,
|
|
85
|
+
PRIMARY KEY(installation_id,jti))`,
|
|
86
|
+
`CREATE TABLE IF NOT EXISTS ik_door_uploads (
|
|
87
|
+
id TEXT PRIMARY KEY, installation_id TEXT NOT NULL, namespace TEXT NOT NULL,
|
|
88
|
+
name TEXT NOT NULL, bundle_sha256 TEXT NOT NULL, total_bytes INTEGER NOT NULL,
|
|
89
|
+
total_chunks INTEGER NOT NULL, intake_json TEXT)`,
|
|
90
|
+
`CREATE TABLE IF NOT EXISTS ik_door_chunks (
|
|
91
|
+
upload_id TEXT NOT NULL REFERENCES ik_door_uploads(id), ordinal INTEGER NOT NULL,
|
|
92
|
+
sha256 TEXT NOT NULL, size INTEGER NOT NULL, base64 TEXT NOT NULL,
|
|
93
|
+
PRIMARY KEY(upload_id,ordinal))`,
|
|
94
|
+
`CREATE TABLE IF NOT EXISTS ik_door_namespaces (
|
|
95
|
+
installation_id TEXT NOT NULL, namespace TEXT NOT NULL, head_id TEXT, pending_id TEXT,
|
|
96
|
+
PRIMARY KEY(installation_id,namespace))`,
|
|
97
|
+
...planTables(),
|
|
98
|
+
...failureSchema,
|
|
99
|
+
...importDoorScopesMigration,
|
|
100
|
+
...importDoorArchiveMigration,
|
|
101
|
+
];
|