control_coverage/reporters/markdown.py
161 lines · 5381 bytes
1"""Markdown renderer — the coverage matrix and blind-spot list, auditor-facing."""
2
3from __future__ import annotations
4
5from typing import TYPE_CHECKING
6
7from .. import __version__
8from ..coverage import (
9 ASSERTED,
10 FAILING,
11 OUT_OF_SCOPE,
12 PARTIALLY_SUPPORTED,
13 STATE_ORDER,
14 SUPPORTED,
15 UNADDRESSED,
16)
17
18if TYPE_CHECKING:
19 from ..coverage import CoverageReport, FrameworkCoverage
20
21_STATE_LABEL = {
22 SUPPORTED: "supported",
23 PARTIALLY_SUPPORTED: "partially supported",
24 FAILING: "failing",
25 ASSERTED: "asserted",
26 UNADDRESSED: "unaddressed",
27 OUT_OF_SCOPE: "out of scope",
28}
29_STATE_MARK = {
30 SUPPORTED: "✓",
31 PARTIALLY_SUPPORTED: "◑",
32 FAILING: "✗",
33 ASSERTED: "◐",
34 UNADDRESSED: "○",
35 OUT_OF_SCOPE: "—",
36}
37
38
39def _evidence_note(result) -> str:
40 """A short 'checked by' cell: rule ids or the exclusion reason."""
41 if result.state == OUT_OF_SCOPE:
42 return f"_excluded: {result.exclusion_reason}_"
43 if not result.observations:
44 return ""
45 rules = sorted({o.rule_id for o in result.observations if o.rule_id})
46 return ", ".join(f"`{r}`" for r in rules)
47
48
49def _framework_section(fc: FrameworkCoverage) -> list[str]:
50 cat = fc.catalog
51 counts = fc.counts
52 out: list[str] = []
53 out.append(f"## {cat.name}")
54 out.append("")
55 suffix = "" if cat.complete else " _(partial catalog — coverage is of the shipped subset)_"
56 out.append(
57 f"**Coverage {fc.coverage_pct}%** ({fc.addressed}/{fc.in_scope} in-scope "
58 f"controls addressed) · **assured {fc.assured_pct}%** "
59 f"({fc.supported} supported){suffix}"
60 )
61 out.append("")
62 out.append(
63 "| " + " · ".join(
64 f"{_STATE_MARK[s]} {counts[s]} {_STATE_LABEL[s]}"
65 for s in STATE_ORDER
66 if counts[s]
67 ) + " |"
68 )
69 out.append("|" + "---|")
70 out.append("")
71 out.append("| Control | Status | Description | Checked by |")
72 out.append("| --- | --- | --- | --- |")
73 for r in fc.results:
74 mark = _STATE_MARK[r.state]
75 label = _STATE_LABEL[r.state]
76 out.append(
77 f"| **{r.control.id}** | {mark} {label} | {r.control.title} | {_evidence_note(r)} |"
78 )
79 out.append("")
80 return out
81
82
83def _blind_spots(report: CoverageReport) -> list[str]:
84 out: list[str] = ["## Blind spots", ""]
85 total = sum(len(fc.blind_spots) for fc in report.frameworks)
86 if total == 0:
87 out.append("_No in-scope control is left unaddressed by the corpus._")
88 out.append("")
89 return out
90 out.append(
91 f"{total} in-scope control(s) are **unaddressed** — no finding in the corpus "
92 "maps to them. These are the framework requirements the evidence does not "
93 "look at yet."
94 )
95 out.append("")
96 for fc in report.frameworks:
97 spots = fc.blind_spots
98 if not spots:
99 continue
100 out.append(f"### {fc.catalog.name} ({len(spots)})")
101 out.append("")
102 for r in spots:
103 fam = f" · _{r.control.family}_" if r.control.family else ""
104 out.append(f"- **{r.control.id}** — {r.control.title}{fam}")
105 out.append("")
106 return out
107
108
109def render(report: CoverageReport) -> str:
110 out: list[str] = []
111 title = report.subject or "Evidence corpus"
112 out.append(f"# Control Coverage — {title}")
113 out.append("")
114 out.append(f"- **Generated:** {report.generated_at}")
115 out.append(f"- **Tool:** control-coverage {__version__}")
116 out.append(f"- **Corpus:** {report.source_count} evidence source(s)")
117 frameworks = ", ".join(
118 f"{fc.catalog.framework} {fc.catalog.version} (`sha256:{fc.catalog.sha256[:12]}`)"
119 for fc in report.frameworks
120 )
121 out.append(f"- **Frameworks:** {frameworks}")
122 out.append("")
123 out.append(
124 "> Coverage measures how much of a framework the evidence corpus addresses — "
125 "not whether the organization is compliant. An unaddressed control is a gap "
126 "in *evidence*, which may reflect a real gap in *controls* or simply a signal "
127 "not yet collected. The final judgment belongs to the organization and its auditor."
128 )
129 out.append("")
130
131 # Headline table across frameworks.
132 out.append("## Summary")
133 out.append("")
134 out.append("| Framework | In scope | Addressed | Supported | Failing | Blind spots | Coverage | Assured |")
135 out.append("| --- | ---: | ---: | ---: | ---: | ---: | ---: | ---: |")
136 for fc in report.frameworks:
137 c = fc.counts
138 out.append(
139 f"| {fc.catalog.name} | {fc.in_scope} | {fc.addressed} | {fc.supported} "
140 f"| {c[FAILING]} | {len(fc.blind_spots)} | {fc.coverage_pct}% | {fc.assured_pct}% |"
141 )
142 out.append("")
143
144 out.extend(_blind_spots(report))
145
146 for fc in report.frameworks:
147 out.extend(_framework_section(fc))
148
149 if report.orphan_codes:
150 out.append("## Unmatched control codes")
151 out.append("")
152 out.append(
153 "The corpus cites these control codes, but no loaded catalog defines them. "
154 "They are typos, renamed controls, or controls outside the bundled catalogs:"
155 )
156 out.append("")
157 for code in report.orphan_codes:
158 out.append(f"- `{code}`")
159 out.append("")
160
161 return "\n".join(out).rstrip() + "\n"