feat: harden firewall policies telemetry and operations
quality / changes (push) Successful in 7s
quality / commitlint (push) Skipped
quality / openapi (push) Successful in 30s
quality / web (push) Successful in 1m3s
quality / api (push) Successful in 1m15s
quality / linux-agent-smoke (push) Failing after 27s
quality / docker-check (push) Failing after 2m28s
CD / quality (push) Failing after 5m52s
CD / publish (push) Skipped

Implement reliable agent apply, telemetry v2, analytics retention, guarded rollouts, enrollment, labels and webhooks. Improve ReUI frontend data safety and bounded list loading, and add Docker smoke, recovery and resource checks. Document local verification and pending native acceptance.
This commit is contained in:
Denozordec committed 2026-10-26 13:10:58 +07:00
1 parent aa475848fd
commit 9fa6269174
242 files changed
+19872 -3057

No files matched your search

+64 -232
View File
@@ -1,13 +1,13 @@
import { readFileSync } from 'node:fs'
import { createHash, timingSafeEqual } from 'node:crypto'
import { createHash } from 'node:crypto'
import { join } from 'node:path'
import type { FastifyPluginAsync } from 'fastify'
import { repos } from '@evofw/db'
import { enrollBodySchema, applyReportBodySchema, hostFwRuleSchema, hostListenerSchema } from '@evofw/shared'
import { applyReportBodySchema, POLICY_BUDGET, POLICY_BUDGET_ERROR, agentPolicyQuerySchema, policyBudgetErrorEnvelopeSchema } from '@evofw/shared'
import type { AppConfig } from '../config.js'
import { hashToken } from '../plugins/auth.js'
import { evaluateAgentPolicy } from '../services/policy/evaluate.js'
import { evaluateAgentPolicy, policyBudgetExceeded } from '../services/policy/evaluate.js'
import { renderMikrotikPolicyRsc } from '../services/policy/mikrotik-rsc.js'
import { ingestTelemetry } from '../services/telemetry.js'
import { AppError } from '../plugins/error-handler.js'
import { resolveAgentScriptsDir } from '../services/agent-scripts-path.js'
import { resolveAndRenderInstall } from '../services/install-links.js'
@@ -18,33 +18,8 @@ import {
const scriptsDir = resolveAgentScriptsDir()
/** Constant-time seed check; hash first so lengths always match. */
function seedMatches(presented: string | undefined, expected: string): boolean {
if (!presented) return false
const a = createHash('sha256').update(presented).digest()
const b = createHash('sha256').update(expected).digest()
return timingSafeEqual(a, b)
}
function sanitizeHostFirewall(raw: {
rules?: unknown[]
listeners?: unknown[]
}): { rules: unknown[]; listeners: unknown[] } {
const rules: unknown[] = []
for (const item of raw.rules ?? []) {
const parsed = hostFwRuleSchema.safeParse(item)
if (parsed.success) rules.push(parsed.data)
}
const listeners: unknown[] = []
for (const item of raw.listeners ?? []) {
const parsed = hostListenerSchema.safeParse(item)
if (parsed.success) listeners.push(parsed.data)
}
return {
rules: rules.slice(0, 500),
listeners: listeners.slice(0, 200),
}
}
export const agentRoutes: FastifyPluginAsync<{ config: AppConfig }> = async (
app,
@@ -68,7 +43,7 @@ export const agentRoutes: FastifyPluginAsync<{ config: AppConfig }> = async (
config.publicBaseUrl,
config.enrollSeed,
)
return reply.type(contentType).send(body)
return reply.header('cache-control', 'no-store').type(contentType).send(body)
},
)
@@ -96,123 +71,59 @@ export const agentRoutes: FastifyPluginAsync<{ config: AppConfig }> = async (
return reply.type('text/plain').send(body)
})
app.post('/v1/agent/enroll', async (req, reply) => {
const seed = req.headers['x-evofw-seed']
const expected =
repos.getSetting(app.db, 'enroll_seed') || config.enrollSeed
if (!seedMatches(String(seed ?? ''), expected)) {
throw new AppError('UNAUTHORIZED', 'Invalid enroll seed', 401)
}
const body = enrollBodySchema.parse(req.body)
const tokenHash = hashToken(body.token)
const existingByToken = repos.getAgentByTokenHash(app.db, tokenHash)
if (existingByToken) {
throw new AppError('CONFLICT', 'Token already enrolled', 409)
}
if (body.install_link_id) {
const link = repos.getInstallLink(app.db, body.install_link_id)
if (!link) {
throw new AppError('NOT_FOUND', 'Install link not found', 404)
}
if (link.revokedAt) {
throw new AppError('GONE', 'Install link revoked', 410)
}
if (!link.agentId) {
throw new AppError('CONFLICT', 'Install link has no agent', 409)
}
const invited = repos.getAgent(app.db, link.agentId)
if (!invited) {
throw new AppError('NOT_FOUND', 'Agent not found', 404)
}
if (invited.status !== 'invited' && invited.status !== 'pending') {
throw new AppError(
'CONFLICT',
`Agent status is ${invited.status}`,
409,
)
}
const agent = repos.updateAgent(app.db, invited.id, {
name: body.name,
hostname: body.hostname ?? null,
platform: body.platform ?? invited.platform,
tokenPrefix: body.token.slice(0, 12),
tokenHash,
status: 'pending',
clientVersion: body.client_version ?? null,
app.get('/v1/agent/policy', async (req, reply) => {
const query = agentPolicyQuerySchema.parse(req.query)
try {
const agentId = req.agentId!
// Record contact first — empty policy (no rule sets) is a valid online state.
repos.updateAgent(app.db, agentId, {
lastSeenAt: new Date().toISOString(),
lastSeenIp: req.ip,
})
return reply.code(201).send({
client_id: agent!.id,
id: agent!.id,
status: agent!.status,
name: agent!.name,
})
}
const id = crypto.randomUUID()
const now = new Date().toISOString()
const agent = repos.insertAgent(app.db, {
id,
name: body.name,
hostname: body.hostname ?? null,
platform: body.platform ?? 'linux',
tokenPrefix: body.token.slice(0, 12),
tokenHash,
status: 'pending',
defaultAction: 'accept',
policyGeneration: 1,
clientVersion: body.client_version ?? null,
settingsJson: '{}',
createdAt: now,
lastSeenAt: now,
lastSeenIp: req.ip,
})
return reply.code(201).send({
client_id: agent!.id,
id: agent!.id,
status: agent!.status,
name: agent!.name,
})
})
app.get('/v1/agent/policy', async (req) => {
const agentId = req.agentId!
// Record contact first — empty policy (no rule sets) is a valid online state.
repos.updateAgent(app.db, agentId, {
lastSeenAt: new Date().toISOString(),
lastSeenIp: req.ip,
})
const policy = evaluateAgentPolicy(app.db, agentId)
const scriptSha = getSyncScriptMeta().sha256
return {
generation: policy.generation,
hash: policy.hash,
apply_version: policy.applyVersion,
default_action: policy.defaultAction,
policy_mode: policy.policyMode,
script_sha256: scriptSha,
deny_cidrs: policy.denyCidrs,
allow_cidrs: policy.allowCidrs,
port_rules: policy.portRules.map((r) => ({
id: r.id,
action: r.action,
protocol: r.protocol,
port_start: r.portStart,
port_end: r.portEnd,
src_cidrs: r.srcCidrs,
})),
sync_interval_sec: policy.syncIntervalSec,
// compat: prefixes = deny when default accept, else allow (legacy single-bag clients)
prefixes:
policy.defaultAction === 'accept'
? policy.denyCidrs
: policy.allowCidrs,
total:
policy.defaultAction === 'accept'
? policy.denyCidrs.length
: policy.allowCidrs.length,
const policy = evaluateAgentPolicy(app.db, agentId)
const scriptSha = getSyncScriptMeta().sha256
const response = {
generation: policy.generation,
hash: policy.hash,
apply_version: policy.applyVersion,
default_action: policy.defaultAction,
policy_mode: policy.policyMode,
script_sha256: scriptSha,
deny_cidrs: policy.denyCidrs,
allow_cidrs: policy.allowCidrs,
port_rules: policy.portRules.map((r) => ({
id: r.id,
action: r.action,
protocol: r.protocol,
port_start: r.portStart,
port_end: r.portEnd,
src_cidrs: r.srcCidrs,
})),
sync_interval_sec: policy.syncIntervalSec,
// compat: prefixes = deny when default accept, else allow (legacy single-bag clients)
prefixes:
policy.defaultAction === 'accept'
? policy.denyCidrs
: policy.allowCidrs,
total:
policy.defaultAction === 'accept'
? policy.denyCidrs.length
: policy.allowCidrs.length,
}
const json = JSON.stringify(response)
if (Buffer.byteLength(json) > POLICY_BUDGET.jsonBytes) policyBudgetExceeded('8MiB policy response JSON')
const etag = `"policy-v1-${createHash('sha256').update(json).digest('hex')}"`
repos.updateAgent(app.db, agentId, { desiredHash: policy.hash, desiredHashGeneration: policy.generation, lastPolicyDeliveredAt: new Date().toISOString() })
reply.header('etag', etag).header('cache-control', 'private, no-cache')
if (ifNoneMatchHits(req.headers['if-none-match'], etag)) return reply.code(304).send()
return reply.type('application/json').send(json)
} catch (error) {
if (query.transport === 'routeros' && error instanceof AppError && error.code === POLICY_BUDGET_ERROR) {
reply.removeHeader('etag')
return reply.header('cache-control', 'no-store').code(200).send(policyBudgetErrorEnvelopeSchema.parse({ error: { code: POLICY_BUDGET_ERROR, message: error.message.slice(0, 512) } }))
}
throw error
}
})
@@ -223,98 +134,19 @@ export const agentRoutes: FastifyPluginAsync<{ config: AppConfig }> = async (
lastSeenIp: req.ip,
})
const policy = evaluateAgentPolicy(app.db, agentId)
return reply.type('text/plain').send(renderMikrotikPolicyRsc(policy))
const script = renderMikrotikPolicyRsc(policy)
if (Buffer.byteLength(script) > POLICY_BUDGET.jsonBytes) policyBudgetExceeded('8MiB RouterOS policy response')
repos.updateAgent(app.db, agentId, { desiredHash: policy.hash, desiredHashGeneration: policy.generation, lastPolicyDeliveredAt: new Date().toISOString() })
const etag = `"policy-rsc-v1-${createHash('sha256').update(script).digest('hex')}"`
reply.header('etag', etag).header('cache-control', 'private, no-cache')
if (ifNoneMatchHits(req.headers['if-none-match'], etag)) return reply.code(304).send()
return reply.type('text/plain').send(script)
})
app.post('/v1/agent/apply-report', async (req) => {
const agentId = req.agentId!
const body = applyReportBodySchema.parse(req.body)
const now = new Date().toISOString()
const prev = repos.getAgent(app.db, agentId)
const reportedDropped = body.packets_dropped ?? 0
const reportedAccepted = body.packets_accepted ?? 0
const prevDropped = prev?.lastApplyPacketsDropped ?? 0
const prevAccepted = prev?.lastApplyPacketsAccepted ?? 0
// Absolute-since-chain-create from agent. If counters reset (re-apply),
// treat the new absolute as the delta; else add the increase.
const deltaDropped =
reportedDropped >= prevDropped
? reportedDropped - prevDropped
: reportedDropped
const deltaAccepted =
reportedAccepted >= prevAccepted
? reportedAccepted - prevAccepted
: reportedAccepted
const totalDropped = (prev?.totalPacketsDropped ?? 0) + deltaDropped
const totalAccepted = (prev?.totalPacketsAccepted ?? 0) + deltaAccepted
// Chain/set counters were flushed (policy apply). Zero per-IP baselines so
// the next epoch of element counters accumulates (zeros are omitted from ip_hits).
// Port hits use a separate dynamic set that is not flushed on apply — leave baselines.
if (reportedDropped < prevDropped) {
repos.resetIpBlockStatsBaselines(app.db, agentId)
}
repos.updateAgent(app.db, agentId, {
lastApplyAt: now,
lastApplyStatus: body.status,
lastApplyError: body.error ?? null,
lastApplyPrefixCount: body.prefix_count ?? 0,
lastApplyPacketsDropped: reportedDropped,
lastApplyPacketsAccepted: reportedAccepted,
totalPacketsDropped: totalDropped,
totalPacketsAccepted: totalAccepted,
lastApplyKernelMethod: body.kernel_method ?? null,
lastSeenAt: now,
lastSeenIp: req.ip,
})
repos.insertStatsSample(app.db, {
id: crypto.randomUUID(),
agentId,
packetsDropped: reportedDropped,
packetsAccepted: reportedAccepted,
prefixCount: body.prefix_count ?? 0,
kernelMethod: body.kernel_method ?? null,
recordedAt: now,
})
if (body.ip_hits?.length) {
const presence = body.source === 'mikrotik'
repos.upsertIpBlockStats(app.db, agentId, body.ip_hits, now, {
mode: presence ? 'presence' : 'absolute',
})
}
if (body.port_hits?.length && body.source !== 'mikrotik') {
repos.upsertPortBlockStats(app.db, agentId, body.port_hits, now)
}
if (body.host_firewall && body.source !== 'mikrotik') {
const sanitized = sanitizeHostFirewall(body.host_firewall)
// Empty snapshot must not wipe a previous good collect (agent bash bugs / race).
if (
sanitized.rules.length === 0 &&
sanitized.listeners.length === 0
) {
const existing = repos.getHostFirewallSnapshot(app.db, agentId)
if (existing) {
return { ok: true }
}
}
const payloadJson = JSON.stringify({
rules: sanitized.rules,
listeners: sanitized.listeners,
})
const rawDigest = createHash('sha256')
.update(payloadJson)
.digest('hex')
.slice(0, 16)
repos.upsertHostFirewallSnapshot(
app.db,
agentId,
payloadJson,
now,
rawDigest,
)
}
return { ok: true }
const result = ingestTelemetry(app.db, req.agentId!, body, req.ip)
return { ...result, ...(body.telemetry_version === 2 ? { report_id: body.report_id, sequence: body.sequence, sequence_floor: repos.getTelemetryState(app.db, req.agentId!)!.sequence } : {}) }
})
app.post('/v1/agent/heartbeat', async (req) => {