Typed control mappings in rules and JSON findings !5
5 files changed, +86 −1
Layout: unified · split
CHANGELOG.md +1
| @@ -8,6 +8,7 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). | ||
| 8 | 8 | |
| 9 | 9 | ### Added |
| 10 | 10 | |
| 11 | - Typed control mappings: a rule's `controls` entries may be `{control, relationship (full|partial), rationale, source}`; JSON findings carry `control_mappings` when present. | |
| 11 | 12 | - Ruleset `schema: 2`: rules may declare a `timestamp` column and `max_gap_days`; `audit_report.periods.check` reports rows outside a test period, undated rows, and gaps. Schema 1 rulesets are unchanged. |
| 12 | 13 | |
| 13 | 14 | ### Fixed |
README.md +6
| @@ -149,6 +149,12 @@ rules: | ||
| 149 | 149 | | `require_any_row` | Passes only if at least one row satisfies `when`. | |
| 150 | 150 | | `assert_row` | Checks each condition in `require` against the first row of a single-row config table. | |
| 151 | 151 | |
| 152 | **Typed control mappings** — an entry in `controls` may be a mapping instead | |
| 153 | of a code: `{control: ISO:A.5.17, relationship: partial, rationale: "covers MFA | |
| 154 | only", source: "firm methodology 2026"}`. `relationship` is `full` (default) or | |
| 155 | `partial`. JSON reports then carry `control_mappings` per finding, which | |
| 156 | control-coverage uses to report `partially_supported` controls. | |
| 157 | ||
| 152 | 158 | **Period checks (schema 2)** — a ruleset with `schema: 2` may give a rule a |
| 153 | 159 | `timestamp` column (ISO date or date-time) and an optional `max_gap_days`. |
| 154 | 160 | `audit_report.periods.check(rows, column, start, end, max_gap_days)` reports |
audit_report/reporters/json.py +2
| @@ -32,6 +32,8 @@ def to_dict(report: Report) -> dict: | ||
| 32 | 32 | "status": f.status, |
| 33 | 33 | "severity": f.rule.severity, |
| 34 | 34 | "controls": f.rule.controls, |
| 35 | # Added only for typed mappings, so v1 consumers see the same keys as before. | |
| 36 | **({"control_mappings": f.rule.control_mappings} if f.rule.control_mappings else {}), | |
| 35 | 37 | "reason": f.reason, |
| 36 | 38 | "evidence": f.evidence, |
| 37 | 39 | } |
audit_report/rules.py +30 −1
| @@ -26,6 +26,32 @@ import yaml | ||
| 26 | 26 | _TRUTHY = {"true", "yes", "1", "y", "t", "enabled", "on"} |
| 27 | 27 | _FALSY = {"false", "no", "0", "n", "f", "disabled", "off", ""} |
| 28 | 28 | |
| 29 | RELATIONSHIPS = {"full", "partial"} | |
| 30 | ||
| 31 | ||
| 32 | def _controls(entry: dict, path) -> tuple[list[str], dict[str, dict]]: | |
| 33 | """A rule's `controls`: plain codes, or `{control, relationship, rationale, source}` mappings.""" | |
| 34 | codes: list[str] = [] | |
| 35 | mappings: dict[str, dict] = {} | |
| 36 | for item in entry.get("controls", []) or []: | |
| 37 | if isinstance(item, str): | |
| 38 | codes.append(item) | |
| 39 | continue | |
| 40 | if not isinstance(item, dict) or not item.get("control"): | |
| 41 | raise ValueError(f"{path}: rule {entry.get('id')}: a control mapping needs 'control'") | |
| 42 | relationship = str(item.get("relationship", "full")) | |
| 43 | if relationship not in RELATIONSHIPS: | |
| 44 | raise ValueError(f"{path}: rule {entry.get('id')}: relationship must be full or partial") | |
| 45 | code = str(item["control"]) | |
| 46 | codes.append(code) | |
| 47 | mappings[code] = { | |
| 48 | "relationship": relationship, | |
| 49 | "rationale": str(item.get("rationale", "")), | |
| 50 | "source": str(item.get("source", "")), | |
| 51 | } | |
| 52 | return codes, mappings | |
| 53 | ||
| 54 | ||
| 29 | 55 | VALID_CHECK_TYPES = { |
| 30 | 56 | "fail_rows_where", |
| 31 | 57 | "fail_if_any_rows", |
| @@ -46,6 +72,7 @@ class Rule: | ||
| 46 | 72 | controls: list[str] = field(default_factory=list) |
| 47 | 73 | rationale: str = "" |
| 48 | 74 | remediation: str = "" |
| 75 | control_mappings: dict[str, dict] = field(default_factory=dict) # code -> relationship, rationale, source | |
| 49 | 76 | timestamp: str | None = None # schema 2: the column that dates each row |
| 50 | 77 | max_gap_days: int | None = None # schema 2: longest allowed run of days with no rows |
| 51 | 78 | |
| @@ -148,6 +175,7 @@ def load_ruleset(path: str | Path) -> Ruleset: | ||
| 148 | 175 | for entry in data.get("rules", []): |
| 149 | 176 | if schema == 1 and ("timestamp" in entry or "max_gap_days" in entry): |
| 150 | 177 | raise ValueError(f"{path}: rule {entry.get('id')}: 'timestamp' and 'max_gap_days' need 'schema: 2'") |
| 178 | codes, mappings = _controls(entry, path) | |
| 151 | 179 | gap = entry.get("max_gap_days") |
| 152 | 180 | if gap is not None and (not isinstance(gap, int) or gap < 1 or "timestamp" not in entry): |
| 153 | 181 | raise ValueError(f"{path}: rule {entry.get('id')}: max_gap_days must be a positive integer with a timestamp") |
| @@ -157,7 +185,8 @@ def load_ruleset(path: str | Path) -> Ruleset: | ||
| 157 | 185 | table=entry["table"], |
| 158 | 186 | check=entry["check"], |
| 159 | 187 | severity=entry.get("severity", "medium"), |
| 160 | controls=entry.get("controls", []), | |
| 188 | controls=codes, | |
| 189 | control_mappings=mappings, | |
| 161 | 190 | rationale=entry.get("rationale", ""), |
| 162 | 191 | remediation=entry.get("remediation", ""), |
| 163 | 192 | timestamp=entry.get("timestamp"), |
tests/test_typed_mappings.py added +47
| @@ -0,0 +1,47 @@ | ||
| 1 | """Typed control mappings in rules and JSON reports.""" | |
| 2 | ||
| 3 | import json | |
| 4 | from pathlib import Path | |
| 5 | ||
| 6 | import pytest | |
| 7 | ||
| 8 | from audit_report import engine, loader, reporters | |
| 9 | from audit_report.rules import load_ruleset | |
| 10 | ||
| 11 | RULESET = """ | |
| 12 | platform: aws | |
| 13 | rules: | |
| 14 | - id: aws.iam.mfa | |
| 15 | title: MFA | |
| 16 | table: iam_users | |
| 17 | controls: | |
| 18 | - SOC2:CC6.1 | |
| 19 | - {control: ISO:A.5.17, relationship: partial, rationale: covers MFA only, source: firm 2026} | |
| 20 | check: {type: fail_rows_where, when: {column: mfa_enabled, op: is_false}} | |
| 21 | """ | |
| 22 | ||
| 23 | ||
| 24 | def test_mappings_parse_and_reach_json(tmp_path: Path): | |
| 25 | rs_path = tmp_path / "aws.yaml" | |
| 26 | rs_path.write_text(RULESET) | |
| 27 | rs = load_ruleset(rs_path) | |
| 28 | assert rs.rules[0].controls == ["SOC2:CC6.1", "ISO:A.5.17"] | |
| 29 | assert rs.rules[0].control_mappings == {"ISO:A.5.17": {"relationship": "partial", "rationale": "covers MFA only", "source": "firm 2026"}} | |
| 30 | pkg_dir = tmp_path / "aws_audit_prod_2026-07-01" | |
| 31 | pkg_dir.mkdir() | |
| 32 | (pkg_dir / "iam_users.csv").write_text("user,mfa_enabled\na,True\n") | |
| 33 | pkg = loader.load_package(pkg_dir) | |
| 34 | report = reporters.build_report(pkg, engine.evaluate(pkg, rs), rs) | |
| 35 | finding = json.loads(reporters.render(report, "json"))["findings"][0] | |
| 36 | assert finding["controls"] == ["SOC2:CC6.1", "ISO:A.5.17"] | |
| 37 | assert finding["control_mappings"]["ISO:A.5.17"]["relationship"] == "partial" | |
| 38 | ||
| 39 | ||
| 40 | def test_bad_mappings_are_refused(tmp_path: Path): | |
| 41 | path = tmp_path / "r.yaml" | |
| 42 | path.write_text(RULESET.replace("relationship: partial", "relationship: intersects")) | |
| 43 | with pytest.raises(ValueError, match="full or partial"): | |
| 44 | load_ruleset(path) | |
| 45 | path.write_text(RULESET.replace("{control: ISO:A.5.17,", "{")) | |
| 46 | with pytest.raises(ValueError, match="needs 'control'"): | |
| 47 | load_ruleset(path) | |