From 521a4dc346ba08096cc6b13b0c39ca0d7523cbdf Mon Sep 17 00:00:00 2001 From: Watson Sato Date: Fri, 10 Nov 2023 10:38:34 +0100 Subject: [PATCH] Add alternatives 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. --- ssg/entities/common.py | 8 ++++++++ ssg/entities/profile_base.py | 8 +++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/ssg/entities/common.py b/ssg/entities/common.py index 0a0ef7ea944..1fd14c57f40 100644 --- a/ssg/entities/common.py +++ b/ssg/entities/common.py @@ -153,6 +153,8 @@ class XCCDFEntity(object): MANDATORY_KEYS = set() + ALTERNATIVE_KEYS = dict() + GENERIC_FILENAME = "" ID_LABEL = "id" @@ -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: @@ -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}'." diff --git a/ssg/entities/profile_base.py b/ssg/entities/profile_base.py index 5c78de767ff..1d217a4cc9c 100644 --- a/ssg/entities/profile_base.py +++ b/ssg/entities/profile_base.py @@ -55,6 +55,10 @@ class Profile(XCCDFEntity, SelectionHandler): "selections", } + 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) @@ -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))