Commit 201d4f7cf4
Unsigned
Layout: unified · split
CHANGELOG.md +1
| @@ -8,6 +8,7 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). | |||
| 8 | 8 | ||
| 9 | ### Added | 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 | - 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 | - 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 | ### Fixed | 14 | ### Fixed |
README.md +6
| @@ -149,6 +149,12 @@ rules: | |||
| 149 | | `require_any_row` | Passes only if at least one row satisfies `when`. | | 149 | | `require_any_row` | Passes only if at least one row satisfies `when`. | |
| 150 | | `assert_row` | Checks each condition in `require` against the first row of a single-row config table. | | 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 | **Period checks (schema 2)** — a ruleset with `schema: 2` may give a rule a | 158 | **Period checks (schema 2)** — a ruleset with `schema: 2` may give a rule a |
| 153 | `timestamp` column (ISO date or date-time) and an optional `max_gap_days`. | 159 | `timestamp` column (ISO date or date-time) and an optional `max_gap_days`. |
| 154 | `audit_report.periods.check(rows, column, start, end, max_gap_days)` reports | 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 | "status": f.status, | 32 | "status": f.status, |
| 33 | "severity": f.rule.severity, | 33 | "severity": f.rule.severity, |
| 34 | "controls": f.rule.controls, | 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 | "reason": f.reason, | 37 | "reason": f.reason, |
| 36 | "evidence": f.evidence, | 38 | "evidence": f.evidence, |
| 37 | } | 39 | } |
audit_report/rules.py +30 −1
| @@ -26,6 +26,32 @@ import yaml | |||
| 26 | _TRUTHY = {"true", "yes", "1", "y", "t", "enabled", "on"} | 26 | _TRUTHY = {"true", "yes", "1", "y", "t", "enabled", "on"} |
| 27 | _FALSY = {"false", "no", "0", "n", "f", "disabled", "off", ""} | 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 | VALID_CHECK_TYPES = { | 55 | VALID_CHECK_TYPES = { |
| 30 | "fail_rows_where", | 56 | "fail_rows_where", |
| 31 | "fail_if_any_rows", | 57 | "fail_if_any_rows", |
| @@ -46,6 +72,7 @@ class Rule: | |||
| 46 | controls: list[str] = field(default_factory=list) | 72 | controls: list[str] = field(default_factory=list) |
| 47 | rationale: str = "" | 73 | rationale: str = "" |
| 48 | remediation: str = "" | 74 | remediation: str = "" |
| 75 | control_mappings: dict[str, dict] = field(default_factory=dict) # code -> relationship, rationale, source | ||
| 49 | timestamp: str | None = None # schema 2: the column that dates each row | 76 | timestamp: str | None = None # schema 2: the column that dates each row |
| 50 | max_gap_days: int | None = None # schema 2: longest allowed run of days with no rows | 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 | for entry in data.get("rules", []): | 175 | for entry in data.get("rules", []): |
| 149 | if schema == 1 and ("timestamp" in entry or "max_gap_days" in entry): | 176 | if schema == 1 and ("timestamp" in entry or "max_gap_days" in entry): |
| 150 | raise ValueError(f"{path}: rule {entry.get('id')}: 'timestamp' and 'max_gap_days' need 'schema: 2'") | 177 | raise ValueError(f"{path}: rule {entry.get('id')}: 'timestamp' and 'max_gap_days' need 'schema: 2'") |
| 178 | codes, mappings = _controls(entry, path) | ||
| 151 | gap = entry.get("max_gap_days") | 179 | gap = entry.get("max_gap_days") |
| 152 | if gap is not None and (not isinstance(gap, int) or gap < 1 or "timestamp" not in entry): | 180 | if gap is not None and (not isinstance(gap, int) or gap < 1 or "timestamp" not in entry): |
| 153 | raise ValueError(f"{path}: rule {entry.get('id')}: max_gap_days must be a positive integer with a timestamp") | 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 | table=entry["table"], | 185 | table=entry["table"], |
| 158 | check=entry["check"], | 186 | check=entry["check"], |
| 159 | severity=entry.get("severity", "medium"), | 187 | severity=entry.get("severity", "medium"), |
| 160 | controls=entry.get("controls", []), | 188 | controls=codes, |
| 189 | control_mappings=mappings, | ||
| 161 | rationale=entry.get("rationale", ""), | 190 | rationale=entry.get("rationale", ""), |
| 162 | remediation=entry.get("remediation", ""), | 191 | remediation=entry.get("remediation", ""), |
| 163 | timestamp=entry.get("timestamp"), | 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) | ||