@k9o/extension-runtime 0.0.0-stage → 1.6.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/LICENSE +201 -0
- package/NOTICE +4 -0
- package/README.md +166 -2
- package/package.json +30 -4
- package/src/base64.ts +11 -0
- package/src/bff-hook-discriminator.ts +72 -0
- package/src/client.ts +148 -0
- package/src/context.ts +70 -0
- package/src/dispatch-context.ts +58 -0
- package/src/guards.ts +35 -0
- package/src/http.ts +233 -0
- package/src/index.ts +163 -0
- package/src/insecure-http.ts +575 -0
- package/src/invoke-server.ts +150 -0
- package/src/kerror.ts +45 -0
- package/src/namespaces/ai-agent-runtime.ts +170 -0
- package/src/namespaces/ai.ts +56 -0
- package/src/namespaces/analytics.ts +122 -0
- package/src/namespaces/attributes.ts +34 -0
- package/src/namespaces/audit.ts +92 -0
- package/src/namespaces/auth-migrations.ts +176 -0
- package/src/namespaces/bff.ts +140 -0
- package/src/namespaces/collections.ts +104 -0
- package/src/namespaces/compliance.ts +48 -0
- package/src/namespaces/credentials.ts +85 -0
- package/src/namespaces/documents.ts +128 -0
- package/src/namespaces/egress.ts +31 -0
- package/src/namespaces/files.ts +96 -0
- package/src/namespaces/functions.ts +91 -0
- package/src/namespaces/hooks.ts +186 -0
- package/src/namespaces/identity.ts +97 -0
- package/src/namespaces/kibo.ts +60 -0
- package/src/namespaces/policy.ts +50 -0
- package/src/namespaces/streaming.ts +80 -0
- package/src/namespaces/tables.ts +166 -0
- package/src/namespaces/vectors.ts +243 -0
- package/src/namespaces/webhook-ingress.ts +98 -0
- package/src/namespaces/workflow.ts +65 -0
- package/src/types.ts +143 -0
- package/src/warden.ts +320 -0
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* k9o.authMigrations — the sign-in migration extension's three calls: register a run, read it, and have the platform
|
|
3
|
+
* create the run's users.
|
|
4
|
+
*
|
|
5
|
+
* Only an installed sign-in migration extension calls these, with its plugin token; the platform refuses any other
|
|
6
|
+
* extension with `K9O_EXTENSION_PERMISSION_DENIED`. The source vendor comes from the extension, never from the caller, and
|
|
7
|
+
* the source account is read from the credential bound to `credentialRole` and pinned to the run. The platform sends back
|
|
8
|
+
* a run without any hash, seed or credential. The extension sends user ids and addresses only; the vendor's password
|
|
9
|
+
* export, the hash parameters, rollback, finalize and end are a project admin's, not an extension's.
|
|
10
|
+
*
|
|
11
|
+
* Refusals reach the caller as the route gave them (`K9O_NOT_FOUND` for a run id that does not exist or belongs to another
|
|
12
|
+
* extension, `K9O_EXTENSION_PERMISSION_DENIED`,
|
|
13
|
+
* `K9O_INVALID_FIELD_VALUE`). A value the route documents a hard limit for is checked here first and refused with
|
|
14
|
+
* `K9O_INVALID_FIELD_VALUE`, with no network call.
|
|
15
|
+
*
|
|
16
|
+
* Gateway routes:
|
|
17
|
+
* POST /@{projectSlug}/auth-migrations body CreateRunBody, 201 with the run
|
|
18
|
+
* GET /@{projectSlug}/auth-migrations/{runId} the run
|
|
19
|
+
* POST /@{projectSlug}/auth-migrations/{runId}/users body { users }, 200 with { users } (one result per user)
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
import type { Transport } from "../http.ts";
|
|
23
|
+
import { RuntimeKError } from "../kerror.ts";
|
|
24
|
+
|
|
25
|
+
/** The most users one `createUsers` call takes. */
|
|
26
|
+
export const AUTH_MIGRATIONS_MAX_USERS_PER_CALL = 1000;
|
|
27
|
+
/** The most days unused imported hashes can be kept. */
|
|
28
|
+
export const AUTH_MIGRATIONS_HASH_EXPIRY_MAX_DAYS = 365;
|
|
29
|
+
/** The most days the rollback snapshot can be kept after cutover. */
|
|
30
|
+
export const AUTH_MIGRATIONS_ROLLBACK_RETENTION_MAX_DAYS = 365;
|
|
31
|
+
|
|
32
|
+
export type AuthMigrationSource = "auth0" | "clerk" | "firebase" | "supabase" | "stytch";
|
|
33
|
+
export type AuthMigrationPasswordHandling = "hash_import" | "reset";
|
|
34
|
+
|
|
35
|
+
export type AuthMigrationsCreateRunBody = {
|
|
36
|
+
/** `hash_import` keeps users' passwords (the platform refuses it for Auth0 and Stytch for now); `reset` has them set a new one. */
|
|
37
|
+
passwordHandling: AuthMigrationPasswordHandling;
|
|
38
|
+
/** The manifest credential role bound to the source vendor's API credential: 1 to 64 characters of a-z, 0-9 and `-`. */
|
|
39
|
+
credentialRole: string;
|
|
40
|
+
/** Days until unused imported hashes are deleted, 1 to 365 (default 90). */
|
|
41
|
+
hashExpiresInDays?: number;
|
|
42
|
+
/** Days the rollback snapshot is kept after cutover, 1 to 365 (default 90). */
|
|
43
|
+
rollbackRetentionDays?: number;
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
/** A sign-in migration run. It never carries a hash, a seed or a credential. */
|
|
47
|
+
export type AuthMigrationRun = {
|
|
48
|
+
/** The run's id (`amr_...`). */
|
|
49
|
+
id: string;
|
|
50
|
+
/** The vendor the users come from, set by the installed extension. */
|
|
51
|
+
source: AuthMigrationSource;
|
|
52
|
+
/** The migration extension that registered the run. */
|
|
53
|
+
extensionSlug: string;
|
|
54
|
+
/** The extension's credential role whose bound credential identifies the source account. */
|
|
55
|
+
credentialRole: string;
|
|
56
|
+
passwordHandling: AuthMigrationPasswordHandling;
|
|
57
|
+
/** The source account the run is pinned to, read from the credential when the run was created. */
|
|
58
|
+
sourceAccount: string;
|
|
59
|
+
/** `per_row`: each row's hash is accepted when its format is one the platform verifies. `awaiting_format_ruling`: this source's hashes are not imported yet. */
|
|
60
|
+
hashImport: "per_row" | "awaiting_format_ruling";
|
|
61
|
+
/** Whether authenticator-app seeds carry over from this source. `none`: those users enrol again. */
|
|
62
|
+
totpCarryOver: "none" | "awaiting_format_ruling";
|
|
63
|
+
/** Whether a rollback can restore authenticator-app factors at the source. */
|
|
64
|
+
rollbackRestoresTotp: boolean;
|
|
65
|
+
/** Firebase only: whether the project's hash parameters are entered. */
|
|
66
|
+
hashParametersSet: boolean;
|
|
67
|
+
/** ISO 8601 date-time when unused imported hashes are deleted; those users then reset their passwords. */
|
|
68
|
+
hashExpiresAt: string;
|
|
69
|
+
/** How long the rollback snapshot is kept, counted from cutover, 1 to 365 days. */
|
|
70
|
+
rollbackRetentionDays: number;
|
|
71
|
+
/** When the first secrets upload ran, or null before it. */
|
|
72
|
+
cutoverAt: string | null;
|
|
73
|
+
/** When the rollback snapshot is deleted, or null when there is none. */
|
|
74
|
+
snapshotExpiresAt: string | null;
|
|
75
|
+
/** `held`: a rollback can run. `pin_changed`: the source credential changed, so the snapshot can only be deleted. */
|
|
76
|
+
snapshotState: "none" | "held" | "pin_changed" | "pushed" | "deleted";
|
|
77
|
+
status: "open" | "finalized" | "rolled_back" | "ended";
|
|
78
|
+
createdAt: string;
|
|
79
|
+
updatedAt: string;
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
export type AuthMigrationUser = {
|
|
83
|
+
/** The user's id at the source, 1 to 256 characters. */
|
|
84
|
+
sourceId: string;
|
|
85
|
+
/** The user's address, 3 to 254 characters. */
|
|
86
|
+
email: string;
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
export type AuthMigrationsCreateUsersOptions = {
|
|
90
|
+
/** 1 to 1000 users. */
|
|
91
|
+
users: AuthMigrationUser[];
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
export type AuthMigrationUserResult = {
|
|
95
|
+
/** The user's id at the source. */
|
|
96
|
+
sourceId: string;
|
|
97
|
+
/** The end user created for it (`eu_...`), or null when refused. */
|
|
98
|
+
endUserId: string | null;
|
|
99
|
+
/** `email_exists`: an end user already has this address; it is never mapped and gets no imported credential. */
|
|
100
|
+
status: "created" | "already_created" | "email_exists";
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
export type AuthMigrationsCreateUsersResult = {
|
|
104
|
+
users: AuthMigrationUserResult[];
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
export type AuthMigrationsNamespace = {
|
|
108
|
+
createRun(body: AuthMigrationsCreateRunBody): Promise<AuthMigrationRun>;
|
|
109
|
+
getRun(runId: string): Promise<AuthMigrationRun>;
|
|
110
|
+
createUsers(runId: string, options: AuthMigrationsCreateUsersOptions): Promise<AuthMigrationsCreateUsersResult>;
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
const invalidField = (field: string, message: string, extra: Record<string, unknown> = {}): RuntimeKError =>
|
|
114
|
+
new RuntimeKError("K9O_INVALID_FIELD_VALUE", message, { field, ...extra });
|
|
115
|
+
|
|
116
|
+
function checkDays(field: string, value: unknown, max: number): void {
|
|
117
|
+
if (value === undefined) return;
|
|
118
|
+
if (typeof value !== "number" || !Number.isInteger(value) || value < 1 || value > max) {
|
|
119
|
+
throw invalidField(field, `${field} must be a whole number of days from 1 to ${max}`, { max });
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function checkRunId(runId: unknown): asserts runId is string {
|
|
124
|
+
if (typeof runId !== "string" || runId.length === 0) throw invalidField("runId", "runId must be a non-empty string");
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export function makeAuthMigrations(transport: Transport, projectSlug: string): AuthMigrationsNamespace {
|
|
128
|
+
const base = `/@${projectSlug}/auth-migrations`;
|
|
129
|
+
return {
|
|
130
|
+
async createRun(body) {
|
|
131
|
+
const { passwordHandling, credentialRole, hashExpiresInDays, rollbackRetentionDays } = body;
|
|
132
|
+
if (passwordHandling !== "hash_import" && passwordHandling !== "reset") {
|
|
133
|
+
throw invalidField("passwordHandling", 'passwordHandling must be "hash_import" or "reset"');
|
|
134
|
+
}
|
|
135
|
+
if (typeof credentialRole !== "string" || !/^[a-z0-9-]{1,64}$/.test(credentialRole)) {
|
|
136
|
+
throw invalidField("credentialRole", "credentialRole must be 1 to 64 characters of a-z, 0-9 and -");
|
|
137
|
+
}
|
|
138
|
+
checkDays("hashExpiresInDays", hashExpiresInDays, AUTH_MIGRATIONS_HASH_EXPIRY_MAX_DAYS);
|
|
139
|
+
checkDays("rollbackRetentionDays", rollbackRetentionDays, AUTH_MIGRATIONS_ROLLBACK_RETENTION_MAX_DAYS);
|
|
140
|
+
const payload: AuthMigrationsCreateRunBody = { passwordHandling, credentialRole };
|
|
141
|
+
if (hashExpiresInDays !== undefined) payload.hashExpiresInDays = hashExpiresInDays;
|
|
142
|
+
if (rollbackRetentionDays !== undefined) payload.rollbackRetentionDays = rollbackRetentionDays;
|
|
143
|
+
const res = await transport.post<AuthMigrationRun>(base, payload);
|
|
144
|
+
return res.data;
|
|
145
|
+
},
|
|
146
|
+
|
|
147
|
+
async getRun(runId) {
|
|
148
|
+
checkRunId(runId);
|
|
149
|
+
const res = await transport.get<AuthMigrationRun>(`${base}/${encodeURIComponent(runId)}`);
|
|
150
|
+
return res.data;
|
|
151
|
+
},
|
|
152
|
+
|
|
153
|
+
async createUsers(runId, options) {
|
|
154
|
+
checkRunId(runId);
|
|
155
|
+
const users = options.users;
|
|
156
|
+
if (!Array.isArray(users) || users.length < 1) throw invalidField("users", "users must hold at least 1 user");
|
|
157
|
+
if (users.length > AUTH_MIGRATIONS_MAX_USERS_PER_CALL) {
|
|
158
|
+
throw invalidField("users", `users takes at most ${AUTH_MIGRATIONS_MAX_USERS_PER_CALL} users per call; send more in further calls`, {
|
|
159
|
+
max: AUTH_MIGRATIONS_MAX_USERS_PER_CALL,
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
users.forEach((u, i) => {
|
|
163
|
+
if (typeof u?.sourceId !== "string" || u.sourceId.length < 1 || u.sourceId.length > 256) {
|
|
164
|
+
throw invalidField(`users[${i}].sourceId`, "sourceId must be 1 to 256 characters");
|
|
165
|
+
}
|
|
166
|
+
if (typeof u.email !== "string" || u.email.length < 3 || u.email.length > 254) {
|
|
167
|
+
throw invalidField(`users[${i}].email`, "email must be 3 to 254 characters");
|
|
168
|
+
}
|
|
169
|
+
});
|
|
170
|
+
const res = await transport.post<AuthMigrationsCreateUsersResult>(`${base}/${encodeURIComponent(runId)}/users`, {
|
|
171
|
+
users: users.map((u) => ({ sourceId: u.sourceId, email: u.email })),
|
|
172
|
+
});
|
|
173
|
+
return res.data;
|
|
174
|
+
},
|
|
175
|
+
};
|
|
176
|
+
}
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* k9o.bff — BFF (Backend-for-Frontend) handler registration namespace.
|
|
3
|
+
*
|
|
4
|
+
* Extension authors register BFF endpoint handlers via `k9o.bff.handle(path, handler)`.
|
|
5
|
+
* The gateway forwarder sends a `BffRequest` and wraps nothing around the
|
|
6
|
+
* response: when the endpoint declares `version_attribute`, the gateway spreads `version` at the
|
|
7
|
+
* top of the JSON body next to the handler's own keys; there is no
|
|
8
|
+
* `data` wrapper. A handler returning its own top-level `version` is
|
|
9
|
+
* `K9O_EXTENSION_BFF_VERSION_COLLISION`.
|
|
10
|
+
*
|
|
11
|
+
* Extensions declare BFF endpoints in `manifest.externalEndpoints[]`;
|
|
12
|
+
* `k9o.bff.handle` registers the runtime implementation. Handler path collisions (two
|
|
13
|
+
* `handle()` calls for the same path) overwrite the first registration — last-write-wins.
|
|
14
|
+
*
|
|
15
|
+
* The operator JWT is never sent to the extension; the gateway verifies it and passes the
|
|
16
|
+
* verified claims as `req.operator`.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import type { DispatchContext } from "../dispatch-context.ts";
|
|
20
|
+
|
|
21
|
+
/** Verified-claims caller, minimal: no email or name unless the endpoint declares it. */
|
|
22
|
+
export type BffOperator = {
|
|
23
|
+
/** Subject identity — typically `user_id`. */
|
|
24
|
+
sub: string;
|
|
25
|
+
roles: string[];
|
|
26
|
+
org_id: string;
|
|
27
|
+
project_id: string;
|
|
28
|
+
request_id: string;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
export type BffHandler = (
|
|
32
|
+
req: BffRequest,
|
|
33
|
+
/** Per-dispatch context: build the callback client from `ctx.client`, never from load-time env. */
|
|
34
|
+
ctx: DispatchContext,
|
|
35
|
+
) => Promise<BffResponse> | BffResponse;
|
|
36
|
+
|
|
37
|
+
export type BffRequest = {
|
|
38
|
+
/** The matched declared path template (e.g. `/forms/:formId`), never the raw request path. */
|
|
39
|
+
path: string;
|
|
40
|
+
/** Values bound to the template's `:name` segments (one segment each; gateway-validated). */
|
|
41
|
+
params: Record<string, string>;
|
|
42
|
+
/** HTTP method from the inbound request (uppercase; e.g., `"GET"`, `"POST"`). */
|
|
43
|
+
method: string;
|
|
44
|
+
/** Explicit parsed query string. */
|
|
45
|
+
query: Record<string, string | string[]>;
|
|
46
|
+
/**
|
|
47
|
+
* Inbound headers forwarded by the gateway through an allowlist: `content-type`, `accept`,
|
|
48
|
+
* `accept-language`, `if-match`, `if-none-match`, `x-request-id`. Never cookies or authorization.
|
|
49
|
+
*/
|
|
50
|
+
headers: Record<string, string>;
|
|
51
|
+
/** Request body parsed as JSON (JSON only at launch), or `null` when no body is present. */
|
|
52
|
+
body: unknown;
|
|
53
|
+
/**
|
|
54
|
+
* Verified-claims caller. Present on `operator-jwt` endpoints; absent on `anonymous` ones.
|
|
55
|
+
* Branch on `req.operator?.sub`.
|
|
56
|
+
*/
|
|
57
|
+
operator?: BffOperator;
|
|
58
|
+
/**
|
|
59
|
+
* Who called when no operator did, set by the gateway: another extension on an `extension` endpoint, or the
|
|
60
|
+
* platform's scheduler on a `schedule` endpoint (`name` is the schedule's, from the manifest), the platform's event
|
|
61
|
+
* delivery on an `event` endpoint (`name` is the declared event subscription's), or the subject-erasure process calling
|
|
62
|
+
* the extension's declared erasure hook (`erasure`), the platform on a route declared `auth: "platform"` (`purpose` is `password_range` for a breached-password lookup, `setting_verify` when an admin presses Verify on an install setting that declares `verify`), a project service token, or an end user.
|
|
63
|
+
*/
|
|
64
|
+
caller?:
|
|
65
|
+
| { kind: "extension"; extensionId: string; slug: string }
|
|
66
|
+
| { kind: "schedule"; name: string }
|
|
67
|
+
| { kind: "event"; name: string }
|
|
68
|
+
| { kind: "erasure" }
|
|
69
|
+
| { kind: "platform"; purpose: "password_range" }
|
|
70
|
+
| { kind: "platform"; purpose: "setting_verify" }
|
|
71
|
+
| { kind: "service_token"; tokenId: string; role: string }
|
|
72
|
+
| { kind: "end_user"; endUserId: string; appId: string | null; amr: string[] | null; authTime: number | null };
|
|
73
|
+
/** Where the call chain started, set by the gateway: an operator, or `system` for a scheduled run. */
|
|
74
|
+
origin?: { kind: "operator"; userId: string; role: string; run?: { id: string; step: number } } | { kind: "system" };
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
export type BffResponse = {
|
|
78
|
+
/** HTTP status code for the response (default: 200). */
|
|
79
|
+
status?: number;
|
|
80
|
+
/** Response body — serialised to JSON by the gateway; passes through unwrapped. */
|
|
81
|
+
body: unknown;
|
|
82
|
+
/**
|
|
83
|
+
* Optional response headers, allowlisted by the gateway: `content-type`, `content-length`,
|
|
84
|
+
* `cache-control`, `etag`, `last-modified`, `content-disposition`, and `location` only as a
|
|
85
|
+
* relative path under the mount. `set-cookie` is always dropped.
|
|
86
|
+
*/
|
|
87
|
+
headers?: Record<string, string>;
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
export type BffNamespace = {
|
|
91
|
+
/**
|
|
92
|
+
* Register a handler for a BFF endpoint path.
|
|
93
|
+
* Registering the same path twice overwrites the first handler (last-write-wins).
|
|
94
|
+
*
|
|
95
|
+
* @param path Must match a path declared in `manifest.externalEndpoints[]`.
|
|
96
|
+
* @param handler Sync or async function receiving a `BffRequest` + returning `BffResponse`.
|
|
97
|
+
*/
|
|
98
|
+
handle(path: string, handler: BffHandler): void;
|
|
99
|
+
/**
|
|
100
|
+
* Invoke the registered handler for a BFF request.
|
|
101
|
+
* Used by the K9O gateway runtime to route incoming BFF requests.
|
|
102
|
+
* Returns `{ status: 404, body: { error: "..." } }` when no handler is registered.
|
|
103
|
+
*/
|
|
104
|
+
dispatch(req: BffRequest, ctx: DispatchContext): Promise<BffResponse>;
|
|
105
|
+
/**
|
|
106
|
+
* Inspect the set of paths that have a registered handler. Returns a
|
|
107
|
+
* frozen snapshot — does not reflect subsequent `handle()` calls.
|
|
108
|
+
*
|
|
109
|
+
* Use cases: partner-CLI simulate-time cross-check against
|
|
110
|
+
* `manifest.externalEndpoints[]` (warn on declared-but-unregistered or
|
|
111
|
+
* registered-but-undeclared paths); test assertions for side-effect
|
|
112
|
+
* registration in bundle entry points; debugging.
|
|
113
|
+
*/
|
|
114
|
+
registeredPaths(): readonly string[];
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
export function makeBff(): BffNamespace {
|
|
118
|
+
const registry = new Map<string, BffHandler>();
|
|
119
|
+
|
|
120
|
+
return {
|
|
121
|
+
handle(path, handler) {
|
|
122
|
+
registry.set(path, handler);
|
|
123
|
+
},
|
|
124
|
+
|
|
125
|
+
async dispatch(req, ctx) {
|
|
126
|
+
const handler = registry.get(req.path);
|
|
127
|
+
if (!handler) {
|
|
128
|
+
return {
|
|
129
|
+
status: 404,
|
|
130
|
+
body: { error: `No BFF handler registered for path: ${req.path}` },
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
return await handler(req, ctx);
|
|
134
|
+
},
|
|
135
|
+
|
|
136
|
+
registeredPaths() {
|
|
137
|
+
return Object.freeze([...registry.keys()]);
|
|
138
|
+
},
|
|
139
|
+
};
|
|
140
|
+
}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* k9o.collections — collection-level read/write + ch04.
|
|
3
|
+
*
|
|
4
|
+
* Extensions read collection metadata (slug, name) and its schema descriptor, and can update
|
|
5
|
+
* mutable collection properties where the manifest grants collections/write.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import type { Transport } from "../http.ts";
|
|
9
|
+
import type { ApiResponse, Collection } from "../types.ts";
|
|
10
|
+
|
|
11
|
+
/** field-level at-rest protection descriptor; `null` on a column means unprotected. */
|
|
12
|
+
export type ColumnProtection = {
|
|
13
|
+
encrypted: boolean;
|
|
14
|
+
/** `inline` = Mode C (ciphertext in the row); `vault` = Mode D (vault token). */
|
|
15
|
+
store: "inline" | "vault";
|
|
16
|
+
blindIndexed: boolean;
|
|
17
|
+
searchable: boolean;
|
|
18
|
+
/** True when a reveal gate is present (its presence, not its rule). */
|
|
19
|
+
revealGated: boolean;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
export type CollectionSchemaColumn = {
|
|
23
|
+
name: string;
|
|
24
|
+
type: string;
|
|
25
|
+
nullable: boolean;
|
|
26
|
+
isPrimary: boolean;
|
|
27
|
+
isUnique: boolean;
|
|
28
|
+
default: string | null;
|
|
29
|
+
vectorDimension: number | null;
|
|
30
|
+
/** Platform-reserved column (name begins with `_`). */
|
|
31
|
+
reserved: boolean;
|
|
32
|
+
/** True for the collection's per-subject owner column (RC-6 erasure attribution). */
|
|
33
|
+
isOwnerColumn: boolean;
|
|
34
|
+
protection: ColumnProtection | null;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
export type CollectionSchemaIndex = {
|
|
38
|
+
name: string;
|
|
39
|
+
columns: string[];
|
|
40
|
+
type: string;
|
|
41
|
+
unique: boolean;
|
|
42
|
+
status: "active" | "building" | "invalid";
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
export type CollectionSchemaRelationship = {
|
|
46
|
+
column: string;
|
|
47
|
+
joinAlias: string;
|
|
48
|
+
joinType: string;
|
|
49
|
+
targetCollection: string;
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
/** The schema-introspection descriptor for one collection. */
|
|
53
|
+
export type CollectionSchema = {
|
|
54
|
+
name: string;
|
|
55
|
+
rowCount: number;
|
|
56
|
+
rowCountIsExact: boolean;
|
|
57
|
+
lastMigrationSlug: string | null;
|
|
58
|
+
lastMigrationAppliedAt: string | null;
|
|
59
|
+
/** The per-subject owner column, or null for a shared / non-per-subject collection. */
|
|
60
|
+
ownerColumn: string | null;
|
|
61
|
+
columns: CollectionSchemaColumn[];
|
|
62
|
+
relationships: CollectionSchemaRelationship[];
|
|
63
|
+
indexes: CollectionSchemaIndex[];
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* One protected column of the project (`GET /@<project>/schema/protected-columns`): names and protection only,
|
|
68
|
+
* never a value. Needs the `schema read ["protected_columns"]` grant.
|
|
69
|
+
*/
|
|
70
|
+
export type ProtectedColumn = {
|
|
71
|
+
collection: string;
|
|
72
|
+
column: string;
|
|
73
|
+
store: string;
|
|
74
|
+
searchable: boolean;
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
export type CollectionsNamespace = {
|
|
78
|
+
read(): Promise<ApiResponse<Collection[]>>;
|
|
79
|
+
/**
|
|
80
|
+
* Schema descriptor for one collection (`GET /@<project>/schema/collections/<name>`).
|
|
81
|
+
* Protection and `ownerColumn` are platform facts: take them from here, never from a caller.
|
|
82
|
+
*/
|
|
83
|
+
schema(name: string): Promise<ApiResponse<CollectionSchema>>;
|
|
84
|
+
/** The project's protected columns, names only (`GET /@<project>/schema/protected-columns`). Needs `schema read ["protected_columns"]`. */
|
|
85
|
+
protectedColumns(): Promise<ApiResponse<ProtectedColumn[]>>;
|
|
86
|
+
write(id: string, updates: Partial<Pick<Collection, "name">>): Promise<ApiResponse<Collection>>;
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
export function makeCollections(
|
|
90
|
+
transport: Transport,
|
|
91
|
+
projectSlug: string,
|
|
92
|
+
): CollectionsNamespace {
|
|
93
|
+
const base = `/@${projectSlug}/collections`;
|
|
94
|
+
|
|
95
|
+
return {
|
|
96
|
+
read: () => transport.get<Collection[]>(base),
|
|
97
|
+
schema: (name) =>
|
|
98
|
+
transport.get<CollectionSchema>(
|
|
99
|
+
`/@${projectSlug}/schema/collections/${encodeURIComponent(name)}`,
|
|
100
|
+
),
|
|
101
|
+
protectedColumns: () => transport.get<ProtectedColumn[]>(`/@${projectSlug}/schema/protected-columns`),
|
|
102
|
+
write: (id, updates) => transport.patch<Collection>(`${base}/${id}`, updates),
|
|
103
|
+
};
|
|
104
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* k9o.compliance — compliance-export EXTENSION-CALLER path.
|
|
3
|
+
*
|
|
4
|
+
* The MINT (`POST .../compliance-export-delegate`) is operator-authed, not
|
|
5
|
+
* extension-authed — an operator holding PROJECT_AUDITOR/PROJECT_ADMIN mints a
|
|
6
|
+
* short-lived delegate token and hands it to the extension out of band (e.g. as
|
|
7
|
+
* an input to the extension's own export function). This namespace is only the
|
|
8
|
+
* FETCH half: the extension presents its own plugin-token bearer (standard
|
|
9
|
+
* Authorization, injected by Transport) PLUS the delegate token via the
|
|
10
|
+
* `X-K9O-Compliance-Delegate` header (never the bearer — a separate credential
|
|
11
|
+
* per the gateway's own fail-closed checks in
|
|
12
|
+
* routes/compliance-export-delegate.ts).
|
|
13
|
+
*
|
|
14
|
+
* Manifest requirement: the extension must declare the `compliance_bundle`
|
|
15
|
+
* `export` permission — a transparency-only declaration; the actual
|
|
16
|
+
* authority is the delegate token, never this manifest grant alone.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import type { Transport } from "../http.ts";
|
|
20
|
+
import type { ApiResponse, CredentialAttestationBundle } from "../types.ts";
|
|
21
|
+
|
|
22
|
+
export type ComplianceNamespace = {
|
|
23
|
+
/**
|
|
24
|
+
* Fetch the compliance attestation bundle using an operator-minted delegate
|
|
25
|
+
* token. `body` is forwarded byte-identical to kibo's compliance-export
|
|
26
|
+
* endpoint (e.g. a window/date-range selector) — optional, shape owned by
|
|
27
|
+
* the kibo side, not validated here.
|
|
28
|
+
*/
|
|
29
|
+
export(
|
|
30
|
+
delegateToken: string,
|
|
31
|
+
body?: Record<string, unknown>,
|
|
32
|
+
): Promise<ApiResponse<CredentialAttestationBundle>>;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
export function makeCompliance(
|
|
36
|
+
transport: Transport,
|
|
37
|
+
projectSlug: string,
|
|
38
|
+
extensionSlug: string,
|
|
39
|
+
): ComplianceNamespace {
|
|
40
|
+
const base = `/@${projectSlug}/extensions-admin/${extensionSlug}`;
|
|
41
|
+
|
|
42
|
+
return {
|
|
43
|
+
export: (delegateToken, body) =>
|
|
44
|
+
transport.post<CredentialAttestationBundle>(`${base}/compliance-export`, body, {
|
|
45
|
+
"X-K9O-Compliance-Delegate": delegateToken,
|
|
46
|
+
}),
|
|
47
|
+
};
|
|
48
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* k9o.credentials.send — authenticated_send, the extension's half.
|
|
3
|
+
*
|
|
4
|
+
* Asks the platform to send ONE request to the destination bound to one of the extension's declared credential roles,
|
|
5
|
+
* signed with that role's credential. The extension never sees the secret; the platform checks the grant, the binding,
|
|
6
|
+
* the destination pins and the credential version, and drops a response that carries the credential back.
|
|
7
|
+
*
|
|
8
|
+
* Manifest requirement: a `(egress, authenticated_send, <role>)` permission for every role named here, and a
|
|
9
|
+
* `requires` entry for the role. Without the grant the route refuses and `send` throws that refusal (a RuntimeKError
|
|
10
|
+
* with the route's own code, message and context). Nothing here retries, falls back to `k9o.fetch`, or answers
|
|
11
|
+
* for the platform: every refusal reaches the caller as the route gave it (K9O_EXTENSION_PERMISSION_DENIED,
|
|
12
|
+
* K9O_INVALID_FIELD_VALUE, K9O_RATE_LIMITED, K9O_ACCESS_DENIED for a reflected credential, K9O_UPSTREAM_UNAVAILABLE ...).
|
|
13
|
+
*
|
|
14
|
+
* Name the destination one of two ways, never both: `url`, the full destination as bound for the role (the route
|
|
15
|
+
* resolves it against the binding's pins and refuses anything outside them); or `path` (with its query), for a role
|
|
16
|
+
* bound by origin, where the platform supplies the host from the binding and refuses a `url`. A `string` body is sent
|
|
17
|
+
* as UTF-8; bytes are sent as base64 (`bodyEncoding: "base64"`). The answer's `body` is decoded from the route's base64.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
import { base64ToBytes, bytesToBase64 } from "../base64.ts";
|
|
21
|
+
import type { Transport } from "../http.ts";
|
|
22
|
+
import { RuntimeKError } from "../kerror.ts";
|
|
23
|
+
|
|
24
|
+
export type CredentialSendRequest = {
|
|
25
|
+
method: "GET" | "HEAD" | "POST" | "PUT" | "PATCH" | "DELETE" | (string & {});
|
|
26
|
+
headers?: Record<string, string>;
|
|
27
|
+
body?: string | Uint8Array | ArrayBuffer;
|
|
28
|
+
/**
|
|
29
|
+
* A push send only: the `deviceLease` carried by the push frame. The platform inserts the device's token into the
|
|
30
|
+
* request and chooses the host from the device's environment, so the extension never holds either. A role that sends
|
|
31
|
+
* push notifications is refused without it (`K9O_ACCESS_DENIED`, reason `push_lease_required`). The lease is passed
|
|
32
|
+
* on exactly as received.
|
|
33
|
+
*/
|
|
34
|
+
deviceLease?: string;
|
|
35
|
+
} & ({ url: string; path?: never } | { path: string; url?: never });
|
|
36
|
+
|
|
37
|
+
export type CredentialSendResult = {
|
|
38
|
+
status: number;
|
|
39
|
+
/** The destination's response headers, after the platform's filter. */
|
|
40
|
+
headers: Record<string, string>;
|
|
41
|
+
/** The response body, decoded from base64. */
|
|
42
|
+
body: Uint8Array;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
export type CredentialsNamespace = {
|
|
46
|
+
send(role: string, request: CredentialSendRequest): Promise<CredentialSendResult>;
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
type SendData = { status: number; headers: Record<string, string>; body: string; bodyEncoding: "base64" };
|
|
50
|
+
|
|
51
|
+
const malformed = (role: string, why: string): RuntimeKError =>
|
|
52
|
+
new RuntimeKError("K9O_UPSTREAM_UNAVAILABLE", `The authenticated send answer was not usable: ${why}`, { role, reason: "malformed_send_answer" }, null);
|
|
53
|
+
|
|
54
|
+
export function makeCredentials(transport: Transport, projectSlug: string, extensionSlug: string): CredentialsNamespace {
|
|
55
|
+
return {
|
|
56
|
+
async send(role, request) {
|
|
57
|
+
const body: Record<string, unknown> =
|
|
58
|
+
request.path !== undefined ? { method: request.method, path: request.path } : { method: request.method, url: request.url };
|
|
59
|
+
if (request.headers !== undefined) body.headers = request.headers;
|
|
60
|
+
if (request.deviceLease !== undefined) body.deviceLease = request.deviceLease;
|
|
61
|
+
if (request.body !== undefined) {
|
|
62
|
+
if (typeof request.body === "string") body.body = request.body;
|
|
63
|
+
else {
|
|
64
|
+
body.body = bytesToBase64(request.body instanceof ArrayBuffer ? new Uint8Array(request.body) : request.body);
|
|
65
|
+
body.bodyEncoding = "base64";
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
const res = await transport.post<SendData>(
|
|
69
|
+
`/@${projectSlug}/extensions/${extensionSlug}/credentials/${encodeURIComponent(role)}/send`,
|
|
70
|
+
body,
|
|
71
|
+
);
|
|
72
|
+
const d = res.data;
|
|
73
|
+
if (d === undefined || d === null || typeof d.status !== "number" || typeof d.body !== "string" || d.bodyEncoding !== "base64") {
|
|
74
|
+
throw malformed(role, "missing status or base64 body");
|
|
75
|
+
}
|
|
76
|
+
let decoded: Uint8Array;
|
|
77
|
+
try {
|
|
78
|
+
decoded = base64ToBytes(d.body);
|
|
79
|
+
} catch {
|
|
80
|
+
throw malformed(role, "body is not base64");
|
|
81
|
+
}
|
|
82
|
+
return { status: d.status, headers: d.headers ?? {}, body: decoded };
|
|
83
|
+
},
|
|
84
|
+
};
|
|
85
|
+
}
|