@coffre/db 0.10.2 → 0.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/connect.d.ts +1 -1
- package/dist/{database-DBtn3jqp.d.ts → database-Ds1Y79h6.d.ts} +1 -1
- package/dist/dialect.d.ts +1 -1
- package/dist/grants.d.ts +12 -5
- package/dist/grants.js +13 -8
- package/dist/hyperdrive.d.ts +9 -2
- package/dist/hyperdrive.js +16 -2
- package/dist/index.d.ts +2 -2
- package/dist/log.d.ts +1 -1
- package/dist/migrations/postgres/0015_workload_access.sql +43 -0
- package/dist/migrations/postgres/0016_spent_tokens.sql +12 -0
- package/dist/migrations/postgres/0017_capped_alerts.sql +10 -0
- package/dist/migrations/postgres/meta/0015_snapshot.json +3256 -0
- package/dist/migrations/postgres/meta/0016_snapshot.json +3256 -0
- package/dist/migrations/postgres/meta/0017_snapshot.json +3295 -0
- package/dist/migrations/postgres/meta/_journal.json +21 -0
- package/dist/migrations/sqlite/0015_workload_access.sql +70 -0
- package/dist/migrations/sqlite/0016_capped_alerts.sql +7 -0
- package/dist/migrations/sqlite/meta/0015_snapshot.json +2959 -0
- package/dist/migrations/sqlite/meta/0016_snapshot.json +2998 -0
- package/dist/migrations/sqlite/meta/_journal.json +14 -0
- package/dist/portable.d.ts +2 -2
- package/dist/{schema-NBVA9q-s.d.ts → schema-DcVkJhCf.d.ts} +393 -2
- package/dist/schema-sqlite.d.ts +408 -1
- package/dist/schema-sqlite.js +61 -5
- package/dist/schema-version.d.ts +1 -1
- package/dist/schema-version.js +35 -0
- package/dist/schema.d.ts +2 -2
- package/dist/schema.js +71 -5
- package/package.json +2 -2
- package/src/grants.ts +18 -9
- package/src/hyperdrive.ts +16 -2
- package/src/migrations/postgres/0015_workload_access.sql +43 -0
- package/src/migrations/postgres/0016_spent_tokens.sql +12 -0
- package/src/migrations/postgres/0017_capped_alerts.sql +10 -0
- package/src/migrations/postgres/meta/0015_snapshot.json +3256 -0
- package/src/migrations/postgres/meta/0016_snapshot.json +3256 -0
- package/src/migrations/postgres/meta/0017_snapshot.json +3295 -0
- package/src/migrations/postgres/meta/_journal.json +21 -0
- package/src/migrations/sqlite/0015_workload_access.sql +70 -0
- package/src/migrations/sqlite/0016_capped_alerts.sql +7 -0
- package/src/migrations/sqlite/meta/0015_snapshot.json +2959 -0
- package/src/migrations/sqlite/meta/0016_snapshot.json +2998 -0
- package/src/migrations/sqlite/meta/_journal.json +14 -0
- package/src/schema.sqlite.ts +83 -2
- package/src/schema.ts +91 -2
package/dist/connect.d.ts
CHANGED
package/dist/dialect.d.ts
CHANGED
package/dist/grants.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { i as Queryable } from "./database-
|
|
1
|
+
import { i as Queryable } from "./database-Ds1Y79h6.js";
|
|
2
2
|
import { Authenticity, Newest } from "./log.js";
|
|
3
3
|
import { StoredEntry } from "@coffre/core/audit";
|
|
4
4
|
import { Conversion, ConversionInput, InstanceRole, Scope } from "@coffre/core/access";
|
|
@@ -92,17 +92,24 @@ export type EveryProjectPreview = {
|
|
|
92
92
|
* converts is listed.
|
|
93
93
|
*/
|
|
94
94
|
export declare function everyProjectPreview(db: Queryable, at: number): Promise<EveryProjectPreview[]>;
|
|
95
|
-
/**
|
|
95
|
+
/**
|
|
96
|
+
* A service token or trust binding whose scope this release has not sealed
|
|
97
|
+
* yet: none of its own, as 0.4 made it (`scope` null), or one that does not
|
|
98
|
+
* say where it reads sensitive values, as a release before 0.11 made it. By
|
|
99
|
+
* whom it was issued, `createdBy`, a person's email.
|
|
100
|
+
*/
|
|
96
101
|
export type Unsealed = {
|
|
97
102
|
kind: 'token' | 'binding';
|
|
98
103
|
id: string;
|
|
99
104
|
principal: string;
|
|
105
|
+
createdBy: string;
|
|
106
|
+
scope: string | null;
|
|
100
107
|
};
|
|
101
108
|
/**
|
|
102
109
|
* The live service tokens and trust bindings of active service accounts
|
|
103
|
-
*
|
|
104
|
-
*
|
|
105
|
-
*
|
|
110
|
+
* whose scope is not sealed yet (`Unsealed`): what the app seals once a
|
|
111
|
+
* process (`sealScopes`), and what `coffre migrate` says it will. A
|
|
112
|
+
* credential a binding issued carries its binding's.
|
|
106
113
|
*/
|
|
107
114
|
export declare function unsealedCredentials(db: Queryable, at: Date): Promise<Unsealed[]>;
|
|
108
115
|
/**
|
package/dist/grants.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { tombstone, truth } from "./dialect.js";
|
|
2
2
|
import { a as prepared, i as tablesOf } from "./database-BadNeeN-.js";
|
|
3
3
|
import { entryColumns, newestAuthentic } from "./log.js";
|
|
4
|
-
import { and, asc, desc, eq, gt, inArray, isNull, lt, or, sql } from "drizzle-orm";
|
|
4
|
+
import { and, asc, desc, eq, gt, inArray, isNull, like, lt, or, sql } from "drizzle-orm";
|
|
5
5
|
import { EVERYWHERE, administers, convertEveryProjectGrants, isInstanceRole, isScope, normalScope, unscoped } from "@coffre/core/access";
|
|
6
6
|
import { ACCESS_ACTIONS } from "@coffre/core/vault";
|
|
7
7
|
//#region src/grants.ts
|
|
@@ -170,19 +170,24 @@ async function everyProjectPreview(db, at) {
|
|
|
170
170
|
}
|
|
171
171
|
/**
|
|
172
172
|
* The live service tokens and trust bindings of active service accounts
|
|
173
|
-
*
|
|
174
|
-
*
|
|
175
|
-
*
|
|
173
|
+
* whose scope is not sealed yet (`Unsealed`): what the app seals once a
|
|
174
|
+
* process (`sealScopes`), and what `coffre migrate` says it will. A
|
|
175
|
+
* credential a binding issued carries its binding's.
|
|
176
176
|
*/
|
|
177
177
|
async function unsealedCredentials(db, at) {
|
|
178
178
|
const { credentials, serviceBindings, vaultMembers } = tablesOf(db);
|
|
179
|
+
const unsealed = (scope) => or(isNull(scope), and(like(scope, "%\"projectId\"%"), sql`${scope} NOT LIKE '%"sensitive":%'`));
|
|
179
180
|
const [tokens, bindings] = await Promise.all([db.select({
|
|
180
181
|
id: credentials.id,
|
|
181
|
-
principal: credentials.principal
|
|
182
|
-
|
|
182
|
+
principal: credentials.principal,
|
|
183
|
+
createdBy: credentials.createdBy,
|
|
184
|
+
scope: credentials.scope
|
|
185
|
+
}).from(credentials).innerJoin(vaultMembers, and(eq(vaultMembers.principal, credentials.principal), eq(vaultMembers.generation, credentials.generation))).where(and(eq(credentials.kind, "service"), unsealed(credentials.scope), sql`${credentials.createdBy} NOT LIKE 'binding:%'`, isNull(credentials.revokedAt), gt(credentials.expiresAt, at), eq(vaultMembers.status, "active"))), db.select({
|
|
183
186
|
id: serviceBindings.id,
|
|
184
|
-
principal: serviceBindings.principal
|
|
185
|
-
|
|
187
|
+
principal: serviceBindings.principal,
|
|
188
|
+
createdBy: serviceBindings.createdBy,
|
|
189
|
+
scope: serviceBindings.scope
|
|
190
|
+
}).from(serviceBindings).innerJoin(vaultMembers, and(eq(vaultMembers.principal, serviceBindings.principal), eq(vaultMembers.generation, serviceBindings.generation))).where(and(unsealed(serviceBindings.scope), isNull(serviceBindings.revokedAt), eq(vaultMembers.status, "active")))]);
|
|
186
191
|
return [...tokens.map((row) => ({
|
|
187
192
|
kind: "token",
|
|
188
193
|
...row
|
package/dist/hyperdrive.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { n as PoolLike } from "./database-
|
|
1
|
+
import { n as PoolLike } from "./database-Ds1Y79h6.js";
|
|
2
2
|
import pg from "pg";
|
|
3
3
|
//#region src/hyperdrive.d.ts
|
|
4
4
|
/** Postgres through a Hyperdrive binding: `postgres(env.HYPERDRIVE)`. */
|
|
@@ -36,7 +36,14 @@ export declare function postgres(hyperdrive: {
|
|
|
36
36
|
*/
|
|
37
37
|
export declare class HyperdrivePool implements Exclude<PoolLike, pg.Pool> {
|
|
38
38
|
#private;
|
|
39
|
-
|
|
39
|
+
/**
|
|
40
|
+
* `closeIdleMs`: for what outlives a call, a Durable Object, which queries
|
|
41
|
+
* now and then: the client closes once it has been idle that long, and
|
|
42
|
+
* the next query connects again.
|
|
43
|
+
*/
|
|
44
|
+
constructor(connectionString: string, options?: {
|
|
45
|
+
closeIdleMs?: number;
|
|
46
|
+
});
|
|
40
47
|
readonly query: pg.Pool['query'];
|
|
41
48
|
/** The client, for a transaction: held from now until `release`. */
|
|
42
49
|
connect(): Promise<Pick<pg.PoolClient, 'query'> & {
|
package/dist/hyperdrive.js
CHANGED
|
@@ -43,12 +43,22 @@ var HyperdrivePool = class {
|
|
|
43
43
|
#busy = 0;
|
|
44
44
|
/** What waits for the client to be idle: `end`. */
|
|
45
45
|
#idle = [];
|
|
46
|
-
|
|
46
|
+
/** How long the client may sit idle before it closes, or null: closed by `end` alone. */
|
|
47
|
+
#closeIdleMs;
|
|
48
|
+
#idleTimer;
|
|
49
|
+
/**
|
|
50
|
+
* `closeIdleMs`: for what outlives a call, a Durable Object, which queries
|
|
51
|
+
* now and then: the client closes once it has been idle that long, and
|
|
52
|
+
* the next query connects again.
|
|
53
|
+
*/
|
|
54
|
+
constructor(connectionString, options = {}) {
|
|
47
55
|
if (connectionString.trim().length === 0) throw new Error("Hyperdrive returned an empty connection string");
|
|
48
56
|
this.#connectionString = connectionString;
|
|
57
|
+
this.#closeIdleMs = options.closeIdleMs ?? null;
|
|
49
58
|
}
|
|
50
59
|
query = (async (...args) => {
|
|
51
60
|
this.#busy += 1;
|
|
61
|
+
clearTimeout(this.#idleTimer);
|
|
52
62
|
try {
|
|
53
63
|
const client = await this.#connected();
|
|
54
64
|
return await new Promise((resolve, reject) => {
|
|
@@ -61,6 +71,7 @@ var HyperdrivePool = class {
|
|
|
61
71
|
/** The client, for a transaction: held from now until `release`. */
|
|
62
72
|
async connect() {
|
|
63
73
|
this.#busy += 1;
|
|
74
|
+
clearTimeout(this.#idleTimer);
|
|
64
75
|
let client;
|
|
65
76
|
try {
|
|
66
77
|
client = await this.#connected();
|
|
@@ -86,6 +97,7 @@ var HyperdrivePool = class {
|
|
|
86
97
|
}
|
|
87
98
|
/** Close the client once nothing waits on it: what a Worker hands `waitUntil` when the call is done. */
|
|
88
99
|
async end() {
|
|
100
|
+
clearTimeout(this.#idleTimer);
|
|
89
101
|
while (this.#busy > 0) await new Promise((resolve) => this.#idle.push(resolve));
|
|
90
102
|
const client = this.#client;
|
|
91
103
|
this.#client = null;
|
|
@@ -104,7 +116,9 @@ var HyperdrivePool = class {
|
|
|
104
116
|
/** One query or transaction done: once none is left, what waits for the client to be idle goes on. */
|
|
105
117
|
#done() {
|
|
106
118
|
this.#busy -= 1;
|
|
107
|
-
if (this.#busy
|
|
119
|
+
if (this.#busy > 0) return;
|
|
120
|
+
for (const resolve of this.#idle.splice(0)) resolve();
|
|
121
|
+
if (this.#closeIdleMs !== null && this.#client !== null) this.#idleTimer = setTimeout(() => void this.end(), this.#closeIdleMs);
|
|
108
122
|
}
|
|
109
123
|
/** The call's client, connecting it the first time. One that fails is dropped: the next query connects again. */
|
|
110
124
|
#connected() {
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { a as Transaction, c as tablesOf, i as Queryable, l as prepared, n as PoolLike, o as createDatabase, r as QueryOutsideTransaction, s as own, t as Database } from "./database-
|
|
1
|
+
import { g as schema_d_exports } from "./schema-DcVkJhCf.js";
|
|
2
|
+
import { a as Transaction, c as tablesOf, i as Queryable, l as prepared, n as PoolLike, o as createDatabase, r as QueryOutsideTransaction, s as own, t as Database } from "./database-Ds1Y79h6.js";
|
|
3
3
|
export { Database, PoolLike, QueryOutsideTransaction, Queryable, Transaction, createDatabase, own, prepared, schema_d_exports as schema, tablesOf };
|
package/dist/log.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { a as Transaction, i as Queryable } from "./database-
|
|
1
|
+
import { a as Transaction, i as Queryable } from "./database-Ds1Y79h6.js";
|
|
2
2
|
import { Placeholder, SQL } from "drizzle-orm";
|
|
3
3
|
import { LogKey, StoredEntry } from "@coffre/core/audit";
|
|
4
4
|
//#region src/log.d.ts
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
-- Workload access (docs/design/workload-access.md, section 11): key-bound sessions, their enrollment codes, and bindings' policies. It only adds: the release before neither reads nor writes any of it, and every check is only looser for its rows, so it serves on this schema unchanged.
|
|
2
|
+
CREATE TABLE "enrollments" (
|
|
3
|
+
"id" uuid PRIMARY KEY NOT NULL,
|
|
4
|
+
"auth_mac" "bytea" NOT NULL,
|
|
5
|
+
"binding_id" uuid NOT NULL,
|
|
6
|
+
"code_hash" "bytea" NOT NULL,
|
|
7
|
+
"code_hint" text NOT NULL,
|
|
8
|
+
"scope" text NOT NULL,
|
|
9
|
+
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
|
|
10
|
+
"created_by" text NOT NULL,
|
|
11
|
+
"created_with" uuid,
|
|
12
|
+
"expires_at" timestamp with time zone NOT NULL,
|
|
13
|
+
CONSTRAINT "enrollments_code_hash_key" UNIQUE("code_hash"),
|
|
14
|
+
CONSTRAINT "enrollments_auth_mac_check" CHECK (octet_length("enrollments"."auth_mac") = 32),
|
|
15
|
+
CONSTRAINT "enrollments_code_hash_check" CHECK (octet_length("enrollments"."code_hash") = 32),
|
|
16
|
+
CONSTRAINT "enrollments_scope_check" CHECK ("enrollments"."scope"::jsonb IS NOT NULL)
|
|
17
|
+
);
|
|
18
|
+
--> statement-breakpoint
|
|
19
|
+
-- The checks widen to a workload's session, a service's credential like a service token; a session carries the key it is bound to.
|
|
20
|
+
ALTER TABLE "credentials" DROP CONSTRAINT "credentials_kind_check";--> statement-breakpoint
|
|
21
|
+
ALTER TABLE "credentials" DROP CONSTRAINT "credentials_kind_matches_principal";--> statement-breakpoint
|
|
22
|
+
ALTER TABLE "credentials" ADD COLUMN "jkt" text;--> statement-breakpoint
|
|
23
|
+
ALTER TABLE "credentials" ADD COLUMN "not_after" timestamp with time zone;--> statement-breakpoint
|
|
24
|
+
ALTER TABLE "credentials" ADD COLUMN "ancestors" text;--> statement-breakpoint
|
|
25
|
+
ALTER TABLE "credentials" ADD COLUMN "constraints" text;--> statement-breakpoint
|
|
26
|
+
ALTER TABLE "credentials" ADD COLUMN "enrollment_id" uuid;--> statement-breakpoint
|
|
27
|
+
ALTER TABLE "credentials" ADD COLUMN "approved_at" timestamp with time zone;--> statement-breakpoint
|
|
28
|
+
ALTER TABLE "credentials" ADD COLUMN "approved_by" text;--> statement-breakpoint
|
|
29
|
+
-- A binding's policy, inserted with it under the table-wide INSERT, and named by no UPDATE grant: as immutable as the rest of it.
|
|
30
|
+
ALTER TABLE "service_bindings" ADD COLUMN "policy" text;--> statement-breakpoint
|
|
31
|
+
ALTER TABLE "enrollments" ADD CONSTRAINT "enrollments_binding_id_fkey" FOREIGN KEY ("binding_id") REFERENCES "public"."service_bindings"("id") ON DELETE restrict ON UPDATE no action;--> statement-breakpoint
|
|
32
|
+
CREATE INDEX "enrollments_binding_idx" ON "enrollments" USING btree ("binding_id","created_at" DESC NULLS LAST);--> statement-breakpoint
|
|
33
|
+
ALTER TABLE "credentials" ADD CONSTRAINT "credentials_enrollment_id_fkey" FOREIGN KEY ("enrollment_id") REFERENCES "public"."enrollments"("id") ON DELETE restrict ON UPDATE no action;--> statement-breakpoint
|
|
34
|
+
-- Freezes are the log's: a target's newest allowed freeze or lift, and a code's supersession, its tombstone.
|
|
35
|
+
CREATE INDEX "audit_log_freeze_idx" ON "audit_log" USING btree ((("metadata")::jsonb ->> 'target'),"seq" DESC NULLS LAST) WHERE "audit_log"."author" = 'app' AND "audit_log"."action" IN ('workload.freeze', 'workload.unfreeze') AND "audit_log"."decision" = 'allow';--> statement-breakpoint
|
|
36
|
+
CREATE INDEX "audit_log_supersede_idx" ON "audit_log" USING btree ((("metadata")::jsonb ->> 'enrollmentId')) WHERE "audit_log"."author" = 'app' AND "audit_log"."action" = 'enrollment.supersede' AND "audit_log"."decision" = 'allow';--> statement-breakpoint
|
|
37
|
+
CREATE INDEX "credentials_enrollment_roots_idx" ON "credentials" USING btree ("enrollment_id","created_at") WHERE "credentials"."kind" = 'workload' AND "credentials"."ancestors" IS NULL;--> statement-breakpoint
|
|
38
|
+
ALTER TABLE "credentials" ADD CONSTRAINT "credentials_workload_jkt_check" CHECK ("credentials"."kind" <> 'workload' OR "credentials"."jkt" IS NOT NULL);--> statement-breakpoint
|
|
39
|
+
ALTER TABLE "credentials" ADD CONSTRAINT "credentials_kind_check" CHECK ("credentials"."kind" IN ('browser', 'cli', 'service', 'personal', 'handle', 'workload'));--> statement-breakpoint
|
|
40
|
+
ALTER TABLE "credentials" ADD CONSTRAINT "credentials_kind_matches_principal" CHECK (("credentials"."kind" IN ('service', 'workload')) = ("credentials"."principal" LIKE 'token:%'));--> statement-breakpoint
|
|
41
|
+
-- The app makes codes and never changes one; a session's key, hard end, ancestors, limits and code are set when it is made, and never after: renewal moves its expiry, and an approval is the one other change.
|
|
42
|
+
GRANT SELECT, INSERT ON enrollments TO coffre_app;--> statement-breakpoint
|
|
43
|
+
GRANT UPDATE (expires_at, approved_at, approved_by) ON credentials TO coffre_app;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
-- Spent tokens (W157): a token's record goes once no exchange could take the token again. An exchange refuses one issued over an hour ago (MAX_TOKEN_AGE_SECONDS, 30 s of clock tolerance), so two hours after it was spent its record guards nothing. The runtime login still deletes no row itself: it asks this function, which deletes only such records, at most at_most a call. The release before never calls it.
|
|
2
|
+
CREATE FUNCTION prune_consumed_tokens(at_most integer) RETURNS integer
|
|
3
|
+
LANGUAGE sql SECURITY DEFINER SET search_path = pg_catalog, pg_temp AS $$
|
|
4
|
+
WITH pruned AS (
|
|
5
|
+
DELETE FROM public.consumed_tokens
|
|
6
|
+
WHERE hash IN (SELECT hash FROM public.consumed_tokens WHERE consumed_at < now() - interval '2 hours' LIMIT at_most)
|
|
7
|
+
RETURNING 1
|
|
8
|
+
)
|
|
9
|
+
SELECT count(*)::integer FROM pruned;
|
|
10
|
+
$$;--> statement-breakpoint
|
|
11
|
+
REVOKE ALL ON FUNCTION prune_consumed_tokens(integer) FROM PUBLIC;--> statement-breakpoint
|
|
12
|
+
GRANT EXECUTE ON FUNCTION prune_consumed_tokens(integer) TO coffre_app;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
-- Capped alerts (T98): when a service's sign-in was last refused `unknown_key` with an alert, so that the next alerts an hour later at the soonest, whichever isolate sees it. It only adds: the release before neither reads nor writes it.
|
|
2
|
+
CREATE TABLE "capped_alerts" (
|
|
3
|
+
"service" text NOT NULL,
|
|
4
|
+
"kind" text NOT NULL,
|
|
5
|
+
"raised_at" timestamp with time zone NOT NULL,
|
|
6
|
+
CONSTRAINT "capped_alerts_pkey" PRIMARY KEY("service","kind")
|
|
7
|
+
);
|
|
8
|
+
--> statement-breakpoint
|
|
9
|
+
-- The app moves a row's time on, and deletes none.
|
|
10
|
+
GRANT SELECT, INSERT, UPDATE (raised_at) ON capped_alerts TO coffre_app;
|