Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*.html
*.png
run.sh
rules.conf
rules.conf*
Cargo.lock
modsecurity
cache
2 changes: 1 addition & 1 deletion src/api/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
1 change: 1 addition & 0 deletions src/api/mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
9 changes: 4 additions & 5 deletions src/api/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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():
Expand All @@ -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 記錄的規則

Expand Down Expand Up @@ -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)
Expand Down
22 changes: 22 additions & 0 deletions tests/dynamic_rule_test.rs
Original file line number Diff line number Diff line change
@@ -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);
}