control_coverage/reporters/soa.py

7497935cba2e8010f0870e82b48c7afd36a22055
control-coverage/control_coverage/reporters/soa.py history · blame · raw

76 lines · 2910 bytes

2 symbols in this file
 1"""Statement of Applicability renderer.
 2
 3ISO 27001 requires a Statement of Applicability (SoA): for every Annex A control,
 4whether it applies, why, and its implementation status. This renders exactly that
 5from the coverage result — applicability comes from the scope file, and the
 6implementation status is derived from the evidence corpus rather than asserted by
 7hand, so the SoA stays honest to what the evidence actually shows.
 8"""
 9
10from __future__ import annotations
11
12from typing import TYPE_CHECKING
13
14from ..coverage import (
15    ASSERTED,
16    FAILING,
17    OUT_OF_SCOPE,
18    PARTIALLY_SUPPORTED,
19    SUPPORTED,
20    UNADDRESSED,
21)
22
23if TYPE_CHECKING:
24    from ..coverage import CoverageReport
25
26# How each assurance state reads as an implementation status in an SoA.
27_IMPL_STATUS = {
28    SUPPORTED: "Implemented — supporting evidence collected",
29    PARTIALLY_SUPPORTED: "Partly evidenced — evidence covers part of the control",
30    FAILING: "Deficient — evidence shows a non-supporting state",
31    ASSERTED: "Claimed — mapped, but evidence not yet obtained",
32    UNADDRESSED: "Not evidenced — no evidence collected yet",
33    OUT_OF_SCOPE: "Excluded",
34}
35
36
37def _justification(result) -> str:
38    if result.state == OUT_OF_SCOPE:
39        return result.exclusion_reason
40    rules = sorted({o.rule_id for o in result.observations if o.rule_id})
41    sources = sorted({o.source for o in result.observations})
42    if rules:
43        return f"Evidenced by {', '.join(rules)} in {', '.join(sources)}."
44    return "No control in the evidence corpus addresses this yet."
45
46
47def render(report: CoverageReport) -> str:
48    out: list[str] = []
49    subject = report.subject or "the organization"
50    out.append(f"# Statement of Applicability — {report.subject or 'Untitled'}")
51    out.append("")
52    out.append(f"- **Generated:** {report.generated_at}")
53    out.append(f"- **Derived from:** {report.source_count} evidence source(s)")
54    out.append("")
55    out.append(
56        f"This Statement of Applicability records, for each control in scope for "
57        f"{subject}, whether it applies and its implementation status. Applicability "
58        "decisions come from the documented scope; implementation status is derived "
59        "from collected evidence, not asserted."
60    )
61    out.append("")
62
63    for fc in report.frameworks:
64        out.append(f"## {fc.catalog.name}")
65        out.append("")
66        out.append("| Control | Description | Applicable | Status | Justification | Owner |")
67        out.append("| --- | --- | --- | --- | --- | --- |")
68        for r in fc.results:
69            applicable = "No" if r.state == OUT_OF_SCOPE else "Yes"
70            out.append(
71                f"| **{r.control.id}** | {r.control.title} | {applicable} "
72                f"| {_IMPL_STATUS[r.state]} | {_justification(r)} | {r.owner} |"
73            )
74        out.append("")
75
76    return "\n".join(out).rstrip() + "\n"