Warn rather than refuse on a storage URL mismatch, and scrub the provisioning DSN before discovery
CI / full-gate (push) Successful in 2m59s
CI / full-gate (push) Successful in 2m59s
This commit is contained in:
@@ -5,7 +5,7 @@
|
||||
import { test } from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { randomUUID } from "node:crypto";
|
||||
import { differentServer, ensureJwks, firstRunBanner, identityPayload, permissionTuple, provisionPluginStorage, seedAdmin, seedPermissions } from "./bootstrap.ts";
|
||||
import { ensureJwks, firstRunBanner, identityPayload, permissionTuple, provisionPluginStorage, seedAdmin, seedPermissions, serverMismatch } from "./bootstrap.ts";
|
||||
import { createLogger } from "../logger.ts";
|
||||
import type { Plugin } from "../plugin-host/plugin.ts";
|
||||
import type { ProvisionOptions, ProvisionResult } from "../plugin-host/storage-provisioning.ts";
|
||||
@@ -200,16 +200,17 @@ test("the connection limit and derived secret reach the provisioner", async () =
|
||||
});
|
||||
|
||||
// bootstrap creates the role on one server; web tells the plugin to connect to another. Left
|
||||
// unchecked it surfaces inside a plugin as "password authentication failed", naming neither.
|
||||
test("provisioning refuses when the two storage URLs name different servers", async () => {
|
||||
// unsaid it surfaces inside a plugin as "password authentication failed", naming neither. Warned
|
||||
// rather than refused: web reaching a pooler bootstrap cannot provision through is legitimate.
|
||||
test("a storage URL mismatch is reported, and provisioning still runs", async () => {
|
||||
const { calls, provision } = recordingProvisioner();
|
||||
const env = { PLUGIN_DB_ADMIN_URL: "postgres://ory:ory@db-a:5432/ory", PLUGIN_DB_URL: "postgres://db-b:5432" };
|
||||
await assert.rejects(provisionPluginStorage(env, [storagePlugin("things")], SILENT, provision), /one server.*db-a:5432 vs db-b:5432/s);
|
||||
assert.deepEqual(calls, []);
|
||||
await provisionPluginStorage(env, [storagePlugin("things")], SILENT, provision);
|
||||
assert.equal(calls.length, 1);
|
||||
});
|
||||
|
||||
test("the same server spelled with an implicit port still agrees", () => {
|
||||
assert.equal(differentServer("postgres://ory:ory@db:5432/ory", "postgres://db"), null); // 5432 is the default
|
||||
assert.equal(differentServer("postgres://ory:ory@db:5432/ory", undefined), null); // web's own boot error to raise
|
||||
assert.equal(differentServer("postgres://ory:ory@db:5432/ory", "postgres://db:6543"), "db:5432 vs db:6543");
|
||||
assert.equal(serverMismatch("postgres://ory:ory@db:5432/ory", "postgres://db"), null); // 5432 is the default
|
||||
assert.equal(serverMismatch("postgres://ory:ory@db:5432/ory", undefined), null); // web's own boot error to raise
|
||||
assert.equal(serverMismatch("postgres://ory:ory@db:5432/ory", "postgres://db:6543"), "db:5432 vs db:6543");
|
||||
});
|
||||
|
||||
+11
-6
@@ -146,7 +146,7 @@ export function firstRunBanner(opts: { appUrl: string; email: string; password:
|
||||
// --- CLI (the bootstrap container entrypoint) ----------------------------------------
|
||||
|
||||
async function main() {
|
||||
const env = process.env;
|
||||
const env = { ...process.env }; // snapshot: the storage credentials leave process.env before discovery
|
||||
// Structured like the web app so prod logs stay uniform; honour LOG_FORMAT/SERVICE_NAME.
|
||||
const log = createLogger({
|
||||
format: env["LOG_FORMAT"] === "json" ? "json" : "text",
|
||||
@@ -155,6 +155,10 @@ async function main() {
|
||||
// runWithLog makes `log` ambient so seedAdmin's tracedFetch traces the Kratos/Keto seed calls.
|
||||
await runWithLog(log, async () => {
|
||||
if (ensureJwks(env["JWKS_FILE"] ?? "/etc/config/kratos/tokenizer/jwks.json")) log.info("generated a JWKS signing key");
|
||||
// Discovery imports every plugin module — and its dependencies — into *this* process, which holds
|
||||
// the credential that may CREATE DATABASE/ROLE. Same move as server.ts, on the stronger secret.
|
||||
delete process.env["PLUGIN_DB_ADMIN_URL"];
|
||||
delete process.env["PLUGIN_DB_SECRET"];
|
||||
const plugins = await discoverPlugins();
|
||||
await provisionPluginStorage(env, plugins, log);
|
||||
await seedAdminAndPermissions(env, plugins, log);
|
||||
@@ -172,10 +176,11 @@ export async function provisionPluginStorage(env: Env, plugins: Plugin[], log: L
|
||||
// last storage plugin is exactly when an orphaned database needs naming.
|
||||
if (ids.length === 0 && !adminUrl) return;
|
||||
if (!adminUrl) throw new Error(`bootstrap: PLUGIN_DB_ADMIN_URL must be set — these plugins declare storage: ${ids.join(", ")}`);
|
||||
// Provisioned here, connected to from web: a different server means the role exists in one place
|
||||
// and is looked for in another, surfacing as "password authentication failed" inside a plugin.
|
||||
const serverMismatch = differentServer(adminUrl, env["PLUGIN_DB_URL"]);
|
||||
if (serverMismatch) throw new Error(`bootstrap: PLUGIN_DB_ADMIN_URL and PLUGIN_DB_URL must name one server (${serverMismatch})`);
|
||||
// Provisioned here, connected to from web: a different server means the role is created in one
|
||||
// place and looked for in another, surfacing inside a plugin as "password authentication failed".
|
||||
// Warned, not refused — web reaching a pooler that cannot run CREATE DATABASE is a legitimate split.
|
||||
const mismatch = serverMismatch(adminUrl, env["PLUGIN_DB_URL"]);
|
||||
if (mismatch) log.warn("PLUGIN_DB_ADMIN_URL and PLUGIN_DB_URL name different servers", { servers: mismatch });
|
||||
const result = await provision({
|
||||
adminUrl,
|
||||
connectionLimit: resolvePluginDbConnectionLimit(env),
|
||||
@@ -191,7 +196,7 @@ export async function provisionPluginStorage(env: Env, plugins: Plugin[], log: L
|
||||
|
||||
// Describes the disagreement, or null when they agree (or when web's URL is unset — that is web's
|
||||
// own boot error to raise, naming the plugin that wanted storage).
|
||||
export function differentServer(adminUrl: string, webUrl: string | undefined): string | null {
|
||||
export function serverMismatch(adminUrl: string, webUrl: string | undefined): string | null {
|
||||
if (!webUrl) return null;
|
||||
const [admin, web] = [safeHostPort(adminUrl), safeHostPort(webUrl)];
|
||||
if (admin === null || web === null || admin === web) return null; // a malformed URL fails in config.ts
|
||||
|
||||
@@ -86,6 +86,9 @@ test("the provisioning superuser DSN reaches bootstrap only, never web", () => {
|
||||
const boot = compose.slice(compose.indexOf("\n bootstrap:"));
|
||||
const overrideWeb = override.slice(override.indexOf("\n web:"), override.indexOf("\n bootstrap:"));
|
||||
assert.match(boot, /PLUGIN_DB_ADMIN_URL:/, "bootstrap is given the superuser DSN");
|
||||
// Reordering the override's services would empty this slice, and every doesNotMatch below would
|
||||
// then pass against "".
|
||||
assert.ok(overrideWeb.includes("PLUGIN_DB_URL"), "sliced the dev override's web block");
|
||||
for (const [name, block] of [["base", webBlock], ["dev override", overrideWeb]] as const)
|
||||
assert.doesNotMatch(block, /PLUGIN_DB_ADMIN_URL/, `${name} web never sees it`);
|
||||
assert.match(webBlock, /PLUGIN_DB_URL:\s*\$\{PLUGIN_DB_URL/, "base wires web's base URL from env");
|
||||
|
||||
@@ -17,7 +17,9 @@ export interface ProvisionResult {
|
||||
}
|
||||
|
||||
export async function provisionStorage(options: ProvisionOptions): Promise<ProvisionResult> {
|
||||
const sql = postgres(options.adminUrl, { connect_timeout: 10, max: 1, onnotice: () => {} });
|
||||
// Notices are left to surface: a REVOKE the account cannot perform only *warns*, and silencing
|
||||
// that would mean reporting a locked-down database that is still open to PUBLIC.
|
||||
const sql = postgres(options.adminUrl, { connect_timeout: 10, max: 1 });
|
||||
try {
|
||||
const provisioned: string[] = [];
|
||||
for (const pluginId of options.pluginIds) {
|
||||
|
||||
@@ -63,8 +63,6 @@ test("quoting doubles an embedded quote", () => {
|
||||
assert.equal(quoteLiteral("we'ird"), "'we''ird'");
|
||||
});
|
||||
|
||||
// No NOSUPERUSER: naming it in an ALTER is superuser-only, so re-asserting it would break every
|
||||
// boot after the first under the least-privilege account the README recommends.
|
||||
const ATTRIBUTES = "LOGIN NOCREATEDB NOCREATEROLE CONNECTION LIMIT 10";
|
||||
|
||||
test("only the plugins that asked for storage are provisioned", () => {
|
||||
|
||||
@@ -76,9 +76,7 @@ export function provisionSql(plan: ProvisionPlan): string[] {
|
||||
throw new Error(`storage: connectionLimit must be a positive integer, got ${plan.connectionLimit}`);
|
||||
}
|
||||
const identifier = quoteIdentifier(plan.name);
|
||||
// No NOSUPERUSER: only a superuser may name SUPERUSER in an ALTER, so re-asserting it would fail
|
||||
// every boot after the first under the CREATEDB+CREATEROLE account the README recommends. CREATE
|
||||
// defaults to NOSUPERUSER and a non-superuser cannot grant it, so nothing is given up.
|
||||
// No NOSUPERUSER: naming SUPERUSER in an ALTER is superuser-only, and CREATE defaults to it anyway.
|
||||
const attributes = `LOGIN NOCREATEDB NOCREATEROLE CONNECTION LIMIT ${plan.connectionLimit} PASSWORD ${quoteLiteral(plan.password)}`;
|
||||
return [
|
||||
plan.roleExists ? `ALTER ROLE ${identifier} WITH ${attributes}` : `CREATE ROLE ${identifier} ${attributes}`,
|
||||
|
||||
@@ -31,9 +31,9 @@ test("init SQL gives each Ory service its own database, and leaves plugin databa
|
||||
}
|
||||
});
|
||||
|
||||
// AGENTS.md records that the driver runs the provisioning DDL in bootstrap "and nothing else".
|
||||
// discovery.ts once imported a validator from the module that held it, quietly putting the driver in
|
||||
// web's graph — the claim outlived the fact, which is what this catches.
|
||||
// AGENTS.md records that the driver runs the provisioning DDL in bootstrap and nothing else. A
|
||||
// single value imported from the wrong module puts it in web's graph without changing behaviour,
|
||||
// so nothing but this would notice.
|
||||
test("the Postgres driver reaches bootstrap only, never web's import graph", () => {
|
||||
const files = sourceFiles();
|
||||
assert.ok(files.length > 40, "walks the source tree");
|
||||
|
||||
@@ -61,6 +61,10 @@ const storageCredentials = new Map<string, StorageCredentials>();
|
||||
if (pluginDbUrl !== undefined) {
|
||||
for (const id of declaresStorage) storageCredentials.set(id, buildCredentials(pluginDbUrl, id, config.pluginDbSecret));
|
||||
}
|
||||
// onBoot is the only way credentials are handed over, so without one the database is provisioned
|
||||
// and unreachable. A warning, not a refusal — the plugin still works, it just cannot store anything.
|
||||
const unreachable = plugins.filter((plugin) => plugin.storage && !plugin.hooks?.onBoot).map((plugin) => plugin.id);
|
||||
if (unreachable.length > 0) log.warn("plugins declare storage but have no onBoot to receive it", { plugins: unreachable.join(", ") });
|
||||
|
||||
// plugin onBoot — after discovery, before listen; a throw aborts boot.
|
||||
await runBootHooks(plugins, (plugin) => {
|
||||
|
||||
Reference in New Issue
Block a user