Skip to content

Commit 927aeaf

Browse files
committed
Allow multiple control files to add same ref type
The build system is currently limited to a single ref type per control file. Two cotnrol files cannot add the same reference type. With this commit, the references added by the control file are tracked separately from the references loaded from the rule.yml. This allows us to differentiate references coming from the rule, and references coming from the control file.
1 parent 816ff35 commit 927aeaf

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

ssg/build_yaml.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -893,6 +893,7 @@ class Rule(XCCDFEntity, Templatable):
893893
rationale=lambda: "",
894894
severity=lambda: "",
895895
references=lambda: dict(),
896+
control_references=lambda: dict(),
896897
components=lambda: list(),
897898
identifiers=lambda: dict(),
898899
ocil_clause=lambda: None,
@@ -1212,16 +1213,23 @@ def _add_ident_elements(self, rule):
12121213
ident.set("system", SSG_IDENT_URIS[ident_type])
12131214
ident.text = ident_val
12141215

1215-
def add_extra_reference(self, ref_type, ref_value):
1216-
if ref_type in self.references:
1217-
if ref_value in self.references[ref_type]:
1216+
def add_control_reference(self, ref_type, ref_value):
1217+
if ref_type in self.control_references:
1218+
if ref_value in self.control_references[ref_type]:
12181219
msg = (
12191220
"Rule %s already contains a '%s' reference with value '%s'." % (
12201221
self.id_, ref_type, ref_value))
12211222
raise ValueError(msg)
1222-
self.references[ref_type].append(ref_value)
1223+
self.control_references[ref_type].append(ref_value)
12231224
else:
1224-
self.references[ref_type] = [ref_value]
1225+
self.control_references[ref_type] = [ref_value]
1226+
1227+
def merge_control_references(self):
1228+
for ref_type in self.control_references:
1229+
if ref_type in self.references:
1230+
self.references[ref_type].append(self.control_references[ref_type])
1231+
else:
1232+
self.references[ref_type] = self.control_references[ref_type]
12251233

12261234
def to_xml_element(self, env_yaml=None):
12271235
rule = ET.Element('{%s}Rule' % XCCDF12_NS)

ssg/controls.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ def add_references(self, reference_type, rules):
184184
if not rule:
185185
continue
186186
try:
187-
rule.add_extra_reference(reference_type, self.id)
187+
rule.add_control_reference(reference_type, self.id)
188188
except ValueError as exc:
189189
msg = (
190190
"Please remove any duplicate listing of rule '%s' in "
@@ -527,5 +527,14 @@ def save_everything(self, output_dir):
527527
policy.dump_yaml(filename)
528528

529529
def add_references(self, rules):
530+
# First we add all control references into a separate attribute
530531
for policy in self.policies.values():
531532
policy.add_references(rules)
533+
# Then we unify them under references attribute
534+
# This allows multiple control files to add references of the same type, and still track
535+
# what references already existed in the rule.
536+
self._merge_references(rules)
537+
538+
def _merge_references(self, rules):
539+
for rule in rules.values():
540+
rule.merge_control_references()

0 commit comments

Comments
 (0)