Skip to content

Commit

Permalink
feat(authored): adds rules view creation for component definitions
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
jpower432 committed Oct 14, 2023
1 parent 8c4172f commit e60ebb5
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 356 deletions.
72 changes: 28 additions & 44 deletions tests/trestlebot/tasks/authored/test_compdef.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,12 @@
import pathlib

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 +40,39 @@ 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
rules_view_dir = trestle_root / RULES_VIEW_DIR
assert rules_view_dir.exists()

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
)
compdef_dir = rules_view_dir / test_comp
assert compdef_dir.exists()

comp_dir = compdef_dir / "test"
assert comp_dir.exists()

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)
yaml_files = list(comp_dir.glob(f"*{YAML_EXTENSION}"))
assert len(yaml_files) == 12

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

# 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

assert (
len(comp.components[1].control_implementations[0].implemented_requirements)
== 12
)
# Read one of the files and check the content
rule_path = pathlib.Path(yaml_files[0])
rule_stream = rule_path.read_text()

# Check existing component
assert comp.components[0] is not None
assert comp.components[0].control_implementations is not None
transformer = ToRulesYAMLTransformer()
rule = transformer.transform(rule_stream)

assert (
len(comp.components[0].control_implementations[0].implemented_requirements) == 2
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 e60ebb5

Please sign in to comment.