fix(gre): отклонять повреждённые и повторяющиеся native идентификаторы

This commit is contained in:
Denozordec committed 2026-10-14 02:09:13 +07:00
1 parent 51a579d370
commit 7a38bb36aa
19 files changed
+678 -19

No files matched your search

+1 -1
View File
@@ -24,7 +24,7 @@
"test:users": "tsx src/modules/users/iface-type.test.ts && tsx src/modules/users/bindings.test.ts && tsx src/modules/users/interface-inventory.test.ts && tsx src/routes/users-catalog.test.ts",
"test:pg": "tsx src/db/pg-pool.test.ts && tsx src/db/sql-bind.test.ts && tsx src/db/sqlite-json.test.ts && tsx src/db/traffic-flags.test.ts && tsx src/db/pg-schema.test.ts && tsx src/services/config-revisions.test.ts",
"test:recursive-routes": "tsx src/routes/recursive-routes.test.ts && tsx src/services/recursive-route-catalog.test.ts && tsx src/services/recursive-apply.test.ts",
"test:config-sync": "tsx src/utils/server-id.test.ts && tsx src/services/config-apply-plan.test.ts && tsx src/services/entity-snapshots.test.ts && tsx src/services/gre-ipv6.test.ts && tsx src/routes/gre-ipv6-validation.test.ts && tsx src/services/network-disabled-server.test.ts",
"test:config-sync": "tsx src/utils/server-id.test.ts && tsx src/services/config-apply-plan.test.ts && tsx src/services/entity-snapshots.test.ts && tsx src/services/gre-native-identity.test.ts && tsx src/services/gre-ipv6.test.ts && tsx src/routes/gre-ipv6-validation.test.ts && tsx src/services/network-disabled-server.test.ts",
"test:ros-file": "tsx src/services/ros-file-contents.test.ts && tsx src/services/mikrotik-response.test.ts",
"test:scheduler": "tsx src/services/scheduler-running.test.ts && tsx src/services/scheduler-recorded-run.test.ts && tsx src/routes/scheduler.test.ts",
"test:backups": "tsx src/services/backup-scheduler-run.test.ts && tsx src/services/backup-restore.test.ts && tsx src/services/backup-deletion.test.ts && tsx src/services/backup-dto.test.ts && tsx src/services/backup-storage-update.test.ts && tsx src/services/backup-job-execution.test.ts && tsx src/routes/backups-operations.test.ts && tsx src/services/s3-backup-client.test.ts",
@@ -0,0 +1,26 @@
import assert from 'node:assert/strict'
import fs from 'node:fs'
import {createServer} from 'node:http'
import {randomBytes} from 'node:crypto'
const database=process.env.MMAPP_OWN_SQL_DB,url=new URL(process.env.DATABASE_URL)
assert(/^mm_accept_166_\d+$/.test(database??''));assert.equal(url.pathname,'/'+database);assert.equal(url.username,database)
process.env.AUTH_REQUIRED='true';process.env.AUTH_JWT_SECRET=randomBytes(32).toString('hex');process.env.AUTH_ISSUER='https://own221.invalid';process.env.AUTH_PORTAL_URL='https://own221.invalid'
const {pool,closePool}=await import('/app/backend/dist/db/index.js'),{initDatabase}=await import('/app/backend/dist/db/bootstrap.js'),{collectGreBgpSnapshotOnce}=await import('/app/backend/dist/services/gre-bgp-snapshot-collector.js'),{buildApp}=await import('/app/backend/dist/index.js')
assert.equal((await pool.query('SELECT current_database() AS name')).rows[0].name,database);assert(Object.values((await pool.query('SELECT rolsuper,rolcreatedb,rolcreaterole,rolbypassrls FROM pg_roles WHERE rolname=current_user')).rows[0]).every(v=>v===false))
const baseline=process.env.MMAPP_BASELINE==='true',cases=[];let app,nativeCalls=0,nativeWrites=0,disabledCalls=0,variant='valid'
const fixture=label=>createServer((req,res)=>{nativeCalls++;if(label==='disabled')disabledCalls++;if(req.method!=='GET')nativeWrites++;res.setHeader('content-type','application/json');const gre=req.url==='/rest/interface/gre',bgp=req.url==='/rest/routing/bgp/session',normal={'.id':'*A',name:'own221-GRE «тест» / peer',disabled:'false',running:'true'},kinds={valid:[normal],empty:[],'missing-id':[{name:normal.name,disabled:'false',running:'true'}],'missing-name':[{'.id':'*A',disabled:'false',running:'true'}],'blank-id':[{...normal,'.id':''}],'blank-name':[{...normal,name:' '}],'whitespace-id':[{...normal,'.id':' *A'}],'number-field':[{...normal,mtu:1400}],'duplicate-id':[normal,{...normal,name:'other-GRE'}],'duplicate-name':[normal,{...normal,'.id':'*B'}],'trimmed-name-collision':[normal,{...normal,'.id':'*B',name:' '+normal.name+' '}]},rows=gre?(label==='second'?kinds[variant]:[normal]):bgp?[{'.id':'*1',name:'own221-peer',established:'true','remote.as':'65221','local.as':'65222'}]:[];res.end(JSON.stringify(rows))})
const natives=['first','second','disabled'].map(fixture);for(const n of natives)await new Promise(r=>n.listen(0,'127.0.0.1',r))
const report={date:'2026-10-14',block:'12.5.221',baseline,database,cases,scope:'Actual compiled Native GRE identity and actual API/config/snapshot paths, owned SQL/native HTTP fixture; signed reader/authenticated GRE API',passed:false}
try{
await initDatabase();const ids=[];for(let i=0;i<natives.length;i++)ids.push(Number((await pool.query("INSERT INTO servers(name,host,port,use_ssl,enabled,username,password,type) VALUES($1,'127.0.0.1',$2,false,$3,'fixture','fixture',$4) RETURNING id",['own221-'+i,natives[i].address().port,i<2,i===2?'home-router':'exit-node'])).rows[0].id))
app=await buildApp({logger:false,startScheduler:false});await app.ready();const token=app.jwt.sign({sub:'own221-reader',apps:['mm'],permissions:['mm:network:read'],iss:process.env.AUTH_ISSUER},{expiresIn:'5m'}),headers={authorization:'Bearer '+token};
for(variant of ['valid','empty','missing-id','missing-name','blank-id','blank-name','whitespace-id','number-field','duplicate-id','duplicate-name','trimmed-name-collision']){
const bad=!['valid','empty'].includes(variant),oldSourceAccepts=['valid','empty','whitespace-id','duplicate-name','trimmed-name-collision'].includes(variant),sourceUnknown=baseline?!oldSourceAccepts:bad,oldCount=['duplicate-id','duplicate-name','trimmed-name-collision'].includes(variant)?3:variant==='empty'?1:2,snapshot=await collectGreBgpSnapshotOnce()
assert.equal(snapshot.greWritten,sourceUnknown?0:oldCount);assert.equal(snapshot.bgpWritten,2);assert.equal(snapshot.errors.length,sourceUnknown?1:0);assert.equal(snapshot.fatalError,undefined)
cases.push({phase:variant,endpoint:'compiled collector',greWritten:snapshot.greWritten,bgpWritten:2,sourceErrors:snapshot.errors.length})
const before=Number((await pool.query("SELECT count(*) AS count FROM config_revisions WHERE server_id=$1 AND section='gre'",[ids[1]])).rows[0].count),api=await app.inject({method:'GET',url:'/api/gre/tunnels',headers}),body=api.json();assert.equal(api.statusCode,200);assert.equal(body.failures.length,bad&&!baseline?1:0);assert.equal(body.tunnels.length,bad&&!baseline?1:oldCount);const after=Number((await pool.query("SELECT count(*) AS count FROM config_revisions WHERE server_id=$1 AND section='gre'",[ids[1]])).rows[0].count);if(bad&&!baseline)assert.equal(after,before)
cases.push({phase:variant,endpoint:'signed GRE catalog',status:200,tunnels:body.tunnels.length,failures:body.failures.length,observedRevisionsAdded:after-before})
}
assert.equal(nativeWrites,0);assert.equal(disabledCalls,0);report.passed=true
}catch(e){report.errorName=e.name;fs.writeFileSync('/app/backend/acceptance/gre221-private.log',String(e.stack??e),{mode:0o600})}finally{if(app)await app.close();for(const n of natives){n.closeAllConnections();await new Promise(r=>n.close(r))}await closePool();report.nativeWrites=nativeWrites;report.disabledHomeRouterCalls=disabledCalls;fs.writeFileSync('/app/backend/acceptance/gre221-'+(baseline?'baseline':'fixed')+'.json',JSON.stringify(report,null,2));console.log(JSON.stringify(report))}if(!report.passed)process.exitCode=1
+6 -5
View File
@@ -12,7 +12,7 @@ import {
type GreSnapshot,
} from "./entity-snapshots.js"
import { captureAndAppendRevision } from "./config-revisions.js"
import { readGreObservedState } from "./gre-observed-state.js"
import { readGreObservedState, readGreNativeRows } from "./gre-observed-state.js"
type ServerRow = typeof servers.$inferSelect
@@ -114,8 +114,9 @@ export function mapGreLive(
gre: GreLiveIface[]
addrs: GreLiveAddr[]
} {
const observedGre = readGreNativeRows(greRaw)
const addrsByIface = new Map<string, GreLiveAddr[]>()
const greNames = new Set(greRaw.map(g => (g.name ?? "").trim()))
const greNames = new Set(observedGre.map(g => g.name.trim()))
for (const a of addrsRaw) {
if (rosDisabled(a.dynamic)) continue
const iface = (a.interface ?? "").trim()
@@ -134,9 +135,9 @@ export function mapGreLive(
const addrs: GreLiveAddr[] = []
const tunnels: LiveGreTunnel[] = []
greRaw.forEach((g, idx) => {
const rosId = String(g[".id"] ?? g.name ?? `gre-${idx}`)
const name = (g.name ?? "").trim() || `gre-${idx + 1}`
observedGre.forEach((g) => {
const rosId = g[".id"]
const name = g.name.trim()
const keepalive = parseKeepalive(g.keepalive)
const fromComment = parseInnerFromComment(g.comment)
const ifaceAddrs = addrsByIface.get(name) ?? []
@@ -0,0 +1,9 @@
import assert from 'node:assert/strict'
import {readGreNativeRows} from './gre-observed-state.js'
import {mapGreLive} from './gre-live.js'
import {servers} from '../db/schema.js'
const row={'.id':'*A',name:'GRE «тест» / peer',disabled:'false',running:'true'},server={id:1} as typeof servers.$inferSelect
assert.deepEqual(readGreNativeRows([]),[]);assert.equal(mapGreLive(server,[row],[]).tunnels[0].id,'*A');assert.equal(mapGreLive(server,[row],[]).tunnels[0].name,row.name)
const broken=[{},null,[null],[[]],[{...row,'.id':''}],[{...row,name:' '}],[{...row,'.id':undefined}],[{...row,name:undefined}],[{...row,mtu:1400}],[{...row,'.id':' *A'}],[row,row],[row,{...row,'.id':'*B'}],[row,{...row,'.id':'*B',name:' '+row.name+' '}]]
for(const raw of broken){assert.throws(()=>readGreNativeRows(raw),/каталог GRE|native ID/);assert.throws(()=>mapGreLive(server,raw as never,[]),/каталог GRE|native ID/)}
console.log('GRE identity: '+broken.length+' malformed/duplicate cases rejected by raw reader and catalog mapper; empty/native ID/special name preserved')
@@ -1,5 +1,20 @@
import { observedBoolean } from "./ospf-observed-values.js"
/** Native identity must exist before commands or configuration snapshots refer to it. */
export function readGreNativeRows(raw: unknown): Array<Record<string, string>> {
if (!Array.isArray(raw) || raw.some(row => !row || typeof row !== "object" || Array.isArray(row)
|| Object.values(row).some(value => typeof value !== "string")
|| [".id", "name"].some(key => typeof row[key] !== "string" || !row[key].trim())
|| row[".id"] !== row[".id"].trim())) {
throw new Error("Некорректный каталог GRE: отсутствует native ID/имя или повреждены поля")
}
if (new Set(raw.map(row => row[".id"])).size !== raw.length
|| new Set(raw.map(row => row.name.trim())).size !== raw.length) {
throw new Error("Повторяющиеся native ID или имена GRE")
}
return raw as Array<Record<string, string>>
}
/** Disabled is authoritative; an enabled interface also needs a known running flag. */
export function readGreObservedState(flags: { running?: string; disabled?: string }, name: string) {
const disabled = observedBoolean(flags.disabled)
@@ -16,7 +16,7 @@ import {mapGreLive} from './gre-live.js'
import {mapGreAlertRow} from './gre-tunnels-live.js'
let combinations=0
for(const disabled of ['true','yes','false','no',undefined,'','junk'])for(const running of ['true','yes','false','no',undefined,'','junk']){
const row={'.id':'*1',name:'own-flags',disabled,running},off=['true','yes'].includes(disabled??''),on=['false','no'].includes(disabled??''),runningKnown=['true','yes','false','no'].includes(running??'')
const row={'.id':'*1',name:'own-flags',...(disabled===undefined?{}:{disabled}),...(running===undefined?{}:{running})},off=['true','yes'].includes(disabled??''),on=['false','no'].includes(disabled??''),runningKnown=['true','yes','false','no'].includes(running??'')
if(off||on&&runningKnown){const expected=off?'down':['true','yes'].includes(running??'')?'up':'degraded';assert.equal(mapGreAlertRow(devices[0],row).status,expected);assert.equal(mapGreLive(devices[0],[row],[]).tunnels[0].status,expected)}
else {assert.throws(()=>mapGreAlertRow(devices[0],row),/Не подтверждено состояние GRE/);assert.throws(()=>mapGreLive(devices[0],[row],[]),/Не подтверждено состояние GRE/)}
combinations++
+2 -4
View File
@@ -2,8 +2,7 @@ import { eq } from "drizzle-orm"
import { db } from "../db/index.js"
import { servers } from "../db/schema.js"
import { MikrotikClient } from "./mikrotik.js"
import { readInternetPathRows } from "./internet-path-snapshot.js"
import { readGreObservedState } from "./gre-observed-state.js"
import { readGreObservedState, readGreNativeRows } from "./gre-observed-state.js"
type ServerRow = typeof servers.$inferSelect
@@ -40,8 +39,7 @@ export function mapGreAlertRow(server: ServerRow, g: RosGre, idx = 0): GreTunnel
async function fetchGreForServer(server: ServerRow): Promise<GreTunnelLiveRow[]> {
const client = MikrotikClient.fromServer(server)
const greRaw = readInternetPathRows(await client.get<unknown>("/interface/gre"), "GRE alert source", [".id", "name"])
if (new Set(greRaw.map(row => row[".id"])).size !== greRaw.length) throw new Error("Повторяющиеся ID GRE")
const greRaw = readGreNativeRows(await client.get<unknown>("/interface/gre"))
return greRaw.map((g, idx) => mapGreAlertRow(server, g, idx))
}