-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PSCE-237 - Adds Rule YAML to CSV transformation (#47)
* chore: WIP commit for rules transformation Signed-off-by: Jennifer Power <[email protected]> * chore: WIP commit with rules transform task type Signed-off-by: Jennifer Power <[email protected]> * chore: completion of initial CSV Builder Signed-off-by: Jennifer Power <[email protected]> * chore: adds config creation rule transform task Signed-off-by: Jennifer Power <[email protected]> * feat: adds rule YAML to OSCAL Component Definition transforms Completes transformation through CSV Adds RuleTransformer task type Signed-off-by: Jennifer Power <[email protected]> * docs: updates comments in RuleTransformTask Signed-off-by: Jennifer Power <[email protected]> * chore: moves TrestleRule dataclass to a new file Improve error handling Fixes my linting error Signed-off-by: Jennifer Power <[email protected]> * chore: adds mypy configutation to pyproject.toml Moves mypy configuration from Makefile to toml file Fixes addtional issues found by configuration changes Signed-off-by: Jennifer Power <[email protected]> --------- Signed-off-by: Jennifer Power <[email protected]>
- Loading branch information
Showing
18 changed files
with
1,031 additions
and
153 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
x-trestle-rule-info: | ||
name: example_rule_1 | ||
description: My rule description for example rule 1 | ||
parameter: | ||
name: prm_1 | ||
description: prm_1 description | ||
alternative-values: {'default': '5%', '5pc': '5%', '10pc': '10%', '15pc': '15%', '20pc': '20%'} | ||
default-value: '5%' | ||
profile: | ||
description: Simple NIST Profile | ||
href: profiles/simplified_nist_profile/profile.json | ||
include-controls: | ||
- id: ac-1 | ||
x-trestle-component-info: | ||
name: Component 1 | ||
description: Component 1 description | ||
type: service |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
x-trestle-rule-info: | ||
name: example_rule_2 | ||
description: My rule description for example rule 2 | ||
profile: | ||
description: Simple NIST Profile | ||
href: profiles/simplified_nist_profile/profile.json | ||
include-controls: | ||
- id: ac-1 | ||
x-trestle-component-info: | ||
name: Component 1 | ||
description: Component 1 description | ||
type: service |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
x-trestle-rule-info: | ||
description: My rule description for example rule 1 | ||
parameter: | ||
name: prm_1 | ||
description: prm_1 description | ||
alternative-values: {'default': '5%', '5pc': '5%', '10pc': '10%', '15pc': '15%', '20pc': '20%'} | ||
default-value: '5%' | ||
profile: | ||
description: Simple NIST Profile | ||
href: profiles/simplified_nist_profile/profile.json | ||
include-controls: | ||
- id: ac-2 | ||
x-trestle-component-info: | ||
name: Component 1 | ||
description: Component 1 description | ||
type: service |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
#!/usr/bin/python | ||
|
||
# Copyright 2023 Red Hat, Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); you may | ||
# not use this file except in compliance with the License. You may obtain | ||
# a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
# License for the specific language governing permissions and limitations | ||
# under the License. | ||
|
||
"""Test for Trestle Bot rule transform task""" | ||
|
||
import pathlib | ||
|
||
import pytest | ||
import trestle.oscal.component as osc_comp | ||
from trestle.common.err import TrestleNotFoundError | ||
from trestle.common.model_utils import ModelUtils | ||
from trestle.core.models.file_content_type import FileContentType | ||
from trestle.tasks.csv_to_oscal_cd import RULE_DESCRIPTION, RULE_ID | ||
|
||
from tests.testutils import setup_rules_view | ||
from trestlebot.tasks.base_task import TaskException | ||
from trestlebot.tasks.rule_transform_task import RuleTransformTask | ||
from trestlebot.transformers.yaml_to_csv import RulesYAMLTransformer | ||
|
||
|
||
test_comp = "test_comp" | ||
test_rules_dir = "test_rules_dir" | ||
|
||
|
||
def test_rule_transform_task(tmp_trestle_dir: str) -> None: | ||
"""Test rule transform task.""" | ||
trestle_root = pathlib.Path(tmp_trestle_dir) | ||
setup_rules_view(trestle_root, test_comp, test_rules_dir) | ||
transformer = RulesYAMLTransformer() | ||
rule_transform_task = RuleTransformTask( | ||
tmp_trestle_dir, test_rules_dir, transformer | ||
) | ||
return_code = rule_transform_task.execute() | ||
assert return_code == 0 | ||
|
||
# Check that the compdef model is unchanged | ||
orig_comp, _ = ModelUtils.load_model_for_class( | ||
trestle_root, test_comp, osc_comp.ComponentDefinition, FileContentType.JSON | ||
) | ||
|
||
assert orig_comp is not None | ||
assert orig_comp.metadata.title == "Component definition for test_comp" | ||
assert orig_comp.components is not None | ||
assert len(orig_comp.components) == 1 | ||
|
||
component = orig_comp.components[0] | ||
|
||
assert component.props is not None | ||
assert len(component.props) == 5 | ||
assert component.props[0].name == RULE_ID | ||
assert component.props[0].value == "example_rule_1" | ||
assert component.props[1].name == RULE_DESCRIPTION | ||
assert component.props[1].value == "My rule description for example rule 1" | ||
|
||
|
||
def test_rule_transform_task_with_no_rules(tmp_trestle_dir: str) -> None: | ||
"""Test rule transform task with no rules.""" | ||
trestle_root = pathlib.Path(tmp_trestle_dir) | ||
setup_rules_view(trestle_root, test_comp, test_rules_dir, skip_rules=True) | ||
transformer = RulesYAMLTransformer() | ||
rule_transform_task = RuleTransformTask( | ||
tmp_trestle_dir, test_rules_dir, transformer | ||
) | ||
|
||
with pytest.raises( | ||
TaskException, match="No rules found for component definition test_comp" | ||
): | ||
rule_transform_task.execute() | ||
|
||
|
||
def test_rule_transform_task_with_invalid_rule(tmp_trestle_dir: str) -> None: | ||
"""Test rule transform task with invalid rule.""" | ||
trestle_root = pathlib.Path(tmp_trestle_dir) | ||
setup_rules_view(trestle_root, test_comp, test_rules_dir, incomplete_rule=True) | ||
transformer = RulesYAMLTransformer() | ||
rule_transform_task = RuleTransformTask( | ||
tmp_trestle_dir, test_rules_dir, transformer | ||
) | ||
|
||
with pytest.raises( | ||
TaskException, match="Failed to transform rule .*: Missing key in YAML file: .*" | ||
): | ||
rule_transform_task.execute() | ||
|
||
|
||
def test_rule_transform_task_with_skip(tmp_trestle_dir: str) -> None: | ||
"""Test rule transform task with skip.""" | ||
trestle_root = pathlib.Path(tmp_trestle_dir) | ||
setup_rules_view(trestle_root, test_comp, test_rules_dir) | ||
transformer = RulesYAMLTransformer() | ||
rule_transform_task = RuleTransformTask( | ||
tmp_trestle_dir, test_rules_dir, transformer, skip_model_list=[test_comp] | ||
) | ||
return_code = rule_transform_task.execute() | ||
assert return_code == 0 | ||
|
||
# Check that the compdef model is not present | ||
with pytest.raises(TrestleNotFoundError): | ||
ModelUtils.load_model_for_class( | ||
trestle_root, test_comp, osc_comp.ComponentDefinition, FileContentType.JSON | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#!/usr/bin/python | ||
|
||
# Copyright 2023 Red Hat, Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); you may | ||
# not use this file except in compliance with the License. You may obtain | ||
# a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
# License for the specific language governing permissions and limitations | ||
# under the License. | ||
"""Test transformers package.""" |
Oops, something went wrong.