Coverage for postrfp/shared/expression.py: 86%
14 statements
« prev ^ index » next coverage.py v7.11.0, created at 2025-10-22 21:34 +0000
« prev ^ index » next coverage.py v7.11.0, created at 2025-10-22 21:34 +0000
1from typing import Any
2import logging
4from cel import evaluate
7log = logging.getLogger(__name__)
10def evaluate_expression(expression: str, context: dict[str, Any]) -> bool:
11 """
12 Evaluate a CEL policy against a given context.
14 @returns True if the policy evaluates to true, False otherwise (including on error).
15 """
16 if not expression or not expression.strip():
17 log.debug("Empty CEL expression, denying by default.")
18 return False
19 try:
20 result = evaluate(expression, context)
21 return bool(result)
22 except Exception as e:
23 log.error(f"Error evaluating policy '{expression}': {e}")
24 return False