Code
This commit is contained in:
176
controller/leaf_controller/state.py
Normal file
176
controller/leaf_controller/state.py
Normal file
@@ -0,0 +1,176 @@
|
||||
"""sqlite-backed state: block list w/ TTL, hit windowing, applied tracking."""
|
||||
from __future__ import annotations
|
||||
|
||||
import sqlite3
|
||||
import time
|
||||
import threading
|
||||
from dataclasses import dataclass
|
||||
|
||||
SCHEMA = """
|
||||
CREATE TABLE IF NOT EXISTS controller_meta (key TEXT PRIMARY KEY, value TEXT NOT NULL);
|
||||
CREATE TABLE IF NOT EXISTS blocks (
|
||||
ip TEXT PRIMARY KEY, -- ip/cidr as pushed to VyOS (plain ip = /32 semantics)
|
||||
kind TEXT NOT NULL, -- dest|src
|
||||
sigs TEXT NOT NULL DEFAULT '',-- csv of triggering sids
|
||||
first_seen REAL NOT NULL,
|
||||
last_hit REAL NOT NULL,
|
||||
expires REAL NOT NULL,
|
||||
applied INTEGER NOT NULL DEFAULT 0
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS hits (
|
||||
sid INTEGER NOT NULL,
|
||||
ip TEXT NOT NULL,
|
||||
ts REAL NOT NULL
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS hits_idx ON hits (sid, ip, ts);
|
||||
CREATE TABLE IF NOT EXISTS eve_ckpt (
|
||||
id INTEGER PRIMARY KEY CHECK (id = 1),
|
||||
dev INTEGER NOT NULL, -- st_dev/st_ino of the EVE file last read
|
||||
ino INTEGER NOT NULL,
|
||||
pos INTEGER NOT NULL, -- byte offset of the next unread record
|
||||
updated_at REAL NOT NULL
|
||||
);
|
||||
"""
|
||||
|
||||
|
||||
@dataclass
|
||||
class BlockRow:
|
||||
ip: str
|
||||
kind: str
|
||||
sigs: str
|
||||
first_seen: float
|
||||
last_hit: float
|
||||
expires: float
|
||||
applied: int
|
||||
|
||||
|
||||
class State:
|
||||
def __init__(self, path: str):
|
||||
self.path = path
|
||||
self._local = threading.local()
|
||||
self.con.executescript(SCHEMA)
|
||||
columns = {r[1] for r in self.con.execute("PRAGMA table_info(hits)")}
|
||||
if "event_key" not in columns:
|
||||
self.con.execute("ALTER TABLE hits ADD COLUMN event_key TEXT")
|
||||
self.con.execute("CREATE UNIQUE INDEX IF NOT EXISTS hits_event ON hits(event_key)")
|
||||
self.con.commit()
|
||||
|
||||
@property
|
||||
def con(self):
|
||||
# Alert processing and router I/O have independent connections.
|
||||
if not hasattr(self._local, "con"):
|
||||
con = sqlite3.connect(self.path, timeout=10)
|
||||
con.execute("PRAGMA journal_mode=WAL")
|
||||
con.execute("PRAGMA busy_timeout=10000")
|
||||
self._local.con = con
|
||||
return self._local.con
|
||||
|
||||
def close(self):
|
||||
if hasattr(self._local, "con"):
|
||||
self._local.con.close()
|
||||
del self._local.con
|
||||
|
||||
# ---- hit windowing (threshold mode) ----
|
||||
def record_hit(self, sid: int, ip: str, ts: float | None = None) -> None:
|
||||
event_key = getattr(self._local, "event_key", None)
|
||||
if event_key is not None:
|
||||
event_key = f'{event_key}:{sid}:{ip}'
|
||||
self.con.execute("INSERT OR IGNORE INTO hits (sid, ip, ts, event_key) VALUES (?,?,?,?)",
|
||||
(sid, ip, ts or time.time(), event_key))
|
||||
self.con.commit()
|
||||
|
||||
def hit_count(self, sid: int, ip: str, window: float, now: float | None = None) -> int:
|
||||
now = now or time.time()
|
||||
(n,) = self.con.execute(
|
||||
"SELECT count(*) FROM hits WHERE sid=? AND ip=? AND ts>=?",
|
||||
(sid, ip, now - window)).fetchone()
|
||||
return n
|
||||
|
||||
def prune_hits(self, max_age: float, now: float | None = None) -> None:
|
||||
now = now or time.time()
|
||||
self.con.execute("DELETE FROM hits WHERE ts < ?", (now - max_age,))
|
||||
self.con.commit()
|
||||
|
||||
# ---- blocks ----
|
||||
def _row(self, r: sqlite3.Row | tuple) -> BlockRow:
|
||||
return BlockRow(*r)
|
||||
|
||||
def get(self, ip: str) -> BlockRow | None:
|
||||
r = self.con.execute("SELECT * FROM blocks WHERE ip=?", (ip,)).fetchone()
|
||||
return self._row(r) if r else None
|
||||
|
||||
def upsert_hit(self, ip: str, kind: str, sid: int, ttl: float,
|
||||
now: float | None = None) -> BlockRow:
|
||||
"""Create block entry or refresh expiry/last_hit of existing one."""
|
||||
now = now or time.time()
|
||||
# Serialize the read/modify/write with expiry cleanup in the worker.
|
||||
with self.con:
|
||||
self.con.execute("BEGIN IMMEDIATE")
|
||||
row = self.get(ip)
|
||||
if row:
|
||||
sigs = row.sigs
|
||||
if str(sid) not in sigs.split(","):
|
||||
sigs = f"{sigs},{sid}" if sigs else str(sid)
|
||||
self.con.execute(
|
||||
"UPDATE blocks SET expires=?, last_hit=?, sigs=? WHERE ip=?",
|
||||
(now + ttl, now, sigs, ip))
|
||||
else:
|
||||
self.con.execute("INSERT INTO blocks VALUES (?,?,?,?,?,?,0)",
|
||||
(ip, kind, str(sid), now, now, now + ttl))
|
||||
result = self.get(ip)
|
||||
return result
|
||||
|
||||
def set_applied(self, ip: str, applied: int = 1) -> None:
|
||||
self.con.execute("UPDATE blocks SET applied=? WHERE ip=?", (applied, ip))
|
||||
self.con.commit()
|
||||
|
||||
def remove(self, ip: str, expired_before: float | None = None) -> bool:
|
||||
# A new alert may refresh a block while the router commit is in flight.
|
||||
with self.con:
|
||||
if expired_before is None:
|
||||
cur = self.con.execute("DELETE FROM blocks WHERE ip=?", (ip,))
|
||||
else:
|
||||
cur = self.con.execute("DELETE FROM blocks WHERE ip=? AND expires<=?",
|
||||
(ip, expired_before))
|
||||
if cur.rowcount:
|
||||
self.con.execute("DELETE FROM hits WHERE ip=?", (ip,))
|
||||
return bool(cur.rowcount)
|
||||
|
||||
def all_blocks(self) -> list[BlockRow]:
|
||||
return [self._row(r) for r in self.con.execute("SELECT * FROM blocks")]
|
||||
|
||||
def expired(self, now: float | None = None) -> list[BlockRow]:
|
||||
now = now or time.time()
|
||||
return [self._row(r) for r in
|
||||
self.con.execute("SELECT * FROM blocks WHERE expires<=?", (now,))]
|
||||
|
||||
def unapplied(self) -> list[BlockRow]:
|
||||
return [self._row(r) for r in
|
||||
self.con.execute("SELECT * FROM blocks WHERE applied=0")]
|
||||
|
||||
def count_active(self, now: float | None = None) -> int:
|
||||
now = now or time.time()
|
||||
(n,) = self.con.execute("SELECT count(*) FROM blocks WHERE expires>?", (now,)).fetchone()
|
||||
return n
|
||||
|
||||
# ---- EVE read checkpoint (survives controller restarts) ----
|
||||
def get_checkpoint(self) -> tuple | None:
|
||||
r = self.con.execute("SELECT dev, ino, pos FROM eve_ckpt WHERE id=1").fetchone()
|
||||
return tuple(r) if r else None
|
||||
|
||||
def save_checkpoint(self, dev: int, ino: int, pos: int, now: float | None = None) -> None:
|
||||
with self.con:
|
||||
self.con.execute(
|
||||
"INSERT INTO eve_ckpt VALUES (1,?,?,?,?) "
|
||||
"ON CONFLICT(id) DO UPDATE SET dev=excluded.dev, ino=excluded.ino, "
|
||||
"pos=excluded.pos, updated_at=excluded.updated_at",
|
||||
(dev, ino, pos, now or time.time()))
|
||||
|
||||
def router_dirty(self):
|
||||
r = self.con.execute("SELECT value FROM controller_meta WHERE key='router_dirty'").fetchone()
|
||||
return bool(r and r[0] == '1')
|
||||
|
||||
def set_router_dirty(self, dirty):
|
||||
with self.con:
|
||||
self.con.execute("INSERT OR REPLACE INTO controller_meta VALUES ('router_dirty', ?)",
|
||||
('1' if dirty else '0',))
|
||||
Reference in New Issue
Block a user