mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-20 19:16:08 +00:00
fix(db): carry the plugin data schemas in Postgres backups and restores
pg_dump now names the trek_plugin_ schemas TREK's role owns beside its own schema. A restore reads the dump's plugin schemas from its table of contents, drops the plugin schemas the role owns, creates the dump's and loads them with TREK's schema, all in the one psql transaction. Schemas of another role and every other schema of the database are never read or dropped. The dump's migration history is read from TREK's mikro_orm_migrations only, so a plugin table of that name cannot pass for it.
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
import { PLUGIN_SCHEMA_PREFIX, quotePluginSchema } from './plugin-schema';
|
||||
|
||||
/**
|
||||
* Drops every table, view, sequence, routine, collation and standalone type in
|
||||
* the current schema, except an extension's, before a dump is loaded: a dump
|
||||
@@ -39,3 +41,25 @@ BEGIN
|
||||
END
|
||||
$clear$;
|
||||
`;
|
||||
|
||||
/**
|
||||
* Drops every plugin data schema TREK's role owns, then creates the ones
|
||||
* `schemas` names (a dump's) empty, for the restore script to fill: the script
|
||||
* holds what is in them, never the schemas themselves. Runs right after
|
||||
* CLEAR_SCHEMA_SQL, in the same transaction. A schema another role owns is
|
||||
* never dropped, whatever its name.
|
||||
*/
|
||||
export function pluginSchemasSql(schemas: readonly string[]): string {
|
||||
const drop = `DO $plugins$
|
||||
DECLARE r record;
|
||||
BEGIN
|
||||
FOR r IN SELECT nspname FROM pg_namespace
|
||||
WHERE starts_with(nspname, '${PLUGIN_SCHEMA_PREFIX}') AND pg_get_userbyid(nspowner) = current_user
|
||||
LOOP
|
||||
EXECUTE format('DROP SCHEMA %I CASCADE', r.nspname);
|
||||
END LOOP;
|
||||
END
|
||||
$plugins$;
|
||||
`;
|
||||
return drop + schemas.map((name) => `CREATE SCHEMA ${quotePluginSchema(name)};\n`).join('');
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { CURRENT_SCHEMA_TABLES_SQL } from '../../dialect/catalog';
|
||||
import { CLEAR_SCHEMA_SQL } from './clear-schema';
|
||||
import { CLEAR_SCHEMA_SQL, pluginSchemasSql } from './clear-schema';
|
||||
import { pgConnectionEnv, PgOutputLimitError, refuseClientOlderThanServer, run, runToFile } from './pg-process';
|
||||
import { isPluginSchemaName, PLUGIN_SCHEMA_PREFIX } from './plugin-schema';
|
||||
import { refuseToolsWithoutRestrict } from './restore-safety';
|
||||
import { PRIVILEGED_ROLE_SQL, serverMajor } from './server-checks';
|
||||
|
||||
@@ -37,16 +38,24 @@ interface PgSchema {
|
||||
/** What a dump or a restore needs to know about the server before it runs. */
|
||||
interface PgServer {
|
||||
schema: PgSchema;
|
||||
/** The plugin data schemas TREK's role owns (`plugin-schema.ts`), which a dump holds beside TREK's own. */
|
||||
pluginSchemas: string[];
|
||||
/** The server's major version, which the client tools must reach. */
|
||||
major: number;
|
||||
/** Whether TREK's role could run programs or write files on the database host (`PRIVILEGED_ROLE_SQL`). */
|
||||
privileged: boolean;
|
||||
}
|
||||
|
||||
/** One line, tab-separated: the current schema, its quoted name, the server version, whether the role is privileged. */
|
||||
/**
|
||||
* One line, tab-separated: the current schema, its quoted name, the server
|
||||
* version, whether the role is privileged, and the plugin schemas the role
|
||||
* owns, comma-separated.
|
||||
*/
|
||||
const SERVER_SQL =
|
||||
"select concat_ws(E'\\t', coalesce(current_schema(), ''), coalesce(quote_ident(current_schema()), ''), " +
|
||||
`current_setting('server_version_num'), (${PRIVILEGED_ROLE_SQL})::text)`;
|
||||
`current_setting('server_version_num'), (${PRIVILEGED_ROLE_SQL})::text, ` +
|
||||
"(select coalesce(string_agg(nspname, ',' order by nspname), '') from pg_namespace " +
|
||||
`where starts_with(nspname, '${PLUGIN_SCHEMA_PREFIX}') and pg_get_userbyid(nspowner) = current_user))`;
|
||||
|
||||
/**
|
||||
* The current schema of the database at `url`, the one TREK creates its
|
||||
@@ -62,16 +71,22 @@ async function pgServer(url: string, binDir: string | undefined): Promise<PgServ
|
||||
['--no-psqlrc', '--no-align', '--tuples-only', `--command=${SERVER_SQL}`],
|
||||
pgConnectionEnv(url),
|
||||
);
|
||||
const [name, quoted, version, privileged] = out.replace(/\n$/, '').split('\t');
|
||||
const [name, quoted, version, privileged, plugins = ''] = out.replace(/\n$/, '').split('\t');
|
||||
if (!name || !quoted) {
|
||||
throw new Error('The database has no current schema: set the search_path of the role in TREK_DB_URL.');
|
||||
}
|
||||
return { schema: { name, quoted }, major: serverMajor(version), privileged: privileged === 'true' };
|
||||
return {
|
||||
schema: { name, quoted },
|
||||
pluginSchemas: plugins.split(',').filter(isPluginSchemaName),
|
||||
major: serverMajor(version),
|
||||
privileged: privileged === 'true',
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes a custom-format dump of the current schema of the database at `url`
|
||||
* to `target`, readable by its owner only. It is written beside the target and
|
||||
* Writes a custom-format dump of the current schema of the database at `url`,
|
||||
* and of the plugin data schemas TREK's role owns, to `target`, readable by
|
||||
* its owner only. No other schema of the database is read. It is written beside the target and
|
||||
* renamed into place, so a dump that fails or is killed never leaves a file
|
||||
* that passes for a copy. A pg_dump older than the server is refused first.
|
||||
*/
|
||||
@@ -79,7 +94,7 @@ export async function pgDump(url: string, target: string, binDir: string | undef
|
||||
const partial = `${target}.partial`;
|
||||
try {
|
||||
createPrivateFile(partial);
|
||||
const { schema, major } = await pgServer(url, binDir);
|
||||
const { schema, pluginSchemas, major } = await pgServer(url, binDir);
|
||||
await refuseClientOlderThanServer(['pg_dump'], binDir, major);
|
||||
await run(
|
||||
'pg_dump',
|
||||
@@ -90,6 +105,8 @@ export async function pgDump(url: string, target: string, binDir: string | undef
|
||||
'--no-privileges',
|
||||
`--file=${partial}`,
|
||||
`--schema="${schema.name.replaceAll('"', '""')}"`,
|
||||
// A plugin schema's name holds lowercase letters, digits and underscores, none of them pattern syntax.
|
||||
...pluginSchemas.map((plugin) => `--schema=${plugin}`),
|
||||
],
|
||||
pgConnectionEnv(url),
|
||||
);
|
||||
@@ -131,11 +148,12 @@ async function scriptHoldsTrekSchema(script: string, schema: PgSchema): Promise<
|
||||
async function writeRestoreScript(
|
||||
source: string,
|
||||
script: string,
|
||||
schema: PgSchema,
|
||||
schemas: readonly string[],
|
||||
binDir: string | undefined,
|
||||
maxBytes: number,
|
||||
): Promise<void> {
|
||||
const args = ['--no-owner', '--no-privileges', `--schema=${schema.name}`, '--file=-', '--', source];
|
||||
const only = schemas.map((name) => `--schema=${name}`);
|
||||
const args = ['--no-owner', '--no-privileges', ...only, '--file=-', '--', source];
|
||||
try {
|
||||
await runToFile('pg_restore', binDir, args, {}, script, maxBytes);
|
||||
} catch (err) {
|
||||
@@ -150,10 +168,12 @@ async function writeRestoreScript(
|
||||
|
||||
/**
|
||||
* Replaces the current schema of the database at `url` with the dump at
|
||||
* `source`: pg_restore turns the dump's objects in that schema into a script
|
||||
* beside it (anything else the dump holds is left out), then psql clears the
|
||||
* schema and runs the script in one transaction, so a restore that fails
|
||||
* leaves the database as it was. Refused before anything is cleared:
|
||||
* `source`, and the plugin data schemas with the dump's: pg_restore turns the
|
||||
* dump's objects in those schemas into a script beside it (anything else the
|
||||
* dump holds is left out), then psql clears the schema, drops the plugin
|
||||
* schemas TREK's role owns, creates the dump's and runs the script, all in
|
||||
* one transaction, so a restore that fails leaves the database as it was. No
|
||||
* other schema of the database is touched. Refused before anything is cleared:
|
||||
*
|
||||
* - client tools without the CVE-2025-8714 fix, or older than the server;
|
||||
* - a role that could run programs or write files on the database host,
|
||||
@@ -179,9 +199,10 @@ export async function pgRestore(
|
||||
try {
|
||||
const clear = path.join(workDir, 'clear-schema.sql');
|
||||
const script = path.join(workDir, 'restore.sql');
|
||||
fs.writeFileSync(clear, CLEAR_SCHEMA_SQL, { mode: 0o600 });
|
||||
const pluginSchemas = await dumpPluginSchemas(source, path.join(workDir, 'toc.txt'), binDir);
|
||||
fs.writeFileSync(clear, CLEAR_SCHEMA_SQL + pluginSchemasSql(pluginSchemas), { mode: 0o600 });
|
||||
createPrivateFile(script);
|
||||
await writeRestoreScript(source, script, schema, binDir, maxScriptBytes);
|
||||
await writeRestoreScript(source, script, [schema.name, ...pluginSchemas], binDir, maxScriptBytes);
|
||||
if (!(await scriptHoldsTrekSchema(script, schema))) {
|
||||
throw new Error(`The backup holds no TREK tables in the schema "${schema.name}"; nothing was replaced.`);
|
||||
}
|
||||
@@ -204,6 +225,23 @@ export async function pgRestore(
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The plugin data schemas the dump at `source` holds, read from its table of
|
||||
* contents (`pg_restore --list`, written to `toc`), which has a line
|
||||
* `<id>; <oid> <oid> SCHEMA - <name> <owner>` per schema. Only a name TREK
|
||||
* gives a plugin schema counts.
|
||||
*/
|
||||
async function dumpPluginSchemas(source: string, toc: string, binDir: string | undefined): Promise<string[]> {
|
||||
createPrivateFile(toc);
|
||||
await run('pg_restore', binDir, ['--list', `--file=${toc}`, '--', source], {});
|
||||
const names = new Set<string>();
|
||||
for (const line of fs.readFileSync(toc, 'utf8').split('\n')) {
|
||||
const name = /^\d+; \d+ \d+ SCHEMA - (\S+) /.exec(line)?.[1];
|
||||
if (name !== undefined && isPluginSchemaName(name)) names.add(name);
|
||||
}
|
||||
return [...names].sort((a, b) => a.localeCompare(b));
|
||||
}
|
||||
|
||||
/** The tables in the current schema of the database at `url`, read before TREK opens it. */
|
||||
export async function pgTableNames(url: string, binDir: string | undefined): Promise<string[]> {
|
||||
const out = await run(
|
||||
@@ -222,7 +260,11 @@ export async function pgTableNames(url: string, binDir: string | undefined): Pro
|
||||
*/
|
||||
function migrationNamesFromCopy(script: string): string[] {
|
||||
const lines = script.split('\n');
|
||||
const copy = lines.findIndex((line) => /^COPY \S*mikro_orm_migrations \(/.test(line));
|
||||
// A plugin may name a table of its own schema mikro_orm_migrations too; only TREK's counts.
|
||||
const copy = lines.findIndex((line) => {
|
||||
const match = /^COPY (?:(\S+)\.)?mikro_orm_migrations \(/.exec(line);
|
||||
return match !== null && !isPluginSchemaName(match[1] ?? '');
|
||||
});
|
||||
if (copy < 0) return [];
|
||||
const columns =
|
||||
/\(([^)]*)\)/
|
||||
|
||||
Reference in New Issue
Block a user