Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion docs/developer-guide/mapping-engine-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,9 @@ value of the wrong kind; a field mapping takes the keys of its `structure` (`sim
(`transform: none`; a composite's `separator`, a blank line; an ident's
`value_template: "{item}"`; `cci_lookup.extract: all`). The engine reads the model this last
check built (`ConfigLoader.load(path).style`): `map_rule` and every field-mapping builder take
the mapping's model, not its YAML.
the mapping's model, not its YAML, and the notice, matter, reference, plain text, Benchmark
elements, Benchmark id and profiles are built from the model's `benchmark` section. A notice
with no `id` (or an empty one) is written with the id `terms-of-use`.
Every mistake is listed with the dotted path of the key and the file just
merged when it appeared, so a parent's mistake names the parent and one namespace URI bound
in parent and child names the child. Loading stops at the first file with mistakes; every
Expand Down
116 changes: 50 additions & 66 deletions src/cis_bench/exporters/mapping_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@
MetadataElement,
MetadataFromConfigMapping,
NestedMapping,
ProfileMapping,
Profiles,
SimpleMapping,
StyleConfig,
StyleModels,
Expand Down Expand Up @@ -102,7 +104,7 @@ class MappingConfig:
@property
def namespaces(self) -> dict[str, str]:
"""Prefix -> namespace URI, declared in benchmark.namespaces."""
return dict(self.benchmark.get("namespaces") or {})
return dict(self.style.benchmark.namespaces)


class TransformRegistry:
Expand Down Expand Up @@ -783,28 +785,27 @@ def create_notice(self, benchmark: Benchmark):
(lang, written as xml:lang) and its variable-substituted text from the style's
benchmark.notice, keeping the content empty when it is left out or blank.
"""
raw_notice = self.config.benchmark.get("notice")
notice_config = raw_notice if isinstance(raw_notice, dict) else {}
notice_config = self.config.style.benchmark.notice

NoticeType = self.element_type("Benchmark", "notice")

substitution_context = {"benchmark": benchmark}

raw_id = notice_config.get("id") or "terms-of-use"
notice_id = str(VariableSubstituter.substitute(str(raw_id), substitution_context))
# XCCDF requires a notice id; "terms-of-use" is the engine's own name when the style
# gives none or an empty one
raw_id = notice_config.id or "terms-of-use"
notice_id = str(VariableSubstituter.substitute(raw_id, substitution_context))

raw_lang = notice_config.get("lang")
notice_lang = (
str(VariableSubstituter.substitute(str(raw_lang), substitution_context))
if raw_lang
str(VariableSubstituter.substitute(notice_config.lang, substitution_context))
if notice_config.lang
else None
)

raw_content = notice_config.get("content")
content: list[object] = []
if isinstance(raw_content, str) and raw_content.strip():
if notice_config.content and notice_config.content.strip():
resolved = str(
VariableSubstituter.substitute(raw_content, substitution_context)
VariableSubstituter.substitute(notice_config.content, substitution_context)
).strip()
if resolved:
content = [resolved]
Expand Down Expand Up @@ -845,7 +846,8 @@ def dublin_core_elements(
elements = []
for dc in dc_elements_config:
prefix, _, local_name = dc.element.partition(":")
namespace = self.config.namespaces.get(prefix)
namespaces = self.config.style.benchmark.namespaces
namespace = namespaces[prefix] if prefix in namespaces else None
if not namespace:
raise ValueError(
f"dc_elements names {dc.element!r} but benchmark.namespaces declares "
Expand All @@ -860,24 +862,22 @@ def create_reference(self, benchmark: Benchmark) -> tuple[str, list[AnyElement]]
benchmark.reference.attributes.href (the benchmark's URL when the style names none)
and the Dublin Core elements of benchmark.reference.dc_elements (none when the style
declares none)."""
reference_config = self.config.benchmark.get("reference", {})
href_template = (reference_config.get("attributes") or {}).get("href")
reference_config = self.config.style.benchmark.reference
href_template = reference_config.attributes.href
values = {"benchmark": benchmark}
href = (
str(VariableSubstituter.substitute(href_template, values))
if href_template
else str(benchmark.url)
)
return href, self.dublin_core_elements(
self.config.style.benchmark.reference.dc_elements, values
)
return href, self.dublin_core_elements(reference_config.dc_elements, values)

def create_plain_texts(self, benchmark: Benchmark):
"""Create plain-text elements from config (version-agnostic).

Per DISA conventions: release-info, generator, conventionsVersion
"""
plain_text_configs = self.config.benchmark.get("plain_text", [])
plain_text_configs = self.config.style.benchmark.plain_text
PlainTextType = self.get_xccdf_class("PlainTextType")
plain_texts = []

Expand All @@ -890,8 +890,8 @@ def create_plain_texts(self, benchmark: Benchmark):
)

for pt_config in plain_text_configs:
pt_id = pt_config.get("id")
content_template = pt_config.get("content", "")
pt_id = pt_config.id
content_template = pt_config.content

# Substitute variables
content = VariableSubstituter.substitute(
Expand Down Expand Up @@ -1302,7 +1302,7 @@ def map_benchmark(self, benchmark: Benchmark, groups: list, context: dict):
# that come from free text are reduced to NCName-safe segments so the
# result satisfies XCCDF's benchmarkIdType whatever the title or
# version contains.
bench_id_template = self.config.benchmark.get("id_template")
bench_id_template = self.config.style.benchmark.id_template
if not bench_id_template:
raise ExportError(
f"Style '{self.config.metadata.get('style_name', '?')}' has no "
Expand All @@ -1324,28 +1324,21 @@ def map_benchmark(self, benchmark: Benchmark, groups: list, context: dict):
"group": groups, # Add all Groups
}

# Loop through benchmark config elements
for element_name, element_config in self.config.benchmark.items():
if not isinstance(element_config, dict):
continue

# Skip special elements (handled by dedicated methods)
if element_name in ["id_template", "namespaces"]:
continue

# Elements read from a benchmark field; the others (notice, front_matter,
# profiles, reference) are built by their own methods below
source = element_config.get("source")
# The Benchmark elements read from a benchmark field (every entry of the benchmark
# section but its named keys); notice, matter, reference, plain text and profiles
# are built by their own methods below
for element_name, element_config in (
self.config.style.benchmark.__pydantic_extra__ or {}
).items():
source = element_config.source
if source:
ElementType = self.element_type("Benchmark", element_name)
value = getattr(benchmark, source, None)

# Apply transform
transform = element_config.get("transform", "none")
transformed = TransformRegistry.apply(transform, value)
transformed = TransformRegistry.apply(element_config.transform, value)

# Apply prepend text if specified
prepend = element_config.get("prepend_text")
prepend = element_config.prepend_text
if prepend and transformed:
transformed = prepend + transformed

Expand All @@ -1372,8 +1365,8 @@ def map_benchmark(self, benchmark: Benchmark, groups: list, context: dict):
benchmark_fields["reference"] = [ReferenceType(href=ref_href, content=ref_content)]

# Generate Profile elements if configured
profile_config = self.config.benchmark.get("profiles")
if profile_config:
profile_config = self.config.style.benchmark.profiles
if profile_config.generate_from_rules:
# Need to pass all recommendations to build profile select lists
# Get recommendations from context (passed by exporter)
recommendations = context.get("recommendations", [])
Expand Down Expand Up @@ -1542,7 +1535,7 @@ def _get_nested_field(self, obj: Any, field_path: str) -> Any:
def generate_profiles_from_rules(
self,
recommendations: list["Recommendation"],
profile_config: dict,
profile_config: Profiles,
profile_definitions: list[ProfileDefinition] | None = None,
) -> list:
"""XCCDF Profile elements at Benchmark level, each with a select for every rule in it.
Expand All @@ -1565,32 +1558,27 @@ def generate_profiles_from_rules(
includes: ["Level 1 - Server"]

Raises:
ValueError: No id_template, a mapping without a match or with its own id, two
mappings for one name, an include or parent that names no profile, profiles
that include each other, or two profiles that get one id.
ValueError: No id_template, a mapping with an empty match, two mappings for one
name, an include or parent that names no profile, profiles that include each
other, or two profiles that get one id.
"""
Profile = self.get_xccdf_class("Profile")
ProfileSelectType = self.get_xccdf_class("ProfileSelectType")
TextWithSubType = self.get_xccdf_class("TextWithSubType")
HtmlTextWithSubType = self.get_xccdf_class("HtmlTextWithSubType")

if not profile_config.get("generate_from_rules"):
if not profile_config.generate_from_rules:
return []

mappings = profile_config.get("profile_mappings", [])
id_template = profile_config.get("id_template")
mappings = profile_config.profile_mappings
id_template = profile_config.id_template
if not id_template:
raise ValueError("benchmark.profiles needs an id_template (a Profile id per title)")
by_match: dict[str, dict] = {}
by_match: dict[str, ProfileMapping] = {}
for mapping in mappings:
name = mapping.get("match")
if not isinstance(name, str) or not name:
name = mapping.match
if not name:
raise ValueError(f"a profile mapping needs a match: {mapping!r}")
if "id" in mapping:
raise ValueError(
f"profile mapping {name!r} sets an id; profile ids come from "
"benchmark.profiles.id_template"
)
if name in by_match:
raise ValueError(f"profile {name!r} has more than one profile mapping")
by_match[name] = mapping
Expand Down Expand Up @@ -1662,7 +1650,7 @@ def generate_profiles_from_rules(
return profiles

def _defined_profiles(
self, definitions: list[ProfileDefinition], profile_config: dict
self, definitions: list[ProfileDefinition], profile_config: Profiles
) -> list[tuple[str, str | None, set[str]]]:
"""(title, description, profile names it selects) for each profile the benchmark's
page defines; a profile selects its own name and every ancestor's, by parent. The
Expand All @@ -1678,7 +1666,7 @@ def _defined_profiles(
f"profile {definition.title!r} extends {definition.parent!r}, which is not "
"one of the benchmark's profiles"
)
transform = TransformRegistry.get(profile_config.get("description_transform", "none"))
transform = TransformRegistry.get(profile_config.description_transform)

def parents(title: str) -> list[str]:
parent = by_title[title].parent
Expand All @@ -1696,8 +1684,8 @@ def parents(title: str) -> list[str]:
def _profiles_named_on_rules(
self,
recommendations: list["Recommendation"],
mappings: list[dict],
by_match: dict[str, dict],
mappings: list[ProfileMapping],
by_match: dict[str, ProfileMapping],
) -> list[tuple[str, str | None, set[str]]]:
"""(title, description, profile names it selects) for a copy saved without the
page's definitions: one entry per profile name its rules use, mapped names in the
Expand All @@ -1712,7 +1700,7 @@ def _profiles_named_on_rules(
def includes(name: str) -> list[str]:
if name not in by_match:
return []
listed = by_match[name].get("includes", [])
listed = by_match[name].includes
for included in listed:
if included not in by_match:
raise ValueError(
Expand All @@ -1723,14 +1711,10 @@ def includes(name: str) -> list[str]:
named = dict.fromkeys(name for rec in recommendations for name in rec.profiles)
entries = []
for mapping in mappings:
included = self._included_profiles(mapping["match"], includes)
if mapping["match"] in named:
included = self._included_profiles(mapping.match, includes)
if mapping.match in named:
entries.append(
(
mapping.get("title") or mapping["match"],
mapping.get("description") or None,
included,
)
(mapping.title or mapping.match, mapping.description or None, included)
)
entries.extend((name, None, {name}) for name in named if name not in by_match)
return entries
Expand Down
9 changes: 6 additions & 3 deletions src/cis_bench/exporters/style_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@ def _one_number_error(value: Any, handler: ValidatorFunctionWrapHandler) -> Any:

# Templates by the values the engine fills them with (_TEMPLATE_VALUES)
BenchmarkTemplate = Annotated[Template, _variables_in("benchmark")]
PlainTextTemplate = Annotated[Template, _variables_in("plain_text")]
PlainTextTemplateText = Annotated[TemplateText, _variables_in("plain_text")]
BenchmarkIdTemplate = Annotated[Template, _variables_in("benchmark_id")]
RefTemplate = Annotated[Template, _variables_in("ref")]
RuleTemplateText = Annotated[TemplateText, _variables_in("rule")]
Expand Down Expand Up @@ -697,7 +697,7 @@ class PlainText(_Section):
"""One plain-text element of the Benchmark."""

id: str
content: PlainTextTemplate = None
content: PlainTextTemplateText = ""


class ProfileMapping(_Section):
Expand Down Expand Up @@ -1202,7 +1202,10 @@ def _metadata_problems(
spec = mapping.metadata_spec
found: list[tuple[tuple[int | str, ...], str]] = []
prefix_place = (*place, "metadata_spec", "namespace_prefix")
namespace = self.benchmark.namespaces.get(spec.namespace_prefix)
namespaces = self.benchmark.namespaces
namespace = (
namespaces[spec.namespace_prefix] if spec.namespace_prefix in namespaces else None
)
if namespace is None:
found.append(
(prefix_place, f"{spec.namespace_prefix!r} is not declared in benchmark.namespaces")
Expand Down
20 changes: 15 additions & 5 deletions tests/regression/test_structure_compliance.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,17 @@ def test_disa_keeps_nist_controls_not_covered_by_ccis(self, sample_benchmark, tm
assert written > 0, "the fixture has NIST controls no CCI covers"

def test_the_benchmark_reference_takes_its_href_from_the_style(
self, sample_benchmark, disa_config_path
self, sample_benchmark, tmp_path, disa_config_path
):
from cis_bench.exporters.mapping_engine import MappingEngine

engine = MappingEngine(disa_config_path)
engine.config.benchmark["reference"]["attributes"]["href"] = (
"https://example.invalid/?from={benchmark.url}"
engine = MappingEngine(
_disa_with(
tmp_path,
disa_config_path,
"benchmark:\n reference:\n attributes:\n"
" href: 'https://example.invalid/?from={benchmark.url}'\n",
)
)

href, _ = engine.create_reference(sample_benchmark)
Expand Down Expand Up @@ -273,8 +277,14 @@ def test_a_style_with_dublin_core_elements_but_no_dc_namespace_is_refused(
# The elements need the namespace URI the style declares; there is no built-in one.
from cis_bench.exporters.mapping_engine import MappingEngine

# Built as the model: a style with dc_elements and no dc namespace is refused when it
# loads, so the export's own check is reached only this way.
engine = MappingEngine(disa_config_path)
del engine.config.benchmark["namespaces"]["dc"]
style = engine.config.style
namespaces = {p: uri for p, uri in style.benchmark.namespaces.items() if p != "dc"}
engine.config.style = style.model_copy(
update={"benchmark": style.benchmark.model_copy(update={"namespaces": namespaces})}
)

with pytest.raises(ValueError, match="dc"):
engine.create_reference(sample_benchmark)
Expand Down
5 changes: 2 additions & 3 deletions tests/unit/test_architecture_compliance.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,8 @@ def test_loop_driven_construction(self, mapping_engine_file):
"map_group() doesn't loop through config.group_elements"
)

assert "for element_name, element_config in self.config.benchmark.items()" in source, (
"map_benchmark() doesn't loop through config.benchmark"
)
# map_benchmark's loop over the benchmark elements is checked by what it writes: a
# title the style changes (test_unit_helpers TestBenchmarkElements)

def test_no_hard_coded_field_checks_in_new_methods(self, mapping_engine_file):
"""New methods must NOT check field names like 'if field_name == title'.
Expand Down
Loading
Loading