Files
MikrotikManager/backend/src/services/server-probe-storage-metadata.sql-helper.ts
T

169 lines
11 KiB
TypeScript

/** Disposable owned PostgreSQL only: cancellation while real source metadata is
* blocked, before any native dispatch or durable admission. */
import assert from "node:assert/strict"
import { randomUUID } from "node:crypto"
import { createServer } from "node:http"
import { realpath } from "node:fs/promises"
import { basename, dirname, resolve } from "node:path"
export async function acceptProbeStorageMetadataSql() {
assert.equal(process.env.MMAPP_OWN_PROBE_STORAGE_JOURNAL, "true")
const url = new URL(process.env.DATABASE_URL ?? "postgresql://invalid")
assert(["postgres:", "postgresql:"].includes(url.protocol)); assert.equal(url.hostname, "127.0.0.1")
assert.equal(decodeURIComponent(url.username), "mcp_test"); assert.equal(url.pathname, "/postgres")
assert.equal(process.env.MMAPP_OWN_SQL_DB, "postgres"); assert.equal(url.search, ""); assert.equal(url.hash, "")
assert(process.env.MCP_STATE_DIR)
const state = resolve(process.env.MCP_STATE_DIR)
assert.equal(basename(state), "state"); assert.match(basename(dirname(state)), /^mm-mcp-test-[A-Za-z0-9-]+$/)
assert.equal(resolve(await realpath(state)).toLowerCase(), state.toLowerCase())
// All application/driver imports follow the owned database and state guards.
const { pool } = await import("../db/index.js")
const { createStorageServerProbeRestExecution } = await import("./server-probe-storage-rest.js")
const { default: Fastify } = await import("fastify")
const { validatorCompiler, serializerCompiler } = await import("@fastify/type-provider-zod")
const { SignJWT } = await import("jose")
const { default: authPlugin, requireAuth } = await import("../plugins/auth.js")
const { createStorageProbeRoutes } = await import("../routes/probe-storage-handler.js")
const { env } = await import("../config.js")
assert.equal(env.authRequired, true)
const wait = async (predicate: () => Promise<boolean>, message: string, budget = 15000) => {
const deadline = Date.now() + budget
while (!await predicate()) {
assert(Date.now() < deadline, message)
await new Promise<void>(yes => setTimeout(yes, 20))
}
}
let nativeCalls = 0, sourceId: number | undefined
const router = createServer((_request, response) => {
nativeCalls++; response.statusCode = 500; response.end('{"error":"Unexpected owned metadata native dispatch"}')
})
await new Promise<void>((yes, no) => { router.once("error", no); router.listen(0, "127.0.0.1", yes) })
const controller = new AbortController(), requestId = randomUUID()
const httpController = new AbortController()
let httpApp: import("fastify").FastifyInstance | undefined
const locker = await pool.connect()
let locked = false, backendPid: number | undefined
try {
assert.equal((await pool.query("SELECT to_regclass('server_probe_storage_admissions') IS NOT NULL AS ready")).rows[0].ready, true)
sourceId = Number((await pool.query(`INSERT INTO servers(name,host,port,use_ssl,verify_ssl,enabled,username,password)
VALUES($1,'127.0.0.1',$2,false,false,true,'owned-metadata-user','owned-metadata-password') RETURNING id`,
["owned-metadata-" + randomUUID(), (router.address() as { port: number }).port])).rows[0].id)
const before = Number((await pool.query("SELECT count(*) AS n FROM server_probe_storage_admissions")).rows[0].n)
const lockerPid = Number((await locker.query("SELECT pg_backend_pid() AS pid")).rows[0].pid)
await locker.query("BEGIN"); locked = true
await locker.query("SET LOCAL idle_in_transaction_session_timeout='20000ms'")
await locker.query("LOCK TABLE servers IN ACCESS EXCLUSIVE MODE")
const run = createStorageServerProbeRestExecution()
const pending = run(String(sourceId), { tool: "ping", target: "127.0.0.1", pingCount: 1 }, {
actor: undefined, authRequired: false, requestId, acceptedAt: Date.now(), signal: controller.signal,
})
// Observe the actual production SELECT waiting on our relation lock. The
// observer never reads query parameters, credentials or provider output.
await wait(async () => {
const rows = (await pool.query(`SELECT pid FROM pg_stat_activity
WHERE datname=current_database() AND usename=current_user AND wait_event_type='Lock'
AND wait_event='relation' AND $1::int=ANY(pg_blocking_pids(pid))
AND query LIKE 'SELECT id FROM servers WHERE id=$1 LIMIT 2%'`, [lockerPid])).rows
assert(rows.length <= 1, "More than one owned metadata SELECT is blocked")
if (rows.length) backendPid = Number(rows[0].pid)
return rows.length === 1
}, "Production source metadata did not reach the owned relation lock")
const abortedAt = Date.now(); controller.abort()
let timer: ReturnType<typeof setTimeout> | undefined
try {
const result = await Promise.race([pending, new Promise<never>((_yes, no) => {
timer = setTimeout(() => no(Error("Metadata cancellation did not settle while table lock remained held")), 2000)
})])
assert(result.statusCode >= 400); assert.deepEqual(Object.keys(result.body), ["error"])
assert.equal(JSON.stringify(result).includes("owned-metadata-password"), false)
} finally { if (timer) clearTimeout(timer) }
const cancellationElapsedMs = Date.now() - abortedAt
assert(cancellationElapsedMs < 2000)
// Prove the caller settled before we release the conflicting table lock.
assert.equal(Number((await locker.query(`SELECT count(*) AS n FROM pg_locks
WHERE pid=pg_backend_pid() AND relation='servers'::regclass AND mode='AccessExclusiveLock' AND granted`)).rows[0].n), 1)
assert.equal(nativeCalls, 0)
assert.equal(Number((await pool.query("SELECT count(*) AS n FROM server_probe_storage_admissions")).rows[0].n), before)
assert.equal(Number((await pool.query("SELECT count(*) AS n FROM server_probe_executions WHERE request_id=$1", [requestId])).rows[0].n), 0)
await locker.query("ROLLBACK"); locked = false
// Closing the driver connection bounds the caller immediately; PostgreSQL
// may observe that disconnect only after the relation lock is released.
await wait(async () => Number((await pool.query("SELECT count(*) AS n FROM pg_stat_activity WHERE pid=$1", [backendPid])).rows[0].n) === 0,
"Cancelled metadata backend survived relation lock release")
assert.equal(nativeCalls, 0)
httpApp = Fastify({ logger: false })
httpApp.setValidatorCompiler(validatorCompiler); httpApp.setSerializerCompiler(serializerCompiler)
await httpApp.register(authPlugin); httpApp.addHook("onRequest", requireAuth)
let handlerFinished!: () => void, handlerCalls = 0
const handlerSettled = new Promise<void>(yes => { handlerFinished = yes })
// Fixture observation only: delegate the real handler with its original
// receiver/arguments; no executor, authority, connector or result override.
httpApp.addHook("onRoute", options => {
const original = options.handler
options.handler = async function (request, reply) {
handlerCalls++
try { return await original.call(this, request, reply) }
finally { handlerFinished() }
}
})
await httpApp.register(createStorageProbeRoutes(), { prefix: "/api" })
const base = await httpApp.listen({ host: "127.0.0.1", port: 0 })
const httpOwner = "owned-metadata-http-" + randomUUID()
const jwt = await new SignJWT({ sub: httpOwner, apps: ["mm"], permissions: ["mm:servers:write"] })
.setProtectedHeader({ alg: "HS256" }).setIssuer(env.authIssuer).setIssuedAt().setExpirationTime("1h")
.sign(new TextEncoder().encode(env.jwtSecret))
await locker.query("BEGIN"); locked = true
await locker.query("SET LOCAL idle_in_transaction_session_timeout='20000ms'")
await locker.query("LOCK TABLE servers IN ACCESS EXCLUSIVE MODE")
const httpPending = fetch(`${base}/api/servers/${sourceId}/probes/run`, { method: "POST", signal: httpController.signal,
headers: { "content-type": "application/json", authorization: "Bearer " + jwt },
body: JSON.stringify({ tool: "ping", target: "127.0.0.1", pingCount: 1 }) }).then(
async response => { await response.body?.cancel(); return false }, () => true)
backendPid = undefined
await wait(async () => {
const rows = (await pool.query(`SELECT pid FROM pg_stat_activity
WHERE datname=current_database() AND usename=current_user AND wait_event_type='Lock'
AND wait_event='relation' AND $1::int=ANY(pg_blocking_pids(pid))
AND query LIKE 'SELECT id FROM servers WHERE id=$1 LIMIT 2%'`, [lockerPid])).rows
assert(rows.length <= 1, "More than one owned HTTP metadata SELECT is blocked")
if (rows.length) backendPid = Number(rows[0].pid)
return rows.length === 1
}, "HTTP metadata did not reach the owned relation lock")
const disconnectedAt = Date.now(); httpController.abort()
let httpTimer: ReturnType<typeof setTimeout> | undefined
try {
await Promise.race([handlerSettled, new Promise<never>((_yes, no) => {
httpTimer = setTimeout(() => no(Error("HTTP handler did not settle while metadata table lock remained held")), 2000)
})])
} finally { if (httpTimer) clearTimeout(httpTimer) }
const httpCancellationElapsedMs = Date.now() - disconnectedAt
assert(httpCancellationElapsedMs < 2000); assert.equal(await httpPending, true); assert.equal(handlerCalls, 1)
assert.equal(Number((await locker.query(`SELECT count(*) AS n FROM pg_locks
WHERE pid=pg_backend_pid() AND relation='servers'::regclass AND mode='AccessExclusiveLock' AND granted`)).rows[0].n), 1)
assert.equal(nativeCalls, 0)
assert.equal(Number((await pool.query("SELECT count(*) AS n FROM server_probe_storage_admissions")).rows[0].n), before)
assert.equal(Number((await pool.query("SELECT count(*) AS n FROM server_probe_executions WHERE actor_id=$1", [httpOwner])).rows[0].n), 0)
await locker.query("ROLLBACK"); locked = false
await wait(async () => Number((await pool.query("SELECT count(*) AS n FROM pg_stat_activity WHERE pid=$1", [backendPid])).rows[0].n) === 0,
"HTTP cancelled metadata backend survived relation lock release")
assert.equal(nativeCalls, 0)
return { actualProductionRestMetadata: true, observedRelationLock: true, callerCancelledWhileLockHeld: true,
cancellationElapsedMs, cancelledBackendGoneAfterUnlock: true, noAdmissionBeforeClaim: true,
localServerOwnedAuthority: true, nativeOrDnsCalls: nativeCalls, httpDisconnectMetadataClaimed: true,
actualHttpSignedJwt: true, handlerSettledWhileLockHeld: true, httpCancellationElapsedMs, routesRegisteredInFixtureOnly: true,
productionDdlApplied: false, routesRegistered: false }
} finally {
controller.abort(); httpController.abort()
try { if (locked) await locker.query("ROLLBACK") }
finally {
locker.release()
try { if (sourceId !== undefined) await pool.query("DELETE FROM servers WHERE id=$1", [sourceId]) }
finally {
try { await httpApp?.close() }
finally { router.closeAllConnections(); await new Promise<void>(yes => router.close(() => yes())) }
}
}
}
}