Skip to content

Remove required_ from core & added tests to ensure both are working #169

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/tirith/core/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,14 +274,14 @@ def start_policy_evaluation_from_dict(policy_dict: Dict, input_dict: Dict, var_d
eval_objects = policy_dict.get("evaluators")

final_evaluation_policy_string = policy_dict.get("eval_expression")
provider_module = policy_meta.get("required_provider", "core")
# TODO: Write functionality for dynamically importing evaluators from other modules.

provider_module = policy_meta.get("provider", policy_meta.get("required_provider", "core"))

eval_results = []
eval_results_obj = {}
for eval_obj in eval_objects:
eval_id = eval_obj.get("id")
eval_description = eval_obj.get("description")
logger.debug(f"Processing evaluator '{eval_id}'")
eval_result = generate_evaluator_result(eval_obj, input_dict, provider_module)
eval_result["id"] = eval_id
eval_result["description"] = eval_description
Expand All @@ -290,10 +290,11 @@ def start_policy_evaluation_from_dict(policy_dict: Dict, input_dict: Dict, var_d
final_evaluation_result, errors = final_evaluator(final_evaluation_policy_string, eval_results_obj)

final_output = {
"meta": {"version": policy_meta.get("version"), "required_provider": provider_module},
"meta": {"version": policy_meta.get("version"), "provider": provider_module},
"final_result": final_evaluation_result,
"evaluators": eval_results,
"errors": errors,
"eval_expression": final_evaluation_policy_string,
}
return final_output

55 changes: 54 additions & 1 deletion tests/core/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from pytest import mark

from tirith.core.core import final_evaluator

from tirith.core.core import start_policy_evaluation_from_dict

@mark.passing
def test_final_evaluator_skipped_check_should_be_removed():
Expand Down Expand Up @@ -38,3 +38,56 @@ def test_final_evaluator_malicious_eval_should_err():
"!skipped_check && passing_check || [].__class__.__base__", dict(skipped_check=None, passing_check=True)
)
assert actual_result == (False, ["The following symbols are not allowed: __class__, __base__"])


@mark.passing
def test_start_policy_evaluation_with_required_provider():
policy_dict = {
"meta": {"version": "1.0", "required_provider": "legacy_provider"},
"evaluators": [],
"eval_expression": "True",
}
input_dict = {}

result = start_policy_evaluation_from_dict(policy_dict, input_dict)

assert result["meta"]["provider"] == "legacy_provider"

@mark.passing
def test_start_policy_evaluation_with_provider():
policy_dict = {
"meta": {"version": "1.0", "provider": "new_provider"},
"evaluators": [],
"eval_expression": "True",
}
input_dict = {}

result = start_policy_evaluation_from_dict(policy_dict, input_dict)

assert result["meta"]["provider"] == "new_provider"

@mark.passing
def test_start_policy_evaluation_with_both_providers():
policy_dict = {
"meta": {"version": "1.0", "provider": "new_provider", "required_provider": "legacy_provider"},
"evaluators": [],
"eval_expression": "True",
}
input_dict = {}

result = start_policy_evaluation_from_dict(policy_dict, input_dict)

assert result["meta"]["provider"] == "new_provider"

@mark.passing
def test_start_policy_evaluation_with_neither_provider():
policy_dict = {
"meta": {"version": "1.0"},
"evaluators": [],
"eval_expression": "True",
}
input_dict = {}

result = start_policy_evaluation_from_dict(policy_dict, input_dict)

assert result["meta"]["provider"] == "core"
Loading