Update json2apache.py

This commit is contained in:
fab 2025-02-28 11:26:45 +01:00 committed by GitHub
parent 4c01d419de
commit ed0c2e736e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -5,6 +5,7 @@ import logging
from pathlib import Path
from typing import List, Dict, Set, Tuple, Optional
from functools import lru_cache
from collections import defaultdict # Import defaultdict
# --- Configuration ---
LOG_LEVEL = logging.INFO # Adjust as needed (DEBUG, INFO, WARNING, ERROR)
@ -96,7 +97,15 @@ def generate_apache_waf(rules: List[Dict]) -> None:
if not isinstance(rule_id, int): # check if is an int
# Extract ID from rule and convert to an integer
match = re.search(r'id:(\d+)', rule_id)
rule_id = int(match.group(1)) if match else rule_id_counter
if match:
try:
rule_id = int(match.group(1))
except ValueError:
logger.warning(f"Invalid rule ID '{match.group(1)}' in rule: {rule}. Using generated ID.")
rule_id = rule_id_counter
rule_id_counter += 1
else:
rule_id = rule_id_counter
rule_id_counter += 1
category = rule.get("category", "generic").lower()