feat(config): enforce runtime nft targets and managed sets from config.yaml
This commit is contained in:
1 parent
3c6c4e384b
commit
bffe28485d
7 files changed
+182
-24
No files matched your search
@@ -9,6 +9,7 @@ DEFAULT_HEAL_VERIFY_DELAY = 5
|
||||
DEFAULT_HEAL_MIN_INTERVAL = 900
|
||||
DEFAULT_CONFIG_PATH = "config.yaml"
|
||||
DEFAULT_NODE_NAME = "Unknown Node"
|
||||
DEFAULT_ENFORCEMENT_TARGETS = (("ip", "crowdsec"), ("ip6", "crowdsec6"))
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
@@ -19,6 +20,8 @@ class RuntimeSettings:
|
||||
check_interval: int
|
||||
heal_verify_delay: int
|
||||
heal_min_interval: int
|
||||
enforcement_targets: tuple[tuple[str, str], ...]
|
||||
enforcement_sets: tuple[str, ...]
|
||||
config_path: str
|
||||
|
||||
|
||||
@@ -43,6 +46,7 @@ def _load_config_minimal(config_path):
|
||||
"""
|
||||
config = {}
|
||||
current_section = None
|
||||
current_list_key = None
|
||||
|
||||
with open(config_path, "r") as f:
|
||||
for raw_line in f:
|
||||
@@ -59,9 +63,11 @@ def _load_config_minimal(config_path):
|
||||
if value == "":
|
||||
config[key] = {}
|
||||
current_section = key
|
||||
current_list_key = None
|
||||
else:
|
||||
config[key] = _parse_scalar(value)
|
||||
current_section = None
|
||||
current_list_key = None
|
||||
continue
|
||||
|
||||
if current_section is None:
|
||||
@@ -69,11 +75,22 @@ def _load_config_minimal(config_path):
|
||||
|
||||
stripped = line.strip()
|
||||
if stripped.startswith("- "):
|
||||
if current_list_key:
|
||||
config[current_section].setdefault(current_list_key, []).append(
|
||||
_parse_scalar(stripped[2:].strip())
|
||||
)
|
||||
continue
|
||||
if ":" not in stripped:
|
||||
continue
|
||||
key, value = stripped.split(":", 1)
|
||||
config[current_section][key.strip()] = _parse_scalar(value)
|
||||
key = key.strip()
|
||||
value = value.strip()
|
||||
if value == "":
|
||||
config[current_section][key] = []
|
||||
current_list_key = key
|
||||
else:
|
||||
config[current_section][key] = _parse_scalar(value)
|
||||
current_list_key = None
|
||||
|
||||
return config
|
||||
|
||||
@@ -119,6 +136,51 @@ def as_int(value, default):
|
||||
return default
|
||||
|
||||
|
||||
def _as_string_list(value):
|
||||
if isinstance(value, list):
|
||||
return [str(item).strip() for item in value if str(item).strip()]
|
||||
if isinstance(value, str):
|
||||
return [item.strip() for item in value.split(",") if item.strip()]
|
||||
return []
|
||||
|
||||
|
||||
def _resolve_enforcement_targets(config):
|
||||
targets_cfg = cfg_get(config, "enforcement", "targets")
|
||||
targets = []
|
||||
if isinstance(targets_cfg, list):
|
||||
for item in targets_cfg:
|
||||
if not isinstance(item, dict):
|
||||
continue
|
||||
family = str(item.get("family", "")).strip()
|
||||
table = str(item.get("table", "")).strip()
|
||||
if family and table:
|
||||
targets.append((family, table))
|
||||
if targets:
|
||||
return tuple(targets)
|
||||
|
||||
table_v4 = str(cfg_get(config, "enforcement", "table", default="crowdsec")).strip() or "crowdsec"
|
||||
table_v6 = str(cfg_get(config, "enforcement", "table6", default="crowdsec6")).strip() or "crowdsec6"
|
||||
families = _as_string_list(cfg_get(config, "enforcement", "families"))
|
||||
|
||||
if not families:
|
||||
return (("ip", table_v4), ("ip6", table_v6))
|
||||
|
||||
resolved = []
|
||||
for family in families:
|
||||
if family == "ip":
|
||||
resolved.append((family, table_v4))
|
||||
elif family == "ip6":
|
||||
resolved.append((family, table_v6))
|
||||
elif family == "inet":
|
||||
resolved.append((family, table_v4))
|
||||
|
||||
return tuple(resolved) if resolved else DEFAULT_ENFORCEMENT_TARGETS
|
||||
|
||||
|
||||
def _resolve_enforcement_sets(config):
|
||||
return tuple(_as_string_list(cfg_get(config, "enforcement", "sets")))
|
||||
|
||||
|
||||
def setup_logging(log_path):
|
||||
os.makedirs(os.path.dirname(os.path.abspath(log_path)), exist_ok=True)
|
||||
logging.basicConfig(
|
||||
@@ -163,6 +225,8 @@ def load_runtime_settings():
|
||||
),
|
||||
DEFAULT_HEAL_MIN_INTERVAL,
|
||||
)
|
||||
enforcement_targets = _resolve_enforcement_targets(config)
|
||||
enforcement_sets = _resolve_enforcement_sets(config)
|
||||
|
||||
return RuntimeSettings(
|
||||
node_name=node_name,
|
||||
@@ -171,5 +235,7 @@ def load_runtime_settings():
|
||||
check_interval=check_interval,
|
||||
heal_verify_delay=heal_verify_delay,
|
||||
heal_min_interval=heal_min_interval,
|
||||
enforcement_targets=enforcement_targets,
|
||||
enforcement_sets=enforcement_sets,
|
||||
config_path=config_path,
|
||||
)
|
||||
Reference in new issue
Block a user