Skip to content

Commit a1a18c2

Browse files
committed
Allow for only id or base_profile_id in JSON
Add tests for the changes
1 parent b2f4779 commit a1a18c2

File tree

4 files changed

+49
-4
lines changed

4 files changed

+49
-4
lines changed

tests/utils/custom.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,24 @@
4040
"evaluate": true
4141
}
4242
}
43+
},
44+
{
45+
"id": "JSON_P12",
46+
"title": "JSON Custom profile",
47+
"rules": {
48+
"R3": {
49+
"evaluate": true
50+
}
51+
}
52+
},
53+
{
54+
"title": "JSON Custom profile with no given id",
55+
"base_profile_id": "P1",
56+
"rules": {
57+
"R3": {
58+
"evaluate": true
59+
}
60+
}
4361
}
4462
]
4563
}

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: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
import importlib
2+
import json
3+
import os.path
4+
import pathlib
5+
26
import pytest
37

48
NS = "http://checklists.nist.gov/xccdf/1.2"
@@ -94,3 +98,12 @@ def test_refine_rule():
9498
"'high'.")
9599
assert p.rule_refinements(fav, "severity") == "high"
96100
assert p.rule_refinements(fav, "role") == "full"
101+
102+
def test_no_id():
103+
p = autotailor.Profile()
104+
profile_dict = None
105+
file_path = pathlib.Path(__file__).parent.joinpath("custom_no_ids.json")
106+
with open(file_path) as fp:
107+
profile_dict = json.load(fp)
108+
with pytest.raises(ValueError):
109+
p.import_json_tailoring_profile(profile_dict)

utils/autotailor

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,8 @@ class Profile:
268268
def to_xml(self, root):
269269
profile = ET.SubElement(root, "{%s}Profile" % NS)
270270
profile.set("id", self._full_profile_id(self.profile_id))
271-
profile.set("extends", self._full_profile_id(self.extends))
271+
if self.extends:
272+
profile.set("extends", self._full_profile_id(self.extends))
272273

273274
# Title has to be there due to the schema definition.
274275
title = ET.SubElement(profile, "{%s}title" % NS)
@@ -285,9 +286,10 @@ class Profile:
285286
self.value_refinements_to_xml(profile)
286287

287288
def import_json_tailoring_profile(self, profile_dict):
288-
self.extends = profile_dict["base_profile_id"]
289-
290-
self.profile_id = profile_dict.get("id", self.profile_id)
289+
if not profile_dict.get("base_profile_id") and not profile_dict.get("id"):
290+
raise ValueError("You must define a base_profile_id or an id")
291+
self.extends = profile_dict.get("base_profile_id")
292+
self.profile_id = profile_dict.get("id")
291293
self.profile_title = profile_dict.get("title", self.profile_title)
292294

293295
self._import_groups_from_tailoring(profile_dict)

0 commit comments

Comments
 (0)