@happyvertical/smrt-vault-relay 0.0.0-stage → 0.60.17
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/AGENTS.md +51 -0
- package/CLAUDE.md +1 -0
- package/LICENSE +7 -0
- package/README.md +364 -2
- package/dist/auth.d.ts +66 -0
- package/dist/auth.d.ts.map +1 -0
- package/dist/chunks/server-0ITpMTMD.js +2358 -0
- package/dist/chunks/server-0ITpMTMD.js.map +1 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +43 -0
- package/dist/cli.js.map +1 -0
- package/dist/config.d.ts +86 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/encoding.d.ts +20 -0
- package/dist/encoding.d.ts.map +1 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +2 -0
- package/dist/lease-store.d.ts +108 -0
- package/dist/lease-store.d.ts.map +1 -0
- package/dist/lease.d.ts +60 -0
- package/dist/lease.d.ts.map +1 -0
- package/dist/manifest.json +15 -0
- package/dist/metadata-store.d.ts +212 -0
- package/dist/metadata-store.d.ts.map +1 -0
- package/dist/node-server.d.ts +14 -0
- package/dist/node-server.d.ts.map +1 -0
- package/dist/object-store.d.ts +60 -0
- package/dist/object-store.d.ts.map +1 -0
- package/dist/relay.d.ts +268 -0
- package/dist/relay.d.ts.map +1 -0
- package/dist/server.d.ts +46 -0
- package/dist/server.d.ts.map +1 -0
- package/dist/smrt-knowledge.json +73 -0
- package/dist/smrt-knowledge.okf.md +56 -0
- package/package.json +54 -3
|
@@ -0,0 +1,2358 @@
|
|
|
1
|
+
import { createHash, createPrivateKey, createPublicKey, randomBytes, randomUUID, sign, timingSafeEqual, verify } from "node:crypto";
|
|
2
|
+
import { createServer } from "node:http";
|
|
3
|
+
import { Readable } from "node:stream";
|
|
4
|
+
import { mkdir, readFile, readdir, rename, rm, rmdir, writeFile } from "node:fs/promises";
|
|
5
|
+
import { dirname, join, relative, resolve, sep } from "node:path";
|
|
6
|
+
import { FEATURE_BACKUP_VAULT, FeatureResolver, FeatureUnavailableError, requireFeature } from "@happyvertical/smrt-features";
|
|
7
|
+
import { SubscriptionFeatureSource } from "@happyvertical/smrt-subscriptions";
|
|
8
|
+
import { getDatabase } from "@happyvertical/sql";
|
|
9
|
+
//#region src/encoding.ts
|
|
10
|
+
var IDENTIFIER = /^[A-Za-z0-9][A-Za-z0-9_-]{0,127}$/;
|
|
11
|
+
var NONCE = /^[A-Za-z0-9_-]{16,64}$/;
|
|
12
|
+
var SHA256_HEX = /^[0-9a-f]{64}$/;
|
|
13
|
+
var BASE64URL = /^[A-Za-z0-9_-]+$/;
|
|
14
|
+
var UPLOAD_ID = /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/;
|
|
15
|
+
var EMPTY_SHA256 = createHash("sha256").digest("hex");
|
|
16
|
+
function isIdentifier(value) {
|
|
17
|
+
return typeof value === "string" && IDENTIFIER.test(value);
|
|
18
|
+
}
|
|
19
|
+
function isNonce(value) {
|
|
20
|
+
return typeof value === "string" && NONCE.test(value);
|
|
21
|
+
}
|
|
22
|
+
function isSha256Hex(value) {
|
|
23
|
+
return typeof value === "string" && SHA256_HEX.test(value);
|
|
24
|
+
}
|
|
25
|
+
function isUploadId(value) {
|
|
26
|
+
return typeof value === "string" && UPLOAD_ID.test(value);
|
|
27
|
+
}
|
|
28
|
+
function sha256Hex(data) {
|
|
29
|
+
return createHash("sha256").update(data).digest("hex");
|
|
30
|
+
}
|
|
31
|
+
function accountHandle(accountId) {
|
|
32
|
+
return sha256Hex(`smrt-vault-relay:account:${accountId}`);
|
|
33
|
+
}
|
|
34
|
+
function encodeBase64Url(bytes) {
|
|
35
|
+
return Buffer.from(bytes).toString("base64url");
|
|
36
|
+
}
|
|
37
|
+
function decodeBase64Url(value) {
|
|
38
|
+
if (!BASE64URL.test(value)) return null;
|
|
39
|
+
const bytes = Buffer.from(value, "base64url");
|
|
40
|
+
if (bytes.toString("base64url") !== value) return null;
|
|
41
|
+
return new Uint8Array(bytes);
|
|
42
|
+
}
|
|
43
|
+
function parseCanonicalInteger(value) {
|
|
44
|
+
if (!/^(0|[1-9][0-9]{0,15})$/.test(value)) return null;
|
|
45
|
+
const parsed = Number(value);
|
|
46
|
+
return Number.isSafeInteger(parsed) ? parsed : null;
|
|
47
|
+
}
|
|
48
|
+
function toSafeInteger(value, field) {
|
|
49
|
+
const parsed = typeof value === "number" ? value : typeof value === "bigint" ? Number(value) : typeof value === "string" ? Number(value) : NaN;
|
|
50
|
+
if (!Number.isSafeInteger(parsed)) throw new Error(`vault relay: ${field} is not a safe integer`);
|
|
51
|
+
return parsed;
|
|
52
|
+
}
|
|
53
|
+
//#endregion
|
|
54
|
+
//#region src/auth.ts
|
|
55
|
+
var SIGNATURE_PROTOCOL = "smrt-vault-relay-v1";
|
|
56
|
+
var HEADER_ACCOUNT = "x-vault-account";
|
|
57
|
+
var HEADER_DEVICE = "x-vault-device";
|
|
58
|
+
var HEADER_TIMESTAMP = "x-vault-timestamp";
|
|
59
|
+
var HEADER_NONCE = "x-vault-nonce";
|
|
60
|
+
var HEADER_CONTENT_SHA256 = "x-vault-content-sha256";
|
|
61
|
+
var HEADER_SIGNATURE = "x-vault-signature";
|
|
62
|
+
var ED25519_SPKI_PREFIX = Buffer.from("302a300506032b6570032100", "hex");
|
|
63
|
+
var ED25519_PKCS8_PREFIX = Buffer.from("302e020100300506032b657004220420", "hex");
|
|
64
|
+
function canonicalRequest(fields) {
|
|
65
|
+
return Buffer.from([
|
|
66
|
+
SIGNATURE_PROTOCOL,
|
|
67
|
+
fields.method.toUpperCase(),
|
|
68
|
+
fields.pathAndQuery,
|
|
69
|
+
fields.accountId,
|
|
70
|
+
fields.deviceId,
|
|
71
|
+
String(fields.timestamp),
|
|
72
|
+
fields.nonce,
|
|
73
|
+
fields.contentSha256
|
|
74
|
+
].join("\n"), "utf8");
|
|
75
|
+
}
|
|
76
|
+
function importDevicePublicKey(encoded) {
|
|
77
|
+
const raw = decodeBase64Url(encoded);
|
|
78
|
+
if (raw?.length !== 32) return null;
|
|
79
|
+
try {
|
|
80
|
+
return createPublicKey({
|
|
81
|
+
key: Buffer.concat([ED25519_SPKI_PREFIX, raw]),
|
|
82
|
+
format: "der",
|
|
83
|
+
type: "spki"
|
|
84
|
+
});
|
|
85
|
+
} catch {
|
|
86
|
+
return null;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
function exportDevicePublicKey(key) {
|
|
90
|
+
const der = key.export({
|
|
91
|
+
format: "der",
|
|
92
|
+
type: "spki"
|
|
93
|
+
});
|
|
94
|
+
return encodeBase64Url(der.subarray(der.length - 32));
|
|
95
|
+
}
|
|
96
|
+
function importDevicePrivateKey(seed) {
|
|
97
|
+
if (seed.length !== 32) throw new Error("vault relay: an Ed25519 seed is 32 bytes");
|
|
98
|
+
return createPrivateKey({
|
|
99
|
+
key: Buffer.concat([ED25519_PKCS8_PREFIX, Buffer.from(seed)]),
|
|
100
|
+
format: "der",
|
|
101
|
+
type: "pkcs8"
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
function signRelayRequest(fields, privateKey) {
|
|
105
|
+
const signature = sign(null, canonicalRequest(fields), privateKey);
|
|
106
|
+
return {
|
|
107
|
+
[HEADER_ACCOUNT]: fields.accountId,
|
|
108
|
+
[HEADER_DEVICE]: fields.deviceId,
|
|
109
|
+
[HEADER_TIMESTAMP]: String(fields.timestamp),
|
|
110
|
+
[HEADER_NONCE]: fields.nonce,
|
|
111
|
+
[HEADER_CONTENT_SHA256]: fields.contentSha256,
|
|
112
|
+
[HEADER_SIGNATURE]: encodeBase64Url(signature)
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
function parseSignatureHeaders(method, pathAndQuery, headers) {
|
|
116
|
+
const accountId = headers.get(HEADER_ACCOUNT);
|
|
117
|
+
const deviceId = headers.get(HEADER_DEVICE);
|
|
118
|
+
const timestampText = headers.get(HEADER_TIMESTAMP);
|
|
119
|
+
const nonce = headers.get(HEADER_NONCE);
|
|
120
|
+
const contentSha256 = headers.get(HEADER_CONTENT_SHA256);
|
|
121
|
+
const signatureText = headers.get(HEADER_SIGNATURE);
|
|
122
|
+
if (!isIdentifier(accountId) || !isIdentifier(deviceId) || !isNonce(nonce) || !isSha256Hex(contentSha256) || timestampText === null || signatureText === null || signatureText.length > 128) return null;
|
|
123
|
+
const timestamp = parseCanonicalInteger(timestampText);
|
|
124
|
+
const signature = decodeBase64Url(signatureText);
|
|
125
|
+
if (timestamp === null || !signature || signature.length !== 64) return null;
|
|
126
|
+
return {
|
|
127
|
+
fields: {
|
|
128
|
+
method,
|
|
129
|
+
pathAndQuery,
|
|
130
|
+
accountId,
|
|
131
|
+
deviceId,
|
|
132
|
+
timestamp,
|
|
133
|
+
nonce,
|
|
134
|
+
contentSha256
|
|
135
|
+
},
|
|
136
|
+
signature
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
function verifySignature(parsed, publicKey) {
|
|
140
|
+
try {
|
|
141
|
+
return verify(null, canonicalRequest(parsed.fields), publicKey, parsed.signature);
|
|
142
|
+
} catch {
|
|
143
|
+
return false;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
//#endregion
|
|
147
|
+
//#region src/config.ts
|
|
148
|
+
var MAX_CHUNK_BYTES_CEILING = 64 * 1024 * 1024;
|
|
149
|
+
var DEFAULT_MAX_CHUNK_BYTES = 8454144;
|
|
150
|
+
var DEFAULT_UPLOAD_TTL_MS = 1440 * 60 * 1e3;
|
|
151
|
+
var DEFAULT_MAX_CLOCK_SKEW_MS = 300 * 1e3;
|
|
152
|
+
var DEFAULT_MAX_SEQUENCE_JUMP = 1e3;
|
|
153
|
+
var DEFAULT_LEASE_TTL_MS = 300 * 1e3;
|
|
154
|
+
var DEFAULT_LAPSE_RETENTION_DAYS = 90;
|
|
155
|
+
var MAX_LAPSE_RETENTION_DAYS = 3650;
|
|
156
|
+
var DAY_MS$1 = 1440 * 60 * 1e3;
|
|
157
|
+
var MAX_JSON_BODY_BYTES = 4096;
|
|
158
|
+
function positiveInteger(value, name) {
|
|
159
|
+
if (!Number.isSafeInteger(value) || value < 1) throw new Error(`vault relay: ${name} must be a positive integer`);
|
|
160
|
+
return value;
|
|
161
|
+
}
|
|
162
|
+
function nonNegativeInteger(value, name) {
|
|
163
|
+
if (!Number.isSafeInteger(value) || value < 0) throw new Error(`vault relay: ${name} must be a non-negative integer`);
|
|
164
|
+
return value;
|
|
165
|
+
}
|
|
166
|
+
function lapseRetention(days) {
|
|
167
|
+
const lapseRetentionDays = positiveInteger(days ?? 90, "retention.lapseRetentionDays");
|
|
168
|
+
if (lapseRetentionDays > 3650) throw new Error(`vault relay: retention.lapseRetentionDays may not exceed ${MAX_LAPSE_RETENTION_DAYS}`);
|
|
169
|
+
return {
|
|
170
|
+
lapseRetentionDays,
|
|
171
|
+
lapseRetentionMs: lapseRetentionDays * DAY_MS$1
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
function resolvePolicy(input) {
|
|
175
|
+
const quota = {
|
|
176
|
+
maxBytes: positiveInteger(input.quota.maxBytes, "quota.maxBytes"),
|
|
177
|
+
maxSnapshots: positiveInteger(input.quota.maxSnapshots, "quota.maxSnapshots")
|
|
178
|
+
};
|
|
179
|
+
const retention = {
|
|
180
|
+
keepLast: positiveInteger(input.retention.keepLast, "retention.keepLast"),
|
|
181
|
+
keepWithinMs: nonNegativeInteger(input.retention.keepWithinMs, "retention.keepWithinMs"),
|
|
182
|
+
...lapseRetention(input.retention.lapseRetentionDays)
|
|
183
|
+
};
|
|
184
|
+
if (retention.keepLast >= quota.maxSnapshots) throw new Error("vault relay: retention.keepLast must be below quota.maxSnapshots so a new upload fits");
|
|
185
|
+
const limits = {
|
|
186
|
+
maxChunkBytes: positiveInteger(input.limits?.maxChunkBytes ?? 8454144, "limits.maxChunkBytes"),
|
|
187
|
+
uploadTtlMs: positiveInteger(input.limits?.uploadTtlMs ?? 864e5, "limits.uploadTtlMs"),
|
|
188
|
+
maxClockSkewMs: positiveInteger(input.limits?.maxClockSkewMs ?? 3e5, "limits.maxClockSkewMs"),
|
|
189
|
+
maxSequenceJump: positiveInteger(input.limits?.maxSequenceJump ?? 1e3, "limits.maxSequenceJump"),
|
|
190
|
+
leaseTtlMs: positiveInteger(input.limits?.leaseTtlMs ?? 3e5, "limits.leaseTtlMs")
|
|
191
|
+
};
|
|
192
|
+
if (limits.leaseTtlMs < 3e4 || limits.leaseTtlMs > 36e5) throw new Error("vault relay: limits.leaseTtlMs must be between 30 seconds and 1 hour");
|
|
193
|
+
if (limits.maxChunkBytes > 67108864) throw new Error(`vault relay: limits.maxChunkBytes may not exceed ${MAX_CHUNK_BYTES_CEILING}`);
|
|
194
|
+
return {
|
|
195
|
+
quota,
|
|
196
|
+
retention,
|
|
197
|
+
limits
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
//#endregion
|
|
201
|
+
//#region src/lease.ts
|
|
202
|
+
var MIN_LEASE_TTL_MS = 3e4;
|
|
203
|
+
var LeaseService = class {
|
|
204
|
+
store;
|
|
205
|
+
ttlMs;
|
|
206
|
+
constructor(store, ttlMs) {
|
|
207
|
+
this.store = store;
|
|
208
|
+
this.ttlMs = ttlMs;
|
|
209
|
+
}
|
|
210
|
+
/** The granted lifetime: the policy TTL, or a shorter one the client asks. */
|
|
211
|
+
lifetime(requestedMs) {
|
|
212
|
+
if (requestedMs === void 0) return this.ttlMs;
|
|
213
|
+
return Math.min(this.ttlMs, Math.max(MIN_LEASE_TTL_MS, requestedMs));
|
|
214
|
+
}
|
|
215
|
+
async status(accountId, now) {
|
|
216
|
+
await this.store.ensureLease(accountId);
|
|
217
|
+
const lease = await this.store.getLease(accountId);
|
|
218
|
+
const latest = await this.store.latestCommittedSequence(accountId);
|
|
219
|
+
const head = lease?.headSequence ?? latest;
|
|
220
|
+
const headStatus = head === 0 ? "committed" : await this.store.snapshotStatusAt(accountId, head);
|
|
221
|
+
const live = lease?.holderDeviceId !== null && lease?.holderDeviceId !== void 0 && lease.expiresAt > now;
|
|
222
|
+
return {
|
|
223
|
+
holder: live ? lease?.holderDeviceId ?? null : null,
|
|
224
|
+
expiresAt: live ? lease?.expiresAt ?? null : null,
|
|
225
|
+
headSequence: head,
|
|
226
|
+
latestCommittedSequence: latest,
|
|
227
|
+
headIsHole: headStatus === null
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
/** Explain a refused acquire or publish from the current row. */
|
|
231
|
+
async refusal(accountId, deviceId, now) {
|
|
232
|
+
const lease = await this.store.getLease(accountId);
|
|
233
|
+
const latest = await this.store.latestCommittedSequence(accountId);
|
|
234
|
+
const head = lease?.headSequence ?? latest;
|
|
235
|
+
if (lease?.holderDeviceId && lease.holderDeviceId !== deviceId && lease.expiresAt > now) return {
|
|
236
|
+
error: "lease_held",
|
|
237
|
+
holder: lease.holderDeviceId,
|
|
238
|
+
expiresAt: lease.expiresAt
|
|
239
|
+
};
|
|
240
|
+
if (await this.store.snapshotStatusAt(accountId, head) === "pending") return {
|
|
241
|
+
error: "publish_in_progress",
|
|
242
|
+
headSequence: head
|
|
243
|
+
};
|
|
244
|
+
return {
|
|
245
|
+
error: "must_restore_first",
|
|
246
|
+
headSequence: head,
|
|
247
|
+
latestSequence: latest
|
|
248
|
+
};
|
|
249
|
+
}
|
|
250
|
+
async acquire(accountId, deviceId, baseSequence, requestedTtlMs, now) {
|
|
251
|
+
await this.store.ensureLease(accountId);
|
|
252
|
+
const grant = await this.store.acquire(accountId, deviceId, baseSequence, now, now + this.lifetime(requestedTtlMs));
|
|
253
|
+
if (grant) return {
|
|
254
|
+
ok: true,
|
|
255
|
+
grant
|
|
256
|
+
};
|
|
257
|
+
return {
|
|
258
|
+
ok: false,
|
|
259
|
+
refusal: await this.refusal(accountId, deviceId, now)
|
|
260
|
+
};
|
|
261
|
+
}
|
|
262
|
+
async renew(accountId, deviceId, fencingToken, requestedTtlMs, now) {
|
|
263
|
+
const grant = await this.store.renew(accountId, deviceId, fencingToken, now, now + this.lifetime(requestedTtlMs));
|
|
264
|
+
if (grant) return {
|
|
265
|
+
ok: true,
|
|
266
|
+
grant
|
|
267
|
+
};
|
|
268
|
+
return {
|
|
269
|
+
ok: false,
|
|
270
|
+
refusal: { error: "lease_lost" }
|
|
271
|
+
};
|
|
272
|
+
}
|
|
273
|
+
async release(accountId, deviceId, fencingToken) {
|
|
274
|
+
await this.store.release(accountId, deviceId, fencingToken);
|
|
275
|
+
}
|
|
276
|
+
/**
|
|
277
|
+
* Begin-time admission (advisory; the commit-time head advance enforces).
|
|
278
|
+
* `null` admits the upload.
|
|
279
|
+
*/
|
|
280
|
+
async admitUpload(accountId, deviceId, fencingToken, sequence, now) {
|
|
281
|
+
await this.store.ensureLease(accountId);
|
|
282
|
+
if (await this.store.holds(accountId, deviceId, fencingToken, sequence, now)) return null;
|
|
283
|
+
return this.mustRestore(accountId);
|
|
284
|
+
}
|
|
285
|
+
/** The `must_restore_first` answer for a publish that lost its lease. */
|
|
286
|
+
async mustRestore(accountId) {
|
|
287
|
+
const lease = await this.store.getLease(accountId);
|
|
288
|
+
const latest = await this.store.latestCommittedSequence(accountId);
|
|
289
|
+
return {
|
|
290
|
+
error: "must_restore_first",
|
|
291
|
+
headSequence: lease?.headSequence ?? latest,
|
|
292
|
+
latestSequence: latest
|
|
293
|
+
};
|
|
294
|
+
}
|
|
295
|
+
};
|
|
296
|
+
//#endregion
|
|
297
|
+
//#region src/lease-store.ts
|
|
298
|
+
var VAULT_RELAY_LEASE_TABLES = ["vault_relay_leases", "vault_relay_upload_fences"];
|
|
299
|
+
var SCHEMA_LEASES_V1 = [`CREATE TABLE IF NOT EXISTS vault_relay_leases (
|
|
300
|
+
account_id TEXT PRIMARY KEY,
|
|
301
|
+
holder_device_id TEXT,
|
|
302
|
+
fencing_token BIGINT NOT NULL DEFAULT 0,
|
|
303
|
+
head_sequence BIGINT NOT NULL DEFAULT 0,
|
|
304
|
+
expires_at BIGINT NOT NULL DEFAULT 0,
|
|
305
|
+
acquired_at BIGINT
|
|
306
|
+
)`, `CREATE TABLE IF NOT EXISTS vault_relay_upload_fences (
|
|
307
|
+
upload_id TEXT PRIMARY KEY,
|
|
308
|
+
account_id TEXT NOT NULL,
|
|
309
|
+
fencing_token BIGINT NOT NULL
|
|
310
|
+
)`];
|
|
311
|
+
async function migrateVaultRelayLeasesV1(db) {
|
|
312
|
+
for (const statement of SCHEMA_LEASES_V1) await db.query(statement);
|
|
313
|
+
}
|
|
314
|
+
function leaseFromRow(row) {
|
|
315
|
+
return {
|
|
316
|
+
accountId: String(row.account_id),
|
|
317
|
+
holderDeviceId: row.holder_device_id === null || row.holder_device_id === void 0 ? null : String(row.holder_device_id),
|
|
318
|
+
fencingToken: toSafeInteger(row.fencing_token, "fencing_token"),
|
|
319
|
+
headSequence: toSafeInteger(row.head_sequence, "head_sequence"),
|
|
320
|
+
expiresAt: toSafeInteger(row.expires_at, "expires_at")
|
|
321
|
+
};
|
|
322
|
+
}
|
|
323
|
+
var HEAD_ROW = `SELECT 1 FROM vault_relay_snapshots s
|
|
324
|
+
WHERE s.account_id = vault_relay_leases.account_id
|
|
325
|
+
AND s.sequence = vault_relay_leases.head_sequence`;
|
|
326
|
+
var LATEST_COMMITTED = `SELECT COALESCE(MAX(c.sequence), 0)
|
|
327
|
+
FROM vault_relay_snapshots c
|
|
328
|
+
WHERE c.account_id = vault_relay_leases.account_id
|
|
329
|
+
AND c.status = 'committed'`;
|
|
330
|
+
var VaultRelayLeaseStore = class {
|
|
331
|
+
db;
|
|
332
|
+
constructor(db) {
|
|
333
|
+
this.db = db;
|
|
334
|
+
}
|
|
335
|
+
/**
|
|
336
|
+
* Create the account's lease row if missing, with no holder and the head at
|
|
337
|
+
* the account's published high-water mark (publishing always needs the
|
|
338
|
+
* lease, so no upload can be in flight before the row exists). When the
|
|
339
|
+
* newest published snapshots were deleted before the lease existed, the
|
|
340
|
+
* head is then a hole and acquire accepts the latest committed base.
|
|
341
|
+
*/
|
|
342
|
+
async ensureLease(accountId) {
|
|
343
|
+
await this.db.query(`INSERT INTO vault_relay_leases
|
|
344
|
+
(account_id, holder_device_id, fencing_token, head_sequence, expires_at, acquired_at)
|
|
345
|
+
SELECT ?, NULL, 0,
|
|
346
|
+
COALESCE((SELECT high_water_sequence FROM vault_relay_accounts
|
|
347
|
+
WHERE account_id = ?), 0),
|
|
348
|
+
0, NULL
|
|
349
|
+
WHERE 1 = 1
|
|
350
|
+
ON CONFLICT (account_id) DO NOTHING`, accountId, accountId);
|
|
351
|
+
}
|
|
352
|
+
async getLease(accountId) {
|
|
353
|
+
const found = await this.db.query(`SELECT account_id, holder_device_id, fencing_token, head_sequence, expires_at
|
|
354
|
+
FROM vault_relay_leases WHERE account_id = ?`, accountId);
|
|
355
|
+
return found.rows[0] ? leaseFromRow(found.rows[0]) : null;
|
|
356
|
+
}
|
|
357
|
+
/** Latest committed sequence of the account (0 when none). */
|
|
358
|
+
async latestCommittedSequence(accountId) {
|
|
359
|
+
return toSafeInteger((await this.db.query(`SELECT COALESCE(MAX(sequence), 0) AS latest FROM vault_relay_snapshots
|
|
360
|
+
WHERE account_id = ? AND status = 'committed'`, accountId)).rows[0]?.latest ?? 0, "latest");
|
|
361
|
+
}
|
|
362
|
+
/** Status of the snapshot row at `sequence`, or `null` when none exists. */
|
|
363
|
+
async snapshotStatusAt(accountId, sequence) {
|
|
364
|
+
const status = (await this.db.query("SELECT status FROM vault_relay_snapshots WHERE account_id = ? AND sequence = ?", accountId, sequence)).rows[0]?.status;
|
|
365
|
+
if (status === void 0) return null;
|
|
366
|
+
return status === "committed" ? "committed" : "pending";
|
|
367
|
+
}
|
|
368
|
+
/**
|
|
369
|
+
* Grant the lease to `deviceId`, in one statement, when it is free, expired
|
|
370
|
+
* or already this device's, and the device's `baseSequence` is the head:
|
|
371
|
+
* either the head itself with no publish in flight, or, for a hole, the
|
|
372
|
+
* latest committed sequence. Every grant increments the fencing token, so a
|
|
373
|
+
* re-acquire also fences an earlier process on the same device.
|
|
374
|
+
*/
|
|
375
|
+
async acquire(accountId, deviceId, baseSequence, now, expiresAt) {
|
|
376
|
+
const row = (await this.db.query(`UPDATE vault_relay_leases
|
|
377
|
+
SET holder_device_id = ?, fencing_token = fencing_token + 1,
|
|
378
|
+
expires_at = ?, acquired_at = ?
|
|
379
|
+
WHERE account_id = ?
|
|
380
|
+
AND (holder_device_id IS NULL OR holder_device_id = ? OR expires_at <= ?)
|
|
381
|
+
AND EXISTS (SELECT 1 FROM vault_relay_devices d
|
|
382
|
+
WHERE d.account_id = vault_relay_leases.account_id
|
|
383
|
+
AND d.device_id = ? AND d.status = 'active')
|
|
384
|
+
AND (
|
|
385
|
+
(head_sequence = ?
|
|
386
|
+
AND NOT EXISTS (${HEAD_ROW} AND s.status = 'pending'))
|
|
387
|
+
OR
|
|
388
|
+
(NOT EXISTS (${HEAD_ROW})
|
|
389
|
+
AND ? = (${LATEST_COMMITTED}))
|
|
390
|
+
)
|
|
391
|
+
RETURNING fencing_token, expires_at, head_sequence`, deviceId, expiresAt, now, accountId, deviceId, now, deviceId, baseSequence, baseSequence)).rows[0];
|
|
392
|
+
if (!row) return null;
|
|
393
|
+
return {
|
|
394
|
+
fencingToken: toSafeInteger(row.fencing_token, "fencing_token"),
|
|
395
|
+
expiresAt: toSafeInteger(row.expires_at, "expires_at"),
|
|
396
|
+
headSequence: toSafeInteger(row.head_sequence, "head_sequence")
|
|
397
|
+
};
|
|
398
|
+
}
|
|
399
|
+
/** Extend an unexpired lease held by this device with this exact token. */
|
|
400
|
+
async renew(accountId, deviceId, fencingToken, now, expiresAt) {
|
|
401
|
+
const row = (await this.db.query(`UPDATE vault_relay_leases SET expires_at = ?
|
|
402
|
+
WHERE account_id = ? AND holder_device_id = ? AND fencing_token = ?
|
|
403
|
+
AND expires_at > ?
|
|
404
|
+
RETURNING fencing_token, expires_at, head_sequence`, expiresAt, accountId, deviceId, fencingToken, now)).rows[0];
|
|
405
|
+
if (!row) return null;
|
|
406
|
+
return {
|
|
407
|
+
fencingToken: toSafeInteger(row.fencing_token, "fencing_token"),
|
|
408
|
+
expiresAt: toSafeInteger(row.expires_at, "expires_at"),
|
|
409
|
+
headSequence: toSafeInteger(row.head_sequence, "head_sequence")
|
|
410
|
+
};
|
|
411
|
+
}
|
|
412
|
+
/** Give up the lease; only the holder with the current token can. */
|
|
413
|
+
async release(accountId, deviceId, fencingToken) {
|
|
414
|
+
return (await this.db.query(`UPDATE vault_relay_leases SET holder_device_id = NULL, expires_at = 0
|
|
415
|
+
WHERE account_id = ? AND holder_device_id = ? AND fencing_token = ?
|
|
416
|
+
RETURNING account_id`, accountId, deviceId, fencingToken)).rows.length === 1;
|
|
417
|
+
}
|
|
418
|
+
/**
|
|
419
|
+
* Clear a lease held by a revoked device and bump the token, so nothing
|
|
420
|
+
* recorded under the old token can publish. Run after the revocation.
|
|
421
|
+
*/
|
|
422
|
+
async clearHolder(accountId, deviceId) {
|
|
423
|
+
await this.db.query(`UPDATE vault_relay_leases
|
|
424
|
+
SET holder_device_id = NULL, expires_at = 0,
|
|
425
|
+
fencing_token = fencing_token + 1
|
|
426
|
+
WHERE account_id = ? AND holder_device_id = ?`, accountId, deviceId);
|
|
427
|
+
}
|
|
428
|
+
/**
|
|
429
|
+
* Advisory begin-time check: this device holds an unexpired lease with this
|
|
430
|
+
* token and `sequence` is above the head. Begin is several statements, so
|
|
431
|
+
* only {@link advanceHead} at commit enforces the lease.
|
|
432
|
+
*/
|
|
433
|
+
async holds(accountId, deviceId, fencingToken, sequence, now) {
|
|
434
|
+
return (await this.db.query(`SELECT 1 AS ok FROM vault_relay_leases
|
|
435
|
+
WHERE account_id = ? AND holder_device_id = ? AND fencing_token = ?
|
|
436
|
+
AND expires_at > ? AND head_sequence < ?`, accountId, deviceId, fencingToken, now, sequence)).rows.length === 1;
|
|
437
|
+
}
|
|
438
|
+
/**
|
|
439
|
+
* Move the head to `sequence` for a pending upload, in one statement: only
|
|
440
|
+
* the holder with the token recorded at begin, only while the upload is
|
|
441
|
+
* still pending, and only forward under an unexpired lease. A head already
|
|
442
|
+
* at `sequence` (a commit retried after the relay failed between the head
|
|
443
|
+
* advance and the publish) is accepted without the expiry check: nobody
|
|
444
|
+
* else can have acquired while that upload is pending at the head.
|
|
445
|
+
*/
|
|
446
|
+
async advanceHead(accountId, deviceId, uploadId, fencingToken, sequence, now) {
|
|
447
|
+
return (await this.db.query(`UPDATE vault_relay_leases SET head_sequence = ?
|
|
448
|
+
WHERE account_id = ? AND holder_device_id = ? AND fencing_token = ?
|
|
449
|
+
AND EXISTS (SELECT 1 FROM vault_relay_snapshots
|
|
450
|
+
WHERE account_id = ? AND upload_id = ?
|
|
451
|
+
AND sequence = ? AND status = 'pending')
|
|
452
|
+
AND ((expires_at > ? AND head_sequence < ?) OR head_sequence = ?)
|
|
453
|
+
RETURNING head_sequence`, sequence, accountId, deviceId, fencingToken, accountId, uploadId, sequence, now, sequence, sequence)).rows.length === 1;
|
|
454
|
+
}
|
|
455
|
+
async recordFence(accountId, uploadId, fencingToken) {
|
|
456
|
+
await this.db.query(`INSERT INTO vault_relay_upload_fences (upload_id, account_id, fencing_token)
|
|
457
|
+
VALUES (?, ?, ?)`, uploadId, accountId, fencingToken);
|
|
458
|
+
}
|
|
459
|
+
async getFence(accountId, uploadId) {
|
|
460
|
+
const row = (await this.db.query(`SELECT fencing_token FROM vault_relay_upload_fences
|
|
461
|
+
WHERE account_id = ? AND upload_id = ?`, accountId, uploadId)).rows[0];
|
|
462
|
+
return row ? toSafeInteger(row.fencing_token, "fencing_token") : null;
|
|
463
|
+
}
|
|
464
|
+
async deleteFence(uploadId) {
|
|
465
|
+
await this.db.query("DELETE FROM vault_relay_upload_fences WHERE upload_id = ?", uploadId);
|
|
466
|
+
}
|
|
467
|
+
/**
|
|
468
|
+
* Operator reset of the account's high-water mark (the one way the head
|
|
469
|
+
* moves back): the head becomes the latest committed sequence that remains
|
|
470
|
+
* and the holder is cleared with a bumped token, so nothing recorded under
|
|
471
|
+
* the old position can publish and the next acquire starts from the
|
|
472
|
+
* restored head.
|
|
473
|
+
*/
|
|
474
|
+
async resetHead(accountId) {
|
|
475
|
+
await this.db.query(`UPDATE vault_relay_leases
|
|
476
|
+
SET head_sequence = COALESCE((SELECT MAX(sequence)
|
|
477
|
+
FROM vault_relay_snapshots
|
|
478
|
+
WHERE account_id = ?
|
|
479
|
+
AND status = 'committed'), 0),
|
|
480
|
+
holder_device_id = NULL, expires_at = 0,
|
|
481
|
+
fencing_token = fencing_token + 1
|
|
482
|
+
WHERE account_id = ?`, accountId, accountId);
|
|
483
|
+
}
|
|
484
|
+
/**
|
|
485
|
+
* Delete a committed snapshot only when it is below the head and below the
|
|
486
|
+
* latest committed snapshot, in one statement. The head never moves back,
|
|
487
|
+
* so a stale read can only refuse more. Returns the row when removed.
|
|
488
|
+
*/
|
|
489
|
+
async deleteCommittedBelowHead(accountId, uploadId) {
|
|
490
|
+
const row = (await this.db.query(`DELETE FROM vault_relay_snapshots
|
|
491
|
+
WHERE account_id = ? AND upload_id = ? AND status = 'committed'
|
|
492
|
+
AND sequence < (SELECT head_sequence FROM vault_relay_leases
|
|
493
|
+
WHERE account_id = ?)
|
|
494
|
+
AND sequence < (SELECT MAX(sequence) FROM vault_relay_snapshots
|
|
495
|
+
WHERE account_id = ? AND status = 'committed')
|
|
496
|
+
RETURNING account_id, sequence, upload_id, device_id, status, byte_size,
|
|
497
|
+
chunk_size, chunk_count, sha256, created_at, committed_at`, accountId, uploadId, accountId, accountId)).rows[0];
|
|
498
|
+
if (!row) return null;
|
|
499
|
+
return {
|
|
500
|
+
accountId: String(row.account_id),
|
|
501
|
+
sequence: toSafeInteger(row.sequence, "sequence"),
|
|
502
|
+
uploadId: String(row.upload_id),
|
|
503
|
+
deviceId: String(row.device_id),
|
|
504
|
+
status: "committed",
|
|
505
|
+
byteSize: toSafeInteger(row.byte_size, "byte_size"),
|
|
506
|
+
chunkSize: toSafeInteger(row.chunk_size, "chunk_size"),
|
|
507
|
+
chunkCount: toSafeInteger(row.chunk_count, "chunk_count"),
|
|
508
|
+
sha256: String(row.sha256),
|
|
509
|
+
createdAt: toSafeInteger(row.created_at, "created_at"),
|
|
510
|
+
committedAt: row.committed_at === null || row.committed_at === void 0 ? null : toSafeInteger(row.committed_at, "committed_at")
|
|
511
|
+
};
|
|
512
|
+
}
|
|
513
|
+
};
|
|
514
|
+
//#endregion
|
|
515
|
+
//#region src/metadata-store.ts
|
|
516
|
+
var VAULT_RELAY_TABLES = [
|
|
517
|
+
"vault_relay_accounts",
|
|
518
|
+
"vault_relay_devices",
|
|
519
|
+
"vault_relay_nonces",
|
|
520
|
+
"vault_relay_snapshots",
|
|
521
|
+
"vault_relay_chunks",
|
|
522
|
+
"vault_relay_supersessions",
|
|
523
|
+
"vault_relay_lapses",
|
|
524
|
+
"vault_relay_audit"
|
|
525
|
+
];
|
|
526
|
+
var SCHEMA_V1 = [
|
|
527
|
+
`CREATE TABLE IF NOT EXISTS vault_relay_accounts (
|
|
528
|
+
account_id TEXT PRIMARY KEY,
|
|
529
|
+
high_water_sequence BIGINT NOT NULL DEFAULT 0,
|
|
530
|
+
used_bytes BIGINT NOT NULL DEFAULT 0,
|
|
531
|
+
used_snapshots BIGINT NOT NULL DEFAULT 0,
|
|
532
|
+
created_at BIGINT NOT NULL
|
|
533
|
+
)`,
|
|
534
|
+
`CREATE TABLE IF NOT EXISTS vault_relay_devices (
|
|
535
|
+
account_id TEXT NOT NULL,
|
|
536
|
+
device_id TEXT NOT NULL,
|
|
537
|
+
public_key TEXT NOT NULL,
|
|
538
|
+
status TEXT NOT NULL CHECK (status IN ('active', 'revoked')),
|
|
539
|
+
registered_at BIGINT NOT NULL,
|
|
540
|
+
revoked_at BIGINT,
|
|
541
|
+
PRIMARY KEY (account_id, device_id)
|
|
542
|
+
)`,
|
|
543
|
+
`CREATE TABLE IF NOT EXISTS vault_relay_nonces (
|
|
544
|
+
account_id TEXT NOT NULL,
|
|
545
|
+
device_id TEXT NOT NULL,
|
|
546
|
+
nonce TEXT NOT NULL,
|
|
547
|
+
expires_at BIGINT NOT NULL,
|
|
548
|
+
PRIMARY KEY (account_id, device_id, nonce)
|
|
549
|
+
)`,
|
|
550
|
+
`CREATE INDEX IF NOT EXISTS vault_relay_nonces_expiry
|
|
551
|
+
ON vault_relay_nonces (expires_at)`,
|
|
552
|
+
`CREATE TABLE IF NOT EXISTS vault_relay_snapshots (
|
|
553
|
+
account_id TEXT NOT NULL,
|
|
554
|
+
sequence BIGINT NOT NULL CHECK (sequence > 0),
|
|
555
|
+
upload_id TEXT NOT NULL UNIQUE,
|
|
556
|
+
device_id TEXT NOT NULL,
|
|
557
|
+
status TEXT NOT NULL CHECK (status IN ('pending', 'committed')),
|
|
558
|
+
byte_size BIGINT NOT NULL CHECK (byte_size > 0),
|
|
559
|
+
chunk_size BIGINT NOT NULL CHECK (chunk_size > 0),
|
|
560
|
+
chunk_count INTEGER NOT NULL CHECK (chunk_count > 0),
|
|
561
|
+
sha256 TEXT NOT NULL,
|
|
562
|
+
created_at BIGINT NOT NULL,
|
|
563
|
+
committed_at BIGINT,
|
|
564
|
+
PRIMARY KEY (account_id, sequence)
|
|
565
|
+
)`,
|
|
566
|
+
`CREATE INDEX IF NOT EXISTS vault_relay_snapshots_pending
|
|
567
|
+
ON vault_relay_snapshots (status, created_at)`,
|
|
568
|
+
`CREATE TABLE IF NOT EXISTS vault_relay_chunks (
|
|
569
|
+
upload_id TEXT NOT NULL,
|
|
570
|
+
chunk_index INTEGER NOT NULL CHECK (chunk_index >= 0),
|
|
571
|
+
byte_size BIGINT NOT NULL CHECK (byte_size > 0),
|
|
572
|
+
sha256 TEXT NOT NULL,
|
|
573
|
+
object_id TEXT NOT NULL,
|
|
574
|
+
PRIMARY KEY (upload_id, chunk_index)
|
|
575
|
+
)`,
|
|
576
|
+
`CREATE TABLE IF NOT EXISTS vault_relay_supersessions (
|
|
577
|
+
account_id TEXT PRIMARY KEY,
|
|
578
|
+
superseded_by TEXT NOT NULL,
|
|
579
|
+
superseded_at BIGINT NOT NULL
|
|
580
|
+
)`,
|
|
581
|
+
`CREATE TABLE IF NOT EXISTS vault_relay_lapses (
|
|
582
|
+
account_id TEXT PRIMARY KEY,
|
|
583
|
+
lapsed_at BIGINT NOT NULL,
|
|
584
|
+
swept_at BIGINT
|
|
585
|
+
)`,
|
|
586
|
+
`CREATE INDEX IF NOT EXISTS vault_relay_lapses_due
|
|
587
|
+
ON vault_relay_lapses (swept_at, lapsed_at)`,
|
|
588
|
+
`CREATE TABLE IF NOT EXISTS vault_relay_audit (
|
|
589
|
+
audit_id TEXT PRIMARY KEY,
|
|
590
|
+
occurred_at BIGINT NOT NULL,
|
|
591
|
+
action TEXT NOT NULL,
|
|
592
|
+
operator TEXT NOT NULL,
|
|
593
|
+
account_handle TEXT NOT NULL,
|
|
594
|
+
snapshots BIGINT NOT NULL DEFAULT 0,
|
|
595
|
+
bytes BIGINT NOT NULL DEFAULT 0,
|
|
596
|
+
detail TEXT
|
|
597
|
+
)`,
|
|
598
|
+
`CREATE INDEX IF NOT EXISTS vault_relay_audit_at
|
|
599
|
+
ON vault_relay_audit (occurred_at)`
|
|
600
|
+
];
|
|
601
|
+
async function migrateVaultRelayV1(db) {
|
|
602
|
+
for (const statement of SCHEMA_V1) await db.query(statement);
|
|
603
|
+
}
|
|
604
|
+
function lapseFromRow(row) {
|
|
605
|
+
return {
|
|
606
|
+
accountId: String(row.account_id),
|
|
607
|
+
lapsedAt: toSafeInteger(row.lapsed_at, "lapsed_at"),
|
|
608
|
+
sweptAt: row.swept_at === null || row.swept_at === void 0 ? null : toSafeInteger(row.swept_at, "swept_at")
|
|
609
|
+
};
|
|
610
|
+
}
|
|
611
|
+
function deviceFromRow(row) {
|
|
612
|
+
return {
|
|
613
|
+
accountId: String(row.account_id),
|
|
614
|
+
deviceId: String(row.device_id),
|
|
615
|
+
publicKey: String(row.public_key),
|
|
616
|
+
status: row.status === "active" ? "active" : "revoked",
|
|
617
|
+
registeredAt: toSafeInteger(row.registered_at, "registered_at"),
|
|
618
|
+
revokedAt: row.revoked_at === null || row.revoked_at === void 0 ? null : toSafeInteger(row.revoked_at, "revoked_at")
|
|
619
|
+
};
|
|
620
|
+
}
|
|
621
|
+
function snapshotFromRow(row) {
|
|
622
|
+
return {
|
|
623
|
+
accountId: String(row.account_id),
|
|
624
|
+
sequence: toSafeInteger(row.sequence, "sequence"),
|
|
625
|
+
uploadId: String(row.upload_id),
|
|
626
|
+
deviceId: String(row.device_id),
|
|
627
|
+
status: row.status === "committed" ? "committed" : "pending",
|
|
628
|
+
byteSize: toSafeInteger(row.byte_size, "byte_size"),
|
|
629
|
+
chunkSize: toSafeInteger(row.chunk_size, "chunk_size"),
|
|
630
|
+
chunkCount: toSafeInteger(row.chunk_count, "chunk_count"),
|
|
631
|
+
sha256: String(row.sha256),
|
|
632
|
+
createdAt: toSafeInteger(row.created_at, "created_at"),
|
|
633
|
+
committedAt: row.committed_at === null || row.committed_at === void 0 ? null : toSafeInteger(row.committed_at, "committed_at")
|
|
634
|
+
};
|
|
635
|
+
}
|
|
636
|
+
function chunkFromRow(row) {
|
|
637
|
+
return {
|
|
638
|
+
index: toSafeInteger(row.chunk_index, "chunk_index"),
|
|
639
|
+
byteSize: toSafeInteger(row.byte_size, "byte_size"),
|
|
640
|
+
sha256: String(row.sha256),
|
|
641
|
+
objectId: String(row.object_id)
|
|
642
|
+
};
|
|
643
|
+
}
|
|
644
|
+
var SNAPSHOT_COLUMNS = "account_id, sequence, upload_id, device_id, status, byte_size, chunk_size, chunk_count, sha256, created_at, committed_at";
|
|
645
|
+
var VaultRelayStore = class {
|
|
646
|
+
db;
|
|
647
|
+
constructor(db) {
|
|
648
|
+
this.db = db;
|
|
649
|
+
}
|
|
650
|
+
/** Names of relay tables that do not exist (empty when migrated). */
|
|
651
|
+
async missingTables() {
|
|
652
|
+
const missing = [];
|
|
653
|
+
for (const table of VAULT_RELAY_TABLES) if (!await this.db.tableExists(table)) missing.push(table);
|
|
654
|
+
return missing;
|
|
655
|
+
}
|
|
656
|
+
async ensureAccount(accountId, now) {
|
|
657
|
+
await this.db.query(`INSERT INTO vault_relay_accounts
|
|
658
|
+
(account_id, high_water_sequence, used_bytes, used_snapshots, created_at)
|
|
659
|
+
VALUES (?, 0, 0, 0, ?)
|
|
660
|
+
ON CONFLICT (account_id) DO NOTHING`, accountId, now);
|
|
661
|
+
}
|
|
662
|
+
async getUsage(accountId) {
|
|
663
|
+
const row = (await this.db.query(`SELECT account_id, high_water_sequence, used_bytes, used_snapshots
|
|
664
|
+
FROM vault_relay_accounts WHERE account_id = ?`, accountId)).rows[0];
|
|
665
|
+
if (!row) return null;
|
|
666
|
+
return {
|
|
667
|
+
accountId: String(row.account_id),
|
|
668
|
+
highWaterSequence: toSafeInteger(row.high_water_sequence, "high_water_sequence"),
|
|
669
|
+
usedBytes: toSafeInteger(row.used_bytes, "used_bytes"),
|
|
670
|
+
usedSnapshots: toSafeInteger(row.used_snapshots, "used_snapshots")
|
|
671
|
+
};
|
|
672
|
+
}
|
|
673
|
+
async registerDevice(accountId, deviceId, publicKey, now) {
|
|
674
|
+
if ((await this.db.query(`INSERT INTO vault_relay_devices
|
|
675
|
+
(account_id, device_id, public_key, status, registered_at, revoked_at)
|
|
676
|
+
VALUES (?, ?, ?, 'active', ?, NULL)
|
|
677
|
+
ON CONFLICT (account_id, device_id) DO NOTHING
|
|
678
|
+
RETURNING device_id`, accountId, deviceId, publicKey, now)).rows.length === 1) return "registered";
|
|
679
|
+
const existing = await this.getDevice(accountId, deviceId);
|
|
680
|
+
return existing?.status === "active" && existing.publicKey === publicKey ? "unchanged" : "conflict";
|
|
681
|
+
}
|
|
682
|
+
async revokeDevice(accountId, deviceId, now) {
|
|
683
|
+
return (await this.db.query(`UPDATE vault_relay_devices SET status = 'revoked', revoked_at = ?
|
|
684
|
+
WHERE account_id = ? AND device_id = ? AND status = 'active'
|
|
685
|
+
RETURNING device_id`, now, accountId, deviceId)).rows.length === 1;
|
|
686
|
+
}
|
|
687
|
+
async getDevice(accountId, deviceId) {
|
|
688
|
+
const found = await this.db.query(`SELECT account_id, device_id, public_key, status, registered_at, revoked_at
|
|
689
|
+
FROM vault_relay_devices WHERE account_id = ? AND device_id = ?`, accountId, deviceId);
|
|
690
|
+
return found.rows[0] ? deviceFromRow(found.rows[0]) : null;
|
|
691
|
+
}
|
|
692
|
+
async listDevices(accountId) {
|
|
693
|
+
return (await this.db.query(`SELECT account_id, device_id, public_key, status, registered_at, revoked_at
|
|
694
|
+
FROM vault_relay_devices WHERE account_id = ? ORDER BY registered_at, device_id`, accountId)).rows.map(deviceFromRow);
|
|
695
|
+
}
|
|
696
|
+
async getSupersession(accountId) {
|
|
697
|
+
const row = (await this.db.query(`SELECT account_id, superseded_by, superseded_at
|
|
698
|
+
FROM vault_relay_supersessions WHERE account_id = ?`, accountId)).rows[0];
|
|
699
|
+
return row ? {
|
|
700
|
+
accountId: String(row.account_id),
|
|
701
|
+
supersededBy: String(row.superseded_by),
|
|
702
|
+
supersededAt: toSafeInteger(row.superseded_at, "superseded_at")
|
|
703
|
+
} : null;
|
|
704
|
+
}
|
|
705
|
+
/**
|
|
706
|
+
* Record that `accountId` is replaced by `supersededBy`. One conditional
|
|
707
|
+
* insert: an existing record for the same target is `unchanged`, for
|
|
708
|
+
* another target a `conflict`; it is never overwritten.
|
|
709
|
+
*/
|
|
710
|
+
async setSupersession(accountId, supersededBy, now) {
|
|
711
|
+
if ((await this.db.query(`INSERT INTO vault_relay_supersessions
|
|
712
|
+
(account_id, superseded_by, superseded_at)
|
|
713
|
+
VALUES (?, ?, ?)
|
|
714
|
+
ON CONFLICT (account_id) DO NOTHING
|
|
715
|
+
RETURNING account_id`, accountId, supersededBy, now)).rows.length === 1) return "superseded";
|
|
716
|
+
return (await this.getSupersession(accountId))?.supersededBy === supersededBy ? "unchanged" : "conflict";
|
|
717
|
+
}
|
|
718
|
+
/** True when some account was replaced by `accountId`. */
|
|
719
|
+
async isSupersessionTarget(accountId) {
|
|
720
|
+
return (await this.db.query(`SELECT account_id FROM vault_relay_supersessions
|
|
721
|
+
WHERE superseded_by = ? LIMIT 1`, accountId)).rows.length > 0;
|
|
722
|
+
}
|
|
723
|
+
async clearSupersession(accountId) {
|
|
724
|
+
return (await this.db.query(`DELETE FROM vault_relay_supersessions WHERE account_id = ?
|
|
725
|
+
RETURNING account_id`, accountId)).rows.length === 1;
|
|
726
|
+
}
|
|
727
|
+
async countActiveDevices(accountId) {
|
|
728
|
+
return toSafeInteger((await this.db.query(`SELECT COUNT(*) AS active FROM vault_relay_devices
|
|
729
|
+
WHERE account_id = ? AND status = 'active'`, accountId)).rows[0]?.active ?? 0, "active");
|
|
730
|
+
}
|
|
731
|
+
async getLapse(accountId) {
|
|
732
|
+
const found = await this.db.query(`SELECT account_id, lapsed_at, swept_at
|
|
733
|
+
FROM vault_relay_lapses WHERE account_id = ?`, accountId);
|
|
734
|
+
return found.rows[0] ? lapseFromRow(found.rows[0]) : null;
|
|
735
|
+
}
|
|
736
|
+
/**
|
|
737
|
+
* Record the first observation of a lapse. One conditional insert: a lapse
|
|
738
|
+
* already recorded keeps its original time, so the window never slides.
|
|
739
|
+
*/
|
|
740
|
+
async recordLapse(accountId, now) {
|
|
741
|
+
await this.db.query(`INSERT INTO vault_relay_lapses (account_id, lapsed_at, swept_at)
|
|
742
|
+
VALUES (?, ?, NULL)
|
|
743
|
+
ON CONFLICT (account_id) DO NOTHING`, accountId, now);
|
|
744
|
+
return await this.getLapse(accountId) ?? {
|
|
745
|
+
accountId,
|
|
746
|
+
lapsedAt: now,
|
|
747
|
+
sweptAt: null
|
|
748
|
+
};
|
|
749
|
+
}
|
|
750
|
+
/** Forget a lapse (the entitlement is active again); `true` if one existed. */
|
|
751
|
+
async clearLapse(accountId) {
|
|
752
|
+
return (await this.db.query(`DELETE FROM vault_relay_lapses WHERE account_id = ?
|
|
753
|
+
RETURNING account_id`, accountId)).rows.length === 1;
|
|
754
|
+
}
|
|
755
|
+
/** Mark a lapse swept, once; `false` when it was swept or cleared. */
|
|
756
|
+
async markLapseSwept(accountId, now) {
|
|
757
|
+
return (await this.db.query(`UPDATE vault_relay_lapses SET swept_at = ?
|
|
758
|
+
WHERE account_id = ? AND swept_at IS NULL
|
|
759
|
+
RETURNING account_id`, now, accountId)).rows.length === 1;
|
|
760
|
+
}
|
|
761
|
+
/** Unswept lapses recorded at or before `cutoff`, oldest first. */
|
|
762
|
+
async listDueLapses(cutoff, limit) {
|
|
763
|
+
return (await this.db.query(`SELECT account_id, lapsed_at, swept_at FROM vault_relay_lapses
|
|
764
|
+
WHERE swept_at IS NULL AND lapsed_at <= ?
|
|
765
|
+
ORDER BY lapsed_at, account_id LIMIT ?`, cutoff, limit)).rows.map(lapseFromRow);
|
|
766
|
+
}
|
|
767
|
+
/** True when the account holds at least one committed snapshot. */
|
|
768
|
+
async hasCommitted(accountId) {
|
|
769
|
+
return (await this.db.query(`SELECT 1 AS present FROM vault_relay_snapshots
|
|
770
|
+
WHERE account_id = ? AND status = 'committed' LIMIT 1`, accountId)).rows.length > 0;
|
|
771
|
+
}
|
|
772
|
+
/**
|
|
773
|
+
* Accounts above `afterAccountId` (ordered) that hold a committed snapshot
|
|
774
|
+
* and have no lapse record: the candidates for the sweep's discovery pass.
|
|
775
|
+
*/
|
|
776
|
+
async listUnrecordedAccounts(afterAccountId, limit) {
|
|
777
|
+
return (await this.db.query(`SELECT s.account_id FROM vault_relay_snapshots AS s
|
|
778
|
+
WHERE s.status = 'committed' AND s.account_id > ?
|
|
779
|
+
AND NOT EXISTS (
|
|
780
|
+
SELECT 1 FROM vault_relay_lapses AS l
|
|
781
|
+
WHERE l.account_id = s.account_id)
|
|
782
|
+
GROUP BY s.account_id
|
|
783
|
+
ORDER BY s.account_id LIMIT ?`, afterAccountId, limit)).rows.map((row) => String(row.account_id));
|
|
784
|
+
}
|
|
785
|
+
/** Append an audit record. The audit table is never updated or deleted. */
|
|
786
|
+
async appendAudit(entry) {
|
|
787
|
+
await this.db.query(`INSERT INTO vault_relay_audit
|
|
788
|
+
(audit_id, occurred_at, action, operator, account_handle, snapshots, bytes, detail)
|
|
789
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?)`, entry.id, entry.at, entry.action, entry.operator, entry.accountHandle, entry.snapshots, entry.bytes, entry.detail);
|
|
790
|
+
}
|
|
791
|
+
/** Newest audit records first. */
|
|
792
|
+
async listAudit(limit) {
|
|
793
|
+
return (await this.db.query(`SELECT audit_id, occurred_at, action, operator, account_handle, snapshots, bytes, detail
|
|
794
|
+
FROM vault_relay_audit ORDER BY occurred_at DESC, audit_id LIMIT ?`, limit)).rows.map((row) => ({
|
|
795
|
+
id: String(row.audit_id),
|
|
796
|
+
at: toSafeInteger(row.occurred_at, "occurred_at"),
|
|
797
|
+
action: String(row.action),
|
|
798
|
+
operator: String(row.operator),
|
|
799
|
+
accountHandle: String(row.account_handle),
|
|
800
|
+
snapshots: toSafeInteger(row.snapshots, "snapshots"),
|
|
801
|
+
bytes: toSafeInteger(row.bytes, "bytes"),
|
|
802
|
+
detail: row.detail === null || row.detail === void 0 ? null : String(row.detail)
|
|
803
|
+
}));
|
|
804
|
+
}
|
|
805
|
+
/** Record a nonce; `false` when it was already used (a replay). */
|
|
806
|
+
async consumeNonce(accountId, deviceId, nonce, expiresAt) {
|
|
807
|
+
return (await this.db.query(`INSERT INTO vault_relay_nonces (account_id, device_id, nonce, expires_at)
|
|
808
|
+
VALUES (?, ?, ?, ?)
|
|
809
|
+
ON CONFLICT (account_id, device_id, nonce) DO NOTHING
|
|
810
|
+
RETURNING nonce`, accountId, deviceId, nonce, expiresAt)).rows.length === 1;
|
|
811
|
+
}
|
|
812
|
+
async pruneNonces(now) {
|
|
813
|
+
await this.db.query("DELETE FROM vault_relay_nonces WHERE expires_at < ?", now);
|
|
814
|
+
}
|
|
815
|
+
/** Reserve bytes and one snapshot slot; `false` when it would exceed quota. */
|
|
816
|
+
async reserve(accountId, bytes, maxBytes, maxSnapshots) {
|
|
817
|
+
return (await this.db.query(`UPDATE vault_relay_accounts
|
|
818
|
+
SET used_bytes = used_bytes + ?, used_snapshots = used_snapshots + 1
|
|
819
|
+
WHERE account_id = ?
|
|
820
|
+
AND used_bytes + ? <= ?
|
|
821
|
+
AND used_snapshots + 1 <= ?
|
|
822
|
+
RETURNING account_id`, bytes, accountId, bytes, maxBytes, maxSnapshots)).rows.length === 1;
|
|
823
|
+
}
|
|
824
|
+
async release(accountId, bytes) {
|
|
825
|
+
await this.db.query(`UPDATE vault_relay_accounts
|
|
826
|
+
SET used_bytes = CASE WHEN used_bytes >= ? THEN used_bytes - ? ELSE 0 END,
|
|
827
|
+
used_snapshots = CASE WHEN used_snapshots >= 1 THEN used_snapshots - 1 ELSE 0 END
|
|
828
|
+
WHERE account_id = ?`, bytes, bytes, accountId);
|
|
829
|
+
}
|
|
830
|
+
/** Recompute the quota counters from the snapshot rows, in one statement. */
|
|
831
|
+
async reconcileUsage(accountId) {
|
|
832
|
+
await this.db.query(`UPDATE vault_relay_accounts SET
|
|
833
|
+
used_bytes = (SELECT COALESCE(SUM(byte_size), 0)
|
|
834
|
+
FROM vault_relay_snapshots WHERE account_id = ?),
|
|
835
|
+
used_snapshots = (SELECT COUNT(*)
|
|
836
|
+
FROM vault_relay_snapshots WHERE account_id = ?)
|
|
837
|
+
WHERE account_id = ?`, accountId, accountId, accountId);
|
|
838
|
+
}
|
|
839
|
+
/**
|
|
840
|
+
* Insert a pending snapshot. `false` when the sequence already exists or is
|
|
841
|
+
* at or below the account's high-water mark (it was published before, even
|
|
842
|
+
* if since deleted).
|
|
843
|
+
*/
|
|
844
|
+
async createPending(record) {
|
|
845
|
+
return (await this.db.query(`INSERT INTO vault_relay_snapshots
|
|
846
|
+
(${SNAPSHOT_COLUMNS})
|
|
847
|
+
SELECT ?, ?, ?, ?, 'pending', ?, ?, ?, ?, ?, NULL
|
|
848
|
+
WHERE NOT EXISTS (
|
|
849
|
+
SELECT 1 FROM vault_relay_accounts
|
|
850
|
+
WHERE account_id = ? AND high_water_sequence >= ?)
|
|
851
|
+
ON CONFLICT DO NOTHING
|
|
852
|
+
RETURNING upload_id`, record.accountId, record.sequence, record.uploadId, record.deviceId, record.byteSize, record.chunkSize, record.chunkCount, record.sha256, record.createdAt, record.accountId, record.sequence)).rows.length === 1;
|
|
853
|
+
}
|
|
854
|
+
async getByUpload(accountId, uploadId) {
|
|
855
|
+
const found = await this.db.query(`SELECT ${SNAPSHOT_COLUMNS} FROM vault_relay_snapshots
|
|
856
|
+
WHERE account_id = ? AND upload_id = ?`, accountId, uploadId);
|
|
857
|
+
return found.rows[0] ? snapshotFromRow(found.rows[0]) : null;
|
|
858
|
+
}
|
|
859
|
+
async getCommitted(accountId, sequence) {
|
|
860
|
+
const found = await this.db.query(`SELECT ${SNAPSHOT_COLUMNS} FROM vault_relay_snapshots
|
|
861
|
+
WHERE account_id = ? AND sequence = ? AND status = 'committed'`, accountId, sequence);
|
|
862
|
+
return found.rows[0] ? snapshotFromRow(found.rows[0]) : null;
|
|
863
|
+
}
|
|
864
|
+
async latestCommitted(accountId) {
|
|
865
|
+
const found = await this.db.query(`SELECT ${SNAPSHOT_COLUMNS} FROM vault_relay_snapshots
|
|
866
|
+
WHERE account_id = ? AND status = 'committed'
|
|
867
|
+
ORDER BY sequence DESC LIMIT 1`, accountId);
|
|
868
|
+
return found.rows[0] ? snapshotFromRow(found.rows[0]) : null;
|
|
869
|
+
}
|
|
870
|
+
/** Every snapshot of the account, newest sequence first. */
|
|
871
|
+
async listSnapshots(accountId, status) {
|
|
872
|
+
return (status ? await this.db.query(`SELECT ${SNAPSHOT_COLUMNS} FROM vault_relay_snapshots
|
|
873
|
+
WHERE account_id = ? AND status = ? ORDER BY sequence DESC`, accountId, status) : await this.db.query(`SELECT ${SNAPSHOT_COLUMNS} FROM vault_relay_snapshots
|
|
874
|
+
WHERE account_id = ? ORDER BY sequence DESC`, accountId)).rows.map(snapshotFromRow);
|
|
875
|
+
}
|
|
876
|
+
/** Pending uploads created before `cutoff`, across accounts. */
|
|
877
|
+
async stalePending(cutoff, limit) {
|
|
878
|
+
return (await this.db.query(`SELECT ${SNAPSHOT_COLUMNS} FROM vault_relay_snapshots
|
|
879
|
+
WHERE status = 'pending' AND created_at < ?
|
|
880
|
+
ORDER BY created_at LIMIT ?`, cutoff, limit)).rows.map(snapshotFromRow);
|
|
881
|
+
}
|
|
882
|
+
/**
|
|
883
|
+
* Raise the account's sequence high-water mark to `sequence`. Fails when a
|
|
884
|
+
* higher sequence is already published, so a stale upload can never become
|
|
885
|
+
* latest and an old sequence can never be republished, and when `sequence`
|
|
886
|
+
* is more than `maxJump` above the mark, so one commit can never push the
|
|
887
|
+
* mark out of reach of every later upload.
|
|
888
|
+
*/
|
|
889
|
+
async advanceHighWater(accountId, sequence, maxJump) {
|
|
890
|
+
return (await this.db.query(`UPDATE vault_relay_accounts SET high_water_sequence = ?
|
|
891
|
+
WHERE account_id = ? AND high_water_sequence <= ?
|
|
892
|
+
AND ? - high_water_sequence <= ?
|
|
893
|
+
RETURNING account_id`, sequence, accountId, sequence, sequence, maxJump)).rows.length === 1;
|
|
894
|
+
}
|
|
895
|
+
/**
|
|
896
|
+
* Operator reset: lower the high-water mark to `sequence`, only while the
|
|
897
|
+
* mark is not already below it and no snapshot row (pending or committed)
|
|
898
|
+
* sits above it, so the mark never ends up beneath a stored sequence.
|
|
899
|
+
*/
|
|
900
|
+
async resetHighWater(accountId, sequence) {
|
|
901
|
+
return (await this.db.query(`UPDATE vault_relay_accounts SET high_water_sequence = ?
|
|
902
|
+
WHERE account_id = ? AND high_water_sequence >= ?
|
|
903
|
+
AND NOT EXISTS (
|
|
904
|
+
SELECT 1 FROM vault_relay_snapshots
|
|
905
|
+
WHERE account_id = ? AND sequence > ?)
|
|
906
|
+
RETURNING account_id`, sequence, accountId, sequence, accountId, sequence)).rows.length === 1;
|
|
907
|
+
}
|
|
908
|
+
/** Publish a pending upload; `false` when it is not pending any more. */
|
|
909
|
+
async markCommitted(accountId, uploadId, now) {
|
|
910
|
+
return (await this.db.query(`UPDATE vault_relay_snapshots SET status = 'committed', committed_at = ?
|
|
911
|
+
WHERE account_id = ? AND upload_id = ? AND status = 'pending'
|
|
912
|
+
RETURNING upload_id`, now, accountId, uploadId)).rows.length === 1;
|
|
913
|
+
}
|
|
914
|
+
/**
|
|
915
|
+
* Delete the snapshot row only while it still has `status`, so removing a
|
|
916
|
+
* pending upload can never delete a snapshot that was published meanwhile.
|
|
917
|
+
* Returns the row when this call removed it.
|
|
918
|
+
*/
|
|
919
|
+
async deleteSnapshotRow(accountId, uploadId, status) {
|
|
920
|
+
const deleted = await this.db.query(`DELETE FROM vault_relay_snapshots
|
|
921
|
+
WHERE account_id = ? AND upload_id = ? AND status = ?
|
|
922
|
+
RETURNING ${SNAPSHOT_COLUMNS}`, accountId, uploadId, status);
|
|
923
|
+
return deleted.rows[0] ? snapshotFromRow(deleted.rows[0]) : null;
|
|
924
|
+
}
|
|
925
|
+
/**
|
|
926
|
+
* Delete a committed snapshot row only while another committed snapshot of
|
|
927
|
+
* the account remains, in one statement. Returns the row when this call
|
|
928
|
+
* removed it, `null` when it is gone or is the account's last committed
|
|
929
|
+
* snapshot. Two devices deleting the final two snapshots at the same
|
|
930
|
+
* instant can still both pass on PostgreSQL (each sees the other), which
|
|
931
|
+
* the device API accepts as a residual risk; retention never goes through
|
|
932
|
+
* here and always keeps at least one.
|
|
933
|
+
*/
|
|
934
|
+
async deleteCommittedUnlessLast(accountId, uploadId) {
|
|
935
|
+
const deleted = await this.db.query(`DELETE FROM vault_relay_snapshots
|
|
936
|
+
WHERE account_id = ? AND upload_id = ? AND status = 'committed'
|
|
937
|
+
AND EXISTS (
|
|
938
|
+
SELECT 1 FROM vault_relay_snapshots AS other
|
|
939
|
+
WHERE other.account_id = ? AND other.status = 'committed'
|
|
940
|
+
AND other.upload_id <> ?)
|
|
941
|
+
RETURNING ${SNAPSHOT_COLUMNS}`, accountId, uploadId, accountId, uploadId);
|
|
942
|
+
return deleted.rows[0] ? snapshotFromRow(deleted.rows[0]) : null;
|
|
943
|
+
}
|
|
944
|
+
/**
|
|
945
|
+
* Record a stored chunk object, only while its upload is still pending for
|
|
946
|
+
* the account and only if the index has no object yet. Returns `false`
|
|
947
|
+
* otherwise; the caller then removes the object it wrote, so a late write
|
|
948
|
+
* after an abort, expiry, or commit leaves nothing behind.
|
|
949
|
+
*/
|
|
950
|
+
async addChunk(accountId, uploadId, chunk) {
|
|
951
|
+
return (await this.db.query(`INSERT INTO vault_relay_chunks
|
|
952
|
+
(upload_id, chunk_index, byte_size, sha256, object_id)
|
|
953
|
+
SELECT ?, ?, ?, ?, ?
|
|
954
|
+
WHERE EXISTS (
|
|
955
|
+
SELECT 1 FROM vault_relay_snapshots
|
|
956
|
+
WHERE account_id = ? AND upload_id = ? AND status = 'pending')
|
|
957
|
+
ON CONFLICT (upload_id, chunk_index) DO NOTHING
|
|
958
|
+
RETURNING chunk_index`, uploadId, chunk.index, chunk.byteSize, chunk.sha256, chunk.objectId, accountId, uploadId)).rows.length === 1;
|
|
959
|
+
}
|
|
960
|
+
async listChunks(uploadId) {
|
|
961
|
+
return (await this.db.query(`SELECT chunk_index, byte_size, sha256, object_id FROM vault_relay_chunks
|
|
962
|
+
WHERE upload_id = ? ORDER BY chunk_index`, uploadId)).rows.map(chunkFromRow);
|
|
963
|
+
}
|
|
964
|
+
async getChunk(uploadId, index) {
|
|
965
|
+
const found = await this.db.query(`SELECT chunk_index, byte_size, sha256, object_id FROM vault_relay_chunks
|
|
966
|
+
WHERE upload_id = ? AND chunk_index = ?`, uploadId, index);
|
|
967
|
+
return found.rows[0] ? chunkFromRow(found.rows[0]) : null;
|
|
968
|
+
}
|
|
969
|
+
/** Delete one recorded chunk attempt; `true` when this call removed it. */
|
|
970
|
+
async deleteChunkAttempt(uploadId, index, objectId) {
|
|
971
|
+
return (await this.db.query(`DELETE FROM vault_relay_chunks
|
|
972
|
+
WHERE upload_id = ? AND chunk_index = ? AND object_id = ?
|
|
973
|
+
RETURNING chunk_index`, uploadId, index, objectId)).rows.length === 1;
|
|
974
|
+
}
|
|
975
|
+
/** Delete every chunk row of an upload; returns the removed rows. */
|
|
976
|
+
async deleteChunks(uploadId) {
|
|
977
|
+
return (await this.db.query(`DELETE FROM vault_relay_chunks WHERE upload_id = ?
|
|
978
|
+
RETURNING chunk_index, byte_size, sha256, object_id`, uploadId)).rows.map(chunkFromRow);
|
|
979
|
+
}
|
|
980
|
+
};
|
|
981
|
+
//#endregion
|
|
982
|
+
//#region src/node-server.ts
|
|
983
|
+
var ORIGIN = "http://vault-relay.invalid";
|
|
984
|
+
function toRequest(req) {
|
|
985
|
+
const headers = new Headers();
|
|
986
|
+
for (const [name, value] of Object.entries(req.headers)) {
|
|
987
|
+
if (value === void 0) continue;
|
|
988
|
+
headers.set(name, Array.isArray(value) ? value.join(", ") : value);
|
|
989
|
+
}
|
|
990
|
+
const method = (req.method ?? "GET").toUpperCase();
|
|
991
|
+
const length = req.headers["content-length"];
|
|
992
|
+
const hasBody = method !== "GET" && method !== "HEAD" && (length !== void 0 && length !== "0" || req.headers["transfer-encoding"] !== void 0);
|
|
993
|
+
return new Request(`${ORIGIN}${req.url ?? "/"}`, {
|
|
994
|
+
method,
|
|
995
|
+
headers,
|
|
996
|
+
...hasBody ? {
|
|
997
|
+
body: Readable.toWeb(req),
|
|
998
|
+
duplex: "half"
|
|
999
|
+
} : {}
|
|
1000
|
+
});
|
|
1001
|
+
}
|
|
1002
|
+
async function writeResponse(response, res) {
|
|
1003
|
+
const headers = {};
|
|
1004
|
+
response.headers.forEach((value, name) => {
|
|
1005
|
+
headers[name] = value;
|
|
1006
|
+
});
|
|
1007
|
+
const body = response.body === null ? null : Buffer.from(await response.arrayBuffer());
|
|
1008
|
+
if (body && headers["content-length"] === void 0) headers["content-length"] = String(body.length);
|
|
1009
|
+
res.writeHead(response.status, headers);
|
|
1010
|
+
res.end(body ?? void 0);
|
|
1011
|
+
}
|
|
1012
|
+
function createRelayHttpServer(handler, options = {}) {
|
|
1013
|
+
const server = createServer(async (req, res) => {
|
|
1014
|
+
try {
|
|
1015
|
+
await writeResponse(await handler(toRequest(req)), res);
|
|
1016
|
+
} catch {
|
|
1017
|
+
if (!res.headersSent) res.writeHead(500, { "content-type": "application/json" });
|
|
1018
|
+
res.end("{\"error\":\"internal_error\"}");
|
|
1019
|
+
}
|
|
1020
|
+
});
|
|
1021
|
+
server.requestTimeout = options.requestTimeoutMs ?? 12e4;
|
|
1022
|
+
server.headersTimeout = 3e4;
|
|
1023
|
+
return server;
|
|
1024
|
+
}
|
|
1025
|
+
//#endregion
|
|
1026
|
+
//#region src/object-store.ts
|
|
1027
|
+
var OBJECT_KEY = /^v1\/[0-9a-f]{64}\/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\/(0|[1-9][0-9]{0,5})\.[0-9a-f]{32}$/;
|
|
1028
|
+
function chunkObjectKey(accountHandle, uploadId, index, objectId) {
|
|
1029
|
+
const key = `v1/${accountHandle}/${uploadId}/${index}.${objectId}`;
|
|
1030
|
+
assertObjectKey(key);
|
|
1031
|
+
return key;
|
|
1032
|
+
}
|
|
1033
|
+
function assertObjectKey(key) {
|
|
1034
|
+
if (!OBJECT_KEY.test(key)) throw new Error("vault relay: invalid object key");
|
|
1035
|
+
}
|
|
1036
|
+
var FilesystemObjectStore = class {
|
|
1037
|
+
root;
|
|
1038
|
+
constructor(root) {
|
|
1039
|
+
this.root = resolve(root);
|
|
1040
|
+
}
|
|
1041
|
+
pathFor(key) {
|
|
1042
|
+
assertObjectKey(key);
|
|
1043
|
+
const path = resolve(this.root, ...key.split("/"));
|
|
1044
|
+
const rel = relative(this.root, path);
|
|
1045
|
+
if (rel.startsWith("..") || rel.includes(`..${sep}`) || rel === "") throw new Error("vault relay: object key escapes the store root");
|
|
1046
|
+
return path;
|
|
1047
|
+
}
|
|
1048
|
+
async put(key, data) {
|
|
1049
|
+
const path = this.pathFor(key);
|
|
1050
|
+
await mkdir(dirname(path), {
|
|
1051
|
+
recursive: true,
|
|
1052
|
+
mode: 448
|
|
1053
|
+
});
|
|
1054
|
+
const temp = join(dirname(path), `.tmp-${randomUUID()}`);
|
|
1055
|
+
try {
|
|
1056
|
+
await writeFile(temp, data, {
|
|
1057
|
+
mode: 384,
|
|
1058
|
+
flag: "wx"
|
|
1059
|
+
});
|
|
1060
|
+
await rename(temp, path);
|
|
1061
|
+
} catch (error) {
|
|
1062
|
+
await rm(temp, { force: true });
|
|
1063
|
+
throw error;
|
|
1064
|
+
}
|
|
1065
|
+
}
|
|
1066
|
+
async get(key) {
|
|
1067
|
+
try {
|
|
1068
|
+
return new Uint8Array(await readFile(this.pathFor(key)));
|
|
1069
|
+
} catch (error) {
|
|
1070
|
+
if (error.code === "ENOENT") return null;
|
|
1071
|
+
throw error;
|
|
1072
|
+
}
|
|
1073
|
+
}
|
|
1074
|
+
async delete(key) {
|
|
1075
|
+
const path = this.pathFor(key);
|
|
1076
|
+
await rm(path, { force: true });
|
|
1077
|
+
try {
|
|
1078
|
+
const dir = dirname(path);
|
|
1079
|
+
if ((await readdir(dir)).length === 0) await rmdir(dir);
|
|
1080
|
+
} catch {}
|
|
1081
|
+
}
|
|
1082
|
+
async check() {
|
|
1083
|
+
await mkdir(this.root, {
|
|
1084
|
+
recursive: true,
|
|
1085
|
+
mode: 448
|
|
1086
|
+
});
|
|
1087
|
+
}
|
|
1088
|
+
};
|
|
1089
|
+
var FilesObjectStore = class {
|
|
1090
|
+
filesystem;
|
|
1091
|
+
isNotFound;
|
|
1092
|
+
constructor(filesystem, isNotFound = defaultIsNotFound) {
|
|
1093
|
+
this.filesystem = filesystem;
|
|
1094
|
+
this.isNotFound = isNotFound;
|
|
1095
|
+
}
|
|
1096
|
+
async put(key, data) {
|
|
1097
|
+
assertObjectKey(key);
|
|
1098
|
+
await this.filesystem.write(key, Buffer.from(data));
|
|
1099
|
+
}
|
|
1100
|
+
async get(key) {
|
|
1101
|
+
assertObjectKey(key);
|
|
1102
|
+
try {
|
|
1103
|
+
const content = await this.filesystem.read(key, { raw: true });
|
|
1104
|
+
return typeof content === "string" ? new Uint8Array(Buffer.from(content, "binary")) : new Uint8Array(content);
|
|
1105
|
+
} catch (error) {
|
|
1106
|
+
if (this.isNotFound(error)) return null;
|
|
1107
|
+
throw error;
|
|
1108
|
+
}
|
|
1109
|
+
}
|
|
1110
|
+
async delete(key) {
|
|
1111
|
+
assertObjectKey(key);
|
|
1112
|
+
try {
|
|
1113
|
+
await this.filesystem.delete(key);
|
|
1114
|
+
} catch (error) {
|
|
1115
|
+
if (!this.isNotFound(error)) throw error;
|
|
1116
|
+
}
|
|
1117
|
+
}
|
|
1118
|
+
async check() {
|
|
1119
|
+
await this.filesystem.exists("v1/readiness-probe");
|
|
1120
|
+
}
|
|
1121
|
+
};
|
|
1122
|
+
function defaultIsNotFound(error) {
|
|
1123
|
+
return error instanceof Error && error.name === "FileNotFoundError";
|
|
1124
|
+
}
|
|
1125
|
+
async function createS3ObjectStore(options) {
|
|
1126
|
+
const files = await import("@happyvertical/files");
|
|
1127
|
+
const settings = {
|
|
1128
|
+
type: "s3",
|
|
1129
|
+
bucket: options.bucket,
|
|
1130
|
+
region: options.region,
|
|
1131
|
+
endpoint: options.endpoint,
|
|
1132
|
+
accessKeyId: options.accessKeyId,
|
|
1133
|
+
secretAccessKey: options.secretAccessKey,
|
|
1134
|
+
forcePathStyle: options.forcePathStyle ?? true
|
|
1135
|
+
};
|
|
1136
|
+
return new FilesObjectStore(await files.getFilesystem(settings), (error) => error instanceof files.FileNotFoundError);
|
|
1137
|
+
}
|
|
1138
|
+
//#endregion
|
|
1139
|
+
//#region src/relay.ts
|
|
1140
|
+
var jsonLineLogger = {
|
|
1141
|
+
info: (event, fields) => process.stdout.write(`${JSON.stringify({
|
|
1142
|
+
level: "info",
|
|
1143
|
+
event,
|
|
1144
|
+
...fields
|
|
1145
|
+
})}
|
|
1146
|
+
`),
|
|
1147
|
+
warn: (event, fields) => process.stdout.write(`${JSON.stringify({
|
|
1148
|
+
level: "warn",
|
|
1149
|
+
event,
|
|
1150
|
+
...fields
|
|
1151
|
+
})}
|
|
1152
|
+
`),
|
|
1153
|
+
error: (event, fields) => process.stderr.write(`${JSON.stringify({
|
|
1154
|
+
level: "error",
|
|
1155
|
+
event,
|
|
1156
|
+
...fields
|
|
1157
|
+
})}
|
|
1158
|
+
`)
|
|
1159
|
+
};
|
|
1160
|
+
var RelayError = class extends Error {
|
|
1161
|
+
status;
|
|
1162
|
+
code;
|
|
1163
|
+
detail;
|
|
1164
|
+
constructor(status, code, detail) {
|
|
1165
|
+
super(code);
|
|
1166
|
+
this.status = status;
|
|
1167
|
+
this.code = code;
|
|
1168
|
+
this.detail = detail;
|
|
1169
|
+
}
|
|
1170
|
+
};
|
|
1171
|
+
var NONCE_PRUNE_INTERVAL_MS = 6e4;
|
|
1172
|
+
var SWEEP_BATCH = 100;
|
|
1173
|
+
var DEFAULT_LAPSE_SWEEP_ACCOUNTS = 10;
|
|
1174
|
+
var MAX_LAPSE_SWEEP_ACCOUNTS = 100;
|
|
1175
|
+
var OPERATOR_LABEL = /^[A-Za-z0-9][A-Za-z0-9._@-]{0,63}$/;
|
|
1176
|
+
function json(status, body, headers = {}) {
|
|
1177
|
+
return new Response(status === 204 ? null : JSON.stringify(body), {
|
|
1178
|
+
status,
|
|
1179
|
+
headers: {
|
|
1180
|
+
"content-type": "application/json",
|
|
1181
|
+
"cache-control": "no-store",
|
|
1182
|
+
...headers
|
|
1183
|
+
}
|
|
1184
|
+
});
|
|
1185
|
+
}
|
|
1186
|
+
function errorResponse(error) {
|
|
1187
|
+
return json(error.status, {
|
|
1188
|
+
error: error.code,
|
|
1189
|
+
...error.detail
|
|
1190
|
+
});
|
|
1191
|
+
}
|
|
1192
|
+
function declaredLength(request) {
|
|
1193
|
+
const header = request.headers.get("content-length");
|
|
1194
|
+
if (header === null) return null;
|
|
1195
|
+
const parsed = parseCanonicalInteger(header.trim());
|
|
1196
|
+
if (parsed === null) throw new RelayError(400, "invalid_content_length");
|
|
1197
|
+
return parsed;
|
|
1198
|
+
}
|
|
1199
|
+
async function readBoundedBody(request, max, exact) {
|
|
1200
|
+
const length = declaredLength(request);
|
|
1201
|
+
if (length === null) {
|
|
1202
|
+
if (request.body === null) return /* @__PURE__ */ new Uint8Array(0);
|
|
1203
|
+
throw new RelayError(411, "length_required");
|
|
1204
|
+
}
|
|
1205
|
+
if (length > max) throw new RelayError(413, "body_too_large");
|
|
1206
|
+
if (exact !== void 0 && length !== exact) throw new RelayError(400, "unexpected_body_length", { expected: exact });
|
|
1207
|
+
const buffer = new Uint8Array(length);
|
|
1208
|
+
if (request.body === null) {
|
|
1209
|
+
if (length !== 0) throw new RelayError(400, "body_length_mismatch");
|
|
1210
|
+
return buffer;
|
|
1211
|
+
}
|
|
1212
|
+
let offset = 0;
|
|
1213
|
+
const reader = request.body.getReader();
|
|
1214
|
+
try {
|
|
1215
|
+
for (;;) {
|
|
1216
|
+
const { done, value } = await reader.read();
|
|
1217
|
+
if (done) break;
|
|
1218
|
+
if (offset + value.length > length) throw new RelayError(400, "body_length_mismatch");
|
|
1219
|
+
buffer.set(value, offset);
|
|
1220
|
+
offset += value.length;
|
|
1221
|
+
}
|
|
1222
|
+
} finally {
|
|
1223
|
+
reader.releaseLock();
|
|
1224
|
+
}
|
|
1225
|
+
if (offset !== length) throw new RelayError(400, "body_length_mismatch");
|
|
1226
|
+
return buffer;
|
|
1227
|
+
}
|
|
1228
|
+
function parseJsonObject(bytes) {
|
|
1229
|
+
try {
|
|
1230
|
+
const value = JSON.parse(Buffer.from(bytes).toString("utf8"));
|
|
1231
|
+
if (value && typeof value === "object" && !Array.isArray(value)) return value;
|
|
1232
|
+
} catch {}
|
|
1233
|
+
throw new RelayError(400, "invalid_json");
|
|
1234
|
+
}
|
|
1235
|
+
function positiveSafeInteger(value, field) {
|
|
1236
|
+
if (typeof value !== "number" || !Number.isSafeInteger(value) || value < 1) throw new RelayError(400, "invalid_request", { field });
|
|
1237
|
+
return value;
|
|
1238
|
+
}
|
|
1239
|
+
function nonNegativeSafeInteger(value, field) {
|
|
1240
|
+
if (typeof value !== "number" || !Number.isSafeInteger(value) || value < 0) throw new RelayError(400, "invalid_request", { field });
|
|
1241
|
+
return value;
|
|
1242
|
+
}
|
|
1243
|
+
function requestedTtlMs(input) {
|
|
1244
|
+
if (input.ttlSeconds === void 0) return void 0;
|
|
1245
|
+
return positiveSafeInteger(input.ttlSeconds, "ttlSeconds") * 1e3;
|
|
1246
|
+
}
|
|
1247
|
+
function refusalError(refusal) {
|
|
1248
|
+
const { error, ...detail } = refusal;
|
|
1249
|
+
return new RelayError(409, error, detail);
|
|
1250
|
+
}
|
|
1251
|
+
function iso(ms) {
|
|
1252
|
+
return new Date(ms).toISOString();
|
|
1253
|
+
}
|
|
1254
|
+
function snapshotView(record) {
|
|
1255
|
+
return {
|
|
1256
|
+
sequence: record.sequence,
|
|
1257
|
+
byteSize: record.byteSize,
|
|
1258
|
+
chunkSize: record.chunkSize,
|
|
1259
|
+
chunkCount: record.chunkCount,
|
|
1260
|
+
sha256: record.sha256,
|
|
1261
|
+
deviceId: record.deviceId,
|
|
1262
|
+
createdAt: iso(record.createdAt),
|
|
1263
|
+
committedAt: record.committedAt === null ? null : iso(record.committedAt)
|
|
1264
|
+
};
|
|
1265
|
+
}
|
|
1266
|
+
function expectedChunkBytes(record, index) {
|
|
1267
|
+
return index < record.chunkCount - 1 ? record.chunkSize : record.byteSize - record.chunkSize * (record.chunkCount - 1);
|
|
1268
|
+
}
|
|
1269
|
+
var SEQUENCE = "(0|[1-9][0-9]{0,15})";
|
|
1270
|
+
var INDEX = "(0|[1-9][0-9]{0,5})";
|
|
1271
|
+
var UPLOAD = "([0-9a-f-]{36})";
|
|
1272
|
+
var SNAPSHOT_PATH = new RegExp(`^/v1/snapshots/${SEQUENCE}$`);
|
|
1273
|
+
var SNAPSHOT_CHUNK_PATH = new RegExp(`^/v1/snapshots/${SEQUENCE}/chunks/${INDEX}$`);
|
|
1274
|
+
var UPLOAD_CHUNK_PATH = new RegExp(`^/v1/uploads/${UPLOAD}/chunks/${INDEX}$`);
|
|
1275
|
+
var UPLOAD_COMMIT_PATH = new RegExp(`^/v1/uploads/${UPLOAD}/commit$`);
|
|
1276
|
+
var UPLOAD_PATH = new RegExp(`^/v1/uploads/${UPLOAD}$`);
|
|
1277
|
+
function only(method, allowed, route) {
|
|
1278
|
+
return allowed.includes(method) ? route : null;
|
|
1279
|
+
}
|
|
1280
|
+
function matchDeviceRoute(method, path) {
|
|
1281
|
+
if (path === "/v1/lease") return only(method, ["GET"], { name: "lease-status" });
|
|
1282
|
+
if (path === "/v1/lease/acquire") return only(method, ["POST"], { name: "lease-acquire" });
|
|
1283
|
+
if (path === "/v1/lease/renew") return only(method, ["POST"], { name: "lease-renew" });
|
|
1284
|
+
if (path === "/v1/lease/release") return only(method, ["POST"], { name: "lease-release" });
|
|
1285
|
+
if (path === "/v1/snapshots") return only(method, ["GET"], { name: "list" });
|
|
1286
|
+
if (path === "/v1/snapshots/latest") return only(method, ["GET"], { name: "latest" });
|
|
1287
|
+
if (path === "/v1/uploads") return only(method, ["POST"], { name: "begin" });
|
|
1288
|
+
const snapshot = SNAPSHOT_PATH.exec(path);
|
|
1289
|
+
if (snapshot) {
|
|
1290
|
+
const sequence = Number(snapshot[1]);
|
|
1291
|
+
if (!Number.isSafeInteger(sequence)) return null;
|
|
1292
|
+
return only(method, ["GET", "DELETE"], {
|
|
1293
|
+
name: "snapshot",
|
|
1294
|
+
sequence
|
|
1295
|
+
});
|
|
1296
|
+
}
|
|
1297
|
+
const chunk = SNAPSHOT_CHUNK_PATH.exec(path);
|
|
1298
|
+
if (chunk) {
|
|
1299
|
+
const sequence = Number(chunk[1]);
|
|
1300
|
+
if (!Number.isSafeInteger(sequence)) return null;
|
|
1301
|
+
return only(method, ["GET"], {
|
|
1302
|
+
name: "chunk",
|
|
1303
|
+
sequence,
|
|
1304
|
+
index: Number(chunk[2])
|
|
1305
|
+
});
|
|
1306
|
+
}
|
|
1307
|
+
const uploadChunk = UPLOAD_CHUNK_PATH.exec(path);
|
|
1308
|
+
if (uploadChunk) {
|
|
1309
|
+
if (!isUploadId(uploadChunk[1])) return null;
|
|
1310
|
+
return only(method, ["PUT"], {
|
|
1311
|
+
name: "upload-chunk",
|
|
1312
|
+
uploadId: uploadChunk[1],
|
|
1313
|
+
index: Number(uploadChunk[2])
|
|
1314
|
+
});
|
|
1315
|
+
}
|
|
1316
|
+
const commit = UPLOAD_COMMIT_PATH.exec(path);
|
|
1317
|
+
if (commit) {
|
|
1318
|
+
if (!isUploadId(commit[1])) return null;
|
|
1319
|
+
return only(method, ["POST"], {
|
|
1320
|
+
name: "commit",
|
|
1321
|
+
uploadId: commit[1]
|
|
1322
|
+
});
|
|
1323
|
+
}
|
|
1324
|
+
const upload = UPLOAD_PATH.exec(path);
|
|
1325
|
+
if (upload) {
|
|
1326
|
+
if (!isUploadId(upload[1])) return null;
|
|
1327
|
+
return only(method, ["DELETE"], {
|
|
1328
|
+
name: "abort",
|
|
1329
|
+
uploadId: upload[1]
|
|
1330
|
+
});
|
|
1331
|
+
}
|
|
1332
|
+
return null;
|
|
1333
|
+
}
|
|
1334
|
+
function isWriteRoute(route, method) {
|
|
1335
|
+
return route.name === "begin" || route.name === "upload-chunk" || route.name === "commit" || route.name === "snapshot" && method.toUpperCase() === "DELETE";
|
|
1336
|
+
}
|
|
1337
|
+
var VaultRelay = class {
|
|
1338
|
+
policy;
|
|
1339
|
+
store;
|
|
1340
|
+
leaseStore;
|
|
1341
|
+
leases;
|
|
1342
|
+
objects;
|
|
1343
|
+
entitlements;
|
|
1344
|
+
adminTokenHash;
|
|
1345
|
+
logger;
|
|
1346
|
+
now;
|
|
1347
|
+
lastNoncePrune = 0;
|
|
1348
|
+
constructor(options) {
|
|
1349
|
+
this.policy = resolvePolicy(options.policy);
|
|
1350
|
+
this.store = options.store;
|
|
1351
|
+
this.leaseStore = options.leaseStore ?? new VaultRelayLeaseStore(options.store.db);
|
|
1352
|
+
this.leases = new LeaseService(this.leaseStore, this.policy.limits.leaseTtlMs);
|
|
1353
|
+
this.objects = options.objects;
|
|
1354
|
+
this.entitlements = options.entitlements;
|
|
1355
|
+
if (options.adminToken !== void 0 && options.adminToken.length < 32) throw new Error("vault relay: the admin token must be at least 32 characters");
|
|
1356
|
+
this.adminTokenHash = options.adminToken ? createHash("sha256").update(options.adminToken).digest() : null;
|
|
1357
|
+
this.logger = options.logger ?? jsonLineLogger;
|
|
1358
|
+
this.now = options.now ?? Date.now;
|
|
1359
|
+
}
|
|
1360
|
+
/** Readiness: schema migrated and object store reachable. */
|
|
1361
|
+
async ready() {
|
|
1362
|
+
try {
|
|
1363
|
+
const missing = await this.store.missingTables();
|
|
1364
|
+
for (const table of VAULT_RELAY_LEASE_TABLES) if (!await this.store.db.tableExists(table)) missing.push(table);
|
|
1365
|
+
if (missing.length > 0) return {
|
|
1366
|
+
ready: false,
|
|
1367
|
+
reason: "schema_missing"
|
|
1368
|
+
};
|
|
1369
|
+
await this.objects.check();
|
|
1370
|
+
return { ready: true };
|
|
1371
|
+
} catch (error) {
|
|
1372
|
+
this.logger.warn("readiness_failed", { error: error instanceof Error ? error.name : "unknown" });
|
|
1373
|
+
return {
|
|
1374
|
+
ready: false,
|
|
1375
|
+
reason: "dependency_unavailable"
|
|
1376
|
+
};
|
|
1377
|
+
}
|
|
1378
|
+
}
|
|
1379
|
+
async health(path) {
|
|
1380
|
+
if (path === "/healthz") return json(200, { status: "ok" });
|
|
1381
|
+
if (path === "/readyz") {
|
|
1382
|
+
const result = await this.ready();
|
|
1383
|
+
return json(result.ready ? 200 : 503, {
|
|
1384
|
+
status: result.ready ? "ready" : "not_ready",
|
|
1385
|
+
...result.reason ? { reason: result.reason } : {}
|
|
1386
|
+
});
|
|
1387
|
+
}
|
|
1388
|
+
return null;
|
|
1389
|
+
}
|
|
1390
|
+
/** Device-facing handler (public listener). */
|
|
1391
|
+
async handle(request) {
|
|
1392
|
+
const started = this.now();
|
|
1393
|
+
const url = new URL(request.url);
|
|
1394
|
+
const method = request.method.toUpperCase();
|
|
1395
|
+
let route = null;
|
|
1396
|
+
let handle;
|
|
1397
|
+
let response;
|
|
1398
|
+
try {
|
|
1399
|
+
if (method === "GET") {
|
|
1400
|
+
const health = await this.health(url.pathname);
|
|
1401
|
+
if (health) return health;
|
|
1402
|
+
}
|
|
1403
|
+
route = matchDeviceRoute(method, url.pathname);
|
|
1404
|
+
if (!route) throw new RelayError(404, "not_found");
|
|
1405
|
+
const device = await this.authenticate(request, method, `${url.pathname}${url.search}`);
|
|
1406
|
+
handle = device.handle;
|
|
1407
|
+
await this.refuseSuperseded(route, request.method, device.accountId);
|
|
1408
|
+
await this.gateEntitlement(route, request.method, device.accountId);
|
|
1409
|
+
response = await this.dispatch(route, request, device);
|
|
1410
|
+
} catch (error) {
|
|
1411
|
+
response = this.toErrorResponse(error, handle);
|
|
1412
|
+
}
|
|
1413
|
+
this.logger.info("request", {
|
|
1414
|
+
route: route?.name ?? "unmatched",
|
|
1415
|
+
method,
|
|
1416
|
+
status: response.status,
|
|
1417
|
+
ms: this.now() - started,
|
|
1418
|
+
...handle ? { account: handle } : {}
|
|
1419
|
+
});
|
|
1420
|
+
return response;
|
|
1421
|
+
}
|
|
1422
|
+
toErrorResponse(error, handle) {
|
|
1423
|
+
if (error instanceof RelayError) return errorResponse(error);
|
|
1424
|
+
if (error instanceof FeatureUnavailableError) return error.toResponse();
|
|
1425
|
+
this.logger.error("request_failed", {
|
|
1426
|
+
error: error instanceof Error ? error.name : "unknown",
|
|
1427
|
+
...handle ? { account: handle } : {}
|
|
1428
|
+
});
|
|
1429
|
+
return json(500, { error: "internal_error" });
|
|
1430
|
+
}
|
|
1431
|
+
async authenticate(request, method, pathAndQuery) {
|
|
1432
|
+
const parsed = parseSignatureHeaders(method, pathAndQuery, request.headers);
|
|
1433
|
+
const denied = new RelayError(401, "unauthorized");
|
|
1434
|
+
if (!parsed) throw denied;
|
|
1435
|
+
const now = this.now();
|
|
1436
|
+
const skew = this.policy.limits.maxClockSkewMs;
|
|
1437
|
+
if (Math.abs(now - parsed.fields.timestamp) > skew) throw denied;
|
|
1438
|
+
const device = await this.store.getDevice(parsed.fields.accountId, parsed.fields.deviceId);
|
|
1439
|
+
if (device?.status !== "active") throw denied;
|
|
1440
|
+
const key = importDevicePublicKey(device.publicKey);
|
|
1441
|
+
if (!key || !verifySignature(parsed, key)) throw denied;
|
|
1442
|
+
await this.pruneNoncesIfDue(now);
|
|
1443
|
+
if (!await this.store.consumeNonce(parsed.fields.accountId, parsed.fields.deviceId, parsed.fields.nonce, parsed.fields.timestamp + skew + 1)) throw denied;
|
|
1444
|
+
return {
|
|
1445
|
+
accountId: parsed.fields.accountId,
|
|
1446
|
+
deviceId: parsed.fields.deviceId,
|
|
1447
|
+
handle: accountHandle(parsed.fields.accountId),
|
|
1448
|
+
contentSha256: parsed.fields.contentSha256
|
|
1449
|
+
};
|
|
1450
|
+
}
|
|
1451
|
+
/**
|
|
1452
|
+
* An account the account layer replaced (#4817) takes no new writes and
|
|
1453
|
+
* offers no snapshot to restore-latest: its devices would publish into a
|
|
1454
|
+
* data line nobody follows, and a restore would hand back the old vault.
|
|
1455
|
+
* Lease status answers it too: that refusal is how a device proves, before
|
|
1456
|
+
* it follows the new account, that the old one was replaced. What only
|
|
1457
|
+
* removes state (lease release, aborting an upload) and reads by explicit
|
|
1458
|
+
* sequence stay open. The refusal names no other
|
|
1459
|
+
* account: the client never follows the relay to a new account.
|
|
1460
|
+
*/
|
|
1461
|
+
async refuseSuperseded(route, method, accountId) {
|
|
1462
|
+
if (route.name === "lease-release" || route.name === "abort" || route.name === "chunk" || route.name === "snapshot" && method.toUpperCase() === "GET") return;
|
|
1463
|
+
if (await this.store.getSupersession(accountId)) throw new RelayError(409, "account_superseded");
|
|
1464
|
+
}
|
|
1465
|
+
async pruneNoncesIfDue(now) {
|
|
1466
|
+
if (now - this.lastNoncePrune < NONCE_PRUNE_INTERVAL_MS) return;
|
|
1467
|
+
this.lastNoncePrune = now;
|
|
1468
|
+
await this.store.pruneNonces(now);
|
|
1469
|
+
}
|
|
1470
|
+
/**
|
|
1471
|
+
* The `backup.vault` check, resolved server-side for the account's tenant:
|
|
1472
|
+
* `null` when granted, the structured denial otherwise. A lookup that
|
|
1473
|
+
* cannot complete fails closed (`503`).
|
|
1474
|
+
*/
|
|
1475
|
+
async checkEntitlement(accountId) {
|
|
1476
|
+
const tenantId = this.entitlements.tenantForAccount?.(accountId) ?? accountId;
|
|
1477
|
+
try {
|
|
1478
|
+
const grantSource = this.entitlements.grantSource ? await this.entitlements.grantSource() : void 0;
|
|
1479
|
+
await requireFeature(FEATURE_BACKUP_VAULT, {
|
|
1480
|
+
resolver: this.entitlements.resolver,
|
|
1481
|
+
tenantId,
|
|
1482
|
+
...grantSource !== void 0 ? { grantSource } : {}
|
|
1483
|
+
});
|
|
1484
|
+
return null;
|
|
1485
|
+
} catch (error) {
|
|
1486
|
+
if (error instanceof FeatureUnavailableError) return error;
|
|
1487
|
+
this.logger.error("entitlement_check_failed", {
|
|
1488
|
+
error: error instanceof Error ? error.name : "unknown",
|
|
1489
|
+
account: accountHandle(accountId)
|
|
1490
|
+
});
|
|
1491
|
+
throw new RelayError(503, "entitlement_unavailable");
|
|
1492
|
+
}
|
|
1493
|
+
}
|
|
1494
|
+
/**
|
|
1495
|
+
* Gate a device route on `backup.vault` (#4819). An entitled account clears
|
|
1496
|
+
* any recorded lapse. A denied account that holds committed snapshots is
|
|
1497
|
+
* *lapsed*: the relay records when it first saw that, refuses the write
|
|
1498
|
+
* routes with `402 entitlement_lapsed`, and keeps the read routes open
|
|
1499
|
+
* until the retention window after the lapse has passed, then answers
|
|
1500
|
+
* `410 retention_expired`. Any other denial (never entitled, operator
|
|
1501
|
+
* disabled) is the original `403`.
|
|
1502
|
+
*/
|
|
1503
|
+
async gateEntitlement(route, method, accountId) {
|
|
1504
|
+
const denial = await this.checkEntitlement(accountId);
|
|
1505
|
+
if (!denial) {
|
|
1506
|
+
if (await this.store.getLapse(accountId)) {
|
|
1507
|
+
await this.store.clearLapse(accountId);
|
|
1508
|
+
this.logger.info("entitlement_restored", { account: accountHandle(accountId) });
|
|
1509
|
+
}
|
|
1510
|
+
return;
|
|
1511
|
+
}
|
|
1512
|
+
const lapse = await this.observeLapse(accountId, denial);
|
|
1513
|
+
if (!lapse) throw denial;
|
|
1514
|
+
const detail = this.lapseDetail(lapse);
|
|
1515
|
+
if (isWriteRoute(route, method)) throw new RelayError(402, "entitlement_lapsed", detail);
|
|
1516
|
+
if (this.now() >= lapse.lapsedAt + this.policy.retention.lapseRetentionMs) throw new RelayError(410, "retention_expired", detail);
|
|
1517
|
+
}
|
|
1518
|
+
/**
|
|
1519
|
+
* The lapse record of a denied account, created on first observation. Only
|
|
1520
|
+
* an upgrade-required denial of an account with data counts as a lapse.
|
|
1521
|
+
*/
|
|
1522
|
+
async observeLapse(accountId, denial) {
|
|
1523
|
+
if (denial.code !== "feature_upgrade_required") return null;
|
|
1524
|
+
const existing = await this.store.getLapse(accountId);
|
|
1525
|
+
if (existing) return existing;
|
|
1526
|
+
if (!await this.store.hasCommitted(accountId)) return null;
|
|
1527
|
+
const recorded = await this.store.recordLapse(accountId, this.now());
|
|
1528
|
+
this.logger.warn("entitlement_lapsed", { account: accountHandle(accountId) });
|
|
1529
|
+
return recorded;
|
|
1530
|
+
}
|
|
1531
|
+
lapseDetail(lapse) {
|
|
1532
|
+
return {
|
|
1533
|
+
lapsedAt: iso(lapse.lapsedAt),
|
|
1534
|
+
restoreAvailableUntil: iso(lapse.lapsedAt + this.policy.retention.lapseRetentionMs)
|
|
1535
|
+
};
|
|
1536
|
+
}
|
|
1537
|
+
/** Reject a body on a route that takes none, and bind the empty digest. */
|
|
1538
|
+
async requireEmptyBody(request, device) {
|
|
1539
|
+
if ((await readBoundedBody(request, 0)).length !== 0) throw new RelayError(400, "unexpected_body");
|
|
1540
|
+
if (device.contentSha256 !== EMPTY_SHA256) throw new RelayError(400, "content_digest_mismatch");
|
|
1541
|
+
}
|
|
1542
|
+
/** A bounded JSON body whose digest is the signed one. */
|
|
1543
|
+
async readJson(request, device) {
|
|
1544
|
+
const body = await readBoundedBody(request, MAX_JSON_BODY_BYTES);
|
|
1545
|
+
if (sha256Hex(body) !== device.contentSha256) throw new RelayError(400, "content_digest_mismatch");
|
|
1546
|
+
return parseJsonObject(body);
|
|
1547
|
+
}
|
|
1548
|
+
async dispatchLease(route, request, device) {
|
|
1549
|
+
const now = this.now();
|
|
1550
|
+
if (route.name === "lease-status") {
|
|
1551
|
+
await this.requireEmptyBody(request, device);
|
|
1552
|
+
const status = await this.leases.status(device.accountId, now);
|
|
1553
|
+
return json(200, {
|
|
1554
|
+
...status,
|
|
1555
|
+
expiresAt: status.expiresAt === null ? null : iso(status.expiresAt)
|
|
1556
|
+
});
|
|
1557
|
+
}
|
|
1558
|
+
const input = await this.readJson(request, device);
|
|
1559
|
+
if (route.name === "lease-acquire") {
|
|
1560
|
+
const result = await this.leases.acquire(device.accountId, device.deviceId, nonNegativeSafeInteger(input.baseSequence, "baseSequence"), requestedTtlMs(input), now);
|
|
1561
|
+
if (!result.ok) throw refusalError(result.refusal);
|
|
1562
|
+
return json(200, {
|
|
1563
|
+
fencingToken: result.grant.fencingToken,
|
|
1564
|
+
expiresAt: iso(result.grant.expiresAt),
|
|
1565
|
+
ttlMs: result.grant.expiresAt - now,
|
|
1566
|
+
headSequence: result.grant.headSequence
|
|
1567
|
+
});
|
|
1568
|
+
}
|
|
1569
|
+
const fencingToken = positiveSafeInteger(input.fencingToken, "fencingToken");
|
|
1570
|
+
if (route.name === "lease-renew") {
|
|
1571
|
+
const result = await this.leases.renew(device.accountId, device.deviceId, fencingToken, requestedTtlMs(input), now);
|
|
1572
|
+
if (!result.ok) throw refusalError(result.refusal);
|
|
1573
|
+
return json(200, {
|
|
1574
|
+
fencingToken: result.grant.fencingToken,
|
|
1575
|
+
expiresAt: iso(result.grant.expiresAt),
|
|
1576
|
+
ttlMs: result.grant.expiresAt - now,
|
|
1577
|
+
headSequence: result.grant.headSequence
|
|
1578
|
+
});
|
|
1579
|
+
}
|
|
1580
|
+
await this.leases.release(device.accountId, device.deviceId, fencingToken);
|
|
1581
|
+
return json(204, null);
|
|
1582
|
+
}
|
|
1583
|
+
async dispatch(route, request, device) {
|
|
1584
|
+
switch (route.name) {
|
|
1585
|
+
case "lease-status":
|
|
1586
|
+
case "lease-acquire":
|
|
1587
|
+
case "lease-renew":
|
|
1588
|
+
case "lease-release": return this.dispatchLease(route, request, device);
|
|
1589
|
+
case "list":
|
|
1590
|
+
await this.requireEmptyBody(request, device);
|
|
1591
|
+
return json(200, { snapshots: (await this.store.listSnapshots(device.accountId, "committed")).map(snapshotView) });
|
|
1592
|
+
case "latest": {
|
|
1593
|
+
await this.requireEmptyBody(request, device);
|
|
1594
|
+
const latest = await this.store.latestCommitted(device.accountId);
|
|
1595
|
+
if (!latest) throw new RelayError(404, "no_snapshot");
|
|
1596
|
+
return json(200, snapshotView(latest));
|
|
1597
|
+
}
|
|
1598
|
+
case "snapshot": {
|
|
1599
|
+
await this.requireEmptyBody(request, device);
|
|
1600
|
+
const record = await this.store.getCommitted(device.accountId, route.sequence);
|
|
1601
|
+
if (!record) throw new RelayError(404, "no_snapshot");
|
|
1602
|
+
if (request.method.toUpperCase() === "DELETE") return this.deleteSnapshotForDevice(record, device);
|
|
1603
|
+
return json(200, snapshotView(record));
|
|
1604
|
+
}
|
|
1605
|
+
case "chunk":
|
|
1606
|
+
await this.requireEmptyBody(request, device);
|
|
1607
|
+
return this.downloadChunk(device, route.sequence, route.index);
|
|
1608
|
+
case "begin": return this.beginUpload(request, device);
|
|
1609
|
+
case "upload-chunk": return this.uploadChunk(request, device, route.uploadId, route.index);
|
|
1610
|
+
case "commit":
|
|
1611
|
+
await this.requireEmptyBody(request, device);
|
|
1612
|
+
return this.commitUpload(device, route.uploadId);
|
|
1613
|
+
case "abort": {
|
|
1614
|
+
await this.requireEmptyBody(request, device);
|
|
1615
|
+
const record = await this.store.getByUpload(device.accountId, route.uploadId);
|
|
1616
|
+
if (record?.status !== "pending") throw new RelayError(404, "no_upload");
|
|
1617
|
+
await this.removeSnapshot(record, device.handle, "pending");
|
|
1618
|
+
return json(204, null);
|
|
1619
|
+
}
|
|
1620
|
+
}
|
|
1621
|
+
}
|
|
1622
|
+
async beginUpload(request, device) {
|
|
1623
|
+
const input = await this.readJson(request, device);
|
|
1624
|
+
const sequence = positiveSafeInteger(input.sequence, "sequence");
|
|
1625
|
+
const fencingToken = positiveSafeInteger(input.fencingToken, "fencingToken");
|
|
1626
|
+
const byteSize = positiveSafeInteger(input.byteSize, "byteSize");
|
|
1627
|
+
const chunkSize = positiveSafeInteger(input.chunkSize, "chunkSize");
|
|
1628
|
+
if (!isSha256Hex(input.sha256)) throw new RelayError(400, "invalid_request", { field: "sha256" });
|
|
1629
|
+
const { quota, limits } = this.policy;
|
|
1630
|
+
if (chunkSize > limits.maxChunkBytes) throw new RelayError(413, "chunk_too_large", { maxChunkBytes: limits.maxChunkBytes });
|
|
1631
|
+
if (byteSize > quota.maxBytes) throw new RelayError(413, "quota_exceeded", {
|
|
1632
|
+
maxBytes: quota.maxBytes,
|
|
1633
|
+
maxSnapshots: quota.maxSnapshots
|
|
1634
|
+
});
|
|
1635
|
+
const chunkCount = Math.ceil(byteSize / chunkSize);
|
|
1636
|
+
if (chunkCount > 999999) throw new RelayError(400, "too_many_chunks");
|
|
1637
|
+
const now = this.now();
|
|
1638
|
+
await this.store.ensureAccount(device.accountId, now);
|
|
1639
|
+
await this.sweepExpired(now);
|
|
1640
|
+
const usage = await this.store.getUsage(device.accountId);
|
|
1641
|
+
if (usage && sequence <= usage.highWaterSequence) throw new RelayError(409, "stale_sequence", { highWaterSequence: usage.highWaterSequence });
|
|
1642
|
+
this.assertSequenceStep(sequence, usage?.highWaterSequence ?? 0);
|
|
1643
|
+
const refused = await this.leases.admitUpload(device.accountId, device.deviceId, fencingToken, sequence, now);
|
|
1644
|
+
if (refused) throw refusalError(refused);
|
|
1645
|
+
if (!await this.store.reserve(device.accountId, byteSize, quota.maxBytes, quota.maxSnapshots)) throw new RelayError(413, "quota_exceeded", {
|
|
1646
|
+
maxBytes: quota.maxBytes,
|
|
1647
|
+
maxSnapshots: quota.maxSnapshots
|
|
1648
|
+
});
|
|
1649
|
+
const uploadId = randomUUID();
|
|
1650
|
+
if (!await this.store.createPending({
|
|
1651
|
+
accountId: device.accountId,
|
|
1652
|
+
sequence,
|
|
1653
|
+
uploadId,
|
|
1654
|
+
deviceId: device.deviceId,
|
|
1655
|
+
byteSize,
|
|
1656
|
+
chunkSize,
|
|
1657
|
+
chunkCount,
|
|
1658
|
+
sha256: input.sha256,
|
|
1659
|
+
createdAt: now
|
|
1660
|
+
})) {
|
|
1661
|
+
await this.store.release(device.accountId, byteSize);
|
|
1662
|
+
const latest = await this.store.getUsage(device.accountId);
|
|
1663
|
+
if (latest && sequence <= latest.highWaterSequence) throw new RelayError(409, "stale_sequence", { highWaterSequence: latest.highWaterSequence });
|
|
1664
|
+
throw new RelayError(409, "sequence_exists");
|
|
1665
|
+
}
|
|
1666
|
+
await this.leaseStore.recordFence(device.accountId, uploadId, fencingToken);
|
|
1667
|
+
return json(201, {
|
|
1668
|
+
uploadId,
|
|
1669
|
+
sequence,
|
|
1670
|
+
byteSize,
|
|
1671
|
+
chunkSize,
|
|
1672
|
+
chunkCount,
|
|
1673
|
+
expiresAt: iso(now + limits.uploadTtlMs)
|
|
1674
|
+
});
|
|
1675
|
+
}
|
|
1676
|
+
/**
|
|
1677
|
+
* A sequence may not jump more than `maxSequenceJump` above the account's
|
|
1678
|
+
* high-water mark. Without the bound, one commit at the largest safe
|
|
1679
|
+
* integer would make every later upload `stale_sequence` with no way back
|
|
1680
|
+
* short of editing the database.
|
|
1681
|
+
*/
|
|
1682
|
+
assertSequenceStep(sequence, highWater) {
|
|
1683
|
+
const { maxSequenceJump } = this.policy.limits;
|
|
1684
|
+
if (sequence - highWater > maxSequenceJump) throw new RelayError(400, "sequence_too_far", {
|
|
1685
|
+
highWaterSequence: highWater,
|
|
1686
|
+
maxSequenceJump
|
|
1687
|
+
});
|
|
1688
|
+
}
|
|
1689
|
+
async pendingUpload(device, uploadId) {
|
|
1690
|
+
const record = await this.store.getByUpload(device.accountId, uploadId);
|
|
1691
|
+
if (record?.status !== "pending") throw new RelayError(404, "no_upload");
|
|
1692
|
+
if (this.now() - record.createdAt > this.policy.limits.uploadTtlMs) {
|
|
1693
|
+
await this.removeSnapshot(record, device.handle, "pending");
|
|
1694
|
+
throw new RelayError(404, "no_upload");
|
|
1695
|
+
}
|
|
1696
|
+
return record;
|
|
1697
|
+
}
|
|
1698
|
+
async uploadChunk(request, device, uploadId, index) {
|
|
1699
|
+
const record = await this.pendingUpload(device, uploadId);
|
|
1700
|
+
if (index >= record.chunkCount) throw new RelayError(404, "no_chunk");
|
|
1701
|
+
const expected = expectedChunkBytes(record, index);
|
|
1702
|
+
const body = await readBoundedBody(request, this.policy.limits.maxChunkBytes, expected);
|
|
1703
|
+
const digest = sha256Hex(body);
|
|
1704
|
+
if (digest !== device.contentSha256) throw new RelayError(400, "content_digest_mismatch");
|
|
1705
|
+
const objectId = randomBytes(16).toString("hex");
|
|
1706
|
+
const key = chunkObjectKey(device.handle, record.uploadId, index, objectId);
|
|
1707
|
+
await this.objects.put(key, body);
|
|
1708
|
+
if (await this.store.addChunk(device.accountId, record.uploadId, {
|
|
1709
|
+
index,
|
|
1710
|
+
byteSize: body.length,
|
|
1711
|
+
sha256: digest,
|
|
1712
|
+
objectId
|
|
1713
|
+
})) {
|
|
1714
|
+
if (!await this.store.getByUpload(device.accountId, record.uploadId)) {
|
|
1715
|
+
if (await this.store.deleteChunkAttempt(record.uploadId, index, objectId)) await this.deleteObject(key, device.handle);
|
|
1716
|
+
throw new RelayError(404, "no_upload");
|
|
1717
|
+
}
|
|
1718
|
+
} else {
|
|
1719
|
+
await this.deleteObject(key, device.handle);
|
|
1720
|
+
const existing = await this.store.getChunk(record.uploadId, index);
|
|
1721
|
+
const current = await this.store.getByUpload(device.accountId, record.uploadId);
|
|
1722
|
+
if (existing?.sha256 !== digest || current?.status !== "pending") throw new RelayError(current?.status === "pending" ? 409 : 404, current?.status === "pending" ? "chunk_exists" : "no_upload");
|
|
1723
|
+
}
|
|
1724
|
+
return json(200, {
|
|
1725
|
+
index,
|
|
1726
|
+
byteSize: body.length,
|
|
1727
|
+
sha256: digest
|
|
1728
|
+
});
|
|
1729
|
+
}
|
|
1730
|
+
async commitUpload(device, uploadId) {
|
|
1731
|
+
const record = await this.pendingUpload(device, uploadId);
|
|
1732
|
+
const chunks = await this.store.listChunks(record.uploadId);
|
|
1733
|
+
if (!(chunks.length === record.chunkCount && chunks.every((chunk, position) => chunk.index === position && chunk.byteSize === expectedChunkBytes(record, position)))) throw new RelayError(409, "incomplete_upload");
|
|
1734
|
+
const whole = createHash("sha256");
|
|
1735
|
+
for (const chunk of chunks) {
|
|
1736
|
+
const bytes = await this.objects.get(chunkObjectKey(device.handle, record.uploadId, chunk.index, chunk.objectId));
|
|
1737
|
+
if (!bytes || sha256Hex(bytes) !== chunk.sha256) throw new RelayError(409, "incomplete_upload");
|
|
1738
|
+
whole.update(bytes);
|
|
1739
|
+
}
|
|
1740
|
+
if (whole.digest("hex") !== record.sha256) throw new RelayError(422, "snapshot_digest_mismatch");
|
|
1741
|
+
const leaseNow = this.now();
|
|
1742
|
+
const fence = await this.leaseStore.getFence(device.accountId, record.uploadId);
|
|
1743
|
+
if (leaseNow - record.createdAt > this.policy.limits.uploadTtlMs) {
|
|
1744
|
+
await this.removeSnapshot(record, device.handle, "pending");
|
|
1745
|
+
throw new RelayError(404, "no_upload");
|
|
1746
|
+
}
|
|
1747
|
+
if (fence === null || !await this.leaseStore.advanceHead(device.accountId, device.deviceId, record.uploadId, fence, record.sequence, leaseNow)) {
|
|
1748
|
+
const refusal = await this.leases.mustRestore(device.accountId);
|
|
1749
|
+
await this.removeSnapshot(record, device.handle, "pending");
|
|
1750
|
+
throw refusalError(refusal);
|
|
1751
|
+
}
|
|
1752
|
+
if (!await this.store.advanceHighWater(device.accountId, record.sequence, this.policy.limits.maxSequenceJump)) {
|
|
1753
|
+
await this.removeSnapshot(record, device.handle, "pending");
|
|
1754
|
+
const latest = await this.store.getUsage(device.accountId);
|
|
1755
|
+
if (latest && record.sequence > latest.highWaterSequence) this.assertSequenceStep(record.sequence, latest.highWaterSequence);
|
|
1756
|
+
throw new RelayError(409, "stale_sequence");
|
|
1757
|
+
}
|
|
1758
|
+
const now = this.now();
|
|
1759
|
+
if (!await this.store.markCommitted(device.accountId, record.uploadId, now)) throw new RelayError(409, "no_upload");
|
|
1760
|
+
await this.leaseStore.deleteFence(record.uploadId);
|
|
1761
|
+
await this.applyRetention(device.accountId, device.handle, now);
|
|
1762
|
+
const committed = await this.store.getCommitted(device.accountId, record.sequence);
|
|
1763
|
+
return json(201, committed ? snapshotView(committed) : { sequence: record.sequence });
|
|
1764
|
+
}
|
|
1765
|
+
/**
|
|
1766
|
+
* Device-initiated delete. The account's last committed snapshot is never
|
|
1767
|
+
* removable from the device API: a compromised or mistaken device would
|
|
1768
|
+
* otherwise erase the only restorable copy. The refusal and every removal
|
|
1769
|
+
* are audited by device and sequence; the operator can still act through
|
|
1770
|
+
* the admin listener.
|
|
1771
|
+
*/
|
|
1772
|
+
async deleteSnapshotForDevice(record, device) {
|
|
1773
|
+
const removed = await this.leaseStore.deleteCommittedBelowHead(record.accountId, record.uploadId);
|
|
1774
|
+
if (!removed) {
|
|
1775
|
+
if (!await this.store.getCommitted(record.accountId, record.sequence)) throw new RelayError(404, "no_snapshot");
|
|
1776
|
+
const sole = (await this.store.listSnapshots(record.accountId, "committed")).length <= 1;
|
|
1777
|
+
this.logger.warn("snapshot_delete_refused", {
|
|
1778
|
+
account: device.handle,
|
|
1779
|
+
device: device.deviceId,
|
|
1780
|
+
sequence: record.sequence,
|
|
1781
|
+
reason: sole ? "last_committed" : "head"
|
|
1782
|
+
});
|
|
1783
|
+
throw new RelayError(409, sole ? "last_snapshot" : "head_snapshot", { sequence: record.sequence });
|
|
1784
|
+
}
|
|
1785
|
+
await this.discardRemoved(removed, device.handle);
|
|
1786
|
+
this.logger.info("snapshot_deleted", {
|
|
1787
|
+
account: device.handle,
|
|
1788
|
+
device: device.deviceId,
|
|
1789
|
+
sequence: removed.sequence
|
|
1790
|
+
});
|
|
1791
|
+
return json(204, null);
|
|
1792
|
+
}
|
|
1793
|
+
async downloadChunk(device, sequence, index) {
|
|
1794
|
+
const record = await this.store.getCommitted(device.accountId, sequence);
|
|
1795
|
+
if (!record) throw new RelayError(404, "no_snapshot");
|
|
1796
|
+
const chunk = await this.store.getChunk(record.uploadId, index);
|
|
1797
|
+
if (!chunk) throw new RelayError(404, "no_chunk");
|
|
1798
|
+
const bytes = await this.objects.get(chunkObjectKey(device.handle, record.uploadId, index, chunk.objectId));
|
|
1799
|
+
if (!bytes || sha256Hex(bytes) !== chunk.sha256) {
|
|
1800
|
+
this.logger.error("stored_chunk_unreadable", {
|
|
1801
|
+
account: device.handle,
|
|
1802
|
+
sequence,
|
|
1803
|
+
index
|
|
1804
|
+
});
|
|
1805
|
+
throw new RelayError(500, "storage_integrity");
|
|
1806
|
+
}
|
|
1807
|
+
return new Response(bytes, {
|
|
1808
|
+
status: 200,
|
|
1809
|
+
headers: {
|
|
1810
|
+
"content-type": "application/octet-stream",
|
|
1811
|
+
"content-length": String(bytes.length),
|
|
1812
|
+
"cache-control": "no-store",
|
|
1813
|
+
"x-vault-chunk-sha256": chunk.sha256
|
|
1814
|
+
}
|
|
1815
|
+
});
|
|
1816
|
+
}
|
|
1817
|
+
/**
|
|
1818
|
+
* Remove a snapshot or upload while it still has `status`: the row first (so
|
|
1819
|
+
* it can never be listed or served half-deleted, and a concurrent publish
|
|
1820
|
+
* or abort cannot be undone), then counters, then chunk rows and objects.
|
|
1821
|
+
*/
|
|
1822
|
+
async removeSnapshot(record, handle, status) {
|
|
1823
|
+
const removed = await this.store.deleteSnapshotRow(record.accountId, record.uploadId, status);
|
|
1824
|
+
if (!removed) return false;
|
|
1825
|
+
await this.discardRemoved(removed, handle);
|
|
1826
|
+
return true;
|
|
1827
|
+
}
|
|
1828
|
+
/** Counters, fence, chunk rows and objects of a snapshot row already deleted. */
|
|
1829
|
+
async discardRemoved(removed, handle) {
|
|
1830
|
+
await this.store.release(removed.accountId, removed.byteSize);
|
|
1831
|
+
await this.leaseStore.deleteFence(removed.uploadId);
|
|
1832
|
+
const chunks = await this.store.deleteChunks(removed.uploadId);
|
|
1833
|
+
for (const chunk of chunks) await this.deleteObject(chunkObjectKey(handle, removed.uploadId, chunk.index, chunk.objectId), handle);
|
|
1834
|
+
}
|
|
1835
|
+
async deleteObject(key, handle) {
|
|
1836
|
+
try {
|
|
1837
|
+
await this.objects.delete(key);
|
|
1838
|
+
} catch (error) {
|
|
1839
|
+
this.logger.warn("object_delete_failed", {
|
|
1840
|
+
account: handle,
|
|
1841
|
+
error: error instanceof Error ? error.name : "unknown"
|
|
1842
|
+
});
|
|
1843
|
+
}
|
|
1844
|
+
}
|
|
1845
|
+
/** Keep the newest `keepLast` committed snapshots plus any within the window. */
|
|
1846
|
+
async applyRetention(accountId, handle, now) {
|
|
1847
|
+
const { keepLast, keepWithinMs } = this.policy.retention;
|
|
1848
|
+
const committed = await this.store.listSnapshots(accountId, "committed");
|
|
1849
|
+
for (const [position, record] of committed.entries()) {
|
|
1850
|
+
if (position < keepLast) continue;
|
|
1851
|
+
if (now - (record.committedAt ?? record.createdAt) <= keepWithinMs) continue;
|
|
1852
|
+
await this.removeSnapshot(record, handle, "committed");
|
|
1853
|
+
}
|
|
1854
|
+
}
|
|
1855
|
+
/** Abort pending uploads older than the upload TTL; returns how many. */
|
|
1856
|
+
async sweepExpired(now = this.now()) {
|
|
1857
|
+
const stale = await this.store.stalePending(now - this.policy.limits.uploadTtlMs, SWEEP_BATCH);
|
|
1858
|
+
for (const record of stale) await this.removeSnapshot(record, accountHandle(record.accountId), "pending");
|
|
1859
|
+
await this.store.pruneNonces(now);
|
|
1860
|
+
return stale.length;
|
|
1861
|
+
}
|
|
1862
|
+
/**
|
|
1863
|
+
* Operator sweep of lapsed accounts. Two bounded phases per run:
|
|
1864
|
+
*
|
|
1865
|
+
* 1. *Discovery* walks accounts that hold committed snapshots but have no
|
|
1866
|
+
* lapse record (`cursor`/`nextCursor` resume it) and records those whose
|
|
1867
|
+
* entitlement is lapsed, so an account whose devices never call again
|
|
1868
|
+
* still starts its window.
|
|
1869
|
+
* 2. *Deletion* takes lapses whose window (`lapseRetentionDays` from the
|
|
1870
|
+
* first observation) has elapsed, re-checks the entitlement, and deletes
|
|
1871
|
+
* the account's snapshots through the normal removal path. An account
|
|
1872
|
+
* that is entitled again has its lapse cleared and keeps its data.
|
|
1873
|
+
*
|
|
1874
|
+
* Every account deleted gets a `lapse_sweep_started` audit record (what is
|
|
1875
|
+
* about to go) before anything is removed and a `lapse_sweep_completed`
|
|
1876
|
+
* record (what went) after, so a crash leaves the intent on record and the
|
|
1877
|
+
* next run finishes the job. A dry run changes and audits nothing.
|
|
1878
|
+
*/
|
|
1879
|
+
async sweepLapsedAccounts(options) {
|
|
1880
|
+
const operator = options.operator;
|
|
1881
|
+
if (typeof operator !== "string" || operator.length < 1 || operator.length > 128) throw new RelayError(400, "invalid_request", { field: "operator" });
|
|
1882
|
+
const limit = options.limit ?? DEFAULT_LAPSE_SWEEP_ACCOUNTS;
|
|
1883
|
+
if (!Number.isSafeInteger(limit) || limit < 1 || limit > MAX_LAPSE_SWEEP_ACCOUNTS) throw new RelayError(400, "invalid_request", { field: "limit" });
|
|
1884
|
+
const cursor = options.cursor ?? "";
|
|
1885
|
+
if (cursor !== "" && !isIdentifier(cursor)) throw new RelayError(400, "invalid_request", { field: "cursor" });
|
|
1886
|
+
const dryRun = options.dryRun === true;
|
|
1887
|
+
const now = options.now ?? this.now();
|
|
1888
|
+
const retentionMs = this.policy.retention.lapseRetentionMs;
|
|
1889
|
+
const result = {
|
|
1890
|
+
dryRun,
|
|
1891
|
+
operator,
|
|
1892
|
+
discovered: 0,
|
|
1893
|
+
swept: [],
|
|
1894
|
+
skipped: [],
|
|
1895
|
+
nextCursor: null,
|
|
1896
|
+
more: false
|
|
1897
|
+
};
|
|
1898
|
+
const unrecorded = await this.store.listUnrecordedAccounts(cursor, limit);
|
|
1899
|
+
for (const accountId of unrecorded) try {
|
|
1900
|
+
if ((await this.checkEntitlement(accountId))?.code !== "feature_upgrade_required") continue;
|
|
1901
|
+
if (!dryRun) await this.store.recordLapse(accountId, now);
|
|
1902
|
+
result.discovered += 1;
|
|
1903
|
+
} catch {
|
|
1904
|
+
result.skipped.push({
|
|
1905
|
+
account: accountHandle(accountId),
|
|
1906
|
+
reason: "entitlement_unavailable"
|
|
1907
|
+
});
|
|
1908
|
+
}
|
|
1909
|
+
if (unrecorded.length === limit) result.nextCursor = unrecorded[unrecorded.length - 1] ?? null;
|
|
1910
|
+
const due = await this.store.listDueLapses(now - retentionMs, limit);
|
|
1911
|
+
result.more = due.length === limit;
|
|
1912
|
+
for (const lapse of due) {
|
|
1913
|
+
const handle = accountHandle(lapse.accountId);
|
|
1914
|
+
let denial;
|
|
1915
|
+
try {
|
|
1916
|
+
denial = await this.checkEntitlement(lapse.accountId);
|
|
1917
|
+
} catch {
|
|
1918
|
+
result.skipped.push({
|
|
1919
|
+
account: handle,
|
|
1920
|
+
reason: "entitlement_unavailable"
|
|
1921
|
+
});
|
|
1922
|
+
continue;
|
|
1923
|
+
}
|
|
1924
|
+
if (!denial) {
|
|
1925
|
+
if (!dryRun) await this.store.clearLapse(lapse.accountId);
|
|
1926
|
+
result.skipped.push({
|
|
1927
|
+
account: handle,
|
|
1928
|
+
reason: "entitled"
|
|
1929
|
+
});
|
|
1930
|
+
continue;
|
|
1931
|
+
}
|
|
1932
|
+
if (denial.code !== "feature_upgrade_required") {
|
|
1933
|
+
result.skipped.push({
|
|
1934
|
+
account: handle,
|
|
1935
|
+
reason: "not_lapsed"
|
|
1936
|
+
});
|
|
1937
|
+
continue;
|
|
1938
|
+
}
|
|
1939
|
+
const records = await this.store.listSnapshots(lapse.accountId);
|
|
1940
|
+
const planned = {
|
|
1941
|
+
account: handle,
|
|
1942
|
+
lapsedAt: iso(lapse.lapsedAt),
|
|
1943
|
+
retentionEndedAt: iso(lapse.lapsedAt + retentionMs),
|
|
1944
|
+
snapshots: records.length,
|
|
1945
|
+
bytes: records.reduce((total, record) => total + record.byteSize, 0)
|
|
1946
|
+
};
|
|
1947
|
+
if (dryRun) {
|
|
1948
|
+
result.swept.push(planned);
|
|
1949
|
+
continue;
|
|
1950
|
+
}
|
|
1951
|
+
await this.audit("lapse_sweep_started", operator, handle, planned, now);
|
|
1952
|
+
let removedSnapshots = 0;
|
|
1953
|
+
let removedBytes = 0;
|
|
1954
|
+
for (const record of records) if (await this.removeSnapshot(record, handle, record.status)) {
|
|
1955
|
+
removedSnapshots += 1;
|
|
1956
|
+
removedBytes += record.byteSize;
|
|
1957
|
+
}
|
|
1958
|
+
await this.store.markLapseSwept(lapse.accountId, this.now());
|
|
1959
|
+
const done = {
|
|
1960
|
+
...planned,
|
|
1961
|
+
snapshots: removedSnapshots,
|
|
1962
|
+
bytes: removedBytes
|
|
1963
|
+
};
|
|
1964
|
+
await this.audit("lapse_sweep_completed", operator, handle, done, this.now());
|
|
1965
|
+
this.logger.warn("lapse_swept", {
|
|
1966
|
+
account: handle,
|
|
1967
|
+
snapshots: removedSnapshots,
|
|
1968
|
+
bytes: removedBytes
|
|
1969
|
+
});
|
|
1970
|
+
result.swept.push(done);
|
|
1971
|
+
}
|
|
1972
|
+
return result;
|
|
1973
|
+
}
|
|
1974
|
+
async audit(action, operator, handle, summary, at) {
|
|
1975
|
+
const { snapshots, bytes, ...rest } = summary;
|
|
1976
|
+
await this.store.appendAudit({
|
|
1977
|
+
id: randomUUID(),
|
|
1978
|
+
at,
|
|
1979
|
+
action,
|
|
1980
|
+
operator,
|
|
1981
|
+
accountHandle: handle,
|
|
1982
|
+
snapshots,
|
|
1983
|
+
bytes,
|
|
1984
|
+
detail: JSON.stringify(rest)
|
|
1985
|
+
});
|
|
1986
|
+
}
|
|
1987
|
+
/**
|
|
1988
|
+
* Register a device's Ed25519 public key (raw 32 bytes, base64url) for an
|
|
1989
|
+
* account. Idempotent for the same key; a revoked device id or a different
|
|
1990
|
+
* key under an existing id is a conflict.
|
|
1991
|
+
*/
|
|
1992
|
+
async registerDevice(accountId, deviceId, publicKey) {
|
|
1993
|
+
if (!isIdentifier(accountId) || !isIdentifier(deviceId)) throw new Error("vault relay: invalid account or device id");
|
|
1994
|
+
if (!importDevicePublicKey(publicKey)) throw new Error("vault relay: invalid Ed25519 public key");
|
|
1995
|
+
const now = this.now();
|
|
1996
|
+
await this.store.ensureAccount(accountId, now);
|
|
1997
|
+
return this.store.registerDevice(accountId, deviceId, publicKey, now);
|
|
1998
|
+
}
|
|
1999
|
+
/** Revoke a device; its signatures fail from the next request on. */
|
|
2000
|
+
async revokeDevice(accountId, deviceId) {
|
|
2001
|
+
if (!isIdentifier(accountId) || !isIdentifier(deviceId)) return false;
|
|
2002
|
+
const revoked = await this.store.revokeDevice(accountId, deviceId, this.now());
|
|
2003
|
+
await this.leaseStore.clearHolder(accountId, deviceId);
|
|
2004
|
+
return revoked;
|
|
2005
|
+
}
|
|
2006
|
+
/**
|
|
2007
|
+
* Operator reset of an account's sequence high-water mark, for an account
|
|
2008
|
+
* whose mark was pushed out of reach (every later upload is
|
|
2009
|
+
* `stale_sequence`). The new mark must not be above the current one. Any
|
|
2010
|
+
* snapshot or pending upload above it is refused with its sequence listed,
|
|
2011
|
+
* unless `removeSnapshotsAbove` is set, in which case those rows and their
|
|
2012
|
+
* objects are deleted first. Every call that changes anything is logged with
|
|
2013
|
+
* the previous mark, the new mark, and the removed sequences.
|
|
2014
|
+
*/
|
|
2015
|
+
async resetHighWater(accountId, highWaterSequence, options = {}) {
|
|
2016
|
+
if (!isIdentifier(accountId)) throw new RelayError(404, "not_found");
|
|
2017
|
+
if (!Number.isSafeInteger(highWaterSequence) || highWaterSequence < 0) throw new RelayError(400, "invalid_request", { field: "highWaterSequence" });
|
|
2018
|
+
const usage = await this.store.getUsage(accountId);
|
|
2019
|
+
if (!usage) throw new RelayError(404, "no_account");
|
|
2020
|
+
if (highWaterSequence > usage.highWaterSequence) throw new RelayError(409, "not_a_reset", { highWaterSequence: usage.highWaterSequence });
|
|
2021
|
+
const handle = accountHandle(accountId);
|
|
2022
|
+
const above = (await this.store.listSnapshots(accountId)).filter((record) => record.sequence > highWaterSequence);
|
|
2023
|
+
if (above.length > 0 && options.removeSnapshotsAbove !== true) throw new RelayError(409, "snapshots_above_high_water", { sequences: above.map((record) => record.sequence) });
|
|
2024
|
+
for (const record of above) await this.removeSnapshot(record, handle, record.status);
|
|
2025
|
+
if (above.length > 0) this.logger.warn("high_water_reset_removed", {
|
|
2026
|
+
account: handle,
|
|
2027
|
+
removed: above.length,
|
|
2028
|
+
removedSequences: above.map((record) => record.sequence).join(",")
|
|
2029
|
+
});
|
|
2030
|
+
if (!await this.store.resetHighWater(accountId, highWaterSequence)) throw new RelayError(409, "high_water_changed");
|
|
2031
|
+
await this.leaseStore.resetHead(accountId);
|
|
2032
|
+
this.logger.warn("high_water_reset", {
|
|
2033
|
+
account: handle,
|
|
2034
|
+
previous: usage.highWaterSequence,
|
|
2035
|
+
to: highWaterSequence,
|
|
2036
|
+
removed: above.length,
|
|
2037
|
+
removedSequences: above.map((record) => record.sequence).join(",")
|
|
2038
|
+
});
|
|
2039
|
+
return {
|
|
2040
|
+
previous: usage.highWaterSequence,
|
|
2041
|
+
highWaterSequence,
|
|
2042
|
+
removed: above.map((record) => record.sequence)
|
|
2043
|
+
};
|
|
2044
|
+
}
|
|
2045
|
+
/**
|
|
2046
|
+
* Mark `accountId` replaced by `supersededBy` (#4817), for the account
|
|
2047
|
+
* layer once it has provisioned the new account and registered a device in
|
|
2048
|
+
* it. Admin only: no device route can set or clear it, so a stolen device
|
|
2049
|
+
* cannot silence or redirect an account. The old account's snapshots stay
|
|
2050
|
+
* in place (readable by explicit sequence); it takes no new writes and
|
|
2051
|
+
* offers nothing to restore-latest. Idempotent for the same target.
|
|
2052
|
+
*/
|
|
2053
|
+
async supersedeAccount(accountId, supersededBy) {
|
|
2054
|
+
if (!isIdentifier(accountId)) throw new RelayError(404, "not_found");
|
|
2055
|
+
if (!isIdentifier(supersededBy) || supersededBy === accountId) throw new RelayError(400, "invalid_request", { field: "supersededBy" });
|
|
2056
|
+
if (!await this.store.getUsage(accountId)) throw new RelayError(404, "no_account");
|
|
2057
|
+
if (await this.store.isSupersessionTarget(accountId)) throw new RelayError(409, "account_is_target");
|
|
2058
|
+
if (await this.store.countActiveDevices(supersededBy) < 1) throw new RelayError(409, "target_not_ready");
|
|
2059
|
+
if (await this.store.getSupersession(supersededBy)) throw new RelayError(409, "target_superseded");
|
|
2060
|
+
const result = await this.store.setSupersession(accountId, supersededBy, this.now());
|
|
2061
|
+
if (result === "conflict") throw new RelayError(409, "already_superseded");
|
|
2062
|
+
if (result === "superseded") this.logger.warn("account_superseded", {
|
|
2063
|
+
account: accountHandle(accountId),
|
|
2064
|
+
supersededBy: accountHandle(supersededBy)
|
|
2065
|
+
});
|
|
2066
|
+
return result;
|
|
2067
|
+
}
|
|
2068
|
+
/** Undo {@link supersedeAccount}, for an operator mistake. */
|
|
2069
|
+
async clearSupersession(accountId) {
|
|
2070
|
+
if (!isIdentifier(accountId)) return false;
|
|
2071
|
+
const cleared = await this.store.clearSupersession(accountId);
|
|
2072
|
+
if (cleared) this.logger.warn("account_supersession_cleared", { account: accountHandle(accountId) });
|
|
2073
|
+
return cleared;
|
|
2074
|
+
}
|
|
2075
|
+
/** Sizes, dates, and device state for one account; never content. */
|
|
2076
|
+
async operatorView(accountId) {
|
|
2077
|
+
const [usage, snapshots, devices, supersession, lapse] = await Promise.all([
|
|
2078
|
+
this.store.getUsage(accountId),
|
|
2079
|
+
this.store.listSnapshots(accountId),
|
|
2080
|
+
this.store.listDevices(accountId),
|
|
2081
|
+
this.store.getSupersession(accountId),
|
|
2082
|
+
this.store.getLapse(accountId)
|
|
2083
|
+
]);
|
|
2084
|
+
return {
|
|
2085
|
+
usage,
|
|
2086
|
+
lapsedAt: lapse ? iso(lapse.lapsedAt) : null,
|
|
2087
|
+
lapseRetentionEndsAt: lapse ? iso(lapse.lapsedAt + this.policy.retention.lapseRetentionMs) : null,
|
|
2088
|
+
supersededBy: supersession?.supersededBy ?? null,
|
|
2089
|
+
snapshots: snapshots.map((record) => ({
|
|
2090
|
+
sequence: record.sequence,
|
|
2091
|
+
status: record.status,
|
|
2092
|
+
byteSize: record.byteSize,
|
|
2093
|
+
chunkCount: record.chunkCount,
|
|
2094
|
+
sha256: record.sha256,
|
|
2095
|
+
createdAt: iso(record.createdAt),
|
|
2096
|
+
committedAt: record.committedAt === null ? null : iso(record.committedAt)
|
|
2097
|
+
})),
|
|
2098
|
+
devices: devices.map((device) => ({
|
|
2099
|
+
deviceId: device.deviceId,
|
|
2100
|
+
status: device.status,
|
|
2101
|
+
registeredAt: iso(device.registeredAt),
|
|
2102
|
+
revokedAt: device.revokedAt === null ? null : iso(device.revokedAt)
|
|
2103
|
+
}))
|
|
2104
|
+
};
|
|
2105
|
+
}
|
|
2106
|
+
adminAuthorized(request) {
|
|
2107
|
+
if (!this.adminTokenHash) return false;
|
|
2108
|
+
const header = request.headers.get("authorization") ?? "";
|
|
2109
|
+
const match = /^Bearer ([^\s]{1,512})$/.exec(header);
|
|
2110
|
+
if (!match) return false;
|
|
2111
|
+
return timingSafeEqual(createHash("sha256").update(match[1]).digest(), this.adminTokenHash);
|
|
2112
|
+
}
|
|
2113
|
+
/** Operator and account-layer handler (private listener). */
|
|
2114
|
+
async handleAdmin(request) {
|
|
2115
|
+
const url = new URL(request.url);
|
|
2116
|
+
const method = request.method.toUpperCase();
|
|
2117
|
+
const path = url.pathname;
|
|
2118
|
+
let status = 500;
|
|
2119
|
+
try {
|
|
2120
|
+
if (method === "GET") {
|
|
2121
|
+
const health = await this.health(path);
|
|
2122
|
+
if (health) return health;
|
|
2123
|
+
}
|
|
2124
|
+
if (!this.adminTokenHash) throw new RelayError(404, "not_found");
|
|
2125
|
+
if (!this.adminAuthorized(request)) throw new RelayError(401, "unauthorized");
|
|
2126
|
+
const response = await this.dispatchAdmin(method, path, request);
|
|
2127
|
+
status = response.status;
|
|
2128
|
+
return response;
|
|
2129
|
+
} catch (error) {
|
|
2130
|
+
const response = this.toErrorResponse(error);
|
|
2131
|
+
status = response.status;
|
|
2132
|
+
return response;
|
|
2133
|
+
} finally {
|
|
2134
|
+
this.logger.info("admin_request", {
|
|
2135
|
+
method,
|
|
2136
|
+
status
|
|
2137
|
+
});
|
|
2138
|
+
}
|
|
2139
|
+
}
|
|
2140
|
+
/**
|
|
2141
|
+
* Operator identity for the audit trail: the optional `x-vault-operator`
|
|
2142
|
+
* label the caller states, plus a fingerprint of the admin token that
|
|
2143
|
+
* authenticated the request (a hash prefix, never the token).
|
|
2144
|
+
*/
|
|
2145
|
+
operatorIdentity(request) {
|
|
2146
|
+
const fingerprint = `token:${this.adminTokenHash?.toString("hex").slice(0, 12) ?? "none"}`;
|
|
2147
|
+
const label = request.headers.get("x-vault-operator");
|
|
2148
|
+
if (label === null) return fingerprint;
|
|
2149
|
+
if (!OPERATOR_LABEL.test(label)) throw new RelayError(400, "invalid_request", { field: "x-vault-operator" });
|
|
2150
|
+
return `${label}/${fingerprint}`;
|
|
2151
|
+
}
|
|
2152
|
+
async adminLapseSweep(request) {
|
|
2153
|
+
const operator = this.operatorIdentity(request);
|
|
2154
|
+
const bytes = await readBoundedBody(request, MAX_JSON_BODY_BYTES);
|
|
2155
|
+
const input = bytes.length === 0 ? {} : parseJsonObject(bytes);
|
|
2156
|
+
if (input.dryRun !== void 0 && typeof input.dryRun !== "boolean") throw new RelayError(400, "invalid_request", { field: "dryRun" });
|
|
2157
|
+
if (input.limit !== void 0 && typeof input.limit !== "number") throw new RelayError(400, "invalid_request", { field: "limit" });
|
|
2158
|
+
if (input.cursor !== void 0 && typeof input.cursor !== "string") throw new RelayError(400, "invalid_request", { field: "cursor" });
|
|
2159
|
+
return this.sweepLapsedAccounts({
|
|
2160
|
+
operator,
|
|
2161
|
+
dryRun: input.dryRun === true,
|
|
2162
|
+
...input.limit !== void 0 ? { limit: input.limit } : {},
|
|
2163
|
+
...input.cursor !== void 0 ? { cursor: input.cursor } : {}
|
|
2164
|
+
});
|
|
2165
|
+
}
|
|
2166
|
+
async dispatchAdmin(method, path, request) {
|
|
2167
|
+
if (path === "/admin/v1/sweep" && method === "POST") return json(200, { expired: await this.sweepExpired() });
|
|
2168
|
+
if (path === "/admin/v1/lapse-sweep" && method === "POST") return json(200, await this.adminLapseSweep(request));
|
|
2169
|
+
let m = /^\/admin\/v1\/accounts\/([^/]+)$/.exec(path);
|
|
2170
|
+
if (m && method === "GET") {
|
|
2171
|
+
if (!isIdentifier(m[1])) throw new RelayError(404, "not_found");
|
|
2172
|
+
return json(200, await this.operatorView(m[1]));
|
|
2173
|
+
}
|
|
2174
|
+
m = /^\/admin\/v1\/accounts\/([^/]+)\/reconcile$/.exec(path);
|
|
2175
|
+
if (m && method === "POST") {
|
|
2176
|
+
if (!isIdentifier(m[1])) throw new RelayError(404, "not_found");
|
|
2177
|
+
await this.store.reconcileUsage(m[1]);
|
|
2178
|
+
return json(200, { usage: await this.store.getUsage(m[1]) });
|
|
2179
|
+
}
|
|
2180
|
+
m = /^\/admin\/v1\/accounts\/([^/]+)\/high-water$/.exec(path);
|
|
2181
|
+
if (m && method === "POST") {
|
|
2182
|
+
if (!isIdentifier(m[1])) throw new RelayError(404, "not_found");
|
|
2183
|
+
const input = parseJsonObject(await readBoundedBody(request, MAX_JSON_BODY_BYTES));
|
|
2184
|
+
if (input.removeSnapshotsAbove !== void 0 && typeof input.removeSnapshotsAbove !== "boolean") throw new RelayError(400, "invalid_request", { field: "removeSnapshotsAbove" });
|
|
2185
|
+
return json(200, await this.resetHighWater(m[1], input.highWaterSequence, { removeSnapshotsAbove: input.removeSnapshotsAbove === true }));
|
|
2186
|
+
}
|
|
2187
|
+
m = /^\/admin\/v1\/accounts\/([^/]+)\/superseded-by$/.exec(path);
|
|
2188
|
+
if (m && (method === "PUT" || method === "DELETE")) {
|
|
2189
|
+
if (!isIdentifier(m[1])) throw new RelayError(404, "not_found");
|
|
2190
|
+
if (method === "DELETE") {
|
|
2191
|
+
if (!await this.clearSupersession(m[1])) throw new RelayError(404, "no_supersession");
|
|
2192
|
+
return json(204, null);
|
|
2193
|
+
}
|
|
2194
|
+
const input = parseJsonObject(await readBoundedBody(request, MAX_JSON_BODY_BYTES));
|
|
2195
|
+
if (typeof input.supersededBy !== "string") throw new RelayError(400, "invalid_request", { field: "supersededBy" });
|
|
2196
|
+
const result = await this.supersedeAccount(m[1], input.supersededBy);
|
|
2197
|
+
return json(result === "superseded" ? 201 : 200, { result });
|
|
2198
|
+
}
|
|
2199
|
+
m = /^\/admin\/v1\/accounts\/([^/]+)\/devices\/([^/]+)$/.exec(path);
|
|
2200
|
+
if (m) {
|
|
2201
|
+
const [, accountId, deviceId] = m;
|
|
2202
|
+
if (!isIdentifier(accountId) || !isIdentifier(deviceId)) throw new RelayError(404, "not_found");
|
|
2203
|
+
if (method === "PUT") {
|
|
2204
|
+
const input = parseJsonObject(await readBoundedBody(request, MAX_JSON_BODY_BYTES));
|
|
2205
|
+
if (typeof input.publicKey !== "string" || !importDevicePublicKey(input.publicKey)) throw new RelayError(400, "invalid_request", { field: "publicKey" });
|
|
2206
|
+
const result = await this.registerDevice(accountId, deviceId, input.publicKey);
|
|
2207
|
+
if (result === "conflict") throw new RelayError(409, "device_conflict");
|
|
2208
|
+
return json(result === "registered" ? 201 : 200, { result });
|
|
2209
|
+
}
|
|
2210
|
+
if (method === "DELETE") {
|
|
2211
|
+
if (!await this.revokeDevice(accountId, deviceId)) throw new RelayError(404, "no_active_device");
|
|
2212
|
+
return json(204, null);
|
|
2213
|
+
}
|
|
2214
|
+
}
|
|
2215
|
+
throw new RelayError(404, "not_found");
|
|
2216
|
+
}
|
|
2217
|
+
};
|
|
2218
|
+
//#endregion
|
|
2219
|
+
//#region src/server.ts
|
|
2220
|
+
var DAY_MS = 1440 * 60 * 1e3;
|
|
2221
|
+
var VaultRelayConfigError = class extends Error {
|
|
2222
|
+
constructor(message) {
|
|
2223
|
+
super(message);
|
|
2224
|
+
this.name = "VaultRelayConfigError";
|
|
2225
|
+
}
|
|
2226
|
+
};
|
|
2227
|
+
function required(env, name) {
|
|
2228
|
+
const value = env[name]?.trim();
|
|
2229
|
+
if (!value) throw new VaultRelayConfigError(`${name} is required`);
|
|
2230
|
+
return value;
|
|
2231
|
+
}
|
|
2232
|
+
function integer(env, name, fallback) {
|
|
2233
|
+
const raw = env[name]?.trim();
|
|
2234
|
+
if (!raw) {
|
|
2235
|
+
if (fallback === void 0) throw new VaultRelayConfigError(`${name} is required`);
|
|
2236
|
+
return fallback;
|
|
2237
|
+
}
|
|
2238
|
+
if (!/^[0-9]{1,16}$/.test(raw) || !Number.isSafeInteger(Number(raw))) throw new VaultRelayConfigError(`${name} must be a non-negative integer`);
|
|
2239
|
+
return Number(raw);
|
|
2240
|
+
}
|
|
2241
|
+
function loadServerConfig(env = process.env) {
|
|
2242
|
+
const kind = required(env, "VAULT_RELAY_STORAGE");
|
|
2243
|
+
let storage;
|
|
2244
|
+
if (kind === "filesystem") storage = {
|
|
2245
|
+
kind,
|
|
2246
|
+
path: required(env, "VAULT_RELAY_STORAGE_PATH")
|
|
2247
|
+
};
|
|
2248
|
+
else if (kind === "s3") storage = {
|
|
2249
|
+
kind,
|
|
2250
|
+
bucket: required(env, "VAULT_RELAY_S3_BUCKET"),
|
|
2251
|
+
endpoint: required(env, "VAULT_RELAY_S3_ENDPOINT"),
|
|
2252
|
+
region: required(env, "VAULT_RELAY_S3_REGION"),
|
|
2253
|
+
accessKeyId: required(env, "VAULT_RELAY_S3_ACCESS_KEY_ID"),
|
|
2254
|
+
secretAccessKey: required(env, "VAULT_RELAY_S3_SECRET_ACCESS_KEY")
|
|
2255
|
+
};
|
|
2256
|
+
else throw new VaultRelayConfigError("VAULT_RELAY_STORAGE must be \"filesystem\" or \"s3\"");
|
|
2257
|
+
const adminToken = env.VAULT_RELAY_ADMIN_TOKEN?.trim();
|
|
2258
|
+
return {
|
|
2259
|
+
databaseUrl: required(env, "VAULT_RELAY_DATABASE_URL"),
|
|
2260
|
+
entitlementsDatabaseUrl: required(env, "VAULT_RELAY_ENTITLEMENTS_DATABASE_URL"),
|
|
2261
|
+
storage,
|
|
2262
|
+
policy: {
|
|
2263
|
+
quota: {
|
|
2264
|
+
maxBytes: integer(env, "VAULT_RELAY_QUOTA_MAX_BYTES"),
|
|
2265
|
+
maxSnapshots: integer(env, "VAULT_RELAY_QUOTA_MAX_SNAPSHOTS")
|
|
2266
|
+
},
|
|
2267
|
+
retention: {
|
|
2268
|
+
keepLast: integer(env, "VAULT_RELAY_RETENTION_KEEP_LAST"),
|
|
2269
|
+
keepWithinMs: integer(env, "VAULT_RELAY_RETENTION_KEEP_WITHIN_DAYS") * DAY_MS,
|
|
2270
|
+
...env.VAULT_RELAY_LAPSE_RETENTION_DAYS ? { lapseRetentionDays: integer(env, "VAULT_RELAY_LAPSE_RETENTION_DAYS") } : {}
|
|
2271
|
+
},
|
|
2272
|
+
limits: {
|
|
2273
|
+
...env.VAULT_RELAY_MAX_CHUNK_BYTES ? { maxChunkBytes: integer(env, "VAULT_RELAY_MAX_CHUNK_BYTES") } : {},
|
|
2274
|
+
...env.VAULT_RELAY_UPLOAD_TTL_SECONDS ? { uploadTtlMs: integer(env, "VAULT_RELAY_UPLOAD_TTL_SECONDS") * 1e3 } : {},
|
|
2275
|
+
...env.VAULT_RELAY_LEASE_TTL_SECONDS ? { leaseTtlMs: integer(env, "VAULT_RELAY_LEASE_TTL_SECONDS") * 1e3 } : {},
|
|
2276
|
+
...env.VAULT_RELAY_MAX_CLOCK_SKEW_SECONDS ? { maxClockSkewMs: integer(env, "VAULT_RELAY_MAX_CLOCK_SKEW_SECONDS") * 1e3 } : {},
|
|
2277
|
+
...env.VAULT_RELAY_MAX_SEQUENCE_JUMP ? { maxSequenceJump: integer(env, "VAULT_RELAY_MAX_SEQUENCE_JUMP") } : {}
|
|
2278
|
+
}
|
|
2279
|
+
},
|
|
2280
|
+
port: integer(env, "VAULT_RELAY_PORT", 8080),
|
|
2281
|
+
adminPort: integer(env, "VAULT_RELAY_ADMIN_PORT", 8081),
|
|
2282
|
+
...adminToken ? { adminToken } : {}
|
|
2283
|
+
};
|
|
2284
|
+
}
|
|
2285
|
+
async function openRelayDatabase(url) {
|
|
2286
|
+
if (/^postgres(ql)?:\/\//.test(url)) return getDatabase({
|
|
2287
|
+
type: "postgres",
|
|
2288
|
+
url
|
|
2289
|
+
});
|
|
2290
|
+
return getDatabase({
|
|
2291
|
+
type: "sqlite",
|
|
2292
|
+
url
|
|
2293
|
+
});
|
|
2294
|
+
}
|
|
2295
|
+
async function openObjectStore(storage) {
|
|
2296
|
+
if (storage.kind === "filesystem") return new FilesystemObjectStore(storage.path);
|
|
2297
|
+
const { kind: _kind, ...options } = storage;
|
|
2298
|
+
return createS3ObjectStore(options);
|
|
2299
|
+
}
|
|
2300
|
+
async function runMigrations(config) {
|
|
2301
|
+
const db = await openRelayDatabase(config.databaseUrl);
|
|
2302
|
+
try {
|
|
2303
|
+
await migrateVaultRelayV1(db);
|
|
2304
|
+
await migrateVaultRelayLeasesV1(db);
|
|
2305
|
+
} finally {
|
|
2306
|
+
await db.close?.();
|
|
2307
|
+
}
|
|
2308
|
+
}
|
|
2309
|
+
async function startVaultRelayServer(config) {
|
|
2310
|
+
const db = await openRelayDatabase(config.databaseUrl);
|
|
2311
|
+
const entitlementsDb = config.entitlementsDatabaseUrl === config.databaseUrl ? db : await openRelayDatabase(config.entitlementsDatabaseUrl);
|
|
2312
|
+
const resolver = new FeatureResolver({ db: entitlementsDb });
|
|
2313
|
+
const relay = new VaultRelay({
|
|
2314
|
+
store: new VaultRelayStore(db),
|
|
2315
|
+
objects: await openObjectStore(config.storage),
|
|
2316
|
+
entitlements: {
|
|
2317
|
+
resolver,
|
|
2318
|
+
grantSource: () => SubscriptionFeatureSource.create({ db: entitlementsDb })
|
|
2319
|
+
},
|
|
2320
|
+
policy: config.policy,
|
|
2321
|
+
...config.adminToken ? { adminToken: config.adminToken } : {},
|
|
2322
|
+
logger: jsonLineLogger
|
|
2323
|
+
});
|
|
2324
|
+
const server = createRelayHttpServer((request) => relay.handle(request));
|
|
2325
|
+
await new Promise((resolve) => server.listen(config.port, resolve));
|
|
2326
|
+
let adminServer = null;
|
|
2327
|
+
if (config.adminToken) {
|
|
2328
|
+
adminServer = createRelayHttpServer((request) => relay.handleAdmin(request));
|
|
2329
|
+
await new Promise((resolve) => adminServer?.listen(config.adminPort, resolve));
|
|
2330
|
+
}
|
|
2331
|
+
const sweep = setInterval(() => {
|
|
2332
|
+
relay.sweepExpired().catch((error) => {
|
|
2333
|
+
jsonLineLogger.warn("sweep_failed", { error: error instanceof Error ? error.name : "unknown" });
|
|
2334
|
+
});
|
|
2335
|
+
}, 6e4);
|
|
2336
|
+
sweep.unref();
|
|
2337
|
+
jsonLineLogger.info("started", {
|
|
2338
|
+
port: config.port,
|
|
2339
|
+
admin: adminServer !== null,
|
|
2340
|
+
storage: config.storage.kind
|
|
2341
|
+
});
|
|
2342
|
+
return {
|
|
2343
|
+
relay,
|
|
2344
|
+
server,
|
|
2345
|
+
adminServer,
|
|
2346
|
+
async close() {
|
|
2347
|
+
clearInterval(sweep);
|
|
2348
|
+
await new Promise((resolve) => server.close(() => resolve()));
|
|
2349
|
+
if (adminServer) await new Promise((resolve) => adminServer?.close(() => resolve()));
|
|
2350
|
+
if (entitlementsDb !== db) await entitlementsDb.close?.();
|
|
2351
|
+
await db.close?.();
|
|
2352
|
+
}
|
|
2353
|
+
};
|
|
2354
|
+
}
|
|
2355
|
+
//#endregion
|
|
2356
|
+
export { HEADER_CONTENT_SHA256 as A, signRelayRequest as B, DEFAULT_MAX_CLOCK_SKEW_MS as C, MAX_LAPSE_RETENTION_DAYS as D, MAX_CHUNK_BYTES_CEILING as E, SIGNATURE_PROTOCOL as F, accountHandle as H, canonicalRequest as I, exportDevicePublicKey as L, HEADER_NONCE as M, HEADER_SIGNATURE as N, resolvePolicy as O, HEADER_TIMESTAMP as P, importDevicePrivateKey as R, DEFAULT_MAX_CHUNK_BYTES as S, DEFAULT_UPLOAD_TTL_MS as T, sha256Hex as U, EMPTY_SHA256 as V, VaultRelayLeaseStore as _, startVaultRelayServer as a, DEFAULT_LAPSE_RETENTION_DAYS as b, FilesObjectStore as c, createS3ObjectStore as d, createRelayHttpServer as f, VAULT_RELAY_LEASE_TABLES as g, migrateVaultRelayV1 as h, runMigrations as i, HEADER_DEVICE as j, HEADER_ACCOUNT as k, FilesystemObjectStore as l, VaultRelayStore as m, loadServerConfig as n, VaultRelay as o, VAULT_RELAY_TABLES as p, openRelayDatabase as r, jsonLineLogger as s, VaultRelayConfigError as t, assertObjectKey as u, migrateVaultRelayLeasesV1 as v, DEFAULT_MAX_SEQUENCE_JUMP as w, DEFAULT_LEASE_TTL_MS as x, MIN_LEASE_TTL_MS as y, importDevicePublicKey as z };
|
|
2357
|
+
|
|
2358
|
+
//# sourceMappingURL=server-0ITpMTMD.js.map
|