diff --git a/.gitignore b/.gitignore index 7bc5758..0800012 100644 --- a/.gitignore +++ b/.gitignore @@ -9,7 +9,7 @@ *.html *.png run.sh -rules.conf +rules.conf* Cargo.lock modsecurity cache \ No newline at end of file diff --git a/src/api/main.py b/src/api/main.py index 2f48e04..f7f0ec1 100644 --- a/src/api/main.py +++ b/src/api/main.py @@ -32,6 +32,6 @@ def main(config: dict[str, Any]): if __name__ == "__main__": - with open(Directory.CONFIG.value, "r") as config: + with open(Directory.CONFIG.value, "rb") as config: config: dict[str, Any] = tomllib.load(config) main(config) diff --git a/src/api/mapping.py b/src/api/mapping.py index 43380a2..d867c7e 100644 --- a/src/api/mapping.py +++ b/src/api/mapping.py @@ -7,6 +7,7 @@ class Directory(Enum): CONFIG = Path("api_config.toml") CRS = Path("include/coreruleset/rules") RULE = Path("rules.conf") + API_FILE = Path("api_records.json") class RuleAppendType(Enum): diff --git a/src/api/rules.py b/src/api/rules.py index 5beff7d..ed84974 100644 --- a/src/api/rules.py +++ b/src/api/rules.py @@ -54,7 +54,6 @@ def __init__(self, source: Path, destination: Path) -> None: self.source: Path = source self.destination: Path = destination self.rules = [] - self.api_file: Path = Path(__file__).parent / "api_records.json" def __rule_append(self, rule_path: Path, reason: Optional[str] = None) -> None: with open(rule_path, "r", encoding="utf-8") as fp: @@ -76,7 +75,7 @@ def __all(self, root: Path, file: Path) -> None: def __mapping(self, root: Path, file: Path) -> None: v_types: list = [] - with open(self.api_file, "r", encoding="utf-8") as report_fp: + with open(Directory.API_FILE.value, "r", encoding="utf-8") as report_fp: vulnerability_report = json.load(report_fp) for key in vulnerability_report.keys(): @@ -86,7 +85,7 @@ def __mapping(self, root: Path, file: Path) -> None: rule_path: Path = PurePath(root, file) self.__rule_append(rule_path, file) - def extract_rule(self, option: RuleAppendType = RuleAppendType.MAPPING) -> None: + def extract_rule(self, option: RuleAppendType = RuleAppendType.ALL) -> None: """ 擷取 `.conf` 規則檔案,可選擇擷取所有規則或僅擷取符合 API 記錄的規則 @@ -115,13 +114,13 @@ def rule_dump(self, overwrite: bool = False) -> None: run(["cp", self.destination, f"{self.destination}.old"]) with open(self.destination, "a", encoding="utf-8") as file: - file.writelines("\n" + {rule for rule in self.rules}) + file.writelines("\n" + rule for rule in self.rules) print("Rules updated successfully.") self.rules = [] def main(): - with open(Directory.CONFIG.value, "r") as config: + with open(Directory.CONFIG.value, "rb") as config: config: dict[str, Any] = tomllib.load(config) rule_manager: RuleUtil = RuleUtil(Directory.CRS.value, Directory.RULE.value) diff --git a/tests/dynamic_rule_test.rs b/tests/dynamic_rule_test.rs new file mode 100644 index 0000000..333c947 --- /dev/null +++ b/tests/dynamic_rule_test.rs @@ -0,0 +1,22 @@ +use std::process::Command; + + +#[test] +pub fn api_fetch() { + let mut output = Command::new("xvfb-run") + .arg("-a") + .arg("--server-args=-screen 0 1920x1080x24") + .arg("python3") + .arg("src/api/main.py") + .output() + .expect("Failed to execute xvfb-run"); + + println!("api_fetch Output: {:?}", output); + + let output = Command::new("python3") + .arg("src/api/rules.py") + .output() + .expect("Failed to update rules"); + + println!("rule_update Output: {:?}", output); +}