Skip to content

Commit ebf85bb

Browse files
authored
Merge pull request #14578 from macko1/fix_14516
GH-14516: Make compare_ds.py generate diffs for removed rules in DISA
2 parents ca0a2b4 + 469c2e3 commit ebf85bb

3 files changed

Lines changed: 22 additions & 16 deletions

File tree

docs/manual/developer/05_tools_and_utilities.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ To execute:
382382
### `utils/compare_ds.py` – Compare two data streams (can also compare XCCDFs)
383383

384384
This script compares two data streams or two benchmarks and generates a diff output.
385-
It can show what changed in rules, for example in description, references and remediation scripts.
385+
It shows what changed in rules, for example in description, references and remediation scripts.
386386
Changes in checks (OVAL and OCIL) are shown too, but the OVAL diff is limited to the `criteria`
387387
and `criterion` order and their IDs.
388388

@@ -396,16 +396,16 @@ diff for the whole data stream or benchmark.
396396
The option `--rule-diffs` can be used to generate a diff file per rule. In this mode the diff files
397397
are created in a directory: `./compare_ds-diffs`. To change the output dir use `--output-dir` option.
398398

399-
Compare current DISA's manual benchmark, and generate per file diffs:
399+
Compare two data streams and save the output to a file:
400400

401401
```bash
402-
$ utils/compare_ds.py --disa-content --rule-diffs ./disa-stig-rhel8-v1r6-xccdf-manual.xml shared/references/disa-stig-rhel8-v1r7-xccdf-manual.xml
402+
$ utils/compare_ds.py <source.xml> <target.xml> > content.diff
403403
```
404404

405-
Compare two data streams:
405+
Compare two DISA's benchmarks, and generate per rule diffs in the `compare_ds-diffs` directory:
406406
407407
```bash
408-
$ utils/compare_ds.py /tmp/ssg-rhel8-ds.xml build/ssg-rhel8-ds.xml > content.diff
408+
$ utils/compare_ds.py --disa-content --rule-diffs <source.xml> <target.xml>
409409
```
410410
411411
#### HTML Diffs
@@ -420,11 +420,12 @@ Install `diff2html`:
420420
$ sudo npm install -g diff2html-cli
421421
```
422422
423-
Generate the HTML diffs:
423+
Generate the HTML diffs in the `html` directory. Run the `utils/compare_ds.py` first to generate the diffs in the `compare_ds-diffs` directory.
424424
425425
```bash
426+
$ rm -r html/
426427
$ mkdir -p html
427-
$ for f in $(ls compare_ds-diffs/); do diff2html -i file -t $f -F "html/$f.html" "compare_ds-diffs/$f"; done
428+
$ for f in compare_ds-diffs/*; do name="${f##*/}"; diff2html -i file -t "$name" -F "html/$name.html" -- "$f" & done; wait
428429
```
429430
430431
### `utils/compare_results.py` &ndash; Compare to two ARF result files

ssg/content_diff.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,8 @@ def compare_rule_texts(self, old_rule, new_rule, identifier):
137137

138138
if old_rule_text == new_rule_text:
139139
return
140-
141-
if old_rule_text != "":
140+
# new_rule_text != "" to avoid printing the message when the rule is removed in the new content
141+
if old_rule_text != "" and new_rule_text != "":
142142
print(
143143
"New content has different text for rule '%s'." % (identifier))
144144

@@ -345,6 +345,10 @@ def compare_existing_rules(self, new_benchmark, old_benchmark,
345345
new_sv_rule_id = new_rule_mapping[old_sv_rule_id]
346346
except KeyError:
347347
print("%s is missing in new data stream." % old_stig_id)
348+
# Compare against empty rule so that a diff is generated for the removed rule
349+
if not self.only_rules and self.show_diffs:
350+
empty_rule = ssg.xml.XMLRule(ET.Element("{%s}Rule" % XCCDF12_NS))
351+
self.compare_rule(old_rule, empty_rule, old_stig_id)
348352
continue
349353
if self.only_rules:
350354
continue
@@ -362,7 +366,7 @@ def check_for_new_rules(self, rules_in_new_benchmark, old_rule_mapping):
362366
new_stig_id = self._get_stig_id(new_rule)
363367
new_sv_rule_id = self.get_stig_rule_SV(new_rule.get_attr("id"))
364368
try:
365-
old_sv_rule_id = old_rule_mapping[new_sv_rule_id] # noqa: F841
369+
_ = old_rule_mapping[new_sv_rule_id]
366370
except KeyError:
367371
print("%s was added in new data stream." % (new_stig_id))
368372

utils/compare_ds.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,20 @@ def parse_args():
2222
"--rule", metavar="RULE_ID",
2323
help="Compare only the rule specified by given RULE_ID"
2424
)
25-
parser.add_argument(
25+
diff_group = parser.add_mutually_exclusive_group()
26+
diff_group.add_argument(
2627
"--no-diffs", action="store_true",
2728
help="Do not perform detailed comparison of checks and remediations contents."
2829
)
30+
diff_group.add_argument(
31+
"--rule-diffs", action="store_true",
32+
help="Output diffs per rule, instead of a single diff. "
33+
"The rule diffs are output to directory './compare_ds-diffs/', override by --output-dir."
34+
)
2935
parser.add_argument(
3036
"--only-rules", action="store_true",
3137
help="Print only removals from rule set."
3238
)
33-
parser.add_argument(
34-
"--rule-diffs", action="store_true",
35-
help="Output diffs per rule, instead of a single diff. "
36-
"The rule diffs are output to directory './compare_ds-diffs/'."
37-
)
3839
parser.add_argument(
3940
"--output-dir", metavar="OUTPUT_DIR",
4041
type=str, action="store", default="./compare_ds-diffs",

0 commit comments

Comments
 (0)