Files
EvoFirewall/apps/api/src/services/policy/evaluate.ts
T
Denozordec 7a3f1fad25 perf(api): ретенция статистики, устранение N+1 и пагинация списков
- retention: ежедневный cron (03:17) + запуск на старте удаляет сырые
  agent_stats_samples старше STATS_RETENTION_DAYS (по умолчанию 30);
  lifetime-итоги на агенте и агрегаты сохраняются
- N+1 на поллинг-пути агента: evaluateAgentPolicy теперь делает по одному
  батч-запросу на записи списков / резолвы hostname / имена списков вместо
  запроса на каждое правило; mapPolicyRules и port-rules GET — аналогично
- пагинация: GET /agents, /lists, /rules, /install-links принимают
  limit/offset и возвращают total; без параметров — прежнее поведение
- openapi.yaml: approve-bulk, PUT /policy-sets/{id}/agents, параметры
  пагинации (redocly lint OK); тесты (47 passed)
2026-09-20 19:20:30 +07:00

296 lines
7.8 KiB
TypeScript

import { createHash } from 'node:crypto'
import type { Db } from '@evofw/db'
import { repos } from '@evofw/db'
import {
defaultActionFromLegacyMode,
legacyModeFromDefaultAction,
type DefaultAction,
} from '@evofw/shared'
import { uniqCidrs } from '../uniq.js'
export const POLICY_APPLY_VERSION = 3 as const
export type PolicyChainStep = {
setId: string | null
setName: string | null
ruleId: string | null
action: 'allow' | 'deny'
sourceKind: 'list' | 'cidr' | 'hostname' | 'override'
sourceLabel: string
cidrCount: number
}
export type EvaluatedPortRule = {
id: string
action: 'open' | 'close'
protocol: 'tcp' | 'udp'
portStart: number
portEnd: number
srcCidrs: string[]
}
export type EvaluatedPolicy = {
generation: number
hash: string
applyVersion: typeof POLICY_APPLY_VERSION
defaultAction: DefaultAction
/** @deprecated mirror for old agents */
policyMode: 'blacklist' | 'whitelist'
denyCidrs: string[]
allowCidrs: string[]
portRules: EvaluatedPortRule[]
conflictsDropped: number
syncIntervalSec: number
chain: PolicyChainStep[]
summary: {
sets: number
rulesDeny: number
rulesAllow: number
cidrsDeny: number
cidrsAllow: number
overrides: number
conflictsDropped: number
portRules: number
}
}
/**
* Prefetched expansion data: one batched query per kind instead of a query
* per rule (this code runs on every agent policy poll, ~60s per agent).
*/
type ExpansionContext = {
entriesByList: Map<string, string[]>
resolvedByRule: Map<string, string[]>
listNames: Map<string, string>
}
function buildExpansionContext(
db: Db,
rules: { id: string; listId: string | null; hostname: string | null }[],
portRuleRows: { listId: string | null }[],
): ExpansionContext {
const listIds = [
...new Set(
[
...rules.map((r) => r.listId?.trim() || ''),
...portRuleRows.map((r) => r.listId?.trim() || ''),
].filter(Boolean),
),
]
const hostnameRuleIds = [
...new Set(rules.filter((r) => r.hostname?.trim()).map((r) => r.id)),
]
return {
entriesByList: repos.mapIpListEntriesByListIds(db, listIds),
resolvedByRule: repos.mapResolvedCidrsByRuleIds(db, hostnameRuleIds),
listNames: repos.mapIpListNames(db, listIds),
}
}
function expandRule(
ctx: ExpansionContext,
rule: {
cidr: string | null
listId: string | null
hostname: string | null
id: string
},
): string[] {
if (rule.cidr?.trim()) return [rule.cidr.trim()]
if (rule.hostname?.trim()) {
return ctx.resolvedByRule.get(rule.id) ?? []
}
const listId = rule.listId?.trim() || ''
return listId ? (ctx.entriesByList.get(listId) ?? []) : []
}
function resolveDefaultAction(agentDefaultAction: string | null | undefined): DefaultAction {
if (agentDefaultAction === 'drop' || agentDefaultAction === 'accept') {
return agentDefaultAction
}
return defaultActionFromLegacyMode(agentDefaultAction)
}
function sourceMeta(
ctx: ExpansionContext,
rule: {
cidr: string | null
listId: string | null
hostname: string | null
},
): { kind: 'list' | 'cidr' | 'hostname'; label: string } {
if (rule.cidr?.trim()) {
return { kind: 'cidr', label: rule.cidr.trim() }
}
if (rule.hostname?.trim()) {
return { kind: 'hostname', label: rule.hostname.trim() }
}
const listId = rule.listId?.trim() || ''
const name = listId ? ctx.listNames.get(listId) : null
return { kind: 'list', label: name || listId || 'list' }
}
function expandPortSrcCidrs(
ctx: ExpansionContext,
row: {
srcKind: string
srcCidr: string | null
listId: string | null
},
): string[] {
if (row.srcKind === 'all') return ['0.0.0.0/0']
if (row.srcKind === 'cidr' && row.srcCidr?.trim()) {
return [row.srcCidr.trim()]
}
if (row.srcKind === 'list') {
const cidrs = row.listId
? (ctx.entriesByList.get(row.listId.trim()) ?? [])
: []
return cidrs.length ? uniqCidrs(cidrs) : []
}
return []
}
function expandPortRules(
ctx: ExpansionContext,
rows: ReturnType<typeof repos.listEnabledAgentPortRules>,
): EvaluatedPortRule[] {
const out: EvaluatedPortRule[] = []
for (const row of rows) {
const action = row.action === 'close' ? 'close' : 'open'
const srcCidrs = expandPortSrcCidrs(ctx, row)
if (!srcCidrs.length) continue
const portStart = Math.max(1, Math.min(65535, row.portStart))
const portEnd = Math.max(portStart, Math.min(65535, row.portEnd))
const protocols: Array<'tcp' | 'udp'> =
row.protocol === 'udp'
? ['udp']
: row.protocol === 'both'
? ['tcp', 'udp']
: ['tcp']
for (const protocol of protocols) {
out.push({
id: row.id,
action,
protocol,
portStart,
portEnd,
srcCidrs,
})
}
}
return out
}
/** Evaluate allow/deny sets for an agent from assigned policy sets. */
export function evaluateAgentPolicy(db: Db, agentId: string): EvaluatedPolicy {
const agent = repos.getAgent(db, agentId)
if (!agent) {
throw new Error(`agent not found: ${agentId}`)
}
const assignedSets = repos
.listSetsForAgent(db, agentId)
.filter((s) => s.enabled === 1)
const ordered = repos.listPolicyRulesForAgent(db, agentId)
const overrides = repos.listOverrides(db, agentId)
const portRuleRows = repos.listEnabledAgentPortRules(db, agentId)
const ctx = buildExpansionContext(db, ordered, portRuleRows)
const deny: string[] = []
const allow: string[] = []
const chain: PolicyChainStep[] = []
let rulesDeny = 0
let rulesAllow = 0
for (const rule of ordered) {
const cidrs = expandRule(ctx, rule)
const action = rule.action === 'deny' ? 'deny' : 'allow'
if (action === 'deny') {
deny.push(...cidrs)
rulesDeny += 1
} else {
allow.push(...cidrs)
rulesAllow += 1
}
const src = sourceMeta(ctx, rule)
const setName =
assignedSets.find((s) => s.setId === rule.setId)?.name ?? null
chain.push({
setId: rule.setId,
setName,
ruleId: rule.id,
action,
sourceKind: src.kind,
sourceLabel: src.label,
cidrCount: cidrs.length,
})
}
for (const o of overrides) {
const action = o.action === 'deny' ? 'deny' : 'allow'
if (action === 'deny') deny.push(o.cidr)
else allow.push(o.cidr)
chain.push({
setId: null,
setName: null,
ruleId: null,
action,
sourceKind: 'override',
sourceLabel: o.cidr,
cidrCount: 1,
})
}
const denyCidrs = uniqCidrs(deny)
const denySet = new Set(denyCidrs)
const allowRaw = uniqCidrs(allow)
const allowCidrs = allowRaw.filter((c) => !denySet.has(c))
const conflictsDropped = allowRaw.length - allowCidrs.length
const defaultAction = resolveDefaultAction(agent.defaultAction)
const policyMode = legacyModeFromDefaultAction(defaultAction)
const portRules = expandPortRules(ctx, portRuleRows)
const payload = JSON.stringify({
apply_version: POLICY_APPLY_VERSION,
generation: agent.policyGeneration,
defaultAction,
denyCidrs,
allowCidrs,
portRules,
})
const hash = `sha256:${createHash('sha256').update(payload).digest('hex')}`
const syncIntervalSec =
Number(repos.getSetting(db, 'agent_sync_interval_sec') || '60') || 60
return {
generation: agent.policyGeneration,
hash,
applyVersion: POLICY_APPLY_VERSION,
defaultAction,
policyMode,
denyCidrs,
allowCidrs,
portRules,
conflictsDropped,
syncIntervalSec,
chain,
summary: {
sets: assignedSets.length,
rulesDeny,
rulesAllow,
cidrsDeny: denyCidrs.length,
cidrsAllow: allowCidrs.length,
overrides: overrides.length,
conflictsDropped,
portRules: portRules.length,
},
}
}
export function truncateCidrs(cidrs: string[], limit: number): string[] {
if (limit <= 0) return []
return cidrs.slice(0, limit)
}