Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add alternatives to mandatory keys #11268

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
8 changes: 8 additions & 0 deletions ssg/entities/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ class XCCDFEntity(object):

MANDATORY_KEYS = set()

ALTERNATIVE_KEYS = dict()

GENERIC_FILENAME = ""
ID_LABEL = "id"

Expand Down Expand Up @@ -192,6 +194,8 @@ def process_input_dict(cls, input_contents, env_yaml, product_cpes=None):
"""
data = dict()

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

if key not in cls.MANDATORY_KEYS:
data[key] = cls.KEYS[key]()
elif key in cls.ALTERNATIVE_KEYS:
if cls.ALTERNATIVE_KEYS[key] in initial_input_keys:
data[key] = cls.KEYS[key]()
continue
else:
msg = (
"Key '{key}' is mandatory for definition of '{class_name}'."
Expand Down
8 changes: 7 additions & 1 deletion ssg/entities/profile_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ class Profile(XCCDFEntity, SelectionHandler):
"selections",
Copy link
Member Author

@yuumasato yuumasato Nov 10, 2023

Choose a reason for hiding this comment

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

@jan-cerny Another approach would be to remove selections from MANDATORY_KEYS, I don't think this would open gaps that could lead to problems. But the ALTERNATIVE_KEYS seem to guard and lead better to what is expected.

}

ALTERNATIVE_KEYS = {
"selections": "extends",
}

@classmethod
def process_input_dict(cls, input_contents, env_yaml, product_cpes):
input_contents = super(Profile, cls).process_input_dict(input_contents, env_yaml)
Expand Down Expand Up @@ -245,7 +249,9 @@ def unselect_empty_groups(self, root_group):
profile_rules = set(self.selected)
is_empty, empty_groups = self._find_empty_groups(root_group, profile_rules)
if is_empty:
msg = "Profile {0} unselects all groups.".format(self.id_)
msg = ("Profile {0} unselects all groups. "
"Check whether it selects any rule or extends any profile."
.format(self.id_))
raise ValueError(msg)
self.unselected_groups.extend(sorted(empty_groups))

Expand Down
Loading