Skip to content

Commit

Permalink
fix - make status and mitre column optional (#1649)
Browse files Browse the repository at this point in the history
Signed-off-by: Lou DeGenaro <[email protected]>
  • Loading branch information
degenaro authored Aug 9, 2024
1 parent 050c425 commit 47e6936
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 10 deletions.
51 changes: 51 additions & 0 deletions tests/trestle/tasks/cis_xlsx_to_oscal_catalog_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
28 changes: 18 additions & 10 deletions trestle/tasks/cis_xlsx_to_oscal_catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down

0 comments on commit 47e6936

Please sign in to comment.