55 lines
4.2 KiB
TypeScript
55 lines
4.2 KiB
TypeScript
/** Opt-in REST v2 assembly; importing it does not replace the existing route.
|
|
* Authentication/local context is supplied by the server, never request JSON. */
|
|
import { McpError } from "../modules/mcp/security.js"
|
|
import { assertPortalClaimsCurrent } from "../modules/mcp/portal-revocation.js"
|
|
import { PROBE_STORAGE_LIMITS, type ProbeStorageLimits } from "../modules/mcp/diagnostic-cancellation-storage.js"
|
|
import { createProbeStorageHostIo } from "./server-probe-storage-host-io.js"
|
|
import { createRestServerProbeAuthority } from "./server-probe-authority.js"
|
|
import { createProductionServerProbeExecutor } from "./server-probe-execution.js"
|
|
import { createSqlServerProbeSourceReaders } from "./server-probe-storage-source.js"
|
|
import { createServerProbeRestExecution, type ServerProbeRestTarget, type ServerProbeRestContext } from "./server-probe-rest-execution.js"
|
|
import { resolveServerProbeRestBandwidthPeer, serverProbeRestErrorReply, ServerProbeRestContractError, type ServerProbeRestPeerMetadata } from "./server-probe-rest-contract.js"
|
|
|
|
const unavailable = (): never => { throw new McpError("STATE_CONFLICT", "Область HTTP-диагностики изменилась", 409) }
|
|
|
|
export function createStorageServerProbeRestExecution(limits: ProbeStorageLimits = PROBE_STORAGE_LIMITS) {
|
|
const io = createProbeStorageHostIo(limits, "rest")
|
|
// Called only inside createRestServerProbeAuthority's before/after portal
|
|
// fences: that authority grants fleet-wide server-write. Metadata is bounded
|
|
// to actual participants; it never enumerates all IDs or reads credentials.
|
|
const participants = async (target: ServerProbeRestTarget): Promise<number[]> => {
|
|
const rows = (await io.query("SELECT id FROM servers WHERE id=$1 LIMIT 2", [target.serverId])).rows
|
|
if (!rows.length) return []
|
|
if (rows.length !== 1 || rows[0].id !== target.serverId) return unavailable()
|
|
if (target.request.tool !== "bandwidth") return [target.serverId]
|
|
const explicit = target.request.dstServerId, remote = target.request.bwRemoteAddress!
|
|
if (explicit !== undefined && (!Number.isSafeInteger(explicit) || explicit <= 0)) throw new ServerProbeRestContractError(400, "INVALID_INPUT", "Некорректный dstServerId")
|
|
const explicitRows = explicit === undefined ? [] : (await io.query("SELECT id,host,enabled FROM servers WHERE id=$1 LIMIT 2", [explicit])).rows
|
|
if (explicitRows.length > 1) return unavailable()
|
|
const candidates = explicitRows.length ? explicitRows : (await io.query(`SELECT id,host,enabled FROM servers
|
|
WHERE enabled=true AND lower(btrim(host))=lower(btrim($1)) LIMIT 1`, [remote])).rows
|
|
const metadata = candidates as unknown as ServerProbeRestPeerMetadata[]
|
|
const peer = await resolveServerProbeRestBandwidthPeer(remote, explicit, metadata.map(row => row.id), {
|
|
byId: async () => explicitRows.length ? metadata[0] : undefined,
|
|
enabledByHost: async () => metadata,
|
|
})
|
|
return [...new Set([target.serverId, peer.id])]
|
|
}
|
|
const execute = createServerProbeRestExecution({ journal: io.journal,
|
|
authority: (actor, context, target) => {
|
|
if (!target) return unavailable()
|
|
return createRestServerProbeAuthority(actor, context, { query: io.rows,
|
|
portal: claims => io.withClient(client => assertPortalClaimsCurrent(claims, client)) }, () => participants(target))
|
|
},
|
|
executor: input => createProductionServerProbeExecutor(input, {
|
|
getServer: async id => createSqlServerProbeSourceReaders({ query: io.query }, (await input.readAuthority()).scopeServerIds).getServer(id),
|
|
findDestination: async (remote, explicit, scope) => createSqlServerProbeSourceReaders({ query: io.query }, scope).findDestination(remote, explicit, scope),
|
|
}),
|
|
})
|
|
return async (serverId: string, body: unknown, context: ServerProbeRestContext) => {
|
|
try { return await io.runRequest({ deadlineAt: Math.min(Date.now() + 300000, context.acceptedAt + 300000, context.actor?.expiresAt ?? Infinity), signal: context.signal }, () => execute(serverId, body, context)) }
|
|
catch (failure) { return serverProbeRestErrorReply(failure && typeof failure === "object" ? failure as { code?: string; statusCode?: number } : {}) }
|
|
}
|
|
}
|
|
export const executeStorageServerProbeRest = createStorageServerProbeRestExecution()
|