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
+17
-13
@@ -12,11 +12,11 @@ except ImportError:
|
||||
return None
|
||||
|
||||
class FirewallAuditor:
|
||||
def __init__(self):
|
||||
def __init__(self, targets=None, managed_sets=None):
|
||||
"""Ensures the NFT kernel state matches the LAPI's desired state."""
|
||||
load_dotenv()
|
||||
self.table = "crowdsec"
|
||||
self.set_name = "0.0.0.0/0-cscli"
|
||||
self.targets = list(targets or (("ip", "crowdsec"), ("ip6", "crowdsec6")))
|
||||
self.managed_sets = {str(name) for name in (managed_sets or ()) if str(name)}
|
||||
|
||||
def get_remote_lapi_truth(self):
|
||||
"""Queries CrowdSec decisions and normalizes across schema variants."""
|
||||
@@ -106,19 +106,18 @@ class FirewallAuditor:
|
||||
def get_kernel_state(self):
|
||||
"""Scans BOTH IPv4 and IPv6 sets in the crowdsec tables."""
|
||||
all_ips = {}
|
||||
targets = [
|
||||
{"family": "ip", "table": "crowdsec"},
|
||||
{"family": "ip6", "table": "crowdsec6"}
|
||||
]
|
||||
for target in targets:
|
||||
for family, table in self.targets:
|
||||
try:
|
||||
cmd = ["/usr/sbin/nft", "-j", "list", "table", target["family"], target["table"]]
|
||||
cmd = ["/usr/sbin/nft", "-j", "list", "table", family, table]
|
||||
res = subprocess.run(cmd, capture_output=True, text=True, check=True)
|
||||
data = json.loads(res.stdout)
|
||||
|
||||
for item in data.get('nftables', []):
|
||||
nft_set = item.get('set')
|
||||
if nft_set:
|
||||
set_name = nft_set.get("name")
|
||||
if self.managed_sets and set_name not in self.managed_sets:
|
||||
continue
|
||||
elements = nft_set.get('elem', [])
|
||||
for e in elements:
|
||||
if isinstance(e, dict):
|
||||
@@ -136,9 +135,9 @@ class FirewallAuditor:
|
||||
# Fallback if it's just a raw string IP
|
||||
all_ips[e] = None
|
||||
except subprocess.CalledProcessError:
|
||||
logging.debug(f"Table {target['table']} not found, skipped.")
|
||||
logging.debug(f"Table {table} not found, skipped.")
|
||||
except Exception as e:
|
||||
logging.error(f"Kernel Table scan failed for {target['table']}: {e}")
|
||||
logging.error(f"Kernel Table scan failed for {table}: {e}")
|
||||
|
||||
return all_ips
|
||||
|
||||
@@ -217,7 +216,7 @@ class FirewallAuditor:
|
||||
return False
|
||||
|
||||
def _flush_crowdsec_sets(self):
|
||||
for family, table in (("ip", "crowdsec"), ("ip6", "crowdsec6"), ("inet", "crowdsec")):
|
||||
for family, table in dict.fromkeys(self.targets):
|
||||
try:
|
||||
res = subprocess.run(
|
||||
["/usr/sbin/nft", "-j", "list", "table", family, table],
|
||||
@@ -237,7 +236,12 @@ class FirewallAuditor:
|
||||
continue
|
||||
set_name = nft_set.get("name")
|
||||
set_handle = nft_set.get("handle")
|
||||
if isinstance(set_name, str) and set_name.endswith("-cscli"):
|
||||
if not isinstance(set_name, str):
|
||||
continue
|
||||
if self.managed_sets:
|
||||
if set_name in self.managed_sets:
|
||||
set_targets.append((set_name, set_handle))
|
||||
elif set_name.endswith("-cscli"):
|
||||
set_targets.append((set_name, set_handle))
|
||||
|
||||
for set_name, set_handle in set_targets:
|
||||
|
||||
Reference in new issue
Block a user