|
| 1 | +"""Tests for pyaegis.rule_plugins — community rule pack manager.""" |
| 2 | +from __future__ import annotations |
| 3 | + |
| 4 | +from pathlib import Path |
| 5 | + |
| 6 | +import pytest |
| 7 | +import yaml |
| 8 | + |
| 9 | +from pyaegis.rule_plugins import RulePluginManager |
| 10 | + |
| 11 | + |
| 12 | +# --------------------------------------------------------------------------- |
| 13 | +# Fixtures |
| 14 | +# --------------------------------------------------------------------------- |
| 15 | + |
| 16 | + |
| 17 | +@pytest.fixture |
| 18 | +def mgr(tmp_path): |
| 19 | + return RulePluginManager(rules_dir=str(tmp_path / "rules")) |
| 20 | + |
| 21 | + |
| 22 | +@pytest.fixture |
| 23 | +def sample_pack(tmp_path): |
| 24 | + """Write a valid YAML rule pack to disk and return its path.""" |
| 25 | + data = { |
| 26 | + "inputs": ["environ.get", "getenv"], |
| 27 | + "sinks": ["ldap.search"], |
| 28 | + "sanitizers": ["escape"], |
| 29 | + "conditional_sinks": [], |
| 30 | + "source_decorators": [], |
| 31 | + } |
| 32 | + p = tmp_path / "sample_pack.yml" |
| 33 | + p.write_text(yaml.safe_dump(data), encoding="utf-8") |
| 34 | + return str(p) |
| 35 | + |
| 36 | + |
| 37 | +# --------------------------------------------------------------------------- |
| 38 | +# Install |
| 39 | +# --------------------------------------------------------------------------- |
| 40 | + |
| 41 | + |
| 42 | +def test_install_local(mgr, sample_pack): |
| 43 | + name = mgr.install(sample_pack, name="test-pack") |
| 44 | + assert name == "test-pack" |
| 45 | + installed = mgr.list_installed() |
| 46 | + assert len(installed) == 1 |
| 47 | + assert installed[0]["name"] == "test-pack" |
| 48 | + |
| 49 | + |
| 50 | +def test_install_auto_name(mgr, sample_pack): |
| 51 | + name = mgr.install(sample_pack) # name inferred from filename |
| 52 | + assert name == "sample_pack" |
| 53 | + |
| 54 | + |
| 55 | +def test_install_duplicate_raises(mgr, sample_pack): |
| 56 | + mgr.install(sample_pack, name="dup") |
| 57 | + with pytest.raises(ValueError, match="already installed"): |
| 58 | + mgr.install(sample_pack, name="dup") |
| 59 | + |
| 60 | + |
| 61 | +def test_install_duplicate_force(mgr, sample_pack): |
| 62 | + mgr.install(sample_pack, name="dup") |
| 63 | + name = mgr.install(sample_pack, name="dup", force=True) |
| 64 | + assert name == "dup" |
| 65 | + assert len(mgr.list_installed()) == 1 |
| 66 | + |
| 67 | + |
| 68 | +def test_install_invalid_yaml(mgr, tmp_path): |
| 69 | + bad = tmp_path / "bad.yml" |
| 70 | + bad.write_text("[unclosed bracket\n", encoding="utf-8") |
| 71 | + with pytest.raises(ValueError, match="Invalid YAML"): |
| 72 | + mgr.install(str(bad), name="bad") |
| 73 | + |
| 74 | + |
| 75 | +def test_install_non_mapping_yaml(mgr, tmp_path): |
| 76 | + p = tmp_path / "list.yml" |
| 77 | + p.write_text("- item1\n- item2\n", encoding="utf-8") |
| 78 | + with pytest.raises(ValueError, match="must be a YAML mapping"): |
| 79 | + mgr.install(str(p), name="list") |
| 80 | + |
| 81 | + |
| 82 | +# --------------------------------------------------------------------------- |
| 83 | +# Remove |
| 84 | +# --------------------------------------------------------------------------- |
| 85 | + |
| 86 | + |
| 87 | +def test_remove_installed(mgr, sample_pack): |
| 88 | + mgr.install(sample_pack, name="to-remove") |
| 89 | + result = mgr.remove("to-remove") |
| 90 | + assert result is True |
| 91 | + assert mgr.list_installed() == [] |
| 92 | + |
| 93 | + |
| 94 | +def test_remove_nonexistent(mgr): |
| 95 | + result = mgr.remove("ghost") |
| 96 | + assert result is False |
| 97 | + |
| 98 | + |
| 99 | +# --------------------------------------------------------------------------- |
| 100 | +# List |
| 101 | +# --------------------------------------------------------------------------- |
| 102 | + |
| 103 | + |
| 104 | +def test_list_empty(mgr): |
| 105 | + assert mgr.list_installed() == [] |
| 106 | + |
| 107 | + |
| 108 | +def test_list_multiple(mgr, tmp_path, sample_pack): |
| 109 | + pack2 = tmp_path / "pack2.yml" |
| 110 | + pack2.write_text(yaml.safe_dump({"sinks": ["eval"]}), encoding="utf-8") |
| 111 | + mgr.install(sample_pack, name="p1") |
| 112 | + mgr.install(str(pack2), name="p2") |
| 113 | + names = {p["name"] for p in mgr.list_installed()} |
| 114 | + assert names == {"p1", "p2"} |
| 115 | + |
| 116 | + |
| 117 | +# --------------------------------------------------------------------------- |
| 118 | +# Merge |
| 119 | +# --------------------------------------------------------------------------- |
| 120 | + |
| 121 | + |
| 122 | +def test_merged_rules_includes_plugin(mgr, sample_pack): |
| 123 | + mgr.install(sample_pack, name="extra") |
| 124 | + merged = mgr.merged_rules(include_builtin=False) |
| 125 | + assert "environ.get" in merged["inputs"] |
| 126 | + assert "ldap.search" in merged["sinks"] |
| 127 | + |
| 128 | + |
| 129 | +def test_merged_rules_deduplicates(mgr, tmp_path): |
| 130 | + p1 = tmp_path / "p1.yml" |
| 131 | + p2 = tmp_path / "p2.yml" |
| 132 | + p1.write_text(yaml.safe_dump({"inputs": ["os.environ"]}), encoding="utf-8") |
| 133 | + p2.write_text( |
| 134 | + yaml.safe_dump({"inputs": ["os.environ", "getenv"]}), encoding="utf-8" |
| 135 | + ) |
| 136 | + mgr.install(str(p1), name="p1") |
| 137 | + mgr.install(str(p2), name="p2") |
| 138 | + merged = mgr.merged_rules(include_builtin=False) |
| 139 | + assert merged["inputs"].count("os.environ") == 1 |
| 140 | + |
| 141 | + |
| 142 | +def test_merged_rules_path_creates_file(mgr, sample_pack): |
| 143 | + mgr.install(sample_pack, name="mp") |
| 144 | + path = mgr.merged_rules_path(include_builtin=False) |
| 145 | + assert Path(path).exists() |
| 146 | + content = yaml.safe_load(Path(path).read_text(encoding="utf-8")) |
| 147 | + assert "sinks" in content |
| 148 | + Path(path).unlink() # cleanup |
| 149 | + |
| 150 | + |
| 151 | +# --------------------------------------------------------------------------- |
| 152 | +# has_plugins |
| 153 | +# --------------------------------------------------------------------------- |
| 154 | + |
| 155 | + |
| 156 | +def test_has_plugins_false(mgr): |
| 157 | + assert mgr.has_plugins() is False |
| 158 | + |
| 159 | + |
| 160 | +def test_has_plugins_true(mgr, sample_pack): |
| 161 | + mgr.install(sample_pack, name="any") |
| 162 | + assert mgr.has_plugins() is True |
| 163 | + |
| 164 | + |
| 165 | +# --------------------------------------------------------------------------- |
| 166 | +# Index persistence |
| 167 | +# --------------------------------------------------------------------------- |
| 168 | + |
| 169 | + |
| 170 | +def test_index_persists_across_instances(tmp_path, sample_pack): |
| 171 | + rules_dir = str(tmp_path / "rules") |
| 172 | + mgr1 = RulePluginManager(rules_dir=rules_dir) |
| 173 | + mgr1.install(sample_pack, name="persist") |
| 174 | + |
| 175 | + mgr2 = RulePluginManager(rules_dir=rules_dir) |
| 176 | + names = {p["name"] for p in mgr2.list_installed()} |
| 177 | + assert "persist" in names |
0 commit comments