Skip to content
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
8 changes: 5 additions & 3 deletions src/tirith/core/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,15 @@

if isinstance(evaluator_input["value"], ProviderError) and evaluator_input.get("err", None):
severity_value = evaluator_input["value"].severity_value
err_result = dict(message=evaluator_input["err"])

Check warning on line 85 in src/tirith/core/core.py

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Replace this constructor call with a literal.

See more on https://sonarcloud.io/project/issues?id=StackGuardian_policy-framework&issues=AaAUWKc9g4tNxaycoreG&open=AaAUWKc9g4tNxaycoreG&pullRequest=282

if severity_value > evaluator_error_tolerance:
err_result.update(dict(passed=False))

Check warning on line 88 in src/tirith/core/core.py

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Replace this constructor call with a literal.

See more on https://sonarcloud.io/project/issues?id=StackGuardian_policy-framework&issues=AaAUWKc9g4tNxaycoreH&open=AaAUWKc9g4tNxaycoreH&pullRequest=282
evaluation_results.append(err_result)
has_evaluation_passed = False
continue
# Mark as skipped evaluation
err_result.update(dict(passed=None))

Check warning on line 93 in src/tirith/core/core.py

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Replace this constructor call with a literal.

See more on https://sonarcloud.io/project/issues?id=StackGuardian_policy-framework&issues=AaAUWKc9g4tNxaycoreI&open=AaAUWKc9g4tNxaycoreI&pullRequest=282
evaluation_results.append(err_result)
has_evaluation_passed = None
continue
Expand All @@ -112,7 +112,7 @@
return result


def generate_compiled_code_without_none_and_variables(eval_str: str) -> Tuple[Optional[CodeType], List[str]]:

Check failure on line 115 in src/tirith/core/core.py

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor this function to reduce its Cognitive Complexity from 18 to the 15 allowed.

See more on https://sonarcloud.io/project/issues?id=StackGuardian_policy-framework&issues=AaAUWKc9g4tNxaycoreJ&open=AaAUWKc9g4tNxaycoreJ&pullRequest=282
# To make sure that the AST tree loop doesn't run forever
MAX_TRIES = 2000

Expand Down Expand Up @@ -214,7 +214,7 @@
for key in eval_id_values:
regex_string = "\\b" + key + "\\b"
eval_string = re.sub(regex_string, str(eval_id_values[key]), eval_string)
# eval_string = eval_string.replace(key, str(eval_id_values[key]["passed"]))

Check warning on line 217 in src/tirith/core/core.py

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Remove this commented out code.

See more on https://sonarcloud.io/project/issues?id=StackGuardian_policy-framework&issues=AaAUWKc-g4tNxaycoreK&open=AaAUWKc-g4tNxaycoreK&pullRequest=282
# print (eval_string)

# TODO: shall we use and, or and not instead of symbols?
Expand Down Expand Up @@ -245,7 +245,7 @@


def start_policy_evaluation(
policy_path: str, input_path: str, var_paths: List[str] = [], inline_vars: List[str] = []
policy_path: str, input_path: str, var_paths: Optional[List[str]] = None, inline_vars: Optional[List[str]] = None
) -> Dict:
"""
Start Tirith policy evaluation from policy file, input file, and optional variable files.
Expand All @@ -255,12 +255,14 @@
:param var_paths: List of paths to the variable files
:return: Policy evaluation result
"""
var_paths = var_paths or []
inline_vars = inline_vars or []
with open(policy_path) as f:
policy_data = json.load(f)
# TODO: validate policy_data against schema

with open(input_path) as f:
if input_path.endswith(".yaml") or input_path.endswith(".yml"):

Check warning on line 265 in src/tirith/core/core.py

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Replace chained "endswith" calls with a single call using a tuple argument.

See more on https://sonarcloud.io/project/issues?id=StackGuardian_policy-framework&issues=AaAUWKc-g4tNxaycoreL&open=AaAUWKc-g4tNxaycoreL&pullRequest=282
input_data = list(yaml.safe_load_all(f))
if len(input_data) == 1:
input_data = input_data[0]
Expand Down Expand Up @@ -304,8 +306,8 @@
return merged_var_dict


def start_policy_evaluation_from_dict(policy_dict: Dict, input_dict: Dict, var_dict: Dict = {}) -> Dict:
policy_dict, not_found_vars = get_policy_with_vars_replaced(policy_dict, var_dict)
def start_policy_evaluation_from_dict(policy_dict: Dict, input_dict: Dict, var_dict: Optional[Dict] = None) -> Dict:
policy_dict, not_found_vars = get_policy_with_vars_replaced(policy_dict, var_dict or {})
if not_found_vars:
return {"errors": [f"Variables not found: {', '.join(not_found_vars)}"]}

Expand Down
2 changes: 1 addition & 1 deletion src/tirith/core/evaluators/regex_match.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def evaluate(self, evaluator_input, evaluator_data):
evaluation_result = {"passed": False, "message": "Not evaluated"}
try:
match = 0
if type(evaluator_input) in (str, list, dict) and type(evaluator_data) == str:
if type(evaluator_input) in (str, list, dict) and type(evaluator_data) is str:
evaluator_input = str(evaluator_input)
match = re.search(evaluator_data, evaluator_input)
if match is None:
Expand Down