Skip to content

Commit

Permalink
PSCE-257: feat(authored): adds rules view creation for component defi…
Browse files Browse the repository at this point in the history
…nitions (#59)

* feat(authored): adds rules view creation for component definitions

Moves YAMLBuilder to compdef.py and changes to RuleViewBuilder
Removes csv_to_yaml because the functions used from it have been removed
Adds minor changes in yaml transformer comments and test

BREAKING CHANGE: This changes the outcome of create_new_default
method for component definitions from OSCAL JSON to the rules YAML.

Signed-off-by: Jennifer Power <[email protected]>

* test: alters test_compdef.py to make the file to test deterministic

Signed-off-by: Jennifer Power <[email protected]>

* chore: updates formatting on test_compdef.py

Signed-off-by: Jennifer Power <[email protected]>

---------

Signed-off-by: Jennifer Power <[email protected]>
  • Loading branch information
jpower432 authored Oct 16, 2023
1 parent d60adda commit 8261e91
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 357 deletions.
80 changes: 35 additions & 45 deletions tests/trestlebot/tasks/authored/test_compdef.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@
"""Test for Trestle Bot Authored Compdef."""

import pathlib
import re

import pytest
from trestle.common.model_utils import ModelUtils
from trestle.core.models.file_content_type import FileContentType
from trestle.oscal.component import ComponentDefinition

from tests import testutils
from trestlebot.const import RULES_VIEW_DIR, YAML_EXTENSION
from trestlebot.tasks.authored.base_authored import AuthoredObjectException
from trestlebot.tasks.authored.compdef import AuthoredComponentsDefinition
from trestlebot.transformers.yaml_transformer import ToRulesYAMLTransformer


test_prof = "simplified_nist_profile"
Expand All @@ -41,54 +41,44 @@ def test_create_new_default(tmp_trestle_dir: str) -> None:

authored_comp.create_new_default(test_prof, test_comp, "test", "My desc", "service")

comp, _ = ModelUtils.load_model_for_class(
trestle_root, test_comp, ComponentDefinition, FileContentType.JSON
)

assert comp is not None

assert comp.components is not None
assert comp.components[0] is not None
assert comp.components[0].control_implementations is not None

assert (
len(comp.components[0].control_implementations[0].implemented_requirements)
== 12
)


def test_create_new_default_existing(tmp_trestle_dir: str) -> None:
"""Test creating new default component in existing component definition"""
# Prepare the workspace
trestle_root = pathlib.Path(tmp_trestle_dir)
_ = testutils.setup_for_compdef(trestle_root, test_comp, "")
authored_comp = AuthoredComponentsDefinition(tmp_trestle_dir)

authored_comp.create_new_default(test_prof, test_comp, "test", "My desc", "service")
rules_view_dir = trestle_root / RULES_VIEW_DIR
assert rules_view_dir.exists()

comp, _ = ModelUtils.load_model_for_class(
trestle_root, test_comp, ComponentDefinition, FileContentType.JSON
)
compdef_dir = rules_view_dir / test_comp
assert compdef_dir.exists()

assert comp is not None
comp_dir = compdef_dir / "test"
assert comp_dir.exists()

# Check new component
assert comp.components is not None
assert comp.components[1] is not None
assert comp.components[1].control_implementations is not None
yaml_files = list(comp_dir.glob(f"*{YAML_EXTENSION}"))
assert len(yaml_files) == 12

assert (
len(comp.components[1].control_implementations[0].implemented_requirements)
== 12
rule_file = next(
(file for file in yaml_files if re.search(r"ac-5", file.stem)), None
)

# Check existing component
assert comp.components[0] is not None
assert comp.components[0].control_implementations is not None

assert (
len(comp.components[0].control_implementations[0].implemented_requirements) == 2
assert rule_file is not None

# Read one of the files and check the content
rule_path = pathlib.Path(rule_file)
rule_stream = rule_path.read_text()

transformer = ToRulesYAMLTransformer()
rule = transformer.transform(rule_stream)

assert rule.name == "rule-ac-5"
assert rule.description == "Rule for ac-5"
assert rule.component.name == "test"
assert rule.component.type == "service"
assert rule.component.description == "My desc"
assert rule.parameter is None
assert rule.profile.description == (
"NIST Special Publication 800-53 Revision 5 MODERATE IMPACT \
BASELINE"
)
assert rule.profile.href == "profiles/simplified_nist_profile/profile.json"
assert rule.profile.include_controls is not None
assert len(rule.profile.include_controls) == 1
assert rule.profile.include_controls[0].id == "ac-5"


def test_create_new_default_no_profile(tmp_trestle_dir: str) -> None:
Expand Down
98 changes: 0 additions & 98 deletions tests/trestlebot/transformers/test_csv_to_yaml.py

This file was deleted.

5 changes: 1 addition & 4 deletions tests/trestlebot/transformers/test_yaml_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,6 @@ def test_read_write_integration(test_rule: TrestleRule) -> None:
to_rules_transformer = ToRulesYAMLTransformer()

yaml_data = from_rules_transformer.transform(test_rule)

blob = yaml_data.getvalue()

read_rule = to_rules_transformer.transform(blob)
read_rule = to_rules_transformer.transform(yaml_data)

assert read_rule == test_rule
Loading

0 comments on commit 8261e91

Please sign in to comment.