Add nft handle fallback when flushing CrowdSec sets with slash-based names
This commit is contained in:
1 parent
716468901c
commit
55cc29071e
2 files changed
+66
-6
No files matched your search
+30
-3
@@ -230,16 +230,17 @@ class FirewallAuditor:
|
||||
continue
|
||||
|
||||
data = json.loads(res.stdout or "{}")
|
||||
set_names = []
|
||||
set_targets = []
|
||||
for item in data.get("nftables", []):
|
||||
nft_set = item.get("set")
|
||||
if not isinstance(nft_set, dict):
|
||||
continue
|
||||
set_name = nft_set.get("name")
|
||||
set_handle = nft_set.get("handle")
|
||||
if isinstance(set_name, str) and set_name.endswith("-cscli"):
|
||||
set_names.append(set_name)
|
||||
set_targets.append((set_name, set_handle))
|
||||
|
||||
for set_name in set_names:
|
||||
for set_name, set_handle in set_targets:
|
||||
try:
|
||||
subprocess.run(
|
||||
["/usr/sbin/nft", "flush", "set", family, table, set_name],
|
||||
@@ -249,6 +250,32 @@ class FirewallAuditor:
|
||||
)
|
||||
except subprocess.CalledProcessError as e:
|
||||
stderr = (e.stderr or "").strip()
|
||||
syntax_error = "syntax error" in stderr.lower()
|
||||
if syntax_error and isinstance(set_handle, int):
|
||||
try:
|
||||
subprocess.run(
|
||||
[
|
||||
"/usr/sbin/nft",
|
||||
"flush",
|
||||
"set",
|
||||
family,
|
||||
table,
|
||||
"handle",
|
||||
str(set_handle),
|
||||
],
|
||||
check=True,
|
||||
capture_output=True,
|
||||
text=True,
|
||||
)
|
||||
continue
|
||||
except subprocess.CalledProcessError as handle_err:
|
||||
handle_stderr = (handle_err.stderr or "").strip()
|
||||
logging.warning(
|
||||
"Heal: Failed to flush set "
|
||||
f"({family} {table} {set_name}, handle={set_handle}): "
|
||||
f"{handle_stderr or handle_err}"
|
||||
)
|
||||
continue
|
||||
logging.warning(
|
||||
"Heal: Failed to flush set "
|
||||
f"({family} {table} {set_name}): {stderr or e}"
|
||||
|
||||
Reference in new issue
Block a user