clinkx 1.2.0 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +32 -0
- package/README.md +4 -2
- package/clinkx-workflows/dist/architecture-arena/admission.d.ts +16 -0
- package/clinkx-workflows/dist/architecture-arena/admission.js +107 -0
- package/clinkx-workflows/dist/architecture-arena/admission.js.map +1 -0
- package/clinkx-workflows/dist/architecture-arena/contract.d.ts +1368 -0
- package/clinkx-workflows/dist/architecture-arena/contract.js +254 -0
- package/clinkx-workflows/dist/architecture-arena/contract.js.map +1 -0
- package/clinkx-workflows/dist/architecture-arena/runtime.d.ts +53 -0
- package/clinkx-workflows/dist/architecture-arena/runtime.js +511 -0
- package/clinkx-workflows/dist/architecture-arena/runtime.js.map +1 -0
- package/clinkx-workflows/dist/architecture-comparison/admission.d.ts +17 -1
- package/clinkx-workflows/dist/architecture-comparison/admission.js +27 -4
- package/clinkx-workflows/dist/architecture-comparison/admission.js.map +1 -1
- package/clinkx-workflows/dist/architecture-comparison/contract.d.ts +59 -62
- package/clinkx-workflows/dist/architecture-comparison/contract.js +4 -21
- package/clinkx-workflows/dist/architecture-comparison/contract.js.map +1 -1
- package/clinkx-workflows/dist/architecture-comparison/runtime.d.ts +4 -3
- package/clinkx-workflows/dist/architecture-comparison/runtime.js +22 -67
- package/clinkx-workflows/dist/architecture-comparison/runtime.js.map +1 -1
- package/clinkx-workflows/dist/artifacts.d.ts +19 -4
- package/clinkx-workflows/dist/artifacts.js +16 -7
- package/clinkx-workflows/dist/artifacts.js.map +1 -1
- package/clinkx-workflows/dist/backend.d.ts +30 -0
- package/clinkx-workflows/dist/clink-client.d.ts +8 -2
- package/clinkx-workflows/dist/clink-client.js +4 -2
- package/clinkx-workflows/dist/clink-client.js.map +1 -1
- package/clinkx-workflows/dist/context-preflight.d.ts +6 -0
- package/clinkx-workflows/dist/context-preflight.js.map +1 -1
- package/clinkx-workflows/dist/council-execution-policy.d.ts +3 -1
- package/clinkx-workflows/dist/council-execution-policy.js +4 -0
- package/clinkx-workflows/dist/council-execution-policy.js.map +1 -1
- package/clinkx-workflows/dist/dashboard/contracts.d.ts +586 -586
- package/clinkx-workflows/dist/dashboard/progress-report-contract.d.ts +28 -28
- package/clinkx-workflows/dist/dashboard/session-log-contract.d.ts +32 -32
- package/clinkx-workflows/dist/dashboard-ui/assets/index-DRId2DVA.js +119 -0
- package/clinkx-workflows/dist/dashboard-ui/assets/style-CHAw57y5.css +1 -0
- package/clinkx-workflows/dist/dashboard-ui/index.html +2 -2
- package/clinkx-workflows/dist/delegate-signature.d.ts +4 -3
- package/clinkx-workflows/dist/delegate-signature.js +4 -3
- package/clinkx-workflows/dist/delegate-signature.js.map +1 -1
- package/clinkx-workflows/dist/durable-file.d.ts +69 -0
- package/clinkx-workflows/dist/durable-file.js +145 -0
- package/clinkx-workflows/dist/durable-file.js.map +1 -0
- package/clinkx-workflows/dist/engine.d.ts +42 -7
- package/clinkx-workflows/dist/engine.js +196 -24
- package/clinkx-workflows/dist/engine.js.map +1 -1
- package/clinkx-workflows/dist/ping-pong/admission.d.ts +127 -0
- package/clinkx-workflows/dist/ping-pong/admission.js +522 -0
- package/clinkx-workflows/dist/ping-pong/admission.js.map +1 -0
- package/clinkx-workflows/dist/ping-pong/contract.d.ts +2539 -0
- package/clinkx-workflows/dist/ping-pong/contract.js +819 -0
- package/clinkx-workflows/dist/ping-pong/contract.js.map +1 -0
- package/clinkx-workflows/dist/ping-pong/control.d.ts +67 -0
- package/clinkx-workflows/dist/ping-pong/control.js +12 -0
- package/clinkx-workflows/dist/ping-pong/control.js.map +1 -0
- package/clinkx-workflows/dist/ping-pong/runtime-handle.d.ts +24 -0
- package/clinkx-workflows/dist/ping-pong/runtime-handle.js +55 -0
- package/clinkx-workflows/dist/ping-pong/runtime-handle.js.map +1 -0
- package/clinkx-workflows/dist/ping-pong/runtime.d.ts +235 -0
- package/clinkx-workflows/dist/ping-pong/runtime.js +1183 -0
- package/clinkx-workflows/dist/ping-pong/runtime.js.map +1 -0
- package/clinkx-workflows/dist/ping-pong/settle.d.ts +57 -0
- package/clinkx-workflows/dist/ping-pong/settle.js +58 -0
- package/clinkx-workflows/dist/ping-pong/settle.js.map +1 -0
- package/clinkx-workflows/dist/ping-pong/sidecar.d.ts +65 -0
- package/clinkx-workflows/dist/ping-pong/sidecar.js +138 -0
- package/clinkx-workflows/dist/ping-pong/sidecar.js.map +1 -0
- package/clinkx-workflows/dist/plan-review/publication.d.ts +2 -1
- package/clinkx-workflows/dist/plan-review/publication.js +5 -6
- package/clinkx-workflows/dist/plan-review/publication.js.map +1 -1
- package/clinkx-workflows/dist/plan-review/snapshot.d.ts +3 -2
- package/clinkx-workflows/dist/plan-review/snapshot.js +8 -8
- package/clinkx-workflows/dist/plan-review/snapshot.js.map +1 -1
- package/clinkx-workflows/dist/recipe-host.d.ts +315 -0
- package/clinkx-workflows/dist/recipe-host.js +183 -0
- package/clinkx-workflows/dist/recipe-host.js.map +1 -0
- package/clinkx-workflows/dist/recipe-seats.d.ts +96 -0
- package/clinkx-workflows/dist/recipe-seats.js +163 -0
- package/clinkx-workflows/dist/recipe-seats.js.map +1 -0
- package/clinkx-workflows/dist/recipe-shared/evidence.d.ts +67 -0
- package/clinkx-workflows/dist/recipe-shared/evidence.js +111 -0
- package/clinkx-workflows/dist/recipe-shared/evidence.js.map +1 -0
- package/clinkx-workflows/dist/reducers/index.d.ts +9 -1
- package/clinkx-workflows/dist/reducers/index.js +30 -0
- package/clinkx-workflows/dist/reducers/index.js.map +1 -1
- package/clinkx-workflows/dist/run-lock-reclaim.d.ts +44 -0
- package/clinkx-workflows/dist/run-lock-reclaim.js +86 -0
- package/clinkx-workflows/dist/run-lock-reclaim.js.map +1 -0
- package/clinkx-workflows/dist/state-schema.d.ts +11 -0
- package/clinkx-workflows/dist/state-schema.js +4 -1
- package/clinkx-workflows/dist/state-schema.js.map +1 -1
- package/clinkx-workflows/dist/state.d.ts +178 -3
- package/clinkx-workflows/dist/state.js +618 -83
- package/clinkx-workflows/dist/state.js.map +1 -1
- package/clinkx-workflows/dist/transport.d.ts +2 -1
- package/clinkx-workflows/dist/transport.js +5 -0
- package/clinkx-workflows/dist/transport.js.map +1 -1
- package/clinkx-workflows/dist/validators/architecture-arena.d.ts +6 -0
- package/clinkx-workflows/dist/validators/architecture-arena.js +16 -0
- package/clinkx-workflows/dist/validators/architecture-arena.js.map +1 -0
- package/clinkx-workflows/dist/validators/index.js +4 -0
- package/clinkx-workflows/dist/validators/index.js.map +1 -1
- package/clinkx-workflows/dist/validators/ping-pong.d.ts +6 -0
- package/clinkx-workflows/dist/validators/ping-pong.js +10 -0
- package/clinkx-workflows/dist/validators/ping-pong.js.map +1 -0
- package/clinkx-workflows/dist/workflow-tools.d.ts +51 -5
- package/clinkx-workflows/dist/workflow-tools.js +785 -34
- package/clinkx-workflows/dist/workflow-tools.js.map +1 -1
- package/clinkx-workflows/dist/workflow-verification-contract.d.ts +4 -4
- package/clinkx-workflows/templates/architecture-arena/candidate.txt +39 -0
- package/clinkx-workflows/templates/architecture-arena/judge.txt +34 -0
- package/clinkx-workflows/templates/ping-pong/implement.txt +14 -0
- package/clinkx-workflows/templates/ping-pong/remediate.txt +14 -0
- package/clinkx-workflows/templates/ping-pong/review.txt +27 -0
- package/clinkx-workflows/workflows/architecture-arena.yaml +127 -0
- package/clinkx-workflows/workflows/ping-pong.yaml +158 -0
- package/dist/context-builder/bundle.js +4 -3
- package/dist/context-builder/bundle.js.map +1 -1
- package/dist/context-builder/delegate-packet.d.ts +1 -0
- package/dist/context-builder/delegate-packet.js +66 -7
- package/dist/context-builder/delegate-packet.js.map +1 -1
- package/dist/context-builder/delegate-validation.d.ts +12 -3
- package/dist/context-builder/delegate-validation.js +23 -13
- package/dist/context-builder/delegate-validation.js.map +1 -1
- package/dist/context-builder/external-gaps.d.ts +14 -3
- package/dist/context-builder/external-gaps.js +124 -18
- package/dist/context-builder/external-gaps.js.map +1 -1
- package/dist/context-builder/handler.js +108 -191
- package/dist/context-builder/handler.js.map +1 -1
- package/dist/context-builder/review-coverage.js +27 -1
- package/dist/context-builder/review-coverage.js.map +1 -1
- package/dist/context-builder/secrets.js +1 -1
- package/dist/context-builder/secrets.js.map +1 -1
- package/dist/context-builder/versions.d.ts +10 -4
- package/dist/context-builder/versions.js +10 -4
- package/dist/context-builder/versions.js.map +1 -1
- package/dist/context-builder/workflow-runner.js +33 -24
- package/dist/context-builder/workflow-runner.js.map +1 -1
- package/dist/dashboard-cli.d.ts +2 -0
- package/dist/dashboard-cli.js +147 -57
- package/dist/dashboard-cli.js.map +1 -1
- package/dist/dashboard-companion/client.d.ts +10 -0
- package/dist/dashboard-companion/client.js +43 -1
- package/dist/dashboard-companion/client.js.map +1 -1
- package/dist/dashboard-companion/config.d.ts +19 -1
- package/dist/dashboard-companion/config.js +104 -3
- package/dist/dashboard-companion/config.js.map +1 -1
- package/dist/dashboard-companion/protocol.d.ts +16 -0
- package/dist/dashboard-companion/protocol.js +38 -1
- package/dist/dashboard-companion/protocol.js.map +1 -1
- package/dist/dashboard-companion/workstation-binding.d.ts +69 -0
- package/dist/dashboard-companion/workstation-binding.js +192 -0
- package/dist/dashboard-companion/workstation-binding.js.map +1 -0
- package/dist/dashboard-gateway/admin-cli.d.ts +10 -0
- package/dist/dashboard-gateway/admin-cli.js +266 -0
- package/dist/dashboard-gateway/admin-cli.js.map +1 -0
- package/dist/dashboard-gateway/config.d.ts +50 -0
- package/dist/dashboard-gateway/config.js +235 -1
- package/dist/dashboard-gateway/config.js.map +1 -1
- package/dist/dashboard-gateway/connection-registry.d.ts +100 -0
- package/dist/dashboard-gateway/connection-registry.js +228 -0
- package/dist/dashboard-gateway/connection-registry.js.map +1 -0
- package/dist/dashboard-gateway/control-plane/account-auth.d.ts +78 -0
- package/dist/dashboard-gateway/control-plane/account-auth.js +198 -0
- package/dist/dashboard-gateway/control-plane/account-auth.js.map +1 -0
- package/dist/dashboard-gateway/control-plane/billing.d.ts +264 -0
- package/dist/dashboard-gateway/control-plane/billing.js +787 -0
- package/dist/dashboard-gateway/control-plane/billing.js.map +1 -0
- package/dist/dashboard-gateway/control-plane/contracts.d.ts +1293 -0
- package/dist/dashboard-gateway/control-plane/contracts.js +280 -0
- package/dist/dashboard-gateway/control-plane/contracts.js.map +1 -0
- package/dist/dashboard-gateway/control-plane/cookies.d.ts +13 -0
- package/dist/dashboard-gateway/control-plane/cookies.js +46 -0
- package/dist/dashboard-gateway/control-plane/cookies.js.map +1 -0
- package/dist/dashboard-gateway/control-plane/credentials.d.ts +63 -0
- package/dist/dashboard-gateway/control-plane/credentials.js +66 -0
- package/dist/dashboard-gateway/control-plane/credentials.js.map +1 -0
- package/dist/dashboard-gateway/control-plane/entitlements.d.ts +118 -0
- package/dist/dashboard-gateway/control-plane/entitlements.js +329 -0
- package/dist/dashboard-gateway/control-plane/entitlements.js.map +1 -0
- package/dist/dashboard-gateway/control-plane/errors.d.ts +17 -0
- package/dist/dashboard-gateway/control-plane/errors.js +23 -0
- package/dist/dashboard-gateway/control-plane/errors.js.map +1 -0
- package/dist/dashboard-gateway/control-plane/hosts.d.ts +6 -0
- package/dist/dashboard-gateway/control-plane/hosts.js +16 -0
- package/dist/dashboard-gateway/control-plane/hosts.js.map +1 -0
- package/dist/dashboard-gateway/control-plane/http.d.ts +34 -0
- package/dist/dashboard-gateway/control-plane/http.js +331 -0
- package/dist/dashboard-gateway/control-plane/http.js.map +1 -0
- package/dist/dashboard-gateway/control-plane/schema.d.ts +9 -0
- package/dist/dashboard-gateway/control-plane/schema.js +247 -0
- package/dist/dashboard-gateway/control-plane/schema.js.map +1 -0
- package/dist/dashboard-gateway/control-plane/secrets.d.ts +20 -0
- package/dist/dashboard-gateway/control-plane/secrets.js +39 -0
- package/dist/dashboard-gateway/control-plane/secrets.js.map +1 -0
- package/dist/dashboard-gateway/control-plane/store.d.ts +579 -0
- package/dist/dashboard-gateway/control-plane/store.js +1115 -0
- package/dist/dashboard-gateway/control-plane/store.js.map +1 -0
- package/dist/dashboard-gateway/cookie-boundary.d.ts +60 -0
- package/dist/dashboard-gateway/cookie-boundary.js +205 -0
- package/dist/dashboard-gateway/cookie-boundary.js.map +1 -0
- package/dist/dashboard-gateway/index.js +271 -494
- package/dist/dashboard-gateway/index.js.map +1 -1
- package/dist/dashboard-gateway/main.js +7 -2
- package/dist/dashboard-gateway/main.js.map +1 -1
- package/dist/dashboard-gateway/routing.d.ts +43 -0
- package/dist/dashboard-gateway/routing.js +65 -0
- package/dist/dashboard-gateway/routing.js.map +1 -0
- package/dist/dashboard-gateway/tenant-gateway.d.ts +56 -0
- package/dist/dashboard-gateway/tenant-gateway.js +989 -0
- package/dist/dashboard-gateway/tenant-gateway.js.map +1 -0
- package/dist/dashboard-gateway/tenant-main.d.ts +41 -0
- package/dist/dashboard-gateway/tenant-main.js +303 -0
- package/dist/dashboard-gateway/tenant-main.js.map +1 -0
- package/dist/dashboard-gateway/tunnel-errors.d.ts +16 -0
- package/dist/dashboard-gateway/tunnel-errors.js +22 -0
- package/dist/dashboard-gateway/tunnel-errors.js.map +1 -0
- package/dist/dashboard-gateway/tunnel-http.d.ts +21 -0
- package/dist/dashboard-gateway/tunnel-http.js +175 -0
- package/dist/dashboard-gateway/tunnel-http.js.map +1 -0
- package/dist/handler.d.ts +6 -0
- package/dist/handler.js +17 -1
- package/dist/handler.js.map +1 -1
- package/dist/local-clink-backend.d.ts +2 -1
- package/dist/local-clink-backend.js +28 -4
- package/dist/local-clink-backend.js.map +1 -1
- package/dist/parsers/extract.d.ts +19 -1
- package/dist/parsers/extract.js +68 -3
- package/dist/parsers/extract.js.map +1 -1
- package/dist/pipeline.d.ts +17 -1
- package/dist/pipeline.js +34 -6
- package/dist/pipeline.js.map +1 -1
- package/dist/process-group.d.ts +65 -0
- package/dist/process-group.js +153 -0
- package/dist/process-group.js.map +1 -0
- package/dist/progress-report/schema.d.ts +23 -23
- package/dist/project-verification/files.js +1 -1
- package/dist/project-verification/files.js.map +1 -1
- package/dist/project-verification/process.js +23 -44
- package/dist/project-verification/process.js.map +1 -1
- package/dist/project-verification/service.js +9 -1
- package/dist/project-verification/service.js.map +1 -1
- package/dist/prompt.d.ts +7 -0
- package/dist/prompt.js +18 -1
- package/dist/prompt.js.map +1 -1
- package/dist/recipe-host/git-trust.d.ts +43 -0
- package/dist/recipe-host/git-trust.js +360 -0
- package/dist/recipe-host/git-trust.js.map +1 -0
- package/dist/recipe-host/git.d.ts +42 -0
- package/dist/recipe-host/git.js +207 -0
- package/dist/recipe-host/git.js.map +1 -0
- package/dist/recipe-host/native-recipe-host.d.ts +127 -0
- package/dist/recipe-host/native-recipe-host.js +880 -0
- package/dist/recipe-host/native-recipe-host.js.map +1 -0
- package/dist/recipe-host/seat-cwd.d.ts +16 -0
- package/dist/recipe-host/seat-cwd.js +170 -0
- package/dist/recipe-host/seat-cwd.js.map +1 -0
- package/dist/runner.d.ts +43 -1
- package/dist/runner.js +234 -35
- package/dist/runner.js.map +1 -1
- package/dist/server.js +9 -1
- package/dist/server.js.map +1 -1
- package/docs/configuration.md +24 -3
- package/docs/engineering-recipes.md +291 -0
- package/docs/security.md +84 -0
- package/docs/verification/project-checks.md +24 -2
- package/package.json +5 -1
- package/clinkx-workflows/dist/dashboard-ui/assets/index-BdpmbPps.js +0 -115
- package/clinkx-workflows/dist/dashboard-ui/assets/style-Im5i1iAs.css +0 -1
|
@@ -0,0 +1,331 @@
|
|
|
1
|
+
import { CONTROL_ERROR_HTTP_STATUS, CONTROL_PATH_PREFIX, CONTROL_ROUTES, MAX_WORKSTATIONS, } from "./contracts.js";
|
|
2
|
+
import { ACCOUNT_SESSION_COOKIE, LOGIN_BINDING_COOKIE, clearHostCookie, readSingleCookie, serializeHostCookie, } from "./cookies.js";
|
|
3
|
+
import { ControlError } from "./errors.js";
|
|
4
|
+
import { isPublicHost } from "./hosts.js";
|
|
5
|
+
import { tenantActorOf } from "./store.js";
|
|
6
|
+
export const DEFAULT_CONTROL_MAX_BODY_BYTES = 16 * 1024;
|
|
7
|
+
const ROUTES_BY_PATH = new Map(Object.keys(CONTROL_ROUTES).map((name) => [CONTROL_ROUTES[name].path, name]));
|
|
8
|
+
const BILLING_ROUTES = new Set(["billingStatus", "billingCheckout", "billingPortal", "stripeWebhook"]);
|
|
9
|
+
const JSON_CONTENT_TYPE = /^application\/json(?:\s*;\s*charset=utf-8)?$/i;
|
|
10
|
+
function iso(ms) {
|
|
11
|
+
return new Date(ms).toISOString();
|
|
12
|
+
}
|
|
13
|
+
function baseHeaders() {
|
|
14
|
+
return {
|
|
15
|
+
"cache-control": "no-store",
|
|
16
|
+
"referrer-policy": "no-referrer",
|
|
17
|
+
"x-content-type-options": "nosniff",
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
function send(response, reply, extraHeaders = {}) {
|
|
21
|
+
const headers = { ...baseHeaders(), ...extraHeaders };
|
|
22
|
+
if (reply.cookies !== undefined && reply.cookies.length > 0)
|
|
23
|
+
headers["set-cookie"] = [...reply.cookies];
|
|
24
|
+
if (reply.location !== undefined) {
|
|
25
|
+
headers.location = reply.location;
|
|
26
|
+
response.writeHead(reply.status, headers);
|
|
27
|
+
response.end();
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
const body = `${JSON.stringify(reply.body ?? {})}\n`;
|
|
31
|
+
headers["content-type"] = "application/json; charset=utf-8";
|
|
32
|
+
headers["content-length"] = Buffer.byteLength(body);
|
|
33
|
+
response.writeHead(reply.status, headers);
|
|
34
|
+
response.end(body);
|
|
35
|
+
}
|
|
36
|
+
function errorReply(code, cookies) {
|
|
37
|
+
return {
|
|
38
|
+
status: CONTROL_ERROR_HTTP_STATUS[code],
|
|
39
|
+
body: { error: { code } },
|
|
40
|
+
...(cookies === undefined ? {} : { cookies }),
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
function singleHeader(request, name) {
|
|
44
|
+
const value = request.headers[name];
|
|
45
|
+
if (Array.isArray(value))
|
|
46
|
+
throw new ControlError("invalid_request");
|
|
47
|
+
return value;
|
|
48
|
+
}
|
|
49
|
+
async function readBody(request, limit) {
|
|
50
|
+
const declared = singleHeader(request, "content-length");
|
|
51
|
+
if (declared !== undefined && (!/^[0-9]+$/.test(declared) || Number(declared) > limit)) {
|
|
52
|
+
throw new ControlError(/^[0-9]+$/.test(declared) ? "payload_too_large" : "invalid_request");
|
|
53
|
+
}
|
|
54
|
+
const chunks = [];
|
|
55
|
+
let size = 0;
|
|
56
|
+
for await (const chunk of request) {
|
|
57
|
+
const buffer = Buffer.isBuffer(chunk) ? chunk : Buffer.from(String(chunk));
|
|
58
|
+
size += buffer.length;
|
|
59
|
+
if (size > limit)
|
|
60
|
+
throw new ControlError("payload_too_large");
|
|
61
|
+
chunks.push(buffer);
|
|
62
|
+
}
|
|
63
|
+
return Buffer.concat(chunks);
|
|
64
|
+
}
|
|
65
|
+
function hasRequestBody(request) {
|
|
66
|
+
const length = request.headers["content-length"];
|
|
67
|
+
return (length !== undefined && length !== "0") || request.headers["transfer-encoding"] !== undefined;
|
|
68
|
+
}
|
|
69
|
+
async function parseJsonBody(request, schema, limit) {
|
|
70
|
+
const body = await readBody(request, limit);
|
|
71
|
+
let value = {};
|
|
72
|
+
if (body.length > 0) {
|
|
73
|
+
const contentType = singleHeader(request, "content-type");
|
|
74
|
+
if (contentType === undefined || !JSON_CONTENT_TYPE.test(contentType))
|
|
75
|
+
throw new ControlError("invalid_request");
|
|
76
|
+
try {
|
|
77
|
+
value = JSON.parse(body.toString("utf8"));
|
|
78
|
+
}
|
|
79
|
+
catch {
|
|
80
|
+
throw new ControlError("invalid_request");
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
const parsed = schema.safeParse(value);
|
|
84
|
+
if (!parsed.success)
|
|
85
|
+
throw new ControlError("invalid_request");
|
|
86
|
+
return parsed.data;
|
|
87
|
+
}
|
|
88
|
+
function parseQuery(search, schema) {
|
|
89
|
+
const params = new URLSearchParams(search);
|
|
90
|
+
const record = {};
|
|
91
|
+
for (const [key, value] of params) {
|
|
92
|
+
if (Object.hasOwn(record, key))
|
|
93
|
+
throw new ControlError("invalid_request");
|
|
94
|
+
record[key] = value;
|
|
95
|
+
}
|
|
96
|
+
const parsed = schema.safeParse(record);
|
|
97
|
+
if (!parsed.success)
|
|
98
|
+
throw new ControlError("invalid_request");
|
|
99
|
+
return parsed.data;
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Dispatch `/api/control/*` on the single public origin. Host and Origin are
|
|
103
|
+
* matched exactly, identity comes only from the account-session cookie and
|
|
104
|
+
* server records, and every unexpected failure denies with `store_unavailable`.
|
|
105
|
+
*/
|
|
106
|
+
export function createControlPlaneHttpHandler(options) {
|
|
107
|
+
const { publicOrigin, accountAuth, credentials } = options;
|
|
108
|
+
const maxBodyBytes = options.maxBodyBytes ?? DEFAULT_CONTROL_MAX_BODY_BYTES;
|
|
109
|
+
function checkOrigin(request, name) {
|
|
110
|
+
const origin = singleHeader(request, "origin");
|
|
111
|
+
if (name === "stripeWebhook")
|
|
112
|
+
return;
|
|
113
|
+
if (CONTROL_ROUTES[name].method === "POST" ? origin !== publicOrigin : origin !== undefined && origin !== publicOrigin) {
|
|
114
|
+
throw new ControlError("forbidden");
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
function requireAccount(request) {
|
|
118
|
+
const context = accountAuth.resolveAccountSession(readSingleCookie(singleHeader(request, "cookie"), ACCOUNT_SESSION_COOKIE));
|
|
119
|
+
if (context === null)
|
|
120
|
+
throw new ControlError("unauthenticated");
|
|
121
|
+
return context;
|
|
122
|
+
}
|
|
123
|
+
function requireOwner(request) {
|
|
124
|
+
const context = accountAuth.resolveTenantMemberFromCookies(singleHeader(request, "cookie"));
|
|
125
|
+
if (context === null)
|
|
126
|
+
throw new ControlError("unauthenticated");
|
|
127
|
+
if (context.role !== "owner")
|
|
128
|
+
throw new ControlError("forbidden");
|
|
129
|
+
return context;
|
|
130
|
+
}
|
|
131
|
+
function requireBilling() {
|
|
132
|
+
if (options.billing === undefined)
|
|
133
|
+
throw new ControlError("not_found");
|
|
134
|
+
return options.billing;
|
|
135
|
+
}
|
|
136
|
+
function workstationSummary(workstation) {
|
|
137
|
+
const live = options.workstationConnection?.(workstation.companionId) ?? null;
|
|
138
|
+
return {
|
|
139
|
+
companion_id: workstation.companionId,
|
|
140
|
+
label: workstation.label,
|
|
141
|
+
created_at: iso(workstation.createdAt),
|
|
142
|
+
key_issued_at: workstation.keyIssuedAt === null ? null : iso(workstation.keyIssuedAt),
|
|
143
|
+
connection: live === null
|
|
144
|
+
? { state: "offline", connection_generation: null }
|
|
145
|
+
: { state: "online", connection_generation: live.connectionGeneration },
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
async function body(request, schema) {
|
|
149
|
+
return parseJsonBody(request, schema, maxBodyBytes);
|
|
150
|
+
}
|
|
151
|
+
// Bodies are always read before authority is resolved, and every mutation
|
|
152
|
+
// re-checks that authority inside its own store transaction: a session or
|
|
153
|
+
// membership that lapses while a slow body is in flight never reaches a write.
|
|
154
|
+
async function route(name, request, url) {
|
|
155
|
+
const cookieHeader = singleHeader(request, "cookie");
|
|
156
|
+
switch (name) {
|
|
157
|
+
case "authStart": {
|
|
158
|
+
await body(request, CONTROL_ROUTES.authStart.request);
|
|
159
|
+
const start = await accountAuth.startLogin();
|
|
160
|
+
return {
|
|
161
|
+
status: 200,
|
|
162
|
+
body: { authorization_url: start.authorizationUrl },
|
|
163
|
+
cookies: [serializeHostCookie(LOGIN_BINDING_COOKIE, start.loginBinding, start.bindingMaxAgeSeconds)],
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
case "authCallback": {
|
|
167
|
+
const query = parseQuery(url.search, CONTROL_ROUTES.authCallback.request);
|
|
168
|
+
const clearLogin = clearHostCookie(LOGIN_BINDING_COOKIE);
|
|
169
|
+
try {
|
|
170
|
+
const done = await accountAuth.completeLogin({
|
|
171
|
+
callbackUrl: url,
|
|
172
|
+
query,
|
|
173
|
+
loginBinding: readSingleCookie(cookieHeader, LOGIN_BINDING_COOKIE),
|
|
174
|
+
});
|
|
175
|
+
return {
|
|
176
|
+
status: 303,
|
|
177
|
+
location: `${publicOrigin}/`,
|
|
178
|
+
cookies: [clearLogin, serializeHostCookie(ACCOUNT_SESSION_COOKIE, done.session.secret, done.session.maxAgeSeconds)],
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
catch (error) {
|
|
182
|
+
if (error instanceof ControlError)
|
|
183
|
+
return errorReply(error.code, [clearLogin]);
|
|
184
|
+
throw error;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
case "authLogout": {
|
|
188
|
+
await body(request, CONTROL_ROUTES.authLogout.request);
|
|
189
|
+
accountAuth.logoutAccount(readSingleCookie(cookieHeader, ACCOUNT_SESSION_COOKIE));
|
|
190
|
+
return { status: 200, body: { logged_out: true }, cookies: [clearHostCookie(ACCOUNT_SESSION_COOKIE)] };
|
|
191
|
+
}
|
|
192
|
+
case "authSession": {
|
|
193
|
+
const context = requireAccount(request);
|
|
194
|
+
return {
|
|
195
|
+
status: 200,
|
|
196
|
+
body: {
|
|
197
|
+
account: { id: context.account.id, display_name: context.account.displayName },
|
|
198
|
+
expires_at: iso(context.expiresAt),
|
|
199
|
+
},
|
|
200
|
+
};
|
|
201
|
+
}
|
|
202
|
+
case "workspaceRead": {
|
|
203
|
+
const context = requireOwner(request);
|
|
204
|
+
const tenantId = context.tenant.id;
|
|
205
|
+
return {
|
|
206
|
+
status: 200,
|
|
207
|
+
body: {
|
|
208
|
+
workspace: { id: tenantId },
|
|
209
|
+
role: context.role,
|
|
210
|
+
entitlement: options.entitlementStatus(tenantId),
|
|
211
|
+
billing_enabled: options.billing !== undefined,
|
|
212
|
+
max_workstations: MAX_WORKSTATIONS,
|
|
213
|
+
workstations: credentials.listWorkstations(tenantId).map(workstationSummary),
|
|
214
|
+
},
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
case "workstationCreate": {
|
|
218
|
+
const input = await body(request, CONTROL_ROUTES.workstationCreate.request);
|
|
219
|
+
const context = requireOwner(request);
|
|
220
|
+
const issued = credentials.createWorkstation({
|
|
221
|
+
tenantId: context.tenant.id,
|
|
222
|
+
actor: tenantActorOf(context),
|
|
223
|
+
label: input.label,
|
|
224
|
+
});
|
|
225
|
+
return { status: 200, body: { workstation: workstationSummary(issued.workstation), api_key: issued.apiKey } };
|
|
226
|
+
}
|
|
227
|
+
case "workstationRotate": {
|
|
228
|
+
const input = await body(request, CONTROL_ROUTES.workstationRotate.request);
|
|
229
|
+
const context = requireOwner(request);
|
|
230
|
+
const rotation = credentials.rotateWorkstationKey({
|
|
231
|
+
tenantId: context.tenant.id,
|
|
232
|
+
companionId: input.companion_id,
|
|
233
|
+
actor: tenantActorOf(context),
|
|
234
|
+
});
|
|
235
|
+
const closed = options.closeWorkstationConnections?.(rotation.companionId) ?? 0;
|
|
236
|
+
return {
|
|
237
|
+
status: 200,
|
|
238
|
+
body: {
|
|
239
|
+
companion_id: rotation.companionId,
|
|
240
|
+
api_key: rotation.apiKey,
|
|
241
|
+
revoked_credential_id: rotation.revokedCredentialId,
|
|
242
|
+
closed_connections: closed,
|
|
243
|
+
},
|
|
244
|
+
};
|
|
245
|
+
}
|
|
246
|
+
case "workstationRevoke": {
|
|
247
|
+
const input = await body(request, CONTROL_ROUTES.workstationRevoke.request);
|
|
248
|
+
const context = requireOwner(request);
|
|
249
|
+
const revocation = credentials.revokeWorkstation({
|
|
250
|
+
tenantId: context.tenant.id,
|
|
251
|
+
companionId: input.companion_id,
|
|
252
|
+
actor: tenantActorOf(context),
|
|
253
|
+
});
|
|
254
|
+
const closed = options.closeWorkstationConnections?.(revocation.companionId) ?? 0;
|
|
255
|
+
return {
|
|
256
|
+
status: 200,
|
|
257
|
+
body: { companion_id: revocation.companionId, revoked_at: iso(revocation.revokedAt), closed_connections: closed },
|
|
258
|
+
};
|
|
259
|
+
}
|
|
260
|
+
case "billingStatus": {
|
|
261
|
+
const context = requireOwner(request);
|
|
262
|
+
return { status: 200, body: { entitlement: requireBilling().status(context.tenant.id) } };
|
|
263
|
+
}
|
|
264
|
+
case "billingCheckout": {
|
|
265
|
+
await body(request, CONTROL_ROUTES.billingCheckout.request);
|
|
266
|
+
const context = requireOwner(request);
|
|
267
|
+
const checkout = await requireBilling().createCheckout({ tenant: context.tenant, actor: tenantActorOf(context) });
|
|
268
|
+
return { status: 200, body: { checkout_url: checkout.url } };
|
|
269
|
+
}
|
|
270
|
+
case "billingPortal": {
|
|
271
|
+
await body(request, CONTROL_ROUTES.billingPortal.request);
|
|
272
|
+
const context = requireOwner(request);
|
|
273
|
+
const portal = await requireBilling().createPortal({ tenant: context.tenant, actor: tenantActorOf(context) });
|
|
274
|
+
return { status: 200, body: { portal_url: portal.url } };
|
|
275
|
+
}
|
|
276
|
+
case "stripeWebhook": {
|
|
277
|
+
const billing = requireBilling();
|
|
278
|
+
const contentType = singleHeader(request, "content-type");
|
|
279
|
+
const signature = singleHeader(request, "stripe-signature");
|
|
280
|
+
const raw = await readBody(request, billing.webhookMaxBodyBytes);
|
|
281
|
+
if (contentType === undefined || !JSON_CONTENT_TYPE.test(contentType))
|
|
282
|
+
throw new ControlError("invalid_request");
|
|
283
|
+
const received = billing.receiveWebhook({ rawBody: raw, signature });
|
|
284
|
+
return { status: 200, body: { received: true, event_id: received.eventId, duplicate: received.duplicate } };
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
async function dispatch(request, response, rawUrl) {
|
|
289
|
+
let name;
|
|
290
|
+
try {
|
|
291
|
+
if (!isPublicHost(request.headersDistinct.host, publicOrigin))
|
|
292
|
+
throw new ControlError("misdirected_request");
|
|
293
|
+
const url = new URL(rawUrl, publicOrigin);
|
|
294
|
+
name = ROUTES_BY_PATH.get(url.pathname);
|
|
295
|
+
if (name === undefined || url.pathname !== rawUrl.split("?", 1)[0])
|
|
296
|
+
throw new ControlError("not_found");
|
|
297
|
+
if (options.billing === undefined && BILLING_ROUTES.has(name))
|
|
298
|
+
throw new ControlError("not_found");
|
|
299
|
+
const definition = CONTROL_ROUTES[name];
|
|
300
|
+
if (request.method !== definition.method) {
|
|
301
|
+
send(response, errorReply("method_not_allowed"), { allow: definition.method });
|
|
302
|
+
return;
|
|
303
|
+
}
|
|
304
|
+
if (definition.method === "GET" && hasRequestBody(request))
|
|
305
|
+
throw new ControlError("invalid_request");
|
|
306
|
+
if (definition.method === "GET" && definition.request === null && url.search !== "") {
|
|
307
|
+
throw new ControlError("invalid_request");
|
|
308
|
+
}
|
|
309
|
+
checkOrigin(request, name);
|
|
310
|
+
send(response, await route(name, request, url));
|
|
311
|
+
}
|
|
312
|
+
catch (error) {
|
|
313
|
+
if (!(error instanceof ControlError) && name !== undefined)
|
|
314
|
+
options.onError?.(error, name);
|
|
315
|
+
const code = error instanceof ControlError ? error.code : "store_unavailable";
|
|
316
|
+
if (response.headersSent) {
|
|
317
|
+
response.destroy();
|
|
318
|
+
return;
|
|
319
|
+
}
|
|
320
|
+
send(response, errorReply(code), code === "payload_too_large" ? { connection: "close" } : {});
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
return async (request, response) => {
|
|
324
|
+
const rawUrl = request.url ?? "";
|
|
325
|
+
if (!rawUrl.startsWith(CONTROL_PATH_PREFIX))
|
|
326
|
+
return false;
|
|
327
|
+
await dispatch(request, response, rawUrl);
|
|
328
|
+
return true;
|
|
329
|
+
};
|
|
330
|
+
}
|
|
331
|
+
//# sourceMappingURL=http.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"http.js","sourceRoot":"","sources":["../../../src/dashboard-gateway/control-plane/http.ts"],"names":[],"mappings":"AAMA,OAAO,EACL,yBAAyB,EACzB,mBAAmB,EACnB,cAAc,EAId,gBAAgB,GAEjB,MAAM,gBAAgB,CAAC;AACxB,OAAO,EACL,sBAAsB,EACtB,oBAAoB,EACpB,eAAe,EACf,gBAAgB,EAChB,mBAAmB,GACpB,MAAM,cAAc,CAAC;AAEtB,O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 @@
|
|
|
1
|
+
import type { DatabaseSync } from "node:sqlite";
|
|
2
|
+
export declare const CONTROL_PLANE_SCHEMA_VERSION = 2;
|
|
3
|
+
/** "CLKX": marks the file as a ClinkX control-plane database. */
|
|
4
|
+
export declare const CONTROL_PLANE_APPLICATION_ID = 1129073496;
|
|
5
|
+
/**
|
|
6
|
+
* Create the schema on an empty file or verify an existing one. There is no
|
|
7
|
+
* migration path: any version other than the current one refuses to open.
|
|
8
|
+
*/
|
|
9
|
+
export declare function applyControlPlaneSchema(db: DatabaseSync): void;
|
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
import { SubscriptionStateSchema } from "./contracts.js";
|
|
2
|
+
import { ControlSchemaVersionError } from "./errors.js";
|
|
3
|
+
export const CONTROL_PLANE_SCHEMA_VERSION = 2;
|
|
4
|
+
/** "CLKX": marks the file as a ClinkX control-plane database. */
|
|
5
|
+
export const CONTROL_PLANE_APPLICATION_ID = 0x434c4b58;
|
|
6
|
+
const SUBSCRIPTION_STATES = SubscriptionStateSchema.options.map((state) => `'${state}'`).join(",");
|
|
7
|
+
// Times are integer UTC milliseconds. Every `verifier` column holds a
|
|
8
|
+
// purpose-bound SHA-256 of a 32-byte random secret, never the secret. The one
|
|
9
|
+
// exception is `pending_login`: it keeps the PKCE code verifier and the OIDC
|
|
10
|
+
// nonce the callback needs, and clears both when the login is consumed.
|
|
11
|
+
const SCHEMA_V2 = `
|
|
12
|
+
create table account (
|
|
13
|
+
id text primary key check (id glob 'acc_*'),
|
|
14
|
+
issuer text not null check (length(issuer) between 1 and 2048),
|
|
15
|
+
subject text not null check (length(subject) between 1 and 255),
|
|
16
|
+
display_name text check (display_name is null or length(display_name) between 1 and 200),
|
|
17
|
+
authority_revision integer not null default 1 check (authority_revision >= 1),
|
|
18
|
+
created_at integer not null,
|
|
19
|
+
updated_at integer not null,
|
|
20
|
+
unique (issuer, subject)
|
|
21
|
+
) strict;
|
|
22
|
+
|
|
23
|
+
create table tenant (
|
|
24
|
+
id text primary key check (id glob 'ten_*'),
|
|
25
|
+
created_at integer not null
|
|
26
|
+
) strict;
|
|
27
|
+
|
|
28
|
+
create trigger tenant_identity_immutable before update of id on tenant
|
|
29
|
+
begin
|
|
30
|
+
select raise(abort, 'tenant identity is immutable');
|
|
31
|
+
end;
|
|
32
|
+
|
|
33
|
+
create table membership (
|
|
34
|
+
tenant_id text not null references tenant(id) on delete cascade,
|
|
35
|
+
account_id text not null references account(id) on delete cascade,
|
|
36
|
+
role text not null check (role in ('owner')),
|
|
37
|
+
created_at integer not null,
|
|
38
|
+
primary key (tenant_id, account_id)
|
|
39
|
+
) strict;
|
|
40
|
+
create unique index membership_one_owner on membership (tenant_id) where role = 'owner';
|
|
41
|
+
create unique index membership_one_workspace_per_owner on membership (account_id) where role = 'owner';
|
|
42
|
+
|
|
43
|
+
create table pending_login (
|
|
44
|
+
state_verifier text primary key,
|
|
45
|
+
binding_verifier text not null,
|
|
46
|
+
code_verifier text,
|
|
47
|
+
nonce text,
|
|
48
|
+
created_at integer not null,
|
|
49
|
+
expires_at integer not null,
|
|
50
|
+
consumed_at integer,
|
|
51
|
+
check (expires_at > created_at),
|
|
52
|
+
check (consumed_at is not null or (code_verifier is not null and nonce is not null))
|
|
53
|
+
) strict;
|
|
54
|
+
create index pending_login_by_expiry on pending_login (expires_at);
|
|
55
|
+
|
|
56
|
+
create table account_session (
|
|
57
|
+
id text primary key check (id glob 'ases_*'),
|
|
58
|
+
verifier text not null unique,
|
|
59
|
+
account_id text not null references account(id) on delete cascade,
|
|
60
|
+
authority_revision integer not null,
|
|
61
|
+
created_at integer not null,
|
|
62
|
+
expires_at integer not null,
|
|
63
|
+
revoked_at integer,
|
|
64
|
+
check (expires_at > created_at)
|
|
65
|
+
) strict;
|
|
66
|
+
|
|
67
|
+
-- A workstation. Live while revoked_at is null; revocation is permanent.
|
|
68
|
+
create table companion (
|
|
69
|
+
id text primary key check (id glob 'cmp_*'),
|
|
70
|
+
tenant_id text not null references tenant(id) on delete cascade,
|
|
71
|
+
label text not null check (length(label) between 1 and 64 and label not glob '*[^ -~]*'),
|
|
72
|
+
created_at integer not null,
|
|
73
|
+
revoked_at integer,
|
|
74
|
+
unique (id, tenant_id)
|
|
75
|
+
) strict;
|
|
76
|
+
create index companion_live_by_tenant on companion (tenant_id) where revoked_at is null;
|
|
77
|
+
|
|
78
|
+
create table device_credential (
|
|
79
|
+
id text primary key check (id glob 'cred_*'),
|
|
80
|
+
tenant_id text not null,
|
|
81
|
+
companion_id text not null,
|
|
82
|
+
purpose text not null check (purpose in ('companion_agent')),
|
|
83
|
+
verifier text not null unique,
|
|
84
|
+
device_label text check (device_label is null or length(device_label) between 1 and 64),
|
|
85
|
+
created_at integer not null,
|
|
86
|
+
revoked_at integer,
|
|
87
|
+
revoked_reason text check (revoked_reason in ('compromise','owner_request','rotation','superseded')),
|
|
88
|
+
foreign key (companion_id, tenant_id) references companion(id, tenant_id) on delete cascade,
|
|
89
|
+
check ((revoked_at is null) = (revoked_reason is null))
|
|
90
|
+
) strict;
|
|
91
|
+
create unique index device_credential_one_unrevoked on device_credential (companion_id) where revoked_at is null;
|
|
92
|
+
|
|
93
|
+
create table entitlement (
|
|
94
|
+
tenant_id text primary key references tenant(id) on delete cascade,
|
|
95
|
+
revision integer not null default 1 check (revision >= 1),
|
|
96
|
+
environment text check (environment in ('test','live')),
|
|
97
|
+
stripe_customer_id text,
|
|
98
|
+
stripe_subscription_id text,
|
|
99
|
+
subscription_state text not null default 'none' check (subscription_state in (${SUBSCRIPTION_STATES})),
|
|
100
|
+
current_period_end integer,
|
|
101
|
+
grace_invoice_id text,
|
|
102
|
+
grace_deadline integer,
|
|
103
|
+
suspended_at integer,
|
|
104
|
+
suspended_reason text check (suspended_reason is null or length(suspended_reason) between 1 and 200),
|
|
105
|
+
grant_expires_at integer,
|
|
106
|
+
grant_reason text check (grant_reason is null or (length(grant_reason) between 1 and 200 and grant_reason not glob '*[^ -~]*')),
|
|
107
|
+
grant_updated_at integer,
|
|
108
|
+
checkout_session_id text,
|
|
109
|
+
checkout_session_expires_at integer,
|
|
110
|
+
cancel_at_period_end integer not null default 0 check (cancel_at_period_end in (0, 1)),
|
|
111
|
+
cancel_at integer,
|
|
112
|
+
last_reconciled_at integer,
|
|
113
|
+
updated_at integer not null,
|
|
114
|
+
check ((grace_invoice_id is null) = (grace_deadline is null)),
|
|
115
|
+
check ((suspended_at is null) = (suspended_reason is null)),
|
|
116
|
+
check ((grant_expires_at is null) = (grant_reason is null)),
|
|
117
|
+
check (stripe_subscription_id is null or stripe_customer_id is not null),
|
|
118
|
+
check ((stripe_customer_id is null) or (environment is not null))
|
|
119
|
+
) strict;
|
|
120
|
+
create unique index entitlement_by_subscription on entitlement (environment, stripe_subscription_id)
|
|
121
|
+
where stripe_subscription_id is not null;
|
|
122
|
+
create unique index entitlement_by_customer on entitlement (environment, stripe_customer_id)
|
|
123
|
+
where stripe_customer_id is not null;
|
|
124
|
+
|
|
125
|
+
create table billing_grace_obligation (
|
|
126
|
+
environment text not null check (environment in ('test','live')),
|
|
127
|
+
invoice_id text not null,
|
|
128
|
+
tenant_id text not null references tenant(id) on delete cascade,
|
|
129
|
+
stripe_customer_id text not null,
|
|
130
|
+
stripe_subscription_id text not null,
|
|
131
|
+
first_failed_at integer not null,
|
|
132
|
+
grace_deadline integer not null,
|
|
133
|
+
primary key (environment, invoice_id),
|
|
134
|
+
check (grace_deadline > first_failed_at)
|
|
135
|
+
) strict;
|
|
136
|
+
|
|
137
|
+
-- An entitlement's grace must be one obligation of its current purchase
|
|
138
|
+
-- binding, at that obligation's fixed deadline.
|
|
139
|
+
create trigger entitlement_grace_insert_binding before insert on entitlement
|
|
140
|
+
when new.grace_invoice_id is not null and not exists (
|
|
141
|
+
select 1 from billing_grace_obligation o
|
|
142
|
+
where o.environment = new.environment and o.invoice_id = new.grace_invoice_id and o.tenant_id = new.tenant_id
|
|
143
|
+
and o.stripe_customer_id = new.stripe_customer_id and o.stripe_subscription_id = new.stripe_subscription_id
|
|
144
|
+
and o.grace_deadline = new.grace_deadline)
|
|
145
|
+
begin
|
|
146
|
+
select raise(abort, 'entitlement grace does not match an obligation of its purchase binding');
|
|
147
|
+
end;
|
|
148
|
+
|
|
149
|
+
create trigger entitlement_grace_update_binding before update on entitlement
|
|
150
|
+
when new.grace_invoice_id is not null and not exists (
|
|
151
|
+
select 1 from billing_grace_obligation o
|
|
152
|
+
where o.environment = new.environment and o.invoice_id = new.grace_invoice_id and o.tenant_id = new.tenant_id
|
|
153
|
+
and o.stripe_customer_id = new.stripe_customer_id and o.stripe_subscription_id = new.stripe_subscription_id
|
|
154
|
+
and o.grace_deadline = new.grace_deadline)
|
|
155
|
+
begin
|
|
156
|
+
select raise(abort, 'entitlement grace does not match an obligation of its purchase binding');
|
|
157
|
+
end;
|
|
158
|
+
|
|
159
|
+
create table billing_event_receipt (
|
|
160
|
+
environment text not null check (environment in ('test','live')),
|
|
161
|
+
event_id text not null check (event_id glob 'evt_*'),
|
|
162
|
+
event_type text not null check (length(event_type) between 1 and 255),
|
|
163
|
+
received_at integer not null,
|
|
164
|
+
primary key (environment, event_id)
|
|
165
|
+
) strict;
|
|
166
|
+
|
|
167
|
+
create table billing_job (
|
|
168
|
+
id text primary key check (id glob 'bjob_*'),
|
|
169
|
+
environment text not null check (environment in ('test','live')),
|
|
170
|
+
trigger text not null check (trigger in ('webhook','startup','periodic')),
|
|
171
|
+
event_id text,
|
|
172
|
+
tenant_id text references tenant(id) on delete cascade,
|
|
173
|
+
stripe_customer_id text,
|
|
174
|
+
stripe_subscription_id text,
|
|
175
|
+
stripe_invoice_id text,
|
|
176
|
+
state text not null default 'pending' check (state in ('pending','running','succeeded','failed')),
|
|
177
|
+
attempts integer not null default 0 check (attempts >= 0),
|
|
178
|
+
next_attempt_at integer not null,
|
|
179
|
+
last_error text check (last_error is null or length(last_error) <= 1000),
|
|
180
|
+
created_at integer not null,
|
|
181
|
+
updated_at integer not null,
|
|
182
|
+
foreign key (environment, event_id) references billing_event_receipt(environment, event_id),
|
|
183
|
+
check ((trigger = 'webhook') = (event_id is not null))
|
|
184
|
+
) strict;
|
|
185
|
+
create unique index billing_job_by_event on billing_job (environment, event_id) where event_id is not null;
|
|
186
|
+
create index billing_job_due on billing_job (state, next_attempt_at);
|
|
187
|
+
create index billing_job_by_tenant on billing_job (tenant_id, environment, state);
|
|
188
|
+
`;
|
|
189
|
+
function pragmaInteger(db, name) {
|
|
190
|
+
const row = db.prepare(`pragma ${name}`).get();
|
|
191
|
+
const value = row?.[name];
|
|
192
|
+
if (typeof value !== "number")
|
|
193
|
+
throw new ControlSchemaVersionError(`cannot read pragma ${name}`);
|
|
194
|
+
return value;
|
|
195
|
+
}
|
|
196
|
+
function hasUserObjects(db) {
|
|
197
|
+
return db.prepare("select 1 from sqlite_master where name not like 'sqlite_%' limit 1").get() !== undefined;
|
|
198
|
+
}
|
|
199
|
+
function assertKnownFile(db, version) {
|
|
200
|
+
const applicationId = pragmaInteger(db, "application_id");
|
|
201
|
+
if (applicationId !== 0 && applicationId !== CONTROL_PLANE_APPLICATION_ID) {
|
|
202
|
+
throw new ControlSchemaVersionError("database file is not a ClinkX control-plane store");
|
|
203
|
+
}
|
|
204
|
+
if (version > CONTROL_PLANE_SCHEMA_VERSION) {
|
|
205
|
+
throw new ControlSchemaVersionError(`control-plane schema version ${version} is newer than supported version ${CONTROL_PLANE_SCHEMA_VERSION}`);
|
|
206
|
+
}
|
|
207
|
+
if (version === 1) {
|
|
208
|
+
throw new ControlSchemaVersionError("control-plane schema version 1 is not supported; recreate the control-plane database: schema v1 predates " +
|
|
209
|
+
"operator access grants and has no migration (stop the gateway and remove the control-plane volume)");
|
|
210
|
+
}
|
|
211
|
+
if (version !== 0 && version !== CONTROL_PLANE_SCHEMA_VERSION) {
|
|
212
|
+
throw new ControlSchemaVersionError(`control-plane schema version ${version} is not supported`);
|
|
213
|
+
}
|
|
214
|
+
if (version === CONTROL_PLANE_SCHEMA_VERSION && applicationId !== CONTROL_PLANE_APPLICATION_ID) {
|
|
215
|
+
throw new ControlSchemaVersionError("database file is not a ClinkX control-plane store");
|
|
216
|
+
}
|
|
217
|
+
if (version === 0 && hasUserObjects(db)) {
|
|
218
|
+
throw new ControlSchemaVersionError("database file has tables but no control-plane schema version");
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* Create the schema on an empty file or verify an existing one. There is no
|
|
223
|
+
* migration path: any version other than the current one refuses to open.
|
|
224
|
+
*/
|
|
225
|
+
export function applyControlPlaneSchema(db) {
|
|
226
|
+
assertKnownFile(db, pragmaInteger(db, "user_version"));
|
|
227
|
+
if (pragmaInteger(db, "user_version") === CONTROL_PLANE_SCHEMA_VERSION)
|
|
228
|
+
return;
|
|
229
|
+
db.exec("begin immediate");
|
|
230
|
+
try {
|
|
231
|
+
// Another process may have initialized the file while this one waited for the lock.
|
|
232
|
+
const version = pragmaInteger(db, "user_version");
|
|
233
|
+
assertKnownFile(db, version);
|
|
234
|
+
if (version === 0) {
|
|
235
|
+
db.exec(SCHEMA_V2);
|
|
236
|
+
db.exec(`pragma application_id = ${CONTROL_PLANE_APPLICATION_ID}`);
|
|
237
|
+
db.exec(`pragma user_version = ${CONTROL_PLANE_SCHEMA_VERSION}`);
|
|
238
|
+
}
|
|
239
|
+
db.exec("commit");
|
|
240
|
+
}
|
|
241
|
+
catch (error) {
|
|
242
|
+
if (db.isTransaction)
|
|
243
|
+
db.exec("rollback");
|
|
244
|
+
throw error;
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
//# sourceMappingURL=schema.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"schema.js","sourceRoot":"","sources":["../../../src/dashboard-gateway/control-plane/schema.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,uBAAuB,EAAE,MAAM,gBAAgB,CAAC;AACzD,OAAO,EAAE,yBAAyB,EAAE,MAAM,aAAa,CAAC;AAExD,MAAM,CAAC,MAAM,4BAA4B,GAAG,CAAC,CAAC;AAC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,CAAC,IAAI,CAAC,2BAA2B,4BAA4B,EAAE,CAAC,CAAC;YACnE,EAAE,CAAC,IAAI,CAAC,yBAAyB,4BAA4B,EAAE,CAAC,CAAC;QACnE,CAAC;QACD,EAAE,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;IACpB,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,IAAI,EAAE,CAAC,aAAa;YAAE,EAAE,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;QAC1C,MAAM,KAAK,CAAC;IACd,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export declare const SECRET_BYTES = 32;
|
|
2
|
+
export declare const SECRET_PREFIX: {
|
|
3
|
+
readonly deviceCredential: "clxd";
|
|
4
|
+
readonly session: "clxs";
|
|
5
|
+
readonly loginBinding: "clxl";
|
|
6
|
+
};
|
|
7
|
+
export type SecretPrefix = (typeof SECRET_PREFIX)[keyof typeof SECRET_PREFIX];
|
|
8
|
+
/**
|
|
9
|
+
* Verifier purposes keep a secret issued for one table from ever matching a
|
|
10
|
+
* verifier stored in another, independently of the textual prefix.
|
|
11
|
+
*/
|
|
12
|
+
export type VerifierPurpose = "account_session" | "device_credential" | "login_state" | "login_binding";
|
|
13
|
+
export declare function generateOpaqueSecret(prefix: SecretPrefix): string;
|
|
14
|
+
export declare function generateNonce(): string;
|
|
15
|
+
export declare function isOpaqueSecret(prefix: SecretPrefix, value: unknown): value is string;
|
|
16
|
+
export declare function deriveVerifier(purpose: VerifierPurpose, secret: string): string;
|
|
17
|
+
export declare function nonceDigest(nonce: string): string;
|
|
18
|
+
export declare function constantTimeEqual(expected: string, actual: string): boolean;
|
|
19
|
+
export type ControlIdPrefix = "acc" | "ten" | "cmp" | "cred" | "ases" | "bjob";
|
|
20
|
+
export declare function generateControlId(prefix: ControlIdPrefix): string;
|