This commit is contained in:
2026-11-15 17:41:40 +01:00
parent e31f6f0ca1
commit 7864d2ed18
13 changed files with 1530 additions and 0 deletions

View File

@@ -0,0 +1,155 @@
"""Config loading + defaults. YAML file, secrets via env var only."""
from __future__ import annotations
import ipaddress
import os
from dataclasses import dataclass, field
from typing import Any
import yaml
DEFAULTS: dict[str, Any] = {
"vyos": {
"url": "https://10.66.0.1",
"key_env": "LEAF_VYOS_KEY",
"ca_bundle": None, # path to CA/pinned cert; None -> system CAs
"tls_verify": True,
"tls_cert_pin": "", # sha256 cert fingerprint; pins TLS trust (IP-hosted self-signed router cert)
"timeout": 10.0,
"retries": 3,
},
"block_group": "IDS-BLOCK",
"ensure_primitives": True, # verify group/rule hook exists on startup
"adopt_foreign_members": True, # members found on router but not in state db
"save_config": False, # POST /config-file save after every mutation
"eve": {
"path": "/var/log/suricata-leaf/eve.json",
"checkpoint": True, # resume at persisted offset across restarts
},
"alert_socket": {
# AF_UNIX listener for suricata's unix_stream output: alerts arrive
# through the socket instead of eve.json. empty path disables the
# socket path entirely (offline/mock runs keep pure file tailing).
"path": "/run/suricata/alerts.sock",
"queue_size": 1000,
},
"audit": {
"max_bytes": 16777216, # rotate decisions.jsonl at 16 MiB
"keep": 3, # rotated files retained
},
"metrics": {
# stats records are counting-only: never reach state/audit/API.
"reconcile_interval": 30.0, # one journal line per stats window
"capture_interval": 3600.0, # capture.log line cadence
"capture_log": None, # default: capture.log next to audit_log
"capture_max_bytes": 10485760, # rotate capture.log at 10 MiB...
"capture_log_keep": 6, # ... keeping six rotated files
"log_level": "INFO",
},
"policy": {
"mode": "threshold", # threshold | immediate
"min_hits": 2,
"window_seconds": 300,
"ttl_seconds": 3600,
"max_blocks": 50,
"allow_cidrs": [], # extra never-block networks
"sids_deny": [], # never act on these sids
"sids_only": [], # if non-empty: act ONLY on these sids
},
"always_allow_cidrs": [
# hard floor: never block routers/LAN/mgmt/mcast-ish space no matter what
"10.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16",
"0.0.0.0/8", "127.0.0.0/8", "240.0.0.0/4",
"100.64.0.0/10", "169.254.0.0/16", "224.0.0.0/4",
"fc00::/7", "fe80::/10",
],
"state_db": "/var/lib/leaf-controller/state.sqlite",
"audit_log": "/var/log/leaf-controller/decisions.jsonl",
"poll_interval": 0.2,
"sync_interval": 300.0,
}
def _merge(base: dict, override: dict) -> dict:
out = dict(base)
for k, v in override.items():
if isinstance(v, dict) and isinstance(out.get(k), dict):
out[k] = _merge(out[k], v)
else:
out[k] = v
return out
@dataclass
class Config:
raw: dict = field(default_factory=dict)
def get(self, dotted: str, default: Any = None) -> Any:
cur: Any = self.raw
for part in dotted.split("."):
if not isinstance(cur, dict) or part not in cur:
return default
cur = cur[part]
return cur
# --- derived helpers ---
def vyos_url(self) -> str:
return self.get("vyos.url").rstrip("/")
def vyos_key(self) -> str:
env = self.get("vyos.key_env", "LEAF_VYOS_KEY")
key = os.environ.get(env, "")
if not key and not self.get("dry_run"):
raise SystemExit(f"missing API key: set env var {env}")
return key
def allow_networks(self) -> list:
nets = list(DEFAULTS["always_allow_cidrs"]) + list(self.get("always_allow_cidrs", [])) + list(self.get("policy.allow_cidrs", []))
return [ipaddress.ip_network(c, strict=False) for c in nets]
def is_allowed_ip(self, ip: str) -> bool:
try:
addr = ipaddress.ip_address(ip)
except ValueError:
return True # malformed / non-ip (eg:66 etc) -> treat as allowed, never block
return any(addr in net for net in self.allow_networks())
def load_config(path: str) -> Config:
with open(path) as fh:
user = yaml.safe_load(fh) or {}
if not isinstance(user, dict):
raise SystemExit(f"{path}: expected a mapping")
cfg = Config(raw=_merge(DEFAULTS, user))
import math
for name in ('alert_socket.queue_size', 'policy.min_hits', 'policy.max_blocks',
'policy.window_seconds', 'policy.ttl_seconds', 'poll_interval',
'sync_interval', 'router_interval', 'vyos.timeout', 'vyos.retries'):
value = cfg.get(name, 2 if name == 'router_interval' else None)
if isinstance(value, bool) or not isinstance(value, (int, float)) or not math.isfinite(value) or value <= 0:
raise ValueError(name + ' must be a positive finite number')
for name in ('alert_socket.queue_size', 'policy.min_hits', 'policy.max_blocks', 'vyos.retries'):
if int(cfg.get(name)) != cfg.get(name):
raise ValueError(name + ' must be an integer')
for name in ('audit.max_bytes', 'audit.keep', 'metrics.capture_max_bytes', 'metrics.capture_log_keep'):
v = cfg.get(name)
if isinstance(v,bool) or not isinstance(v,int) or v < 0:
raise ValueError(name + ' must be a non-negative integer')
if cfg.get('policy.mode') not in ('threshold', 'immediate'):
raise ValueError('policy.mode must be threshold or immediate')
cfg.allow_networks() # Validate every configured CIDR at startup.
return cfg
def read_key_env(path, name='LEAF_VYOS_KEY'):
"""Read the simple quoted assignment emitted by the credential tool; never eval."""
import shlex
with open(path) as fh:
for line in fh:
key, sep, value = line.strip().partition('=')
if sep and key == name:
parts = shlex.split(value, comments=True)
if len(parts) != 1 or not parts[0]:
raise ValueError('invalid API key assignment')
return parts[0]
raise ValueError('API key assignment missing')