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
2 changes: 1 addition & 1 deletion tests/utils/autotailor_integration_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,4 @@ assert_exists 1 '/Benchmark/TestResult/rule-result[@idref="xccdf_com.example.www
# use JSON tailoring (P11) with a new profile from the command line
python3 $autotailor --id-namespace "com.example.www" --json-tailoring $json_tailoring --tailored-profile-id=CMDL_P --select R3 $ds $original_profile > $tailoring
$OSCAP xccdf eval --profile CMDL_P --progress --tailoring-file $tailoring --results $result $ds
assert_exists 1 '/Benchmark/TestResult/rule-result[@idref="xccdf_com.example.www_rule_R3"]/result[text()="pass"]'
assert_exists 1 '/Benchmark/TestResult/rule-result[@idref="xccdf_com.example.www_rule_R3"]/result[text()="pass"]'
18 changes: 18 additions & 0 deletions tests/utils/custom.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,24 @@
"evaluate": true
}
}
},
{
"id": "JSON_P12",
"title": "JSON Custom profile",
"rules": {
"R3": {
"evaluate": true
}
}
},
{
"title": "JSON Custom profile with no given id",
"base_profile_id": "P1",
"rules": {
"R3": {
"evaluate": true
}
}
}
]
}
12 changes: 12 additions & 0 deletions tests/utils/custom_no_ids.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"profiles": [
{
"title": "JSON Tailored Profile P11",
"rules": {
"R3": {
"evaluate": true
}
}
}
]
}
14 changes: 14 additions & 0 deletions tests/utils/test_autotailor.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import importlib
import pathlib
import json

import pytest

NS = "http://checklists.nist.gov/xccdf/1.2"
Expand Down Expand Up @@ -94,3 +97,14 @@ def test_refine_rule():
"'high'.")
assert p.rule_refinements(fav, "severity") == "high"
assert p.rule_refinements(fav, "role") == "full"

def test_no_id():
p = autotailor.Profile()
profile_dict = None
file_path = pathlib.Path(__file__).parent.joinpath("custom_no_ids.json")
with open(file_path) as fp:
json_data = json.load(fp)
profile_dict = json_data["profiles"][0]
with pytest.raises(ValueError) as e:
p.import_json_tailoring_profile(profile_dict)
Comment on lines +105 to +109
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like the test accidentally passes, but there is a problem that the JSON isn't the profile object, it's a dictionary where the profiles key contains a list of profile objects.

Suggested change
with open(file_path) as fp:
profile_dict = json.load(fp)
with pytest.raises(ValueError):
p.import_json_tailoring_profile(profile_dict)
with open(file_path) as fp:
json_data = json.load(fp)
profile_dict = json_data["profiles"][0]
with pytest.raises(ValueError) as e:
p.import_json_tailoring_profile(profile_dict)
assert str(e.value) == "You must define a base_profile_id or an id"

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

assert str(e.value) == "You must define a base_profile_id or an id"
10 changes: 6 additions & 4 deletions utils/autotailor
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,8 @@ class Profile:
def to_xml(self, root):
profile = ET.SubElement(root, "{%s}Profile" % NS)
profile.set("id", self._full_profile_id(self.profile_id))
profile.set("extends", self._full_profile_id(self.extends))
if self.extends:
profile.set("extends", self._full_profile_id(self.extends))

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

def import_json_tailoring_profile(self, profile_dict):
self.extends = profile_dict["base_profile_id"]

self.profile_id = profile_dict.get("id", self.profile_id)
if not profile_dict.get("base_profile_id") and not profile_dict.get("id"):
raise ValueError("You must define a base_profile_id or an id")
self.extends = profile_dict.get("base_profile_id")
self.profile_id = profile_dict.get("id")
self.profile_title = profile_dict.get("title", self.profile_title)

self._import_groups_from_tailoring(profile_dict)
Expand Down
Loading