Files
MikrotikManager/backend/src/modules/mcp/telemetry-settings-configuration.ts
T

50 lines
3.8 KiB
TypeScript

import { pool } from "../../db/index.js"
import { getTrafficSettings } from "../../services/traffic-collector.js"
import { readUptimeSettings } from "../../services/uptime-collector.js"
import { getServersApiPingSettings } from "../../services/servers-rest-ping-collector.js"
import { fingerprint, McpError, privateSecretFingerprint } from "./security.js"
// Fixed sources only. Bound the configuration AFTER removing the four fields
// collectors update; an arbitrarily long last_error is not configuration drift.
const trafficSql = `SELECT CASE WHEN octet_length((to_jsonb(s)-ARRAY['last_collected_at','last_duration_ms','last_error','updated_at'])::text)<=65536
THEN to_jsonb(s)-ARRAY['last_collected_at','last_duration_ms','last_error','updated_at'] ELSE NULL END AS configuration FROM traffic_settings s WHERE id=1 LIMIT 2`
const uptimeSql = `SELECT CASE WHEN octet_length((to_jsonb(s)-ARRAY['last_collected_at','last_duration_ms','last_error','updated_at'])::text)<=65536
THEN to_jsonb(s)-ARRAY['last_collected_at','last_duration_ms','last_error','updated_at'] ELSE NULL END AS configuration FROM uptime_settings s WHERE id=1 LIMIT 2`
const apiPingSql = `SELECT CASE WHEN octet_length((to_jsonb(s)-ARRAY['last_collected_at','last_duration_ms','last_error','updated_at'])::text)<=65536
THEN to_jsonb(s)-ARRAY['last_collected_at','last_duration_ms','last_error','updated_at'] ELSE NULL END AS configuration FROM servers_api_ping_settings s WHERE id=1 LIMIT 2`
const fail = (): never => { throw new McpError("STATE_UNAVAILABLE", "Конфигурация расписания недоступна или превышает предел", 409) }
async function read(sql: string, domain: string, fields: readonly (readonly [string, string, "boolean" | "integer"])[]) {
const rows = (await pool.query(sql)).rows
const raw: unknown = rows[0]?.configuration
if (rows.length !== 1 || !raw || typeof raw !== "object" || Array.isArray(raw) || Buffer.byteLength(JSON.stringify(raw)) > 65536) return fail()
const configuration = raw as Record<string, unknown>
if (configuration.id !== 1) return fail()
const value: Record<string, unknown> = {}
for (const [column, property, kind] of fields) {
const item = configuration[column]
if (kind === "boolean" ? typeof item !== "boolean" : !Number.isSafeInteger(item) || Number(item) <= 0) return fail()
value[property] = item
}
// Unknown/future raw columns remain sealed in the approval proof, including
// opaque configuration secrets. Only known fields and a purpose-keyed HMAC
// leave this reader; a public outer fingerprint cannot expose those values.
const configurationProof = await privateSecretFingerprint(fingerprint(configuration), "mcp-telemetry-settings-configuration-" + domain + "-v1")
return { ...value, configurationProof }
}
export async function readTrafficConfiguration() {
await getTrafficSettings() // Preserve the existing singleton initializer.
return read(trafficSql, "traffic", [["enabled", "enabled", "boolean"], ["interval_sec", "intervalSec", "integer"], ["retention_days", "retentionDays", "integer"]])
}
export async function readUptimeConfiguration() {
await readUptimeSettings()
return read(uptimeSql, "uptime", [["enabled", "enabled", "boolean"], ["resources_enabled", "resourcesEnabled", "boolean"],
["ping_enabled", "pingEnabled", "boolean"], ["speed_enabled", "speedEnabled", "boolean"], ["interval_sec", "intervalSec", "integer"],
["probe_interval_sec", "probeIntervalSec", "integer"], ["speed_interval_sec", "speedIntervalSec", "integer"], ["retention_days", "retentionDays", "integer"]])
}
export async function readServersApiPingConfiguration() {
await getServersApiPingSettings()
return read(apiPingSql, "servers-api-ping", [["enabled", "enabled", "boolean"], ["interval_sec", "intervalSec", "integer"]])
}