@substrat-run/adapter-cloudflare 0.164.0 → 0.165.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/control-plane-do.d.ts +40 -18
- package/dist/control-plane-do.d.ts.map +1 -1
- package/dist/control-plane-do.js +268 -135
- package/dist/control-plane-do.js.map +1 -1
- package/dist/host.d.ts.map +1 -1
- package/dist/host.js +26 -57
- package/dist/host.js.map +1 -1
- package/package.json +10 -10
package/dist/control-plane-do.js
CHANGED
|
@@ -842,6 +842,25 @@ export const REPLIED_METHODS = [
|
|
|
842
842
|
'deleteHostname',
|
|
843
843
|
'grantEntitlement',
|
|
844
844
|
'revokeEntitlement',
|
|
845
|
+
// #2479: these names are new even where the bare method above already exists. A DO from
|
|
846
|
+
// before the audit unit must refuse the name, not drop a trailing audit argument and commit.
|
|
847
|
+
'putTenantStoreAndAudit',
|
|
848
|
+
'putBlobStoreAndAudit',
|
|
849
|
+
'recordStoreDriverReleaseAndAudit',
|
|
850
|
+
'beginStoreDriverRolloutAndAudit',
|
|
851
|
+
'finishStoreDriverRolloutAndAudit',
|
|
852
|
+
'importDumpAndAudit',
|
|
853
|
+
'registerIdentityPoolAndAudit',
|
|
854
|
+
'insertConnectionAndAudit',
|
|
855
|
+
'updateConnectionSecretAndAudit',
|
|
856
|
+
'revokeConnectionAndAudit',
|
|
857
|
+
'linkIdentityAndAudit',
|
|
858
|
+
'unlinkIdentityAndAudit',
|
|
859
|
+
'markAccessLogDrainedAndAudit',
|
|
860
|
+
'pruneAccessLogAndAudit',
|
|
861
|
+
'writeImpersonationAndAudit',
|
|
862
|
+
'endImpersonationAndAudit',
|
|
863
|
+
'setIssueStatusAndAudit',
|
|
845
864
|
];
|
|
846
865
|
export class ControlPlaneDO extends DurableObject {
|
|
847
866
|
sql;
|
|
@@ -1287,7 +1306,7 @@ export class ControlPlaneDO extends DurableObject {
|
|
|
1287
1306
|
* are filled in afterwards, inside the same transaction. A refusal anywhere in it leaves the
|
|
1288
1307
|
* directory as it was.
|
|
1289
1308
|
*/
|
|
1290
|
-
async importDump(tables) {
|
|
1309
|
+
async importDump(tables, audit) {
|
|
1291
1310
|
// Same untrusted dump, same two holes as the scope path (#1143): names reaching SQL as
|
|
1292
1311
|
// identifiers, and a `ddl` that is never executed here but is still held to one CREATE
|
|
1293
1312
|
// TABLE. Pure input validation, so it runs before the first DROP: a refused dump touches
|
|
@@ -1326,6 +1345,10 @@ export class ControlPlaneDO extends DurableObject {
|
|
|
1326
1345
|
// A dump never carries one (`exportDump`); a hand-made dump that does is outranked too.
|
|
1327
1346
|
// Every deployment then takes this history's deliveries, whatever its counters say.
|
|
1328
1347
|
this.sql.exec("INSERT OR REPLACE INTO lifecycle_revisions (subject, revision) VALUES ('directory:epoch', ?)", Math.max(epochBefore + 1, this.lifecycleEpoch() + 1, Date.now()));
|
|
1348
|
+
// Append the restore's own row after the dump rows have landed in the rebuilt
|
|
1349
|
+
// admin table, inside this same import transaction.
|
|
1350
|
+
if (audit)
|
|
1351
|
+
this.recordAdmin(audit);
|
|
1329
1352
|
});
|
|
1330
1353
|
// A restore is a new #1764 backfill episode. It replaced the data the old failure count and
|
|
1331
1354
|
// backoff were about, so neither may delay or silence it: the count is cleared and the next
|
|
@@ -1494,21 +1517,26 @@ export class ControlPlaneDO extends DurableObject {
|
|
|
1494
1517
|
* callers converge on one canonical row and the loser can tear its orphan mint down.
|
|
1495
1518
|
* Fails closed on an unknown/non-active tenant, exactly as provisionScope does.
|
|
1496
1519
|
*/
|
|
1497
|
-
putTenantStore(row) {
|
|
1498
|
-
|
|
1499
|
-
|
|
1500
|
-
.
|
|
1501
|
-
|
|
1502
|
-
|
|
1503
|
-
|
|
1504
|
-
|
|
1505
|
-
|
|
1506
|
-
|
|
1507
|
-
|
|
1508
|
-
|
|
1509
|
-
|
|
1510
|
-
|
|
1511
|
-
|
|
1520
|
+
putTenantStore(row, audit) {
|
|
1521
|
+
let stored;
|
|
1522
|
+
this.writeAndAudit(() => {
|
|
1523
|
+
const tenantRow = this.sql
|
|
1524
|
+
.exec('SELECT status FROM tenants WHERE tenant_id = ?', row.tenantId)
|
|
1525
|
+
.toArray()[0];
|
|
1526
|
+
if (!tenantRow) {
|
|
1527
|
+
throw substratError('not_found', `cannot provision tenant store under unknown tenant: ${row.tenantId}`);
|
|
1528
|
+
}
|
|
1529
|
+
if (tenantRow.status !== 'active') {
|
|
1530
|
+
throw substratError('conflict', `cannot provision tenant store under non-active tenant (status: ${tenantRow.status}): ${row.tenantId}`);
|
|
1531
|
+
}
|
|
1532
|
+
const inserted = this.sql.exec(`INSERT OR IGNORE INTO tenant_stores (tenant_id, vertical, binding, kind, ref, created_at)
|
|
1533
|
+
VALUES (?, ?, ?, ?, ?, ?)`, row.tenantId, row.vertical, row.binding, row.kind, row.ref, row.createdAt).rowsWritten > 0;
|
|
1534
|
+
const found = this.getTenantStore(row.tenantId, row.vertical, row.binding);
|
|
1535
|
+
if (!found)
|
|
1536
|
+
throw new Error(`tenant store write did not land for tenant ${row.tenantId}`);
|
|
1537
|
+
stored = found;
|
|
1538
|
+
return inserted;
|
|
1539
|
+
}, audit);
|
|
1512
1540
|
return stored;
|
|
1513
1541
|
}
|
|
1514
1542
|
listTenantStores(filter) {
|
|
@@ -1538,21 +1566,26 @@ export class ControlPlaneDO extends DurableObject {
|
|
|
1538
1566
|
}
|
|
1539
1567
|
/** Record a minted blob store — FIRST WRITER WINS, same race protocol as
|
|
1540
1568
|
* putTenantStore: INSERT OR IGNORE then read back; the loser tears its orphan down. */
|
|
1541
|
-
putBlobStore(row) {
|
|
1542
|
-
|
|
1543
|
-
|
|
1544
|
-
.
|
|
1545
|
-
|
|
1546
|
-
|
|
1547
|
-
|
|
1548
|
-
|
|
1549
|
-
|
|
1550
|
-
|
|
1551
|
-
|
|
1552
|
-
|
|
1553
|
-
|
|
1554
|
-
|
|
1555
|
-
|
|
1569
|
+
putBlobStore(row, audit) {
|
|
1570
|
+
let stored;
|
|
1571
|
+
this.writeAndAudit(() => {
|
|
1572
|
+
const tenantRow = this.sql
|
|
1573
|
+
.exec('SELECT status FROM tenants WHERE tenant_id = ?', row.tenantId)
|
|
1574
|
+
.toArray()[0];
|
|
1575
|
+
if (!tenantRow) {
|
|
1576
|
+
throw substratError('not_found', `cannot provision blob store under unknown tenant: ${row.tenantId}`);
|
|
1577
|
+
}
|
|
1578
|
+
if (tenantRow.status !== 'active') {
|
|
1579
|
+
throw substratError('conflict', `cannot provision blob store under non-active tenant (status: ${tenantRow.status}): ${row.tenantId}`);
|
|
1580
|
+
}
|
|
1581
|
+
const inserted = this.sql.exec(`INSERT OR IGNORE INTO blob_stores (tenant_id, vertical, binding, kind, ref, created_at)
|
|
1582
|
+
VALUES (?, ?, ?, ?, ?, ?)`, row.tenantId, row.vertical, row.binding, row.kind, row.ref, row.createdAt).rowsWritten > 0;
|
|
1583
|
+
const found = this.getBlobStore(row.tenantId, row.vertical, row.binding);
|
|
1584
|
+
if (!found)
|
|
1585
|
+
throw new Error(`blob store write did not land for tenant ${row.tenantId}`);
|
|
1586
|
+
stored = found;
|
|
1587
|
+
return inserted;
|
|
1588
|
+
}, audit);
|
|
1556
1589
|
return stored;
|
|
1557
1590
|
}
|
|
1558
1591
|
listBlobStores(filter) {
|
|
@@ -1575,8 +1608,13 @@ export class ControlPlaneDO extends DurableObject {
|
|
|
1575
1608
|
// -- store-driver releases, rollouts and holds (D-63, #2300) ---------------------
|
|
1576
1609
|
// Kernel code over the directory (store-driver-releases.ts), shared with the SQLite host. Each
|
|
1577
1610
|
// call is one transaction, and the object runs one at a time, so a hold is taken atomically.
|
|
1578
|
-
recordStoreDriverRelease(input) {
|
|
1579
|
-
|
|
1611
|
+
recordStoreDriverRelease(input, audit) {
|
|
1612
|
+
let result;
|
|
1613
|
+
this.writeAndAudit(() => {
|
|
1614
|
+
result = recordStoreDriverReleaseIn(this.kernelSql, input);
|
|
1615
|
+
return result.created;
|
|
1616
|
+
}, audit ? () => ({ ...audit, after: result.record }) : undefined);
|
|
1617
|
+
return result;
|
|
1580
1618
|
}
|
|
1581
1619
|
getStoreDriverRelease(driver, sha256) {
|
|
1582
1620
|
return storeDriverReleaseIn(this.kernelSql, driver, sha256);
|
|
@@ -1599,11 +1637,21 @@ export class ControlPlaneDO extends DurableObject {
|
|
|
1599
1637
|
releaseStoreDriverHold(input) {
|
|
1600
1638
|
releaseStoreDriverHoldIn(this.kernelSql, input);
|
|
1601
1639
|
}
|
|
1602
|
-
beginStoreDriverRollout(input) {
|
|
1603
|
-
|
|
1640
|
+
beginStoreDriverRollout(input, audit) {
|
|
1641
|
+
let row;
|
|
1642
|
+
this.writeAndAudit(() => {
|
|
1643
|
+
row = beginStoreDriverRolloutIn(this.kernelSql, { ...input, nowMs: Date.now() });
|
|
1644
|
+
return true;
|
|
1645
|
+
}, audit ? () => ({ ...audit, after: row }) : undefined);
|
|
1646
|
+
return row;
|
|
1604
1647
|
}
|
|
1605
|
-
finishStoreDriverRollout(input) {
|
|
1606
|
-
|
|
1648
|
+
finishStoreDriverRollout(input, audit) {
|
|
1649
|
+
let row;
|
|
1650
|
+
this.writeAndAudit(() => {
|
|
1651
|
+
row = finishStoreDriverRolloutIn(this.kernelSql, { ...input, nowMs: Date.now() });
|
|
1652
|
+
return true;
|
|
1653
|
+
}, audit ? () => ({ ...audit, action: row.action === 'rollback' ? 'rollBackStoreDriver' : 'rollOutStoreDriver', after: row }) : undefined);
|
|
1654
|
+
return row;
|
|
1607
1655
|
}
|
|
1608
1656
|
storeDriverRebuild(driver) {
|
|
1609
1657
|
return storeDriverRebuildIn(this.kernelSql, driver);
|
|
@@ -2730,6 +2778,62 @@ export class ControlPlaneDO extends DurableObject {
|
|
|
2730
2778
|
return changed;
|
|
2731
2779
|
});
|
|
2732
2780
|
}
|
|
2781
|
+
/**
|
|
2782
|
+
* The coordinator calls these new RPC names for directory-only audited writes. The old names
|
|
2783
|
+
* remain callable by an older coordinator, which records its own row afterwards. An older DO
|
|
2784
|
+
* knows none of these names and refuses before the state write instead of dropping the row.
|
|
2785
|
+
*/
|
|
2786
|
+
putTenantStoreAndAudit(...args) {
|
|
2787
|
+
return this.putTenantStore(...args);
|
|
2788
|
+
}
|
|
2789
|
+
putBlobStoreAndAudit(...args) {
|
|
2790
|
+
return this.putBlobStore(...args);
|
|
2791
|
+
}
|
|
2792
|
+
recordStoreDriverReleaseAndAudit(...args) {
|
|
2793
|
+
return this.recordStoreDriverRelease(...args);
|
|
2794
|
+
}
|
|
2795
|
+
beginStoreDriverRolloutAndAudit(...args) {
|
|
2796
|
+
return this.beginStoreDriverRollout(...args);
|
|
2797
|
+
}
|
|
2798
|
+
finishStoreDriverRolloutAndAudit(...args) {
|
|
2799
|
+
return this.finishStoreDriverRollout(...args);
|
|
2800
|
+
}
|
|
2801
|
+
importDumpAndAudit(...args) {
|
|
2802
|
+
return this.importDump(...args);
|
|
2803
|
+
}
|
|
2804
|
+
registerIdentityPoolAndAudit(...args) {
|
|
2805
|
+
return this.registerIdentityPool(...args);
|
|
2806
|
+
}
|
|
2807
|
+
insertConnectionAndAudit(...args) {
|
|
2808
|
+
return this.insertConnection(...args);
|
|
2809
|
+
}
|
|
2810
|
+
updateConnectionSecretAndAudit(...args) {
|
|
2811
|
+
return this.updateConnectionSecret(...args);
|
|
2812
|
+
}
|
|
2813
|
+
revokeConnectionAndAudit(...args) {
|
|
2814
|
+
return this.revokeConnection(...args);
|
|
2815
|
+
}
|
|
2816
|
+
linkIdentityAndAudit(...args) {
|
|
2817
|
+
return this.linkIdentity(...args);
|
|
2818
|
+
}
|
|
2819
|
+
unlinkIdentityAndAudit(...args) {
|
|
2820
|
+
return this.unlinkIdentity(...args);
|
|
2821
|
+
}
|
|
2822
|
+
markAccessLogDrainedAndAudit(...args) {
|
|
2823
|
+
return this.markAccessLogDrained(...args);
|
|
2824
|
+
}
|
|
2825
|
+
pruneAccessLogAndAudit(...args) {
|
|
2826
|
+
return this.pruneAccessLog(...args);
|
|
2827
|
+
}
|
|
2828
|
+
writeImpersonationAndAudit(...args) {
|
|
2829
|
+
return this.writeImpersonation(...args);
|
|
2830
|
+
}
|
|
2831
|
+
endImpersonationAndAudit(...args) {
|
|
2832
|
+
return this.endImpersonation(...args);
|
|
2833
|
+
}
|
|
2834
|
+
setIssueStatusAndAudit(...args) {
|
|
2835
|
+
return this.setIssueStatus(...args);
|
|
2836
|
+
}
|
|
2733
2837
|
scopeErasureEpoch(tenantId, scopeId) {
|
|
2734
2838
|
// NULL on every row from before the column (#1722): the same 0 every CAS compares against.
|
|
2735
2839
|
const row = this.sql.exec('SELECT COALESCE(erasure_epoch, 0) AS erasure_epoch FROM scopes WHERE tenant_id = ? AND scope_id = ?', tenantId, scopeId)
|
|
@@ -3312,16 +3416,18 @@ export class ControlPlaneDO extends DurableObject {
|
|
|
3312
3416
|
.toArray()[0];
|
|
3313
3417
|
}
|
|
3314
3418
|
/** Returns false when an identical registration already exists; throws on a conflicting one. */
|
|
3315
|
-
registerIdentityPool(provider, topology, tenantId, createdAt) {
|
|
3316
|
-
|
|
3317
|
-
|
|
3318
|
-
if (existing
|
|
3319
|
-
|
|
3320
|
-
|
|
3321
|
-
|
|
3322
|
-
|
|
3323
|
-
|
|
3324
|
-
|
|
3419
|
+
registerIdentityPool(provider, topology, tenantId, createdAt, audit) {
|
|
3420
|
+
return this.writeAndAudit(() => {
|
|
3421
|
+
const existing = this.readPool(provider);
|
|
3422
|
+
if (existing) {
|
|
3423
|
+
if (existing.topology === topology && existing.tenant_id === tenantId)
|
|
3424
|
+
return false;
|
|
3425
|
+
throw substratError('conflict', `identity pool '${provider}' is already registered as ${existing.topology}` +
|
|
3426
|
+
`${existing.tenant_id ? ` for tenant ${existing.tenant_id}` : ''}`);
|
|
3427
|
+
}
|
|
3428
|
+
this.sql.exec('INSERT INTO _substrat_identity_pools (provider, topology, tenant_id, created_at) VALUES (?, ?, ?, ?)', provider, topology, tenantId, createdAt);
|
|
3429
|
+
return true;
|
|
3430
|
+
}, audit);
|
|
3325
3431
|
}
|
|
3326
3432
|
identityTenants(provider, externalId) {
|
|
3327
3433
|
return this.sql
|
|
@@ -3389,27 +3495,30 @@ export class ControlPlaneDO extends DurableObject {
|
|
|
3389
3495
|
// The DO owns the rows; the coordinator owns the SecretBox, so ciphertext
|
|
3390
3496
|
// arrives already sealed and leaves still sealed. This DO has never seen a
|
|
3391
3497
|
// plaintext credential and cannot.
|
|
3392
|
-
insertConnection(row) {
|
|
3393
|
-
|
|
3394
|
-
|
|
3395
|
-
|
|
3396
|
-
|
|
3397
|
-
|
|
3498
|
+
insertConnection(row, audit) {
|
|
3499
|
+
this.writeAndAudit(() => {
|
|
3500
|
+
// Live-uniqueness is per ACCOUNT (COALESCEd like the index): a provider
|
|
3501
|
+
// without account refs stays a singleton; a multi-namespace provider may
|
|
3502
|
+
// hold one live connection per external account.
|
|
3503
|
+
const live = this.sql
|
|
3504
|
+
.exec(`SELECT id FROM _substrat_connections
|
|
3398
3505
|
WHERE tenant_id = ? AND vertical = ? AND provider = ?
|
|
3399
3506
|
AND COALESCE(external_account_ref, '') = COALESCE(?, '') AND revoked_at IS NULL`, row.tenantId, row.vertical, row.provider, row.externalAccountRef)
|
|
3400
|
-
|
|
3401
|
-
|
|
3402
|
-
|
|
3403
|
-
|
|
3404
|
-
|
|
3405
|
-
|
|
3406
|
-
|
|
3507
|
+
.toArray()[0];
|
|
3508
|
+
if (live) {
|
|
3509
|
+
const account = row.externalAccountRef ? ` under account '${row.externalAccountRef}'` : '';
|
|
3510
|
+
throw new Error(`tenant ${row.tenantId} already has a live '${row.provider}' connection ` +
|
|
3511
|
+
`for vertical '${row.vertical}'${account} — revoke it before connecting another`);
|
|
3512
|
+
}
|
|
3513
|
+
this.sql.exec(`INSERT INTO _substrat_connections
|
|
3407
3514
|
(id, tenant_id, vertical, provider, label, status, external_account_ref,
|
|
3408
3515
|
scopes, expires_at, last_ok_at, last_error, last_error_at,
|
|
3409
3516
|
created_by, created_at, revoked_at)
|
|
3410
3517
|
VALUES (?, ?, ?, ?, ?, 'active', ?, ?, ?, NULL, NULL, NULL, ?, ?, NULL)`, row.id, row.tenantId, row.vertical, row.provider, row.label, row.externalAccountRef, row.scopes, row.expiresAt, row.createdBy, row.createdAt);
|
|
3411
|
-
|
|
3518
|
+
this.sql.exec(`INSERT INTO _substrat_connection_secrets (connection_id, key_id, ciphertext, updated_at)
|
|
3412
3519
|
VALUES (?, ?, ?, ?)`, row.id, row.keyId, row.ciphertext, row.createdAt);
|
|
3520
|
+
return true;
|
|
3521
|
+
}, audit);
|
|
3413
3522
|
}
|
|
3414
3523
|
/** This connection's key rows — every one, current and retired (#687). */
|
|
3415
3524
|
readConnectionKeys(connectionId) {
|
|
@@ -3478,12 +3587,15 @@ export class ControlPlaneDO extends DurableObject {
|
|
|
3478
3587
|
}
|
|
3479
3588
|
return rows[0];
|
|
3480
3589
|
}
|
|
3481
|
-
updateConnectionSecret(id, keyId, ciphertext, expiresAt, at) {
|
|
3482
|
-
this.
|
|
3590
|
+
updateConnectionSecret(id, keyId, ciphertext, expiresAt, at, audit) {
|
|
3591
|
+
this.writeAndAudit(() => {
|
|
3592
|
+
this.sql.exec(`UPDATE _substrat_connection_secrets
|
|
3483
3593
|
SET key_id = ?, ciphertext = ?, updated_at = ? WHERE connection_id = ?`, keyId, ciphertext, at, id);
|
|
3484
|
-
|
|
3594
|
+
this.sql.exec(`UPDATE _substrat_connections
|
|
3485
3595
|
SET status = 'active', expires_at = ?, last_error = NULL, last_error_at = NULL
|
|
3486
3596
|
WHERE id = ?`, expiresAt, id);
|
|
3597
|
+
return true;
|
|
3598
|
+
}, audit);
|
|
3487
3599
|
}
|
|
3488
3600
|
/**
|
|
3489
3601
|
* Tombstone the connection and DELETE the sealed blob.
|
|
@@ -3492,21 +3604,23 @@ export class ControlPlaneDO extends DurableObject {
|
|
|
3492
3604
|
* only be a liability. Returns false when already revoked, so the caller can
|
|
3493
3605
|
* skip an audit row for a no-op.
|
|
3494
3606
|
*/
|
|
3495
|
-
revokeConnection(id, at) {
|
|
3496
|
-
|
|
3497
|
-
|
|
3498
|
-
|
|
3499
|
-
|
|
3500
|
-
|
|
3501
|
-
|
|
3502
|
-
|
|
3503
|
-
|
|
3504
|
-
|
|
3505
|
-
|
|
3506
|
-
|
|
3507
|
-
|
|
3607
|
+
revokeConnection(id, at, audit) {
|
|
3608
|
+
return this.writeAndAudit(() => {
|
|
3609
|
+
const row = this.readConnection(id);
|
|
3610
|
+
if (!row)
|
|
3611
|
+
throw new Error(`connection not found: ${id}`);
|
|
3612
|
+
if (row.revoked_at)
|
|
3613
|
+
return false;
|
|
3614
|
+
this.sql.exec(`UPDATE _substrat_connections SET status = 'revoked', revoked_at = ? WHERE id = ?`, at, id);
|
|
3615
|
+
this.sql.exec('DELETE FROM _substrat_connection_secrets WHERE connection_id = ?', id);
|
|
3616
|
+
// Connector state dies with the connection — its private bookkeeping.
|
|
3617
|
+
this.sql.exec('DELETE FROM _substrat_connector_state WHERE connection_id = ?', id);
|
|
3618
|
+
// #592: its grants tombstone (K-21 — evidence, not roster). Absent from the
|
|
3619
|
+
// next gather, so no later provision/reconcile delivers them again.
|
|
3620
|
+
this.sql.exec(`UPDATE _substrat_connection_grants SET revoked_at = ?
|
|
3508
3621
|
WHERE connection_id = ? AND revoked_at IS NULL`, at, id);
|
|
3509
|
-
|
|
3622
|
+
return true;
|
|
3623
|
+
}, audit);
|
|
3510
3624
|
}
|
|
3511
3625
|
// -- a vertical's mailed connect links (connections.md §3.5.4) ------------------
|
|
3512
3626
|
// The kernel's statements (`connect-links.ts`), shared with the pure adapter. Each
|
|
@@ -3715,31 +3829,35 @@ export class ControlPlaneDO extends DurableObject {
|
|
|
3715
3829
|
.filter((r) => !prefix || r.state_key.startsWith(prefix))
|
|
3716
3830
|
.map((r) => ({ key: r.state_key, value: r.value }));
|
|
3717
3831
|
}
|
|
3718
|
-
linkIdentity(provider, externalId, principal, tenantId, scopeId, createdAt) {
|
|
3719
|
-
|
|
3720
|
-
|
|
3832
|
+
linkIdentity(provider, externalId, principal, tenantId, scopeId, createdAt, audit) {
|
|
3833
|
+
return this.writeAndAudit(() => {
|
|
3834
|
+
const existing = this.sql
|
|
3835
|
+
.exec(`SELECT principal_id FROM _substrat_identities
|
|
3721
3836
|
WHERE tenant_id = ? AND provider = ? AND external_id = ?`, tenantId, provider, externalId)
|
|
3722
|
-
|
|
3723
|
-
|
|
3724
|
-
|
|
3725
|
-
|
|
3726
|
-
|
|
3727
|
-
|
|
3728
|
-
|
|
3837
|
+
.toArray()[0];
|
|
3838
|
+
if (existing) {
|
|
3839
|
+
if (existing.principal_id === principal)
|
|
3840
|
+
return false;
|
|
3841
|
+
throw substratError('conflict', `identity ${provider}:${externalId} in tenant ${tenantId} is already bound to ${existing.principal_id}`);
|
|
3842
|
+
}
|
|
3843
|
+
this.sql.exec(`INSERT INTO _substrat_identities
|
|
3729
3844
|
(provider, external_id, principal_id, tenant_id, scope_id, created_at)
|
|
3730
3845
|
VALUES (?, ?, ?, ?, ?, ?)`, provider, externalId, principal, tenantId, scopeId, createdAt);
|
|
3731
|
-
|
|
3846
|
+
return true;
|
|
3847
|
+
}, audit);
|
|
3732
3848
|
}
|
|
3733
3849
|
/** Remove a principal's identity link(s) in a tenant. Returns whether a row was
|
|
3734
3850
|
* deleted so a no-op stays silent (idempotent). */
|
|
3735
|
-
unlinkIdentity(tenantId, principal) {
|
|
3736
|
-
|
|
3737
|
-
|
|
3738
|
-
|
|
3739
|
-
|
|
3740
|
-
|
|
3741
|
-
|
|
3742
|
-
|
|
3851
|
+
unlinkIdentity(tenantId, principal, audit) {
|
|
3852
|
+
return this.writeAndAudit(() => {
|
|
3853
|
+
const before = this.sql
|
|
3854
|
+
.exec(`SELECT 1 FROM _substrat_identities WHERE tenant_id = ? AND principal_id = ?`, tenantId, principal)
|
|
3855
|
+
.toArray();
|
|
3856
|
+
if (before.length === 0)
|
|
3857
|
+
return false;
|
|
3858
|
+
this.sql.exec(`DELETE FROM _substrat_identities WHERE tenant_id = ? AND principal_id = ?`, tenantId, principal);
|
|
3859
|
+
return true;
|
|
3860
|
+
}, audit);
|
|
3743
3861
|
}
|
|
3744
3862
|
resolveIdentity(tenantId, provider, externalId) {
|
|
3745
3863
|
const row = this.sql
|
|
@@ -3802,25 +3920,35 @@ export class ControlPlaneDO extends DurableObject {
|
|
|
3802
3920
|
* Stamp `drained_at` on undrained rows up to `upToId`. The `IS NULL` guard makes a
|
|
3803
3921
|
* retried shipment idempotent — it re-stamps nothing and returns 0 (K-24).
|
|
3804
3922
|
*/
|
|
3805
|
-
markAccessLogDrained(upToId, drainedAt) {
|
|
3806
|
-
|
|
3807
|
-
|
|
3808
|
-
|
|
3809
|
-
|
|
3810
|
-
|
|
3811
|
-
|
|
3812
|
-
|
|
3923
|
+
markAccessLogDrained(upToId, drainedAt, audit) {
|
|
3924
|
+
let drained = 0;
|
|
3925
|
+
this.writeAndAudit(() => {
|
|
3926
|
+
const doomed = this.sql
|
|
3927
|
+
.exec('SELECT id FROM _substrat_access_log WHERE id <= ? AND drained_at IS NULL', upToId)
|
|
3928
|
+
.toArray().map((r) => r.id);
|
|
3929
|
+
for (const id of doomed) {
|
|
3930
|
+
this.sql.exec('UPDATE _substrat_access_log SET drained_at = ? WHERE id = ?', drainedAt, id);
|
|
3931
|
+
}
|
|
3932
|
+
drained = doomed.length;
|
|
3933
|
+
return drained > 0;
|
|
3934
|
+
}, audit ? () => ({ ...audit, after: { drained, upToId, drainedAt } }) : undefined);
|
|
3935
|
+
return drained;
|
|
3813
3936
|
}
|
|
3814
3937
|
/** ONLY drained rows. Age alone is not a licence to delete evidence (K-24). */
|
|
3815
|
-
pruneAccessLog(limit) {
|
|
3816
|
-
|
|
3817
|
-
|
|
3818
|
-
|
|
3819
|
-
|
|
3820
|
-
|
|
3821
|
-
|
|
3822
|
-
|
|
3823
|
-
|
|
3938
|
+
pruneAccessLog(limit, audit) {
|
|
3939
|
+
let pruned = 0;
|
|
3940
|
+
this.writeAndAudit(() => {
|
|
3941
|
+
assertRowLimit('limit', limit);
|
|
3942
|
+
const doomed = this.sql
|
|
3943
|
+
.exec('SELECT id FROM _substrat_access_log WHERE drained_at IS NOT NULL ORDER BY id LIMIT ?', limit)
|
|
3944
|
+
.toArray().map((r) => r.id);
|
|
3945
|
+
for (const id of doomed) {
|
|
3946
|
+
this.sql.exec('DELETE FROM _substrat_access_log WHERE id = ?', id);
|
|
3947
|
+
}
|
|
3948
|
+
pruned = doomed.length;
|
|
3949
|
+
return pruned > 0;
|
|
3950
|
+
}, audit ? () => ({ ...audit, after: { pruned } }) : undefined);
|
|
3951
|
+
return pruned;
|
|
3824
3952
|
}
|
|
3825
3953
|
// -- per-subject keys (#37) -------------------------------------------------
|
|
3826
3954
|
// The three row operations `createSubjectKeys` needs. The crypto is the kernel's; what
|
|
@@ -3873,9 +4001,9 @@ export class ControlPlaneDO extends DurableObject {
|
|
|
3873
4001
|
// never reads these — the coordinator resolves the session and hands the DO the
|
|
3874
4002
|
// two actors, which keeps the DO's invoke free of a directory round-trip.
|
|
3875
4003
|
/** Insert a session the coordinator minted. Returns nothing; the caller holds it. */
|
|
3876
|
-
writeImpersonation(values) {
|
|
3877
|
-
this.sql.exec(`INSERT INTO _substrat_impersonations (${IMPERSONATION_COLUMNS})
|
|
3878
|
-
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, ...values);
|
|
4004
|
+
writeImpersonation(values, audit) {
|
|
4005
|
+
this.writeAndAudit(() => this.sql.exec(`INSERT INTO _substrat_impersonations (${IMPERSONATION_COLUMNS})
|
|
4006
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, ...values).rowsWritten > 0, audit);
|
|
3879
4007
|
}
|
|
3880
4008
|
/** One session by id, or undefined. The door's lookup and every invoke's re-read. */
|
|
3881
4009
|
readImpersonation(id) {
|
|
@@ -3883,8 +4011,8 @@ export class ControlPlaneDO extends DurableObject {
|
|
|
3883
4011
|
return this.sql.exec(q.sql, ...q.params).toArray()[0];
|
|
3884
4012
|
}
|
|
3885
4013
|
/** Close a session. Idempotent — a row already ended keeps its first `ended_at`. */
|
|
3886
|
-
endImpersonation(id, endedAt) {
|
|
3887
|
-
this.sql.exec('UPDATE _substrat_impersonations SET ended_at = ? WHERE id = ? AND ended_at IS NULL', endedAt, id);
|
|
4014
|
+
endImpersonation(id, endedAt, audit) {
|
|
4015
|
+
this.writeAndAudit(() => this.sql.exec('UPDATE _substrat_impersonations SET ended_at = ? WHERE id = ? AND ended_at IS NULL', endedAt, id).rowsWritten > 0, audit);
|
|
3888
4016
|
}
|
|
3889
4017
|
listImpersonations(filter, now) {
|
|
3890
4018
|
const q = impersonationListQuery(filter, now);
|
|
@@ -4415,22 +4543,27 @@ export class ControlPlaneDO extends DurableObject {
|
|
|
4415
4543
|
return rows.map((r) => issueOf(r));
|
|
4416
4544
|
}
|
|
4417
4545
|
/** The lifecycle write (#1233). Returns before/after for the caller's audit, or undefined. */
|
|
4418
|
-
setIssueStatus(fingerprint, status, at) {
|
|
4419
|
-
|
|
4420
|
-
|
|
4421
|
-
|
|
4422
|
-
|
|
4423
|
-
|
|
4424
|
-
|
|
4425
|
-
|
|
4426
|
-
|
|
4427
|
-
|
|
4428
|
-
|
|
4429
|
-
|
|
4430
|
-
|
|
4431
|
-
|
|
4432
|
-
|
|
4433
|
-
|
|
4546
|
+
setIssueStatus(fingerprint, status, at, audit) {
|
|
4547
|
+
let existing;
|
|
4548
|
+
let after;
|
|
4549
|
+
this.writeAndAudit(() => {
|
|
4550
|
+
const rows = this.sql
|
|
4551
|
+
.exec('SELECT * FROM _substrat_issues WHERE fingerprint = ?', fingerprint)
|
|
4552
|
+
.toArray();
|
|
4553
|
+
existing = rows[0];
|
|
4554
|
+
if (!existing)
|
|
4555
|
+
return false;
|
|
4556
|
+
const resolvedAt = status === 'resolved' ? at : null;
|
|
4557
|
+
// "Resolved under X": freeze the newest version at the verdict, so a later
|
|
4558
|
+
// regression can name the pair. A reopen or ignore clears it with the timestamp.
|
|
4559
|
+
const resolvedVersion = status === 'resolved' ? existing.last_version : null;
|
|
4560
|
+
this.sql.exec('UPDATE _substrat_issues SET status = ?, resolved_at = ?, resolved_version = ? WHERE fingerprint = ?', status, resolvedAt, resolvedVersion, fingerprint);
|
|
4561
|
+
after = { ...existing, status, resolved_at: resolvedAt, resolved_version: resolvedVersion };
|
|
4562
|
+
return true;
|
|
4563
|
+
}, audit ? () => ({ ...audit, vertical: after.last_vertical,
|
|
4564
|
+
before: { fingerprint, status: existing.status },
|
|
4565
|
+
after: { fingerprint, status: after.status } }) : undefined);
|
|
4566
|
+
return existing && after ? { before: issueOf(existing), after: issueOf(after) } : undefined;
|
|
4434
4567
|
}
|
|
4435
4568
|
/** #1054: idempotent on request_id — a replayed intent writes nothing. */
|
|
4436
4569
|
recordModelUsage(row) {
|