Skip to content

Commit

Permalink
fix: prevent duplicates in set-parameters (#1450)
Browse files Browse the repository at this point in the history
Signed-off-by: Lou DeGenaro <[email protected]>
  • Loading branch information
degenaro authored Sep 11, 2023
1 parent 420f341 commit 62e2f05
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
26 changes: 26 additions & 0 deletions tests/trestle/tasks/csv_to_oscal_cd_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,32 @@ def test_execute_add_rule(tmp_path: pathlib.Path) -> None:
assert len(component.control_implementations[1].set_parameters) == 1


def test_execute_param_duplicate_value(tmp_path: pathlib.Path) -> None:
"""Test execute param duplicate default value."""
_, section = _get_config_section_init(tmp_path, 'test-csv-to-oscal-cd-bp.config')
# duplicate default param default value
rows = _get_rows('tests/data/csv/bp.sample.v2.csv')
row = rows[3]
assert row[13] == 'allowed_admins_per_account'
assert row[15] == '10'
row = rows[2]
row[13] = 'allowed_admins_per_account'
row[15] = '10'
rows[2] = row
with mock.patch('trestle.tasks.csv_to_oscal_cd.csv.reader') as mock_csv_reader:
mock_csv_reader.return_value = rows
tgt = csv_to_oscal_cd.CsvToOscalComponentDefinition(section)
retval = tgt.execute()
assert retval == TaskOutcome.SUCCESS
row[15] = '11'
rows[2] = row
with mock.patch('trestle.tasks.csv_to_oscal_cd.csv.reader') as mock_csv_reader:
mock_csv_reader.return_value = rows
tgt = csv_to_oscal_cd.CsvToOscalComponentDefinition(section)
retval = tgt.execute()
assert retval == TaskOutcome.FAILURE


def test_execute_missing_param_default_value(tmp_path: pathlib.Path) -> None:
"""Test execute missing param default_value."""
_, section = _get_config_section_init(tmp_path, 'test-csv-to-oscal-cd-bp.config')
Expand Down
14 changes: 12 additions & 2 deletions trestle/tasks/csv_to_oscal_cd.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ def _calculate_set_params(self, mod_rules: List) -> tuple:
logger.debug(f'params mod: {key}')
else:
add_set_params.append(key)
logger.debug(f'prams add: {key}')
logger.debug(f'params add: {key}')
return (del_set_params, add_set_params, mod_set_params)

def _calculate_control_mappings(self, mod_rules: List) -> tuple:
Expand Down Expand Up @@ -824,7 +824,17 @@ class _OscalHelper():
@staticmethod
def add_set_parameter(set_parameter_list: List[SetParameter], set_parameter: SetParameter) -> None:
"""Add set parameter."""
set_parameter_list.append(set_parameter)
add = True
# don't add duplicate
for sp in set_parameter_list:
if sp.param_id == set_parameter.param_id:
add = False
if sp.values != set_parameter.values:
text = f'set-parameter id={sp.param_id} conflicting values'
raise RuntimeError(text)
break
if add:
set_parameter_list.append(set_parameter)

@staticmethod
def remove_rule_statement(statements: List[Statement], rule_id: str, smt_id: str) -> List[Statement]:
Expand Down

0 comments on commit 62e2f05

Please sign in to comment.