diff --git a/src/tirith/providers/json/handler.py b/src/tirith/providers/json/handler.py index ceba38c5..173e56d2 100644 --- a/src/tirith/providers/json/handler.py +++ b/src/tirith/providers/json/handler.py @@ -1,7 +1,7 @@ import pydash from typing import Callable, Dict, List -from ..common import create_result_dict +from ..common import create_result_dict, ProviderError class PydashPathNotFound: @@ -39,6 +39,16 @@ def get_value(provider_args: Dict, input_data: Dict) -> List[dict]: key_path: str = provider_args["key_path"] values = get_path_value_from_dict(key_path, input_data) + + if len(values) == 0: + severity_value = 2 + return [ + create_result_dict( + value=ProviderError(severity_value=severity_value), + err=f"key_path: `{key_path}` is not found (severity: {severity_value})", + ) + ] + outputs = [create_result_dict(value=value, meta=None, err=None) for value in values] return outputs diff --git a/tests/providers/json/policy.json b/tests/providers/json/policy.json index e18b662f..90ae1781 100644 --- a/tests/providers/json/policy.json +++ b/tests/providers/json/policy.json @@ -4,6 +4,18 @@ "required_provider": "stackguardian/json" }, "evaluators": [ + { + "id": "check0", + "provider_args": { + "operation_type": "get_value", + "key_path": "z.b" + }, + "condition": { + "type": "LessThanEqualTo", + "value": 1, + "error_tolerance": 2 + } + }, { "id": "check1", "provider_args": { diff --git a/tests/providers/json/test_get_value.py b/tests/providers/json/test_get_value.py new file mode 100644 index 00000000..7d754a37 --- /dev/null +++ b/tests/providers/json/test_get_value.py @@ -0,0 +1,16 @@ +import json +import os + +from tirith.core.core import start_policy_evaluation_from_dict + + +# TODO: Need to split this into multiple tests +def test_get_value(): + test_dir = os.path.dirname(os.path.realpath(__file__)) + with open(os.path.join(test_dir, "input.json")) as f: + input_data = json.load(f) + with open(os.path.join(test_dir, "policy.json")) as f: + policy = json.load(f) + + result = start_policy_evaluation_from_dict(policy, input_data) + assert result["final_result"] == True