From ddf75ed70a217ac972f665801a763163fdf78db6 Mon Sep 17 00:00:00 2001 From: Lou DeGenaro Date: Thu, 8 Aug 2024 13:29:48 -0400 Subject: [PATCH] fix - make status and mitre column optional Signed-off-by: Lou DeGenaro --- .../tasks/cis_xlsx_to_oscal_catalog_test.py | 51 +++++++++++++++++++ trestle/tasks/cis_xlsx_to_oscal_catalog.py | 28 ++++++---- 2 files changed, 69 insertions(+), 10 deletions(-) diff --git a/tests/trestle/tasks/cis_xlsx_to_oscal_catalog_test.py b/tests/trestle/tasks/cis_xlsx_to_oscal_catalog_test.py index 996a5ba25..cd1b1ec20 100644 --- a/tests/trestle/tasks/cis_xlsx_to_oscal_catalog_test.py +++ b/tests/trestle/tasks/cis_xlsx_to_oscal_catalog_test.py @@ -185,6 +185,57 @@ def test_cis_xlsx_to_oscal_catalog_unexpected_section(tmp_path: pathlib.Path): assert retval == TaskOutcome.FAILURE +def test_cis_xlsx_to_oscal_catalog_no_status(tmp_path: pathlib.Path): + """Test no column with name status.""" + folder = 'tests/data/tasks/cis-xlsx-to-oscal-catalog' + file_ = f'{folder}/CIS_RedHat_OpenShift_Container_Platform_Benchmark_v1.2.0-2.snippet.xlsx' + wb_hacked = load_workbook(file_) + sheet = wb_hacked['Combined Profiles'] + cell = sheet.cell(1, 5) + assert cell.value == 'status' + cell.value = 'X' + with mock.patch('trestle.tasks.cis_xlsx_to_oscal_catalog.load_workbook') as load_workbook_mock: + load_workbook_mock.return_value = wb_hacked + section = _get_section(tmp_path, ocp_config) + tgt = cis_xlsx_to_oscal_catalog.CisXlsxToOscalCatalog(section) + retval = tgt.execute() + assert retval == TaskOutcome.SUCCESS + + +def test_cis_xlsx_to_oscal_catalog_no_mitre(tmp_path: pathlib.Path): + """Test no column with name mitre.""" + folder = 'tests/data/tasks/cis-xlsx-to-oscal-catalog' + file_ = f'{folder}/CIS_RedHat_OpenShift_Container_Platform_Benchmark_v1.2.0-2.snippet.xlsx' + wb_hacked = load_workbook(file_) + sheet = wb_hacked['Combined Profiles'] + cell = sheet.cell(1, 20) + assert cell.value == 'MITRE ATT&CK Mappings' + cell.value = 'X' + with mock.patch('trestle.tasks.cis_xlsx_to_oscal_catalog.load_workbook') as load_workbook_mock: + load_workbook_mock.return_value = wb_hacked + section = _get_section(tmp_path, ocp_config) + tgt = cis_xlsx_to_oscal_catalog.CisXlsxToOscalCatalog(section) + retval = tgt.execute() + assert retval == TaskOutcome.SUCCESS + + +def test_cis_xlsx_to_oscal_catalog_no_v7_ig1(tmp_path: pathlib.Path): + """Test no column with name v7 IG1.""" + folder = 'tests/data/tasks/cis-xlsx-to-oscal-catalog' + file_ = f'{folder}/CIS_RedHat_OpenShift_Container_Platform_Benchmark_v1.2.0-2.snippet.xlsx' + wb_hacked = load_workbook(file_) + sheet = wb_hacked['Combined Profiles'] + cell = sheet.cell(1, 14) + assert cell.value == 'v7 IG1' + cell.value = 'Z' + with mock.patch('trestle.tasks.cis_xlsx_to_oscal_catalog.load_workbook') as load_workbook_mock: + load_workbook_mock.return_value = wb_hacked + section = _get_section(tmp_path, ocp_config) + tgt = cis_xlsx_to_oscal_catalog.CisXlsxToOscalCatalog(section) + retval = tgt.execute() + assert retval == TaskOutcome.SUCCESS + + def test_cis_xlsx_to_oscal_catalog_execute_rhel(tmp_path: pathlib.Path): """Test execute call - rhel.""" section = _get_section(tmp_path, rhel_config) diff --git a/trestle/tasks/cis_xlsx_to_oscal_catalog.py b/trestle/tasks/cis_xlsx_to_oscal_catalog.py index cc68c6ae2..65788dae0 100644 --- a/trestle/tasks/cis_xlsx_to_oscal_catalog.py +++ b/trestle/tasks/cis_xlsx_to_oscal_catalog.py @@ -281,19 +281,27 @@ def _create_property(self, name: str, value: str) -> Property: def _add_property(self, xlsx_helper: XlsxHelper, props: List[Property], row: int, key: str) -> None: """Add property.""" - name = self._get_normalized_name(key) - value = xlsx_helper.get(row, key) - if value: - props.append(self._create_property(name, value)) + try: + name = self._get_normalized_name(key) + value = xlsx_helper.get(row, key) + if value: + props.append(self._create_property(name, value)) + except KeyError as e: + text = f'key {e} not found.' + logger.debug(text) def _add_property_boolean(self, xlsx_helper: XlsxHelper, props: List[Property], row: int, key: str) -> None: """Add property.""" - name = self._get_normalized_name(key) - value = xlsx_helper.get(row, key) - if value: - props.append(self._create_property(name, 'True')) - else: - props.append(self._create_property(name, 'False')) + try: + name = self._get_normalized_name(key) + value = xlsx_helper.get(row, key) + if value: + props.append(self._create_property(name, 'True')) + else: + props.append(self._create_property(name, 'False')) + except KeyError as e: + text = f'key {e} not found.' + logger.debug(text) def _add_part(self, xlsx_helper: XlsxHelper, parts: List[Part], id_: str, row: int, key: str) -> None: """Add part."""