Skip to content

Commit cd8b25b

Browse files
committed
Add alteratives to mandatory keys
Don't require profiles to have 'selections' if they have an 'extends'. This allows a profile to just extend a profile without having to add any selection. Making it possible to have non-versioned profiles to extend versioned profiles.
1 parent f7113b3 commit cd8b25b

2 files changed

Lines changed: 15 additions & 1 deletion

File tree

ssg/entities/common.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,8 @@ class XCCDFEntity(object):
153153

154154
MANDATORY_KEYS = set()
155155

156+
ALTERNATIVE_KEYS = dict()
157+
156158
GENERIC_FILENAME = ""
157159
ID_LABEL = "id"
158160

@@ -192,6 +194,8 @@ def process_input_dict(cls, input_contents, env_yaml, product_cpes=None):
192194
"""
193195
data = dict()
194196

197+
# Lets keep a list of the initial keys for alternative comparison
198+
initial_input_keys = input_contents.keys()
195199
for key, default in cls.KEYS.items():
196200
if key in input_contents:
197201
if input_contents[key] is not None:
@@ -201,6 +205,10 @@ def process_input_dict(cls, input_contents, env_yaml, product_cpes=None):
201205

202206
if key not in cls.MANDATORY_KEYS:
203207
data[key] = cls.KEYS[key]()
208+
elif key in cls.ALTERNATIVE_KEYS:
209+
if cls.ALTERNATIVE_KEYS[key] in initial_input_keys:
210+
data[key] = cls.KEYS[key]()
211+
continue
204212
else:
205213
msg = (
206214
"Key '{key}' is mandatory for definition of '{class_name}'."

ssg/entities/profile_base.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ class Profile(XCCDFEntity, SelectionHandler):
5555
"selections",
5656
}
5757

58+
ALTERNATIVE_KEYS = {
59+
"selections": "extends",
60+
}
61+
5862
@classmethod
5963
def process_input_dict(cls, input_contents, env_yaml, product_cpes):
6064
input_contents = super(Profile, cls).process_input_dict(input_contents, env_yaml)
@@ -245,7 +249,9 @@ def unselect_empty_groups(self, root_group):
245249
profile_rules = set(self.selected)
246250
is_empty, empty_groups = self._find_empty_groups(root_group, profile_rules)
247251
if is_empty:
248-
msg = "Profile {0} unselects all groups.".format(self.id_)
252+
msg = ("Profile {0} unselects all groups. "
253+
"Check whether it selects any rule or extends any profile."
254+
.format(self.id_))
249255
raise ValueError(msg)
250256
self.unselected_groups.extend(sorted(empty_groups))
251257

0 commit comments

Comments
 (0)