@substrat-run/control-plane-api 0.49.0 → 0.50.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.
- package/dist/api.d.ts +27 -0
- package/dist/api.d.ts.map +1 -1
- package/dist/api.js +332 -4
- package/dist/api.js.map +1 -1
- package/dist/backups.d.ts +93 -0
- package/dist/backups.d.ts.map +1 -0
- package/dist/backups.js +30 -0
- package/dist/backups.js.map +1 -0
- package/dist/directory-backup.d.ts +57 -0
- package/dist/directory-backup.d.ts.map +1 -0
- package/dist/directory-backup.js +68 -0
- package/dist/directory-backup.js.map +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -0
- package/dist/index.js.map +1 -1
- package/dist/mask.d.ts +15 -0
- package/dist/mask.d.ts.map +1 -1
- package/dist/mask.js +25 -1
- package/dist/mask.js.map +1 -1
- package/dist/r2-backups.d.ts +24 -0
- package/dist/r2-backups.d.ts.map +1 -0
- package/dist/r2-backups.js +175 -0
- package/dist/r2-backups.js.map +1 -0
- package/package.json +6 -6
package/dist/api.d.ts
CHANGED
|
@@ -6,6 +6,7 @@ import type { VerticalClient } from './vertical-client.js';
|
|
|
6
6
|
import type { DeployVerticalFn, FetchVerticalModulesFn } from './deploy.js';
|
|
7
7
|
import type { PatchScriptBindingsFn } from './wfp.js';
|
|
8
8
|
import type { ObservabilityReader } from './observability.js';
|
|
9
|
+
import type { DirectoryBackupStore, ScopeBackupStore } from './backups.js';
|
|
9
10
|
import { type CustomHostnameProvisioner } from './custom-hostnames.js';
|
|
10
11
|
export interface ControlPlaneApiOptions {
|
|
11
12
|
host: ScopeHost;
|
|
@@ -109,6 +110,32 @@ export interface ControlPlaneApiOptions {
|
|
|
109
110
|
* means forgetting that costs a feature, never a leak.
|
|
110
111
|
*/
|
|
111
112
|
observability?: ObservabilityReader;
|
|
113
|
+
/**
|
|
114
|
+
* Where a reap's recoverable copy is stored (#493) — host-injected like
|
|
115
|
+
* `observability`, so this package holds no bucket binding. When present, reaping a
|
|
116
|
+
* scope writes a full-fidelity dump here FIRST and records its ref on the admin-log
|
|
117
|
+
* entry; a store that throws aborts the reap, because a wipe with no copy is exactly
|
|
118
|
+
* what the seam exists to prevent.
|
|
119
|
+
*
|
|
120
|
+
* Absent ⇒ a reap that did not explicitly ask for a backup proceeds without one (the
|
|
121
|
+
* self-host / embedded / test shape, where there is no platform bucket), and one that
|
|
122
|
+
* DID ask is refused 501 rather than silently reaping. That asymmetry is deliberate:
|
|
123
|
+
* the console always asks, so a control plane deployed with the binding missing fails
|
|
124
|
+
* loudly instead of quietly dropping the guarantee — the lesson `PLATFORM_BASE_DOMAINS`
|
|
125
|
+
* taught when it silently went unset.
|
|
126
|
+
*/
|
|
127
|
+
scopeBackups?: ScopeBackupStore;
|
|
128
|
+
/**
|
|
129
|
+
* Where the platform's OWN copies live (#40) — the directory, not a tenant's scope.
|
|
130
|
+
* Host-injected on the same posture as `scopeBackups`, and pointable at the same
|
|
131
|
+
* bucket (the key prefixes keep the two apart) or at a different one.
|
|
132
|
+
*
|
|
133
|
+
* Absent ⇒ the directory backup routes answer 501 and the cron's backup phase is
|
|
134
|
+
* skipped. Loud, never silent: a control plane running with no directory copy is a
|
|
135
|
+
* platform one bug away from unrecoverable, and that must be visible rather than
|
|
136
|
+
* inferred from an absence of backups nobody looked for.
|
|
137
|
+
*/
|
|
138
|
+
directoryBackups?: DirectoryBackupStore;
|
|
112
139
|
/**
|
|
113
140
|
* Issues + polls Cloudflare-for-SaaS custom hostnames (#305, §4.7) — host-injected
|
|
114
141
|
* like `deployVertical`, so this package holds no Cloudflare credential (D-34). When
|
package/dist/api.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"api.d.ts","sourceRoot":"","sources":["../src/api.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AA+B5B,OAAO,KAAK,EAGV,eAAe,
|
|
1
|
+
{"version":3,"file":"api.d.ts","sourceRoot":"","sources":["../src/api.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AA+B5B,OAAO,KAAK,EAGV,eAAe,EAMhB,MAAM,yBAAyB,CAAC;AACjC,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,sBAAsB,CAAC;AAGtD,OAAO,KAAK,EAAE,iBAAiB,EAAE,WAAW,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AAC3E,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AAe3D,OAAO,KAAK,EAAe,gBAAgB,EAAE,sBAAsB,EAAE,MAAM,aAAa,CAAC;AACzF,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,UAAU,CAAC;AAQtD,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,oBAAoB,CAAC;AAC9D,OAAO,KAAK,EAEV,oBAAoB,EAEpB,gBAAgB,EACjB,MAAM,cAAc,CAAC;AAEtB,OAAO,EAGL,KAAK,yBAAyB,EAC/B,MAAM,uBAAuB,CAAC;AAE/B,MAAM,WAAW,sBAAsB;IACrC,IAAI,EAAE,SAAS,CAAC;IAChB;;;;;;;OAOG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,cAAc,CAAC,CAAC;IAC3C;;;;;;OAMG;IACH,eAAe,CAAC,EAAE,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,eAAe,KAAK,OAAO,CAAC,cAAc,GAAG,SAAS,CAAC,CAAC;IAChG;;;;;;;;;OASG;IACH,sBAAsB,CAAC,EAAE,CACvB,IAAI,EAAE,MAAM,EACZ,SAAS,EAAE,MAAM,EACjB,KAAK,EAAE,eAAe,KACnB,OAAO,CAAC,cAAc,GAAG,SAAS,CAAC,CAAC;IACzC;;;;;;;OAOG;IACH,kBAAkB,CAAC,EAAE,CAAC,aAAa,EAAE,MAAM,KAAK,OAAO,CAAC,cAAc,GAAG,SAAS,CAAC,CAAC;IACpF;;;;;OAKG;IACH,cAAc,CAAC,EAAE,gBAAgB,CAAC;IAClC;;;;;OAKG;IACH,oBAAoB,CAAC,EAAE,sBAAsB,CAAC;IAC9C;;;;;;OAMG;IACH,mBAAmB,CAAC,EAAE,qBAAqB,CAAC;IAC5C;;;;;;;;OAQG;IACH,sBAAsB,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;IAC3C;;;;OAIG;IACH,YAAY,EAAE,iBAAiB,CAAC;IAChC;;;;;;OAMG;IACH,mBAAmB,CAAC,EAAE,WAAW,CAAC;IAClC;;;;;OAKG;IACH,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB;;;;;;;;OAQG;IACH,aAAa,CAAC,EAAE,mBAAmB,CAAC;IACpC;;;;;;;;;;;;;OAaG;IACH,YAAY,CAAC,EAAE,gBAAgB,CAAC;IAChC;;;;;;;;;OASG;IACH,gBAAgB,CAAC,EAAE,oBAAoB,CAAC;IACxC;;;;;;;OAOG;IACH,iBAAiB,CAAC,EAAE,yBAAyB,CAAC;IAC9C;;;;;;OAMG;IACH,mBAAmB,CAAC,EAAE,MAAM,EAAE,CAAC;CAChC;AAKD,KAAK,IAAI,GAAG;IAAE,KAAK,EAAE,eAAe,CAAC;IAAC,SAAS,EAAE,SAAS,CAAA;CAAE,CAAC;AA0O7D;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,wBAAgB,qBAAqB,CAAC,OAAO,EAAE,sBAAsB,GAAG,IAAI,CAAC;IAAE,SAAS,EAAE,IAAI,CAAA;CAAE,CAAC,CAu9FhG"}
|
package/dist/api.js
CHANGED
|
@@ -5,10 +5,11 @@ import { TENANT_HEADER } from './auth.js';
|
|
|
5
5
|
import { ControlPlaneError } from './client.js';
|
|
6
6
|
import { provisionSiblingScope } from './platform-drain.js';
|
|
7
7
|
import { mapError } from './errors.js';
|
|
8
|
-
import { maskDump } from './mask.js';
|
|
8
|
+
import { maskDump, maskRecords } from './mask.js';
|
|
9
9
|
import { assertSandboxContract, deployManifest, storedDeployManifest, deploymentRefFor, stableDeploymentRefFor, nextMigrationTag, upstreamStatusOf, } from './deploy.js';
|
|
10
10
|
import { blobStoreBindings, collectBlobStoreHandles, collectTenantStoreHandles, tenantStoreBindings, } from './tenant-stores.js';
|
|
11
11
|
import { mintPushToken, pushActorFor } from './push-token.js';
|
|
12
|
+
import { backupDirectoryIfDue } from './directory-backup.js';
|
|
12
13
|
import { isCustomHostname, validateBindableHostname, } from './custom-hostnames.js';
|
|
13
14
|
// -- request schemas ---------------------------------------------------------
|
|
14
15
|
// Parse, don't trust: every input crosses Zod at the boundary. The ids stay
|
|
@@ -142,6 +143,31 @@ const snapshotScopeBody = z.object({
|
|
|
142
143
|
kind: z.string().min(1).optional(),
|
|
143
144
|
expiresAt: z.string().datetime({ offset: true }).optional(),
|
|
144
145
|
});
|
|
146
|
+
// A reap request (#493). `backup` is deliberately TRI-STATE, not a defaulted boolean:
|
|
147
|
+
// true — back up or refuse (the console always sends this, so a control plane
|
|
148
|
+
// deployed without a backup store fails loudly instead of quietly wiping)
|
|
149
|
+
// false — the explicit "I accept an unrecoverable wipe"
|
|
150
|
+
// undefined — back up if a store is configured, proceed without one if not, which is
|
|
151
|
+
// what keeps every pre-#493 caller (and self-host) working unchanged
|
|
152
|
+
const reapScopeBody = z.object({
|
|
153
|
+
backup: z.boolean().optional(),
|
|
154
|
+
});
|
|
155
|
+
// A directory-restore request (#40). `capturedAt` addresses the copy — there is one
|
|
156
|
+
// directory, so that is its whole address. `overwrite` is the guard against the
|
|
157
|
+
// dangerous case: replaying a restore onto a control plane that already recovered.
|
|
158
|
+
const restoreDirectoryBody = z.object({
|
|
159
|
+
capturedAt: z.string().min(1),
|
|
160
|
+
overwrite: z.boolean().optional(),
|
|
161
|
+
});
|
|
162
|
+
/**
|
|
163
|
+
* How a stored backup is named in the admin log and to callers: the route that fetches
|
|
164
|
+
* it. Store-neutral by construction — the R2 key scheme stays private to the store — and
|
|
165
|
+
* an operator reading a reap entry gets an address they can actually GET, rather than a
|
|
166
|
+
* bucket path they would have to know the platform's internals to use.
|
|
167
|
+
*/
|
|
168
|
+
function backupRefOf(b) {
|
|
169
|
+
return `/tenants/${b.tenantId}/scopes/${b.scopeId}/backups/${b.capturedAt}`;
|
|
170
|
+
}
|
|
145
171
|
// A per-PR preview request (preview-and-snapshots.md §2/§9 — the "run a new version
|
|
146
172
|
// against a fork of prod" slice). `tag` is a short DNS-safe label (`pr-123`): it names
|
|
147
173
|
// the preview both in its scope slug (`<vertical>--<tag>`) and in its hostname
|
|
@@ -391,6 +417,7 @@ export function createControlPlaneApi(options) {
|
|
|
391
417
|
app.post('/tenants/:tenantId/reap', async (c) => {
|
|
392
418
|
const tenantId = tenantIdSchema.parse(c.req.param('tenantId'));
|
|
393
419
|
const actor = c.get('actor');
|
|
420
|
+
const { backup: wantsBackup } = reapScopeBody.parse(await c.req.json().catch(() => ({})));
|
|
394
421
|
const tenant = await admin.getTenant(actor, tenantId);
|
|
395
422
|
if (!tenant)
|
|
396
423
|
return c.json({ error: `unknown tenant: ${tenantId}` }, 404);
|
|
@@ -405,12 +432,22 @@ export function createControlPlaneApi(options) {
|
|
|
405
432
|
continue;
|
|
406
433
|
if (scope.status !== 'archived')
|
|
407
434
|
await admin.archiveScope(actor, tenantId, scope.id);
|
|
435
|
+
// A backup here is OPT-IN, the inverse of the per-scope reap's default (#493).
|
|
436
|
+
// A scope reap is operational cleanup, so leaving a copy is the safe default; a
|
|
437
|
+
// TENANT reap is the deletion of a customer, and §4.8 exists partly to serve an
|
|
438
|
+
// Art. 17 erasure — silently writing that customer's data to a bucket the reap
|
|
439
|
+
// does not clear would defeat the request it was made to satisfy. Staff who are
|
|
440
|
+
// retiring (not erasing) a tenant pass `backup: true` deliberately.
|
|
441
|
+
const backup = wantsBackup === true ? await backupScope(c, tenantId, scope) : null;
|
|
408
442
|
const vertical = await verticalForScope(c, scope);
|
|
409
443
|
if (vertical)
|
|
410
444
|
await vertical.deleteScope({ scopeId: scope.id });
|
|
411
445
|
// Tenant teardown reaps every scope and releases every name by design — force past
|
|
412
446
|
// the bound-hostname guard (which fences the interactive per-scope reap route below).
|
|
413
|
-
await admin.reapScope(actor, tenantId, scope.id, {
|
|
447
|
+
await admin.reapScope(actor, tenantId, scope.id, {
|
|
448
|
+
force: true,
|
|
449
|
+
...(backup ? { backupRef: backupRefOf(backup) } : {}),
|
|
450
|
+
});
|
|
414
451
|
}
|
|
415
452
|
await admin.reapTenant(actor, tenantId);
|
|
416
453
|
return c.json(await admin.getTenant(actor, tenantId));
|
|
@@ -1051,6 +1088,154 @@ export function createControlPlaneApi(options) {
|
|
|
1051
1088
|
throw e;
|
|
1052
1089
|
}
|
|
1053
1090
|
});
|
|
1091
|
+
// -- backups (#493) --------------------------------------------------------
|
|
1092
|
+
// The recoverable copy a reap leaves behind. Delegation mirrors the export route
|
|
1093
|
+
// exactly: `admin.exportScope` is the canonical call — it writes the K-24 access-log
|
|
1094
|
+
// entry, and IS the bytes when the host is co-located — and a vertical-held scope's
|
|
1095
|
+
// real tables overlay it. FULL fidelity, never masked: a masked dump cannot restore,
|
|
1096
|
+
// and a backup that cannot restore is a false promise (see `backups.ts`).
|
|
1097
|
+
const backupScope = async (c, tenantId, scope) => {
|
|
1098
|
+
const store = options.scopeBackups;
|
|
1099
|
+
if (!store) {
|
|
1100
|
+
throw new ControlPlaneError(501, 'no backup target configured — this control plane cannot store a scope backup ' +
|
|
1101
|
+
'(bind one, or reap with backup=false to accept an unrecoverable wipe)');
|
|
1102
|
+
}
|
|
1103
|
+
// Residency (K-7/K-32): the platform bucket is global, so writing a jurisdiction-
|
|
1104
|
+
// pinned scope's bytes into it would move them out of the region the scope was
|
|
1105
|
+
// promised. Refuse rather than back up to the wrong place — and, because the reap
|
|
1106
|
+
// aborts with it, rather than wipe a scope we cannot legally copy.
|
|
1107
|
+
if (scope.jurisdiction !== 'global') {
|
|
1108
|
+
throw new ControlPlaneError(409, `scope ${scope.id} is pinned to '${scope.jurisdiction}' — the platform backup ` +
|
|
1109
|
+
`store is global, so a backup would move its data out of that jurisdiction ` +
|
|
1110
|
+
`(K-32); refused until a per-jurisdiction store exists`);
|
|
1111
|
+
}
|
|
1112
|
+
const dump = await admin.exportScope(c.get('actor'), tenantId, scope.id);
|
|
1113
|
+
const vertical = await verticalForScope(c, scope);
|
|
1114
|
+
const tables = vertical ? await vertical.exportScope(scope.id) : dump.tables;
|
|
1115
|
+
return store.put({ vertical: scope.vertical, dump: { ...dump, tables } });
|
|
1116
|
+
};
|
|
1117
|
+
// The copies held for one scope — metadata only, so listing a reaped scope's backups
|
|
1118
|
+
// is cheap and hands out no bytes. Readable AFTER the reap (that is the point): the
|
|
1119
|
+
// directory row survives as a tombstone, and this is what tells the operator a
|
|
1120
|
+
// recoverable copy exists and when it was taken.
|
|
1121
|
+
app.get('/tenants/:tenantId/scopes/:scopeId/backups', async (c) => {
|
|
1122
|
+
const tenantId = tenantIdSchema.parse(c.req.param('tenantId'));
|
|
1123
|
+
const scopeId = scopeIdSchema.parse(c.req.param('scopeId'));
|
|
1124
|
+
if (!options.scopeBackups)
|
|
1125
|
+
return c.json({ error: 'no backup target configured' }, 501);
|
|
1126
|
+
// Reads the directory, not the store, for the tenant cross-check: the store is keyed
|
|
1127
|
+
// by (tenant, scope) but nothing there proves the caller's tenant owns the scope.
|
|
1128
|
+
const scope = await admin.getScopeRecord(c.get('actor'), tenantId, scopeId);
|
|
1129
|
+
if (!scope)
|
|
1130
|
+
return c.json({ error: `unknown scope for tenant: (${tenantId}, ${scopeId})` }, 404);
|
|
1131
|
+
return c.json(await options.scopeBackups.list({ tenantId, scopeId }));
|
|
1132
|
+
});
|
|
1133
|
+
// One backup's DUMP — the restore source. Staff-only like the export it came from
|
|
1134
|
+
// (not in BUILDER_ROUTES), and full-fidelity, so it is the same governed-pull posture:
|
|
1135
|
+
// K-3 cross-checked above, and `POST …/restore` is where it goes back.
|
|
1136
|
+
app.get('/tenants/:tenantId/scopes/:scopeId/backups/:capturedAt', async (c) => {
|
|
1137
|
+
const tenantId = tenantIdSchema.parse(c.req.param('tenantId'));
|
|
1138
|
+
const scopeId = scopeIdSchema.parse(c.req.param('scopeId'));
|
|
1139
|
+
const capturedAt = c.req.param('capturedAt');
|
|
1140
|
+
if (!options.scopeBackups)
|
|
1141
|
+
return c.json({ error: 'no backup target configured' }, 501);
|
|
1142
|
+
const scope = await admin.getScopeRecord(c.get('actor'), tenantId, scopeId);
|
|
1143
|
+
if (!scope)
|
|
1144
|
+
return c.json({ error: `unknown scope for tenant: (${tenantId}, ${scopeId})` }, 404);
|
|
1145
|
+
const dump = await options.scopeBackups.get({ tenantId, scopeId, capturedAt });
|
|
1146
|
+
if (!dump)
|
|
1147
|
+
return c.json({ error: `no backup for scope ${scopeId} at ${capturedAt}` }, 404);
|
|
1148
|
+
return c.json(dump);
|
|
1149
|
+
});
|
|
1150
|
+
// Take a backup WITHOUT reaping — the standalone copy (a pre-migration checkpoint, an
|
|
1151
|
+
// export-to-keep). The reap route below takes its own; this is the same act made
|
|
1152
|
+
// available on its own, so "back up" is not something only a destructive path can do.
|
|
1153
|
+
app.post('/tenants/:tenantId/scopes/:scopeId/backups', async (c) => {
|
|
1154
|
+
const tenantId = tenantIdSchema.parse(c.req.param('tenantId'));
|
|
1155
|
+
const scopeId = scopeIdSchema.parse(c.req.param('scopeId'));
|
|
1156
|
+
const scope = await admin.getScopeRecord(c.get('actor'), tenantId, scopeId);
|
|
1157
|
+
if (!scope)
|
|
1158
|
+
return c.json({ error: `unknown scope for tenant: (${tenantId}, ${scopeId})` }, 404);
|
|
1159
|
+
try {
|
|
1160
|
+
return c.json(await backupScope(c, tenantId, scope), 201);
|
|
1161
|
+
}
|
|
1162
|
+
catch (e) {
|
|
1163
|
+
if (e instanceof ControlPlaneError) {
|
|
1164
|
+
return c.json({ error: e.message }, e.status);
|
|
1165
|
+
}
|
|
1166
|
+
throw e;
|
|
1167
|
+
}
|
|
1168
|
+
});
|
|
1169
|
+
// -- directory backups (#40) -----------------------------------------------
|
|
1170
|
+
// The platform's own disaster recovery, on the same store posture as the scope
|
|
1171
|
+
// backups above and deliberately on a different axis from them: a scope has ~30-day
|
|
1172
|
+
// point-in-time recovery, the directory has one Durable Object and no second copy of
|
|
1173
|
+
// the mapping that makes every scope addressable. These routes are the manual arms of
|
|
1174
|
+
// the cron phase (`backupDirectoryIfDue`) — take one now, see what is held, and the
|
|
1175
|
+
// break-glass restore. Staff-only: none is in BUILDER_ROUTES, and none is per-tenant,
|
|
1176
|
+
// because the subject of all three is every tenant at once.
|
|
1177
|
+
// What copies exist, newest first — metadata only, so this is cheap and hands out no
|
|
1178
|
+
// bytes. This is also the answer to "is the backup actually running?", which is the
|
|
1179
|
+
// question an unrehearsed backup story never has a way to ask.
|
|
1180
|
+
app.get('/directory/backups', async (c) => {
|
|
1181
|
+
if (!options.directoryBackups)
|
|
1182
|
+
return c.json({ error: 'no directory backup target configured' }, 501);
|
|
1183
|
+
return c.json(await options.directoryBackups.list());
|
|
1184
|
+
});
|
|
1185
|
+
// Take one NOW, cadence ignored — the pre-migration checkpoint an operator takes by
|
|
1186
|
+
// hand before touching the directory, and the way a fresh deployment gets its first
|
|
1187
|
+
// copy without waiting a day for the cron.
|
|
1188
|
+
app.post('/directory/backups', async (c) => {
|
|
1189
|
+
if (!options.directoryBackups)
|
|
1190
|
+
return c.json({ error: 'no directory backup target configured' }, 501);
|
|
1191
|
+
const result = await backupDirectoryIfDue({
|
|
1192
|
+
admin,
|
|
1193
|
+
store: options.directoryBackups,
|
|
1194
|
+
actor: c.get('actor'),
|
|
1195
|
+
force: true,
|
|
1196
|
+
});
|
|
1197
|
+
return c.json(result.taken, 201);
|
|
1198
|
+
});
|
|
1199
|
+
// One copy's DUMP — the restore source, and the off-platform escape hatch: an operator
|
|
1200
|
+
// who wants the directory on their own disk GETs this. The most privileged read the
|
|
1201
|
+
// control plane offers (every tenant, every hostname, every identity), so staff-only
|
|
1202
|
+
// and audited by `exportDirectory` underneath.
|
|
1203
|
+
app.get('/directory/backups/:capturedAt', async (c) => {
|
|
1204
|
+
if (!options.directoryBackups)
|
|
1205
|
+
return c.json({ error: 'no directory backup target configured' }, 501);
|
|
1206
|
+
const capturedAt = c.req.param('capturedAt');
|
|
1207
|
+
const dump = await options.directoryBackups.get({ capturedAt });
|
|
1208
|
+
if (!dump)
|
|
1209
|
+
return c.json({ error: `no directory backup at ${capturedAt}` }, 404);
|
|
1210
|
+
return c.json(dump);
|
|
1211
|
+
});
|
|
1212
|
+
// Break-glass: REPLACE the directory with a stored copy.
|
|
1213
|
+
//
|
|
1214
|
+
// Guarded by an explicit `overwrite` rather than a confirmation string, because the
|
|
1215
|
+
// dangerous case is not a slip of the fingers — it is a well-formed retry against a
|
|
1216
|
+
// control plane that has already recovered, which would silently roll the platform
|
|
1217
|
+
// back to the copy's moment and lose every tenant created since. So a directory that
|
|
1218
|
+
// still holds tenants refuses (409) unless the caller says, in the body, that
|
|
1219
|
+
// replacing them is the intent. An EMPTY directory — the actual disaster, a fresh DO
|
|
1220
|
+
// with nothing in it — needs no such ceremony.
|
|
1221
|
+
app.post('/directory/restore', async (c) => {
|
|
1222
|
+
if (!options.directoryBackups)
|
|
1223
|
+
return c.json({ error: 'no directory backup target configured' }, 501);
|
|
1224
|
+
const body = restoreDirectoryBody.parse(await c.req.json().catch(() => ({})));
|
|
1225
|
+
const dump = await options.directoryBackups.get({ capturedAt: body.capturedAt });
|
|
1226
|
+
if (!dump)
|
|
1227
|
+
return c.json({ error: `no directory backup at ${body.capturedAt}` }, 404);
|
|
1228
|
+
const actor = c.get('actor');
|
|
1229
|
+
const live = await admin.listTenants(actor, { limit: 1 });
|
|
1230
|
+
if (live.length > 0 && !body.overwrite) {
|
|
1231
|
+
return c.json({
|
|
1232
|
+
error: 'the directory is not empty — a restore REPLACES it, so anything created ' +
|
|
1233
|
+
'since this copy was taken would be lost; pass overwrite=true to confirm',
|
|
1234
|
+
}, 409);
|
|
1235
|
+
}
|
|
1236
|
+
await admin.restoreDirectory(actor, dump);
|
|
1237
|
+
return c.json({ capturedAt: dump.capturedAt, tables: dump.tables.length });
|
|
1238
|
+
});
|
|
1054
1239
|
// Reap an ARCHIVED primary scope (control-plane.md §4.4): free its DO storage —
|
|
1055
1240
|
// Cloudflare never garbage-collects a Durable Object, so a deleted app's bytes persist
|
|
1056
1241
|
// forever otherwise — while keeping the directory row as a tombstone. A POST verb, not
|
|
@@ -1064,6 +1249,9 @@ export function createControlPlaneApi(options) {
|
|
|
1064
1249
|
const tenantId = tenantIdSchema.parse(c.req.param('tenantId'));
|
|
1065
1250
|
const scopeId = scopeIdSchema.parse(c.req.param('scopeId'));
|
|
1066
1251
|
const actor = c.get('actor');
|
|
1252
|
+
// Body is optional so the bare `POST …/reap` every existing caller sends still
|
|
1253
|
+
// parses; `backup` tri-states on purpose (see the ordering comment below).
|
|
1254
|
+
const { backup: wantsBackup } = reapScopeBody.parse(await c.req.json().catch(() => ({})));
|
|
1067
1255
|
const scope = await admin.getScopeRecord(actor, tenantId, scopeId);
|
|
1068
1256
|
if (!scope)
|
|
1069
1257
|
return c.json({ error: `unknown scope for tenant: (${tenantId}, ${scopeId})` }, 404);
|
|
@@ -1082,12 +1270,41 @@ export function createControlPlaneApi(options) {
|
|
|
1082
1270
|
`unbind it before reaping (reap wipes storage and cannot be undone)`,
|
|
1083
1271
|
}, 409);
|
|
1084
1272
|
}
|
|
1273
|
+
// The recoverable copy, BEFORE any byte is wiped (#493). Ordering is the whole
|
|
1274
|
+
// guarantee: `backupScope` has to have resolved — durably stored — before
|
|
1275
|
+
// `deleteScope` runs, so a store that throws (or is missing when one was asked for)
|
|
1276
|
+
// aborts the reap with the scope intact. `backup: false` is the explicit "I accept an
|
|
1277
|
+
// unrecoverable wipe"; omitting it backs up when a store is configured and proceeds
|
|
1278
|
+
// without one when the platform has none.
|
|
1279
|
+
//
|
|
1280
|
+
// Its own try/catch, OUTSIDE the reap's: a backup failure and a reap failure are
|
|
1281
|
+
// different facts to an operator, and collapsing a dead bucket into the generic 500
|
|
1282
|
+
// would read as "the reap broke" when the scope is in fact untouched (#321's lesson).
|
|
1283
|
+
let backup = null;
|
|
1284
|
+
if (!(wantsBackup === false || (wantsBackup === undefined && !options.scopeBackups))) {
|
|
1285
|
+
try {
|
|
1286
|
+
backup = await backupScope(c, tenantId, scope);
|
|
1287
|
+
}
|
|
1288
|
+
catch (e) {
|
|
1289
|
+
if (e instanceof ControlPlaneError) {
|
|
1290
|
+
return c.json({ error: e.message }, e.status);
|
|
1291
|
+
}
|
|
1292
|
+
return c.json({
|
|
1293
|
+
error: 'backup failed — the scope was NOT reaped and its data is intact; ' +
|
|
1294
|
+
'retry, or reap with backup=false to accept an unrecoverable wipe',
|
|
1295
|
+
detail: e instanceof Error ? e.message : String(e),
|
|
1296
|
+
}, 502);
|
|
1297
|
+
}
|
|
1298
|
+
}
|
|
1085
1299
|
try {
|
|
1086
1300
|
const vertical = await verticalForScope(c, scope);
|
|
1087
1301
|
if (vertical)
|
|
1088
1302
|
await vertical.deleteScope({ scopeId });
|
|
1089
|
-
await admin.reapScope(actor, tenantId, scopeId
|
|
1090
|
-
|
|
1303
|
+
await admin.reapScope(actor, tenantId, scopeId, {
|
|
1304
|
+
...(backup ? { backupRef: backupRefOf(backup) } : {}),
|
|
1305
|
+
});
|
|
1306
|
+
const reaped = await admin.getScopeRecord(actor, tenantId, scopeId);
|
|
1307
|
+
return c.json({ ...reaped, backup });
|
|
1091
1308
|
}
|
|
1092
1309
|
catch (e) {
|
|
1093
1310
|
if (e instanceof ControlPlaneError) {
|
|
@@ -1136,6 +1353,117 @@ export function createControlPlaneApi(options) {
|
|
|
1136
1353
|
throw e;
|
|
1137
1354
|
}
|
|
1138
1355
|
});
|
|
1356
|
+
// -- tenant export (#36) ----------------------------------------------------
|
|
1357
|
+
//
|
|
1358
|
+
// GDPR Art. 20 portability, and the escrow handover: one tenant, whole, in one file.
|
|
1359
|
+
//
|
|
1360
|
+
// Composed ENTIRELY from the sanctioned reads above — `listScopes`, `listOrgs`,
|
|
1361
|
+
// `listMembers`, `listRoles`, `listEntitlements`, `listIdentityLinks`,
|
|
1362
|
+
// `listHostnames`, the store ledgers, `listConnections`, `exportScope`. That is the
|
|
1363
|
+
// constraint the design puts on this route, not an implementation preference:
|
|
1364
|
+
// control-plane.md §7 says the control plane must not acquire a back door into scope
|
|
1365
|
+
// databases, and the only sanctioned path is the audited admin surface. An export
|
|
1366
|
+
// that reached past it would BE the back door — and every read here is already
|
|
1367
|
+
// K-24 access-logged, so the trail is a property of the parts.
|
|
1368
|
+
//
|
|
1369
|
+
// It is deliberately NOT the same shape as a directory dump (#40): that one is raw
|
|
1370
|
+
// tables for recovery, this one is the platform's documented vocabulary for a reader
|
|
1371
|
+
// who does not know the schema. Only the per-scope `data` is raw, because that is the
|
|
1372
|
+
// half that has to be reloadable.
|
|
1373
|
+
app.get('/tenants/:tenantId/export', async (c) => {
|
|
1374
|
+
const tenantId = tenantIdSchema.parse(c.req.param('tenantId'));
|
|
1375
|
+
const actor = c.get('actor');
|
|
1376
|
+
const tenant = await admin.getTenant(actor, tenantId);
|
|
1377
|
+
if (!tenant)
|
|
1378
|
+
return c.json({ error: `unknown tenant: ${tenantId}` }, 404);
|
|
1379
|
+
// Every scope, tombstones included: an archived or reaped scope is part of the
|
|
1380
|
+
// tenant's history, and an export that quietly dropped them would misrepresent what
|
|
1381
|
+
// the tenant was. Their DATA is a different question, handled below.
|
|
1382
|
+
const scopes = await admin.listScopes(actor, { tenantId });
|
|
1383
|
+
// Residency (K-7/K-32), checked across the WHOLE tenant before anything is read: an
|
|
1384
|
+
// export lands on a machine outside the platform's control, so one pinned scope
|
|
1385
|
+
// taints the file. Refused as a unit rather than silently exporting the global
|
|
1386
|
+
// scopes and omitting the pinned ones — a partial export that does not say it is
|
|
1387
|
+
// partial is the failure mode worth avoiding.
|
|
1388
|
+
const pinned = scopes.filter((s) => s.jurisdiction !== 'global');
|
|
1389
|
+
if (pinned.length > 0) {
|
|
1390
|
+
return c.json({
|
|
1391
|
+
error: `tenant ${tenantId} has ${pinned.length} scope(s) pinned to a jurisdiction ` +
|
|
1392
|
+
`(${[...new Set(pinned.map((s) => s.jurisdiction))].join(', ')}) — an export would ` +
|
|
1393
|
+
`move that data out of the region it was promised; refused (K-32). Export the ` +
|
|
1394
|
+
`global scopes individually, or wait for a per-jurisdiction path.`,
|
|
1395
|
+
}, 403);
|
|
1396
|
+
}
|
|
1397
|
+
const full = c.req.query('full') === 'true';
|
|
1398
|
+
try {
|
|
1399
|
+
const orgs = await admin.listOrgs(actor, tenantId);
|
|
1400
|
+
// Revoked memberships included: K-21 makes a removal a tombstone precisely because
|
|
1401
|
+
// "was a member until March" is the fact an audit asks for.
|
|
1402
|
+
const members = (await Promise.all(orgs.map((o) => admin.listMembers(actor, tenantId, o.id, { includeRevoked: true })))).flat();
|
|
1403
|
+
const [roles, entitlements, identityLinks, hostnames, stores, blobStores, connections] = await Promise.all([
|
|
1404
|
+
admin.listRoles(actor, { tenantId }),
|
|
1405
|
+
admin.listEntitlements(actor, tenantId),
|
|
1406
|
+
admin.listIdentityLinks(actor, tenantId),
|
|
1407
|
+
admin.listHostnames(actor, { tenantId }),
|
|
1408
|
+
admin.listTenantStores(actor, { tenantId }),
|
|
1409
|
+
admin.listBlobStores(actor, { tenantId }),
|
|
1410
|
+
admin.listConnections(actor, { tenantId }),
|
|
1411
|
+
]);
|
|
1412
|
+
// Scope DATA, from the same delegation the per-scope export route uses: the
|
|
1413
|
+
// canonical `exportScope` writes the audit entry and is the bytes when co-located;
|
|
1414
|
+
// a vertical-held scope's real tables overlay it. A reaped scope has no storage
|
|
1415
|
+
// left to read, so it is skipped here while its RECORD stays above — the tombstone
|
|
1416
|
+
// is honest, an error would not be.
|
|
1417
|
+
const live = scopes.filter((s) => s.status !== 'reaped');
|
|
1418
|
+
const data = [];
|
|
1419
|
+
for (const scope of live) {
|
|
1420
|
+
const dump = await admin.exportScope(actor, tenantId, scope.id);
|
|
1421
|
+
const vertical = await verticalForScope(c, scope);
|
|
1422
|
+
const tables = vertical ? await vertical.exportScope(scope.id) : dump.tables;
|
|
1423
|
+
data.push({ ...dump, tables: full ? tables : maskDump(tables) });
|
|
1424
|
+
}
|
|
1425
|
+
// The admin log is FULL-only (#36): it records what STAFF did, so it is not the
|
|
1426
|
+
// customer's Art. 20 data, and it carries staff actor ids and internal action
|
|
1427
|
+
// names. An escrow or a dispute needs it, which is why break-glass reaches it
|
|
1428
|
+
// rather than nothing reaching it.
|
|
1429
|
+
const adminLog = full ? await admin.auditLog(actor, { tenantId }) : null;
|
|
1430
|
+
const body = {
|
|
1431
|
+
tenantId,
|
|
1432
|
+
capturedAt: new Date().toISOString(),
|
|
1433
|
+
masked: !full,
|
|
1434
|
+
tenant: full ? tenant : maskRecords([tenant])[0],
|
|
1435
|
+
scopes: full ? scopes : maskRecords(scopes),
|
|
1436
|
+
orgs: full ? orgs : maskRecords(orgs),
|
|
1437
|
+
members: full ? members : maskRecords(members),
|
|
1438
|
+
// Roles, entitlements and hostnames are configuration rather than personal data,
|
|
1439
|
+
// so they read the same in both fidelities — but they still go through the sweep,
|
|
1440
|
+
// because deciding per-collection what "cannot contain PII" means is exactly the
|
|
1441
|
+
// assumption that ages badly. One rule, applied everywhere.
|
|
1442
|
+
roles: full ? roles : maskRecords(roles),
|
|
1443
|
+
entitlements: full ? entitlements : maskRecords(entitlements),
|
|
1444
|
+
// Identity links are the sharpest item here: `externalId` is usually an email.
|
|
1445
|
+
identityLinks: full ? identityLinks : maskRecords(identityLinks),
|
|
1446
|
+
hostnames: full ? hostnames : maskRecords(hostnames),
|
|
1447
|
+
stores: [...stores, ...blobStores].map((s) => ({
|
|
1448
|
+
kind: s.kind,
|
|
1449
|
+
vertical: s.vertical,
|
|
1450
|
+
binding: s.binding,
|
|
1451
|
+
ref: s.ref,
|
|
1452
|
+
createdAt: s.createdAt,
|
|
1453
|
+
})),
|
|
1454
|
+
connections: full ? connections : maskRecords(connections),
|
|
1455
|
+
adminLog,
|
|
1456
|
+
data,
|
|
1457
|
+
};
|
|
1458
|
+
return c.json(body);
|
|
1459
|
+
}
|
|
1460
|
+
catch (e) {
|
|
1461
|
+
if (e instanceof ControlPlaneError) {
|
|
1462
|
+
return c.json({ error: e.message }, e.status);
|
|
1463
|
+
}
|
|
1464
|
+
throw e;
|
|
1465
|
+
}
|
|
1466
|
+
});
|
|
1139
1467
|
// The write half of the governed pull (§8) — load a dump INTO an existing scope:
|
|
1140
1468
|
// restore a backup, back out to a snapshot, or land a locally-built world on a
|
|
1141
1469
|
// hosted app. Staff-only like the export (not in BUILDER_ROUTES). No jurisdiction
|