Skip to content

Commit 5884209

Browse files
committed
Backport Allow for only id or base_profile_id in JSON
Add tests for the changes Backport OpenSCAP#2318
1 parent a204589 commit 5884209

4 files changed

Lines changed: 30 additions & 2 deletions

File tree

tests/utils/autotailor_integration_test.sh

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ assert_exists 1 '/Benchmark/TestResult/rule-result[@idref="xccdf_com.example.www
8383
assert_exists 1 '/Benchmark/TestResult/rule-result[@idref="xccdf_com.example.www_rule_R4"]/result[text()="notchecked"]'
8484
assert_exists 1 '/Benchmark/TestResult/rule-result[@idref="xccdf_com.example.www_rule_R4" and @role="unchecked"]'
8585

86-
8786
# select additional rule R3; the customized profile will have a special profile ID
8887
customized_profile="xccdf_com.pink.elephant_profile_pineapple"
8988
python3 $autotailor --new-profile-id $customized_profile --id-namespace "com.example.www" --select R3 $ds $original_profile > $tailoring

tests/utils/custom_no_ids.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"profiles": [
3+
{
4+
"title": "JSON Tailored Profile P11",
5+
"rules": {
6+
"R3": {
7+
"evaluate": true
8+
}
9+
}
10+
}
11+
]
12+
}

tests/utils/test_autotailor.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
import importlib
2+
import pathlib
3+
import json
4+
25
import pytest
36

47
NS = "http://checklists.nist.gov/xccdf/1.2"
@@ -94,3 +97,14 @@ def test_refine_rule():
9497
"'high'.")
9598
assert t.rule_refinements(fav, "severity") == "high"
9699
assert t.rule_refinements(fav, "role") == "full"
100+
101+
def test_no_id():
102+
p = autotailor.Profile()
103+
profile_dict = None
104+
file_path = pathlib.Path(__file__).parent.joinpath("custom_no_ids.json")
105+
with open(file_path) as fp:
106+
json_data = json.load(fp)
107+
profile_dict = json_data["profiles"][0]
108+
with pytest.raises(ValueError) as e:
109+
p.import_json_tailoring_profile(profile_dict)
110+
assert str(e.value) == "You must define a base_profile_id or an id"

utils/autotailor

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,8 @@ class Tailoring:
254254
profile = ET.SubElement(root, "{%s}Profile" % NS)
255255
profile.set("id", self._full_profile_id(self.profile_id))
256256
profile.set("extends", self._full_profile_id(self.extends))
257+
if self.extends:
258+
profile.set("extends", self._full_profile_id(self.extends))
257259

258260
# Title has to be there due to the schema definition.
259261
title = ET.SubElement(profile, "{%s}title" % NS)
@@ -315,7 +317,8 @@ class Tailoring:
315317
raise ValueError("JSON Tailoring does not define any profiles.")
316318

317319
self.extends = tailoring["base_profile_id"]
318-
320+
if not tailoring.get("base_profile_id") and not tailoring.get("id"):
321+
raise ValueError("You must define a base_profile_id or an id")
319322
self.profile_id = tailoring.get("id", self.profile_id)
320323
self.profile_title = tailoring.get("title", self.profile_title)
321324

0 commit comments

Comments
 (0)