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

fix: make status and mitre column optional #1649

Merged
merged 4 commits into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
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:
jpower432 marked this conversation as resolved.
Show resolved Hide resolved
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
Loading