@coffre/db 0.14.2 → 0.14.3
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/dialect.d.ts +3 -2
- package/dist/dialect.js +1 -0
- package/dist/migrate.d.ts +21 -5
- package/dist/migrate.js +49 -9
- package/dist/migrations/postgres/0019_backup_role.sql +63 -0
- package/dist/migrations/postgres/meta/0019_snapshot.json +3295 -0
- package/dist/migrations/postgres/meta/_journal.json +7 -0
- package/dist/schema-version.js +7 -0
- package/package.json +2 -2
- package/src/dialect.ts +3 -2
- package/src/migrate-main.ts +6 -3
- package/src/migrate.ts +49 -10
- package/src/migrations/postgres/0019_backup_role.sql +63 -0
- package/src/migrations/postgres/meta/0019_snapshot.json +3295 -0
- package/src/migrations/postgres/meta/_journal.json +7 -0
package/dist/schema-version.js
CHANGED
|
@@ -141,6 +141,13 @@ const KNOWN_MIGRATIONS = {
|
|
|
141
141
|
"when": 1792871149580,
|
|
142
142
|
"tag": "0018_rotate_app_key",
|
|
143
143
|
"breakpoints": true
|
|
144
|
+
},
|
|
145
|
+
{
|
|
146
|
+
"idx": 19,
|
|
147
|
+
"version": "7",
|
|
148
|
+
"when": 1792937576477,
|
|
149
|
+
"tag": "0019_backup_role",
|
|
150
|
+
"breakpoints": true
|
|
144
151
|
}
|
|
145
152
|
].map((entry) => entry.tag),
|
|
146
153
|
sqlite: [
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@coffre/db",
|
|
3
|
-
"version": "0.14.
|
|
3
|
+
"version": "0.14.3",
|
|
4
4
|
"description": "coffre's database: the schema, its migrations and the connections, on Postgres or SQLite.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -79,7 +79,7 @@
|
|
|
79
79
|
"drizzle-orm": "0.45.2",
|
|
80
80
|
"pg": "8.16.3",
|
|
81
81
|
"pg-connection-string": "2.14.0",
|
|
82
|
-
"@coffre/core": "0.14.
|
|
82
|
+
"@coffre/core": "0.14.3"
|
|
83
83
|
},
|
|
84
84
|
"devDependencies": {
|
|
85
85
|
"@types/node": "26.1.1",
|
package/src/dialect.ts
CHANGED
|
@@ -39,11 +39,12 @@ export function engineOf(db: Queryable): Engine {
|
|
|
39
39
|
*
|
|
40
40
|
* `no key update` is for rows others reference by foreign key: it keeps out
|
|
41
41
|
* every other writer of the row, but not an insert that points at it.
|
|
42
|
+
* `share` keeps out every writer, but not another `share`.
|
|
42
43
|
*/
|
|
43
|
-
export function forUpdate<Query extends { for: (strength: 'update' | 'no key update') => unknown }>(
|
|
44
|
+
export function forUpdate<Query extends { for: (strength: 'update' | 'no key update' | 'share') => unknown }>(
|
|
44
45
|
db: Queryable,
|
|
45
46
|
query: Query,
|
|
46
|
-
strength: 'update' | 'no key update' = 'update',
|
|
47
|
+
strength: 'update' | 'no key update' | 'share' = 'update',
|
|
47
48
|
): ReturnType<Query['for']> {
|
|
48
49
|
if (engineOf(db) === 'sqlite') return query as ReturnType<Query['for']>;
|
|
49
50
|
return query.for(strength) as ReturnType<Query['for']>;
|
package/src/migrate-main.ts
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
|
-
// `pnpm db:migrate`: the workspace's own migrator,
|
|
2
|
-
// and the tests. A deployment runs `coffre-server migrate`.
|
|
1
|
+
// `pnpm db:migrate [--backup-login <role>]`: the workspace's own migrator,
|
|
2
|
+
// for dev and the tests. A deployment runs `coffre-server migrate`.
|
|
3
|
+
import { parseArgs } from 'node:util';
|
|
4
|
+
|
|
3
5
|
import { migrateDatabase } from './migrate.ts';
|
|
4
6
|
|
|
5
7
|
const url = process.env.DATABASE_URL?.trim();
|
|
6
8
|
if (!url) throw new Error('DATABASE_URL is required');
|
|
7
|
-
|
|
9
|
+
const { values } = parseArgs({ options: { 'backup-login': { type: 'string' } }, strict: true });
|
|
10
|
+
await migrateDatabase(url, { backupLogin: values['backup-login'] });
|
|
8
11
|
console.log('database schema is up to date');
|
package/src/migrate.ts
CHANGED
|
@@ -24,12 +24,33 @@ const MIGRATION_LOCK_KEY = '7165058122361679213';
|
|
|
24
24
|
export async function restrictDatabase(client: Pick<PoolClient, 'query'>): Promise<void> {
|
|
25
25
|
await client.query(`DO $$ BEGIN
|
|
26
26
|
EXECUTE format(
|
|
27
|
-
'REVOKE CREATE, TEMPORARY ON DATABASE %I FROM PUBLIC, coffre_app, coffre_runtime, coffre_vault, coffre_vault_runtime',
|
|
27
|
+
'REVOKE CREATE, TEMPORARY ON DATABASE %I FROM PUBLIC, coffre_app, coffre_runtime, coffre_vault, coffre_vault_runtime, coffre_backup',
|
|
28
28
|
current_database()
|
|
29
29
|
);
|
|
30
30
|
END $$`);
|
|
31
31
|
}
|
|
32
32
|
|
|
33
|
+
/** Why `login` cannot be the backup login, before anything runs, or null when it may. */
|
|
34
|
+
export function backupLoginProblem(login: string): string | null {
|
|
35
|
+
if (login === '') return 'the backup login is empty: name the role to grant coffre_backup';
|
|
36
|
+
if (['coffre_backup', 'coffre_app', 'coffre_vault', 'coffre_runtime', 'coffre_vault_runtime'].includes(login)) {
|
|
37
|
+
return `the backup login is ${login}, one of coffre's own roles: name the login that backs up`;
|
|
38
|
+
}
|
|
39
|
+
return null;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* The migrations are applied, by this run or an earlier one, and their
|
|
44
|
+
* ledger checked, but reasserting the privileges after them failed: unlike
|
|
45
|
+
* a migration's failure, this one may have changed the database.
|
|
46
|
+
*/
|
|
47
|
+
export class AfterMigrations extends Error {
|
|
48
|
+
constructor(error: unknown) {
|
|
49
|
+
super(`the database is at the migrated schema, but reasserting its privileges failed: ${error instanceof Error ? error.message : String(error)}`, { cause: error });
|
|
50
|
+
this.name = 'AfterMigrations';
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
33
54
|
interface JournalEntry {
|
|
34
55
|
idx: number;
|
|
35
56
|
when: number;
|
|
@@ -55,8 +76,8 @@ interface Migrator {
|
|
|
55
76
|
lock(): Promise<void>;
|
|
56
77
|
unlock(): Promise<void>;
|
|
57
78
|
migrate(migrationsFolder: string): Promise<void>;
|
|
58
|
-
/** Reassert privileges a one-database backup does not carry. */
|
|
59
|
-
restrict(): Promise<void>;
|
|
79
|
+
/** Reassert privileges a one-database backup does not carry, and grant `backupLogin` the backup group (0019_backup_role.sql). */
|
|
80
|
+
restrict(backupLogin: string | undefined): Promise<void>;
|
|
60
81
|
/** Why this login cannot change the objects migrations change, `managed` among them, or null when it can. */
|
|
61
82
|
notOwner(managed: Managed): Promise<NotOwner | null>;
|
|
62
83
|
close(): Promise<void>;
|
|
@@ -264,7 +285,15 @@ async function postgresMigrator(url: string): Promise<Migrator> {
|
|
|
264
285
|
await client.query('SELECT pg_advisory_unlock($1::bigint)', [MIGRATION_LOCK_KEY]);
|
|
265
286
|
},
|
|
266
287
|
migrate: (migrationsFolder) => migrate(drizzle(client), { migrationsFolder }),
|
|
267
|
-
restrict
|
|
288
|
+
async restrict(backupLogin) {
|
|
289
|
+
await restrictDatabase(client);
|
|
290
|
+
if (backupLogin !== undefined) {
|
|
291
|
+
// Compared as text: as a name, Postgres would cut it to 63 bytes, and grant whichever role bears the cut.
|
|
292
|
+
const { rowCount } = await client.query('SELECT 1 FROM pg_roles WHERE rolname::text = $1', [backupLogin]);
|
|
293
|
+
if (rowCount === 0) throw new Error(`role ${client.escapeIdentifier(backupLogin)} does not exist: make it, coffre never does`);
|
|
294
|
+
await client.query(`GRANT coffre_backup TO ${client.escapeIdentifier(backupLogin)} WITH ADMIN FALSE, INHERIT TRUE, SET FALSE`);
|
|
295
|
+
}
|
|
296
|
+
},
|
|
268
297
|
async notOwner({ relations, functions }) {
|
|
269
298
|
const { rows } = await client.query<{ owner: string; member: boolean; login: string; grants_inherit: boolean; inherits: boolean }>(FOREIGN_OWNERS, [relations, functions]);
|
|
270
299
|
if (rows.length === 0) return null;
|
|
@@ -342,17 +371,23 @@ export async function migrationStatus(url: string): Promise<{ applied: string[];
|
|
|
342
371
|
}
|
|
343
372
|
|
|
344
373
|
/**
|
|
345
|
-
* Apply missing migrations, check their history and reassert database
|
|
346
|
-
* privileges, as a login that can
|
|
347
|
-
*
|
|
374
|
+
* Apply missing migrations, check their history, and reassert database
|
|
375
|
+
* privileges, on every run, even with nothing to apply, as a login that can
|
|
376
|
+
* change coffre's objects: another is refused, NotOwner, before anything
|
|
377
|
+
* changes. `onPlan` hears what there is to apply before it is, under
|
|
348
378
|
* the migration lock, and may stop the run by throwing: all of it then goes
|
|
349
|
-
* in one call, with no word of each.
|
|
379
|
+
* in one call, with no word of each. `backupLogin` names an existing role to
|
|
380
|
+
* grant the backup group, `coffre_backup`, on Postgres; it is never made, and
|
|
381
|
+
* never revoked when a later run names none.
|
|
350
382
|
*/
|
|
351
383
|
export async function migrateDatabase(
|
|
352
384
|
url: string,
|
|
353
|
-
onPlan?: (plan: MigrationPlan) => void | Promise<void
|
|
385
|
+
{ onPlan, backupLogin }: { onPlan?: (plan: MigrationPlan) => void | Promise<void>; backupLogin?: string } = {},
|
|
354
386
|
): Promise<void> {
|
|
355
387
|
const engine = engineOfUrl(url);
|
|
388
|
+
const problem = backupLogin === undefined ? null : backupLoginProblem(backupLogin);
|
|
389
|
+
if (problem !== null) throw new Error(problem);
|
|
390
|
+
if (engine === 'sqlite' && backupLogin !== undefined) throw new Error('SQLite has no roles, so no backup login');
|
|
356
391
|
const expected = await expectedMigrations(engine);
|
|
357
392
|
const migrator = await { postgres: postgresMigrator, sqlite: sqliteMigrator }[engine](url);
|
|
358
393
|
let locked = false;
|
|
@@ -369,7 +404,11 @@ export async function migrateDatabase(
|
|
|
369
404
|
await onPlan?.({ applied: done, total: expected.length, pending: expected.slice(done).map((entry) => entry.tag) });
|
|
370
405
|
await migrator.migrate(migrationsFolder(engine));
|
|
371
406
|
verifyHistory(expected, await migrator.applied(), true);
|
|
372
|
-
|
|
407
|
+
try {
|
|
408
|
+
await migrator.restrict(backupLogin);
|
|
409
|
+
} catch (error) {
|
|
410
|
+
throw new AfterMigrations(error);
|
|
411
|
+
}
|
|
373
412
|
} finally {
|
|
374
413
|
try {
|
|
375
414
|
if (locked) await migrator.unlock();
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
-- Backups (W2356): the group coffre_backup reads every row and writes none, so a nightly pg_dump need not hold the owner's password. No login is made or granted here: `coffre migrate --backup-login <role>` grants the group to the role it names. Reading every table is not enough: the audit log's row-level security shows its rows only to the roles a policy names, so pg_dump as a role that reads every table (pg_read_all_data) is refused the log, or with --enable-row-security dumps no entry. It only adds a role, read grants and a read policy: the release before never meets them.
|
|
2
|
+
DO $$
|
|
3
|
+
BEGIN
|
|
4
|
+
IF NOT EXISTS (SELECT 1 FROM pg_roles WHERE rolname = 'coffre_backup') THEN
|
|
5
|
+
-- No migration since the baseline made a role: a login that migrates as a member of the owner inherits its privileges, never its CREATEROLE.
|
|
6
|
+
IF NOT EXISTS (SELECT 1 FROM pg_roles WHERE rolname = current_user AND (rolsuper OR rolcreaterole)) THEN
|
|
7
|
+
RAISE EXCEPTION '% may not create roles, and this migration makes coffre_backup: migrate as a login with CREATEROLE, or have one run CREATE ROLE coffre_backup NOLOGIN; GRANT coffre_backup TO % WITH ADMIN TRUE, INHERIT FALSE, SET FALSE first', current_user, current_user;
|
|
8
|
+
END IF;
|
|
9
|
+
-- Roles are the server's, and the migration lock each database's: another database's 0019 may make it meanwhile.
|
|
10
|
+
BEGIN
|
|
11
|
+
CREATE ROLE coffre_backup NOLOGIN INHERIT NOCREATEDB NOCREATEROLE;
|
|
12
|
+
EXCEPTION WHEN duplicate_object OR unique_violation THEN
|
|
13
|
+
NULL;
|
|
14
|
+
END;
|
|
15
|
+
END IF;
|
|
16
|
+
-- Managed owners cannot ALTER the superuser-only attributes, even to turn them off.
|
|
17
|
+
IF EXISTS (
|
|
18
|
+
SELECT 1 FROM pg_roles WHERE rolname = 'coffre_backup'
|
|
19
|
+
AND (rolsuper OR rolreplication OR rolbypassrls)
|
|
20
|
+
) THEN
|
|
21
|
+
RAISE EXCEPTION 'coffre_backup has unsafe role attributes: SUPERUSER, REPLICATION and BYPASSRLS must be off';
|
|
22
|
+
END IF;
|
|
23
|
+
-- It would pass on what it is a member of to its members.
|
|
24
|
+
IF EXISTS (SELECT 1 FROM pg_auth_members WHERE member = 'coffre_backup'::regrole) THEN
|
|
25
|
+
RAISE EXCEPTION 'coffre_backup is a member of another role, whose privileges its members would inherit: revoke its memberships';
|
|
26
|
+
END IF;
|
|
27
|
+
-- Revoking cannot take an owner's rights away: its members would write what it owns.
|
|
28
|
+
IF EXISTS (
|
|
29
|
+
SELECT 1 FROM pg_shdepend
|
|
30
|
+
WHERE refclassid = 'pg_authid'::regclass AND refobjid = 'coffre_backup'::regrole AND deptype = 'o'
|
|
31
|
+
) THEN
|
|
32
|
+
RAISE EXCEPTION 'coffre_backup owns database objects, which its members could change: reassign them';
|
|
33
|
+
END IF;
|
|
34
|
+
-- Only when it must: altering a role takes CREATEROLE too.
|
|
35
|
+
IF EXISTS (
|
|
36
|
+
SELECT 1 FROM pg_roles WHERE rolname = 'coffre_backup'
|
|
37
|
+
AND (rolcanlogin OR NOT rolinherit OR rolcreatedb OR rolcreaterole)
|
|
38
|
+
) THEN
|
|
39
|
+
ALTER ROLE coffre_backup NOLOGIN INHERIT NOCREATEDB NOCREATEROLE;
|
|
40
|
+
END IF;
|
|
41
|
+
END
|
|
42
|
+
$$;
|
|
43
|
+
--> statement-breakpoint
|
|
44
|
+
-- coffre's own, by name, as the baseline grants: public may hold a database's other tables (FOREIGN_OWNERS in migrate.ts), which are not coffre's to grant, whichever login migrates, a superuser included. A group made before this keeps no other privilege on them. A later migration that makes a table grants it SELECT too, as it grants coffre_app, rather than default privileges, which would give it every table the login makes, coffre's or not, and none another login makes.
|
|
45
|
+
REVOKE ALL ON SCHEMA public, drizzle FROM coffre_backup;
|
|
46
|
+
--> statement-breakpoint
|
|
47
|
+
GRANT USAGE ON SCHEMA public, drizzle TO coffre_backup;
|
|
48
|
+
--> statement-breakpoint
|
|
49
|
+
REVOKE ALL ON audit_chain_head, audit_log, capped_alerts, consumed_tokens, credentials, device_authorizations, dismissed_keys, enrollments, environments, identities, mcp_approvals, mcp_connections, oauth_clients, projects, secret_references, secret_versions, secrets, service_bindings, vault_grants, vault_members FROM coffre_backup;
|
|
50
|
+
--> statement-breakpoint
|
|
51
|
+
REVOKE ALL ON ALL TABLES IN SCHEMA drizzle FROM coffre_backup;
|
|
52
|
+
--> statement-breakpoint
|
|
53
|
+
REVOKE ALL ON ALL SEQUENCES IN SCHEMA drizzle FROM coffre_backup;
|
|
54
|
+
--> statement-breakpoint
|
|
55
|
+
REVOKE ALL ON FUNCTION audit_log_append_only(), secret_versions_erase_only(), prune_consumed_tokens(integer) FROM coffre_backup;
|
|
56
|
+
--> statement-breakpoint
|
|
57
|
+
GRANT SELECT ON audit_chain_head, audit_log, capped_alerts, consumed_tokens, credentials, device_authorizations, dismissed_keys, enrollments, environments, identities, mcp_approvals, mcp_connections, oauth_clients, projects, secret_references, secret_versions, secrets, service_bindings, vault_grants, vault_members TO coffre_backup;
|
|
58
|
+
--> statement-breakpoint
|
|
59
|
+
GRANT SELECT ON ALL TABLES IN SCHEMA drizzle TO coffre_backup;
|
|
60
|
+
--> statement-breakpoint
|
|
61
|
+
GRANT SELECT ON ALL SEQUENCES IN SCHEMA drizzle TO coffre_backup;
|
|
62
|
+
--> statement-breakpoint
|
|
63
|
+
CREATE POLICY audit_log_backup_read ON audit_log FOR SELECT TO coffre_backup USING (true);
|