-
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-245 - Add pydantic for basic data validation of Trestle Rule (#50)
* refactor: reworks csv_to_yaml for pydantic Update write empty keys to write default keys Create transformer for CSV reading and writing Moves CSV specific logic t csv_transformer Moves YAML specific logic to yaml_transformer and adds writing logic Signed-off-by: Jennifer Power <[email protected]> * docs: updates comment Signed-off-by: Jennifer Power <[email protected]> * docs: improves comment and variable naming Signed-off-by: Jennifer Power <[email protected]> * refactor: reduce extra code in RulesYAMLTransformer for simplicity Signed-off-by: Jennifer Power <[email protected]> * feat: adds Field aliases to remove underscores from YAML Signed-off-by: Jennifer Power <[email protected]> * refactor: breaks transformer into ToRuleTransformer and FromRuleTransformer To align with upstream trestle, the transformers are broken down into single responsibilties and importing the Transformer Base. Signed-off-by: Jennifer Power <[email protected]> --------- Signed-off-by: Jennifer Power <[email protected]>
- Loading branch information
Showing
19 changed files
with
759 additions
and
437 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
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: "invalid" | ||
default-value: true | ||
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
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: {'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
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,70 @@ | ||
#!/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 CSV Transformer.""" | ||
|
||
import csv | ||
import pathlib | ||
from typing import List | ||
|
||
import pytest | ||
|
||
from trestlebot.transformers.csv_transformer import CSVBuilder | ||
from trestlebot.transformers.trestle_rule import TrestleRule | ||
|
||
|
||
def test_csv_builder(test_rule: TrestleRule, tmp_trestle_dir: str) -> None: | ||
"""Test CSV builder on a happy path""" | ||
|
||
csv_builder = CSVBuilder() | ||
csv_builder.add_row(test_rule) | ||
|
||
assert len(csv_builder._rows) == 1 | ||
row = csv_builder._rows[0] | ||
assert row["Rule_Id"] == test_rule.name | ||
assert row["Rule_Description"] == test_rule.description | ||
assert row["Component_Title"] == test_rule.component.name | ||
assert row["Component_Type"] == test_rule.component.type | ||
assert row["Component_Description"] == test_rule.component.description | ||
assert row["Control_Id_List"] == "ac-1" | ||
assert row["Parameter_Id"] == test_rule.parameter.name # type: ignore | ||
assert row["Parameter_Description"] == test_rule.parameter.description # type: ignore | ||
assert row["Parameter_Value_Alternatives"] == "{}" | ||
assert row["Parameter_Value_Default"] == test_rule.parameter.default_value # type: ignore | ||
assert row["Profile_Description"] == test_rule.profile.description | ||
assert row["Profile_Source"] == test_rule.profile.href | ||
|
||
trestle_root = pathlib.Path(tmp_trestle_dir) | ||
tmp_csv_path = trestle_root.joinpath("test.csv") | ||
csv_builder.write_to_file(tmp_csv_path) | ||
|
||
assert tmp_csv_path.exists() | ||
|
||
first_row: List[str] = [] | ||
with open(tmp_csv_path, "r", newline="") as csvfile: | ||
csv_reader = csv.reader(csvfile) | ||
first_row = next(csv_reader) | ||
|
||
for column in csv_builder._csv_columns.get_required_column_names(): | ||
assert column in first_row | ||
|
||
|
||
def test_validate_row() -> None: | ||
"""Test validate row with an invalid row.""" | ||
row = {"Rule_Id": "test"} | ||
csv_builder = CSVBuilder() | ||
with pytest.raises(RuntimeError, match="Row missing key: *"): | ||
csv_builder.validate_row(row) |
Oops, something went wrong.